rails_pwnerer 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.5.3. Better control of mongrels using the process table and configuration database.
2
+
1
3
  v0.5.2. Extracts per-app and per-instance configuration from the application dir.
2
4
 
3
5
  v0.5.1. Application instances with per-instance configuration.
@@ -57,6 +57,38 @@ class RailsPwnage::App::ClusterConfig
57
57
  File.delete launch_config if File.exists? launch_config
58
58
  end
59
59
 
60
+ def kill_stray_mongrels(app_name, instance_name)
61
+ app_config = RailsPwnage::Config[app_name, instance_name]
62
+ # silently die if the app was completely busted
63
+ return unless app_config and File.exists? app_config[:app_path]
64
+
65
+ # a list of patterns that show up in mongrels' cmdline
66
+ patterns = ['mongrel', app_config[:app_path] + ' ', 'tmp/pids/mongrel', 'log/mongrel']
67
+
68
+ Dir.chdir app_config[:app_path] do
69
+ # Phase 1: look through PID files and issue kill orders
70
+ pinfo = process_info()
71
+ Dir.glob('tmp/pids/*.pid').each do |fname|
72
+ begin
73
+ pid = File.open(fname, 'r') { |f| f.read.strip! }
74
+ # avoid killing innocent victims
75
+ if pinfo[pid].nil? or patterns.all? { |pattern| pinfo[pid][:cmdline].index pattern }
76
+ Process.kill 'TERM', pid.to_i
77
+ end
78
+ rescue
79
+ # just in case the file gets wiped before we see it
80
+ end
81
+ end
82
+
83
+ # Phase 2: look through the process table and kill anything that looks good
84
+ pinfo = process_info()
85
+ pinfo.each do |pid, info|
86
+ next unless patterns.all? { |pattern| info[:cmdline].index pattern }
87
+ Process.kill 'TERM', pid.to_i
88
+ end
89
+ end
90
+ end
91
+
60
92
  def stop_mongrels(app_name, instance_name)
61
93
  app_config = RailsPwnage::Config[app_name, instance_name]
62
94
  # silently die if the app was completely busted
@@ -64,16 +96,17 @@ class RailsPwnage::App::ClusterConfig
64
96
 
65
97
  Dir.chdir app_config[:app_path] do
66
98
  system 'mongrel_rails cluster::stop'
67
-
68
- # clean up PID files in case the mongrels died and left them behind
69
- Dir.glob('tmp/pids/*.pid').each { |f| File.delete(f) }
70
99
  end
100
+ sleep 0.5
101
+ kill_stray_mongrels app_name, instance_name
71
102
  end
72
103
 
73
104
  def start_mongrels(app_name, instance_name)
74
105
  app_config = RailsPwnage::Config[app_name, instance_name]
75
106
  # silently die if the app was completely busted
76
107
  return unless app_config and File.exists? app_config[:app_path]
108
+
109
+ kill_stray_mongrels app_name, instance_name
77
110
 
78
111
  # alloc a port if somehow that slipped through the cracks
79
112
  if app_config[:port0] == 0
@@ -119,11 +152,13 @@ class RailsPwnage::App::ClusterConfig
119
152
  end
120
153
 
121
154
  def control_all(action)
122
- case action
123
- when :start
124
- control_boot_script('mongrel_cluster', :start)
125
- when :stop
126
- control_boot_script('mongrel_cluster', :stop)
127
- end
155
+ RailsPwnage::Config.all_applications.each do |ai|
156
+ case action
157
+ when :start
158
+ RailsPwnage::App::ClusterConfig.new.start_mongrels ai[0], ai[1]
159
+ when :stop
160
+ RailsPwnage::App::ClusterConfig.new.stop_mongrels ai[0], ai[1]
161
+ end
162
+ end
128
163
  end
129
164
  end
@@ -73,7 +73,6 @@ module RailsPwnage::App
73
73
  def self.control_all(action = :start)
74
74
  case action
75
75
  when :start
76
- control_all :stop
77
76
  Database.new.control_all :start
78
77
  ClusterConfig.new.control_all :start
79
78
  NginxConfig.new.control_all :start
@@ -8,6 +8,14 @@ module RailsPwnage::Config
8
8
 
9
9
  # the instances of an application installed on this box
10
10
  def self.app_instances(app_name)
11
- self.databases().filter { |db| db.include? ?. }.map { |db| db[0..(db.rindex ?.)] }
11
+ self.databases().select { |db| db.include? ?. }.map { |db| db[0...(db.rindex ?.)] }
12
+ end
13
+
14
+ # all instances of all applications installed on this box
15
+ def self.all_applications()
16
+ self.databases().select { |db| db.include? ?. }.map do |db|
17
+ lastdot = db.rindex ?.
18
+ [db[0...lastdot], db[(lastdot + 1)..-1]]
19
+ end
12
20
  end
13
21
  end
@@ -17,6 +17,9 @@ class RailsPwnage::Scaffolds::Gems
17
17
  end
18
18
 
19
19
  def install_tools
20
+ # we need this to do controlled mongrel startups
21
+ install_gems %w(sys-proctable)
22
+
20
23
  # TODO: an application should have its own tools
21
24
  install_gems %w(highline echoe)
22
25
  end
data/lib/rails_pwnage.rb CHANGED
@@ -17,6 +17,7 @@ require 'pwnage/base/dirs.rb'
17
17
  require 'pwnage/base/gems.rb'
18
18
  require 'pwnage/base/hostname.rb'
19
19
  require 'pwnage/base/packages.rb'
20
+ require 'pwnage/base/process.rb'
20
21
  require 'pwnage/base/startup.rb'
21
22
 
22
23
  require 'pwnage/config/app.rb'
@@ -1,11 +1,11 @@
1
1
 
2
- # Gem::Specification for Rails_pwnerer-0.5.2
2
+ # Gem::Specification for Rails_pwnerer-0.5.3
3
3
  # Originally generated by Echoe
4
4
 
5
5
  --- !ruby/object:Gem::Specification
6
6
  name: rails_pwnerer
7
7
  version: !ruby/object:Gem::Version
8
- version: 0.5.2
8
+ version: 0.5.3
9
9
  platform: ruby
10
10
  authors:
11
11
  - Victor Costan
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_pwnerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan