From 079d9d36b083b7b53c3756dc52496f55dadd1373 Mon Sep 17 00:00:00 2001
From: martin <martin@martin-ThinkPad-T410.(none)>
Date: Tue, 31 May 2011 18:55:22 -0400
Subject: [PATCH] cc-2055: switch to init.d

-fix uninstall scripts
-make daemon script for media-monitor and show-recorder
-create pid file for liquidsoap
-fix airtime-check-system
-make sure all startup scripts are using exec
---
 install/airtime-install                       |   2 +
 .../media-monitor/airtime-media-monitor       |   2 +-
 .../airtime-media-monitor-init-d              |  49 +++++++++
 .../install/media-monitor-install.py          |  11 +-
 .../install/media-monitor-uninstall.py        |   8 +-
 python_apps/pypo/airtime-playout              |   1 +
 python_apps/pypo/airtime-playout-init-d       |  16 ++-
 python_apps/pypo/install/pypo-install.py      |   8 +-
 python_apps/pypo/install/pypo-uninstall.py    |  17 +--
 .../show-recorder/airtime-show-recorder       |   5 +-
 .../airtime-show-recorder-init-d              |  49 +++++++++
 .../show-recorder/install/recorder-install.py |  13 ++-
 .../install/recorder-uninstall.py             |   8 +-
 utils/airtime-check-system.php                | 101 +++++++++---------
 14 files changed, 192 insertions(+), 98 deletions(-)
 create mode 100755 python_apps/media-monitor/airtime-media-monitor-init-d
 create mode 100755 python_apps/show-recorder/airtime-show-recorder-init-d

diff --git a/install/airtime-install b/install/airtime-install
index 0d88f03a3..6e90a16c9 100755
--- a/install/airtime-install
+++ b/install/airtime-install
@@ -16,6 +16,8 @@ python ../python_apps/pypo/install/pypo-install.py
 #python ".__DIR__."/../python_apps/show-recorder/install/recorder-install.py
 #python ../python_apps/show-recorder/install/recorder-install.py
 
+sleep 4
+
 airtime-check-system
 
 echo -e "\n******************************* Install Complete *******************************";
diff --git a/python_apps/media-monitor/airtime-media-monitor b/python_apps/media-monitor/airtime-media-monitor
index a703ebc25..26c5e7999 100755
--- a/python_apps/media-monitor/airtime-media-monitor
+++ b/python_apps/media-monitor/airtime-media-monitor
@@ -12,6 +12,6 @@ exec 2>&1
 export PYTHONPATH=${api_client_path}
 
 # Note the -u when calling python! we need it to get unbuffered binary stdout and stderr
-python -u ${media_monitor_path}${media_monitor_script}
+exec python -u ${media_monitor_path}${media_monitor_script}
 
 # EOF
diff --git a/python_apps/media-monitor/airtime-media-monitor-init-d b/python_apps/media-monitor/airtime-media-monitor-init-d
new file mode 100755
index 000000000..db38dd67a
--- /dev/null
+++ b/python_apps/media-monitor/airtime-media-monitor-init-d
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+USERID=pypo
+GROUPID=pypo
+NAME=Airtime
+
+DAEMON=/usr/bin/airtime-media-monitor
+PIDFILE=/var/run/airtime-media-monitor.pid
+
+start () {    
+        start-stop-daemon --start --background --quiet --chuid $USERID:$GROUPID --make-pidfile --pidfile $PIDFILE --startas $DAEMON
+}
+
+stop () {
+        # Send TERM after 5 seconds, wait at most 30 seconds.
+        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --pidfile $PIDFILE
+        rm -f $PIDFILE
+}
+
+
+case "${1:-''}" in
+  'start')
+            # start commands here
+            echo -n "Starting $NAME: "
+            start
+            echo "Done."
+        ;;
+  'stop')
+            # stop commands here
+            echo -n "Stopping $NAME: "
+            stop
+            echo "Done."
+        ;;
+  'restart')
+           # restart commands here
+           echo -n "Restarting $NAME: "
+           stop
+           start
+           echo "Done."
+        ;;
+  'status')
+           # status commands here
+           /usr/bin/airtime-check-system
+        ;;
+  *)      # no parameter specified
+        echo "Usage: $SELF start|stop|restart|status"
+        exit 1
+        ;;
+esac
diff --git a/python_apps/media-monitor/install/media-monitor-install.py b/python_apps/media-monitor/install/media-monitor-install.py
index fa698daed..2d6bdb542 100755
--- a/python_apps/media-monitor/install/media-monitor-install.py
+++ b/python_apps/media-monitor/install/media-monitor-install.py
@@ -72,9 +72,11 @@ try:
     sys.exit()
 
   current_script_dir = get_current_script_dir()
