<s> from __future__ import unicode_literals <EOL> import hashlib <EOL> import six <EOL> from devops . helpers . xmlgenerator import XMLGenerator <EOL> class LibvirtXMLBuilder ( object ) : <EOL> NAME_SIZE = <NUM_LIT> <EOL> @ classmethod <EOL> def _crop_name ( cls , name ) : <EOL> if len ( name ) > cls . NAME_SIZE : <EOL> if isinstance ( name , six . string_types ) : <EOL> hash_str = hashlib . md5 ( name . encode ( '<STR_LIT:utf8>' ) ) . hexdigest ( ) <EOL> else : <EOL> hash_str = hashlib . md5 ( name ) . hexdigest ( ) <EOL> name = ( hash_str + name ) [ : cls . NAME_SIZE ] <EOL> return name <EOL> @ classmethod <EOL> def build_network_xml ( cls , network_name , bridge_name , addresses = None , <EOL> forward = None , ip_network_address = None , <EOL> ip_network_prefixlen = None , stp = True , <EOL> has_pxe_server = False , has_dhcp_server = False , <EOL> dhcp_range_start = None , dhcp_range_end = None , <EOL> tftp_root_dir = None ) : <EOL> """<STR_LIT>""" <EOL> if addresses is None : <EOL> addresses = [ ] <EOL> network_xml = XMLGenerator ( '<STR_LIT>' ) <EOL> network_xml . name ( cls . _crop_name ( network_name ) ) <EOL> network_xml . bridge ( <EOL> name = bridge_name , <EOL> stp = '<STR_LIT>' if stp else '<STR_LIT>' , <EOL> delay = '<STR_LIT:0>' ) <EOL> if forward : <EOL> network_xml . forward ( mode = forward ) <EOL> if ip_network_address is None : <EOL> return str ( network_xml ) <EOL> with network_xml . ip ( <EOL> address = ip_network_address , <EOL> prefix = ip_network_prefixlen ) : <EOL> if has_pxe_server and tftp_root_dir : <EOL> network_xml . tftp ( root = tftp_root_dir ) <EOL> if has_dhcp_server : <EOL> with network_xml . dhcp : <EOL> network_xml . range ( <EOL> start = dhcp_range_start , <EOL> end = dhcp_range_end ) <EOL> for address in addresses : <EOL> network_xml . host ( <EOL> mac = address [ '<STR_LIT>' ] , <EOL> ip = address [ '<STR_LIT>' ] , <EOL> name = address [ '<STR_LIT:name>' ] , <EOL> ) <EOL> if has_pxe_server : <EOL> network_xml . bootp ( file = '<STR_LIT>' ) <EOL> return str ( network_xml ) <EOL> @ classmethod <EOL> def build_volume_xml ( cls , name , capacity , format , backing_store_path , <EOL> backing_store_format ) : <EOL> """<STR_LIT>""" <EOL> volume_xml = XMLGenerator ( '<STR_LIT>' ) <EOL> volume_xml . name ( cls . _crop_name ( name ) ) <EOL> volume_xml . capacity ( str ( capacity ) ) <EOL> with volume_xml . target : <EOL> volume_xml . format ( type = format ) <EOL> volume_xml . permissions . mode ( "<STR_LIT>" ) <EOL> if backing_store_path : <EOL> with volume_xml . backingStore : <EOL> volume_xml . path ( backing_store_path ) <EOL> volume_xml . format ( type = backing_store_format ) <EOL> return str ( volume_xml ) <EOL> @ classmethod <EOL> def build_snapshot_xml ( cls , name = None , description = None , <EOL> external = False , disk_only = False , memory_file = '<STR_LIT>' , <EOL> domain_isactive = False , local_disk_devices = None ) : <EOL> """<STR_LIT>""" <EOL> xml_builder = XMLGenerator ( '<STR_LIT>' ) <EOL> if name is not None : <EOL> xml_builder . name ( name ) <EOL> if description is not None : <EOL> xml_builder . description ( description ) <EOL> if external : <EOL> if domain_isactive and not disk_only : <EOL> xml_builder . memory ( <EOL> file = memory_file , <EOL> snapshot = '<STR_LIT>' ) <EOL> else : <EOL> xml_builder . memory ( snapshot = '<STR_LIT>' ) <EOL> for disk in local_disk_devices or [ ] : <EOL> with xml_builder . disks : <EOL> xml_builder . disk ( name = disk [ '<STR_LIT>' ] , <EOL> file = disk [ '<STR_LIT>' ] , <EOL> snapshot = '<STR_LIT>' ) <EOL> return str ( xml_builder ) <EOL> @ classmethod <EOL> def _build_disk_device ( cls , device_xml , disk_type , disk_device , <EOL> disk_volume_format , disk_volume_path , disk_bus , <EOL> disk_target_dev , disk_serial ) : <EOL> """<STR_LIT>""" <EOL> with device_xml . disk ( type = disk_type , device = disk_device ) : <EOL> device_xml . driver ( type = disk_volume_format , cache = "<STR_LIT>" ) <EOL> device_xml . source ( file = disk_volume_path ) <EOL> if disk_bus == '<STR_LIT>' : <EOL> device_xml . target ( <EOL> dev = disk_target_dev , <EOL> bus = disk_bus , <EOL> removable = '<STR_LIT>' ) <EOL> device_xml . readonly ( ) <EOL> else : <EOL> device_xml . target ( <EOL> dev = disk_target_dev , <EOL> bus = disk_bus ) <EOL> device_xml . serial ( disk_serial ) <EOL> @ classmethod <EOL> def _build_interface_device ( cls , device_xml , interface_type , <EOL> interface_mac_address , interface_network_name , <EOL> interface_id , interface_model ) : <EOL> """<STR_LIT>""" <EOL> with device_xml . interface ( type = interface_type ) : <EOL> device_xml . mac ( address = interface_mac_address ) <EOL> device_xml . source ( <EOL> network = interface_network_name ) <EOL> device_xml . target ( dev = "<STR_LIT>" . format ( interface_id ) ) <EOL> if interface_model is not None : <EOL> device_xml . model ( type = interface_model ) <EOL> @ classmethod <EOL> def build_node_xml ( cls , name , hypervisor , use_host_cpu , vcpu , memory , <EOL> use_hugepages , hpet , os_type , architecture , boot , <EOL> reboot_timeout , bootmenu_timeout , emulator , <EOL> has_vnc , vnc_password , local_disk_devices , interfaces , <EOL> acpi , numa ) : <EOL> """<STR_LIT>""" <EOL> node_xml = XMLGenerator ( "<STR_LIT>" , type = hypervisor ) <EOL> node_xml . name ( cls . _crop_name ( name ) ) <EOL> if acpi : <EOL> with node_xml . features : <EOL> node_xml . acpi <EOL> cpu_args = { } <EOL> if use_host_cpu : <EOL> cpu_args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if numa : <EOL> with node_xml . cpu ( ** cpu_args ) : <EOL> with node_xml . numa : <EOL> for cell in numa : <EOL> node_xml . cell ( <EOL> cpus = str ( cell [ '<STR_LIT>' ] ) , <EOL> memory = str ( cell [ '<STR_LIT>' ] * <NUM_LIT> ) , <EOL> unit = '<STR_LIT>' , <EOL> ) <EOL> elif cpu_args : <EOL> node_xml . cpu ( ** cpu_args ) <EOL> node_xml . vcpu ( str ( vcpu ) ) <EOL> node_xml . memory ( str ( memory * <NUM_LIT> ) , unit = '<STR_LIT>' ) <EOL> if use_hugepages : <EOL> with node_xml . memoryBacking : <EOL> node_xml . hugepages <EOL> node_xml . clock ( offset = '<STR_LIT>' ) <EOL> with node_xml . clock . timer ( name = '<STR_LIT>' , <EOL> tickpolicy = '<STR_LIT>' , track = '<STR_LIT>' ) : <EOL> node_xml . catchup ( <EOL> threshold = '<STR_LIT>' , <EOL> slew = '<STR_LIT>' , <EOL> limit = '<STR_LIT>' ) <EOL> node_xml . clock . timer ( <EOL> name = '<STR_LIT>' , <EOL> tickpolicy = '<STR_LIT>' ) <EOL> node_xml . clock . timer ( <EOL> name = '<STR_LIT>' , <EOL> present = '<STR_LIT:yes>' if hpet else '<STR_LIT>' ) <EOL> with node_xml . os : <EOL> node_xml . type ( os_type , arch = architecture ) <EOL> for boot_dev in boot : <EOL> node_xml . boot ( dev = boot_dev ) <EOL> if reboot_timeout : <EOL> node_xml . bios ( rebootTimeout = str ( reboot_timeout ) ) <EOL> if bootmenu_timeout : <EOL> node_xml . bootmenu ( enable = '<STR_LIT:yes>' , timeout = str ( bootmenu_timeout ) ) <EOL> with node_xml . devices : <EOL> node_xml . controller ( type = '<STR_LIT>' , model = '<STR_LIT>' ) <EOL> node_xml . emulator ( emulator ) <EOL> if has_vnc : <EOL> if vnc_password : <EOL> node_xml . graphics ( <EOL> type = '<STR_LIT>' , <EOL> listen = '<STR_LIT>' , <EOL> autoport = '<STR_LIT:yes>' , <EOL> passwd = vnc_password ) <EOL> else : <EOL> node_xml . graphics ( <EOL> type = '<STR_LIT>' , <EOL> listen = '<STR_LIT>' , <EOL> autoport = '<STR_LIT:yes>' ) <EOL> for disk_device in local_disk_devices : <EOL> cls . _build_disk_device ( node_xml , ** disk_device ) <EOL> for interface in interfaces : <EOL> cls . _build_interface_device ( node_xml , ** interface ) <EOL> with node_xml . video : <EOL> node_xml . model ( type = '<STR_LIT>' , vram = '<STR_LIT>' , heads = '<STR_LIT:1>' ) <EOL> with node_xml . serial ( type = '<STR_LIT>' ) : <EOL> node_xml . target ( port = '<STR_LIT:0>' ) <EOL> with node_xml . console ( type = '<STR_LIT>' ) : <EOL> node_xml . target ( type = '<STR_LIT>' , port = '<STR_LIT:0>' ) <EOL> return str ( node_xml ) <EOL> @ classmethod <EOL> def build_iface_xml ( cls , name , ip = None , prefix = None , vlanid = None ) : <EOL> """<STR_LIT>""" <EOL> if vlanid : <EOL> iface_type = '<STR_LIT>' <EOL> iface_name = "<STR_LIT>" . format ( name , str ( vlanid ) ) <EOL> else : <EOL> iface_type = '<STR_LIT>' <EOL> iface_name = "<STR_LIT>" . format ( name ) <EOL> interface_xml = XMLGenerator ( '<STR_LIT>' , <EOL> type = iface_type , <EOL> name = iface_name ) <EOL> interface_xml . start ( mode = "<STR_LIT>" ) <EOL> if vlanid : <EOL> with interface_xml . vlan ( tag = str ( vlanid ) ) : <EOL> interface_xml . interface ( name = name ) <EOL> if ( ip is not None ) and ( prefix is not None ) : <EOL> with interface_xml . protocol ( family = '<STR_LIT>' ) : <EOL> interface_xml . ip ( address = ip , prefix = prefix ) <EOL> return str ( interface_xml ) </s>
