Add missing impl of virNetworkListAllPorts
[libvirt-python/ericb.git] / examples / domstart.py
blobce1b60ccbadbdb37726dc1de7e8b09e21ee05871
1 #!/usr/bin/env python
2 # domstart - make sure a given domU is running, if not start it
4 import libvirt
5 import sys
6 import os
7 import libxml2
8 import pdb
10 # Parse the XML description of domU from FNAME
11 # and return a tuple (name, xmldesc) where NAME
12 # is the name of the domain, and xmldesc is the contetn of FNAME
13 def read_domain(fname):
14 fp = open(fname, "r")
15 xmldesc = fp.read()
16 fp.close()
18 doc = libxml2.parseDoc(xmldesc)
19 name = doc.xpathNewContext().xpathEval("/domain/name")[0].content
20 return (name, xmldesc)
22 def usage():
23 print('Usage: %s domain.xml' % sys.argv[0])
24 print(' Check that the domain described by DOMAIN.XML is running')
25 print(' If the domain is not running, create it')
26 print(' DOMAIN.XML must be a XML description of the domain')
27 print(' in libvirt\'s XML format')
29 if len(sys.argv) != 2:
30 usage()
31 sys.exit(2)
33 (name, xmldesc) = read_domain(sys.argv[1])
35 conn = libvirt.open(None)
36 if conn is None:
37 print('Failed to open connection to the hypervisor')
38 sys.exit(1)
40 try:
41 dom = conn.lookupByName(name)
42 except libvirt.libvirtError:
43 print("Starting domain %s ... " % name)
44 dom = conn.createLinux(xmldesc, 0)
45 if dom is None:
46 print("failed")
47 sys.exit(1)
48 else:
49 print("done")