a local XML-RPC server for ncic: daemonize and respond to TERM, RT#7780
authorivan <ivan>
Wed, 16 Jun 2010 21:53:24 +0000 (21:53 +0000)
committerivan <ivan>
Wed, 16 Jun 2010 21:53:24 +0000 (21:53 +0000)
FS/FS/Conf.pm
FS/FS/Daemon.pm
FS/bin/freeside-selfservice-xmlrpcd
init.d/freeside-init

index 9046b26..0cbf78b 100644 (file)
@@ -1645,6 +1645,7 @@ worry that config_items is freeside-specific and icky.
     'description' => 'Run billing for signup server signups immediately, and do not provision accounts which subsequently have a balance.',
     'type'        => 'checkbox',
   },
     'description' => 'Run billing for signup server signups immediately, and do not provision accounts which subsequently have a balance.',
     'type'        => 'checkbox',
   },
+
   {
     'key'         => 'signup_server-classnum2',
     'section'     => 'self-service',
   {
     'key'         => 'signup_server-classnum2',
     'section'     => 'self-service',
@@ -1660,6 +1661,13 @@ worry that config_items is freeside-specific and icky.
   },
 
   {
   },
 
   {
+    'key'         => 'selfservice-xmlrpc',
+    'section'     => 'self-service',
+    'description' => 'Run a standalone self-service XML-RPC server on the backend (on port 8080).',
+    'type'        => 'checkbox',
+  },
+
+  {
     'key'         => 'backend-realtime',
     'section'     => 'billing',
     'description' => 'Run billing for backend signups immediately.',
     'key'         => 'backend-realtime',
     'section'     => 'billing',
     'description' => 'Run billing for backend signups immediately.',
index 38e6d75..b58cde4 100644 (file)
@@ -6,6 +6,8 @@ use Exporter;
 use Fcntl qw(:flock);
 use POSIX qw(setsid);
 use IO::File;
 use Fcntl qw(:flock);
 use POSIX qw(setsid);
 use IO::File;
+use File::Basename;
+use File::Slurp qw(slurp);
 use Date::Format;
 
 #this is a simple refactoring of the stuff from freeside-queued, just to
 use Date::Format;
 
 #this is a simple refactoring of the stuff from freeside-queued, just to
@@ -20,11 +22,18 @@ use Date::Format;
 $pid_dir = '/var/run';
 
 $NOSIG = 0;
 $pid_dir = '/var/run';
 
 $NOSIG = 0;
+$PID_NEWSTYLE = 0;
 
 sub daemonize1 {
   $me = shift;
 
 
 sub daemonize1 {
   $me = shift;
 
-  $pid_file = "$pid_dir/$me";
+  $pid_file = $pid_dir;
+  if ( $PID_NEWSTYLE ) {
+    $pid_file .= '/freeside';
+    mkdir $pid_file unless -d $pid_file;
+    chown $FS::UID::freeside_uid, -1, $pid_file;
+  }
+  $pid_file .= "/$me";
   $pid_file .= '.'.shift if scalar(@_);
   $pid_file .= '.pid';
 
   $pid_file .= '.'.shift if scalar(@_);
   $pid_file .= '.pid';
 
@@ -35,6 +44,7 @@ sub daemonize1 {
     print "$me started with pid $pid\n"; #logging to $log_file\n";
     exit unless $pid_file;
     my $pidfh = new IO::File ">$pid_file" or exit;
     print "$me started with pid $pid\n"; #logging to $log_file\n";
     exit unless $pid_file;
     my $pidfh = new IO::File ">$pid_file" or exit;
+    chown $FS::UID::freeside_uid, -1, $pid_file;
     print $pidfh "$pid\n";
     exit;
   }
     print $pidfh "$pid\n";
     exit;
   }
@@ -82,14 +92,18 @@ sub sigterm { $sigterm; }
 sub logfile { $logfile = shift; } #_logmsg('test'); }
 
 sub myexit {
 sub logfile { $logfile = shift; } #_logmsg('test'); }
 
 sub myexit {
-  unlink $pid_file if -e $pid_file;
+  chomp( my $pid = slurp($pid_file) );
+  unlink $pid_file if -e $pid_file && $$ == $pid;
   exit;  
 }
 
 sub _die {
   die @_ if $^S; # $^S = 1 during an eval(), don't break exception handling
   my $msg = shift;
   exit;  
 }
 
 sub _die {
   die @_ if $^S; # $^S = 1 during an eval(), don't break exception handling
   my $msg = shift;
-  unlink $pid_file if -e $pid_file;
+
+  chomp( my $pid = slurp($pid_file) );
+  unlink $pid_file if -e $pid_file && $$ == $pid;
+
   _logmsg($msg);
 }
 
   _logmsg($msg);
 }
 
index fd7639c..a902051 100755 (executable)
@@ -25,6 +25,7 @@ use XMLRPC::Lite; # for XMLRPC::Serializer
 
 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 );
 use FS::UID qw(adminsuidsetup);
 
 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 );
 use FS::UID qw(adminsuidsetup);
+use FS::Conf;
 use FS::ClientAPI qw( load_clientapi_modules );
 use FS::ClientAPI_XMLRPC; #FS::SelfService::XMLRPC;
 
 use FS::ClientAPI qw( load_clientapi_modules );
 use FS::ClientAPI_XMLRPC; #FS::SelfService::XMLRPC;
 
@@ -47,18 +48,26 @@ my %typelookup = (
 my $user = shift or die &usage;
 
 $FS::Daemon::NOSIG = 1;
 my $user = shift or die &usage;
 
 $FS::Daemon::NOSIG = 1;
-daemonize1('freeside-selfservice-xmlrpcd');
+$FS::Daemon::PID_NEWSTYLE = 1;
+daemonize1('selfservice-xmlrpcd');
 
 POE::Kernel->has_forked(); #daemonize forks...
 
 drop_root();
 
 
 POE::Kernel->has_forked(); #daemonize forks...
 
 drop_root();
 
+adminsuidsetup($user);
+
 load_clientapi_modules;
 
 logfile("$FREESIDE_LOG/selfservice-xmlrpcd.log");
 
 daemonize2();
 
 load_clientapi_modules;
 
 logfile("$FREESIDE_LOG/selfservice-xmlrpcd.log");
 
 daemonize2();
 
+my $conf = new FS::Conf;
+
+die "not running; selfservice-xmlrpc conf option is off\n"
+  unless $conf->exists('selfservice-xmlrpc');
+
 ###
 # the main loop
 ###
 ###
 # the main loop
 ###
index b4609d4..1acefdc 100644 (file)
@@ -48,6 +48,10 @@ case "$1" in
           echo "done."
         done
 
           echo "done."
         done
 
+        echo -n "Starting freeside-selfservice-xmlrpcd: "
+        freeside-selfservice-xmlrpcd $SELFSERVICE_USER
+        echo "done."
+
         #ip=`/sbin/ifconfig $IF | grep 'inet addr:' | cut -d: -f2- | cut -d' ' -f1`
         #cp /opt/rt3/etc/RT_SiteConfig.pm.ORIG /opt/rt3/etc/RT_SiteConfig.pm
         #perl -pi -e "s/localhost/$ip/" /opt/rt3/etc/RT_SiteConfig.pm
         #ip=`/sbin/ifconfig $IF | grep 'inet addr:' | cut -d: -f2- | cut -d' ' -f1`
         #cp /opt/rt3/etc/RT_SiteConfig.pm.ORIG /opt/rt3/etc/RT_SiteConfig.pm
         #perl -pi -e "s/localhost/$ip/" /opt/rt3/etc/RT_SiteConfig.pm
@@ -104,6 +108,12 @@ case "$1" in
           fi
         done
 
           fi
         done
 
+        if [ -e /var/run/freeside/selfservice-xmlrpcd.pid ]; then
+          echo -n "Stopping freeside-selfservice-xmlrpcd: "
+          kill `cat /var/run/freeside/selfservice-xmlrpcd.pid`
+          echo "done."
+        fi
+
         ;;
 
   restart)
         ;;
 
   restart)