Alex Lourie has uploaded a new change for review.

Change subject: WIP: packaging: adding support for ServiceD
......................................................................

WIP: packaging: adding support for ServiceD

Change-Id: Ie33c898b4e4d0334f2e0ee84642dc29b3177d7db
Signed-off-by: Alex Lourie <[email protected]>
---
M packaging/fedora/setup/common_utils.py
M packaging/fedora/setup/output_messages.py
2 files changed, 52 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/28/11228/1

diff --git a/packaging/fedora/setup/common_utils.py 
b/packaging/fedora/setup/common_utils.py
index 9e001f1..65f37d4 100755
--- a/packaging/fedora/setup/common_utils.py
+++ b/packaging/fedora/setup/common_utils.py
@@ -1217,14 +1217,14 @@
     def __init__(self, name):
         self.wasStopped = False
         self.wasStarted = False
+        self.sysv = False
+        self.sysd = False
         self.name = name
-
-    def isServiceAvailable(self):
-        if os.path.exists("/etc/init.d/%s" % self.name):
-            return True
-        return False
+        self._initService()
 
     def start(self, raiseFailure = False):
+        if not self.available:
+            raise OSError(output_messages.ERR_FAILED_START_SERVICE % self.name)
         logging.debug("starting %s", self.name)
         (output, rc) = self._serviceFacility("start")
         if rc == 0:
@@ -1235,6 +1235,8 @@
         return (output, rc)
 
     def stop(self, raiseFailure = False):
+        if not self.available:
+            raise OSError(output_messages.ERR_FAILED_STOP_SERVICE % self.name)
         logging.debug("stopping %s", self.name)
         (output, rc) = self._serviceFacility("stop")
         if rc == 0:
@@ -1245,9 +1247,13 @@
         return (output, rc)
 
     def autoStart(self, start=True):
+        if not self.available:
+            raise OSError(output_messages.ERR_SERVICE_NOT_PRESENT)
         mode = "on" if start else "off"
         cmd = [
-            basedefs.EXEC_CHKCONFIG, self.name, mode,
+            basedefs.EXEC_CHKCONFIG,
+            self.name,
+            mode,
         ]
         execCmd(cmdList=cmd, failOnError=True)
 
@@ -1255,6 +1261,8 @@
         """
         Will only start if wasStopped is set to True
         """
+        if not self.available:
+            raise OSError(output_messages.ERR_SERVICE_NOT_PRESENT)
         if self.wasStopped:
             logging.debug("Service %s was stopped. starting it 
again"%self.name)
             return self.start(raiseFailure)
@@ -1263,6 +1271,8 @@
             return (False, False)
 
     def status(self):
+        if not self.available:
+            raise OSError(output_messages.ERR_SERVICE_NOT_PRESENT)
         logging.debug("getting status for %s", self.name)
         (output, rc) = self._serviceFacility("status")
         return (output, rc)
@@ -1272,32 +1282,52 @@
         Execute the command "service NAME action"
         returns: output, rc
         """
+        if not self.available:
+            raise OSError(output_messages.ERR_SERVICE_NOT_PRESENT)
         logging.debug("executing action %s on service %s", self.name, action)
-        cmd = [
-            basedefs.EXEC_SERVICE, self.name, action
-        ]
+        if self.sysd:
+            cmd = [
+                basedefs.EXEC_SYSTEMCTL,
+                action,
+                self.name
+            ]
+        else:
+            cmd = [
+                basedefs.EXEC_SERVICE,
+                self.name,
+                action
+            ]
         return execCmd(cmdList=cmd, usePipeFiles=True)
 
-    def available(self):
+    def _initService(self):
+        self._detectInitSystem()
+        if self.sysd:
+            self.name = self.name + ".service"
+
+        self.available = self._isServiceAvailable()
+
+    def _detectInitSystem(self):
+        if os.path.exists(basedefs.EXEC_SYSTEMCTL):
+            self.sysd = True
+        else:
+            self.sysv = True
+
+    def _isServiceAvailable(self):
         logging.debug("checking if %s service is available", self.name)
 
-        # Checks if systemd service available
-        cmd = [
-            basedefs.EXEC_SYSTEMCTL,
-            "show",
-            "%s.service" % self.name
-        ]
-        if os.path.exists(basedefs.EXEC_SYSTEMCTL):
-            out, rc = execCmd(cmdList=cmd)
-            sysd = "LoadState=loaded" in out
-        else:
-            sysd = False
+        # Checks if SystemD service available
+        if self.sysd:
+            out, rc = self._serviceFacility("is-enabled")
+            sysd = "enabled" in out and rc == 0
 
         # Checks if systemV service available
         sysv = os.path.exists("/etc/init.d/%s" % self.name)
 
         return (sysd or sysv)
 
+    def available(self):
+        return self.service_available
+
 def chown(target,uid, gid):
     logging.debug("chown %s to %s:%s" % (target, uid, gid))
     os.chown(target, uid, gid)
diff --git a/packaging/fedora/setup/output_messages.py 
b/packaging/fedora/setup/output_messages.py
index 7bbd8f6..e517f48 100644
--- a/packaging/fedora/setup/output_messages.py
+++ b/packaging/fedora/setup/output_messages.py
@@ -264,6 +264,7 @@
 for the maintenance"
 
 #START NFS SERVICE
+ERR_SERVICE_NOT_PRESENT = "Error: service %s was not found on the system"
 ERR_FAILED_TO_START_NFS_SERVICE="Failed to start the NFS services"
 ERR_RESTARTING_NFS_SERVICE="Failed starting the %s service"
 ERR_FAILED_CHKCFG_NFS="Failed to configure %s service to start on boot"


--
To view, visit http://gerrit.ovirt.org/11228
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie33c898b4e4d0334f2e0ee84642dc29b3177d7db
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alex Lourie <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to