activerain-mongrel_runit 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/CHANGELOG.txt +8 -0
  2. data/History.txt +0 -0
  3. data/LICENSE +339 -0
  4. data/Manifest.txt +11 -0
  5. data/README.txt +123 -0
  6. data/Rakefile +58 -0
  7. data/bin/mongrel_runit +9 -0
  8. data/lib/mongrel_runit.rb +25 -0
  9. data/lib/mongrel_runit/base.rb +58 -0
  10. data/lib/mongrel_runit/cli.rb +60 -0
  11. data/lib/mongrel_runit/config.rb +184 -0
  12. data/lib/mongrel_runit/service.rb +253 -0
  13. data/lib/mongrel_runit/servicerunner.rb +91 -0
  14. data/lib/mongrel_runit/version.rb +9 -0
  15. data/test/config/mongrel_runit.yml +21 -0
  16. data/test/config/mongrel_runit_service.yml +12 -0
  17. data/test/tc_config.rb +93 -0
  18. data/test/tc_service.rb +153 -0
  19. data/test/tc_servicerunner.rb +74 -0
  20. data/test/test_helper.rb +2 -0
  21. data/test/test_mongrel_runit.rb +9 -0
  22. data/test/testapp/README +182 -0
  23. data/test/testapp/Rakefile +10 -0
  24. data/test/testapp/app/controllers/application.rb +7 -0
  25. data/test/testapp/app/helpers/application_helper.rb +3 -0
  26. data/test/testapp/config/boot.rb +45 -0
  27. data/test/testapp/config/database.yml +36 -0
  28. data/test/testapp/config/environment.rb +60 -0
  29. data/test/testapp/config/environments/development.rb +21 -0
  30. data/test/testapp/config/environments/production.rb +18 -0
  31. data/test/testapp/config/environments/test.rb +19 -0
  32. data/test/testapp/config/routes.rb +23 -0
  33. data/test/testapp/doc/README_FOR_APP +2 -0
  34. data/test/testapp/log/development.log +0 -0
  35. data/test/testapp/log/production.log +3010 -0
  36. data/test/testapp/log/server.log +0 -0
  37. data/test/testapp/log/test.log +0 -0
  38. data/test/testapp/public/404.html +30 -0
  39. data/test/testapp/public/500.html +30 -0
  40. data/test/testapp/public/dispatch.cgi +10 -0
  41. data/test/testapp/public/dispatch.fcgi +24 -0
  42. data/test/testapp/public/dispatch.rb +10 -0
  43. data/test/testapp/public/favicon.ico +0 -0
  44. data/test/testapp/public/images/rails.png +0 -0
  45. data/test/testapp/public/index.html +277 -0
  46. data/test/testapp/public/javascripts/application.js +2 -0
  47. data/test/testapp/public/javascripts/controls.js +833 -0
  48. data/test/testapp/public/javascripts/dragdrop.js +942 -0
  49. data/test/testapp/public/javascripts/effects.js +1088 -0
  50. data/test/testapp/public/javascripts/prototype.js +2515 -0
  51. data/test/testapp/public/robots.txt +1 -0
  52. data/test/testapp/script/about +3 -0
  53. data/test/testapp/script/breakpointer +3 -0
  54. data/test/testapp/script/console +3 -0
  55. data/test/testapp/script/destroy +3 -0
  56. data/test/testapp/script/generate +3 -0
  57. data/test/testapp/script/performance/benchmarker +3 -0
  58. data/test/testapp/script/performance/profiler +3 -0
  59. data/test/testapp/script/plugin +3 -0
  60. data/test/testapp/script/process/inspector +3 -0
  61. data/test/testapp/script/process/reaper +3 -0
  62. data/test/testapp/script/process/spawner +3 -0
  63. data/test/testapp/script/runner +3 -0
  64. data/test/testapp/script/server +3 -0
  65. data/test/testapp/test/test_helper.rb +28 -0
  66. metadata +157 -0
