golden_brindle 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # A sample Gemfile
2
+ source "http://rubygems.org"
3
+ gem "unicorn"
4
+
5
+ group :development do
6
+ gem "rspec"
7
+ gem "yard"
8
+ gem "bundler"
9
+ gem "jeweler"
10
+ gem "rcov", ">= 0"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ kgio (2.6.0)
11
+ rack (1.3.2)
12
+ raindrops (0.7.0)
13
+ rake (0.9.2)
14
+ rcov (0.9.10)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.4)
20
+ rspec-expectations (2.6.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.6.0)
23
+ unicorn (4.1.1)
24
+ kgio (~> 2.4)
25
+ rack
26
+ raindrops (~> 0.6)
27
+ yard (0.7.2)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler
34
+ jeweler
35
+ rcov
36
+ rspec
37
+ unicorn
38
+ yard
data/Rakefile CHANGED
@@ -1,55 +1,42 @@
1
- require 'rubygems'
2
- require 'rake'
1
+ # encoding: utf-8
3
2
 
3
+ require 'rubygems'
4
+ require 'bundler'
4
5
  begin
5
- require 'jeweler'
6
- require File.dirname(__FILE__) + "/lib/golden_brindle/const.rb"
7
- Jeweler::Tasks.new do |gem|
8
- gem.name = "golden_brindle"
9
- gem.summary = %Q{Unicorn HTTP server multiple application runner tool}
10
- gem.description = %Q{Unicorn HTTP server multiple application runner tool}
11
- gem.email = "alex@simonov.me"
12
- gem.homepage = "http://github.com/simonoff/golden_brindle"
13
- gem.authors = ["Alexander Simonov"]
14
- gem.add_dependency "gem_plugin", ">= 0.2.3"
15
- gem.add_dependency "unicorn", ">= 1.00"
16
- gem.version = GoldenBrindle::Const::VERSION
17
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
- end
19
- Jeweler::GemcutterTasks.new
20
- rescue LoadError
21
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
22
11
  end
23
-
24
- require 'rake/testtask'
25
- Rake::TestTask.new(:test) do |test|
26
- test.libs << 'lib' << 'test'
27
- test.pattern = 'test/**/test_*.rb'
28
- test.verbose = true
12
+ require 'rake'
13
+ $LOAD_PATH.unshift('lib')
14
+ require 'jeweler'
15
+ require 'golden_brindle'
16
+ Jeweler::Tasks.new do |gem|
17
+ gem.name = "golden_brindle"
18
+ gem.summary = %Q{Unicorn HTTP server multiple application runner tool}
19
+ gem.description = %Q{Unicorn HTTP server multiple application runner tool}
20
+ gem.email = "alex@simonov.me"
21
+ gem.homepage = "http://github.com/simonoff/golden_brindle"
22
+ gem.authors = ["Alexander Simonov"]
23
+ gem.version = GoldenBrindle::Const::VERSION
24
+ gem.license = "GPL2"
29
25
  end
26
+ Jeweler::RubygemsDotOrgTasks.new
30
27
 
31
- begin
32
- require 'rcov/rcovtask'
33
- Rcov::RcovTask.new do |test|
34
- test.libs << 'test'
35
- test.pattern = 'test/**/test_*.rb'
36
- test.verbose = true
37
- end
38
- rescue LoadError
39
- task :rcov do
40
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
- end
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
42
32
  end
43
33
 
44
- task :test => :check_dependencies
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
45
38
 
46
- task :default => :test
39
+ task :default => :spec
47
40
 
48
- require 'rake/rdoctask'
49
- Rake::RDocTask.new do |rdoc|
50
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
- rdoc.rdoc_dir = 'rdoc'
52
- rdoc.title = "golden_brindle #{version}"
53
- rdoc.rdoc_files.include('README*')
54
- rdoc.rdoc_files.include('lib/**/*.rb')
55
- end
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/bin/golden_brindle CHANGED
@@ -1,12 +1,9 @@
1
1
  #!usr/bin/env ruby
2
2
  require 'rubygems'
3
- require 'gem_plugin'
4
3
 
5
4
  $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
6
5
  require 'golden_brindle'
7
6
 
