From d5801f182dd5689949cc1e9b198e504ac416027d Mon Sep 17 00:00:00 2001
From: Aaron Raddon <araddon@yahoo.com>
Date: Sun, 8 Feb 2009 19:00:48 -0700
Subject: [PATCH] changes to service module to get at least partially working in ubuntu, get_running still doesnt work, no suitable package to mirror /sbin/service --status-all

---
 func/minion/modules/service.py |   43 +++++++++++++++++++++++++++-------------
 1 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/func/minion/modules/service.py b/func/minion/modules/service.py
index 1a1eefc..51aaf07 100644
--- a/func/minion/modules/service.py
+++ b/func/minion/modules/service.py
@@ -18,22 +18,37 @@ import func_module
 import sub_process
 import os
 
-class Service(func_module.FuncModule):
+service_path = "/sbin/service"
+chkconfig_path = "/sbin/chkconfig"
+if not os.path.exists(service_path): # not rh
+    if os.path.exists("/usr/sbin/service"):
+        service_path = "/usr/sbin/service" # debian apt-get install sysvconfig
+    else:
+        print('Missing dependency for Service')
+    if os.path.exists("/usr/sbin/sysv-rc-conf"):
+        chkconfig_path = "/usr/sbin/sysv-rc-conf" # debian apt-get install sysv-rc-conf
+    else:
+        print('Missing dependency for sysv-rc-conf')
 
+class Service(func_module.FuncModule):
+    
     version = "0.0.1"
     api_version = "0.0.1"
     description = "Allows for service control via func."
-
+    
     def __command(self, service_name, command):
-	
-	service_name = service_name.strip() # remove useless spaces
-
-        filename = os.path.join("/etc/rc.d/init.d/",service_name)
-        if os.path.exists(filename):
-            return sub_process.call(["/sbin/service", service_name, command], close_fds=True)
-        else:
-            raise codes.FuncException("Service not installed: %s" % service_name)
-
+        service_name = service_name.strip() # remove useless spaces
+        filename = None
+        if os.path.exists("/etc/rc.d/"): # rh
+            filename = os.path.join("/etc/rc.d/init.d/",service_name)
+            if os.path.exists(filename):
+                return sub_process.call([service_path, service_name, command], close_fds=True)
+        else: # non rh
+            filename = os.path.join("/etc/init.d/",service_name)
+            if os.path.exists(filename):
+                return sub_process.call(["/etc/init.d", service_name, command], close_fds=True)
+        raise codes.FuncException("Service not installed: %s" % service_name)
+    
     def start(self, service_name):
         return self.__command(service_name, "start")
 
@@ -60,8 +75,8 @@ class Service(func_module.FuncModule):
         Get the list of services that are enabled at the various runlevels.  Xinetd services
         only provide whether or not they are running, not specific runlevel info.
         """
-
-        chkconfig = sub_process.Popen(["/sbin/chkconfig", "--list"], stdout=sub_process.PIPE, close_fds=True)
+        
+        chkconfig = sub_process.Popen([chkconfig_path, "--list"], stdout=sub_process.PIPE, close_fds=True)
         data = chkconfig.communicate()[0]
         results = []
         for line in data.split("\n"):
@@ -80,7 +95,7 @@ class Service(func_module.FuncModule):
         """
         Get a list of which services are running, stopped, or disabled.
         """
-        chkconfig = sub_process.Popen(["/sbin/service", "--status-all"], stdout=sub_process.PIPE, close_fds=True)
+        chkconfig = sub_process.Popen([service_path, "--status-all"], stdout=sub_process.PIPE, close_fds=True)
         data = chkconfig.communicate()[0]
         results = []
         for line in data.split("\n"):
-- 
1.5.5.3

