ia64/xen-unstable

changeset 12810:cf1ca0615414

Added support for configuration file for xm, and use that to specify
contact and authentication details for the Xen-API server.

The default behaviour is still to use the existing legacy XML-RPC server.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Dec 06 11:02:32 2006 +0000 (2006-12-06)
parents 92127156ec49
children 795a87426e48
files tools/examples/xm-config.xml tools/python/xen/xm/create.py tools/python/xen/xm/main.py tools/python/xen/xm/migrate.py tools/python/xen/xm/new.py tools/python/xen/xm/shutdown.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/examples/xm-config.xml	Wed Dec 06 11:02:32 2006 +0000
     1.3 @@ -0,0 +1,43 @@
     1.4 +<!--
     1.5 +
     1.6 +Copyright (C) 2006 XenSource Inc.
     1.7 +
     1.8 +This library is free software; you can redistribute it and/or
     1.9 +modify it under the terms of version 2.1 of the GNU Lesser General Public
    1.10 +License as published by the Free Software Foundation.
    1.11 +
    1.12 +This library is distributed in the hope that it will be useful,
    1.13 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 +Lesser General Public License for more details.
    1.16 +
    1.17 +You should have received a copy of the GNU Lesser General Public
    1.18 +License along with this library; if not, write to the Free Software
    1.19 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 +
    1.21 +-->
    1.22 +
    1.23 +<!--
    1.24 +
    1.25 +This is a configuration file for xm; it should be placed in
    1.26 +/etc/xen/xm-config.xml.  If this file is missing, then xm will fall back to
    1.27 +the normal behaviour that's in Xen 3.0.4 and below.  The settings here are
    1.28 +most useful for experimenting with the Xen-API preview in Xen 3.0.4.
    1.29 +
    1.30 +-->
    1.31 +
    1.32 +<xm>
    1.33 +  <!-- The server element describes how to talk to Xend.  The type may be 
    1.34 +       Xen-API or LegacyXMLRPC (the default).  The URI is that of the
    1.35 +       server; you might try http://server:9363/ or
    1.36 +       httpu:///var/run/xend/xen-api.sock for the Xen-API, or
    1.37 +       httpu:///var/run/xend/xmlrpc.sock for the legacy server.
    1.38 +
    1.39 +       The username and password attributes will be used to log in if Xen-API
    1.40 +       is being used.
    1.41 +    -->
    1.42 +  <server type='Xen-API'
    1.43 +          uri='http://localhost:9363/'
    1.44 +          username='me'
    1.45 +          password='mypassword' />
    1.46 +</xm>
     2.1 --- a/tools/python/xen/xm/create.py	Wed Dec 06 10:47:31 2006 +0000
     2.2 +++ b/tools/python/xen/xm/create.py	Wed Dec 06 11:02:32 2006 +0000
     2.3 @@ -29,13 +29,13 @@ import xmlrpclib
     2.4  from xen.xend import sxp
     2.5  from xen.xend import PrettyPrint
     2.6  import xen.xend.XendClient
     2.7 -from xen.xend.XendClient import server
     2.8  from xen.xend.XendBootloader import bootloader
     2.9  from xen.util import blkif
    2.10  from xen.util import security
    2.11  
    2.12  from xen.xm.opts import *
    2.13  
    2.14 +from main import server
    2.15  import console
    2.16  
    2.17  
     3.1 --- a/tools/python/xen/xm/main.py	Wed Dec 06 10:47:31 2006 +0000
     3.2 +++ b/tools/python/xen/xm/main.py	Wed Dec 06 11:02:32 2006 +0000
     3.3 @@ -21,6 +21,7 @@
     3.4  
     3.5  """Grand unified management application for Xen.
     3.6  """
     3.7 +import atexit
     3.8  import os
     3.9  import sys
    3.10  import re
    3.11 @@ -31,6 +32,7 @@ import xmlrpclib
    3.12  import traceback
    3.13  import datetime
    3.14  from select import select
    3.15 +import xml.dom.minidom
    3.16  
    3.17  import warnings
    3.18  warnings.filterwarnings('ignore', category=FutureWarning)
    3.19 @@ -38,12 +40,14 @@ warnings.filterwarnings('ignore', catego
    3.20  from xen.xend import PrettyPrint
    3.21  from xen.xend import sxp
    3.22  from xen.xend import XendClient
    3.23 -from xen.xend.XendClient import server
    3.24  from xen.xend.XendConstants import *
    3.25  
    3.26  from xen.xm.opts import OptionError, Opts, wrap, set_true
    3.27  from xen.xm import console
    3.28  from xen.util import security
    3.29 +from xen.util.xmlrpclib2 import ServerProxy
    3.30 +
    3.31 +import XenAPI
    3.32  
    3.33  # getopt.gnu_getopt is better, but only exists in Python 2.3+.  Use
    3.34  # getopt.getopt if gnu_getopt is not available.  This will mean that options
    3.35 @@ -51,6 +55,12 @@ from xen.util import security
    3.36  if not hasattr(getopt, 'gnu_getopt'):
    3.37      getopt.gnu_getopt = getopt.getopt
    3.38  
    3.39 +XM_CONFIG_FILE = '/etc/xen/xm-config.xml'
    3.40 +
    3.41 +# Supported types of server
    3.42 +SERVER_LEGACY_XMLRPC = 'LegacyXMLRPC'
    3.43 +SERVER_XEN_API = 'Xen-API'
    3.44 +
    3.45  # General help message
    3.46  
    3.47  USAGE_HELP = "Usage: xm <subcommand> [args]\n\n" \
    3.48 @@ -319,6 +329,35 @@ acm_commands = [
    3.49  all_commands = (domain_commands + host_commands + scheduler_commands +
    3.50                  device_commands + vnet_commands + acm_commands)
    3.51  
    3.52 +
    3.53 +##
    3.54 +# Configuration File Parsing
    3.55 +##
    3.56 +
    3.57 +if os.path.isfile(XM_CONFIG_FILE):
    3.58 +    config = xml.dom.minidom.parse(XM_CONFIG_FILE)
    3.59 +else:
    3.60 +    config = None
    3.61 +
    3.62 +def parseServer():
    3.63 +    if config:
    3.64 +        server = config.getElementsByTagName('server')
    3.65 +        if server:
    3.66 +            st = server[0].getAttribute('type')
    3.67 +            if st != SERVER_XEN_API:
    3.68 +                st = SERVER_LEGACY_XMLRPC
    3.69 +            return (st, server[0].getAttribute('uri'))
    3.70 +
    3.71 +    return SERVER_LEGACY_XMLRPC, XendClient.uri
    3.72 +
    3.73 +def parseAuthentication():
    3.74 +    server = config.getElementsByTagName('server')[0]
    3.75 +    return (server.getAttribute('username'),
    3.76 +            server.getAttribute('password'))
    3.77 +
    3.78 +serverType, serverURI = parseServer()
    3.79 +
    3.80 +
    3.81  ####################################################################
    3.82  #
    3.83  #  Help/usage printing functions
    3.84 @@ -469,6 +508,16 @@ def err(msg):
    3.85      print >>sys.stderr, "Error:", msg
    3.86  
    3.87  
    3.88 +def get_single_vm(dom):
    3.89 +    uuids = server.xenapi.VM.get_by_name_label(dom)
    3.90 +    n = len(uuids)
    3.91 +    if n == 1:
    3.92 +        return uuids[0]
    3.93 +    else:
    3.94 +        dominfo = server.xend.domain(dom, False)
    3.95 +        return dominfo['uuid']
    3.96 +
    3.97 +
    3.98  #########################################################################
    3.99  #
   3.100  #  Main xm functions
   3.101 @@ -767,17 +816,26 @@ def xm_start(args):
   3.102          sys.exit(1)
   3.103  
   3.104      dom = params[0]
   3.105 -    server.xend.domain.start(dom, paused)
   3.106 +    if serverType == SERVER_XEN_API:
   3.107 +        server.xenapi.VM.start(get_single_vm(dom), paused)
   3.108 +    else:
   3.109 +        server.xend.domain.start(dom, paused)
   3.110  
   3.111  def xm_delete(args):
   3.112      arg_check(args, "delete", 1)
   3.113      dom = args[0]
   3.114 -    server.xend.domain.delete(dom)
   3.115 +    if serverType == SERVER_XEN_API:
   3.116 +        server.xenapi.VM.destroy(dom)
   3.117 +    else:
   3.118 +        server.xend.domain.delete(dom)
   3.119  
   3.120  def xm_suspend(args):
   3.121      arg_check(args, "suspend", 1)
   3.122      dom = args[0]
   3.123 -    server.xend.domain.suspend(dom)
   3.124 +    if serverType == SERVER_XEN_API:
   3.125 +        server.xenapi.VM.suspend(get_single_vm(dom))
   3.126 +    else:
   3.127 +        server.xend.domain.suspend(dom)
   3.128  
   3.129  def xm_resume(args):
   3.130      arg_check(args, "resume", 1, 2)
   3.131 @@ -799,7 +857,10 @@ def xm_resume(args):
   3.132          sys.exit(1)
   3.133  
   3.134      dom = params[0]
   3.135 -    server.xend.domain.resume(dom, paused)
   3.136 +    if serverType == SERVER_XEN_API:
   3.137 +        server.xenapi.VM.resume(dom, paused)
   3.138 +    else:
   3.139 +        server.xend.domain.resume(dom, paused)
   3.140      
   3.141  def xm_reboot(args):
   3.142      arg_check(args, "reboot", 1, 3)
   3.143 @@ -1577,6 +1638,8 @@ def deprecated(old,new):
   3.144          "Command %s is deprecated.  Please use xm %s instead." % (old, new))
   3.145  
   3.146  def main(argv=sys.argv):
   3.147 +    global server
   3.148 +
   3.149      if len(argv) < 2:
   3.150          usage()
   3.151  
   3.152 @@ -1595,6 +1658,19 @@ def main(argv=sys.argv):
   3.153      args = argv[2:]
   3.154      if cmd:
   3.155          try:
   3.156 +            if serverType == SERVER_XEN_API:
   3.157 +                server = XenAPI.Session(serverURI)
   3.158 +                username, password = parseAuthentication()
   3.159 +                server.login_with_password(username, password)
   3.160 +                def logout():
   3.161 +                    try:
   3.162 +                        server.xenapi.session.logout()
   3.163 +                    except:
   3.164 +                        pass
   3.165 +                atexit.register(logout)
   3.166 +            else:
   3.167 +                server = ServerProxy(serverURI)
   3.168 +
   3.169              rc = cmd(args)
   3.170              if rc:
   3.171                  usage()
   3.172 @@ -1615,6 +1691,9 @@ def main(argv=sys.argv):
   3.173              sys.exit(1)
   3.174          except SystemExit:
   3.175              sys.exit(1)
   3.176 +        except XenAPI.Failure, exn:
   3.177 +            err(str(exn))
   3.178 +            sys.exit(1)
   3.179          except xmlrpclib.Fault, ex:
   3.180              if ex.faultCode == XendClient.ERROR_INVALID_DOMAIN:
   3.181                  err("Domain '%s' does not exist." % ex.faultString)
     4.1 --- a/tools/python/xen/xm/migrate.py	Wed Dec 06 10:47:31 2006 +0000
     4.2 +++ b/tools/python/xen/xm/migrate.py	Wed Dec 06 11:02:32 2006 +0000
     4.3 @@ -21,9 +21,10 @@
     4.4  
     4.5  import sys
     4.6  
     4.7 -from xen.xend.XendClient import server
     4.8  from xen.xm.opts import *
     4.9  
    4.10 +from main import server
    4.11 +
    4.12  gopts = Opts(use="""[options] DOM HOST
    4.13  
    4.14  Migrate domain DOM to host HOST.
     5.1 --- a/tools/python/xen/xm/new.py	Wed Dec 06 10:47:31 2006 +0000
     5.2 +++ b/tools/python/xen/xm/new.py	Wed Dec 06 11:02:32 2006 +0000
     5.3 @@ -21,10 +21,9 @@ import xmlrpclib
     5.4  from xen.xend import PrettyPrint
     5.5  from xen.xend import sxp
     5.6  from xen.xend import XendClient
     5.7 -from xen.xend.XendClient import server
     5.8  
     5.9 -from xen.xm.opts import *
    5.10 -from xen.xm.create import *
    5.11 +from opts import *
    5.12 +from create import *
    5.13  
    5.14  def make_unstarted_domain(opts, config):
    5.15      """Create an unstarted domain.
     6.1 --- a/tools/python/xen/xm/shutdown.py	Wed Dec 06 10:47:31 2006 +0000
     6.2 +++ b/tools/python/xen/xm/shutdown.py	Wed Dec 06 11:02:32 2006 +0000
     6.3 @@ -19,9 +19,9 @@
     6.4  """
     6.5  import time
     6.6  
     6.7 -from xen.xend.XendClient import server
     6.8  from xen.xend import sxp
     6.9 -from xen.xm.opts import *
    6.10 +from opts import *
    6.11 +from main import server
    6.12  
    6.13  gopts = Opts(use="""[options] [DOM]
    6.14