Change in vdsm[master]: vdsm-tool: avoid service action redirections

zhshzhou at linux.vnet.ibm.com zhshzhou at linux.vnet.ibm.com
Mon May 27 14:43:15 UTC 2013


Zhou Zheng Sheng has uploaded a new change for review.

Change subject: vdsm-tool: avoid service action redirections
......................................................................

vdsm-tool: avoid service action redirections

Some smart init system will redirect actions to other init system when
the service is not managed natively. This behaviour interferes the
parsing of the service action output.

Add a decorator _XXXNative to check if a service is managed by a certain
init system. _XXXNative tag is then added to every action function to
avoid execution of an action on a service that is not managed by that
particular init system, so as to avoid redirections.

An environment variable is also added for SysV actions to prevent
redirection to SystemD, this is specific for those systems with
co-existence of SystemD and SysV, and is no harm to those systems with
only SysV or with co-existence of Upstart and SysV.

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


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/99/15099/1

diff --git a/lib/vdsm/tool/service.py b/lib/vdsm/tool/service.py
index 8f69f41..b483ed5 100644
--- a/lib/vdsm/tool/service.py
+++ b/lib/vdsm/tool/service.py
@@ -31,8 +31,8 @@
 from vdsm.utils import execCmd as _execCmd
 
 
-def execCmd(argv, raw=True):
-    return _execCmd(argv, raw=raw)
+def execCmd(argv, raw=True, *args, **kwargs):
+    return _execCmd(argv, raw=raw, *args, **kwargs)
 
 
 _SYSTEMCTL = CommandPath("systemctl",
@@ -76,9 +76,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
@@ -122,6 +122,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"
@@ -132,6 +144,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'
@@ -142,6 +155,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)
@@ -150,12 +164,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, "", ""
@@ -170,30 +186,47 @@
     _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:
+    _sysvEnv = {'SYSTEMCTL_SKIP_REDIRECT': '1'}
+    _execSysvEnv = functools.partial(execCmd, env=_sysvEnv)
+
+    @_sysvNative
     def _serviceStart(srvName):
         cmd = [_SERVICE.cmd, srvName, "start"]
-        return execCmd(cmd)
+        return _execSysvEnv(cmd)
 
+    @_sysvNative
     def _serviceStop(srvName):
         cmd = [_SERVICE.cmd, srvName, "stop"]
-        return execCmd(cmd)
+        return _execSysvEnv(cmd)
 
+    @_sysvNative
     def _serviceStatus(srvName):
         cmd = [_SERVICE.cmd, srvName, "status"]
-        rc, out, err = execCmd(cmd)
+        rc, out, err = _execSysvEnv(cmd)
         if rc == 0:
             # certain service rc is 0 even though the service is stopped
             rc = _isStopped(out)
         return (rc, out, err)
 
+    @_sysvNative
     def _serviceRestart(srvName):
         cmd = [_SERVICE.cmd, srvName, "restart"]
-        return execCmd(cmd)
+        return _execSysvEnv(cmd)
 
     _srvStartAlts.append(_serviceStart)
     _srvStopAlts.append(_serviceStop)
@@ -206,6 +239,7 @@
 except OSError:
     pass
 else:
+    @_sysvNative
     def _chkconfigDisable(srvName):
         cmd = [_CHKCONFIG.cmd, srvName, "off"]
         return execCmd(cmd)
@@ -218,6 +252,7 @@
 except OSError:
     pass
 else:
+    @_sysvNative
     def _updatercDisable(srvName):
         cmd = [_UPDATERC.cmd, srvName, "disable"]
         return execCmd(cmd)


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idff541a724e3e2d2167b1f235af5ed04a3dc2ee0
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