# HG changeset patch
# User Alastair Tse <atse@xxxxxxxxxxxxx>
# Node ID 336fd2e8745b6b26b0d67ea64141fd5ae9e5a76c
# Parent 73cd81de36eaecb01e67e13ea17b27da2aa2b3f6
[XENAPI] Add shell option for xapi.py so it can remember sessions.
Signed-off-by: Alastair Tse <atse@xxxxxxxxxxxxx>
---
tools/python/scripts/xapi.domcfg.py | 2
tools/python/scripts/xapi.py | 101 ++++++++++++++++++++++++++++--------
tools/python/scripts/xapi.vbdcfg.py | 2
3 files changed, 83 insertions(+), 22 deletions(-)
diff -r 73cd81de36ea -r 336fd2e8745b tools/python/scripts/xapi.domcfg.py
--- a/tools/python/scripts/xapi.domcfg.py Fri Oct 20 13:33:55 2006 +0100
+++ b/tools/python/scripts/xapi.domcfg.py Fri Oct 20 13:35:25 2006 +0100
@@ -31,7 +31,7 @@ builder = ''
builder = ''
boot_method = '' # this will remove the kernel/initrd ??
kernel_kernel = '/boot/vmlinuz-2.6.16.29-xen'
-kernel_initrd = '/root/initrd.img-2.6.16.29-xen.ramfs'
+kernel_initrd = '/root/initrd-2.6.16.29-xen.img'
kernel_args = 'root=/dev/sda1 ro'
grub_cmdline = ''
PCI_bus = ''
diff -r 73cd81de36ea -r 336fd2e8745b tools/python/scripts/xapi.py
--- a/tools/python/scripts/xapi.py Fri Oct 20 13:33:55 2006 +0100
+++ b/tools/python/scripts/xapi.py Fri Oct 20 13:35:25 2006 +0100
@@ -26,7 +26,7 @@ MB = 1024 * 1024
HOST_INFO_FORMAT = '%-20s: %-50s'
VM_LIST_FORMAT = '%(name_label)-18s %(memory_actual)-5s %(vcpus_number)-5s'\
- ' %(power_state)-12s %(uuid)-36s'
+ ' %(power_state)-10s %(uuid)-36s'
SR_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(physical_size)-10s' \
'%(type)-10s'
VDI_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(virtual_size)-8s '\
@@ -130,15 +130,19 @@ def execute(fn, *args):
raise XenAPIError(*result['ErrorDescription'])
return result['Value']
-
+_initialised = False
+_server = None
+_session = None
def _connect(*args):
- server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
- login = raw_input("Login: ")
- password = getpass()
- creds = (login, password)
- session = execute(server.session.login_with_password, *creds)
- host = execute(server.session.get_this_host, session)
- return (server, session)
+ global _server, _session, _initialised
+ if not _initialised:
+ _server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
+ login = raw_input("Login: ")
+ password = getpass()
+ creds = (login, password)
+ _session = execute(_server.session.login_with_password, *creds)
+ _initialised = True
+ return (_server, _session)
def _stringify(adict):
return dict([(k, str(v)) for k, v in adict.items()])
@@ -269,10 +273,11 @@ def xapi_vm_shutdown(*args):
print 'Done.'
def xapi_vbd_create(*args):
+ opts, args = parse_args('vbd-create', args)
+
if len(args) < 2:
- raise OptionError("Configuration file not specified")
-
- opts, args = parse_args('vbd-create', args)
+ raise OptionError("Configuration file and domain not specified")
+
domname = args[0]
filename = args[1]
@@ -372,13 +377,67 @@ def xapi_vdi_rename(*args):
#
# Command Line Utils
#
-
-def usage(command = None):
+import cmd
+class XenAPICmd(cmd.Cmd):
+ def __init__(self, server, session):
+ cmd.Cmd.__init__(self)
+ self.server = server
+ self.session = session
+ self.prompt = ">>> "
+
+ def default(self, line):
+ words = line.split()
+ if len(words) > 0:
+ cmd_name = words[0].replace('-', '_')
+ func_name = 'xapi_%s' % cmd_name
+ func = globals().get(func_name)
+ if func:
+ try:
+ args = tuple(words[1:])
+ func(*args)
+ return True
+ except SystemExit:
+ return False
+ except OptionError, e:
+ print 'Error:', str(e)
+ return False
+ except Exception, e:
+ import traceback
+ traceback.print_exc()
+ return False
+ print '*** Unknown command: %s' % words[0]
+ return False
+
+ def do_EOF(self, line):
+ print
+ sys.exit(0)
+
+ def do_help(self, line):
+ usage(print_usage = False)
+
+ def postcmd(self, stop, line):
+ return False
+
+ def precmd(self, line):
+ words = line.split()
+ if len(words) > 0:
+ words0 = words[0].replace('-', '_')
+ return ' '.join([words0] + words[1:])
+ else:
+ return line
+
+def shell():
+ server, session = _connect()
+ x = XenAPICmd(server, session)
+ x.cmdloop('Xen API Prompt. Type "help" for a list of functions')
+
+def usage(command = None, print_usage = True):
if not command:
- print 'Usage: xapi <subcommand> [options] [args]'
- print
- print 'Subcommands:'
- print
+ if print_usage:
+ print 'Usage: xapi <subcommand> [options] [args]'
+ print
+ print 'Subcommands:'
+ print
sorted_commands = sorted(COMMANDS.keys())
for command in sorted_commands:
args, description = COMMANDS[command]
@@ -394,10 +453,12 @@ def main(args):
sys.exit(1)
subcmd = args[0]
-
subcmd_func_name = 'xapi_' + subcmd.replace('-', '_')
subcmd_func = globals().get(subcmd_func_name, None)
- if not subcmd_func or not callable(subcmd_func):
+
+ if subcmd == 'shell':
+ shell()
+ elif not subcmd_func or not callable(subcmd_func):
print 'Error: Unable to find subcommand \'%s\'' % subcmd
usage()
sys.exit(1)
diff -r 73cd81de36ea -r 336fd2e8745b tools/python/scripts/xapi.vbdcfg.py
--- a/tools/python/scripts/xapi.vbdcfg.py Fri Oct 20 13:33:55 2006 +0100
+++ b/tools/python/scripts/xapi.vbdcfg.py Fri Oct 20 13:35:25 2006 +0100
@@ -9,4 +9,4 @@ device = 'sda1'
device = 'sda1'
mode = 'RW'
driver = 'paravirtualised'
-image = 'file:/root/gentoo-64-xenU.img'
+image = 'file:/root/gentoo.amd64.img'
_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog
|