/
god
executable file
·219 lines (174 loc) · 4.79 KB
/
god
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'rubygems'
require 'optparse'
require 'drb'
options = {:daemonize => true, :port => 17165}
OptionParser.new do |opts|
opts.banner = <<-EOF
Usage: god [command] [options]
Commands:
start <watch or group name>
stop <watch or group name>
monitor <watch or group name>
unmonitor <watch or group name>
load <file>
log <watch name>
status
terminate
Options:
EOF
opts.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x|
options[:config] = x
end
opts.on("-pPORT", "--port PORT", "Communications port") do |x|
options[:port] = x
end
opts.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x|
options[:pid] = x
end
opts.on("-lFILE", "--log FILE", "Where to write the log file") do |x|
options[:log] = x
end
opts.on("-D", "--no-daemonize", "Don't daemonize") do
options[:daemonize] = false
end
opts.on("-v", "--version", "Print the version number and exit") do
options[:version] = true
end
opts.on("-V", "Print extended version and build information") do
options[:info] = true
end
end.parse!
if options[:version]
require 'god'
# print version
puts "Version #{God::VERSION}"
exit!(0)
elsif options[:info]
require 'god'
puts "Version: #{God::VERSION}"
puts "Polls: enabled"
puts "Events: " + God::EventHandler.event_system
exit!(0)
elsif command = ARGV[0]
require 'god'
# a command was specified
# connect to remote drb
DRb.start_service
server = DRbObject.new nil, "druby://localhost:#{options[:port]}"
begin
server.ping
rescue DRb::DRbConnError
puts "The server is not available (or you do not have permissions to access it)"
exit!
end
if command == 'load'
file = ARGV[1]
puts "Sending '#{command}' command"
code = File.read(file)
watches = server.running_load(code)
# output response
puts 'The following watches were affected:'
watches.each do |w|
puts ' ' + w.name
end
puts "Done"
elsif command == 'status'
puts server.status
elsif command == 'log'
begin
Signal.trap('INT') { exit!(0) }
name = ARGV[1]
t = Time.at(0)
loop do
print server.running_log(name, t)
t = Time.now
sleep 1
end
rescue God::NoSuchWatchError
puts "No such watch"
rescue DRb::DRbConnError
puts "The server went away"
rescue => e
puts e.message
puts e.backtrace.join("\n")
ensure
exit!(0)
end
elsif command == 'terminate'
if server.stop_all
puts 'Stopped all watches'
else
puts 'Could not stop all watches within 10 seconds'
end
begin
server.terminate
abort 'Could not stop god'
rescue DRb::DRbConnError
puts 'Stopped god'
exit!(0)
end
else
# get the name of the watch/group
name = ARGV[1]
begin
puts "Sending '#{command}' command"
# send command
watches = server.control(name, command)
# output response
puts 'The following watches were affected:'
watches.each do |w|
puts ' ' + w.name
end
rescue God::InvalidCommandError
abort "Command '#{command}' is not valid. Run 'god --help' for usage"
end
end
exit!(0)
else
# start god
if !options[:daemonize]
require 'god'
if options[:port]
God.port = options[:port]
end
load File.expand_path(options[:config])
else
pid = fork do
begin
require 'god'
log_file = options[:log] || "/dev/null"
STDIN.reopen "/dev/null"
STDOUT.reopen(log_file, "a")
STDERR.reopen STDOUT
puts "Starting god"
unless God::EventHandler.loaded?
puts
puts "***********************************************************************"
puts "*"
puts "* Event conditions are not available for your installation of god."
puts "* You may still use and write custom conditions using the poll system"
puts "*"
puts "***********************************************************************"
puts
end
puts "Resetting file descriptors"
puts "Loading config"
if options[:port]
God.port = options[:port]
end
load File.expand_path(options[:config])
Signal.trap('HUP') {}
rescue => e
File.open('god.log', 'a') { |f| f.puts e.message + "\n" + e.backtrace }
abort "!!! ERROR - See god.log !!!"
end
end
if options[:pid]
File.open(options[:pid], 'w') { |f| f.write pid }
end
::Process.detach pid
exit!(0)
end
end