ia64/xen-unstable

changeset 12143:336fd2e8745b

[XENAPI] Add shell option for xapi.py so it can remember sessions.

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Oct 20 13:35:25 2006 +0100 (2006-10-20)
parents 73cd81de36ea
children 1f790f5fcdbb
files tools/python/scripts/xapi.domcfg.py tools/python/scripts/xapi.py tools/python/scripts/xapi.vbdcfg.py
line diff
     1.1 --- a/tools/python/scripts/xapi.domcfg.py	Fri Oct 20 13:33:55 2006 +0100
     1.2 +++ b/tools/python/scripts/xapi.domcfg.py	Fri Oct 20 13:35:25 2006 +0100
     1.3 @@ -31,7 +31,7 @@ platform_enable_audio =  False
     1.4  builder =  ''
     1.5  boot_method =  '' # this will remove the kernel/initrd ??
     1.6  kernel_kernel =  '/boot/vmlinuz-2.6.16.29-xen'
     1.7 -kernel_initrd =  '/root/initrd.img-2.6.16.29-xen.ramfs'
     1.8 +kernel_initrd =  '/root/initrd-2.6.16.29-xen.img'
     1.9  kernel_args =  'root=/dev/sda1 ro'
    1.10  grub_cmdline =  ''
    1.11  PCI_bus =  ''
     2.1 --- a/tools/python/scripts/xapi.py	Fri Oct 20 13:33:55 2006 +0100
     2.2 +++ b/tools/python/scripts/xapi.py	Fri Oct 20 13:35:25 2006 +0100
     2.3 @@ -26,7 +26,7 @@ MB = 1024 * 1024
     2.4  
     2.5  HOST_INFO_FORMAT = '%-20s: %-50s'
     2.6  VM_LIST_FORMAT = '%(name_label)-18s %(memory_actual)-5s %(vcpus_number)-5s'\
     2.7 -                 ' %(power_state)-12s %(uuid)-36s'
     2.8 +                 ' %(power_state)-10s %(uuid)-36s'
     2.9  SR_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(physical_size)-10s' \
    2.10                   '%(type)-10s'
    2.11  VDI_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(virtual_size)-8s '\
    2.12 @@ -130,15 +130,19 @@ def execute(fn, *args):
    2.13          raise XenAPIError(*result['ErrorDescription'])
    2.14      return result['Value']
    2.15  
    2.16 -
    2.17 +_initialised = False
    2.18 +_server = None
    2.19 +_session = None
    2.20  def _connect(*args):
    2.21 -    server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
    2.22 -    login = raw_input("Login: ")
    2.23 -    password = getpass()
    2.24 -    creds = (login, password)
    2.25 -    session = execute(server.session.login_with_password, *creds)
    2.26 -    host = execute(server.session.get_this_host, session)
    2.27 -    return (server, session)
    2.28 +    global _server, _session, _initialised
    2.29 +    if not _initialised:
    2.30 +        _server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
    2.31 +        login = raw_input("Login: ")
    2.32 +        password = getpass()
    2.33 +        creds = (login, password)
    2.34 +        _session = execute(_server.session.login_with_password, *creds)
    2.35 +        _initialised = True
    2.36 +    return (_server, _session)
    2.37  
    2.38  def _stringify(adict):
    2.39      return dict([(k, str(v)) for k, v in adict.items()])
    2.40 @@ -269,10 +273,11 @@ def xapi_vm_shutdown(*args):
    2.41      print 'Done.'
    2.42  
    2.43  def xapi_vbd_create(*args):
    2.44 +    opts, args = parse_args('vbd-create', args)
    2.45 +
    2.46      if len(args) < 2:
    2.47 -        raise OptionError("Configuration file not specified")
    2.48 +        raise OptionError("Configuration file and domain not specified")
    2.49  
    2.50 -    opts, args = parse_args('vbd-create', args)
    2.51      domname = args[0]
    2.52      filename = args[1]
    2.53  
    2.54 @@ -372,13 +377,67 @@ def xapi_vdi_rename(*args):
    2.55  #
    2.56  # Command Line Utils
    2.57  #
    2.58 +import cmd
    2.59 +class XenAPICmd(cmd.Cmd):
    2.60 +    def __init__(self, server, session):
    2.61 +        cmd.Cmd.__init__(self)
    2.62 +        self.server = server
    2.63 +        self.session = session
    2.64 +        self.prompt = ">>> "
    2.65  
    2.66 -def usage(command = None):
    2.67 +    def default(self, line):
    2.68 +        words = line.split()
    2.69 +        if len(words) > 0:
    2.70 +            cmd_name = words[0].replace('-', '_')
    2.71 +            func_name = 'xapi_%s' % cmd_name
    2.72 +            func = globals().get(func_name)
    2.73 +            if func:
    2.74 +                try:
    2.75 +                    args = tuple(words[1:])
    2.76 +                    func(*args)
    2.77 +                    return True
    2.78 +                except SystemExit:
    2.79 +                    return False
    2.80 +                except OptionError, e:
    2.81 +                    print 'Error:', str(e)
    2.82 +                    return False
    2.83 +                except Exception, e:
    2.84 +                    import traceback
    2.85 +                    traceback.print_exc()
    2.86 +                    return False
    2.87 +        print '*** Unknown command: %s' % words[0]
    2.88 +        return False
    2.89 +
    2.90 +    def do_EOF(self, line):
    2.91 +        print
    2.92 +        sys.exit(0)
    2.93 +
    2.94 +    def do_help(self, line):
    2.95 +        usage(print_usage = False)
    2.96 +
    2.97 +    def postcmd(self, stop, line):
    2.98 +        return False
    2.99 +
   2.100 +    def precmd(self, line):
   2.101 +        words = line.split()
   2.102 +        if len(words) > 0:
   2.103 +            words0 = words[0].replace('-', '_')
   2.104 +            return ' '.join([words0] + words[1:])
   2.105 +        else:
   2.106 +            return line
   2.107 +
   2.108 +def shell():
   2.109 +    server, session = _connect()
   2.110 +    x = XenAPICmd(server, session)
   2.111 +    x.cmdloop('Xen API Prompt. Type "help" for a list of functions')
   2.112 +
   2.113 +def usage(command = None, print_usage = True):
   2.114      if not command:
   2.115 -        print 'Usage: xapi <subcommand> [options] [args]'
   2.116 -        print
   2.117 -        print 'Subcommands:'
   2.118 -        print
   2.119 +        if print_usage:
   2.120 +            print 'Usage: xapi <subcommand> [options] [args]'
   2.121 +            print
   2.122 +            print 'Subcommands:'
   2.123 +            print
   2.124          sorted_commands = sorted(COMMANDS.keys())
   2.125          for command  in sorted_commands:
   2.126              args, description = COMMANDS[command]
   2.127 @@ -394,10 +453,12 @@ def main(args):
   2.128          sys.exit(1)
   2.129  
   2.130      subcmd = args[0]
   2.131 -
   2.132      subcmd_func_name = 'xapi_' + subcmd.replace('-', '_')
   2.133      subcmd_func = globals().get(subcmd_func_name, None)
   2.134 -    if not subcmd_func or not callable(subcmd_func):
   2.135 +
   2.136 +    if subcmd == 'shell':
   2.137 +        shell()
   2.138 +    elif not subcmd_func or not callable(subcmd_func):
   2.139          print 'Error: Unable to find subcommand \'%s\'' % subcmd
   2.140          usage()
   2.141          sys.exit(1)
     3.1 --- a/tools/python/scripts/xapi.vbdcfg.py	Fri Oct 20 13:33:55 2006 +0100
     3.2 +++ b/tools/python/scripts/xapi.vbdcfg.py	Fri Oct 20 13:35:25 2006 +0100
     3.3 @@ -9,4 +9,4 @@ VDI =  ''
     3.4  device = 'sda1'
     3.5  mode = 'RW'
     3.6  driver = 'paravirtualised'
     3.7 -image = 'file:/root/gentoo-64-xenU.img'
     3.8 +image = 'file:/root/gentoo.amd64.img'