-  print "Checking and removing any existing media monitor processes"
-  os.system("python %s/media-monitor-uninstall.py 1>/dev/null 2>&1"% current_script_dir)
-  time.sleep(5)
+  #print "Checking and removing any existing media monitor processes"
+  #os.system("python %s/media-monitor-uninstall.py 1>/dev/null 2>&1"% current_script_dir)
+  #time.sleep(5)
+  p = Popen("/etc/init.d/airtime-media-monitor stop", shell=True)
+  sts = os.waitpid(p.pid, 0)[1]
 
   # Create users
   create_user("pypo")
@@ -93,6 +95,9 @@ try:
   print "Creating symbolic links"
   os.system("rm -f /usr/bin/airtime-media-monitor")
   os.system("ln -s "+config["bin_dir"]+"/airtime-media-monitor /usr/bin/")
+
+  print "Installing media-monitor daemon"
+  shutil.copy(config["bin_dir"]+"/airtime-media-monitor-init-d", "/etc/init.d/airtime-media-monitor")
   
   print "Waiting for processes to start..."
   p = Popen("/etc/init.d/airtime-media-monitor start", shell=True)
diff --git a/python_apps/media-monitor/install/media-monitor-uninstall.py b/python_apps/media-monitor/install/media-monitor-uninstall.py
index e0a5ca262..2f90364d8 100755
--- a/python_apps/media-monitor/install/media-monitor-uninstall.py
+++ b/python_apps/media-monitor/install/media-monitor-uninstall.py
@@ -37,21 +37,17 @@ try:
         print 'Error loading config file: ', e
         sys.exit()
 
-    os.system("python /usr/bin/airtime-media-monitor-stop")
+    os.system("/etc/init.d/airtime-media-monitor stop")
     
     print "Removing log directories"
     remove_path(config["log_dir"])
     
     print "Removing symlinks"
-    os.system("rm -f /usr/bin/airtime-media-monitor-start")
-    os.system("rm -f /usr/bin/airtime-media-monitor-stop")
+    os.system("rm -f /usr/bin/airtime-media-monitor")
     
     print "Removing application files"
     remove_path(config["bin_dir"])
 
-    print "Removing daemontool script media-monitor"
-    remove_path("rm -rf /etc/service/media-monitor")
-
     remove_user("pypo")
     print "Uninstall complete."
 except Exception, e:
diff --git a/python_apps/pypo/airtime-playout b/python_apps/pypo/airtime-playout
index f20555474..995575622 100755
--- a/python_apps/pypo/airtime-playout
+++ b/python_apps/pypo/airtime-playout
@@ -14,4 +14,5 @@ export PYTHONPATH=${api_client_path}:$PYTHONPATH
 
 # Note the -u when calling python! we need it to get unbuffered binary stdout and stderr
 exec python -u ${pypo_path}${pypo_script}
+
 # EOF
diff --git a/python_apps/pypo/airtime-playout-init-d b/python_apps/pypo/airtime-playout-init-d
index 940ff8781..6beb333a0 100755
--- a/python_apps/pypo/airtime-playout-init-d
+++ b/python_apps/pypo/airtime-playout-init-d
@@ -4,17 +4,23 @@ USERID=pypo
 GROUPID=pypo
 NAME=Airtime
 
-DAEMON=/usr/bin/airtime-playout
-PIDFILE=/var/run/airtime.pid
+DAEMON0=/usr/bin/airtime-playout
+PIDFILE0=/var/run/airtime-playout.pid
+
+DAEMON1=/usr/bin/airtime-liquidsoap
+PIDFILE1=/var/run/airtime-liquidsoap.pid
 
 start () {    
-        start-stop-daemon --start --background --quiet --chuid $USERID:$GROUPID --make-pidfile --pidfile $PIDFILE --startas $DAEMON
+        start-stop-daemon --start --background --quiet --chuid $USERID:$GROUPID --make-pidfile --pidfile $PIDFILE0 --startas $DAEMON0
+        start-stop-daemon --start --background --quiet --chuid $USERID:$GROUPID --make-pidfile --pidfile $PIDFILE1 --startas $DAEMON1
 }
 
 stop () {
         # Send TERM after 5 seconds, wait at most 30 seconds.
-        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --pidfile $PIDFILE
-        rm -f $PIDFILE
+        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --pidfile $PIDFILE0
+        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --pidfile $PIDFILE1
+        rm -f $PIDFILE0
+        rm -f $PIDFILE1
 }
 
 
