Change in vdsm[master]: WIP: Refactoring moving watchCmd from storage.misc to lib/ut...

gvallare at redhat.com gvallare at redhat.com
Fri May 3 16:46:38 UTC 2013


Giuseppe Vallarelli has uploaded a new change for review.

Change subject: WIP: Refactoring moving watchCmd from storage.misc to lib/utils
......................................................................

WIP: Refactoring moving watchCmd from storage.misc to lib/utils

This refactoring is needed to break the depedency
of lib/vdsm/qemuImg from vdsm/storage/misc.

Change-Id: I45eaf487b0260863af09087e5114fb836276a750
Signed-off-by: Giuseppe Vallarelli <gvallare at redhat.com>
---
M lib/vdsm/qemuImg.py
M lib/vdsm/utils.py
M tests/main.py
M tests/miscTests.py
M vdsm/storage/misc.py
M vdsm/storage/storage_exception.py
6 files changed, 93 insertions(+), 91 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/08/14408/1

diff --git a/lib/vdsm/qemuImg.py b/lib/vdsm/qemuImg.py
index 297ad53..2e5d487 100644
--- a/lib/vdsm/qemuImg.py
+++ b/lib/vdsm/qemuImg.py
@@ -20,7 +20,7 @@
 
 import re
 from storage import misc
