<s> """<STR_LIT>""" <EOL> import libvirt <EOL> import xml . etree . ElementTree as etree <EOL> from see . context . resources import resources <EOL> from see . context . resources . helpers import subelement <EOL> def domain_xml ( identifier , xml , disk_path ) : <EOL> """<STR_LIT>""" <EOL> domain = etree . fromstring ( xml ) <EOL> subelement ( domain , '<STR_LIT>' , '<STR_LIT:name>' , identifier ) <EOL> subelement ( domain , '<STR_LIT>' , '<STR_LIT>' , identifier ) <EOL> devices = subelement ( domain , '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> disk = subelement ( devices , '<STR_LIT>' , '<STR_LIT>' , None , type = '<STR_LIT:file>' , device = '<STR_LIT>' ) <EOL> subelement ( disk , '<STR_LIT>' , '<STR_LIT:source>' , None , file = disk_path ) <EOL> return etree . tostring ( domain ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def domain_create ( hypervisor , identifier , configuration , disk_path ) : <EOL> """<STR_LIT>""" <EOL> with open ( configuration [ '<STR_LIT>' ] ) as config_file : <EOL> domain_config = config_file . read ( ) <EOL> xml = domain_xml ( identifier , domain_config , disk_path ) <EOL> return hypervisor . defineXML ( xml ) <EOL> def domain_delete ( domain , logger ) : <EOL> """<STR_LIT>""" <EOL> if domain is not None : <EOL> try : <EOL> if domain . isActive ( ) : <EOL> domain . destroy ( ) <EOL> except libvirt . libvirtError : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> try : <EOL> domain . undefine ( ) <EOL> except libvirt . libvirtError : <EOL> try : <EOL> domain . undefineFlags ( libvirt . VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA ) <EOL> except libvirt . libvirtError : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> class VBoxResources ( resources . Resources ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , identifier , configuration ) : <EOL> super ( VBoxResources , self ) . __init__ ( identifier , configuration ) <EOL> self . _domain = None <EOL> self . _initialize ( ) <EOL> @ property <EOL> def hypervisor ( self ) : <EOL> return self . _hypervisor <EOL> @ property <EOL> def domain ( self ) : <EOL> return self . _domain <EOL> def _initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> disk_path = self . configuration [ '<STR_LIT>' ] [ '<STR_LIT:image>' ] <EOL> url = '<STR_LIT>' in self . configuration and self . configuration [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> self . _hypervisor = libvirt . open ( url ) <EOL> self . _domain = domain_create ( self . _hypervisor , self . identifier , self . configuration [ '<STR_LIT>' ] , <EOL> disk_path ) <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _domain is not None : <EOL> domain_delete ( self . _domain , self . logger ) <EOL> if self . _hypervisor is not None : <EOL> try : <EOL> self . _hypervisor . close ( ) <EOL> except Exception : <EOL> self . logger . exception ( "<STR_LIT>" ) </s>
