resurrected_god 0.14.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.md +559 -0
- data/README.md +9 -4
- data/bin/god +26 -28
- data/ext/god/extconf.rb +9 -9
- data/lib/god/behavior.rb +3 -5
- data/lib/god/behaviors/clean_pid_file.rb +4 -6
- data/lib/god/behaviors/clean_unix_socket.rb +4 -6
- data/lib/god/behaviors/notify_when_flapping.rb +13 -15
- data/lib/god/cli/command.rb +109 -89
- data/lib/god/cli/run.rb +51 -75
- data/lib/god/cli/version.rb +2 -4
- data/lib/god/condition.rb +12 -14
- data/lib/god/conditions/always.rb +3 -2
- data/lib/god/conditions/complex.rb +23 -24
- data/lib/god/conditions/cpu_usage.rb +12 -16
- data/lib/god/conditions/degrading_lambda.rb +15 -19
- data/lib/god/conditions/disk_usage.rb +7 -8
- data/lib/god/conditions/file_mtime.rb +3 -7
- data/lib/god/conditions/file_touched.rb +4 -5
- data/lib/god/conditions/flapping.rb +57 -62
- data/lib/god/conditions/http_response_code.rb +27 -32
- data/lib/god/conditions/lambda.rb +3 -5
- data/lib/god/conditions/memory_usage.rb +12 -16
- data/lib/god/conditions/process_exits.rb +12 -12
- data/lib/god/conditions/process_running.rb +8 -10
- data/lib/god/conditions/socket_responding.rb +23 -28
- data/lib/god/conditions/tries.rb +12 -14
- data/lib/god/configurable.rb +7 -10
- data/lib/god/contact.rb +41 -52
- data/lib/god/contacts/airbrake.rb +10 -12
- data/lib/god/contacts/campfire.rb +18 -20
- data/lib/god/contacts/email.rb +27 -28
- data/lib/god/contacts/prowl.rb +16 -17
- data/lib/god/contacts/scout.rb +2 -5
- data/lib/god/contacts/sensu.rb +15 -11
- data/lib/god/contacts/slack.rb +23 -33
- data/lib/god/contacts/statsd.rb +8 -8
- data/lib/god/contacts/twitter.rb +2 -2
- data/lib/god/contacts/webhook.rb +21 -24
- data/lib/god/driver.rb +12 -18
- data/lib/god/errors.rb +0 -2
- data/lib/god/event_handler.rb +40 -49
- data/lib/god/event_handlers/dummy_handler.rb +1 -1
- data/lib/god/event_handlers/kqueue_handler.rb +2 -2
- data/lib/god/event_handlers/netlink_handler.rb +1 -1
- data/lib/god/logger.rb +5 -13
- data/lib/god/metric.rb +13 -15
- data/lib/god/process.rb +93 -98
- data/lib/god/simple_logger.rb +13 -15
- data/lib/god/socket.rb +21 -23
- data/lib/god/sugar.rb +8 -8
- data/lib/god/sys_logger.rb +6 -8
- data/lib/god/system/portable_poller.rb +1 -9
- data/lib/god/system/process.rb +4 -6
- data/lib/god/system/slash_proc_poller.rb +17 -19
- data/lib/god/task.rb +110 -136
- data/lib/god/timeline.rb +2 -4
- data/lib/god/trigger.rb +7 -11
- data/lib/god/version.rb +1 -1
- data/lib/god/watch.rb +51 -57
- data/lib/god.rb +122 -148
- metadata +17 -370
- data/Announce.txt +0 -135
- data/Gemfile +0 -5
- data/Rakefile +0 -129
- data/doc/god.asciidoc +0 -1592
- data/doc/intro.asciidoc +0 -20
- data/ext/god/.gitignore +0 -5
- data/lib/god/compat19.rb +0 -33
- data/lib/god/contacts/hipchat.rb +0 -117
- data/lib/god/contacts/jabber.rb +0 -75
- data/test/configs/child_events/child_events.god +0 -44
- data/test/configs/child_events/simple_server.rb +0 -3
- data/test/configs/child_polls/child_polls.god +0 -37
- data/test/configs/child_polls/simple_server.rb +0 -12
- data/test/configs/complex/complex.god +0 -59
- data/test/configs/complex/simple_server.rb +0 -3
- data/test/configs/contact/contact.god +0 -118
- data/test/configs/contact/simple_server.rb +0 -3
- data/test/configs/daemon_events/daemon_events.god +0 -37
- data/test/configs/daemon_events/simple_server.rb +0 -8
- data/test/configs/daemon_events/simple_server_stop.rb +0 -11
- data/test/configs/daemon_polls/daemon_polls.god +0 -17
- data/test/configs/daemon_polls/simple_server.rb +0 -6
- data/test/configs/degrading_lambda/degrading_lambda.god +0 -31
- data/test/configs/degrading_lambda/tcp_server.rb +0 -15
- data/test/configs/keepalive/keepalive.god +0 -9
- data/test/configs/keepalive/keepalive.rb +0 -12
- data/test/configs/lifecycle/lifecycle.god +0 -25
- data/test/configs/matias/matias.god +0 -50
- data/test/configs/real.rb +0 -59
- data/test/configs/running_load/running_load.god +0 -16
- data/test/configs/stop_options/simple_server.rb +0 -12
- data/test/configs/stop_options/stop_options.god +0 -39
- data/test/configs/stress/simple_server.rb +0 -3
- data/test/configs/stress/stress.god +0 -15
- data/test/configs/task/logs/.placeholder +0 -0
- data/test/configs/task/task.god +0 -26
- data/test/configs/test.rb +0 -61
- data/test/configs/usr1_trapper.rb +0 -10
- data/test/helper.rb +0 -172
- data/test/suite.rb +0 -6
- data/test/test_airbrake.rb +0 -14
- data/test/test_behavior.rb +0 -18
- data/test/test_campfire.rb +0 -22
- data/test/test_condition.rb +0 -52
- data/test/test_conditions_disk_usage.rb +0 -50
- data/test/test_conditions_http_response_code.rb +0 -109
- data/test/test_conditions_process_running.rb +0 -40
- data/test/test_conditions_socket_responding.rb +0 -176
- data/test/test_conditions_tries.rb +0 -67
- data/test/test_contact.rb +0 -109
- data/test/test_driver.rb +0 -26
- data/test/test_email.rb +0 -34
- data/test/test_event_handler.rb +0 -82
- data/test/test_god.rb +0 -710
- data/test/test_god_system.rb +0 -201
- data/test/test_handlers_kqueue_handler.rb +0 -16
- data/test/test_hipchat.rb +0 -23
- data/test/test_jabber.rb +0 -29
- data/test/test_logger.rb +0 -55
- data/test/test_metric.rb +0 -74
- data/test/test_process.rb +0 -263
- data/test/test_prowl.rb +0 -15
- data/test/test_registry.rb +0 -15
- data/test/test_sensu.rb +0 -11
- data/test/test_slack.rb +0 -57
- data/test/test_socket.rb +0 -34
- data/test/test_statsd.rb +0 -22
- data/test/test_sugar.rb +0 -42
- data/test/test_system_portable_poller.rb +0 -17
- data/test/test_system_process.rb +0 -30
- data/test/test_task.rb +0 -246
- data/test/test_timeline.rb +0 -37
- data/test/test_trigger.rb +0 -63
- data/test/test_watch.rb +0 -286
- data/test/test_webhook.rb +0 -22
data/lib/god/process.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module God
|
2
2
|
class Process
|
3
|
-
WRITES_PID = [:start, :restart]
|
3
|
+
WRITES_PID = [:start, :restart].freeze
|
4
4
|
|
5
5
|
attr_accessor :name, :uid, :gid, :log, :log_cmd, :err_log, :err_log_cmd,
|
6
6
|
:start, :stop, :restart, :unix_socket, :chroot, :env, :dir,
|
@@ -20,8 +20,8 @@ module God
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def alive?
|
23
|
-
if
|
24
|
-
System::Process.new(
|
23
|
+
if pid
|
24
|
+
System::Process.new(pid).exists?
|
25
25
|
else
|
26
26
|
false
|
27
27
|
end
|
@@ -30,21 +30,21 @@ module God
|
|
30
30
|
def file_writable?(file)
|
31
31
|
pid = fork do
|
32
32
|
begin
|
33
|
-
if
|
34
|
-
user_method =
|
35
|
-
uid_num = Etc.send(user_method,
|
36
|
-
gid_num = Etc.send(user_method,
|
33
|
+
if uid
|
34
|
+
user_method = uid.is_a?(Integer) ? :getpwuid : :getpwnam
|
35
|
+
uid_num = Etc.send(user_method, uid).uid
|
36
|
+
gid_num = Etc.send(user_method, uid).gid
|
37
37
|
end
|
38
|
-
if
|
39
|
-
group_method =
|
40
|
-
gid_num = Etc.send(group_method,
|
38
|
+
if gid
|
39
|
+
group_method = gid.is_a?(Integer) ? :getgrgid : :getgrnam
|
40
|
+
gid_num = Etc.send(group_method, gid).gid
|
41
41
|
end
|
42
42
|
|
43
|
-
::Dir.chroot(
|
43
|
+
::Dir.chroot(chroot) if chroot
|
44
44
|
::Process.groups = [gid_num] if gid_num
|
45
|
-
::Process.initgroups(
|
45
|
+
::Process.initgroups(uid, gid_num) if uid && gid_num
|
46
46
|
::Process::Sys.setgid(gid_num) if gid_num
|
47
|
-
::Process::Sys.setuid(uid_num) if
|
47
|
+
::Process::Sys.setuid(uid_num) if uid
|
48
48
|
rescue ArgumentError, Errno::EPERM, Errno::ENOENT
|
49
49
|
exit(1)
|
50
50
|
end
|
@@ -52,94 +52,94 @@ module God
|
|
52
52
|
File.writable?(file_in_chroot(file)) ? exit!(0) : exit!(1)
|
53
53
|
end
|
54
54
|
|
55
|
-
|
56
|
-
status.exitstatus == 0
|
55
|
+
_wpid, status = ::Process.waitpid2(pid)
|
56
|
+
status.exitstatus == 0
|
57
57
|
end
|
58
58
|
|
59
59
|
def valid?
|
60
60
|
# determine if we're tracking pid or not
|
61
|
-
|
61
|
+
pid_file
|
62
62
|
|
63
63
|
valid = true
|
64
64
|
|
65
65
|
# a start command must be specified
|
66
|
-
if
|
66
|
+
if start.nil?
|
67
67
|
valid = false
|
68
|
-
applog(self, :error,
|
68
|
+
applog(self, :error, 'No start command was specified')
|
69
69
|
end
|
70
70
|
|
71
71
|
# uid must exist if specified
|
72
|
-
if
|
72
|
+
if uid
|
73
73
|
begin
|
74
|
-
Etc.getpwnam(
|
74
|
+
Etc.getpwnam(uid)
|
75
75
|
rescue ArgumentError
|
76
76
|
valid = false
|
77
|
-
applog(self, :error, "UID for '#{
|
77
|
+
applog(self, :error, "UID for '#{uid}' does not exist")
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
81
|
# gid must exist if specified
|
82
|
-
if
|
82
|
+
if gid
|
83
83
|
begin
|
84
|
-
Etc.getgrnam(
|
84
|
+
Etc.getgrnam(gid)
|
85
85
|
rescue ArgumentError
|
86
86
|
valid = false
|
87
|
-
applog(self, :error, "GID for '#{
|
87
|
+
applog(self, :error, "GID for '#{gid}' does not exist")
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
# dir must exist and be a directory if specified
|
92
|
-
if
|
93
|
-
if !File.exist?(
|
92
|
+
if dir
|
93
|
+
if !File.exist?(dir)
|
94
94
|
valid = false
|
95
|
-
applog(self, :error, "Specified directory '#{
|
96
|
-
elsif !File.directory?(
|
95
|
+
applog(self, :error, "Specified directory '#{dir}' does not exist")
|
96
|
+
elsif !File.directory?(dir)
|
97
97
|
valid = false
|
98
|
-
applog(self, :error, "Specified directory '#{
|
98
|
+
applog(self, :error, "Specified directory '#{dir}' is not a directory")
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
102
|
# pid dir must exist if specified
|
103
|
-
if !@tracking_pid && !File.exist?(File.dirname(
|
103
|
+
if !@tracking_pid && !File.exist?(File.dirname(pid_file))
|
104
104
|
valid = false
|
105
|
-
applog(self, :error, "PID file directory '#{File.dirname(
|
105
|
+
applog(self, :error, "PID file directory '#{File.dirname(pid_file)}' does not exist")
|
106
106
|
end
|
107
107
|
|
108
108
|
# pid dir must be writable if specified
|
109
|
-
if !@tracking_pid && File.exist?(File.dirname(
|
109
|
+
if !@tracking_pid && File.exist?(File.dirname(pid_file)) && !file_writable?(File.dirname(pid_file))
|
110
110
|
valid = false
|
111
|
-
applog(self, :error, "PID file directory '#{File.dirname(
|
111
|
+
applog(self, :error, "PID file directory '#{File.dirname(pid_file)}' is not writable by #{uid || Etc.getlogin}")
|
112
112
|
end
|
113
113
|
|
114
114
|
# log dir must exist
|
115
|
-
|
115
|
+
unless File.exist?(File.dirname(log))
|
116
116
|
valid = false
|
117
|
-
applog(self, :error, "Log directory '#{File.dirname(
|
117
|
+
applog(self, :error, "Log directory '#{File.dirname(log)}' does not exist")
|
118
118
|
end
|
119
119
|
|
120
120
|
# log file or dir must be writable
|
121
|
-
if File.exist?(
|
122
|
-
unless file_writable?(
|
121
|
+
if File.exist?(log)
|
122
|
+
unless file_writable?(log)
|
123
123
|
valid = false
|
124
|
-
applog(self, :error, "Log file '#{
|
124
|
+
applog(self, :error, "Log file '#{log}' exists but is not writable by #{uid || Etc.getlogin}")
|
125
125
|
end
|
126
126
|
else
|
127
|
-
unless file_writable?(File.dirname(
|
127
|
+
unless file_writable?(File.dirname(log))
|
128
128
|
valid = false
|
129
|
-
applog(self, :error, "Log directory '#{File.dirname(
|
129
|
+
applog(self, :error, "Log directory '#{File.dirname(log)}' is not writable by #{uid || Etc.getlogin}")
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
133
|
# chroot directory must exist and have /dev/null in it
|
134
|
-
if
|
135
|
-
|
134
|
+
if chroot
|
135
|
+
unless File.directory?(chroot)
|
136
136
|
valid = false
|
137
|
-
applog(self, :error, "CHROOT directory '#{
|
137
|
+
applog(self, :error, "CHROOT directory '#{chroot}' does not exist")
|
138
138
|
end
|
139
139
|
|
140
|
-
|
140
|
+
unless File.exist?(File.join(chroot, '/dev/null'))
|
141
141
|
valid = false
|
142
|
-
applog(self, :error, "CHROOT directory '#{
|
142
|
+
applog(self, :error, "CHROOT directory '#{chroot}' does not contain '/dev/null'")
|
143
143
|
end
|
144
144
|
end
|
145
145
|
|
@@ -150,11 +150,11 @@ module God
|
|
150
150
|
# No really, trust me. Use the instance variable.
|
151
151
|
def pid_file=(value)
|
152
152
|
# if value is nil, do the right thing
|
153
|
-
if value
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
153
|
+
@tracking_pid = if value
|
154
|
+
false
|
155
|
+
else
|
156
|
+
true
|
157
|
+
end
|
158
158
|
|
159
159
|
@pid_file = value
|
160
160
|
end
|
@@ -169,8 +169,8 @@ module God
|
|
169
169
|
#
|
170
170
|
# Returns Integer(pid) or nil
|
171
171
|
def pid
|
172
|
-
contents = File.read(
|
173
|
-
real_pid =
|
172
|
+
contents = File.read(pid_file).strip rescue ''
|
173
|
+
real_pid = /^\d+$/.match?(contents) ? contents.to_i : nil
|
174
174
|
|
175
175
|
if real_pid
|
176
176
|
@pid = real_pid
|
@@ -185,8 +185,8 @@ module God
|
|
185
185
|
# Returns nothing
|
186
186
|
def signal(sig)
|
187
187
|
sig = sig.to_i if sig.to_i != 0
|
188
|
-
applog(self, :info, "#{
|
189
|
-
::Process.kill(sig,
|
188
|
+
applog(self, :info, "#{name} sending signal '#{sig}' to pid #{pid}")
|
189
|
+
::Process.kill(sig, pid) rescue nil
|
190
190
|
end
|
191
191
|
|
192
192
|
def start!
|
@@ -202,7 +202,7 @@ module God
|
|
202
202
|
end
|
203
203
|
|
204
204
|
def default_pid_file
|
205
|
-
File.join(God.pid_file_directory, "#{
|
205
|
+
File.join(God.pid_file_directory, "#{name}.pid")
|
206
206
|
end
|
207
207
|
|
208
208
|
def call_action(action)
|
@@ -210,12 +210,11 @@ module God
|
|
210
210
|
|
211
211
|
if action == :stop && command.nil?
|
212
212
|
pid = self.pid
|
213
|
-
name = self.name
|
214
213
|
command = lambda do
|
215
|
-
applog(self, :info, "#{
|
214
|
+
applog(self, :info, "#{name} stop: default lambda killer")
|
216
215
|
|
217
216
|
::Process.kill(@stop_signal, pid) rescue nil
|
218
|
-
applog(self, :info, "#{
|
217
|
+
applog(self, :info, "#{name} sent SIG#{@stop_signal}")
|
219
218
|
|
220
219
|
# Poll to see if it's dead
|
221
220
|
pid_not_found = false
|
@@ -225,11 +224,11 @@ module God
|
|
225
224
|
::Process.kill(0, pid)
|
226
225
|
rescue Errno::ESRCH
|
227
226
|
# It died. Good.
|
228
|
-
applog(self, :info, "#{
|
227
|
+
applog(self, :info, "#{name} process stopped")
|
229
228
|
return
|
230
229
|
end
|
231
230
|
else
|
232
|
-
applog(self, :warn, "#{
|
231
|
+
applog(self, :warn, "#{name} pid not found in #{pid_file}") unless pid_not_found
|
233
232
|
pid_not_found = true
|
234
233
|
end
|
235
234
|
|
@@ -237,20 +236,19 @@ module God
|
|
237
236
|
end
|
238
237
|
|
239
238
|
::Process.kill('KILL', pid) rescue nil
|
240
|
-
applog(self, :warn, "#{
|
239
|
+
applog(self, :warn, "#{name} still alive after #{@stop_timeout}s; sent SIGKILL")
|
241
240
|
end
|
242
241
|
end
|
243
242
|
|
244
|
-
|
245
|
-
|
246
|
-
|
243
|
+
case command
|
244
|
+
when String
|
247
245
|
if [:start, :restart].include?(action) && @tracking_pid
|
248
246
|
# double fork god-daemonized processes
|
249
247
|
# we don't want to wait for them to finish
|
250
248
|
r, w = IO.pipe
|
251
249
|
begin
|
252
250
|
opid = fork do
|
253
|
-
|
251
|
+
$stdout.reopen(w)
|
254
252
|
r.close
|
255
253
|
pid = self.spawn(command)
|
256
254
|
puts pid.to_s # send pid back to forker
|
@@ -272,22 +270,18 @@ module God
|
|
272
270
|
status = ::Process.waitpid2(pid, 0)
|
273
271
|
exit_code = status[1] >> 8
|
274
272
|
|
275
|
-
if exit_code != 0
|
276
|
-
applog(self, :warn, "#{self.name} #{action} command exited with non-zero code = #{exit_code}")
|
277
|
-
end
|
273
|
+
applog(self, :warn, "#{name} #{action} command exited with non-zero code = #{exit_code}") if exit_code != 0
|
278
274
|
|
279
275
|
ensure_stop if action == :stop
|
280
276
|
end
|
281
277
|
|
282
|
-
if @tracking_pid
|
283
|
-
File.
|
284
|
-
f.write pid
|
285
|
-
end
|
278
|
+
if @tracking_pid || (@pid_file.nil? && WRITES_PID.include?(action))
|
279
|
+
File.write(default_pid_file, pid)
|
286
280
|
|
287
281
|
@tracking_pid = true
|
288
282
|
@pid_file = default_pid_file
|
289
283
|
end
|
290
|
-
|
284
|
+
when Proc
|
291
285
|
# lambda command
|
292
286
|
command.call
|
293
287
|
else
|
@@ -301,39 +295,39 @@ module God
|
|
301
295
|
# Returns nothing
|
302
296
|
def spawn(command)
|
303
297
|
fork do
|
304
|
-
File.umask
|
305
|
-
uid_num = Etc.getpwnam(
|
306
|
-
gid_num = Etc.getgrnam(
|
307
|
-
gid_num = Etc.getpwnam(
|
298
|
+
File.umask umask if umask
|
299
|
+
uid_num = Etc.getpwnam(uid).uid if uid
|
300
|
+
gid_num = Etc.getgrnam(gid).gid if gid
|
301
|
+
gid_num = Etc.getpwnam(uid).gid if gid.nil? && uid
|
308
302
|
|
309
|
-
::Dir.chroot(
|
303
|
+
::Dir.chroot(chroot) if chroot
|
310
304
|
::Process.setsid
|
311
305
|
::Process.groups = [gid_num] if gid_num
|
312
|
-
::Process.initgroups(
|
306
|
+
::Process.initgroups(uid, gid_num) if uid && gid_num
|
313
307
|
::Process::Sys.setgid(gid_num) if gid_num
|
314
|
-
::Process::Sys.setuid(uid_num) if
|
308
|
+
::Process::Sys.setuid(uid_num) if uid
|
315
309
|
self.dir ||= '/'
|
316
310
|
Dir.chdir self.dir
|
317
311
|
$0 = command
|
318
|
-
|
319
|
-
if
|
320
|
-
|
312
|
+
$stdin.reopen '/dev/null'
|
313
|
+
if log_cmd
|
314
|
+
$stdout.reopen IO.popen(log_cmd, 'a')
|
321
315
|
else
|
322
|
-
|
316
|
+
$stdout.reopen file_in_chroot(log), 'a'
|
323
317
|
end
|
324
318
|
if err_log_cmd
|
325
|
-
|
319
|
+
$stderr.reopen IO.popen(err_log_cmd, 'a')
|
326
320
|
elsif err_log && (log_cmd || err_log != log)
|
327
|
-
|
321
|
+
$stderr.reopen file_in_chroot(err_log), 'a'
|
328
322
|
else
|
329
|
-
|
323
|
+
$stderr.reopen $stdout
|
330
324
|
end
|
331
325
|
|
332
326
|
# close any other file descriptors
|
333
|
-
3.upto(256){|fd| IO
|
327
|
+
3.upto(256) { |fd| IO.new(fd).close rescue nil }
|
334
328
|
|
335
|
-
if
|
336
|
-
|
329
|
+
if env.is_a?(Hash)
|
330
|
+
env.each do |(key, value)|
|
337
331
|
ENV[key] = value.to_s
|
338
332
|
end
|
339
333
|
end
|
@@ -347,17 +341,17 @@ module God
|
|
347
341
|
#
|
348
342
|
# Returns nothing
|
349
343
|
def ensure_stop
|
350
|
-
applog(self, :warn, "#{
|
344
|
+
applog(self, :warn, "#{name} ensuring stop...")
|
351
345
|
|
352
|
-
unless
|
353
|
-
applog(self, :warn, "#{
|
346
|
+
unless pid
|
347
|
+
applog(self, :warn, "#{name} stop called but pid is uknown")
|
354
348
|
return
|
355
349
|
end
|
356
350
|
|
357
351
|
# Poll to see if it's dead
|
358
352
|
@stop_timeout.times do
|
359
353
|
begin
|
360
|
-
::Process.kill(0,
|
354
|
+
::Process.kill(0, pid)
|
361
355
|
rescue Errno::ESRCH
|
362
356
|
# It died. Good.
|
363
357
|
return
|
@@ -367,15 +361,16 @@ module God
|
|
367
361
|
end
|
368
362
|
|
369
363
|
# last resort
|
370
|
-
::Process.kill('KILL',
|
371
|
-
applog(self, :warn, "#{
|
364
|
+
::Process.kill('KILL', pid) rescue nil
|
365
|
+
applog(self, :warn, "#{name} still alive after #{@stop_timeout}s; sent SIGKILL")
|
372
366
|
end
|
373
367
|
|
374
368
|
private
|
369
|
+
|
375
370
|
def file_in_chroot(file)
|
376
|
-
return file unless
|
371
|
+
return file unless chroot
|
377
372
|
|
378
|
-
file.gsub(/^#{Regexp.escape(File.expand_path(
|
373
|
+
file.gsub(/^#{Regexp.escape(File.expand_path(chroot))}/, '')
|
379
374
|
end
|
380
375
|
end
|
381
376
|
end
|
data/lib/god/simple_logger.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
module God
|
2
|
-
|
3
2
|
class SimpleLogger
|
4
3
|
DEBUG = 2
|
5
4
|
INFO = 4
|
@@ -7,53 +6,52 @@ module God
|
|
7
6
|
ERROR = 16
|
8
7
|
FATAL = 32
|
9
8
|
|
10
|
-
SEV_LABEL = {DEBUG => 'DEBUG',
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
SEV_LABEL = { DEBUG => 'DEBUG',
|
10
|
+
INFO => 'INFO',
|
11
|
+
WARN => 'WARN',
|
12
|
+
ERROR => 'ERROR',
|
13
|
+
FATAL => 'FATAL' }.freeze
|
15
14
|
|
16
15
|
CONSTANT_TO_SYMBOL = { DEBUG => :debug,
|
17
16
|
INFO => :info,
|
18
17
|
WARN => :warn,
|
19
18
|
ERROR => :error,
|
20
|
-
FATAL => :fatal }
|
19
|
+
FATAL => :fatal }.freeze
|
21
20
|
|
22
21
|
attr_accessor :datetime_format, :level
|
23
22
|
|
24
23
|
def initialize(io)
|
25
24
|
@io = io
|
26
25
|
@level = INFO
|
27
|
-
@datetime_format =
|
26
|
+
@datetime_format = '%Y-%m-%d %H:%M:%S'
|
28
27
|
end
|
29
28
|
|
30
29
|
def output(level, msg)
|
31
30
|
return if level < self.level
|
32
31
|
|
33
|
-
time = Time.now.strftime(
|
32
|
+
time = Time.now.strftime(datetime_format)
|
34
33
|
label = SEV_LABEL[level]
|
35
34
|
@io.print("#{label[0..0]} [#{time}] #{label.rjust(5)}: #{msg}\n")
|
36
35
|
end
|
37
36
|
|
38
37
|
def fatal(msg)
|
39
|
-
|
38
|
+
output(FATAL, msg)
|
40
39
|
end
|
41
40
|
|
42
41
|
def error(msg)
|
43
|
-
|
42
|
+
output(ERROR, msg)
|
44
43
|
end
|
45
44
|
|
46
45
|
def warn(msg)
|
47
|
-
|
46
|
+
output(WARN, msg)
|
48
47
|
end
|
49
48
|
|
50
49
|
def info(msg)
|
51
|
-
|
50
|
+
output(INFO, msg)
|
52
51
|
end
|
53
52
|
|
54
53
|
def debug(msg)
|
55
|
-
|
54
|
+
output(DEBUG, msg)
|
56
55
|
end
|
57
56
|
end
|
58
|
-
|
59
57
|
end
|
data/lib/god/socket.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'drb'
|
2
2
|
|
3
3
|
module God
|
4
|
-
|
5
4
|
# The God::Server oversees the DRb server which dishes out info on this God daemon.
|
6
5
|
class Socket
|
7
6
|
attr_reader :port
|
@@ -19,7 +18,7 @@ module God
|
|
19
18
|
#
|
20
19
|
# Returns String (drb address)
|
21
20
|
def self.socket(port)
|
22
|
-
"drbunix://#{
|
21
|
+
"drbunix://#{socket_file(port)}"
|
23
22
|
end
|
24
23
|
|
25
24
|
# The location of the socket for this Server
|
@@ -63,7 +62,7 @@ module God
|
|
63
62
|
# Returns nothing
|
64
63
|
def stop
|
65
64
|
DRb.stop_service
|
66
|
-
FileUtils.rm_f(
|
65
|
+
FileUtils.rm_f(socket_file)
|
67
66
|
end
|
68
67
|
|
69
68
|
private
|
@@ -74,40 +73,39 @@ module God
|
|
74
73
|
# Returns nothing
|
75
74
|
def start
|
76
75
|
begin
|
77
|
-
@drb ||= DRb.start_service(
|
76
|
+
@drb ||= DRb.start_service(socket, self)
|
78
77
|
applog(nil, :info, "Started on #{DRb.uri}")
|
79
78
|
rescue Errno::EADDRINUSE
|
80
|
-
applog(nil, :info,
|
81
|
-
server = DRbObject.new(nil,
|
79
|
+
applog(nil, :info, 'Socket already in use')
|
80
|
+
server = DRbObject.new(nil, socket)
|
82
81
|
|
83
82
|
begin
|
84
83
|
Timeout.timeout(5) do
|
85
84
|
server.ping
|
86
85
|
end
|
87
|
-
abort "Socket #{
|
86
|
+
abort "Socket #{socket} already in use by another instance of god"
|
88
87
|
rescue StandardError, Timeout::Error
|
89
|
-
applog(nil, :info,
|
90
|
-
File.delete(
|
91
|
-
@drb ||= DRb.start_service(
|
88
|
+
applog(nil, :info, 'Socket is stale, reopening')
|
89
|
+
File.delete(socket_file) rescue nil
|
90
|
+
@drb ||= DRb.start_service(socket, self)
|
92
91
|
applog(nil, :info, "Started on #{DRb.uri}")
|
93
92
|
end
|
94
93
|
end
|
95
94
|
|
96
|
-
|
97
|
-
if @user
|
98
|
-
user_method = @user.is_a?(Integer) ? :getpwuid : :getpwnam
|
99
|
-
uid = Etc.send(user_method, @user).uid
|
100
|
-
gid = Etc.send(user_method, @user).gid
|
101
|
-
end
|
102
|
-
if @group
|
103
|
-
group_method = @group.is_a?(Integer) ? :getgrgid : :getgrnam
|
104
|
-
gid = Etc.send(group_method, @group).gid
|
105
|
-
end
|
95
|
+
return unless File.exist?(socket_file)
|
106
96
|
|
107
|
-
|
108
|
-
|
97
|
+
if @user
|
98
|
+
user_method = @user.is_a?(Integer) ? :getpwuid : :getpwnam
|
99
|
+
uid = Etc.send(user_method, @user).uid
|
100
|
+
gid = Etc.send(user_method, @user).gid
|
109
101
|
end
|
102
|
+
if @group
|
103
|
+
group_method = @group.is_a?(Integer) ? :getgrgid : :getgrnam
|
104
|
+
gid = Etc.send(group_method, @group).gid
|
105
|
+
end
|
106
|
+
|
107
|
+
File.chmod(Integer(@perm), socket_file) if @perm
|
108
|
+
File.chown(uid, gid, socket_file) if uid || gid
|
110
109
|
end
|
111
110
|
end
|
112
|
-
|
113
111
|
end
|
data/lib/god/sugar.rb
CHANGED
@@ -5,7 +5,7 @@ class Numeric
|
|
5
5
|
end
|
6
6
|
|
7
7
|
# Public: Units of seconds.
|
8
|
-
alias
|
8
|
+
alias second seconds
|
9
9
|
|
10
10
|
# Public: Units of minutes (60 seconds).
|
11
11
|
def minutes
|
@@ -13,7 +13,7 @@ class Numeric
|
|
13
13
|
end
|
14
14
|
|
15
15
|
# Public: Units of minutes (60 seconds).
|
16
|
-
alias
|
16
|
+
alias minute minutes
|
17
17
|
|
18
18
|
# Public: Units of hours (3600 seconds).
|
19
19
|
def hours
|
@@ -21,7 +21,7 @@ class Numeric
|
|
21
21
|
end
|
22
22
|
|
23
23
|
# Public: Units of hours (3600 seconds).
|
24
|
-
alias
|
24
|
+
alias hour hours
|
25
25
|
|
26
26
|
# Public: Units of days (86400 seconds).
|
27
27
|
def days
|
@@ -29,7 +29,7 @@ class Numeric
|
|
29
29
|
end
|
30
30
|
|
31
31
|
# Public: Units of days (86400 seconds).
|
32
|
-
alias
|
32
|
+
alias day days
|
33
33
|
|
34
34
|
# Units of kilobytes.
|
35
35
|
def kilobytes
|
@@ -37,7 +37,7 @@ class Numeric
|
|
37
37
|
end
|
38
38
|
|
39
39
|
# Units of kilobytes.
|
40
|
-
alias
|
40
|
+
alias kilobyte kilobytes
|
41
41
|
|
42
42
|
# Units of megabytes (1024 kilobytes).
|
43
43
|
def megabytes
|
@@ -45,15 +45,15 @@ class Numeric
|
|
45
45
|
end
|
46
46
|
|
47
47
|
# Units of megabytes (1024 kilobytes).
|
48
|
-
alias
|
48
|
+
alias megabyte megabytes
|
49
49
|
|
50
50
|
# Units of gigabytes (1,048,576 kilobytes).
|
51
51
|
def gigabytes
|
52
|
-
self * (1024
|
52
|
+
self * (1024**2)
|
53
53
|
end
|
54
54
|
|
55
55
|
# Units of gigabytes (1,048,576 kilobytes).
|
56
|
-
alias
|
56
|
+
alias gigabyte gigabytes
|
57
57
|
|
58
58
|
# Units of percent. e.g. 50.percent.
|
59
59
|
def percent
|
data/lib/god/sys_logger.rb
CHANGED
@@ -8,16 +8,15 @@ begin
|
|
8
8
|
Syslog.reopen('god')
|
9
9
|
end
|
10
10
|
|
11
|
-
Syslog.info(
|
11
|
+
Syslog.info('Syslog enabled.')
|
12
12
|
|
13
13
|
module God
|
14
|
-
|
15
14
|
class SysLogger
|
16
|
-
SYMBOL_EQUIVALENTS = { :
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
20
|
-
:
|
15
|
+
SYMBOL_EQUIVALENTS = { fatal: Syslog::LOG_CRIT,
|
16
|
+
error: Syslog::LOG_ERR,
|
17
|
+
warn: Syslog::LOG_WARNING,
|
18
|
+
info: Syslog::LOG_INFO,
|
19
|
+
debug: Syslog::LOG_DEBUG }.freeze
|
21
20
|
|
22
21
|
# Set the log level
|
23
22
|
# +level+ is the Symbol level to set as maximum. One of:
|
@@ -38,7 +37,6 @@ begin
|
|
38
37
|
Syslog.log(SYMBOL_EQUIVALENTS[level], '%s', text)
|
39
38
|
end
|
40
39
|
end
|
41
|
-
|
42
40
|
end
|
43
41
|
rescue Object => e
|
44
42
|
puts "Syslog could not be enabled: #{e.message}"
|
@@ -4,6 +4,7 @@ module God
|
|
4
4
|
def initialize(pid)
|
5
5
|
@pid = pid
|
6
6
|
end
|
7
|
+
|
7
8
|
# Memory usage in kilobytes (resident set size)
|
8
9
|
def memory
|
9
10
|
ps_int('rss')
|
@@ -28,15 +29,6 @@ module God
|
|
28
29
|
def ps_float(keyword)
|
29
30
|
`ps -o #{keyword}= -p #{@pid}`.to_f
|
30
31
|
end
|
31
|
-
|
32
|
-
def ps_string(keyword)
|
33
|
-
`ps -o #{keyword}= -p #{@pid}`.strip
|
34
|
-
end
|
35
|
-
|
36
|
-
def time_string_to_seconds(text)
|
37
|
-
_, minutes, seconds, useconds = *text.match(/(\d+):(\d{2}).(\d{2})/)
|
38
|
-
(minutes.to_i * 60) + seconds.to_i
|
39
|
-
end
|
40
32
|
end
|
41
33
|
end
|
42
34
|
end
|