-from utils import CommandPath, execCmd
+from utils import CommandPath, execCmd, watchCmd
 
 _qemuimg = CommandPath("qemu-img",
                        "/usr/bin/qemu-img",  # Fedora, EL6
@@ -114,9 +114,9 @@
 
     cmd.append(dstVolPath)
 
-    (rc, out, err) = misc.watchCmd(cmd, stop=stop,
-                                   nice=misc.NICENESS.LOW,
-                                   ioclass=misc.IOCLASS.IDLE)
+    (rc, out, err) = watchCmd(cmd, stop=stop,
+                              nice=misc.NICENESS.LOW,
+                              ioclass=misc.IOCLASS.IDLE)
 
     if rc != 0:
         raise QImgError(rc, out, err)
diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index cfec8cc..047391a 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -62,6 +62,25 @@
     _THP_STATE_PATH = '/sys/kernel/mm/redhat_transparent_hugepage/enabled'
 
 
+class GeneralException(Exception):
+    code = 100
+    message = "General Exception"
+
+    def __init__(self, *value):
+        self.value = value
+
+    def __str__(self):
+        return "%s: %s" % (self.message, repr(self.value))
+
+    def response(self):
+        return {'status': {'code': self.code, 'message': str(self)}}
+
+
+class ActionStopped(GeneralException):
+    code = 443
+    message = "Action was stopped"
+
+
 def isBlockDevice(path):
     path = os.path.abspath(path)
     return stat.S_ISBLK(os.stat(path).st_mode)
@@ -452,6 +471,35 @@
     return (p.returncode, out, err)
 
 
+def stripNewLines(lines):
+    return [l[:-1] if l.endswith('\n') else l for l in lines]
+
+
+def watchCmd(command, stop, cwd=None, data=None, recoveryCallback=None,
+             nice=None, ioclass=None, execCmdLogger=logging.root):
+    """
+    Executes an external command, optionally via sudo with stop abilities.
+    """
+    proc = execCmd(command, sudo=False, cwd=cwd, data=data, sync=False,
+                   nice=nice, ioclass=ioclass)
+    if recoveryCallback:
+        recoveryCallback(proc)
+
+    if not proc.wait(cond=stop):
+        proc.kill()
+        raise ActionStopped()
+
+    out = stripNewLines(proc.stdout)
+    err = stripNewLines(proc.stderr)
+
+    execCmdLogger.debug("%s: <err> = %s; <rc> = %d",
+                        {True: "SUCCESS", False: "FAILED"}
+                        [proc.returncode == 0],
+                        repr(err), proc.returncode)
+
+    return (proc.returncode, out, err)
+
+
 def checkPathStat(pathToCheck):
     try:
         startTime = time.time()
diff --git a/tests/main.py b/tests/main.py
index 145af3b..b3be852 100644
--- a/tests/main.py
+++ b/tests/main.py
@@ -21,6 +21,7 @@
 import types
 import unittest
 from storage import storage_exception
+from vdsm import utils
 from storage import securable
 from testrunner import VdsmTestCase as TestCaseBase
 from gluster import exception as gluster_exception
@@ -48,7 +49,7 @@
             if not isinstance(obj, types.TypeType):
                 continue
 
-            if not issubclass(obj, storage_exception.GeneralException):
+            if not issubclass(obj, utils.GeneralException):
                 continue
 
             self.assertFalse(obj.code in codes)
diff --git a/tests/miscTests.py b/tests/miscTests.py
index 77e78d6..d685385 100644
--- a/tests/miscTests.py
+++ b/tests/miscTests.py
@@ -56,7 +56,7 @@
 
 
 def watchCmd(cmd, stop, cwd=None, data=None, recoveryCallback=None):
-    ret, out, err = misc.watchCmd(cmd, stop, cwd=cwd, data=data,
+    ret, out, err = utils.watchCmd(cmd, stop, cwd=cwd, data=data,
                                   recoveryCallback=recoveryCallback)
 
     return ret, out, err
@@ -511,7 +511,7 @@
                 os.mkfifo(src.name)
                 with tempfile.NamedTemporaryFile() as dst:
                     ddWatchCopy(src.name, dst.name, lambda: True, 100)
-        except misc.se.ActionStopped:
+        except utils.ActionStopped:
             self.log.info("Looks like it stopped!")
         else:
             self.fail("Copying didn't stop!")
@@ -936,7 +936,7 @@
         sleepTime = "10"
         try:
             watchCmd([EXT_SLEEP, sleepTime], lambda: True)
-        except misc.se.ActionStopped:
+        except utils.ActionStopped:
             self.log.info("Looks like task stopped!")
         else:
             self.fail("watchCmd didn't stop!")
diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py
index b500f25..61eec4e 100644
--- a/vdsm/storage/misc.py
+++ b/vdsm/storage/misc.py
@@ -72,10 +72,6 @@
     return dict(imap(lambda f: (f, getattr(nt, f)), nt._fields))
 
 
-def stripNewLines(lines):
-    return [l[:-1] if l.endswith('\n') else l for l in lines]
-
-
 class _LogSkip(object):
     _ignoreMap = defaultdict(list)
     ALL_KEY = "##ALL##"
@@ -207,31 +203,6 @@
                       "Could not find process with pid %s" % pid)
 
     return str(ctime)
-
-
-def watchCmd(command, stop, cwd=None, data=None, recoveryCallback=None,
-             nice=None, ioclass=None):
-    """
-    Executes an external command, optionally via sudo with stop abilities.
-    """
-    proc = execCmd(command, sudo=False, cwd=cwd, data=data, sync=False,
-                   nice=nice, ioclass=ioclass)
-    if recoveryCallback:
-        recoveryCallback(proc)
-
-    if not proc.wait(cond=stop):
-        proc.kill()
-        raise se.ActionStopped()
-
-    out = stripNewLines(proc.stdout)
-    err = stripNewLines(proc.stderr)
-
-    execCmdLogger.debug("%s: <err> = %s; <rc> = %d",
-                        {True: "SUCCESS", False: "FAILED"}
-                        [proc.returncode == 0],
-                        repr(err), proc.returncode)
-
-    return (proc.returncode, out, err)
 
 
 def readfile(name, buffersize=None):
@@ -371,9 +342,9 @@
             (rc, out, err) = execCmd(cmd, sudo=False, nice=NICENESS.LOW,
                                      ioclass=IOCLASS.IDLE)
         else:
-            (rc, out, err) = watchCmd(cmd, stop=stop,
-                                      recoveryCallback=recoveryCallback,
-                                      nice=NICENESS.LOW, ioclass=IOCLASS.IDLE)
+            (rc, out, err) = utils.watchCmd(cmd, stop=stop,
+                                            recoveryCallback=recoveryCallback,
+                                            nice=NICENESS.LOW, ioclass=IOCLASS.IDLE)
 
         if rc:
             raise se.MiscBlockWriteException(dst, offset, size)
diff --git a/vdsm/storage/storage_exception.py b/vdsm/storage/storage_exception.py
index c2c3dc8..e04d68c 100644
--- a/vdsm/storage/storage_exception.py
+++ b/vdsm/storage/storage_exception.py
@@ -32,6 +32,7 @@
 #######################################################
 
 from securable import SecureError
+from vdsm import utils
 SPM_STATUS_ERROR = (654, "Not SPM")
 
 GENERAL_EXCEPTION = lambda e: (100, str(e))
@@ -50,25 +51,11 @@
     return {'status': {'code': code, 'message': msg}}
 
 
-class GeneralException(Exception):
-    code = 100
-    message = "General Exception"
-
-    def __init__(self, *value):
-        self.value = value
-
-    def __str__(self):
-        return "%s: %s" % (self.message, repr(self.value))
-
-    def response(self):
-        return {'status': {'code': self.code, 'message': str(self)}}
-
-
 #################################################
 # Validation Exceptions
 #################################################
 
-class InvalidParameterException(GeneralException):
+class InvalidParameterException(utils.GeneralException):
     code = 1000
     message = "Invalid parameter"
 
@@ -76,7 +63,7 @@
         self.value = "%s=%s" % (name, value)
 
 
-class InvalidDefaultExceptionException(GeneralException):
+class InvalidDefaultExceptionException(utils.GeneralException):
     code = 1001
     message = "Cannot set exception as default, type not supported"
 
@@ -85,12 +72,12 @@
 # General Storage Exceptions
 #################################################
 
-class StorageException(GeneralException):
+class StorageException(utils.GeneralException):
     code = 200
     message = "General Storage Exception"
 
 
-class ResourceException(GeneralException):
+class ResourceException(utils.GeneralException):
     code = 3000
 
     def __init__(self, UUID):
@@ -100,7 +87,7 @@
         return "%s, UUID: %s" % (self.message, repr(self.value))
 
 
-class VolumeGeneralException(GeneralException):
+class VolumeGeneralException(utils.GeneralException):
     code = 4000
 
     def __init__(self, volume, *args):
@@ -116,7 +103,7 @@
             self.value += list(args)
 
 
-class UnicodeArgumentException(GeneralException):
+class UnicodeArgumentException(utils.GeneralException):
     code = 4900
     message = "Unicode arguments are not supported"
 
@@ -125,7 +112,7 @@
 # Misc Exceptions
 #################################################
 
-class NotImplementedException(GeneralException):
+class NotImplementedException(utils.GeneralException):
     code = 2000
     message = "Method not implemented"
 
@@ -910,37 +897,37 @@
 # Task Exceptions
 #################################################
 
-class InvalidTask(GeneralException):
+class InvalidTask(utils.GeneralException):
     code = 400
     message = "Task invalid"
 
 
-class UnknownTask(GeneralException):
+class UnknownTask(utils.GeneralException):
     code = 401
     message = "Task id unknown"
 
 
-class TaskClearError(GeneralException):
+class TaskClearError(utils.GeneralException):
     code = 402
     message = "Could not clear task"
 
 
-class TaskNotFinished(GeneralException):
+class TaskNotFinished(utils.GeneralException):
     code = 403
     message = "Task not finished"
 
 
-class InvalidTaskType(GeneralException):
+class InvalidTaskType(utils.GeneralException):
     code = 404
     message = "Invalid task type"
 
 
-class AddTaskError(GeneralException):
+class AddTaskError(utils.GeneralException):
     code = 405
     message = "TaskManager error, unable to add task"
 
 
-class TaskInProgress(GeneralException):
+class TaskInProgress(utils.GeneralException):
     code = 406
     message = "Running Task in progress"
 
@@ -948,27 +935,27 @@
         self.value = "Pool %s task %s" % (spUUID, task)
 
 
-class TaskMetaDataSaveError(GeneralException):
+class TaskMetaDataSaveError(utils.GeneralException):
     code = 407
     message = "Can't save Task Metadata"
 
 
-class TaskMetaDataLoadError(GeneralException):
+class TaskMetaDataLoadError(utils.GeneralException):
     code = 408
     message = "Can't load Task Metadata"
 
 
-class TaskDirError(GeneralException):
+class TaskDirError(utils.GeneralException):
     code = 409
     message = "can't find/access task dir"
 
 
-class TaskStateError(GeneralException):
+class TaskStateError(utils.GeneralException):
     code = 410
     message = "Operation is not allowed in this task state"
 
 
-class TaskAborted(GeneralException):
+class TaskAborted(utils.GeneralException):
     code = 411
     message = "Task is aborted"
 
@@ -981,44 +968,39 @@
                 (self.message, repr(self.value), self.abortedcode))
 
 
