Register the watch's process at the end of meddle.
[god.git] / bin / god
blobb3ff218bf52490acf157d5aa62fa85069a2ba7e2
1 #!/usr/bin/env ruby
3 $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5 require 'rubygems'
6 require 'optparse'
7 require 'drb'
9 require 'daemons'
10 require 'god'
12 unless God::EventHandler.loaded?
13 puts
14 puts "***********************************************************************"
15 puts "*"
16 puts "* Event conditions are not available for your installation of god."
17 puts "* You may still use and write custom conditions using the poll system"
18 puts "*"
19 puts "***********************************************************************"
20 puts
21 end
23 options = {:daemonize => true, :port => 17165}
25 OptionParser.new do |opts|
26 opts.banner = <<-EOF
27 Usage: god [command] [options]
29 Commands:
30 start <watch or group name>
31 stop <watch or group name>
32 monitor <watch or group name>
33 unmonitor <watch or group name>
35 Options:
36 EOF
38 opts.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x|
39 options[:config] = x
40 end
42 opts.on("-pPORT", "--port PORT", "Communications port") do |x|
43 options[:port] = x
44 end
46 opts.on("-D", "--no-daemonize", "Don't daemonize") do
47 options[:daemonize] = false
48 end
50 opts.on("-v", "--version", "Print the version number and exit") do
51 options[:version] = true
52 end
53 end.parse!
55 if options[:version]
56 puts "Version #{God::VERSION}"
57 exit
58 elsif command = ARGV[0]
59 # a command was specified
61 # get the name of the watch/group
62 name = ARGV[1]
64 # connect to remote drb
65 DRb.start_service
66 server = DRbObject.new nil, "druby://localhost:#{options[:port]}"
68 # get the list of watches
69 watches = Array(server.meddle.watches[name] || server.meddle.groups[name])
71 # do the command
72 case command
73 when "start", "monitor"
74 watches.each { |w| w.monitor }
75 when "restart"
76 watches.each { |w| w.move(:restart) }
77 when "stop"
78 watches.each { |w| w.unmonitor.action(:stop) }
79 when "unmonitor"
80 watches.each { |w| w.unmonitor }
81 else
82 abort "Command '#{command}' is not valid. Run 'god --help' for usage"
83 end
84 else
85 # start god
87 options[:config] = File.expand_path(options[:config]) if options[:config]
89 Daemons.daemonize if options[:daemonize]
91 load options[:config]
92 end