8
- GemPlugin::Manager.instance.load "golden_brindle" => GemPlugin::INCLUDE
9
-
10
- if not GoldenBrindle::Command::Registry.instance.run ARGV
7
+ if not GoldenBrindle::Registry.run ARGV
11
8
  exit 1
12
9
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{golden_brindle}
8
- s.version = "0.2"
8
+ s.version = "0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexander Simonov"]
12
- s.date = %q{2011-09-03}
12
+ s.date = %q{2011-09-14}
13
13
  s.default_executable = %q{golden_brindle}
14
14
  s.description = %q{Unicorn HTTP server multiple application runner tool}
15
15
  s.email = %q{alex@simonov.me}
@@ -21,7 +21,10 @@ Gem::Specification.new do |s|
21
21
  ]
22
22
  s.files = [
23
23
  ".document",
24
+ ".rspec",
24
25
  "COPYING",
26
+ "Gemfile",
27
+ "Gemfile.lock",
25
28
  "LICENSE",
26
29
  "README.rdoc",
27
30
  "Rakefile",
@@ -30,20 +33,23 @@ Gem::Specification.new do |s|
30
33
  "bin/golden_brindle",
31
34
  "golden_brindle.gemspec",
32
35
  "lib/golden_brindle.rb",
33
- "lib/golden_brindle/cluster.rb",
36
+ "lib/golden_brindle/actions/cluster.rb",
37
+ "lib/golden_brindle/actions/configure.rb",
38
+ "lib/golden_brindle/actions/restart.rb",
39
+ "lib/golden_brindle/actions/start.rb",
40
+ "lib/golden_brindle/actions/stop.rb",
41
+ "lib/golden_brindle/base.rb",
34
42
  "lib/golden_brindle/command.rb",
35
- "lib/golden_brindle/configure.rb",
36
43
  "lib/golden_brindle/const.rb",
37
44
  "lib/golden_brindle/hooks.rb",
38
45
  "lib/golden_brindle/rails_support.rb",
39
- "lib/golden_brindle/restart.rb",
40
- "lib/golden_brindle/start.rb",
41
- "lib/golden_brindle/stop.rb",
46
+ "lib/golden_brindle/validations.rb",
42
47
  "resources/golden_brindle",
43
- "test/helper.rb",
44
- "test/test_golden_brindle.rb"
48
+ "spec/golden_brindle_spec.rb",
49
+ "spec/spec_helper.rb"
45
50
  ]
