2019-12-04 13:27:36 +03:00
#!/usr/bin/env python3
2020-07-28 08:05:19 +03:00
"""
Check that the domain described by DOMAIN . XML is running .
If the domain is not running , create it .
"""
2013-11-22 19:55:39 +04:00
import libvirt
import libxml2
2020-07-28 08:05:19 +03:00
from argparse import ArgumentParser
2020-04-27 12:10:13 +03:00
from typing import Tuple
2013-11-22 19:55:39 +04:00
2018-11-20 10:34:48 +03:00
2013-11-22 19:55:39 +04:00
# 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
2020-04-27 12:10:13 +03:00
def read_domain ( fname : str ) - > Tuple [ str , str ] :
2013-11-22 19:55:39 +04:00
fp = open ( fname , " r " )
xmldesc = fp . read ( )
fp . close ( )
doc = libxml2 . parseDoc ( xmldesc )
name = doc . xpathNewContext ( ) . xpathEval ( " /domain/name " ) [ 0 ] . content
return ( name , xmldesc )
2020-07-28 08:05:19 +03:00
parser = ArgumentParser ( description = __doc__ )
parser . add_argument ( " file " , metavar = " DOMAIN.XML " , help = " XML configuration of the domain in libvirt ' s XML format " )
args = parser . parse_args ( )
2013-11-22 19:55:39 +04:00
2024-08-05 20:36:35 +03:00
name , xmldesc = read_domain ( args . file )
2013-11-22 19:55:39 +04:00
2020-04-20 09:05:00 +03:00
try :
conn = libvirt . open ( None )
except libvirt . libvirtError :
2013-12-03 19:34:56 +04:00
print ( ' Failed to open connection to the hypervisor ' )
2020-07-28 08:15:55 +03:00
exit ( 1 )
2013-11-22 19:55:39 +04:00
try :
dom = conn . lookupByName ( name )
except libvirt . libvirtError :
2013-12-03 19:34:56 +04:00
print ( " Starting domain %s ... " % name )
2013-11-22 19:55:39 +04:00
dom = conn . createLinux ( xmldesc , 0 )
if dom is None :
2013-12-03 19:34:56 +04:00
print ( " failed " )
2020-07-28 08:15:55 +03:00
exit ( 1 )
2013-11-22 19:55:39 +04:00
else :
2013-12-03 19:34:56 +04:00
print ( " done " )