-class UnmanagedTask(GeneralException):
+class UnmanagedTask(utils.GeneralException):
     code = 412
     message = "Operation can't be performed on unmanaged task"
 
 
-class TaskPersistError(GeneralException):
+class TaskPersistError(utils.GeneralException):
     code = 413
     message = "Can't persist task"
 
 
-class InvalidJob(GeneralException):
+class InvalidJob(utils.GeneralException):
     code = 420
     message = "Job is invalid"
 
 
-class InvalidRecovery(GeneralException):
+class InvalidRecovery(utils.GeneralException):
     code = 430
     message = "Recovery is invalid"
 
 
-class InvalidTaskMng(GeneralException):
+class InvalidTaskMng(utils.GeneralException):
     code = 440
     message = "invalid Task Manager"
 
 
-class TaskStateTransitionError(GeneralException):
+class TaskStateTransitionError(utils.GeneralException):
     code = 441
     message = "cannot move task to requested state"
 
 
-class TaskHasRefs(GeneralException):
+class TaskHasRefs(utils.GeneralException):
     code = 442
     message = "operation cannot be performed - task has active references"
-
-
-class ActionStopped(GeneralException):
-    code = 443
-    message = "Action was stopped"
 
 
 #################################################
@@ -1691,33 +1673,33 @@
 #  Resource Exceptions
 #################################################
 
-class ResourceNamespaceNotEmpty(GeneralException):
+class ResourceNamespaceNotEmpty(utils.GeneralException):
     code = 850
     message = "Resource Namespace is not empty"
 
 
-class ResourceTimeout(GeneralException):
+class ResourceTimeout(utils.GeneralException):
     code = 851
     message = "Resource timeout"
 
 
-class ResourceDoesNotExist(GeneralException):
+class ResourceDoesNotExist(utils.GeneralException):
     code = 852
     message = "Resource does not exist"
 
 
-class InvalidResourceName(GeneralException):
+class InvalidResourceName(utils.GeneralException):
     code = 853
     message = "Invalid resource name"
 
 
-class ResourceReferenceInvalid(GeneralException):
+class ResourceReferenceInvalid(utils.GeneralException):
     code = 854
     message = ("Cannot perform operation. This resource has been released or "
                "expired.")
 
 
-class ResourceAcqusitionFailed(GeneralException):
+class ResourceAcqusitionFailed(utils.GeneralException):
     code = 855
     message = ("Could not acquire resource. "
                "Probably resource factory threw an exception.")


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I45eaf487b0260863af09087e5114fb836276a750
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Giuseppe Vallarelli <gvallare at redhat.com>


More information about the vdsm-patches mailing list