diff options
author | Daniel P. Berrange <berrange@redhat.com> | 2013-11-22 15:55:39 +0000 |
---|---|---|
committer | Daniel P. Berrange <berrange@redhat.com> | 2013-11-22 15:55:39 +0000 |
commit | 34aa32ba8a4d3373ce01f999021e58638c4c3324 (patch) | |
tree | 3bba26150dff600f9f1ca99a34e5397c4c786c66 /examples/domstart.py | |
parent | 70b8c9a2a3fe293aa055fde0d8a9df13263c3fd8 (diff) | |
download | libvirt-python-v9-34aa32ba8a4d3373ce01f999021e58638c4c3324.tar.gz libvirt-python-v9-34aa32ba8a4d3373ce01f999021e58638c4c3324.tar.xz libvirt-python-v9-34aa32ba8a4d3373ce01f999021e58638c4c3324.zip |
Move python example programs into python/examples/ subdirectory
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'examples/domstart.py')
-rwxr-xr-x | examples/domstart.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/domstart.py b/examples/domstart.py new file mode 100755 index 0000000..b14fad1 --- /dev/null +++ b/examples/domstart.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# domstart - make sure a given domU is running, if not start it + +import libvirt +import sys +import os +import libxml2 +import pdb + +# Parse the XML description of domU from FNAME +# and return a tuple (name, xmldesc) where NAME +# is the name of the domain, and xmldesc is the contetn of FNAME +def read_domain(fname): + fp = open(fname, "r") + xmldesc = fp.read() + fp.close() + + doc = libxml2.parseDoc(xmldesc) + name = doc.xpathNewContext().xpathEval("/domain/name")[0].content + return (name, xmldesc) + +def usage(): + print 'Usage: %s domain.xml' % sys.argv[0] + print ' Check that the domain described by DOMAIN.XML is running' + print ' If the domain is not running, create it' + print ' DOMAIN.XML must be a XML description of the domain' + print ' in libvirt\'s XML format' + +if len(sys.argv) != 2: + usage() + sys.exit(2) + +(name, xmldesc) = read_domain(sys.argv[1]) + +conn = libvirt.open(None) +if conn is None: + print 'Failed to open connection to the hypervisor' + sys.exit(1) + +try: + dom = conn.lookupByName(name) +except libvirt.libvirtError: + print "Starting domain %s ... " % name, + sys.stdout.flush() + dom = conn.createLinux(xmldesc, 0) + if dom is None: + print "failed" + sys.exit(1) + else: + print "done" |