46
51
  s.homepage = %q{http://github.com/simonoff/golden_brindle}
52
+ s.licenses = ["GPL2"]
47
53
  s.require_paths = ["lib"]
48
54
  s.rubygems_version = %q{1.6.2}
49
55
  s.summary = %q{Unicorn HTTP server multiple application runner tool}
@@ -52,15 +58,27 @@ Gem::Specification.new do |s|
52
58
  s.specification_version = 3
53
59
 
54
60
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
- s.add_runtime_dependency(%q<gem_plugin>, [">= 0.2.3"])
56
- s.add_runtime_dependency(%q<unicorn>, [">= 1.00"])
61
+ s.add_runtime_dependency(%q<unicorn>, [">= 0"])
62
+ s.add_development_dependency(%q<rspec>, [">= 0"])
63
+ s.add_development_dependency(%q<yard>, [">= 0"])
64
+ s.add_development_dependency(%q<bundler>, [">= 0"])
65
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
66
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
67
  else
58
- s.add_dependency(%q<gem_plugin>, [">= 0.2.3"])
59
- s.add_dependency(%q<unicorn>, [">= 1.00"])
68
+ s.add_dependency(%q<unicorn>, [">= 0"])
69
+ s.add_dependency(%q<rspec>, [">= 0"])
70
+ s.add_dependency(%q<yard>, [">= 0"])
71
+ s.add_dependency(%q<bundler>, [">= 0"])
72
+ s.add_dependency(%q<jeweler>, [">= 0"])
73
+ s.add_dependency(%q<rcov>, [">= 0"])
60
74
  end
61
75
  else
62
- s.add_dependency(%q<gem_plugin>, [">= 0.2.3"])
63
- s.add_dependency(%q<unicorn>, [">= 1.00"])
76
+ s.add_dependency(%q<unicorn>, [">= 0"])
77
+ s.add_dependency(%q<rspec>, [">= 0"])
78
+ s.add_dependency(%q<yard>, [">= 0"])
79
+ s.add_dependency(%q<bundler>, [">= 0"])
80
+ s.add_dependency(%q<jeweler>, [">= 0"])
81
+ s.add_dependency(%q<rcov>, [">= 0"])
64
82
  end
65
83
  end
66
84
 
@@ -0,0 +1,50 @@
1
+ module GoldenBrindle
2
+
3
+ module Cluster
4
+ class Base < ::GoldenBrindle::Base
5
+ def configure
6
+ options [
7
+ ["-c", "--conf_path PATH", "Path to golden_brindle configuration files", :@cwd, "."],
8
+ ["-V", "", "Verbose output", :@verbose, false]
9
+ ]
10
+ end
11
+
12
+ def validate
13
+ @cwd = File.expand_path(@cwd)
14
+ valid_dir? @cwd, "Invalid path to golden_brindle configuration files: #{@cwd}"
15
+ @valid
16
+ end
17
+
18
+ def run
19
+ command = self.class.to_s.downcase.split('::')[1]
20
+ counter = 0
21
+ errors = 0
22
+ Dir.chdir @cwd do
23
+ Dir.glob("**/*.{yml,conf}").each do |conf|
24
+ cmd = "golden_brindle #{command} -C #{File.join(@cwd,conf)}"
25
+ cmd += " -d" if command == "start" #daemonize only when start
26
+ puts cmd if @verbose
27
+ output = `#{cmd}`
28
+ puts output if @verbose
29
+ status = $?.success?
30
+ puts "golden_brindle #{command} returned an error." unless status
31
+ status ? counter += 1 : errors += 1
32
+ end
33
+ end
34
+ puts "Success:#{counter}; Errors: #{errors}"
35
+ end
36
+ end
37
+ end
38
+
39
+ module Actions
40
+ module Cluster
41
+
42
+ class Start < ::GoldenBrindle::Cluster::Base; end
43
+
44
+ class Stop < ::GoldenBrindle::Cluster::Base; end
45
+
46
+ class Restart < ::GoldenBrindle::Cluster::Base; end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,63 @@
1
+ module GoldenBrindle
2
+ module Actions
3
+
4
+ class Configure < ::GoldenBrindle::Base
5
+
6
+ def configure
7
+ options [
8
+ ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
9
+ ["-d", "--daemonize", "Run daemonized in the background", :@daemon, false],
10
+ ["-b", "--bundler", "Use bundler to start unicorn instances", :@bundler, false],
11
+ ['', "--preload", "Preload application", :@preload, false],
12
+ ['-p', '--port PORT', "Which port to bind to (if set numbers of servers - start port number)", :@port, Unicorn::Const::DEFAULT_PORT],
13
+ ['-a', '--address ADDR', "Address to bind to", :@address, Unicorn::Const::DEFAULT_HOST],
14
+ ['-o', '--listen {HOST:PORT|PATH}',"listen on HOST:PORT or PATH, separated by comma ", :@listen, nil] ,
15
+ ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/unicorn.log"],
16
+ ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "tmp/pids/unicorn.pid"],
17
+ ['-n', '--num-workers INT', "Number of Unicorn workers", :@workers, 4],
18
+ ['-N', '--num-servers INT', "Number of Unicorn listen records", :@servers, 1],
19
+ ['-t', '--timeout INT', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60],
20
+ ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
21
+ ['-D', '--debug', "Enable debugging mode", :@debug, false],
22
+ ['-C', '--config PATH', "Path to brindle configuration file", :@config_file, "config/brindle.yml"],
23
+ ['-S', '--script PATH', "Load the Unicorn-specific config file", :@config_script, nil],
24
+ ['', '--user USER', "User to run as", :@user, nil],
25
+ ['', '--group GROUP', "Group to run as", :@group, nil],
26
+ ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil]
27
+ ]
28
+ end
29
+
30
+ def validate
31
+
32
+ valid_dir? File.dirname(@config_file), "Path to config file not valid: #{@config_file}"
33
+
34
+ if @config_script
35
+ valid_exists?(@config_script, "Unicorn-specific config file not there: #{@config_script}")
36
+ return false unless @valid
37
+ end
38
+
39
+ valid?(@prefix[0] == ?/ && @prefix[-1] != ?/, "Prefix must begin with / and not end in /") if @prefix
40
+ valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
41
+ valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
42
+ valid_user? @user if @user
43
+ valid_group? @group if @group
44
+ @valid
45
+ end
46
+
47
+ def run
48
+
49
+ file_options = {}
50
+
51
+ self.config_keys.each do |key|
52
+ key_val = self.instance_variable_get "@#{key}"
53
+ file_options[key] = key_val unless key_val.nil?
54
+ end
55
+
56
+ $stdout.puts "Writing configuration file to #{@config_file}."
57
+ File.open(@config_file,"w") {|f| f.write(file_options.to_yaml)}
58
+
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,40 @@
1
+ module GoldenBrindle
2
+ module Actions
3
+
4
+ class Restart < ::GoldenBrindle::Base
5
+
6
+ def configure
7
+ options [
8
+ ['-c', '--chdir PATH', "Change to dir before starting (will be expanded).", :@cwd, "."],
9
+ ['-C', '--config PATH', "Use a mongrel based config file", :@config_file, nil],
10
+ ['-s', '--soft', "Do a soft restart rather than a process exit restart", :@soft, false],
11
+ ['-P', '--pid FILE', "Where the PID file is located.", :@pid_file, "tmp/pids/unicorn.pid"]
12
+ ]
13
+ end
14
+
15
+ def validate
16
+ if @config_file
17
+ valid_exists?(@config_file, "Config file not there: #{@config_file}")
18
+ return false unless @valid
19
+ @config_file = File.expand_path(@config_file)
20
+ load_config
21
+ return false unless @valid
22
+ end
23
+
24
+ @cwd = File.expand_path(@cwd)
25
+ valid_dir? @cwd, "Invalid path to application dir: #{@cwd}"
26
+ valid_exists? File.join(@cwd,@pid_file), "PID file #{@pid_file} does not exist. Not running?"
27
+ @valid
28
+ end
29
+
30
+ def run
31
+ if @soft
32
+ GoldenBrindle::send_signal("HUP", File.join(@cwd,@pid_file))
33
+ else
34
+ GoldenBrindle::send_signal("USR2", File.join(@cwd,@pid_file))
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,131 @@
1
+ module GoldenBrindle
2
+
3
+ module Actions
4
+
5
+ class Start < ::GoldenBrindle::Base
6
+ include ::GoldenBrindle::Hooks
7
+
8
+ def configure
9
+ options [
10
+ ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
11
+ ["-b", "--bundler", "Use bundler to start unicorn instances", :@bundler, false],
12
+ ["-d", "--daemonize", "Run daemonized in the background", :@daemon, false],
13
+ ['', "--preload", "Preload application", :@preload, false],
14
+ ['-p', '--port PORT', "Which port to bind to (if set numbers of servers - start port number)", :@port, Unicorn::Const::DEFAULT_PORT],
15
+ ['-a', '--address ADDR', "Address to bind to", :@address, Unicorn::Const::DEFAULT_HOST],
16
+ ['-o', '--listen {HOST:PORT|PATH}',"listen on HOST:PORT or PATH, separated by comma (default: #{Unicorn::Const::DEFAULT_LISTEN})", :@listen, Unicorn::Const::DEFAULT_LISTEN],
17
+ ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/unicorn.log"],
18
+ ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "tmp/pids/unicorn.pid"],
19
+ ['-n', '--num-workers INT', "Number of Unicorn workers", :@workers, 4],
20
+ ['-N', '--num-servers INT', "Number of Unicorn listen records", :@servers, 1],
21
+ ['-t', '--timeout INT', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60],
22
+ ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
23
+ ['-D', '--debug', "Enable debugging mode", :@debug, false],
24
+ ['-C', '--config PATH', "Use a mongrel based config file", :@config_file, nil],
25
+ ['-S', '--script PATH', "Load the Unicorn-specific config file", :@config_script, nil],
26
+ ['', '--user USER', "User to run as", :@user, nil],
27
+ ['', '--group GROUP', "Group to run as", :@group, nil],
28
+ ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil]
29
+ ]
30
+ end
31
+
32
+ def validate
33
+ if @config_file
34
+ valid_exists?(@config_file, "Config file not there: #@config_file")
35
+ return false unless @valid
36
+ @config_file = File.expand_path(@config_file)
37
+ load_config
38
+ return false unless @valid
39
+ end
40
+
41
+ if @config_script
42
+ valid_exists?(@config_script, "Unicorn-specific config file not there: #@config_script")
43
+ return false unless @valid
44
+ end
45
+
46
+ if @bundler
47
+ valid_exists?('Gemfile', "Cannot use Bundler - no Gemfile in your application root dir")
48
+ return false unless @valid
49
+ end
50
+
51
+ @cwd = File.expand_path(@cwd)
52
+ valid_dir? @cwd, "Invalid path to change to during daemon mode: #{@cwd}"
53
+ valid?(@prefix[0] == ?/ && @prefix[-1] != ?/, "Prefix must begin with / and not end in /") if @prefix
54
+ valid_dir? File.dirname(File.join(@cwd,@log_file)), "Path to log file not valid: #{@log_file}"
55
+ valid_dir? File.dirname(File.join(@cwd,@pid_file)), "Path to pid file not valid: #{@pid_file}"
56
+ valid_user? @user if @user
57
+ valid_group? @group if @group
58
+ @valid
59
+ end
60
+
61
+ def default_options
62
+ {
63
+ :listeners => [],
64
+ :pid => @pid_file,
65
+ :config_file => @config_script,
66
+ :worker_processes => @workers.to_i,
67
+ :working_directory => @cwd,
68
+ :timeout => @timeout.to_i
69
+ }
70
+ end
71
+
72
+ def bundler_cmd
73
+ cmd = "bundle exec #{@opt.program_name}"
74
+ @original_args.each_slice(2) do |arg_key,value|
75
+ cmd << " #{arg_key} #{value}" if arg_key != "-b"
76
+ end
77
+ cmd
78
+ end
79
+
80
+ def collect_listeners
81
+ return if @port.nil?
82
+ start_port = end_port = nil
83
+ start_port ||= @port.to_i
84
+ end_port ||= start_port + @servers.to_i - 1
85
+ (start_port..end_port).map do |port|
86
+ "#{@address}:#{port}"
87
+ end
88
+ end
89
+
90
+ def parse_listen_option
91
+ return if @listen.nil? || @listen.empty?
92
+ @listen.split(',').map do |listen|
93
+ listen = File.join(@cwd,listen) if listen[0..0] != "/" && !listen.match(/\w+\:\w+/)
94
+ "#{listen}"
95
+ end
96
+ end
97
+
98
+ def run
99
+ # Change there to start, then we'll have to come back after daemonize
100
+ Dir.chdir(@cwd)
101
+ if @bundler
102
+ puts "Using Bundler"
103
+ puts "reexec via bundle exec"
104
+ exec(bundler_cmd)
105
+ end
106
+ options = default_options
107
+ # set user via Unicorn options. If we don't set group - then use only user
108
+ options[:user] = @user unless @user.nil?
109
+ options[:stderr_path] = options[:stdout_path] = @log_file if @daemon
110
+ options[:preload_app] = @preload
111
+ options.merge!(collect_hooks)
112
+ ENV['RAILS_ENV'] = @environment
113
+ ENV['RAILS_RELATIVE_URL_ROOT'] = @prefix
114
+ [collect_listeners, parse_listen_option].each do |listeners|
115
+ options[:listeners] += listeners if listeners
116
+ end
117
+ app = ::GoldenBrindle::RailsSupport.rails_builder(@daemon)
118
+ if @daemon
119
+ Unicorn::Launcher.daemonize!(options)
120
+ end
121
+ puts "start Unicorn v#{Unicorn::Const::UNICORN_VERSION}..."
122
+ if Unicorn.respond_to?(:run)
123
+ Unicorn.run(app, options)
124
+ else
125
+ Unicorn::HttpServer.new(app, options).start.join
126
+ end
127
+ end
128
+ end
129
+
130
+ end
131
+ end