diff --git a/python_apps/pypo/install/pypo-install.py b/python_apps/pypo/install/pypo-install.py
index fe75d0860..c6b625fd7 100755
--- a/python_apps/pypo/install/pypo-install.py
+++ b/python_apps/pypo/install/pypo-install.py
@@ -89,9 +89,11 @@ try:
     sys.exit()
 
   current_script_dir = get_current_script_dir()
-  print "Checking and removing any existing pypo processes"
-  os.system("python %s/pypo-uninstall.py 1>/dev/null 2>&1"% current_script_dir)
-  time.sleep(5)
+  #print "Checking and removing any existing pypo processes"
+  #os.system("python %s/pypo-uninstall.py 1>/dev/null 2>&1"% current_script_dir)
+  #time.sleep(5)
+  p = Popen("/etc/init.d/airtime-playout stop", shell=True)
+  sts = os.waitpid(p.pid, 0)[1]
 
   # Create users
   create_user("pypo")
diff --git a/python_apps/pypo/install/pypo-uninstall.py b/python_apps/pypo/install/pypo-uninstall.py
index 36ae334e0..50910b06c 100755
--- a/python_apps/pypo/install/pypo-uninstall.py
+++ b/python_apps/pypo/install/pypo-uninstall.py
@@ -37,30 +37,17 @@ try:
         print 'Error loading config file: ', e
         sys.exit()
         
-    os.system("python /usr/bin/airtime-playout-stop")
+    os.system("/etc/init.d/airtime-playout stop")
         
     print "Removing cache directories"
     remove_path(config["cache_base_dir"])
     
     print "Removing symlinks"
-    os.system("rm -f /usr/bin/airtime-playout-start") 
-    os.system("rm -f /usr/bin/airtime-playout-stop") 
+    os.system("rm -f /usr/bin/airtime-playout")
     
     print "Removing pypo files"
     remove_path(config["bin_dir"])
     
-    print "Removing daemontool script pypo"
-    remove_path("/etc/service/pypo")
-
-    if os.path.exists("/etc/service/pypo-fetch"):
-        remove_path("/etc/service/pypo-fetch")
-
-    if os.path.exists("/etc/service/pypo-push"):
-        remove_path("/etc/service/pypo-push")
-        
-    print "Removing daemontool script pypo-liquidsoap"
-    remove_path("/etc/service/pypo-liquidsoap")
-
     remove_user("pypo")
     print "Pypo uninstall complete."
 except Exception, e:
diff --git a/python_apps/show-recorder/airtime-show-recorder b/python_apps/show-recorder/airtime-show-recorder
index f7409f600..9e10abed9 100755
--- a/python_apps/show-recorder/airtime-show-recorder
+++ b/python_apps/show-recorder/airtime-show-recorder
@@ -16,10 +16,11 @@ exec 2>&1
 
 export PYTHONPATH=${api_client_path}
 
-su ${recorder_user} -c "python -u ${recorder_path}${recorder_script}"
+
+#this line works: su ${recorder_user} -c "python -u ${recorder_path}${recorder_script}"
+exec python -u ${recorder_path}${recorder_script}
 
 #ecasound does not work when recorder script is called with setuidgid. 
 #setuidgid ${recorder_user} python -u ${recorder_path}${recorder_script}
 
-
 # EOF
diff --git a/python_apps/show-recorder/airtime-show-recorder-init-d b/python_apps/show-recorder/airtime-show-recorder-init-d
new file mode 100755
index 000000000..b77036a84
--- /dev/null
+++ b/python_apps/show-recorder/airtime-show-recorder-init-d
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+USERID=pypo
+GROUPID=pypo
+NAME=Airtime
+
+DAEMON=/usr/bin/airtime-show-recorder
+PIDFILE=/var/run/airtime-show-recorder.pid
+
+start () {    
+        start-stop-daemon --start --background --quiet --chuid $USERID:$GROUPID --make-pidfile --pidfile $PIDFILE --startas $DAEMON
+}
+
+stop () {
+        # Send TERM after 5 seconds, wait at most 30 seconds.
+        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --pidfile $PIDFILE
+        rm -f $PIDFILE
+}
+
+
+case "${1:-''}" in
+  'start')
+            # start commands here
+            echo -n "Starting $NAME: "
+            start
+            echo "Done."
+        ;;
+  'stop')
+            # stop commands here
+            echo -n "Stopping $NAME: "
+            stop
+            echo "Done."
+        ;;
+  'restart')
+           # restart commands here
+           echo -n "Restarting $NAME: "
+           stop
+           start
+           echo "Done."
+        ;;
+  'status')
+           # status commands here
+           /usr/bin/airtime-check-system
+        ;;
+  *)      # no parameter specified
+        echo "Usage: $SELF start|stop|restart|status"
+        exit 1
+        ;;
+esac
diff --git a/python_apps/show-recorder/install/recorder-install.py b/python_apps/show-recorder/install/recorder-install.py
index 0b49e9547..af2e6ac02 100755
--- a/python_apps/show-recorder/install/recorder-install.py
+++ b/python_apps/show-recorder/install/recorder-install.py
@@ -70,9 +70,11 @@ try:
     sys.exit()
 
   current_script_dir = get_current_script_dir()
