[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] Test scripts for Xen API updated to support Async support



# HG changeset patch
# User Alastair Tse <atse@xxxxxxxxxxxxx>
# Date 1169646565 0
# Node ID 9292da5e9a27344e26e5e81dbfcf391f13c4e5fd
# Parent  248a9c36d81670735f5b1d89bbf50ba985903fe5
Test scripts for Xen API updated to support Async support
---
 tools/python/scripts/test_vm_create.py |  177 +++++++++++++++
 tools/python/scripts/xapi.py           |  378 ++++++++++++++++++++++-----------
 2 files changed, 435 insertions(+), 120 deletions(-)

diff -r 248a9c36d816 -r 9292da5e9a27 tools/python/scripts/test_vm_create.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/python/scripts/test_vm_create.py    Wed Jan 24 13:49:25 2007 +0000
@@ -0,0 +1,177 @@
+#!/usr/bin/python
+
+vm_cfg = {
+    'name_label': 'APIVM',
+    'user_version': 1,
+    'is_a_template': False,
+    'auto_power_on': False, # TODO
+
+    'memory_static_min': 64,    
+    'memory_static_max': 128,
+    #'memory_dynamic_min': 64,
+    #'memory_dynamic_max': 128,
+    
+    
+    'VCPUs_policy': 'credit',
+    'VCPUs_params': '',
+    'VCPUs_number': 2,
+    'VCPUs_features_required': '',
+    'VCPUs_features_can_use': '',
+    'VCPUs_features_force_on': '',
+    'VCPUs_features_force_off': '',
+
+    'actions_after_shutdown': 'destroy',
+    'actions_after_reboot': 'restart',
+    'actions_after_suspend': 'destroy',
+    'actions_after_crash': 'destroy',
+    
+    'PV_bootloader': '',
+    'PV_bootloader_args': '',
+    
+    'PV_kernel': '/boot/vmlinuz-2.6.18-xenU',
+    'PV_ramdisk': '',
+    'PV_args': 'root=/dev/sda1 ro',
+
+    #'HVM_boot': '',
+    'platform_std_VGA': False,
+    'platform_serial': '',
+    'platform_localtime': False,
+    'platform_clock_offset': False,
+    'platform_enable_audio': False,
+    'PCI_bus': ''
+}
+
+vdi_cfg = {
+    'name_label': 'API_VDI',
+    'name_description': '',
+    'virtual_size': 100 * 1024,
+    'sector_size': 1024,
+    'type': 'system',
+    'parent': '',    
+    'sharable': False,
+    'read_only': False,
+}
+
+vbd_cfg = {
+    'VDI': '',
+    'VM': '',
+    'device': 'sda2',
+    'mode': 'RW',
+    'type': 'disk',
+    'driver': 'paravirtualised',
+}
+
+local_vbd_cfg = {
+    'VDI': '',
+    'VM': '',
+    'device': 'sda1',
+    'mode': 'RW',
+    'type': 'disk',
+    'driver': 'paravirtualised',
+    'image': 'file:/root/gentoo.amd64.img'
+}
+
+vif_cfg = {
+    'name': 'API_VIF',
+    'type': 'paravirtualised',
+    'device': '',
+    'network': '',
+    'MAC': '',
+    'MTU': 1500,
+}    
+
+import sys
+import time
+sys.path.append('/usr/lib/python')
+
+from xapi import connect, execute
+
+def test_vm_create():
+    server, session = connect()
+    vm_uuid = None
+    vdi_uuid = None
+    local_vbd_uuid = None
+    vbd_uuid = None
+    vif_uuid = None
+    
+    # List all VMs
+    vm_list = execute(server, 'VM.get_all', (session,))
+    vm_names = []
+    for vm_uuid in vm_list:
+        vm_record = execute(server, 'VM.get_record', (session, vm_uuid))
+        vm_names.append(vm_record['name_label'])
+
+    # Get default SR
+    sr_list = execute(server, 'SR.get_by_name_label', (session, 'Local'))
+    sr_uuid = sr_list[0]
+
+    # Get default network
+    net_list = execute(server, 'network.get_all', (session,))
+    net_uuid = net_list[0]
+
+    try:
+        # Create a new VM
+        vm_uuid = execute(server, 'VM.create', (session, vm_cfg))
+        
+        # Create a new VDI
+        vdi_cfg['SR'] = sr_uuid
+        vdi_uuid = execute(server, 'VDI.create', (session, vdi_cfg))
+
+        # Create a VDI backed VBD
+        vbd_cfg['VM'] = vm_uuid
+        vbd_cfg['VDI'] = vdi_uuid
+        vbd_uuid = execute(server, 'VBD.create', (session, vbd_cfg))
+
+        # Create a new VBD (Local)
+        local_vbd_cfg['VM'] = vm_uuid
+        local_vbd_cfg['VDI'] = ''
+        local_vbd_uuid = execute(server, 'VBD.create',
+                                 (session, local_vbd_cfg))
+        
+        # Create a new VIF
+        vif_cfg['network'] = net_uuid
+        vif_cfg['VM'] = vm_uuid
+        vif_uuid = execute(server, 'VIF.create', (session, vif_cfg))
+
+        # Start the VM
+        execute(server, 'VM.start', (session, vm_uuid, False))
+
+        time.sleep(30)
+
+        print 'Suspending VM..'
+        execute(server, 'VM.suspend', (session, vm_uuid))
+        print 'Suspended VM.'
+        time.sleep(5)
+        print 'Resuming VM ...'
+        execute(server, 'VM.resume', (session, vm_uuid, False))
+        print 'Resumed VM.'
+
+        # Wait for user to say we're good to shut it down
+        while True:
+            destroy = raw_input('destroy VM? ')
+            if destroy[0] in ('y', 'Y'):
+                break
+
+    finally:
+        # Clean up
+        if vif_uuid:
+            execute(server, 'VIF.destroy', (session, vif_uuid))
+        if local_vbd_uuid:
+            execute(server, 'VBD.destroy', (session, local_vbd_uuid))
+        if vbd_uuid:
+            execute(server, 'VBD.destroy', (session, vbd_uuid))
+        if vdi_uuid:
+            execute(server, 'VDI.destroy', (session, vdi_uuid))
+        if vm_uuid:
+            try:
+                execute(server, 'VM.hard_shutdown', (session, vm_uuid))
+                time.sleep(2)
+            except:
+                pass
+                
+            execute(server, 'VM.destroy', (session, vm_uuid))
+
+
+if __name__ == "__main__":
+    test_vm_create()
+    
diff -r 248a9c36d816 -r 9292da5e9a27 tools/python/scripts/xapi.py
--- a/tools/python/scripts/xapi.py      Wed Jan 24 13:26:26 2007 +0000
+++ b/tools/python/scripts/xapi.py      Wed Jan 24 13:49:25 2007 +0000
@@ -17,6 +17,8 @@
 #============================================================================
 
 import sys
