add --log-level to cli tool
[god.git] / lib / god / cli / run.rb
blob4c640b127caf4e35e760a776bcc56ffa24901bcd
1 module God
2   module CLI
3     
4     class Run
5       def initialize(options)
6         @options = options
7         
8         dispatch
9       end
10       
11       def dispatch
12         # have at_exit start god
13         $run = true
14         
15         if @options[:daemonize]
16           run_daemonized
17         else
18           run_in_front
19         end
20       end
21       
22       def run_daemonized
23         # trap and ignore SIGHUP
24         Signal.trap('HUP') {}
25         
26         pid = fork do
27           begin
28             require 'god'
29             
30             log_file = @options[:log] || "/dev/null"
31             
32             unless God::EventHandler.loaded?
33               puts
34               puts "***********************************************************************"
35               puts "*"
36               puts "* Event conditions are not available for your installation of god."
37               puts "* You may still use and write custom conditions using the poll system"
38               puts "*"
39               puts "***********************************************************************"
40               puts
41             end
42             
43             # set port if requested
44             if @options[:port]
45               God.port = @options[:port]
46             end
47             
48             # set pid if requested
49             if @options[:pid]
50               God.pid = @options[:pid] 
51             end
52             
53             # set log level if requested
54             if @options[:log_level]
55               God.log_level = @options[:log_level]
56             end
57             
58             # load config
59             if @options[:config]
60               unless File.exist?(@options[:config])
61                 abort "File not found: #{@options[:config]}"
62               end
63               
64               begin
65                 load File.expand_path(@options[:config])
66               rescue Exception => e
67                 if e.instance_of?(SystemExit)
68                   raise
69                 else
70                   puts e.message
71                   puts e.backtrace.join("\n")
72                   abort "There was an error in your configuration file (see above)"
73                 end
74               end
75             end
76             
77             # reset file descriptors
78             STDIN.reopen "/dev/null"
79             STDOUT.reopen(log_file, "a")
80             STDERR.reopen STDOUT
81           rescue => e
82             puts e.message
83             puts e.backtrace.join("\n")
84             abort "There was a fatal system error while starting god (see above)"
85           end
86         end
87         
88         if @options[:pid]
89           File.open(@options[:pid], 'w') { |f| f.write pid }
90         end
91         
92         ::Process.detach pid
93         
94         exit
95       end
96       
97       def run_in_front
98         require 'god'
99         
100         if @options[:port]
101           God.port = @options[:port]
102         end
103         
104         # set log level if requested
105         if @options[:log_level]
106           God.log_level = @options[:log_level]
107         end
108         
109         if @options[:config]
110           unless File.exist?(@options[:config])
111             abort "File not found: #{@options[:config]}"
112           end
113           
114           begin
115             load File.expand_path(@options[:config])
116           rescue Exception => e
117             if e.instance_of?(SystemExit)
118               raise
119             else
120               puts e.message
121               puts e.backtrace.join("\n")
122               abort "There was an error in your configuration file (see above)"
123             end
124           end
125         end
126       end
127     end # Run
128     
129   end