Change in vdsm[master]: vdsm-tool: Add command name to args for command

dkuznets at redhat.com dkuznets at redhat.com
Tue Apr 8 16:46:55 UTC 2014


Dima Kuznetsov has uploaded a new change for review.

Change subject: vdsm-tool: Add command name to args for command
......................................................................

vdsm-tool: Add command name to args for command

Each command now receives its own copy of 'sys.argv'-like argument list,
starting with command name and all its arguments.

Added exception each command can throw in case wrong params were passed.
Exception handler for this exception prints the doc string for the
command.

Some command functions were split because they're called both from vdsm
code and vdsm-tool code, so the vdsm-tool expose goes to a wrapper
function first and ensures the parameters are there, then passes to the
actual function.

Change-Id: I831bde947f451805e086e25f1c0b9943b17e7bd8
Signed-off-by: Dima Kuznetsov <dkuznets at redhat.com>
---
M lib/vdsm/tool/__init__.py
M lib/vdsm/tool/configurator.py
M lib/vdsm/tool/dummybr.py
M lib/vdsm/tool/load_needed_modules.py.in
M lib/vdsm/tool/nwfilter.py
M lib/vdsm/tool/passwd.py
M lib/vdsm/tool/restore_nets.py
M lib/vdsm/tool/seboolsetup.py
M lib/vdsm/tool/service.py
M lib/vdsm/tool/transient.py
M lib/vdsm/tool/validate_ovirt_certs.py.in
M lib/vdsm/tool/vdsm-id.py
M vdsm-tool/vdsm-tool
13 files changed, 180 insertions(+), 49 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/75/26575/1

diff --git a/lib/vdsm/tool/__init__.py b/lib/vdsm/tool/__init__.py
index 863ad51..9323835 100644
--- a/lib/vdsm/tool/__init__.py
+++ b/lib/vdsm/tool/__init__.py
@@ -34,3 +34,9 @@
 class VdsmToolNotRootError(VdsmToolRuntimeError):
     def __init__(self):
         VdsmToolRuntimeError.__init__(self, "Must run as root")
+
+
+class VdsmToolWrongArgsError(VdsmToolRuntimeError):
+    def __init__(self):
+        VdsmToolRuntimeError.__init__(self,
+                                      "Wrong parameters passed to command")
diff --git a/lib/vdsm/tool/configurator.py b/lib/vdsm/tool/configurator.py
index 601288f..7561cf6 100644
--- a/lib/vdsm/tool/configurator.py
+++ b/lib/vdsm/tool/configurator.py
@@ -213,7 +213,7 @@
     """
     Configure external services for vdsm
     """
-    args = _parse_args("configure")
+    args = _parse_args(*args)
     configurer_to_trigger = []
 
     sys.stdout.write("\nChecking configuration status...\n\n")
@@ -257,7 +257,7 @@
     Determine if module is configured
     """
     ret = True
-    args = _parse_args('is-configured')
+    args = _parse_args(*args)
 
     m = [
         c.getName() for c in __configurers
@@ -292,7 +292,7 @@
     Determine if configuration is valid
     """
     ret = True