@@ -0,0 +1,91 @@
1
+ require 'mongrel_runit/base'
2
+ require 'mongrel_runit/service'
3
+ require 'find'
4
+
5
+ module MongrelRunit
6
+ class ServiceRunner < Base
7
+ include Enumerable
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ @mongrel_services = {}
12
+ @mongrel_names = []
13
+
14
+ raise ArgumentError, "Must have an application_name!" unless @config.has_key?("application_name")
15
+ raise ArgumentError, "Must have a port!" unless @config.has_key?("port")
16
+ raise ArgumentError, "Must have a servers option!" unless @config.has_key?("servers")
17
+
18
+ start_port = @config["port"].to_i
19
+ port_end = start_port + @config["servers"].to_i - 1
20
+ start_port.upto(port_end) do |port|
21
+ local_config = config.dup
22
+ local_config["port"] = port
23
+ @mongrel_services[port] = MongrelRunit::Service.new(local_config)
24
+ @mongrel_names << "mongrel-#{@config["application_name"]}-#{port}"
25
+ end
26
+ end
27
+
28
+ def method_missing(method)
29
+ if self.has_command?(method.id2name) || method.id2name == "create"
30
+ run(method.id2name)
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def run(method)
37
+ return self.create if method == "create"
38
+ threads = []
39
+ response = {}
40
+ final_status = true
41
+ self.each do |svc|
42
+ threads << Thread.new(svc) do |s|
43
+ output, status = s.send(:run, method)
44
+ response[svc.config["port"]] = [ output, status ]
45
+ final_status = status if status == false
46
+ end
47
+ end
48
+ threads.each { |t| t.join }
49
+ return response, final_status
50
+ end
51
+
52
+ def create
53
+
54
+ response = {}
55
+ final_status = true
56
+ self.each do |svc|
57
+ output, status = svc.create
58
+ response[svc.config["port"]] = [ output, status ]
59
+ final_status = status if status == false
60
+ end
61
+
62
+ service_path = File.expand_path(@config["runit_service_dir"])
63
+ sv_path = File.expand_path(@config["runit_sv_dir"])
64
+ Find.find(sv_path, service_path) do |file|
65
+ if File.directory?(file) || File.symlink?(file)
66
+ if file =~ /mongrel-#{@config["application_name"]}-(\d+)$/
67
+ safe = @mongrel_names.detect { |cm| cm =~ /#{File.basename(file)}$/ }
68
+ if ! safe
69
+ output = `rm -r #{file}`
70
+ raise "Cannot remove stale mongrel config" unless $?.success?
71
+ end
72
+ end
73
+ end
74
+ end
75
+ return response, final_status
76
+ end
77
+
78
+ def [](port)
79
+ @mongrel_services[port]
80
+ end
81
+
82
+ def length
83
+ @mongrel_services.length
84
+ end
85
+
86
+ def each
87
+ @mongrel_services.each_value { |value| yield value }
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,9 @@
1
+ module MongrelRunit #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ ---
2
+ port: 3000
3
+ address: 127.0.0.1
4
+ pid_file: log/mongrel.pid
5
+ servers: 2
6
+ log_file: foo.log
7
+ environment: production
8
+ cwd: /Users/adam/src/sandbox/mongrel_runit
9
+ timeout: 100
10
+ mime_map: log/mime.map
11
+ docroot: htdocs
12
+ num_procs: 1024
13
+ debug: true
14
+ config_path: /foo/bar/config
15
+ config_script: /foo/bar/config/yml
16
+ user: www-data
17
+ group: www-data
18
+ prefix: /monkey
19
+ runit_sv_dir: /etc/sv
20
+ runit_service_dir: /var/service
21
+ application_name: monkey
@@ -0,0 +1,12 @@
1
+ ---
2
+ port: 3000
3
+ address: 127.0.0.1
4
+ servers: 2
5
+ environment: production
6
+ docroot: htdocs
7
+ num_procs: 1024
8
+ user: www-data
9
+ group: www-data
10
+ application_name: monkey
11
+ env_vars:
12
+ PATH: "$PATH:/var/lib/gems/1.8/bin"
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ require "mongrel_runit/config"
4
+ require "yaml"
5
+
6
+ class TestMongrelRunitConfig < Test::Unit::TestCase
7
+ YAMLFILE = File.dirname(__FILE__) + "/config/mongrel_runit.yml"
8
+ ARGS = " -e PRODUCTION"
9
+ ARGS << " -p 8000"
10
+ ARGS << " -a 127.0.0.1"
11
+ ARGS << " -l log/file.log"
12
+ ARGS << " -P log/file.pid"
13
+ ARGS << " -n 1024"
14
+ ARGS << " -m mime-types.conf"
15
+ ARGS << " -c /tmp"
16
+ ARGS << " -r /tmp"
17
+ ARGS << " --debug"
18
+ ARGS << " -C /conf/path"
19
+ ARGS << " -S /conf/path/script"
20
+ ARGS << " --user www-data"
21
+ ARGS << " --group www-data"
22
+
23
+ def setup
24
+ @file_template = {
25
+ :port => 3000,
26
+ :address => '127.0.0.1',
27
+ :pid_file => 'log/mongrel.pid',
28
+ :servers => 2,
29
+ :log_file => 'foo.log',
30
+ :environment => "production",
31
+ :cwd => "/Users/adam/src/sandbox/mongrel_runit",
32
+ :timeout => 100,
33
+ :mime_map => "log/mime.map",
34
+ :docroot => "htdocs",
35
+ :num_procs => 1024,
36
+ :debug => true,
37
+ :config_path => "/foo/bar/config",
38
+ :config_script => "/foo/bar/config/yml",
39
+ :user => "www-data",
40
+ :group => "www-data",
41
+ :prefix => "/monkey",
42
+ :runit_sv_dir => "/etc/sv",
43
+ :runit_service_dir => "/var/service",
44
+ :application_name => "monkey",
45
+ }
46
+ end
47
+
48
+ def load_file
49
+ @from_file = MongrelRunit::Config.load_file(YAMLFILE)
50
+ end
51
+
52
+ def load_args(args)
53
+ config = MongrelRunit::Config.load_args(args)
54
+ end
55
+
56
+ def test_load_args
57
+ args = [ "-c#{File.expand_path('./config/mongrel_runit.yml')}", "status" ]
58
+ config = load_args(args)
59
+ @file_template.each do |key, value|
60
+ assert_equal(value, config["#{key}"], "#{key} has proper value")
61
+ end
62
+ end
63
+
64
+ def test_load_appname_args
65
+ args = [
66
+ "-c#{File.expand_path('./config/mongrel_runit.yml')}",
67
+ "-amoo",
68
+ "status",
69
+ ]
70
+ config = load_args(args)
71
+ @file_template[:application_name] = "moo"
72
+ @file_template.each do |key, value|
73
+ assert_equal(value, config["#{key}"], "#{key} has proper value")
74
+ end
75
+ assert_equal("status", config.command, "Command is correct")
76
+ end
77
+
78
+ def test_load_nothing
79
+ args = [ "status" ]
80
+ config = load_args(args)
81
+ @file_template.each do |key, value|
82
+ assert_equal(value, config["#{key}"], "#{key} has proper value")
83
+ end
84
+ assert_equal("status", config.command, "Command is correct")
85
+ end
86
+
87
+ def test_load_file
88
+ load_file
89
+ @file_template.each do |key, value|
90
+ assert_equal(value, @from_file["#{key}"], "#{key} has proper value")
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,153 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ require "mongrel_runit/config"
4
+ require "mongrel_runit/service"
5
+ require "find"
6
+
7
+ class TestMongrelRunitService < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @config = {
11
+ "port" => "8191",
12
+ "address" => "127.0.0.1",
13
+ "servers" => "2",
14
+ "environment" => "production",
15
+ "cwd" => File.dirname(__FILE__) + '/testapp',
16
+ "num_procs" => "1024",
17
+ #"user" => "www-data",
18
+ #"group" => "www-data",
19
+ "runit_sv_dir" => File.dirname(__FILE__) + "/sv",
20
+ "runit_service_dir" => File.dirname(__FILE__) + "/service",
21
+ "application_name" => "monkey",
22
+ "svlogd_config" => <<EOH
23
+ s100000
24
+ n100
25
+ EOH
26
+ }
27
+ @service = MongrelRunit::Service.new(@config)
28
+ end
29
+
30
+ def teardown
31
+ cleanup
32
+ end
33
+
34
+ def test_service_create
35
+ assert_equal(MongrelRunit::Service, @service.class, "Got the right object")
36
+ end
37
+
38
+ def test_run_testapp
39
+ @service.create
40
+ if ENV["RUNSVTEST"]
41
+ sleep 5
42
+ output, status = @service.start
43
+ assert(status, "Service Start")
44
+ ouput, status = @service.stop
45
+ assert(status, "Service Stop")
46
+ output, status = @service.start
47
+ assert(status, "Service Start")
48
+ end
49
+ end
50
+
51
+ def test_make_service_link
52
+ @service.create
53
+ link = @service.make_service_link
54
+ assert(File.symlink?(link), "Created service symlink")
55
+ end
56
+
57
+ def test_make_sv_dir
58
+ svlogdir = @service.make_sv_dir
59
+ assert(File.directory?(svlogdir), "Created sv directory")
60
+ end
61
+
62
+ def test_check_service_dir
63
+ dir_exists = @service.check_service_dir
64
+ assert(dir_exists, "Has a service directory")
65
+ end
66
+
67
+ def test_make_log
68
+ runlog = @service.make_log
69
+ assert_equal(@service.logrunfile, IO.read(runlog), "log/run file created")
70
+ assert(File.executable?(runlog), "log/run file executable")
71
+ end
72
+
73
+ def test_make_check
74
+ checkfile = @service.make_check
75
+ assert_equal(@service.checkfile, IO.read(checkfile), "check file created")
76
+ assert(File.executable?(checkfile), "check file executable")
77
+ new_config = @config.dup
78
+ new_config["checkfile"] = <<EOH
79
+ foo #{@config['address']} #{@config['port']}
80
+ EOH
81
+ config_service = MongrelRunit::Service.new(new_config)
82
+ checkfile = config_service.make_check
83
+ assert_equal("foo 127.0.0.1 8191\n", config_service.checkfile)
84
+ end
85
+
86
+ def test_make_log_config
87
+ logconfig = @service.make_log_config
88
+ assert_equal(@service.logconfig, IO.read(logconfig)) if logconfig
89
+ end
90
+
91
+ def test_make_run
92
+ no_env_runfile = @service.make_run
93
+ no_env_file = <<EOH
94
+ #!/bin/sh
95
+ exec \\
96
+ mongrel_rails start -e production -p 8191 -a 127.0.0.1 -c #{File.dirname(__FILE__) + '/testapp'} -n 1024 2>&1
97
+ EOH
98
+ env_file = <<EOH
99
+ #!/bin/sh
100
+ exec \\
101
+ env PATH=$PATH:/var/lib/gems/1.8 \\
102
+ mongrel_rails start -e production -p 8191 -a 127.0.0.1 -c #{File.dirname(__FILE__) + '/testapp'} -n 1024 2>&1
103
+ EOH
104
+ command_line_file = <<EOH
105
+ #!/bin/sh
106
+ exec \\
107
+ moofyboogles -p 8191 2>&1
108
+ EOH
109
+ chpst_path = `which chpst`.chomp
110
+ chpst_file = <<EOH
111
+ #!/bin/sh
112
+ exec \\
113
+ #{chpst_path} -o 3365 \\
114
+ mongrel_rails start -e production -p 8191 -a 127.0.0.1 -c #{File.dirname(__FILE__) + '/testapp'} -n 1024 2>&1
115
+ EOH
116
+ assert_equal(no_env_file, @service.runfile)
117
+ assert_equal(@service.runfile, IO.read(no_env_runfile))
118
+
119
+ env_config = @config.dup
120
+ env_config["env_vars"] = { "PATH" => "$PATH:/var/lib/gems/1.8" }
121
+ env_service = MongrelRunit::Service.new(env_config)
122
+ env_service_runfile = env_service.make_run
123
+ assert_equal(env_file, env_service.runfile)
124
+ assert_equal(env_service.runfile, IO.read(env_service_runfile))
125
+ assert(File.executable?(env_service_runfile), "run file executable")
126
+
127
+ chpst_config = @config.dup
128
+ chpst_config["chpst"] = "-o 3365"
129
+ chpst_service = MongrelRunit::Service.new(chpst_config)
130
+ chpst_service_runfile = chpst_service.make_run
131
+ assert_equal(chpst_file, chpst_service.runfile)
132
+ assert_equal(chpst_service.runfile, IO.read(chpst_service_runfile))
133
+ assert(File.executable?(chpst_service_runfile), "run file executable")
134
+
135
+ command_line_config = @config.dup
136
+ command_line_config["command_line"] = 'moofyboogles -p #{@config["port"]}'
137
+ command_line_service = MongrelRunit::Service.new(command_line_config)
138
+ command_line_runfile = command_line_service.make_run
139
+ assert_equal(command_line_file, command_line_service.runfile, "Command line runfile works")
140
+ assert_equal(command_line_service.runfile, IO.read(command_line_runfile), "Command line generated file works")
141
+ end
142
+
143
+ def cleanup
144
+ Find.find(@config["runit_sv_dir"], @config["runit_service_dir"]) do |file|
145
+ if File.directory?(file) || File.symlink?(file)
146
+ if File.basename(file) =~ /(.+)-(\d+)/
147
+ `rm -r #{file}`
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
153
+
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ require "mongrel_runit/servicerunner"
4
+ require "find"
5
+
6
+ class TestMongrelRunitServiceRunner < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @config = {
10
+ "port" => "8191",
11
+ "address" => "127.0.0.1",
12
+ "servers" => "2",
13
+ "environment" => "production",
14
+ "cwd" => File.dirname(__FILE__) + '/testapp',
15
+ "num_procs" => "1024",
16
+ #"user" => "www-data",
17
+ #"group" => "www-data",
18
+ "runit_sv_dir" => File.dirname(__FILE__) + "/sv",
19
+ "runit_service_dir" => File.dirname(__FILE__) + "/service",
20
+ "application_name" => "monkey",
21
+ "svlogd_config" => <<EOH
22
+ s100000
23
+ n100
24
+ EOH
25
+ }
26
+ @servicerunner = MongrelRunit::ServiceRunner.new(@config)
27
+ end
28
+
29
+ def teardown
30
+ cleanup
31
+ end
32
+
33
+ def test_service_create
34
+ assert_equal(MongrelRunit::ServiceRunner, @servicerunner.class, "Got the right object")
35
+ end
36
+
37
+ def test_create
38
+ result, status = @servicerunner.create
39
+ @servicerunner.each do |service|
40
+ assert(result.has_key?(service.config["port"]), "Created #{service.config['port']}")
41
+ assert(File.directory?(service.svdir), "Created svdir")
42
+ end
43
+ end
44
+
45
+ def test_run_start
46
+ if ENV["RUNSVTEST"]
47
+ result, status = @servicerunner.create
48
+ assert(status, "Created for each service")
49
+ sleep 5
50
+ result, status = @servicerunner.start
51
+ assert(status, "Started each service")
52
+ end
53
+ end
54
+
55
+ def test_run_stop
56
+ if ENV["RUNSVTEST"]
57
+ result, status = @servicerunner.create
58
+ assert(status, "Created each service")
59
+ sleep 5
60
+ result, status = @servicerunner.stop
61
+ assert(status, "Stopped each service")
62
+ end
63
+ end
64
+
65
+ def cleanup
66
+ Find.find(@config["runit_sv_dir"], @config["runit_service_dir"]) do |file|
67
+ if File.directory?(file) || File.symlink?(file)
68
+ if File.basename(file) =~ /mongrel-(.+)-(\d+)/
69
+ `rm -r #{file}`
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end