updated docs, remove daemons dependency; update manifest
[god.git] / bin / god
blob63d69c1e96d5b421dc1d68c08351bad8106fdc09
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>
21 Options:
22 EOF
24 opts.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x|
25 options[:config] = x
26 end
28 opts.on("-pPORT", "--port PORT", "Communications port") do |x|
29 options[:port] = x
30 end
32 opts.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x|
33 options[:pid] = x
34 end
36 opts.on("-lFILE", "--log FILE", "Where to write the log file") do |x|
37 options[:log] = x
38 end
40 opts.on("-D", "--no-daemonize", "Don't daemonize") do
41 options[:daemonize] = false
42 end
44 opts.on("-v", "--version", "Print the version number and exit") do
45 options[:version] = true
46 end
48 opts.on("-V", "Print extended version and build information") do
49 options[:info] = true
50 end
51 end.parse!
53 if options[:version]
54 require 'god'
56 # print version
57 puts "Version #{God::VERSION}"
58 exit!
59 elsif options[:info]
60 require 'god'
62 puts "Version: #{God::VERSION}"
63 puts "Polls: enabled"
64 puts "Events: " + God::EventHandler.event_system
66 exit!
67 elsif command = ARGV[0]
68 require 'god'
70 # a command was specified
72 # disable at_exit
73 # module God; def self.at_exit; end; end
75 # get the name of the watch/group
76 name = ARGV[1]
78 # connect to remote drb
79 DRb.start_service
80 server = DRbObject.new nil, "druby://localhost:#{options[:port]}"
82 begin
83 puts "Sending '#{command}' command"
85 # send command
86 watches = server.control(name, command)
88 # output response
89 puts 'The following watches were affected:'
90 watches.each do |w|
91 puts ' ' + w.name
92 end
93 rescue God::InvalidCommandError
94 abort "Command '#{command}' is not valid. Run 'god --help' for usage"
95 end
97 exit!
98 else
99 # start god
100 if !options[:daemonize]
101 require 'god'
102 load File.expand_path(options[:config])
103 else
104 pid = fork do
105 begin
106 require 'god'
108 log_file = options[:log] || "/dev/null"
110 STDIN.reopen "/dev/null"
111 STDOUT.reopen(log_file, "a")
112 STDERR.reopen STDOUT
114 puts "Starting god"
116 unless God::EventHandler.loaded?
117 puts
118 puts "***********************************************************************"
119 puts "*"
120 puts "* Event conditions are not available for your installation of god."
121 puts "* You may still use and write custom conditions using the poll system"
122 puts "*"
123 puts "***********************************************************************"
124 puts
127 puts "Resetting file descriptors"
129 puts "Loading config"
131 load File.expand_path(options[:config])
132 rescue => e
133 File.open('god.log', 'a') { |f| f.puts e.message + "\n" + e.backtrace }
134 abort "!!! ERROR !!!"
138 if options[:pid]
139 File.open(options[:pid], 'w') { |f| f.write pid }
142 ::Process.detach pid
144 exit!