-    args = _parse_args('validate-config')
+    args = _parse_args(*args)
 
     m = [
         c.getName() for c in __configurers
@@ -309,7 +309,7 @@
         raise VdsmToolRuntimeError("Config is not valid. Check conf files")
 
 
-def _parse_args(action):
+def _parse_args(action, *args):
     parser = argparse.ArgumentParser('vdsm-tool %s' % (action))
     allModules = [n.getName() for n in __configurers]
     parser.add_argument(
@@ -334,7 +334,7 @@
             action='store_true',
             help='Force configuration, trigger services restart',
         )
-    args = parser.parse_args(sys.argv[2:])
+    args = parser.parse_args(args)
     if not args.modules:
         args.modules = allModules
     return args
diff --git a/lib/vdsm/tool/dummybr.py b/lib/vdsm/tool/dummybr.py
index 59a43d2..9daafe6 100644
--- a/lib/vdsm/tool/dummybr.py
+++ b/lib/vdsm/tool/dummybr.py
@@ -23,7 +23,7 @@
 
 from ..netinfo import DUMMY_BRIDGE
 from .. import libvirtconnection, utils, constants
-from . import expose, VdsmToolRuntimeError
+from . import expose, VdsmToolRuntimeError, VdsmToolWrongArgsError
 
 
 def createEphemeralBridge(bridgeName):
@@ -43,10 +43,15 @@
 
 
 @expose('dummybr')
-def main():
+def main(*args):
     """
+    dummybr
     Defines dummy bridge on libvirt network.
     """
+
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     if not os.path.exists('/sys/class/net/%s' % DUMMY_BRIDGE):
         createEphemeralBridge(DUMMY_BRIDGE)
     addBridgeToLibvirt(DUMMY_BRIDGE)
diff --git a/lib/vdsm/tool/load_needed_modules.py.in b/lib/vdsm/tool/load_needed_modules.py.in
index 8ebb1be..f65b5f5 100644
--- a/lib/vdsm/tool/load_needed_modules.py.in
+++ b/lib/vdsm/tool/load_needed_modules.py.in
@@ -21,7 +21,7 @@
 import os.path
 
 from .. import netinfo
-from . import expose, VdsmToolRuntimeError
+from . import expose, VdsmToolRuntimeError, VdsmToolWrongArgsError
 from ..utils import execCmd
 
 
@@ -54,11 +54,15 @@
 
 
 @expose('load-needed-modules')
-def load_needed_modules():
+def load_needed_modules(*args):
     """
+    load-needed-modules
     Load needed modules
     """
 
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     for mod in ['bridge', 'tun', 'bonding', '8021q']:
         _exec_command([EX_MODPROBE, mod])
     _enable_bond_dev()
diff --git a/lib/vdsm/tool/nwfilter.py b/lib/vdsm/tool/nwfilter.py
index 25febc0..951061f 100755
--- a/lib/vdsm/tool/nwfilter.py
+++ b/lib/vdsm/tool/nwfilter.py
@@ -22,14 +22,18 @@
 import logging
 
 from .. import libvirtconnection
-from . import expose
+from . import expose, VdsmToolWrongArgsError
 
 
 @expose('nwfilter')
-def main():
+def main(*args):
     """
+    nwfilter
     Defines network filters on libvirt
     """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     conn = libvirtconnection.get(None, False)
     NoMacSpoofingFilter().defineNwFilter(conn)
     conn.close()
diff --git a/lib/vdsm/tool/passwd.py b/lib/vdsm/tool/passwd.py
index 654d81d..ccd0ac8 100644
--- a/lib/vdsm/tool/passwd.py
+++ b/lib/vdsm/tool/passwd.py
@@ -20,14 +20,19 @@
 import errno
 import subprocess
 from .. import constants
-from . import expose, VdsmToolRuntimeError
+from . import expose, VdsmToolRuntimeError, VdsmToolWrongArgsError
 
 
 @expose("set-saslpasswd")
-def set_saslpasswd():
+def set_saslpasswd(*args):
     """
+    set-saslpasswd
     Set vdsm password for libvirt connection
     """
+
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     script = ['/usr/sbin/saslpasswd2', '-p', '-a', 'libvirt',
               constants.SASL_USERNAME]
 
diff --git a/lib/vdsm/tool/restore_nets.py b/lib/vdsm/tool/restore_nets.py
index 8ffe5d7..ddd6c2a 100644
--- a/lib/vdsm/tool/restore_nets.py
+++ b/lib/vdsm/tool/restore_nets.py
@@ -21,20 +21,27 @@
 import sys
 
 from .. import utils
-from . import expose, VdsmToolRuntimeError
+from . import expose, VdsmToolRuntimeError, VdsmToolWrongArgsError
 from ..constants import P_VDSM
 
 
 @expose('restore-nets')
-def restore(*args, **kwargs):
+def restore_command(*args):
     """
+    restore-nets
     Restores the networks to what was previously persisted via vdsm.
     """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+    restore(raiseType=VdsmToolRuntimeError)
+
+
+def restore(raiseType=RuntimeError):
     rc, out, err = utils.execCmd([os.path.join(
         P_VDSM, 'vdsm-restore-net-config')], raw=True)
     sys.stdout.write(out)
     sys.stderr.write(err)
     if rc != 0:
-        raise VdsmToolRuntimeError(
+        raise raiseType(
             'Failed to restore the persisted networks'
         )
diff --git a/lib/vdsm/tool/seboolsetup.py b/lib/vdsm/tool/seboolsetup.py
index f664ecd..c942f5d 100644
--- a/lib/vdsm/tool/seboolsetup.py
+++ b/lib/vdsm/tool/seboolsetup.py
@@ -18,7 +18,7 @@
 # Refer to the README and COPYING files for full details of the license
 #
 
-from . import expose
+from . import expose, VdsmToolWrongArgsError
 
 SEBOOL_ENABLED = "on"
 SEBOOL_DISABLED = "off"
@@ -54,12 +54,24 @@
 
 
 @expose("sebool-config")
-def sebool_config():
-    """Enable the required selinux booleans"""
+def sebool_config(*args):
+    """
+    sebool-config
+    Enable the required selinux booleans
+    """
+
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     setup_booleans(True)
 
 
 @expose("sebool-unconfig")
-def sebool_unconfig():
-    """Disable the required selinux booleans"""
+def sebool_unconfig(*args):
+    """
+    sebool-unconfig
+    Disable the required selinux booleans
+    """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
     setup_booleans(False)
diff --git a/lib/vdsm/tool/service.py b/lib/vdsm/tool/service.py
index 9c6f19c..d104cd9 100644
--- a/lib/vdsm/tool/service.py
+++ b/lib/vdsm/tool/service.py
@@ -27,7 +27,7 @@
 import sys
 from collections import defaultdict
 
-from . import expose, VdsmToolRuntimeError
+from . import expose, VdsmToolRuntimeError, VdsmToolWrongArgsError
 from ..utils import CommandPath
 from ..utils import execCmd as _execCmd
 
@@ -355,26 +355,56 @@
 
 
 @expose("service-start")
+def service_start_command(cmdName, *args):
+    """
+    service-start service-name
+    Start a system service.
+
+    Parameters:
+    service-start - service to start
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_start(args[0])
+
+
 def service_start(srvName):
-    """
-    Start a system service
-    """
     return _runAlts(_srvStartAlts, srvName)
 
 
 @expose("service-stop")
+def service_stop_command(cmdName, *args):
+    """
+    service-stop service-name
+    Stop a system service.
+
+    Parameters:
+    service-name - service to stop
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_stop(args[0])
+
+
 def service_stop(srvName):
-    """
-    Stop a system service
-    """
     return _runAlts(_srvStopAlts, srvName)
 
 
 @expose("service-status")
+def service_status_command(cmdName, *args):
+    """
+    service-status service-name
+    Get status of a system service.
+
+    Parameters:
+    service-name - service to query
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_status(args[0])
+
+
 def service_status(srvName, verbose=True):
-    """
-    Get status of a system service
-    """
     try:
         return _runAlts(_srvStatusAlts, srvName)
     except ServiceError as e:
@@ -384,34 +414,74 @@
 
 
 @expose("service-restart")
+def service_restart_command(cmdName, *args):
+    """
+    service-restart service-name
+    Restart a system service.
+
+    Parameters:
+    service-name - service to restart
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_restart(args[0])
+
+
 def service_restart(srvName):
-    """
-    Restart a system service
-    """
     return _runAlts(_srvRestartAlts, srvName)
 
 
 @expose("service-reload")
+def service_reload_command(cmdName, *args):
+    """
+    service-reload service-name
+    Notify a system service to reload configurations.
+
+    Parameters:
+    service-name - service to notify
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_reload(args[0])
+
+
 def service_reload(srvName):
-    """
-    Notify a system service to reload configurations
-    """
     return _runAlts(_srvReloadAlts, srvName)
 
 
 @expose("service-disable")
+def service_disable_command(cmdName, *args):
+    """
+    service-disable service-name
+    Disable a system service.
+
+    Parameters:
+    service-name - service to disable
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_disable(args[0])
+
+
 def service_disable(srvName):
-    """
-    Disable a system service
-    """
     return _runAlts(_srvDisableAlts, srvName)
 
 
 @expose("service-is-managed")
+def service_is_managed_command(cmdName, *args):
+    """
+    service-is-managed service-name
+    Check the existence of a service.
+
+    Parameters:
+    service-name - service to query
+    """
+    if len(args) != 1:
+        raise VdsmToolWrongArgsError()
+    return service_is_managed(args[0])
+
+
 def service_is_managed(srvName):
-    """
-    Check the existence of a service
-    """
     try:
         return _runAlts(_srvIsManagedAlts, srvName)
     except ServiceError as e:
diff --git a/lib/vdsm/tool/transient.py b/lib/vdsm/tool/transient.py
index d58e9fb..36190da 100644
--- a/lib/vdsm/tool/transient.py
+++ b/lib/vdsm/tool/transient.py
@@ -24,7 +24,7 @@
 
 from .. import constants
 from ..config import config
-from . import expose
+from . import expose, VdsmToolWrongArgsError
 from ..utils import execCmd, CommandPath
 
 
@@ -40,8 +40,12 @@
 @expose("setup-transient-repository")
 def setup_transient_repository(*args):
     """
+    setup-transient-repository
     Prepare the transient disks repository
     """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     _, _, vdsm_uid, vdsm_gid, _, _, _ = pwd.getpwnam(constants.VDSM_USER)
 
     try:
@@ -58,10 +62,14 @@
 @expose("cleanup-transient-repository")
 def cleanup_transient_repository(*args):
     """
+    cleanup-transient-repository
     Cleanup the unused transient disks present in the repository.
     (NOTE: it is recommended to NOT execute this command when the vdsm
     daemon is running)
     """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     transient_images = set(glob.glob(os.path.join(TRANSIENT_DISKS_REPO, "*")))
 
     if len(transient_images) == 0:
diff --git a/lib/vdsm/tool/validate_ovirt_certs.py.in b/lib/vdsm/tool/validate_ovirt_certs.py.in
index 6a8c13f..4693f4a 100644
--- a/lib/vdsm/tool/validate_ovirt_certs.py.in
+++ b/lib/vdsm/tool/validate_ovirt_certs.py.in
@@ -21,7 +21,7 @@
 import pwd
 import shutil
 
-from . import expose
+from . import expose, VdsmToolWrongArgsError
 from ..utils import execCmd
 
 try:
@@ -44,12 +44,16 @@
 
 
 @expose('validate-ovirt-certs')
-def validate_ovirt_certs():
+def validate_ovirt_certs(*args):
     """
+    validate-ovirt-certs
     Validate vdsmcert.pem against cacert.pem.
     If current cacert.pem is invalidate, it will find a validate certificate
     file and replace the old cacert.pem with it. And then persist it.
     """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
+
     print "checking certs.."
     uid, gid = pwd.getpwnam('vdsm')[2:4]
     if not is_our_cafile(PATH_CACERT):
diff --git a/lib/vdsm/tool/vdsm-id.py b/lib/vdsm/tool/vdsm-id.py
index 2113621..8ea4abc 100644
--- a/lib/vdsm/tool/vdsm-id.py
+++ b/lib/vdsm/tool/vdsm-id.py
@@ -18,15 +18,18 @@
 #
 
 from ..utils import getHostUUID
-from . import expose, VdsmToolRuntimeError
+from . import expose, VdsmToolRuntimeError, VdsmToolWrongArgsError
 import sys
 
 
 @expose("vdsm-id")
-def getUUID():
+def getUUID(*args):
     """
+    vdsm-id
     Printing host uuid
     """
+    if len(args) > 1:
+        raise VdsmToolWrongArgsError()
     hostUUID = getHostUUID(False)
     if hostUUID is None:
         raise VdsmToolRuntimeError('Cannot retrieve host UUID')
diff --git a/vdsm-tool/vdsm-tool b/vdsm-tool/vdsm-tool
index 8eb4e52..d1ba58f 100755
--- a/vdsm-tool/vdsm-tool
+++ b/vdsm-tool/vdsm-tool
@@ -148,7 +148,10 @@
         usage_and_exit(1)
 
     try:
-        return tool_command[cmd]["command"](*args[1:])
+        return tool_command[cmd]["command"](*args)
+    except vdsm.tool.VdsmToolWrongArgsError as e:
+        print >> sys.stderr, 'usage:', sys.argv[0], '[options]', \
+            tool_command[cmd]["command"].__doc__.lstrip()
     except vdsm.tool.VdsmToolRuntimeError as e:
         print >> sys.stderr, 'Error:', e
     except Exception:


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I831bde947f451805e086e25f1c0b9943b17e7bd8
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Dima Kuznetsov <dkuznets at redhat.com>


More information about the vdsm-patches mailing list