# HG changeset patch
# User Tom Wilkie <tom.wilkie@xxxxxxxxx>
# Date 1174917432 -3600
# Node ID d01e7ace9d2c27d652d624a313fe9d3224aae1b1
# Parent f6c38a084bef70bbf95816ae3b035ac8cb706339
Add autodetect to xm create for xml files
signed-off-by: Tom Wilkie <tom.wilkie@xxxxxxxxx>
---
tools/python/xen/xm/create.py | 61 +++++++++++++++++++++--------------
tools/python/xen/xm/opts.py | 19 ++++++++++
tools/python/xen/xm/xenapi_create.py | 4 +-
3 files changed, 58 insertions(+), 26 deletions(-)
diff -r f6c38a084bef -r d01e7ace9d2c tools/python/xen/xm/create.py
--- a/tools/python/xen/xm/create.py Tue Mar 27 00:54:05 2007 +0100
+++ b/tools/python/xen/xm/create.py Mon Mar 26 14:57:12 2007 +0100
@@ -1098,6 +1098,8 @@ def parseCommandLine(argv):
if not gopts.vals.xauthority:
gopts.vals.xauthority = get_xauthority()
+ gopts.is_xml = False
+
# Process remaining args as config variables.
for arg in args:
if '=' in arg:
@@ -1106,11 +1108,16 @@ def parseCommandLine(argv):
if gopts.vals.config:
config = gopts.vals.config
else:
- gopts.load_defconfig()
- preprocess(gopts.vals)
- if not gopts.getopt('name') and gopts.getopt('defconfig'):
- gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
- config = make_config(gopts.vals)
+ try:
+ gopts.load_defconfig()
+ preprocess(gopts.vals)
+ if not gopts.getopt('name') and gopts.getopt('defconfig'):
+ gopts.setopt('name',
os.path.basename(gopts.getopt('defconfig')))
+ config = make_config(gopts.vals)
+ except XMLFileError, ex:
+ XMLFile = ex.getFile()
+ gopts.is_xml = True
+ config = ex.getFile()
return (gopts, config)
@@ -1233,6 +1240,8 @@ def help():
return str(gopts)
def main(argv):
+ is_xml = False
+
try:
(opts, config) = parseCommandLine(argv)
except StandardError, ex:
@@ -1241,23 +1250,24 @@ def main(argv):
if not opts:
return
- if type(config) == str:
- try:
- config = sxp.parse(file(config))[0]
- except IOError, exn:
- raise OptionError("Cannot read file %s: %s" % (config, exn[1]))
-
- if serverType == SERVER_XEN_API:
- from xen.xm.xenapi_create import sxp2xml
- sxp2xml_inst = sxp2xml()
- doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True)
-
- if opts.vals.dryrun:
- SXPPrettyPrint.prettyprint(config)
-
- if opts.vals.xmldryrun and serverType == SERVER_XEN_API:
- from xml.dom.ext import PrettyPrint as XMLPrettyPrint
- XMLPrettyPrint(doc)
+ if not opts.is_xml:
+ if type(config) == str:
+ try:
+ config = sxp.parse(file(config))[0]
+ except IOError, exn:
+ raise OptionError("Cannot read file %s: %s" % (config, exn[1]))
+
+ if serverType == SERVER_XEN_API:
+ from xen.xm.xenapi_create import sxp2xml
+ sxp2xml_inst = sxp2xml()
+ doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True)
+
+ if opts.vals.dryrun and not opts.is_xml:
+ SXPPrettyPrint.prettyprint(config)
+
+ if opts.vals.xmldryrun and serverType == SERVER_XEN_API:
+ from xml.dom.ext import PrettyPrint as XMLPrettyPrint
+ XMLPrettyPrint(doc)
if opts.vals.dryrun or opts.vals.xmldryrun:
return
@@ -1268,10 +1278,13 @@ def main(argv):
if serverType == SERVER_XEN_API:
from xen.xm.xenapi_create import xenapi_create
xenapi_create_inst = xenapi_create()
- vm_refs = xenapi_create_inst.create(document = doc)
+ if opts.is_xml:
+ vm_refs = xenapi_create_inst.create(filename = config)
+ else:
+ vm_refs = xenapi_create_inst.create(document = doc)
map(lambda vm_ref: server.xenapi.VM.start(vm_ref, 0), vm_refs)
- else:
+ elif not opts.is_xml:
if not create_security_check(config):
raise security.ACMError(
'Security Configuration prevents domain from starting')
diff -r f6c38a084bef -r d01e7ace9d2c tools/python/xen/xm/opts.py
--- a/tools/python/xen/xm/opts.py Tue Mar 27 00:54:05 2007 +0100
+++ b/tools/python/xen/xm/opts.py Mon Mar 26 14:57:12 2007 +0100
@@ -24,6 +24,8 @@ import sys
import sys
import types
+
+
def _line_wrap(text, width = 70):
lines = []
current_line = ''
@@ -59,6 +61,15 @@ class OptionError(Exception):
self.usage = usage
def __str__(self):
return self.message
+
+class XMLFileError(Exception):
+ """Thrown is input is an XML File"""
+ def __init__(self, XMLFile):
+ self.XMLFile = XMLFile
+ def __str__(self):
+ return "XMLFileError: %s" % self.XMLFile
+ def getFile(self):
+ return self.XMLFile
class Opt:
"""An individual option.
@@ -492,6 +503,14 @@ class Opts:
p = os.path.join(os.path.curdir, p)
if os.path.exists(p):
self.info('Using config file "%s".' % p)
+
+ f = open(p)
+ is_xml = (f.read(1) == '<')
+ f.close()
+
+ if is_xml:
+ raise XMLFileError(p)
+
self.load(p, help)
break
else:
diff -r f6c38a084bef -r d01e7ace9d2c tools/python/xen/xm/xenapi_create.py
--- a/tools/python/xen/xm/xenapi_create.py Tue Mar 27 00:54:05 2007 +0100
+++ b/tools/python/xen/xm/xenapi_create.py Mon Mar 26 14:57:12 2007 +0100
@@ -80,8 +80,8 @@ class xenapi_create:
Create a domain from an XML file or DOM tree
"""
if filename is not None:
- self.check_dtd(file)
- document = parse(file)
+ self.check_dtd(filename)
+ document = parse(filename)
elif document is not None:
self.check_dom_against_dtd(document)
_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog
|