With all of this in place, dbomatic now behaves like a
proper daemon, including daemonizing by default and
having the initscript respond to start/stop/status.

Signed-off-by: Chris Lalancette <[email protected]>
---
 conf/deltacloud-dbomatic |    7 ++++---
 src/dbomatic/dbomatic    |   42 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/conf/deltacloud-dbomatic b/conf/deltacloud-dbomatic
index 3ee407c..b56a848 100755
--- a/conf/deltacloud-dbomatic
+++ b/conf/deltacloud-dbomatic
@@ -19,14 +19,15 @@ USER="${USER:-dcloud}"
 GROUP="${GROUP:-dcloud}"
 
 DBOMATIC_PATH=/usr/share/deltacloud-aggregator/dbomatic
-DBOMATIC_PROC=dbomatic
+DBOMATIC_PROG=dbomatic
+DBOMATIC_PID=/var/run/deltacloud-aggregator/dbomatic.pid
 
 . /etc/init.d/functions
 
 start() {
     echo -n "Starting deltacloud-dbomatic: "
 
-    $DBOMATIC_PATH/$DBOMATIC_PROC&
+    daemon --user=$USER $DBOMATIC_PATH/$DBOMATIC_PROG
     RETVAL=$?
     if [ $RETVAL -eq 0 ] && touch $DBOMATIC_LOCKFILE ; then
       echo_success
@@ -39,7 +40,7 @@ start() {
 
 stop() {
     echo -n "Shutting down deltacloud-dbomatic: "
-    killall $DBOMATIC_PROC
+    killproc -p $DBOMATIC_PID $DBOMATIC_PROG
     RETVAL=$?
     if [ $RETVAL -eq 0 ] && rm -f $DBOMATIC_LOCKFILE ; then
       echo_success
diff --git a/src/dbomatic/dbomatic b/src/dbomatic/dbomatic
index 9d15a54..f6647ba 100755
--- a/src/dbomatic/dbomatic
+++ b/src/dbomatic/dbomatic
@@ -18,41 +18,49 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 
 $: << File.join(File.dirname(__FILE__), "../dutils")
+
+require 'rubygems'
 require 'dutils'
 require 'nokogiri'
 require 'rb-inotify'
 require 'optparse'
 
 help = false
-
+daemon = true
 condor_event_log_dir    =   "/var/log/condor"
 dbomatic_run_dir        =   "/var/run/deltacloud-aggregator"
 dbomatic_log_dir        =   "/var/log/deltacloud-aggregator"
+dbomatic_pid_dir        =   "/var/run/deltacloud-aggregator"
 
 optparse = OptionParser.new do |opts|
 
-opts.banner = <<BANNER
+  opts.banner = <<BANNER
 Usage:
 dbomatic [options]
 
 Options:
 BANNER
+  opts.on( '-f', '--pid-file PATH', "Use PATH to the dbomatic pid directory 
(defaults to #{dbomatic_pid_dir})") do |newpath|
+    dbomatic_pid_dir = newpath
+  end
+  opts.on( '-h', '--help', '') { help = true }
+  opts.on( '-l', '--log PATH', "Use PATH to the dbomatic log directory 
(defaults to #{dbomatic_log_dir}).  Use '-' for stdout") do |newpath|
+    dbomatic_log_dir = newpath
+  end
+  opts.on( '-n', '--nodaemon', 'Do not daemonize (useful in combination with 
-l for debugging)') { daemon = false }
   opts.on( '-p', '--path PATH', "Use PATH to the condor log directory 
(defaults to #{condor_event_log_dir})") do |newpath|
     condor_event_log_dir = newpath
   end
   opts.on( '-r', '--run PATH', "Use PATH to the dbomatic runtime directory 
(defaults to #{dbomatic_run_dir})") do |newpath|
     dbomatic_run_dir = newpath
   end
-  opts.on( '-l', '--log PATH', "Use PATH to the dbomatic log directory 
(defaults to #{dbomatic_log_dir}).  Use '-' for stdout") do |newpath|
-    dbomatic_log_dir = newpath
-  end
-  opts.on( '-h', '--help', '') { help = true }
 end
 
 begin
-optparse.parse!
+  optparse.parse!
 rescue OptionParser::InvalidOption => e
-  puts e
+  puts "Invalid option #{e.args}"
+  puts
   puts optparse
   exit(1)
 end
@@ -74,6 +82,24 @@ logger = Logger.new(DBOMATIC_LOG_FILE)
 logger.level = Logger::DEBUG
 logger.info "DBOmatic starting up"
 
+# daemonize
+if daemon
+  # note that this requires 'active_support', which we get for free from dutils
+  Process.daemon
+end
+
+begin
+  DBOMATIC_PID_FILE = "#{dbomatic_pid_dir}/dbomatic.pid"
+  FileUtils.mkdir_p File.dirname(DBOMATIC_PID_FILE)
+  open(DBOMATIC_PID_FILE, "w") {|f| f.write(Process.pid) }
+  File.chmod(0644, DBOMATIC_PID_FILE)
+rescue => e
+  logger.error "#{e.backtrace.shift}: #{e.message}"
+  e.backtrace.each do |step|
+    logger.error "\tfrom #{step}"
+  end
+end
+
 # Handle the event log's xml
 class CondorEventLog < Nokogiri::XML::SAX::Document
   attr_accessor :tag, :event_type, :event_cmd, :event_time, :trigger_type, 
:grid_resource, :execute_host
-- 
1.7.2.3

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to