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