mongrel_jcluster 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,15 @@
1
+ == Mongrel JCluster Plugin
2
+
3
+ Tool to help start/stop/restart multiple mongrel servers in the same JVM using JRuby to use behind a load balancer like Apache 2.2 (mod_proxy_balancer), Lighttpd, Pound, Pen or Balance. This plugin adds an option to specify a number of Mongrel servers to launch, a range of ports, and a configuration file for the cluster. Use "-h" to see command syntax.
4
+
5
+ Configure cluster and write configuration file:
6
+ mongrel_rails jcluster::configure
7
+
8
+ Start cluster:
9
+ mongrel_rails jcluster::start
10
+
11
+ Restart cluster:
12
+ mongrel_rails jcluster::restart
13
+
14
+ Stop cluster:
15
+ mongrel_rails jcluster::stop
@@ -0,0 +1,53 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'tools/rakehelp'
7
+ require 'fileutils'
8
+ include FileUtils
9
+
10
+ setup_tests
11
+ setup_clean ["pkg", "lib/*.bundle", "*.gem", ".config"]
12
+
13
+ setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']
14
+
15
+ desc "Does a full compile, test run"
16
+ task :default => [:test, :package]
17
+
18
+ version="0.0.1"
19
+ name="mongrel_jcluster"
20
+
21
+ setup_gem(name, version) do |spec|
22
+ spec.summary = "Mongrel plugin that provides commands for managing multiple Mongrel processes in a JVM."
23
+ spec.description = spec.summary
24
+ spec.author="Bradley Taylor & Ola Bini"
25
+ spec.add_dependency('gem_plugin', '>= 0.2.1')
26
+ spec.add_dependency('mongrel', '>= 0.3.13.4')
27
+ spec.files += Dir.glob("resources/**/*")
28
+ spec.has_rdoc = false
29
+ spec.files += Dir.glob("bin/*")
30
+ spec.files += Dir.glob("examples/*")
31
+ spec.default_executable = "mongrel_jcluster_ctl"
32
+ spec.executables = ["mongrel_jcluster_ctl"]
33
+ end
34
+
35
+
36
+ task :install => [:test, :package] do
37
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
38
+ end
39
+
40
+ task :uninstall => [:clean] do
41
+ sh %{sudo gem uninstall #{name}}
42
+ end
43
+
44
+ task :gem_source do
45
+ mkdir_p "pkg/gems"
46
+
47
+ FileList["**/*.gem"].each { |gem| mv gem, "pkg/gems" }
48
+ FileList["pkg/*.tgz"].each {|tgz| rm tgz }
49
+ rm_rf "pkg/#{name}-#{version}"
50
+
51
+ sh %{ generate_yaml_index.rb -d pkg }
52
+ end
53
+
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env jruby
2
+ # Copyright (c) 2006 Bradley Taylor, bradley@fluxura.com
3
+
4
+ require 'optparse'
5
+
6
+ def run(command, verbose)
7
+ Dir.chdir @options[:conf_path] do
8
+ confs = Dir.glob("*.yml")
9
+ confs += Dir.glob("*.conf")
10
+ confs.each do |conf|
11
+ cmd = "jruby -S mongrel_rails jcluster::#{command} -c #{conf}"
12
+ cmd += " -v" if verbose
13
+ puts cmd if verbose
14
+ output = `sh -c '#{cmd}'`
15
+ puts output if verbose
16
+ puts "mongrel_rails jcluster::#{command} returned an error." unless $?.success?
17
+ end
18
+ end
19
+ end
20
+
21
+ @options = {}
22
+ @options[:conf_path] = "/etc/mongrel_jcluster"
23
+ @options[:verbose] = false
24
+
25
+ OptionParser.new do |opts|
26
+ opts.banner = "Usage: #{$0} (start|stop|restart) [options]"
27
+
28
+ opts.on("-c", "--conf_path PATH", "Path to mongrel_jcluster configuration files") { |value| @options[:conf_path] = value }
29
+ opts.on('-v', '--verbose', "Print all called commands and output.") { |value| @options[:verbose] = value }
30
+
31
+ if ARGV.empty?
32
+ puts opts
33
+ exit
34
+ else
35
+ @cmd = opts.parse!(ARGV)
36
+ if @cmd.nil?
37
+ puts opts
38
+ exit
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ if @options[:conf_path] == nil && !File.directory?(@options[:conf_path])
45
+ puts "Invalid path to mongrel_jcluster configuration files: #{@options[:conf_path]}"
46
+ exit
47
+ end
48
+
49
+ case @cmd[0]
50
+ when "start":
51
+ puts "Starting all mongrel_jclusters..."
52
+ run "start", @options[:verbose]
53
+ when "stop":
54
+ puts "Stopping all mongrel_jclusters..."
55
+ run "stop", @options[:verbose]
56
+ when "restart":
57
+ puts "Restarting all mongrel_jclusters..."
58
+ run "stop", @options[:verbose]
59
+ run "start", @options[:verbose]
60
+ else
61
+ puts "Unknown command."
62
+ end
63
+
64
+ exit
@@ -0,0 +1,192 @@
1
+ require 'gem_plugin'
2
+ require 'mongrel'
3
+ require 'yaml'
4
+
5
+ module JCluster
6
+
7
+ module ExecBase
8
+ include Mongrel::Command::Base
9
+
10
+ def validate
11
+ valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
12
+ return @valid
13
+ end
14
+
15
+ def read_options
16
+ @options = {
17
+ "environment" => ENV['RAILS_ENV'] || "development",
18
+ "port" => 3000,
19
+ "pid_file" => "log/mongrel.pid",
20
+ "servers" => 2,
21
+ "jport" => 19222,
22
+ "jkey" => "aggfeeeeeeeeeeeeeergergwer35t45t327283428738fewgfygdsf8yesr834"
23
+ }
24
+ conf = YAML.load_file(@config_file)
25
+ @options.merge! conf if conf
26
+ end
27
+
28
+ def start
29
+ read_options
30
+ port = @options["port"].to_i - 1
31
+ pid = @options["pid_file"].split(".")
32
+ puts "Starting JRuby server..."
33
+ system("sh -c 'jrubysrv -p #{@options["jport"]} -k #{@options["jkey"]} </dev/null >jrubyserver.stdout.log 2>jrubyserver.stderr.log &'")
34
+ sleep 1
35
+ puts "Starting #{@options["servers"]} Mongrel servers..."
36
+ 1.upto(@options["servers"].to_i) do |i|
37
+ argv = [ "mongrel_rails" ]
38
+ argv << "start"
39
+ argv << "-e #{@options["environment"]}" if @options["environment"]
40
+ argv << "-p #{port+i}"
41
+ argv << "-a #{@options["address"]}" if @options["address"]
42
+ argv << "-l #{@options["log_file"]}" if @options["log_file"]
43
+ argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
44
+ argv << "-c #{@options["cwd"]}" if @options["cwd"]
45
+ argv << "-t #{@options["timeout"]}" if @options["timeout"]
46
+ argv << "-m #{@options["mime_map"]}" if @options["mime_map"]
47
+ argv << "-r #{@options["docroot"]}" if @options["docroot"]
48
+ argv << "-n #{@options["num_procs"]}" if @options["num_procs"]
49
+ argv << "-B" if @options["debug"]
50
+ argv << "-S #{@options["config_script"]}" if @options["config_script"]
51
+ argv << "--user #{@options["user"]}" if @options["user"]
52
+ argv << "--group #{@options["group"]}" if @options["group"]
53
+ argv << "--prefix #{@options["prefix"]}" if @options["prefix"]
54
+ cmd = argv.join " "
55
+
56
+ puts cmd if @verbose
57
+ output = `sh -c 'jrubycli -p #{@options["jport"]} -k #{@options["jkey"]} -S #{cmd}'`
58
+ unless $?.success?
59
+ puts cmd unless @verbose
60
+ puts output
61
+ end
62
+ end
63
+ end
64
+
65
+ def stop
66
+ read_options
67
+ port = @options["port"].to_i - 1
68
+ pid = @options["pid_file"].split(".")
69
+ puts "Stopping #{@options["servers"]} Mongrel servers..."
70
+ output = `sh -c 'jrubycli -p #{@options["jport"]} -k #{@options["jkey"]} -t'`
71
+ unless $?.success?
72
+ puts cmd unless @verbose
73
+ puts output
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ class Start < GemPlugin::Plugin "/commands"
80
+ include ExecBase
81
+
82
+ def configure
83
+ options [
84
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_jcluster.yml"],
85
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false]
86
+ ]
87
+ end
88
+
89
+ def run
90
+ start
91
+ end
92
+ end
93
+
94
+ class Stop < GemPlugin::Plugin "/commands"
95
+ include ExecBase
96
+
97
+ def configure
98
+ options [
99
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_jcluster.yml"],
100
+ ['-f', '--force', "Force the shutdown.", :@force, false],
101
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false]
102
+ ]
103
+ end
104
+
105
+ def run
106
+ stop
107
+ end
108
+ end
109
+
110
+ class Restart < GemPlugin::Plugin "/commands"
111
+ include ExecBase
112
+
113
+ def configure
114
+ options [
115
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_jcluster.yml"],
116
+ ['-f', '--force', "Force the shutdown.", :@force, false],
117
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false]
118
+ ]
119
+ end
120
+
121
+ def run
122
+ stop
123
+ start
124
+ end
125
+
126
+ end
127
+
128
+ class Configure < GemPlugin::Plugin "/commands"
129
+ include Mongrel::Command::Base
130
+
131
+ def configure
132
+ options [
133
+ ["-e", "--environment ENV", "Rails environment to run as", :@environment, nil],
134
+ ['-p', '--port PORT', "Starting port to bind to", :@port, 3000],
135
+ ['-a', '--address ADDR', "Address to bind to", :@address, nil],
136
+ ['-l', '--log FILE', "Where to write log messages", :@log_file, nil],
137
+ ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
138
+ ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, nil],
139
+ ['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, nil],
140
+ ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
141
+ ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, nil],
142
+ ['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, nil],
143
+ ['-B', '--debug', "Enable debugging mode", :@debug, nil],
144
+ ['-S', '--script PATH', "Load the given file as an extra config script.", :@config_script, nil],
145
+ ['-N', '--num-servers INT', "Number of Mongrel servers", :@servers, 2],
146
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_jcluster.yml"],
147
+ ['-R', '--jport PORT', "Local port where JRuby server should run", :@jport, 19222],
148
+ ['-K', '--jkey KEY', "Key to use for connecting to JRuby server", :@jkey, nil],
149
+ ['', '--user USER', "User to run as", :@user, nil],
150
+ ['', '--group GROUP', "Group to run as", :@group, nil],
151
+ ['', '--prefix PREFIX', "Rails prefix to use", :@prefix, nil]
152
+ ]
153
+ end
154
+
155
+ def validate
156
+ @servers = @servers.to_i
157
+
158
+ valid?(@servers > 0, "Must give a valid number of servers")
159
+ valid_dir? File.dirname(@config_file), "Path to config file not valid: #{@config_file}"
160
+
161
+ return @valid
162
+ end
163
+
164
+ def run
165
+ @options = {
166
+ "port" => @port,
167
+ "servers" => @servers,
168
+ "pid_file" => @pid_file
169
+ }
170
+
171
+ @options["log_file"] = @log_file if @log_file
172
+ @options["debug"] = @debug if @debug
173
+ @options["num_procs"] = @num_procs if @num_procs
174
+ @options["docroot"] = @docroot if @docroots
175
+ @options["address"] = @address if @address
176
+ @options["timeout"] = @timeout if @timeout
177
+ @options["environment"] = @environment if @environment
178
+ @options["mime_map"] = @mime_map if @mime_map
179
+ @options["config_script"] = @config_script if @config_script
180
+ @options["cwd"] = @cwd if @cwd
181
+ @options["user"] = @user if @user
182
+ @options["group"] = @group if @group
183
+ @options["prefix"] = @prefix if @prefix
184
+ @options["jkey"] = @jkey if @jkey
185
+ @options["jport"] = @jport if @jport
186
+
187
+ puts "Writing configuration file to #{@config_file}."
188
+ File.open(@config_file,"w") {|f| f.write(@options.to_yaml)}
189
+ end
190
+ end
191
+ end
192
+
@@ -0,0 +1,2 @@
1
+ ---
2
+ :debug: false
@@ -0,0 +1,41 @@
1
+ #!/bin/bash
2
+ #
3
+ # Copyright (c) 2006 Bradley Taylor, bradley@railsmachine.com
4
+ #
5
+ # mongrel_jcluster Startup script for Mongrel clusters.
6
+ #
7
+ # chkconfig: - 85 15
8
+ # description: mongrel_jcluster manages multiple Mongrel processes for use \
9
+ # behind a load balancer.
10
+ #
11
+
12
+ CONF_DIR=/etc/mongrel_jcluster
13
+ RETVAL=0
14
+
15
+
16
+ # Gracefully exit if the controller is missing.
17
+ which mongrel_jcluster_ctl >/dev/null || exit 0
18
+
19
+ # Go no further if config directory is missing.
20
+ [ -d "$CONF_DIR" ] || exit 0
21
+
22
+ case "$1" in
23
+ start)
24
+ mongrel_jcluster_ctl start -c $CONF_DIR
25
+ RETVAL=$?
26
+ ;;
27
+ stop)
28
+ mongrel_jcluster_ctl stop -c $CONF_DIR
29
+ RETVAL=$?
30
+ ;;
31
+ restart)
32
+ mongrel_jcluster_ctl restart -c $CONF_DIR
33
+ RETVAL=$?
34
+ ;;
35
+ *)
36
+ echo "Usage: mongrel_jcluster {start|stop|restart}"
37
+ exit 1
38
+ ;;
39
+ esac
40
+
41
+ exit $RETVAL
@@ -0,0 +1,105 @@
1
+
2
+ def make(makedir)
3
+ Dir.chdir(makedir) do
4
+ sh(PLATFORM =~ /win32/ ? 'nmake' : 'make')
5
+ end
6
+ end
7
+
8
+
9
+ def extconf(dir)
10
+ Dir.chdir(dir) do ruby "extconf.rb" end
11
+ end
12
+
13
+
14
+ def setup_tests
15
+ Rake::TestTask.new do |t|
16
+ t.libs << "test"
17
+ t.test_files = FileList['test/test*.rb']
18
+ t.verbose = true
19
+ end
20
+ end
21
+
22
+
23
+ def setup_clean otherfiles
24
+ files = ['build/*', '**/*.o', '**/*.so', '**/*.a', 'lib/*-*', '**/*.log'] + otherfiles
25
+ CLEAN.include(files)
26
+ end
27
+
28
+
29
+ def setup_rdoc files
30
+ Rake::RDocTask.new do |rdoc|
31
+ rdoc.rdoc_dir = 'doc/rdoc'
32
+ rdoc.options << '--line-numbers'
33
+ rdoc.rdoc_files.add(files)
34
+ end
35
+ end
36
+
37
+
38
+ def setup_extension(dir, extension)
39
+ ext = "ext/#{dir}"
40
+ ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
41
+ ext_files = FileList[
42
+ "#{ext}/*.c",
43
+ "#{ext}/*.h",
44
+ "#{ext}/extconf.rb",
45
+ "#{ext}/Makefile",
46
+ "lib"
47
+ ]
48
+
49
+ task "lib" do
50
+ directory "lib"
51
+ end
52
+
53
+ desc "Builds just the #{extension} extension"
54
+ task extension.to_sym => ["#{ext}/Makefile", ext_so ]
55
+
56
+ file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
57
+ extconf "#{ext}"
58
+ end
59
+
60
+ file ext_so => ext_files do
61
+ make "#{ext}"
62
+ cp ext_so, "lib"
63
+ end
64
+ end
65
+
66
+
67
+ def base_gem_spec(pkg_name, pkg_version)
68
+ pkg_version = pkg_version
69
+ pkg_name = pkg_name
70
+ pkg_file_name = "#{pkg_name}-#{pkg_version}"
71
+ Gem::Specification.new do |s|
72
+ s.name = pkg_name
73
+ s.version = pkg_version
74
+ s.platform = Gem::Platform::RUBY
75
+ s.has_rdoc = true
76
+ s.extra_rdoc_files = [ "README" ]
77
+
78
+ s.files = %w(COPYING LICENSE README Rakefile) +
79
+ Dir.glob("{bin,doc/rdoc,test,lib}/**/*") +
80
+ Dir.glob("ext/**/*.{h,c,rb}") +
81
+ Dir.glob("examples/**/*.rb") +
82
+ Dir.glob("tools/*.rb")
83
+
84
+ s.require_path = "lib"
85
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
86
+ s.bindir = "bin"
87
+ end
88
+ end
89
+
90
+ def setup_gem(pkg_name, pkg_version)
91
+ spec = base_gem_spec(pkg_name, pkg_version)
92
+ yield spec if block_given?
93
+
94
+ Rake::GemPackageTask.new(spec) do |p|
95
+ p.gem_spec = spec
96
+ p.need_tar = true
97
+ end
98
+ end
99
+
100
+ def setup_win32_gem(pkg_name, pkg_version)
101
+ spec = base_gem_spec(pkg_name, pkg_version)
102
+ yield spec if block_given?
103
+
104
+ Gem::Builder.new(spec).build
105
+ end