ia64/xen-unstable

changeset 8147:6a2acc1cc505

Added xm rename command. Improve the argument checking and error reporting.
author emellor@leeni.uk.xensource.com
date Wed Nov 30 19:54:28 2005 +0000 (2005-11-30)
parents 7c90df7df37e
children 28ce48573771
files tools/python/xen/xend/XendClient.py tools/python/xen/xend/server/SrvDomain.py tools/python/xen/xm/main.py
line diff
     1.1 --- a/tools/python/xen/xend/XendClient.py	Wed Nov 30 19:48:54 2005 +0000
     1.2 +++ b/tools/python/xen/xend/XendClient.py	Wed Nov 30 19:54:28 2005 +0000
     1.3 @@ -232,6 +232,11 @@ class Xend:
     1.4          return self.xendPost(self.domainurl(id),
     1.5                               {'op'      : 'pause' })
     1.6  
     1.7 +    def xend_domain_rename(self, id, name):
     1.8 +        return self.xendPost(self.domainurl(id),
     1.9 +                             {'op'      : 'rename',
    1.10 +                              'name'    : name})
    1.11 +
    1.12      def xend_domain_shutdown(self, id, reason):
    1.13          return self.xendPost(self.domainurl(id),
    1.14                               {'op'      : 'shutdown',
     2.1 --- a/tools/python/xen/xend/server/SrvDomain.py	Wed Nov 30 19:48:54 2005 +0000
     2.2 +++ b/tools/python/xen/xend/server/SrvDomain.py	Wed Nov 30 19:54:28 2005 +0000
     2.3 @@ -55,6 +55,10 @@ class SrvDomain(SrvDir):
     2.4          req.setResponseCode(http.ACCEPTED)
     2.5          req.setHeader("Location", "%s/.." % req.prePathURL())
     2.6  
     2.7 +    def op_rename(self, _, req):
     2.8 +        self.acceptCommand(req)
     2.9 +        return self.dom.setName(req.args['name'][0])
    2.10 +
    2.11      def op_shutdown(self, _, req):
    2.12          self.acceptCommand(req)
    2.13          return self.dom.shutdown(req.args['reason'][0])
     3.1 --- a/tools/python/xen/xm/main.py	Wed Nov 30 19:48:54 2005 +0000
     3.2 +++ b/tools/python/xen/xm/main.py	Wed Nov 30 19:54:28 2005 +0000
     3.3 @@ -70,6 +70,7 @@ vcpu_list_help = "vcpu-list <DomId>     
     3.4  vcpu_pin_help = "vcpu-pin <DomId> <VCPU> <CPUs>   Set which cpus a VCPU can use" 
     3.5  dmesg_help =   "dmesg [--clear]                  Read or clear Xen's message buffer"
     3.6  info_help =    "info                             Get information about the xen host"
     3.7 +rename_help =  "rename <DomId> <New Name>        Rename a domain"
     3.8  log_help =     "log                              Print the xend log"
     3.9  sched_bvt_help = """sched-bvt <Parameters>           Set Borrowed Virtual Time scheduler
    3.10                                      parameters"""
    3.11 @@ -125,6 +126,7 @@ domain_commands = [
    3.12      "migrate",
    3.13      "pause",
    3.14      "reboot",
    3.15 +    "rename",
    3.16      "restore",
    3.17      "save",
    3.18      "shutdown",
    3.19 @@ -226,10 +228,20 @@ for command in all_commands:
    3.20  #
    3.21  ####################################################################
    3.22  
    3.23 -def arg_check(args,num,name):
    3.24 -    if len(args) < num:
    3.25 -        err("'xm %s' requires %s argument(s)!\n" % (name, num))
    3.26 -        usage(name)
    3.27 +def arg_check(args, name, lo, hi = -1):
    3.28 +    n = len(args)
    3.29 +    
    3.30 +    if hi == -1:
    3.31 +        if n != lo:
    3.32 +            err("'xm %s' requires %d argument%s.\n" % (name, lo,
    3.33 +                                                       lo > 1 and 's' or ''))
    3.34 +            usage(name)
    3.35 +    else:
    3.36 +        if n < lo or n > hi:
    3.37 +            err("'xm %s' requires between %d and %d arguments.\n" %
    3.38 +                (name, lo, hi))
    3.39 +            usage(name)
    3.40 +
    3.41  
    3.42  def unit(c):
    3.43      if not c.isalpha():
    3.44 @@ -259,14 +271,17 @@ def int_unit(str, dest):
    3.45  def err(msg):
    3.46      print >>sys.stderr, "Error:", msg
    3.47  
    3.48 -def handle_xend_error(cmd, dom, ex):
    3.49 +def handle_xend_error(cmd, args, ex):
    3.50 +    non_option = filter(lambda x: x[0] != '-', args)
    3.51 +    dom = len(non_option) > 0 and non_option[0] or None
    3.52 +
    3.53      error = str(ex)
    3.54      if error == "Not found" and dom != None:
    3.55          err("Domain '%s' not found when running 'xm %s'" % (dom, cmd))
    3.56 -        sys.exit(1)
    3.57      else:
    3.58          err(error)
    3.59 -        sys.exit(1)
    3.60 +
    3.61 +    sys.exit(1)
    3.62      
    3.63  
    3.64  #########################################################################
    3.65 @@ -276,7 +291,7 @@ def handle_xend_error(cmd, dom, ex):
    3.66  #########################################################################
    3.67  
    3.68  def xm_save(args):
    3.69 -    arg_check(args,2,"save")
    3.70 +    arg_check(args, "save", 2)
    3.71  
    3.72      dom = args[0] # TODO: should check if this exists
    3.73      savefile = os.path.abspath(args[1])
    3.74 @@ -289,7 +304,7 @@ def xm_save(args):
    3.75      server.xend_domain_save(dom, savefile)
    3.76      
    3.77  def xm_restore(args):
    3.78 -    arg_check(args,1,"restore")
    3.79 +    arg_check(args, "restore", 1)
    3.80  
    3.81      savefile = os.path.abspath(args[0])
    3.82  
    3.83 @@ -479,24 +494,30 @@ def xm_vcpu_list(args):
    3.84  
    3.85  
    3.86  def xm_reboot(args):
    3.87 -    arg_check(args,1,"reboot")
    3.88 +    arg_check(args, "reboot", 1, 4)
    3.89      from xen.xm import shutdown
    3.90      shutdown.main(["shutdown", "-R"] + args)
    3.91  
    3.92  def xm_pause(args):
    3.93 -    arg_check(args, 1, "pause")
    3.94 +    arg_check(args, "pause", 1)
    3.95      dom = args[0]
    3.96  
    3.97      from xen.xend.XendClient import server
    3.98      server.xend_domain_pause(dom)
    3.99  
   3.100  def xm_unpause(args):
   3.101 -    arg_check(args, 1, "unpause")
   3.102 +    arg_check(args, "unpause", 1)
   3.103      dom = args[0]
   3.104  
   3.105      from xen.xend.XendClient import server
   3.106      server.xend_domain_unpause(dom)
   3.107  
   3.108 +def xm_rename(args):
   3.109 +    arg_check(args, "rename", 2)
   3.110 +
   3.111 +    from xen.xend.XendClient import server
   3.112 +    server.xend_domain_rename(args[0], args[1])
   3.113 +
   3.114  def xm_subcommand(command, args):
   3.115      cmd = __import__(command, globals(), locals(), 'xen.xm')
   3.116      cmd.main([command] + args)
   3.117 @@ -517,7 +538,7 @@ def cpu_make_map(cpulist):
   3.118      return cpus
   3.119  
   3.120  def xm_vcpu_pin(args):
   3.121 -    arg_check(args, 3, "vcpu-pin")
   3.122 +    arg_check(args, "vcpu-pin", 3)
   3.123  
   3.124      dom  = args[0]
   3.125      vcpu = int(args[1])
   3.126 @@ -527,7 +548,7 @@ def xm_vcpu_pin(args):
   3.127      server.xend_domain_pincpu(dom, vcpu, cpumap)
   3.128  
   3.129  def xm_mem_max(args):
   3.130 -    arg_check(args, 2, "mem-max")
   3.131 +    arg_check(args, "mem-max", 2)
   3.132  
   3.133      dom = args[0]
   3.134      mem = int_unit(args[1], 'm')
   3.135 @@ -536,7 +557,7 @@ def xm_mem_max(args):
   3.136      server.xend_domain_maxmem_set(dom, mem)
   3.137      
   3.138  def xm_mem_set(args):
   3.139 -    arg_check(args, 2, "mem-set")
   3.140 +    arg_check(args, "mem-set", 2)
   3.141  
   3.142      dom = args[0]
   3.143      mem_target = int_unit(args[1], 'm')
   3.144 @@ -545,13 +566,13 @@ def xm_mem_set(args):
   3.145      server.xend_domain_mem_target_set(dom, mem_target)
   3.146      
   3.147  def xm_set_vcpus(args):
   3.148 -    arg_check(args, 2, "set-vcpus")
   3.149 +    arg_check(args, "set-vcpus", 2)
   3.150      
   3.151      from xen.xend.XendClient import server
   3.152      server.xend_domain_set_vcpus(args[0], int(args[1]))
   3.153  
   3.154  def xm_domid(args):
   3.155 -    arg_check(args, 1, "domid")
   3.156 +    arg_check(args, "domid", 1)
   3.157  
   3.158      name = args[0]
   3.159  
   3.160 @@ -560,7 +581,7 @@ def xm_domid(args):
   3.161      print sxp.child_value(dom, 'domid')
   3.162      
   3.163  def xm_domname(args):
   3.164 -    arg_check(args, 1, "domname")
   3.165 +    arg_check(args, "domname", 1)
   3.166  
   3.167      name = args[0]
   3.168  
   3.169 @@ -569,21 +590,21 @@ def xm_domname(args):
   3.170      print sxp.child_value(dom, 'name')
   3.171  
   3.172  def xm_sched_bvt(args):
   3.173 -    arg_check(args, 6, "sched-bvt")
   3.174 +    arg_check(args, "sched-bvt", 6)
   3.175      dom = args[0]
   3.176      v = map(long, args[1:6])
   3.177      from xen.xend.XendClient import server
   3.178      server.xend_domain_cpu_bvt_set(dom, *v)
   3.179  
   3.180  def xm_sched_bvt_ctxallow(args):
   3.181 -    arg_check(args, 1, "sched-bvt-ctxallow")
   3.182 +    arg_check(args, "sched-bvt-ctxallow", 1)
   3.183  
   3.184      slice = int(args[0])
   3.185      from xen.xend.XendClient import server
   3.186      server.xend_node_cpu_bvt_slice_set(slice)
   3.187  
   3.188  def xm_sched_sedf(args):
   3.189 -    arg_check(args, 6, "sched-sedf")
   3.190 +    arg_check(args, "sched-sedf", 6)
   3.191      
   3.192      dom = args[0]
   3.193      v = map(int, args[1:6])
   3.194 @@ -600,9 +621,8 @@ def xm_info(args):
   3.195          else: 
   3.196              print "%-23s:" % x[0], x[1]
   3.197  
   3.198 -# TODO: remove as soon as console server shows up
   3.199  def xm_console(args):
   3.200 -    arg_check(args,1,"console")
   3.201 +    arg_check(args, "console", 1)
   3.202  
   3.203      dom = args[0]
   3.204      from xen.xend.XendClient import server
   3.205 @@ -639,11 +659,13 @@ its contents if the [-c|--clear] flag is
   3.206          server.xend_node_clear_dmesg()
   3.207  
   3.208  def xm_log(args):
   3.209 +    arg_check(args, 'xm-log', 0)
   3.210 +    
   3.211      from xen.xend.XendClient import server
   3.212      print server.xend_node_log()
   3.213  
   3.214  def xm_network_list(args):
   3.215 -    arg_check(args,1,"network-list")
   3.216 +    arg_check(args, "network-list", 1)
   3.217      dom = args[0]
   3.218      from xen.xend.XendClient import server
   3.219      for x in server.xend_domain_devices(dom, 'vif'):
   3.220 @@ -651,7 +673,7 @@ def xm_network_list(args):
   3.221          print
   3.222  
   3.223  def xm_block_list(args):
   3.224 -    arg_check(args,1,"block-list")
   3.225 +    arg_check(args, "block-list", 1)
   3.226      dom = args[0]
   3.227      from xen.xend.XendClient import server
   3.228      for x in server.xend_domain_devices(dom, 'vbd'):
   3.229 @@ -659,20 +681,14 @@ def xm_block_list(args):
   3.230          print
   3.231  
   3.232  def xm_block_attach(args):
   3.233 -    n = len(args)
   3.234 -    if n == 0:
   3.235 -        usage("block-attach")
   3.236 -        
   3.237 -    if n < 4 or n > 5:
   3.238 -        err("%s: Invalid argument(s)" % args[0])
   3.239 -        usage("block-attach")
   3.240 +    arg_check(args, 'block-attach', 4, 5)
   3.241  
   3.242      dom = args[0]
   3.243      vbd = ['vbd',
   3.244             ['uname', args[1]],
   3.245             ['dev',   args[2]],
   3.246             ['mode',  args[3]]]
   3.247 -    if n == 5:
   3.248 +    if len(args) == 5:
   3.249          vbd.append(['backend', args[4]])
   3.250  
   3.251      from xen.xend.XendClient import server
   3.252 @@ -680,10 +696,8 @@ def xm_block_attach(args):
   3.253  
   3.254  
   3.255  def xm_network_attach(args):
   3.256 -    n = len(args)
   3.257 -    if n == 0:
   3.258 -        usage("network-attach")
   3.259 -        
   3.260 +    arg_check(args, 'network-attach', 1, 10000)
   3.261 +
   3.262      dom = args[0]
   3.263      vif = ['vif']
   3.264  
   3.265 @@ -695,7 +709,7 @@ def xm_network_attach(args):
   3.266  
   3.267  
   3.268  def detach(args, command, deviceClass):
   3.269 -    arg_check(args, 2, command)
   3.270 +    arg_check(args, command, 2)
   3.271  
   3.272      dom = args[0]
   3.273      dev = args[1]
   3.274 @@ -742,7 +756,7 @@ def xm_vnet_list(args):
   3.275              print vnet, ex
   3.276  
   3.277  def xm_vnet_create(args):
   3.278 -    arg_check(args, 1, "vnet-create")
   3.279 +    arg_check(args, "vnet-create", 1)
   3.280      conf = args[0]
   3.281      if not os.access(conf, os.R_OK):
   3.282          print "File not found: %s" % conf
   3.283 @@ -752,7 +766,7 @@ def xm_vnet_create(args):
   3.284      server.xend_vnet_create(conf)
   3.285  
   3.286  def xm_vnet_delete(args):
   3.287 -    arg_check(args, 1, "vnet-delete")
   3.288 +    arg_check(args, "vnet-delete", 1)
   3.289      vnet = args[0]
   3.290      from xen.xend.XendClient import server
   3.291      server.xend_vnet_delete(vnet)
   3.292 @@ -765,6 +779,7 @@ commands = {
   3.293      # domain commands
   3.294      "domid": xm_domid,
   3.295      "domname": xm_domname,
   3.296 +    "rename": xm_rename,
   3.297      "restore": xm_restore,
   3.298      "save": xm_save,
   3.299      "reboot": xm_reboot,
   3.300 @@ -888,7 +903,7 @@ def main(argv=sys.argv):
   3.301              sys.exit(1)
   3.302          except xen.xend.XendError.XendError, ex:
   3.303              if len(args) > 0:
   3.304 -                handle_xend_error(argv[1], args[0], ex)
   3.305 +                handle_xend_error(argv[1], args, ex)
   3.306              else:
   3.307                  print "Unexpected error:", sys.exc_info()[0]
   3.308                  print
   3.309 @@ -896,7 +911,7 @@ def main(argv=sys.argv):
   3.310                  raise
   3.311          except xen.xend.XendProtocol.XendError, ex:
   3.312              if len(args) > 0:
   3.313 -                handle_xend_error(argv[1], args[0], ex)
   3.314 +                handle_xend_error(argv[1], args, ex)
   3.315              else:
   3.316                  print "Unexpected error:", sys.exc_info()[0]
   3.317                  print