ia64/xen-unstable

changeset 14573:d01e7ace9d2c

Add autodetect to xm create for xml files

signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author Tom Wilkie <tom.wilkie@gmail.com>
date Mon Mar 26 14:57:12 2007 +0100 (2007-03-26)
parents f6c38a084bef
children 3b2efc3cf6b6
files tools/python/xen/xm/create.py tools/python/xen/xm/opts.py tools/python/xen/xm/xenapi_create.py
line diff
     1.1 --- a/tools/python/xen/xm/create.py	Tue Mar 27 00:54:05 2007 +0100
     1.2 +++ b/tools/python/xen/xm/create.py	Mon Mar 26 14:57:12 2007 +0100
     1.3 @@ -1098,6 +1098,8 @@ def parseCommandLine(argv):
     1.4      if not gopts.vals.xauthority:
     1.5          gopts.vals.xauthority = get_xauthority()
     1.6  
     1.7 +    gopts.is_xml = False
     1.8 +
     1.9      # Process remaining args as config variables.
    1.10      for arg in args:
    1.11          if '=' in arg:
    1.12 @@ -1106,11 +1108,16 @@ def parseCommandLine(argv):
    1.13      if gopts.vals.config:
    1.14          config = gopts.vals.config
    1.15      else:
    1.16 -        gopts.load_defconfig()
    1.17 -        preprocess(gopts.vals)
    1.18 -        if not gopts.getopt('name') and gopts.getopt('defconfig'):
    1.19 -            gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
    1.20 -        config = make_config(gopts.vals)
    1.21 +        try:
    1.22 +            gopts.load_defconfig()
    1.23 +            preprocess(gopts.vals)
    1.24 +            if not gopts.getopt('name') and gopts.getopt('defconfig'):
    1.25 +                gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
    1.26 +            config = make_config(gopts.vals)
    1.27 +        except XMLFileError, ex:
    1.28 +            XMLFile = ex.getFile()
    1.29 +            gopts.is_xml = True
    1.30 +            config = ex.getFile()
    1.31  
    1.32      return (gopts, config)
    1.33  
    1.34 @@ -1233,6 +1240,8 @@ def help():
    1.35      return str(gopts)
    1.36  
    1.37  def main(argv):
    1.38 +    is_xml = False
    1.39 +    
    1.40      try:
    1.41          (opts, config) = parseCommandLine(argv)
    1.42      except StandardError, ex:
    1.43 @@ -1241,23 +1250,24 @@ def main(argv):
    1.44      if not opts:
    1.45          return
    1.46  
    1.47 -    if type(config) == str:
    1.48 -        try:
    1.49 -            config = sxp.parse(file(config))[0]
    1.50 -        except IOError, exn:
    1.51 -            raise OptionError("Cannot read file %s: %s" % (config, exn[1]))
    1.52 +    if not opts.is_xml:
    1.53 +        if type(config) == str:
    1.54 +            try:
    1.55 +                config = sxp.parse(file(config))[0]
    1.56 +            except IOError, exn:
    1.57 +                raise OptionError("Cannot read file %s: %s" % (config, exn[1]))
    1.58 +        
    1.59 +        if serverType == SERVER_XEN_API:
    1.60 +            from xen.xm.xenapi_create import sxp2xml
    1.61 +            sxp2xml_inst = sxp2xml()
    1.62 +            doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True)
    1.63  
    1.64 -    if serverType == SERVER_XEN_API:
    1.65 -        from xen.xm.xenapi_create import sxp2xml
    1.66 -        sxp2xml_inst = sxp2xml()
    1.67 -        doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True)
    1.68 +        if opts.vals.dryrun and not opts.is_xml:
    1.69 +            SXPPrettyPrint.prettyprint(config)
    1.70  
    1.71 -    if opts.vals.dryrun:
    1.72 -        SXPPrettyPrint.prettyprint(config)
    1.73 -
    1.74 -    if opts.vals.xmldryrun and serverType == SERVER_XEN_API:
    1.75 -        from xml.dom.ext import PrettyPrint as XMLPrettyPrint
    1.76 -        XMLPrettyPrint(doc)
    1.77 +        if opts.vals.xmldryrun and serverType == SERVER_XEN_API:
    1.78 +            from xml.dom.ext import PrettyPrint as XMLPrettyPrint
    1.79 +            XMLPrettyPrint(doc)
    1.80  
    1.81      if opts.vals.dryrun or opts.vals.xmldryrun:
    1.82          return                                               
    1.83 @@ -1268,10 +1278,13 @@ def main(argv):
    1.84      if serverType == SERVER_XEN_API:        
    1.85          from xen.xm.xenapi_create import xenapi_create
    1.86          xenapi_create_inst = xenapi_create()
    1.87 -        vm_refs = xenapi_create_inst.create(document = doc)
    1.88 +        if opts.is_xml:
    1.89 +            vm_refs = xenapi_create_inst.create(filename = config)
    1.90 +        else:
    1.91 +            vm_refs = xenapi_create_inst.create(document = doc)
    1.92  
    1.93          map(lambda vm_ref: server.xenapi.VM.start(vm_ref, 0), vm_refs)
    1.94 -    else:
    1.95 +    elif not opts.is_xml:
    1.96          if not create_security_check(config):
    1.97              raise security.ACMError(
    1.98                  'Security Configuration prevents domain from starting')
     2.1 --- a/tools/python/xen/xm/opts.py	Tue Mar 27 00:54:05 2007 +0100
     2.2 +++ b/tools/python/xen/xm/opts.py	Mon Mar 26 14:57:12 2007 +0100
     2.3 @@ -24,6 +24,8 @@ import os.path
     2.4  import sys
     2.5  import types
     2.6  
     2.7 +
     2.8 +
     2.9  def _line_wrap(text, width = 70):
    2.10      lines = []
    2.11      current_line = ''
    2.12 @@ -60,6 +62,15 @@ class OptionError(Exception):
    2.13      def __str__(self):
    2.14          return self.message
    2.15  
    2.16 +class XMLFileError(Exception):
    2.17 +    """Thrown is input is an XML File"""
    2.18 +    def __init__(self, XMLFile):
    2.19 +        self.XMLFile = XMLFile
    2.20 +    def __str__(self):
    2.21 +        return "XMLFileError: %s" % self.XMLFile
    2.22 +    def getFile(self):
    2.23 +        return self.XMLFile
    2.24 +
    2.25  class Opt:
    2.26      """An individual option.
    2.27      """
    2.28 @@ -492,6 +503,14 @@ class Opts:
    2.29                  p = os.path.join(os.path.curdir, p)
    2.30              if os.path.exists(p):
    2.31                  self.info('Using config file "%s".' % p)
    2.32 +
    2.33 +                f = open(p)
    2.34 +                is_xml = (f.read(1) == '<')
    2.35 +                f.close()
    2.36 +
    2.37 +                if is_xml:
    2.38 +                    raise XMLFileError(p)
    2.39 +
    2.40                  self.load(p, help)
    2.41                  break
    2.42          else:
     3.1 --- a/tools/python/xen/xm/xenapi_create.py	Tue Mar 27 00:54:05 2007 +0100
     3.2 +++ b/tools/python/xen/xm/xenapi_create.py	Mon Mar 26 14:57:12 2007 +0100
     3.3 @@ -80,8 +80,8 @@ class xenapi_create:
     3.4          Create a domain from an XML file or DOM tree
     3.5          """
     3.6          if filename is not None:
     3.7 -            self.check_dtd(file)
     3.8 -            document = parse(file)
     3.9 +            self.check_dtd(filename)
    3.10 +            document = parse(filename)
    3.11          elif document is not None:
    3.12              self.check_dom_against_dtd(document)
    3.13