<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import libvirt <EOL> import xml . etree . ElementTree as etree <EOL> from see . context . resources import network <EOL> from see . context . resources import resources <EOL> from see . context . resources . helpers import subelement <EOL> def mountpoint ( mount , identifier ) : <EOL> source_path = os . path . join ( mount [ '<STR_LIT>' ] , identifier ) <EOL> os . makedirs ( source_path ) <EOL> return ( source_path , mount [ '<STR_LIT>' ] ) <EOL> def domain_xml ( identifier , xml , mounts , network_name = None ) : <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> for mount in mounts : <EOL> filesystem = etree . SubElement ( devices , '<STR_LIT>' , type = '<STR_LIT>' ) <EOL> etree . SubElement ( filesystem , '<STR_LIT:source>' , dir = mount [ <NUM_LIT:0> ] ) <EOL> etree . SubElement ( filesystem , '<STR_LIT:target>' , dir = mount [ <NUM_LIT:1> ] ) <EOL> if network_name is not None : <EOL> network = subelement ( devices , '<STR_LIT>' , '<STR_LIT>' , None , type = '<STR_LIT>' ) <EOL> subelement ( network , '<STR_LIT>' , '<STR_LIT:source>' , None , network = network_name ) <EOL> return etree . tostring ( domain ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def domain_create ( hypervisor , identifier , configuration , network_name = None ) : <EOL> """<STR_LIT>""" <EOL> mounts = [ ] <EOL> with open ( configuration [ '<STR_LIT>' ] ) as config_file : <EOL> domain_config = config_file . read ( ) <EOL> if '<STR_LIT>' in configuration : <EOL> if isinstance ( configuration [ '<STR_LIT>' ] , ( list , tuple ) ) : <EOL> for mount in configuration [ '<STR_LIT>' ] : <EOL> mounts . append ( mountpoint ( mount , identifier ) ) <EOL> else : <EOL> mounts . append ( mountpoint ( configuration [ '<STR_LIT>' ] , identifier ) ) <EOL> xml_config = domain_xml ( identifier , domain_config , tuple ( mounts ) , network_name = network_name ) <EOL> return hypervisor . defineXML ( xml_config ) <EOL> def domain_delete ( domain , logger , filesystem ) : <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> logger . exception ( "<STR_LIT>" ) <EOL> try : <EOL> if filesystem is not None and os . path . exists ( filesystem ) : <EOL> shutil . rmtree ( filesystem ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> class LXCResources ( resources . Resources ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , identifier , configuration ) : <EOL> super ( LXCResources , self ) . __init__ ( identifier , configuration ) <EOL> self . _domain = None <EOL> self . _network = None <EOL> self . _hypervisor = 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> @ property <EOL> def network ( self ) : <EOL> return self . _network <EOL> def _initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> network_name = None <EOL> url = '<STR_LIT>' in self . configuration and self . configuration [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> self . _hypervisor = libvirt . open ( url ) <EOL> if '<STR_LIT>' in self . configuration : <EOL> self . _network = network . create ( self . _hypervisor , self . identifier , self . configuration [ '<STR_LIT>' ] ) <EOL> network_name = self . _network . name ( ) <EOL> self . _domain = domain_create ( self . _hypervisor , self . identifier , self . configuration [ '<STR_LIT>' ] , <EOL> network_name = network_name ) <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> filesystem = None <EOL> if self . _domain is not None : <EOL> if '<STR_LIT>' in self . configuration : <EOL> filesystem = os . path . join ( self . configuration [ '<STR_LIT>' ] [ '<STR_LIT>' ] , self . identifier ) <EOL> domain_delete ( self . _domain , self . logger , filesystem ) <EOL> if self . _network is not None : <EOL> try : <EOL> network . delete ( self . _network ) <EOL> except Exception : <EOL> self . logger . exception ( "<STR_LIT>" ) <EOL> if self . _hypervisor is not None : <EOL> try : <EOL> self . _hypervisor . close ( ) <EOL> except Exception : <EOL> self . logger . exception ( "<STR_LIT>" ) </s>
