def domain_xml_to_test(domain_id, ssl=True): id = domain_id # INFO TO DEVELOPER, QUE DE UN ERROR SI EL ID NO EXISTE dict_domain = get_domain(domain_id) xml = dict_domain['xml'] x = DomainXML(xml) if ssl is True: x.reset_viewer_passwd() else: x.spice_remove_passwd_nossl() x.remove_selinux_options() x.remove_boot_order_from_disks() xml = x.return_xml() log.debug('#####################################################3') log.debug('#####################################################3') log.debug('#####################################################3') log.debug('#####################################################3') log.debug(xml) log.debug('#####################################################3') log.debug('#####################################################3') log.debug('#####################################################3') log.debug('#####################################################3') x.dict_from_xml() update_domain_dict_hardware(id, x.vm_dict, xml=xml) # update_domain_viewer_started_values(id,passwd=x.viewer_passwd) return xml
def recreate_xml_to_start(id, ssl=True): dict_domain = get_domain(id) xml = dict_domain['xml'] x = DomainXML(xml) ##### actions to customize xml # spice password if ssl is True: x.reset_viewer_passwd() else: # only for test purposes, not use in production x.spice_remove_passwd_nossl() # redo network try: list_interfaces = dict_domain['create_dict']['hardware']['interfaces'] except KeyError: list_interfaces = [] log.info('domain {} withouth key interfaces in crate_dict'.format(id)) mac_address = [] for interface_index in range(len(x.vm_dict['interfaces'])): mac_address.append(x.vm_dict['interfaces'][interface_index]['mac']) x.remove_interface(order=interface_index) interface_index = 0 for id_interface in list_interfaces: d_interface = get_interface(id_interface) x.add_interface(type=d_interface['kind'], id=d_interface['ifname'], model_type=d_interface['model'], mac=mac_address[interface_index]) interface_index += 1 x.remove_selinux_options() #remove boot order in disk definition that conflict with /os/boot order in xml x.remove_boot_order_from_disks() x.dict_from_xml() # INFO TO DEVELOPER, OJO, PORQUE AQUI SE PIERDE EL BACKING CHAIN?? update_domain_dict_hardware(id, x.vm_dict, xml=xml) if 'viewer_passwd' in x.__dict__.keys(): update_domain_viewer_started_values(id, passwd=x.viewer_passwd) log.debug("updated viewer password {} in domain {}".format(x.viewer_passwd, id)) xml = x.return_xml() # log.debug('#####################################################') # log.debug(xml) # log.debug('#####################################################') return xml
def update_xml_from_dict_domain(id_domain, xml=None): d = get_domain(id_domain) hw = d['hardware'] if xml is None: v = DomainXML(d['xml']) else: v = DomainXML(xml) if v.parser is False: return False # v.set_memory(memory=hw['currentMemory'],unit=hw['currentMemory_unit']) v.set_memory(memory=hw['memory'], unit=hw['memory_unit'], current=int( hw.get('currentMemory', hw['memory']) * DEFAULT_BALLOON)) total_disks_in_xml = len( v.tree.xpath('/domain/devices/disk[@device="disk"]')) total_cdroms_in_xml = len( v.tree.xpath('/domain/devices/disk[@device="cdrom"]')) total_floppies_in_xml = len( v.tree.xpath('/domain/devices/disk[@device="floppy"]')) if 'boot_order' in hw.keys(): if 'boot_menu_enable' in hw.keys(): v.update_boot_order(hw['boot_order'], hw['boot_menu_enable']) else: v.update_boot_order(hw['boot_order']) if 'disks' in hw.keys(): num_remove_disks = total_disks_in_xml - len(hw['disks']) if num_remove_disks > 0: for i in range(num_remove_disks): v.remove_disk() for i in range(len(hw['disks'])): s = hw['disks'][i]['file'] if s[s.rfind('.'):].lower().find('qcow') == 1: type_disk = 'qcow2' else: type_disk = 'raw' if i >= total_disks_in_xml: force_bus = hw['disks'][i].get('bus', False) if force_bus is False: force_bus = 'virtio' v.add_disk(index=i, path_disk=hw['disks'][i]['file'], type_disk=type_disk, bus=force_bus) else: force_bus = hw['disks'][i].get('bus', False) if force_bus is False: v.set_vdisk(hw['disks'][i]['file'], index=i, type_disk=type_disk) else: v.set_vdisk(hw['disks'][i]['file'], index=i, type_disk=type_disk, force_bus=force_bus) elif total_disks_in_xml > 0: for i in range(total_disks_in_xml): v.remove_disk() if 'isos' in hw.keys(): num_remove_cdroms = total_cdroms_in_xml - len(hw['isos']) if num_remove_cdroms > 0: for i in range(num_remove_cdroms): v.remove_cdrom() for i in range(len(hw['isos'])): if i >= total_cdroms_in_xml: v.add_cdrom(index=i, path_cdrom=hw['isos'][i]['path']) else: v.set_cdrom(hw['isos'][i]['path'], index=i) elif total_cdroms_in_xml > 0: for i in range(total_cdroms_in_xml): v.remove_cdrom() if 'floppies' in hw.keys(): num_remove_floppies = total_floppies_in_xml - len(hw['floppies']) if num_remove_floppies > 0: for i in range(num_remove_floppies): v.remove_floppy() for i in range(len(hw['floppies'])): if i >= total_floppies_in_xml: v.add_floppy(index=i, path_floppy=hw['floppies'][i]['path']) else: v.set_floppy(hw['floppies'][i]['path'], index=i) elif total_floppies_in_xml > 0: for i in range(total_floppies_in_xml): v.remove_floppy() v.set_name(id_domain) # INFO TO DEVELOPER, deberíamos poder usar la funcion v.set_description(para poner algo) # INFO TO DEVELOPER, estaría bien guardar la plantilla de la que deriva en algún campo de rethink, # la puedo sacar a partir de hw['name'], hay que decidir como le llamamos al campo # es importante para poder filtrar y saber cuantas máquinas han derivado, # aunque la información que vale de verdad es la backing-chain del disco template_name = hw['name'] # TODO FALTA AÑADIR RED, Y PUEDE HABER MÁS DE UNA, # SE PUEDE HACER UN REMOVE DE TODAS LAS REDES Y LUEGO AÑADIR # CON v.add_interface() for interface_index in range(len(v.vm_dict['interfaces'])): v.remove_interface(order=interface_index) if 'interfaces' in hw.keys(): for d_interface in hw['interfaces']: v.add_interface(type=d_interface['type'], id=d_interface['id'], model_type=d_interface['model']) v.set_vcpu(hw['vcpus']) v.set_graphics_type(hw['graphics']['type']) v.set_video_type(hw['video']['type']) # INFO TO DEVELOPER, falta hacer un v.set_network_id (para ver contra que red hace bridge o se conecta # INFO TO DEVELOPER, falta hacer un v.set_netowk_type (para seleccionar si quiere virtio o realtek por ejemplo) v.randomize_vm() v.remove_selinux_options() v.remove_boot_order_from_disks() # v.print_xml() xml_raw = v.return_xml() hw_updated = v.dict_from_xml() update_domain_dict_hardware(id_domain, hw_updated, xml=xml_raw) return xml_raw
def creating_disks_from_template(self, id_new): dict_domain = get_domain(id_new) if 'create_dict' in dict_domain.keys(): dict_to_create = dict_domain['create_dict'] pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] # INFO TO DEVELOPER DEBERÍA SER UN FOR PARA CADA DISCO # y si el disco no tiene backing_chain, crear un disco vacío # del tamño que marcase # d['hardware']['disks'][0]['size'] # el backing_file debería estar asociado a cada disco: # d['hardware']['disks'][0]['backing_file'] for index_disk in range(len(dict_to_create['hardware']['disks'])): relative_path = dict_to_create['hardware']['disks'][index_disk][ 'file'] new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN dict_to_create['hardware']['disks'][index_disk]['file'] = new_file dict_to_create['hardware']['disks'][index_disk][ 'path_selected'] = path_selected update_table_field('domains', id_new, 'create_dict', dict_to_create) #TODO: REVISAR SI RELAMENTE ES NECESARIO o esta acción responde a versiones antiguas de nuestras funciones de creación hardware_update = {} hardware_update['disks'] = dict_to_create['hardware']['disks'] update_domain_dict_hardware(id_new, hardware_update) ################## for index_disk in range(len(dict_to_create['hardware']['disks'])): backing_file = dict_to_create['hardware']['disks'][index_disk][ 'parent'] new_file = dict_to_create['hardware']['disks'][index_disk]['file'] path_selected = dict_to_create['hardware']['disks'][index_disk][ 'path_selected'] hyp_to_disk_create = get_host_disk_operations_from_path( path_selected, pool=pool_id, type_path='groups') cmds = create_cmds_disk_from_base(path_base=backing_file, path_new=new_file) log.debug( 'commands to disk create to launch in disk_operations: \n{}'. format('\n'.join(cmds))) action = {} action['type'] = 'create_disk' action['disk_path'] = new_file action['index_disk'] = index_disk action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status( status='CreatingDisk', id_domain=id_new, hyp_id=False, detail= 'Creating disk operation is launched in hypervisor {} ({} operations in queue)' .format( hyp_to_disk_create, self.manager. q_disk_operations[hyp_to_disk_create].qsize())) self.manager.q_disk_operations[hyp_to_disk_create].put(action) except Exception as e: update_domain_status( status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail= 'Creating disk operation failed when insert action in queue for disk operations' ) log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}' .format(e))
def creating_disk_from_virtbuilder(self, id_new): dict_domain = get_domain(id_new) pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] dict_to_create = dict_domain['create_dict'] relative_path = dict_to_create['hardware']['disks'][0]['file'] path_new_disk, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN dict_to_create['hardware']['disks'][0]['file'] = path_new_disk dict_to_create['hardware']['disks'][0]['path_selected'] = path_selected size_str = dict_to_create['hardware']['disks'][0]['size'] memory_in_mb = int(dict_to_create['hardware']['memory'] / 1024) options_virt_builder = dict_to_create['builder']['options'] options_virt_install = dict_to_create['install']['options'] id_domains_virt_builder = dict_to_create['builder']['id'] id_os_virt_install = dict_to_create['install']['id'] # UPDATE HARDWARE DICT hardware_update = {} hardware_update['disks'] = dict_to_create['hardware']['disks'] update_domain_dict_hardware(id_new, hardware_update) hyp_to_disk_create = get_host_long_operations_from_path( path_selected, pool=pool_id, type_path='groups') cmds = create_cmd_disk_from_virtbuilder( path_new_qcow=path_new_disk, os_version=id_domains_virt_builder, id_os_virt_install=id_os_virt_install, name_domain_in_xml=id_new, size_str=size_str, memory_in_mb=memory_in_mb, options_cmd=options_virt_builder) # cmds = [{'cmd':'ls -lah > /tmp/prova.txt','title':'es un ls'}] action = {} action['type'] = 'create_disk_virt_builder' action['disk_path'] = path_new_disk action['index_disk'] = 0 action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status( status='RunningVirtBuilder', id_domain=id_new, hyp_id=False, detail= 'Creating virt-builder image operation is launched in hypervisor {} ({} operations in queue)' .format( hyp_to_disk_create, self.manager. q_long_operations[hyp_to_disk_create].qsize())) self.manager.q_long_operations[hyp_to_disk_create].put(action) except Exception as e: update_domain_status( status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail= 'Creating disk operation failed when insert action in queue for disk operations' ) log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}' .format(e))
def creating_disk_from_scratch(self, id_new): dict_domain = get_domain(id_new) pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] dict_to_create = dict_domain['create_dict'] if 'disks' in dict_to_create['hardware'].keys(): if len(dict_to_create['hardware']['disks']) > 0: # for index_disk in range(len(dict_to_create['hardware']['disks'])): # relative_path = dict_to_create['hardware']['disks'][index_disk]['file'] # path_new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id) # # UPDATE PATH IN DOMAIN # dict_to_create['hardware']['disks'][index_disk]['file'] = new_file # dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected relative_path = dict_to_create['hardware']['disks'][0]['file'] path_new_disk, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN d_update_domain = {'hardware': {'disks': [{}]}} d_update_domain['hardware']['disks'][0]['file'] = path_new_disk d_update_domain['hardware']['disks'][0][ 'path_selected'] = path_selected update_domain_dict_hardware(id_new, d_update_domain) update_domain_dict_create_dict(id_new, d_update_domain) size_str = dict_to_create['hardware']['disks'][0]['size'] hyp_to_disk_create = get_host_disk_operations_from_path( path_selected, pool=pool_id, type_path='groups') cmds = create_cmd_disk_from_scratch( path_new_disk=path_new_disk, size_str=size_str) action = {} action['type'] = 'create_disk_from_scratch' action['disk_path'] = path_new_disk action['index_disk'] = 0 action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status( status='CreatingDiskFromScratch', id_domain=id_new, hyp_id=False, detail= 'Creating disk commands are launched in hypervisor {} ({} operations in queue)' .format( hyp_to_disk_create, self.manager. q_disk_operations[hyp_to_disk_create].qsize())) self.manager.q_disk_operations[hyp_to_disk_create].put( action) except Exception as e: update_domain_status( status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail= 'Creating disk operation failed when insert action in queue for disk operations' ) log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}' .format(e))
def creating_disk_from_scratch(self, id_new): dict_domain = get_domain(id_new) pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] dict_to_create = dict_domain['create_dict'] if 'disks' in dict_to_create['hardware'].keys(): if len(dict_to_create['hardware']['disks']) > 0: # for index_disk in range(len(dict_to_create['hardware']['disks'])): # relative_path = dict_to_create['hardware']['disks'][index_disk]['file'] # path_new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id) # # UPDATE PATH IN DOMAIN # dict_to_create['hardware']['disks'][index_disk]['file'] = new_file # dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected relative_path = dict_to_create['hardware']['disks'][0]['file'] path_new_disk, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN d_update_domain = {'hardware': {'disks': [{}]}} if len(dict_to_create['hardware']['disks']) > 0: ## supplementary disks for i, dict_other_disk in enumerate( dict_to_create['hardware']['disks'][1:]): path_other_disk, path_other_disk_selected = get_path_to_disk( dict_other_disk['file'], pool=pool_id, type_path=dict_other_disk['type_path']) d_update_domain['hardware']['disks'].append({}) d_update_domain['hardware']['disks'][ i + 1]['file'] = path_other_disk d_update_domain['hardware']['disks'][ i + 1]['path_selected'] = path_other_disk_selected d_update_domain['hardware']['disks'][ i + 1]['bus'] = dict_other_disk.get( 'bus', 'virtio') if dict_other_disk.get('readonly', True) is True: d_update_domain['hardware']['disks'][ i + 1]['readonly'] = True else: pass # TODO # update_media_write_access_by_domain(id_media,id_domain) d_update_domain['hardware']['disks'][0]['file'] = path_new_disk d_update_domain['hardware']['disks'][0][ 'path_selected'] = path_selected d_update_domain['hardware']['disks'][0][ 'size'] = dict_to_create['hardware']['disks'][0]['size'] if 'bus' in dict_to_create['hardware']['disks'][0].keys(): if dict_to_create['hardware']['disks'][0][ 'bus'] in BUS_TYPES: d_update_domain['hardware']['disks'][0][ 'bus'] = dict_to_create['hardware']['disks'][0][ 'bus'] update_domain_dict_hardware(id_new, d_update_domain) update_domain_dict_create_dict(id_new, d_update_domain) size_str = dict_to_create['hardware']['disks'][0]['size'] hyp_to_disk_create = get_host_disk_operations_from_path( path_selected, pool=pool_id, type_path='groups') cmds = create_cmd_disk_from_scratch( path_new_disk=path_new_disk, size_str=size_str) action = {} action['type'] = 'create_disk_from_scratch' action['disk_path'] = path_new_disk action['index_disk'] = 0 action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status( status='CreatingDiskFromScratch', id_domain=id_new, hyp_id=False, detail= 'Creating disk commands are launched in hypervisor {} ({} operations in queue)' .format( hyp_to_disk_create, self.manager. q_disk_operations[hyp_to_disk_create].qsize())) self.manager.q_disk_operations[hyp_to_disk_create].put( action) except Exception as e: update_domain_status( status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail= 'Creating disk operation failed when insert action in queue for disk operations' ) log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}' .format(e)) else: update_domain_status(status='CreatingDomain', id_domain=id_new, hyp_id=False, detail='Creating domain withouth disks')
def update_xml_from_dict_domain(id_domain, xml=None): d = get_domain(id_domain) hw = d['hardware'] if xml is None: v = DomainXML(d['xml']) else: v = DomainXML(xml) if v.parser is False: return False # v.set_memory(memory=hw['currentMemory'],unit=hw['currentMemory_unit']) v.set_memory(memory=hw['memory'], unit=hw['memory_unit'],current=int(hw.get('currentMemory',hw['memory'])*DEFAULT_BALLOON)) total_disks_in_xml = len(v.tree.xpath('/domain/devices/disk[@device="disk"]')) total_cdroms_in_xml = len(v.tree.xpath('/domain/devices/disk[@device="cdrom"]')) total_floppies_in_xml = len(v.tree.xpath('/domain/devices/disk[@device="floppy"]')) if 'boot_order' in hw.keys(): if 'boot_menu_enable' in hw.keys(): v.update_boot_order(hw['boot_order'], hw['boot_menu_enable']) else: v.update_boot_order(hw['boot_order']) if 'disks' in hw.keys(): num_remove_disks = total_disks_in_xml - len(hw['disks']) if num_remove_disks > 0: for i in range(num_remove_disks): v.remove_disk() for i in range(len(hw['disks'])): s=hw['disks'][i]['file'] if s[s.rfind('.'):].lower().find('qcow') == 1: type_disk = 'qcow2' else: type_disk = 'raw' if i >= total_disks_in_xml: force_bus = hw['disks'][i].get('bus', False) if force_bus is False: force_bus = 'virtio' v.add_disk(index=i,path_disk=hw['disks'][i]['file'],type_disk=type_disk,bus=force_bus) else: force_bus = hw['disks'][i].get('bus', False) if force_bus is False: v.set_vdisk(hw['disks'][i]['file'], index=i,type_disk=type_disk) else: v.set_vdisk(hw['disks'][i]['file'], index=i, type_disk=type_disk, force_bus=force_bus) elif total_disks_in_xml > 0: for i in range(total_disks_in_xml): v.remove_disk() if 'isos' in hw.keys(): num_remove_cdroms = total_cdroms_in_xml - len(hw['isos']) if num_remove_cdroms > 0: for i in range(num_remove_cdroms): v.remove_cdrom() for i in range(len(hw['isos'])): if i >= total_cdroms_in_xml: v.add_cdrom(index=i, path_cdrom=hw['isos'][i]['path']) else: v.set_cdrom(hw['isos'][i]['path'], index=i) elif total_cdroms_in_xml > 0: for i in range(total_cdroms_in_xml): v.remove_cdrom() if 'floppies' in hw.keys(): num_remove_floppies = total_floppies_in_xml - len(hw['floppies']) if num_remove_floppies > 0: for i in range(num_remove_floppies): v.remove_floppy() for i in range(len(hw['floppies'])): if i >= total_floppies_in_xml: v.add_floppy(index=i, path_floppy=hw['floppies'][i]['path']) else: v.set_floppy(hw['floppies'][i]['path'], index=i) elif total_floppies_in_xml > 0: for i in range(total_floppies_in_xml): v.remove_floppy() v.set_name(id_domain) # INFO TO DEVELOPER, deberíamos poder usar la funcion v.set_description(para poner algo) # INFO TO DEVELOPER, estaría bien guardar la plantilla de la que deriva en algún campo de rethink, # la puedo sacar a partir de hw['name'], hay que decidir como le llamamos al campo # es importante para poder filtrar y saber cuantas máquinas han derivado, # aunque la información que vale de verdad es la backing-chain del disco template_name = hw['name'] # TODO FALTA AÑADIR RED, Y PUEDE HABER MÁS DE UNA, # SE PUEDE HACER UN REMOVE DE TODAS LAS REDES Y LUEGO AÑADIR # CON v.add_interface() for interface_index in range(len(v.vm_dict['interfaces'])): v.remove_interface(order=interface_index) if 'interfaces' in hw.keys(): for d_interface in hw['interfaces']: v.add_interface(type=d_interface['type'], id=d_interface['id'], model_type=d_interface['model']) v.set_vcpu(hw['vcpus']) v.set_graphics_type(hw['graphics']['type']) v.set_video_type(hw['video']['type']) # INFO TO DEVELOPER, falta hacer un v.set_network_id (para ver contra que red hace bridge o se conecta # INFO TO DEVELOPER, falta hacer un v.set_netowk_type (para seleccionar si quiere virtio o realtek por ejemplo) v.randomize_vm() v.remove_selinux_options() v.remove_boot_order_from_disks() # v.print_xml() xml_raw = v.return_xml() hw_updated = v.dict_from_xml() update_domain_dict_hardware(id_domain, hw_updated, xml=xml_raw) return xml_raw
def recreate_xml_to_start(id, ssl=True, cpu_host_model=False): dict_domain = get_domain(id) xml = dict_domain['xml'] x = DomainXML(xml) if x.parser is False: #error when parsing xml return False ##### actions to customize xml if dict_domain.get('not_change_cpu_section',False) is False: x.set_cpu_host_model(cpu_host_model) # spice password x.add_spice_graphics_if_not_exist() if ssl is True: #recreate random password in x.viewer_passwd x.reset_viewer_passwd() else: # only for test purposes, not use in production x.spice_remove_passwd_nossl() #add vlc access x.add_vlc_with_websockets() # redo network try: list_interfaces = dict_domain['create_dict']['hardware']['interfaces'] except KeyError: list_interfaces = [] log.info('domain {} withouth key interfaces in create_dict'.format(id)) mac_address = [] for interface_index in range(len(x.vm_dict['interfaces'])): mac_address.append(x.vm_dict['interfaces'][interface_index]['mac']) x.remove_interface(order=interface_index) interface_index = 0 for id_interface in list_interfaces: d_interface = get_interface(id_interface) x.add_interface(type=d_interface['kind'], id=d_interface['ifname'], model_type=d_interface['model'], mac=mac_address[interface_index]) interface_index += 1 x.remove_selinux_options() #remove boot order in disk definition that conflict with /os/boot order in xml x.remove_boot_order_from_disks() x.dict_from_xml() # INFO TO DEVELOPER, OJO, PORQUE AQUI SE PIERDE EL BACKING CHAIN?? update_domain_dict_hardware(id, x.vm_dict, xml=xml) if 'viewer_passwd' in x.__dict__.keys(): #update password in database update_domain_viewer_started_values(id, passwd=x.viewer_passwd) log.debug("updated viewer password {} in domain {}".format(x.viewer_passwd, id)) xml = x.return_xml() # log.debug('#####################################################') # log.debug(xml) # log.debug('#####################################################') return xml
def creating_disks_from_template(self, id_new): dict_domain = get_domain(id_new) if 'create_dict' in dict_domain.keys(): dict_to_create = dict_domain['create_dict'] pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] # INFO TO DEVELOPER DEBERÍA SER UN FOR PARA CADA DISCO # y si el disco no tiene backing_chain, crear un disco vacío # del tamño que marcase # d['hardware']['disks'][0]['size'] # el backing_file debería estar asociado a cada disco: # d['hardware']['disks'][0]['backing_file'] for index_disk in range(len(dict_to_create['hardware']['disks'])): relative_path = dict_to_create['hardware']['disks'][index_disk]['file'] new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN dict_to_create['hardware']['disks'][index_disk]['file'] = new_file dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected update_table_field('domains',id_new,'create_dict',dict_to_create) #TODO: REVISAR SI RELAMENTE ES NECESARIO o esta acción responde a versiones antiguas de nuestras funciones de creación hardware_update = {} hardware_update['disks'] = dict_to_create['hardware']['disks'] update_domain_dict_hardware(id_new, hardware_update) ################## for index_disk in range(len(dict_to_create['hardware']['disks'])): backing_file = dict_to_create['hardware']['disks'][index_disk]['parent'] new_file = dict_to_create['hardware']['disks'][index_disk]['file'] path_selected = dict_to_create['hardware']['disks'][index_disk]['path_selected'] hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=pool_id, type_path='groups') cmds = create_cmds_disk_from_base(path_base=backing_file, path_new=new_file) log.debug('commands to disk create to launch in disk_operations: \n{}'.format('\n'.join(cmds))) action = {} action['type'] = 'create_disk' action['disk_path'] = new_file action['index_disk'] = index_disk action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status(status='CreatingDisk', id_domain=id_new, hyp_id=False, detail='Creating disk operation is launched in hypervisor {} ({} operations in queue)'.format( hyp_to_disk_create, self.manager.q_disk_operations[hyp_to_disk_create].qsize())) self.manager.q_disk_operations[hyp_to_disk_create].put(action) except Exception as e: update_domain_status(status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail='Creating disk operation failed when insert action in queue for disk operations') log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'.format( e))
def creating_disk_from_virtbuilder(self, id_new): dict_domain = get_domain(id_new) pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] dict_to_create = dict_domain['create_dict'] relative_path = dict_to_create['hardware']['disks'][0]['file'] path_new_disk, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN dict_to_create['hardware']['disks'][0]['file'] = path_new_disk dict_to_create['hardware']['disks'][0]['path_selected'] = path_selected size_str = dict_to_create['hardware']['disks'][0]['size'] memory_in_mb = int(dict_to_create['hardware']['memory'] / 1024) options_virt_builder = dict_to_create['builder']['options'] options_virt_install = dict_to_create['install']['options'] id_domains_virt_builder = dict_to_create['builder']['id'] id_os_virt_install = dict_to_create['install']['id'] # UPDATE HARDWARE DICT hardware_update = {} hardware_update['disks'] = dict_to_create['hardware']['disks'] update_domain_dict_hardware(id_new, hardware_update) hyp_to_disk_create = get_host_long_operations_from_path(path_selected, pool=pool_id, type_path='groups') cmds = create_cmd_disk_from_virtbuilder(path_new_qcow=path_new_disk, os_version=id_domains_virt_builder, id_os_virt_install=id_os_virt_install, name_domain_in_xml=id_new, size_str=size_str, memory_in_mb=memory_in_mb, options_cmd=options_virt_builder) # cmds = [{'cmd':'ls -lah > /tmp/prova.txt','title':'es un ls'}] action = {} action['type'] = 'create_disk_virt_builder' action['disk_path'] = path_new_disk action['index_disk'] = 0 action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status(status='RunningVirtBuilder', id_domain=id_new, hyp_id=False, detail='Creating virt-builder image operation is launched in hypervisor {} ({} operations in queue)'.format( hyp_to_disk_create, self.manager.q_long_operations[hyp_to_disk_create].qsize())) self.manager.q_long_operations[hyp_to_disk_create].put(action) except Exception as e: update_domain_status(status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail='Creating disk operation failed when insert action in queue for disk operations') log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'.format( e))
def creating_disk_from_scratch(self,id_new): dict_domain = get_domain(id_new) pool_var = dict_domain['hypervisors_pools'] pool_id = pool_var if type(pool_var) is str else pool_var[0] dict_to_create = dict_domain['create_dict'] if 'disks' in dict_to_create['hardware'].keys(): if len(dict_to_create['hardware']['disks']) > 0: # for index_disk in range(len(dict_to_create['hardware']['disks'])): # relative_path = dict_to_create['hardware']['disks'][index_disk]['file'] # path_new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id) # # UPDATE PATH IN DOMAIN # dict_to_create['hardware']['disks'][index_disk]['file'] = new_file # dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected relative_path = dict_to_create['hardware']['disks'][0]['file'] path_new_disk, path_selected = get_path_to_disk(relative_path, pool=pool_id) # UPDATE PATH IN DOMAIN d_update_domain = {'hardware':{'disks':[{}]}} if len(dict_to_create['hardware']['disks']) > 0: ## supplementary disks for i,dict_other_disk in enumerate(dict_to_create['hardware']['disks'][1:]): path_other_disk, path_other_disk_selected = get_path_to_disk(dict_other_disk['file'], pool=pool_id, type_path=dict_other_disk['type_path']) d_update_domain['hardware']['disks'].append({}) d_update_domain['hardware']['disks'][i+1]['file'] = path_other_disk d_update_domain['hardware']['disks'][i+1]['path_selected'] = path_other_disk_selected d_update_domain['hardware']['disks'][i + 1]['bus'] = dict_other_disk.get('bus','virtio') if dict_other_disk.get('readonly',True) is True: d_update_domain['hardware']['disks'][i + 1]['readonly'] = True else: pass # TODO # update_media_write_access_by_domain(id_media,id_domain) d_update_domain['hardware']['disks'][0]['file'] = path_new_disk d_update_domain['hardware']['disks'][0]['path_selected'] = path_selected d_update_domain['hardware']['disks'][0]['size'] = dict_to_create['hardware']['disks'][0]['size'] if 'bus' in dict_to_create['hardware']['disks'][0].keys(): if dict_to_create['hardware']['disks'][0]['bus'] in BUS_TYPES: d_update_domain['hardware']['disks'][0]['bus'] = dict_to_create['hardware']['disks'][0]['bus'] update_domain_dict_hardware(id_new, d_update_domain) update_domain_dict_create_dict(id_new, d_update_domain) size_str = dict_to_create['hardware']['disks'][0]['size'] hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=pool_id, type_path='groups') cmds = create_cmd_disk_from_scratch(path_new_disk=path_new_disk, size_str=size_str) action = {} action['type'] = 'create_disk_from_scratch' action['disk_path'] = path_new_disk action['index_disk'] = 0 action['domain'] = id_new action['ssh_commands'] = cmds try: update_domain_status(status='CreatingDiskFromScratch', id_domain=id_new, hyp_id=False, detail='Creating disk commands are launched in hypervisor {} ({} operations in queue)'.format( hyp_to_disk_create, self.manager.q_disk_operations[hyp_to_disk_create].qsize())) self.manager.q_disk_operations[hyp_to_disk_create].put(action) except Exception as e: update_domain_status(status='FailedCreatingDomain', id_domain=id_new, hyp_id=False, detail='Creating disk operation failed when insert action in queue for disk operations') log.error( 'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'.format( e)) else: update_domain_status(status='CreatingDomain', id_domain=id_new, hyp_id=False, detail='Creating domain withouth disks')
def recreate_xml_to_start(id, ssl=True, cpu_host_model=False): dict_domain = get_domain(id) xml = dict_domain['xml'] x = DomainXML(xml) if x.parser is False: #error when parsing xml return False ##### actions to customize xml if dict_domain.get('not_change_cpu_section', False) is False: x.set_cpu_host_model(cpu_host_model) # spice video compression #x.add_spice_graphics_if_not_exist() x.set_spice_video_options() # spice password if ssl is True: #recreate random password in x.viewer_passwd x.reset_viewer_passwd() else: # only for test purposes, not use in production x.spice_remove_passwd_nossl() #add vlc access x.add_vlc_with_websockets() # redo network try: list_interfaces = dict_domain['create_dict']['hardware']['interfaces'] except KeyError: list_interfaces = [] log.info('domain {} withouth key interfaces in create_dict'.format(id)) mac_address = [] for interface_index in range(len(x.vm_dict['interfaces'])): mac_address.append(x.vm_dict['interfaces'][interface_index]['mac']) x.remove_interface(order=interface_index) interface_index = 0 for id_interface in list_interfaces: d_interface = get_interface(id_interface) x.add_interface(type=d_interface['kind'], id=d_interface['ifname'], model_type=d_interface['model'], mac=mac_address[interface_index]) interface_index += 1 x.remove_selinux_options() #remove boot order in disk definition that conflict with /os/boot order in xml x.remove_boot_order_from_disks() x.dict_from_xml() # INFO TO DEVELOPER, OJO, PORQUE AQUI SE PIERDE EL BACKING CHAIN?? update_domain_dict_hardware(id, x.vm_dict, xml=xml) if 'viewer_passwd' in x.__dict__.keys(): #update password in database update_domain_viewer_started_values(id, passwd=x.viewer_passwd) log.debug("updated viewer password {} in domain {}".format( x.viewer_passwd, id)) xml = x.return_xml() # log.debug('#####################################################') # log.debug(xml) # log.debug('#####################################################') return xml