+import time
+import re
 sys.path.append('/usr/lib/python')
 
 from xen.util.xmlrpclib2 import ServerProxy
@@ -35,8 +37,9 @@ SR_LIST_FORMAT = '%(name_label)-18s %(uu
                  '%(type)-10s'
 VDI_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(virtual_size)-8s '\
                   '%(sector_size)-8s'
-VBD_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(VDI)-8s '\
-                  '%(image)-8s'
+VBD_LIST_FORMAT = '%(device)-6s %(uuid)-36s %(VDI)-8s %(image)-8s'
+TASK_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(status)-8s %(progress)-4s'
+VIF_LIST_FORMAT = '%(name)-8s %(device)-7s %(uuid)-36s %(MAC)-10s'
 
 COMMANDS = {
     'host-info': ('', 'Get Xen Host Info'),
@@ -49,7 +52,7 @@ COMMANDS = {
     'vdi-create': ('<pycfg> [opts]', 'Create a VDI'),
     'vdi-list'  : ('', 'List all VDI'),
     'vdi-rename': ('<vdi_uuid> <new_name>', 'Rename VDI'),
-    'vdi-delete': ('<vdi_uuid>', 'Delete VDI'),
+    'vdi-destroy': ('<vdi_uuid>', 'Delete VDI'),
     'vif-create': ('<domname> <pycfg>', 'Create VIF attached to domname'),
     'vtpm-create' : ('<domname> <pycfg>', 'Create VTPM attached to domname'),
 
@@ -61,6 +64,7 @@ COMMANDS = {
     'vm-shutdown': ('<name> [opts]', 'Shutdown VM with name'),
     'vm-start':  ('<name>', 'Start VM with name'),
     'vm-uuid':   ('<name>', 'UUID of a domain by name.'),
+    'async-vm-start': ('<name>', 'Start VM asynchronously'),
 }
 
 OPTIONS = {
@@ -140,8 +144,13 @@ def parse_args(cmd_name, args, set_defau
                                           values = defaults)
     return opts, extraargs
 
-def execute(fn, *args):
-    result = fn(*args)
+def execute(server, fn, args, async = False):
+    if async:
+        func = eval('server.Async.%s' % fn)
+    else:
+        func = eval('server.%s' % fn)
+        
+    result = func(*args)
     if type(result) != DictType:
         raise TypeError("Function returned object of type: %s" %
                         str(type(result)))
@@ -152,14 +161,14 @@ _initialised = False
 _initialised = False
 _server = None
 _session = None
-def _connect(*args):
+def connect(*args):
     global _server, _session, _initialised
     if not _initialised:
-        _server = ServerProxy('httpu:///var/run/xend/xen-api.sock')
+        _server = ServerProxy('http://localhost:9363/')
         login = raw_input("Login: ")
         password = getpass()
         creds = (login, password)
-        _session = execute(_server.session.login_with_password, *creds)
+        _session = execute(_server.session, 'login_with_password', creds)
         _initialised = True
     return (_server, _session)
 
@@ -172,14 +181,14 @@ def _read_python_cfg(filename):
     return cfg
 
 def resolve_vm(server, session, vm_name):
-    vm_uuid = execute(server.VM.get_by_name_label, session, vm_name)
+    vm_uuid = execute(server, 'VM.get_by_name_label', (session, vm_name))
     if not vm_uuid:
         return None
     else:
         return vm_uuid[0]
 
 def resolve_vdi(server, session, vdi_name):
-    vdi_uuid = execute(server.VDI.get_by_name_label, session, vdi_name)
+    vdi_uuid = execute(server, 'VDI.get_by_name_label', (session, vdi_name))
     if not vdi_uuid:
         return None
     else:
@@ -189,11 +198,11 @@ def resolve_vdi(server, session, vdi_nam
 # Actual commands
 #
 
-def xapi_host_info(*args):
-    server, session = _connect()
-    hosts = execute(server.host.get_all, session)
+def xapi_host_info(args, async = False):
+    server, session = connect()
+    hosts = execute(server, 'host.get_all', (session,))
     for host in hosts: # there is only one, but ..
-        hostinfo = execute(server.host.get_record, session, host)
+        hostinfo = execute(server, 'host.get_record', (session, host))
         print HOST_INFO_FORMAT % ('Name', hostinfo['name_label'])
         print HOST_INFO_FORMAT % ('Version', hostinfo['software_version'])
         print HOST_INFO_FORMAT % ('CPUs', len(hostinfo['host_CPUs']))
@@ -201,44 +210,44 @@ def xapi_host_info(*args):
         print HOST_INFO_FORMAT % ('UUID', host)        
 
         for host_cpu_uuid in hostinfo['host_CPUs']:
-            host_cpu = execute(server.host_cpu.get_record, session,
-                               host_cpu_uuid)
+            host_cpu = execute(server, 'host_cpu.get_record',
+                               (session, host_cpu_uuid))
             print 'CPU %s Util: %.2f' % (host_cpu['number'],
                                          float(host_cpu['utilisation']))
         
-def xapi_host_set_name(*args):
+def xapi_host_set_name(args, async = False):
     if len(args) < 1:
         raise OptionError("No hostname specified")
     
-    server, session = _connect()
-    hosts = execute(server.host.get_all, session)
+    server, session = connect()
+    hosts = execute(server, 'host.get_all', (session,))
     if len(hosts) > 0:
-        execute(server.host.set_name_label, session, hosts[0], args[0])
-        print 'Hostname: %s' % execute(server.host.get_name_label, session,
-                                       hosts[0])
-
-def xapi_vm_uuid(*args):
+        execute(server, 'host.set_name_label', (session, hosts[0], args[0]))
+        print 'Hostname: %s' % execute(server, 'host.get_name_label',
+                                       (session, hosts[0]))
+
+def xapi_vm_uuid(args, async = False):
     if len(args) < 1:
         raise OptionError("No domain name specified")
     
-    server, session = _connect()
+    server, session = connect()
     vm_uuid = resolve_vm(server, session, args[0])
     print vm_uuid
 
-def xapi_vm_name(*args):
+def xapi_vm_name(args, async = False):
     if len(args) < 1:
         raise OptionError("No UUID specified")
     
-    server, session = _connect()
-    vm_name = execute(server.VM.get_name_label, session, args[0])
+    server, session = connect()
+    vm_name = execute(server, 'VM.get_name_label', (session, args[0]))
     print vm_name
 
-def xapi_vm_list(*args):
+def xapi_vm_list(args, async = False):
     opts, args = parse_args('vm-list', args, set_defaults = True)
     is_long = opts and opts.long
     
-    server, session = _connect()
-    vm_uuids = execute(server.VM.get_all, session)
+    server, session = connect()
+    vm_uuids = execute(server, 'VM.get_all', (session,))
     if not is_long:
         print VM_LIST_FORMAT % {'name_label':'Name',
                                 'memory_actual':'Mem',
@@ -247,7 +256,7 @@ def xapi_vm_list(*args):
                                 'uuid': 'UUID'}
 
     for uuid in vm_uuids:
-        vm_info = execute(server.VM.get_record, session, uuid)
+        vm_info = execute(server, 'VM.get_record', (session, uuid))
         if is_long:
             vbds = vm_info['VBDs']
             vifs = vm_info['VIFs']
@@ -256,13 +265,13 @@ def xapi_vm_list(*args):
             vbd_infos = []
             vtpm_infos = []
             for vbd in vbds:
-                vbd_info = execute(server.VBD.get_record, session, vbd)
+                vbd_info = execute(server, 'VBD.get_record', (session, vbd))
                 vbd_infos.append(vbd_info)
             for vif in vifs:
-                vif_info = execute(server.VIF.get_record, session, vif)
+                vif_info = execute(server, 'VIF.get_record', (session, vif))
                 vif_infos.append(vif_info)
             for vtpm in vtpms:
-                vtpm_info = execute(server.VTPM.get_record, session, vtpm)
+                vtpm_info = execute(server, 'VTPM.get_record', (session, vtpm))
                 vtpm_infos.append(vtpm_info)
             vm_info['VBDs'] = vbd_infos
             vm_info['VIFs'] = vif_infos
@@ -271,7 +280,7 @@ def xapi_vm_list(*args):
         else:
             print VM_LIST_FORMAT % _stringify(vm_info)
 
-def xapi_vm_create(*args):
+def xapi_vm_create(args, async = False):
     if len(args) < 1:
         raise OptionError("Configuration file not specified")
 
@@ -279,49 +288,129 @@ def xapi_vm_create(*args):
     cfg = _read_python_cfg(filename)
 
     print 'Creating VM from %s ..' % filename
-    server, session = _connect()
-    uuid = execute(server.VM.create, session, cfg)
+    server, session = connect()
+    uuid = execute(server, 'VM.create', (session, cfg), async = async)
     print 'Done. (%s)' % uuid
     print uuid
 
-def xapi_vm_destroy(*args):
+def xapi_vm_destroy(args, async = False):
     if len(args) < 1:
         raise OptionError("No domain name specified.")
     
-    server, session = _connect()
+    server, session = connect()
     vm_uuid = resolve_vm(server, session, args[0])    
     print 'Destroying VM %s (%s)' % (args[0], vm_uuid)
-    success = execute(server.VM.destroy, session, vm_uuid)
+    success = execute(server, 'VM.destroy', (session, vm_uuid), async = async)
     print 'Done.'
     
 
-def xapi_vm_start(*args):
+def xapi_vm_start(args, async = False):
     if len(args) < 1:
         raise OptionError("No Domain name specified.")
     
-    server, session = _connect()
+    server, session = connect()
     vm_uuid = resolve_vm(server, session, args[0])
     print 'Starting VM %s (%s)' % (args[0], vm_uuid)
-    success = execute(server.VM.start, session, vm_uuid, False)
-    print 'Done.'
-
-def xapi_vm_shutdown(*args):
+    success = execute(server, 'VM.start', (session, vm_uuid, False), async = 
async)
+    if async:
+        print 'Task started: %s' % success
+    else:
+        print 'Done.'
+
+def xapi_vm_suspend(args, async = False):
+    if len(args) < 1:
+        raise OptionError("No Domain name specified.")
+    
+    server, session = connect()
+    vm_uuid = resolve_vm(server, session, args[0])
+    print 'Suspending VM %s (%s)' % (args[0], vm_uuid)
+    success = execute(server, 'VM.suspend', (session, vm_uuid), async = async)
+    if async:
+        print 'Task started: %s' % success
+    else:
+        print 'Done.'        
+
+
+def xapi_vm_resume(args, async = False):
+    if len(args) < 1:
+        raise OptionError("No Domain name specified.")
+    
+    server, session = connect()
+    vm_uuid = resolve_vm(server, session, args[0])
+    print 'Resuming VM %s (%s)' % (args[0], vm_uuid)
+    success = execute(server, 'VM.resume', (session, vm_uuid, False), async = 
async)
+    if async:
+        print 'Task started: %s' % success
+    else:
+        print 'Done.'
+
+def xapi_vm_pause(args, async = False):
+    if len(args) < 1:
+        raise OptionError("No Domain name specified.")
+    
+    server, session = connect()
+    vm_uuid = resolve_vm(server, session, args[0])
+    print 'Pausing VM %s (%s)' % (args[0], vm_uuid)
+    success = execute(server, 'VM.pause', (session, vm_uuid), async = async)
+    if async:
+        print 'Task started: %s' % success
+    else:
+        print 'Done.'
+
+def xapi_vm_unpause(args, async = False):
+    if len(args) < 1:
+        raise OptionError("No Domain name specified.")
+    
+    server, session = connect()
+    vm_uuid = resolve_vm(server, session, args[0])
+    print 'Pausing VM %s (%s)' % (args[0], vm_uuid)
+    success = execute(server, 'VM.unpause', (session, vm_uuid), async = async)
+    if async:
+        print 'Task started: %s' % success
+    else:
+        print 'Done.'                        
+
+def xapi_task_list(args, async = False):
+    server, session = connect()
+    all_tasks = execute(server, 'task.get_all', (session,))
+
+    print TASK_LIST_FORMAT % {'name_label': 'Task Name',
+                              'uuid': 'UUID',
+                              'status': 'Status',
+                              'progress': '%'}
+    
+    for task_uuid in all_tasks:
+        task = execute(server, 'task.get_record', (session, task_uuid))
+        print TASK_LIST_FORMAT % task
+
+def xapi_task_clear(args, async = False):
+    server, session = connect()
+    all_tasks = execute(server, 'task.get_all', (session,))
+    for task_uuid in all_tasks:
+        success = execute(server, 'task.destroy', (session, task_uuid))
+        print 'Destroyed Task %s' % task_uuid
+
+def xapi_vm_shutdown(args, async = False):
     opts, args = parse_args("vm-shutdown", args, set_defaults = True)
     
     if len(args) < 1:
         raise OptionError("No Domain name specified.")
 
-    server, session = _connect()
+    server, session = connect()
     vm_uuid = resolve_vm(server, session, args[0])
     if opts.force:
         print 'Forcefully shutting down VM %s (%s)' % (args[0], vm_uuid)
-        success = execute(server.VM.hard_shutdown, session, vm_uuid)
+        success = execute(server, 'VM.hard_shutdown', (session, vm_uuid), 
async = async)
     else:
         print 'Shutting down VM %s (%s)' % (args[0], vm_uuid)
-        success = execute(server.VM.clean_shutdown, session, vm_uuid)
-    print 'Done.'
-
-def xapi_vbd_create(*args):
+        success = execute(server, 'VM.clean_shutdown', (session, vm_uuid), 
async = async)
+
+    if async:
+        print 'Task started: %s' % success
+    else:
+        print 'Done.'
+
+def xapi_vbd_create(args, async = False):
     opts, args = parse_args('vbd-create', args)
 
     if len(args) < 2:
@@ -339,13 +428,16 @@ def xapi_vbd_create(*args):
         cfg[opt] = val
     
     print 'Creating VBD ...',
-    server, session = _connect()
+    server, session = connect()
     vm_uuid = resolve_vm(server, session, domname)
     cfg['VM'] = vm_uuid
-    vbd_uuid = execute(server.VBD.create, session, cfg)
-    print 'Done. (%s)' % vbd_uuid
-
-def xapi_vif_create(*args):
+    vbd_uuid = execute(server, 'VBD.create', (session, cfg), async = async)
+    if async:
+        print 'Task started: %s' % vbd_uuid
+    else:
+        print 'Done. (%s)' % vbd_uuid
+
+def xapi_vif_create(args, async = False):
     if len(args) < 2:
         raise OptionError("Configuration file not specified")
 
@@ -354,34 +446,53 @@ def xapi_vif_create(*args):
     cfg = _read_python_cfg(filename)
     
     print 'Creating VIF from %s ..' % filename
-    server, session = _connect()
+    server, session = connect()
     vm_uuid = resolve_vm(server, session, domname)
     cfg['VM'] = vm_uuid
-    vif_uuid = execute(server.VIF.create, session, cfg)
-    print 'Done. (%s)' % vif_uuid
-
-def xapi_vbd_list(*args):
-    server, session = _connect()
+    vif_uuid = execute(server, 'VIF.create', (session, cfg), async = async)
+    if async:
+        print 'Task started: %s' % vif_uuid
+    else:
+        print 'Done. (%s)' % vif_uuid
+
+def xapi_vbd_list(args, async = False):
+    server, session = connect()
     domname = args[0]
     
     dom_uuid = resolve_vm(server, session, domname)
-    vbds = execute(server.VM.get_VBDs, session, dom_uuid)
-    
-    print VBD_LIST_FORMAT % {'name_label': 'VDI Label',
+    vbds = execute(server, 'VM.get_VBDs', (session, dom_uuid))
+    
+    print VBD_LIST_FORMAT % {'device': 'Device',
                              'uuid' : 'UUID',
                              'VDI': 'VDI',
                              'image': 'Image'}
     
     for vbd in vbds:
-        vbd_struct = execute(server.VBD.get_record, session, vbd)
+        vbd_struct = execute(server, 'VBD.get_record', (session, vbd))
         print VBD_LIST_FORMAT % vbd_struct
-
-def xapi_vdi_list(*args):
+ 
+def xapi_vif_list(args, async = False):
+    server, session = connect()
+    domname = args[0]
+    
+    dom_uuid = resolve_vm(server, session, domname)
+    vifs = execute(server, 'VM.get_VIFs', (session, dom_uuid))
+    
+    print VIF_LIST_FORMAT % {'name': 'Name',
+                             'device': 'Device',
+                             'uuid' : 'UUID',
+                             'MAC': 'MAC'}
+    
+    for vif in vifs:
+        vif_struct = execute(server, 'VIF.get_record', (session, vif))
+        print VIF_LIST_FORMAT % vif_struct       
+
+def xapi_vdi_list(args, async = False):
     opts, args = parse_args('vdi-list', args, set_defaults = True)
     is_long = opts and opts.long
 
-    server, session = _connect()
-    vdis = execute(server.VDI.get_all, session)
+    server, session = connect()
+    vdis = execute(server, 'VDI.get_all', (session,))
 
     if not is_long:
         print VDI_LIST_FORMAT % {'name_label': 'VDI Label',
@@ -390,21 +501,20 @@ def xapi_vdi_list(*args):
                                  'sector_size': 'Sector Size'}
         
         for vdi in vdis:
-            vdi_struct = execute(server.VDI.get_record, session, vdi)
+            vdi_struct = execute(server, 'VDI.get_record', (session, vdi))
             print VDI_LIST_FORMAT % vdi_struct
 
     else:
-
         for vdi in vdis:
-            vdi_struct = execute(server.VDI.get_record, session, vdi)
+            vdi_struct = execute(server, 'VDI.get_record', (session, vdi))
             pprint(vdi_struct)
 
-def xapi_sr_list(*args):
+def xapi_sr_list(args, async = False):
     opts, args = parse_args('sr-list', args, set_defaults = True)
     is_long = opts and opts.long
     
-    server, session = _connect()
-    srs = execute(server.SR.get_all, session)
+    server, session = connect()
+    srs = execute(server, 'SR.get_all', (session,))
     if not is_long:
         print SR_LIST_FORMAT % {'name_label': 'SR Label',
                                 'uuid' : 'UUID',
@@ -412,20 +522,20 @@ def xapi_sr_list(*args):
                                 'type': 'Type'}
         
         for sr in srs:
-            sr_struct = execute(server.SR.get_record, session, sr)
+            sr_struct = execute(server, 'SR.get_record', (session, sr))
             sr_struct['physical_size'] = int(sr_struct['physical_size'])/MB
             print SR_LIST_FORMAT % sr_struct
     else:
         for sr in srs:
-            sr_struct = execute(server.SR.get_record, session, sr)        
+            sr_struct = execute(server, 'SR.get_record', (session, sr))  
             pprint(sr_struct)
 
-def xapi_sr_rename(*args):
-    server, session = _connect()
-    sr = execute(server.SR.get_by_name_label, session, args[0])
-    execute(server.SR.set_name_label, session, sr[0], args[1])
-
-def xapi_vdi_create(*args):
+def xapi_sr_rename(args, async = False):
+    server, session = connect()
+    sr = execute(server, 'SR.get_by_name_label', (session, args[0]))
+    execute(server, 'SR.set_name_label', (session, sr[0], args[1]))
+
+def xapi_vdi_create(args, async = False):
     opts, args = parse_args('vdi-create', args)
 
     if len(args) > 0:
@@ -436,66 +546,86 @@ def xapi_vdi_create(*args):
     for opt, val in opts:
         cfg[opt] = val
 
-    server, session = _connect()
-    srs = execute(server.SR.get_all, session)
+    server, session = connect()
+    srs = execute(server, 'SR.get_all', (session,))
     sr = srs[0]
     cfg['SR'] = sr
 
     size = (cfg['virtual_size'] * cfg['sector_size'])/MB
     print 'Creating VDI of size: %dMB ..' % size,
-    uuid = execute(server.VDI.create, session, cfg)
-    print 'Done. (%s)' % uuid
-
-def xapi_vdi_delete(*args):
-    server, session = _connect()
+    uuid = execute(server, 'VDI.create', (session, cfg), async = async)
+    if async:
+        print 'Task started: %s' % uuid
+    else:
+        print 'Done. (%s)' % uuid
+    
+
+def xapi_vdi_destroy(args, async = False):
+    server, session = connect()
     if len(args) < 1:
         raise OptionError('Not enough arguments')
 
     vdi_uuid = args[0]
     print 'Deleting VDI %s' % vdi_uuid
-    result = execute(server.VDI.destroy, session, vdi_uuid)
-    print 'Done.'
-
-def xapi_vdi_rename(*args):
-    server, session = _connect()
+    result = execute(server, 'VDI.destroy', (session, vdi_uuid), async = async)
+    if async:
+        print 'Task started: %s' % result
+    else:
+        print 'Done.'
+
+def xapi_vdi_rename(args, async = False):
+    server, session = connect()
     if len(args) < 2:
         raise OptionError('Not enough arguments')
 
-    vdi_uuid = execute(server.VDI.get_by_name_label, session, args[0])
+    vdi_uuid = execute(server, 'VDI.get_by_name_label', session, args[0])
     vdi_name = args[1]
     
     print 'Renaming VDI %s to %s' % (vdi_uuid[0], vdi_name)
-    result = execute(server.VDI.set_name_label, session, vdi_uuid[0], vdi_name)
-    print 'Done.'
-
-
-def xapi_vtpm_create(*args):
-    server, session = _connect()
+    result = execute(server, 'VDI.set_name_label',
+                     (session, vdi_uuid[0], vdi_name), async = async)
+    if async:
+        print 'Task started: %s' % result
+    else:
+        print 'Done.'
+
+
+
+def xapi_vtpm_create(args, async = False):
+    server, session = connect()
     domname = args[0]
     cfg = _read_python_cfg(args[1])
 
     vm_uuid = resolve_vm(server, session, domname)
     cfg['VM'] = vm_uuid
     print "Creating vTPM with cfg = %s" % cfg
-    vtpm_uuid = execute(server.VTPM.create, session, cfg)
+    vtpm_uuid = execute(server, 'VTPM.create', (session, cfg))
     print "Done. (%s)" % vtpm_uuid
-    vtpm_id = execute(server.VTPM.get_instance, session, vtpm_uuid)
+    vtpm_id = execute(server, 'VTPM.get_instance', (session, vtpm_uuid))
     print "Has instance number '%s'" % vtpm_id
-    vtpm_be = execute(server.VTPM.get_backend, session, vtpm_uuid)
+    vtpm_be = execute(server, 'VTPM.get_backend', (session, vtpm_uuid))
     print "Has backend in '%s'" % vtpm_be
-    driver = execute(server.VTPM.get_driver, session, vtpm_uuid)
+    driver = execute(server, 'VTPM.get_driver', (session, vtpm_uuid))
     print "Has driver type '%s'" % driver
-    vtpm_rec = execute(server.VTPM.get_record, session, vtpm_uuid)
+    vtpm_rec = execute(server, 'VTPM.get_record', (session, vtpm_uuid))
     print "Has vtpm record '%s'" % vtpm_rec
 
 
-def xapi_pif_list(*args):
-    server, session = _connect()
-    pif_uuids = execute(server.PIF.get_all, session)
+def xapi_pif_list(args, async = False):
+    server, session = connect()
+    pif_uuids = execute(server, 'PIF.get_all', (session,))
     for pif_uuid in pif_uuids:
-        pif = execute(server.PIF.get_record, session, pif_uuid)
+        pif = execute(server, 'PIF.get_record', (session, pif_uuid))
         print pif
-    
+
+
+def xapi_debug_wait(args, async = False):
+    secs = 10
+    if len(args) > 0:
+        secs = int(args[0])
+    server, session = connect()
+    task_uuid = execute(server, 'Debug.wait', (session, secs), async=async)
+    print 'Task UUID: %s' % task_uuid
 
 #
 # Command Line Utils
@@ -514,12 +644,17 @@ class XenAPICmd(cmd.Cmd):
         words = shlex.split(line)
         if len(words) > 0:
             cmd_name = words[0].replace('-', '_')
+            is_async = 'async' in cmd_name
+            if is_async:
+                cmd_name = re.sub('async_', '', cmd_name)
+                
             func_name = 'xapi_%s' % cmd_name
             func = globals().get(func_name)
+            
             if func:
                 try:
                     args = tuple(words[1:])
-                    func(*args)
+                    func(args, async = is_async)
                     return True
                 except SystemExit:
                     return False
@@ -555,7 +690,7 @@ class XenAPICmd(cmd.Cmd):
             return line
 
 def shell():
-    server, session = _connect()
+    server, session = connect()
     x = XenAPICmd(server, session)
     x.cmdloop('Xen API Prompt. Type "help" for a list of functions')
 
@@ -582,8 +717,11 @@ def main(args):
         usage()
         sys.exit(1)
 
-    subcmd = args[0]
-    subcmd_func_name = 'xapi_' + subcmd.replace('-', '_')
+    subcmd = args[0].replace('-', '_')
+    is_async = 'async' in subcmd
+    if is_async:
+        subcmd = re.sub('async_', '', subcmd)
+    subcmd_func_name = 'xapi_' + subcmd
     subcmd_func = globals().get(subcmd_func_name, None)
 
     if subcmd == 'shell':
@@ -598,7 +736,7 @@ def main(args):
         sys.exit(1)
         
     try:
-        subcmd_func(*args[1:])
+        subcmd_func(args[1:], async = is_async)
     except XenAPIError, e:
         print 'Error: %s' % str(e.args[0])
         sys.exit(2)

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.