Change in vdsm[master]: Extend vdsm.tool.service to cover alternative service names

zhshzhou at linux.vnet.ibm.com zhshzhou at linux.vnet.ibm.com
Tue May 14 06:41:05 UTC 2013


Zhou Zheng Sheng has uploaded a new change for review.

Change subject: Extend vdsm.tool.service to cover alternative service names
......................................................................

Extend vdsm.tool.service to cover alternative service names

Service names are different on various systems, instead of hard-coded
service names for each supported system, we can extend vdsm.tool.service
to detect the right service name given some hints (possible alternative
names). This makes the service management more flexible and helps to
port us to new systems easily.

This patch detect each alternative service name before actual commiting
the management operation. This is done by decorating each management
operation with _XXXNative tag, and iterating over the alternative
service names list for each management operation.

Change-Id: I5308c9db0399dbe9c4f0d6308943307e51d3d447
Signed-off-by: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
---
M lib/vdsm/tool/service.py
1 file changed, 52 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/20/14720/1

diff --git a/lib/vdsm/tool/service.py b/lib/vdsm/tool/service.py
index f71f714..4aafb67 100644
--- a/lib/vdsm/tool/service.py
+++ b/lib/vdsm/tool/service.py
@@ -56,6 +56,13 @@
                         "/usr/sbin/update-rc.d",
                         )
 
+_srvNameAlts = {
+    'iscsid': ['iscsid', 'open-iscsi'],
+    'libvirtd': ['libvirtd', 'libvirt-bin'],
+    'multipathd': ['multipathd', 'multipath-tools'],
+    'network': ['network', 'networking'],
+}
+
 _srvStartAlts = []
 _srvStopAlts = []
 _srvStatusAlts = []
@@ -75,9 +82,9 @@
             if rc != 0:
                 return (rc, out, err)
             for line in out:
-                if srvName + ".service" == line.split(" ", 1):
+                if srvName + ".service" == line.split(" ", 1)[0]:
                     return systemctlFun(srvName)
-            return (1, "", "%s is not native systemctl service")
+            return (1, "", "%s is not native systemctl service" % srvName)
         return wrapper
 
     @_systemctlNative
@@ -121,6 +128,18 @@
 except OSError:
     pass
 else:
+    def _initctlNative(initctlFun):
+        @functools.wraps(initctlFun)
+        def wrapper(srvName):
+            cmd = [_INITCTL.cmd, "usage", srvName]
+            rc, out, err = execCmd(cmd, raw=False)
+            if rc != 0:
+                return (1, "", "%s is not an Upstart service" % srvName)
+
+            return initctlFun(srvName)
+        return wrapper
+
+    @_initctlNative
     def _initctlStart(srvName):
         cmd = [_INITCTL.cmd, "start", srvName]
         alreadyRunRegex = r"\bis already running\b"
@@ -131,6 +150,7 @@
             rc = int(not re.search(alreadyRunRegex, err, re.MULTILINE))
         return (rc, out, err)
 
+    @_initctlNative
     def _initctlStop(srvName):
         cmd = [_INITCTL.cmd, "stop", srvName]
         alreadyStoppedRegex = r'\bUnknown instance\b'
@@ -141,6 +161,7 @@
             rc = int(not re.search(alreadyStoppedRegex, err, re.MULTILINE))
         return (rc, out, err)
 
+    @_initctlNative
     def _initctlStatus(srvName):
         cmd = [_INITCTL.cmd, "status", srvName]
         rc, out, err = execCmd(cmd)
@@ -149,12 +170,14 @@
             rc = _isStopped(out)
         return (rc, out, err)
 
+    @_initctlNative
     def _initctlRestart(srvName):
         # "initctl restart someSrv" will not restart the service if it is
         # already running, so we force it to do so
         _initctlStop(srvName)
         return _initctlStart(srvName)
 
+    @_initctlNative
     def _initctlDisable(srvName):
         if not os.path.isfile("/etc/init/%s.conf" % srvName):
             return 1, "", ""
@@ -169,19 +192,32 @@
     _srvDisableAlts.append(_initctlDisable)
 
 
+def _sysvNative(sysvFun):
+    @functools.wraps(sysvFun)
+    def wrapper(srvName):
+        srvPath = os.path.join(os.sep + 'etc', 'init.d', srvName)
+        if os.path.exists(srvPath):
+            return sysvFun(srvName)
+
+        return (1, "", "%s is not a SysV service" % srvName)
+    return wrapper
+
 try:
     _SERVICE.cmd
 except OSError:
     pass
 else:
+    @_sysvNative
     def _serviceStart(srvName):
         cmd = [_SERVICE.cmd, srvName, "start"]
         return execCmd(cmd)
 
+    @_sysvNative
     def _serviceStop(srvName):
         cmd = [_SERVICE.cmd, srvName, "stop"]
         return execCmd(cmd)
 
+    @_sysvNative
     def _serviceStatus(srvName):
         cmd = [_SERVICE.cmd, srvName, "status"]
         rc, out, err = execCmd(cmd)
@@ -190,6 +226,7 @@
             rc = _isStopped(out)
         return (rc, out, err)
 
+    @_sysvNative
     def _serviceRestart(srvName):
         cmd = [_SERVICE.cmd, srvName, "restart"]
         return execCmd(cmd)
@@ -205,6 +242,7 @@
 except OSError:
     pass
 else:
+    @_sysvNative
     def _chkconfigDisable(srvName):
         cmd = [_CHKCONFIG.cmd, srvName, "off"]
         return execCmd(cmd)
@@ -217,6 +255,7 @@
 except OSError:
     pass
 else:
+    @_sysvNative
     def _updatercDisable(srvName):
         cmd = [_UPDATERC.cmd, srvName, "disable"]
         return execCmd(cmd)
@@ -224,18 +263,19 @@
     _srvDisableAlts.append(_updatercDisable)
 
 
-def _runAlts(alts, *args, **kwarg):
+def _runAlts(actionAlts, srvName, *args, **kwarg):
     errors = {}
-    for alt in alts:
-        try:
-            rc, out, err = alt(*args, **kwarg)
-        except Exception as e:
-            errors[alt.func_name] = e
-        else:
-            if rc == 0:
-                return
+    for action in actionAlts:
+        for srv in _srvNameAlts.get(srvName, [srvName]):
+            try:
+                rc, out, err = action(srv, *args, **kwarg)
+            except Exception as e:
+                errors[action.func_name] = e
             else:
-                errors[alt.func_name] = (rc, out, err)
+                if rc == 0:
+                    return
+                else:
+                    errors[action.func_name] = (rc, out, err)
     raise RuntimeError("Tried all alternatives but failed:\n%s" % errors)
 
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5308c9db0399dbe9c4f0d6308943307e51d3d447
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>


More information about the vdsm-patches mailing list