ia64/xen-unstable

changeset 12109:19ab41db7fd2

[XENAPI] Pretty print error messages, insert simple argument parsing
framework

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Oct 06 12:31:24 2006 +0100 (2006-10-06)
parents dd044380b44d
children c0d9f8b9c0e5
files tools/python/scripts/xapi.py
line diff
     1.1 --- a/tools/python/scripts/xapi.py	Fri Oct 06 12:30:46 2006 +0100
     1.2 +++ b/tools/python/scripts/xapi.py	Fri Oct 06 12:31:24 2006 +0100
     1.3 @@ -18,6 +18,7 @@
     1.4  
     1.5  from xen.util.xmlrpclib2 import ServerProxy
     1.6  from optparse import *
     1.7 +from pprint import pprint
     1.8  from types import DictType
     1.9  
    1.10  HOST_INFO_FORMAT = '%-20s: %-50s'
    1.11 @@ -40,20 +41,40 @@ COMMANDS = {
    1.12      'vm-destroy': ('<name>', 'Hard shutdown a VM with name'),
    1.13  }
    1.14  
    1.15 +OPTIONS = {
    1.16 +    'vm-list': [(('-l', '--long'),
    1.17 +                 {'action':'store_true',
    1.18 +                  'help':'List all properties of VMs'})
    1.19 +               ],
    1.20 +   
    1.21 +}
    1.22 +
    1.23  class OptionError(Exception):
    1.24      pass
    1.25  
    1.26 +class XenAPIError(Exception):
    1.27 +    pass
    1.28 +
    1.29  # 
    1.30  # Extra utility functions
    1.31  #
    1.32  
    1.33 +def parse_args(cmd_name, args):
    1.34 +    if cmd_name in OPTIONS:
    1.35 +        parser = OptionParser()
    1.36 +        for optargs, optkwds in OPTIONS[cmd_name]:
    1.37 +            parser.add_option(*optargs, **optkwds)
    1.38 +        (opts, extraargs) = parser.parse_args(list(args))
    1.39 +        return opts, extraargs
    1.40 +    return None, []
    1.41 +
    1.42  def execute(fn, *args):
    1.43      result = fn(*args)
    1.44      if type(result) != DictType:
    1.45          raise TypeError("Function returned object of type: %s" %
    1.46                          str(type(result)))
    1.47      if 'Value' not in result:
    1.48 -        raise Exception(result['ErrorDescription'])
    1.49 +        raise XenAPIError(*result['ErrorDescription'])
    1.50      return result['Value']
    1.51  
    1.52  
    1.53 @@ -87,16 +108,24 @@ def xapi_host_info(*args):
    1.54          print HOST_INFO_FORMAT % ('UUID', host)        
    1.55  
    1.56  def xapi_vm_list(*args):
    1.57 +    opts, args = parse_args('vm-list', args)
    1.58 +    is_long = opts and opts.long
    1.59 +    
    1.60      server, session = _connect()
    1.61      vm_uuids = execute(server.VM.get_all, session)
    1.62 -    print VM_LIST_FORMAT % {'name_label':'Name',
    1.63 -                            'memory_actual':'Mem',
    1.64 -                            'vcpus_number': 'VCPUs',
    1.65 -                            'power_state': 'State',
    1.66 -                            'uuid': 'UUID'}
    1.67 +    if not is_long:
    1.68 +        print VM_LIST_FORMAT % {'name_label':'Name',
    1.69 +                                'memory_actual':'Mem',
    1.70 +                                'vcpus_number': 'VCPUs',
    1.71 +                                'power_state': 'State',
    1.72 +                                'uuid': 'UUID'}
    1.73 +
    1.74      for uuid in vm_uuids:
    1.75          vm_info = execute(server.VM.get_record, session, uuid)
    1.76 -        print VM_LIST_FORMAT % _stringify(vm_info)
    1.77 +        if is_long:
    1.78 +            pprint(vm_info)
    1.79 +        else:
    1.80 +            print VM_LIST_FORMAT % _stringify(vm_info)
    1.81  
    1.82  def xapi_vm_create(*args):
    1.83      if len(args) < 1:
    1.84 @@ -108,7 +137,7 @@ def xapi_vm_create(*args):
    1.85      print 'Creating VM from %s ..' % filename
    1.86      server, session = _connect()
    1.87      uuid = execute(server.VM.create, session, cfg)
    1.88 -    print 'Done.'
    1.89 +    print 'Done. (%s)' % uuid
    1.90      print uuid
    1.91  
    1.92  def xapi_vm_delete(*args):
    1.93 @@ -164,8 +193,7 @@ def xapi_vbd_create(*args):
    1.94      vm_uuid = execute(server.VM.get_by_label, session, domname)
    1.95      cfg['VM'] = vm_uuid
    1.96      vbd_uuid = execute(server.VBD.create, session, cfg)
    1.97 -    print 'Done.'
    1.98 -    print vbd_uuid
    1.99 +    print 'Done. (%s)' % vbd_uuid
   1.100  
   1.101  def xapi_vif_create(*args):
   1.102      if len(args) < 2:
   1.103 @@ -179,8 +207,7 @@ def xapi_vif_create(*args):
   1.104      vm_uuid = execute(server.VM.get_by_label, session, domname)
   1.105      cfg['VM'] = vm_uuid
   1.106      vif_uuid = execute(server.VIF.create, session, cfg)
   1.107 -    print 'Done.'
   1.108 -    print vif_uuid        
   1.109 +    print 'Done. (%s)' % vif_uuid
   1.110  
   1.111  #
   1.112  # Command Line Utils
   1.113 @@ -207,11 +234,18 @@ def main(args):
   1.114  
   1.115      subcmd_func_name = 'xapi_' + subcmd.replace('-', '_')
   1.116      subcmd_func = globals().get(subcmd_func_name, None)
   1.117 -    if subcmd_func and callable(subcmd_func):
   1.118 -        subcmd_func(*args[1:])
   1.119 -    else:
   1.120 +    if not subcmd_func or not callable(subcmd_func):
   1.121          print 'Error: Unable to find subcommand \'%s\'' % subcmd
   1.122          usage()
   1.123 +        sys.exit(-1)
   1.124 +                  
   1.125 +    try:
   1.126 +        subcmd_func(*args[1:])
   1.127 +    except XenAPIError, e:
   1.128 +        print 'Error: %s' % str(e.args[1])
   1.129 +        sys.exit(-1)
   1.130 +
   1.131 +    sys.exit(0)
   1.132      
   1.133  if __name__ == "__main__":
   1.134      import sys