-  print "Checking and removing any existing recorder processes"
-  os.system("python %s/recorder-uninstall.py 1>/dev/null 2>&1"% current_script_dir)
-  time.sleep(5)
+  #print "Checking and removing any existing recorder processes"
+  #os.system("python %s/recorder-uninstall.py 1>/dev/null 2>&1"% current_script_dir)
+  #time.sleep(5)
+  p = Popen("/etc/init.d/airtime-show-recorder stop", shell=True)
+  sts = os.waitpid(p.pid, 0)[1]
 
   # Create users
   create_user("pypo")
@@ -95,7 +97,10 @@ try:
 
   print "Creating symbolic links"
   os.system("rm -f /usr/bin/airtime-show-recorder")
-  os.system("ln -s "+config["bin_dir"]+"/airtime-show-recorder /usr/bin/") 
+  os.system("ln -s "+config["bin_dir"]+"/airtime-show-recorder /usr/bin/")
+
+  print "Installing show-recorder daemon"
+  shutil.copy(config["bin_dir"]+"/airtime-show-recorder-init-d", "/etc/init.d/airtime-show-recorder")
 
   print "Waiting for processes to start..."
   p = Popen("/etc/init.d/airtime-show-recorder start", shell=True)
diff --git a/python_apps/show-recorder/install/recorder-uninstall.py b/python_apps/show-recorder/install/recorder-uninstall.py
index fceabfa78..3fc3514e2 100755
--- a/python_apps/show-recorder/install/recorder-uninstall.py
+++ b/python_apps/show-recorder/install/recorder-uninstall.py
@@ -37,14 +37,13 @@ try:
         print 'Error loading config file: ', e
         sys.exit()
 
-    os.system("python /usr/bin/airtime-show-recorder-stop")
+    os.system("/etc/init.d/airtime-show-recorder stop")
     
     print "Removing log directories"
     remove_path(config["log_dir"])
     
     print "Removing symlinks"
-    os.system("rm -f /usr/bin/airtime-show-recorder-start")
-    os.system("rm -f /usr/bin/airtime-show-recorder-stop")
+    os.system("rm -f /usr/bin/airtime-show-recorder")
     
     print "Removing application files"
     remove_path(config["bin_dir"])
@@ -52,9 +51,6 @@ try:
     print "Removing media files"
     remove_path(config["base_recorded_files"])
     
-    print "Removing daemontool script recorder"
-    remove_path("rm -rf /etc/service/recorder")
-
     remove_user("pypo")
     print "Uninstall complete."
 except Exception, e:
diff --git a/utils/airtime-check-system.php b/utils/airtime-check-system.php
index fbd705514..a866f6a9a 100644
--- a/utils/airtime-check-system.php
+++ b/utils/airtime-check-system.php
@@ -24,7 +24,7 @@ AirtimeCheck::CheckRabbitMqConnection($airtimeIni);
 AirtimeCheck::CheckApacheVHostFiles();
 
 AirtimeCheck::GetAirtimeServerVersion($pypoCfg);
-AirtimeCheck::CheckPypoRunning();
+AirtimeCheck::CheckAirtimePlayoutRunning();
 AirtimeCheck::CheckLiquidsoapRunning();
 AirtimeCheck::CheckIcecastRunning();
 
@@ -71,38 +71,36 @@ class AirtimeCheck {
         }
     }
 
-    public static function CheckPypoRunning()
+    public static function CheckAirtimePlayoutRunning()
     {
-        $command = "sudo svstat /etc/service/pypo";
-        exec($command, $output, $result);
+
+        //check if airtime-playout.pid exists
+
+        //if it exists we need to get the process id
+        //from the file as well as the time the process
+        //has been running. We can get the latter from
+        //the timestamp of the file
+        $filename = "/var/run/airtime-playout.pid";
+
+        $pid = false;
+        $numSecondsRunning = 0;
+
+        if (file_exists($filename)){
+            //first get pid
+            $pid = trim(file_get_contents($filename));
 
 
-        $key_value = split(":", $output[0]);
-        $value = trim($key_value[1]);
+            //now lets get the running time
+            $lastModified = filemtime($filename);
+            $currentTime = time();
 
-        $status = AirtimeCheck::CHECK_FAILED;
-        $pos = strpos($value, "pid");
-        if ($pos !== false){
-            $start = $pos + 4;
-            $end = strpos($value, ")", $start);
-            $status = substr($value, $start, $end-$start);
-        } else {
-            self::$check_system_ok = false;
+            $numSecondsRunning = $currentTime - $lastModified;            
         }
 
-        output_status("PLAYOUT_ENGINE_PROCESS_ID", $status);
+        output_status("PLAYOUT_ENGINE_PROCESS_ID", $pid);
 
-        $status = AirtimeCheck::CHECK_FAILED;
-        $pos = strpos($value, ")");
-        if ($pos !== false){
-            $start = $pos + 2;
-            $end = strpos($value, " ", $start);
-            $status = substr($value, $start, $end-$start);
-        } else {
-            self::$check_system_ok = false;
-        }
-        output_status("PLAYOUT_ENGINE_RUNNING_SECONDS", $status);
-        if (is_numeric($status) && (int)$status < 3) {
+        output_status("PLAYOUT_ENGINE_RUNNING_SECONDS", $numSecondsRunning);
+        if (is_numeric($numSecondsRunning) && (int)$numSecondsRunning < 3) {
             self::$check_system_ok = false;
             output_msg("WARNING! It looks like the playout engine is continually restarting.");
             $command = "tail -10 /var/log/airtime/pypo/main/current";
@@ -115,39 +113,36 @@ class AirtimeCheck {
 
     public static function CheckLiquidsoapRunning()
     {
-        $command = "sudo svstat /etc/service/pypo-liquidsoap";
-        exec($command, $output, $result);
+        //check if airtime-playout.pid exists
 
-        $key_value = split(":", $output[0]);
-        $value = trim($key_value[1]);
+        //if it exists we need to get the process id
+        //from the file as well as the time the process
+        //has been running. We can get the latter from
+        //the timestamp of the file
+        $filename = "/var/run/airtime-liquidsoap.pid";
 
-        $status = AirtimeCheck::CHECK_FAILED;
-        $pos = strpos($value, "pid");
-        if ($pos !== false){
-            $start = $pos + 4;
-            $end = strpos($value, ")", $start);
-            $status = substr($value, $start, $end-$start);
-        } else {
-            self::$check_system_ok = false;
+        $pid = false;
+        $numSecondsRunning = 0;
+
+        if (file_exists($filename)){
+            //first get pid
+            $pid = trim(file_get_contents($filename));
+
+
+            //now lets get the running time
+            $lastModified = filemtime($filename);
+            $currentTime = time();
+
+            $numSecondsRunning = $currentTime - $lastModified;            
         }
 
-        output_status("LIQUIDSOAP_PROCESS_ID", $status);
+        output_status("LIQUIDSOAP_PROCESS_ID", $pid);
 
-        $status = AirtimeCheck::CHECK_FAILED;
-        $pos = strpos($value, ")");
-        if ($pos !== false){
-            $start = $pos + 2;
-            $end = strpos($value, " ", $start);
-            $status = substr($value, $start, $end-$start);
-        } else {
+        output_status("LIQUIDSOAP_RUNNING_SECONDS", $numSecondsRunning);
+        if (is_numeric($numSecondsRunning) && (int)$numSecondsRunning < 3) {
             self::$check_system_ok = false;
-        }
-
-        output_status("LIQUIDSOAP_RUNNING_SECONDS", $status);
-        if (is_numeric($status) && (int)$status < 3) {
-            self::$check_system_ok = false;
-            output_msg("WARNING! It looks like liquidsoap is continually restarting.");
-            $command = "tail -10 /var/log/airtime/pypo-liquidsoap/main/current";
+            output_msg("WARNING! It looks like the playout engine is continually restarting.");
+            $command = "tail -10 /var/log/airtime/pypo/main/current";
             exec($command, $output, $result);
             foreach ($output as $line) {
                 output_msg($line);