def delete(**kwargs): resource_id = ctx.instance.runtime_properties.get('resource_id') ctx.logger.info("Delete: {}".format(repr(resource_id))) if not resource_id: # not raise exception on 'uninstall' workflow ctx.logger.info("No network for delete") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: # lookup the default network by name try: network = conn.networkLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the network: {}'.format(repr(e))) if network.destroy() < 0: raise cfy_exc.NonRecoverableError('Can not undefine network.') ctx.instance.runtime_properties['resource_id'] = None ctx.instance.runtime_properties['backups'] = {} finally: conn.close()
def snapshot_apply(**kwargs): resource_id = ctx.instance.runtime_properties.get('resource_id') ctx.logger.info("Snapshot restore for: {}".format(repr(resource_id))) if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No network for restore") libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: # lookup the default network by name try: network = conn.networkLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the network: {}'.format(repr(e))) common.xml_snapshot_apply(kwargs, resource_id, network.XMLDesc()) finally: conn.close()
def delete(**kwargs): ctx.logger.info("delete") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not raise exception on 'uninstall' workflow ctx.logger.info("No servers for delete") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) _delete_force(dom) ctx.instance.runtime_properties['resource_id'] = None finally: conn.close()
def snapshot_apply(**kwargs): ctx.logger.info("restore") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for restore.") snapshot_name = common.get_backupname(kwargs) libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) if kwargs.get("snapshot_incremental"): # raised exception if libvirt has not found any snapshot = dom.snapshotLookupByName(snapshot_name) dom.revertToSnapshot(snapshot) ctx.logger.info("Reverted to: {}".format(snapshot.getName())) else: _backup_apply(conn, dom, resource_id, snapshot_name, template_params.get('full_dump', False), kwargs) ctx.logger.info("Restored to: {}".format(snapshot_name)) finally: conn.close()
def snapshot_create(**kwargs): resource_id = ctx.instance.runtime_properties.get('resource_id') ctx.logger.info("Snapshot create: {}".format(repr(resource_id))) if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No volume for backup") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor' ) try: # lookup the default volume by name try: pool = conn.storagePoolLookupByName(template_params["pool"]) volume = pool.storageVolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the volume: {}'.format(repr(e)) ) common.xml_snapshot_create(kwargs, resource_id, volume.XMLDesc()) finally: conn.close()
def reboot(**kwargs): ctx.logger.info("reboot") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for reboot") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) if dom.reboot() < 0: raise cfy_exc.NonRecoverableError('Can not reboot guest domain.') finally: conn.close()
def configure(**kwargs): ctx.logger.info("configure") libvirt_auth, template_params = get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') domain_file = kwargs.get('domain_file') domain_template = kwargs.get('domain_template') if domain_file: domain_template = ctx.get_resource(domain_file) if not domain_file and not domain_template: resource_dir = resource_filename(__name__, 'templates') domain_file = '{}/domain.xml'.format(resource_dir) ctx.logger.info("Will be used internal: %s" % domain_file) if not domain_template: domain_desc = open(domain_file) with domain_desc: domain_template = domain_desc.read() template_engine = Template(domain_template) if not template_params: template_params = {} if not template_params.get("resource_id"): template_params["resource_id"] = ctx.instance.id if (not template_params.get("memmory_minsize") and template_params.get('memmory_size')): template_params["memmory_minsize"] = int( template_params['memmory_size']) / 2 if not template_params.get("instance_uuid"): template_params["instance_uuid"] = str(uuid.uuid4()) params = {"ctx": ctx} params.update(template_params) xmlconfig = template_engine.render(params) ctx.logger.info(xmlconfig) dom = conn.defineXML(xmlconfig) if dom is None: raise cfy_exc.NonRecoverableError( 'Failed to define a domain from an XML definition.') ctx.instance.runtime_properties['resource_id'] = dom.name() if dom.create() < 0: raise cfy_exc.NonRecoverableError('Can not boot guest domain.') conn.close() ctx.logger.info('Guest ' + dom.name() + ' has booted') ctx.instance.runtime_properties['resource_id'] = dom.name() ctx.instance.runtime_properties['params'] = template_params
def create(**kwargs): ctx.logger.info("Creating new network.") libvirt_auth, template_params = get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') network_file = kwargs.get('network_file') network_template = kwargs.get('network_template') if network_file: network_template = ctx.get_resource(network_file) if not network_file and not network_template: resource_dir = resource_filename(__name__, 'templates') network_file = '{}/network.xml'.format(resource_dir) ctx.logger.info("Will be used internal: %s" % network_file) if not network_template: domain_desc = open(network_file) with domain_desc: network_template = domain_desc.read() template_engine = Template(network_template) if not template_params: template_params = {} if not template_params.get("resource_id"): template_params["resource_id"] = ctx.instance.id if not template_params.get("instance_uuid"): template_params["instance_uuid"] = str(uuid.uuid4()) params = {"ctx": ctx} params.update(template_params) xmlconfig = template_engine.render(params) ctx.logger.info(xmlconfig) # create a persistent virtual network network = conn.networkCreateXML(xmlconfig) if network is None: raise cfy_exc.NonRecoverableError('Failed to create a virtual network') active = network.isActive() if active == 1: ctx.logger.info('The new persistent virtual network is active') else: ctx.logger.info('The new persistent virtual network is not active') conn.close() ctx.logger.info('Network ' + network.name() + ' has created.') ctx.instance.runtime_properties['resource_id'] = network.name() ctx.logger.info('Params: ' + repr(template_params)) ctx.instance.runtime_properties['params'] = template_params
def create(**kwargs): ctx.logger.info("Creating new network.") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: if ctx.instance.runtime_properties.get("use_external_resource"): # lookup the default network by name resource_id = ctx.instance.runtime_properties["resource_id"] try: network = conn.networkLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the network: {}'.format(repr(e))) # save settings ctx.instance.runtime_properties['params'] = template_params ctx.instance.runtime_properties['resource_id'] = network.name() ctx.instance.runtime_properties['use_external_resource'] = True return xmlconfig = common.gen_xml_template(kwargs, template_params, 'network') resource_id = ctx.instance.runtime_properties.get('resource_id') if resource_id: ctx.logger.info("Network is already alive, skip create.") try: network = conn.networkLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the network: {}'.format(repr(e))) else: # create a persistent virtual network network = conn.networkCreateXML(xmlconfig) if network is None: raise cfy_exc.NonRecoverableError( 'Failed to create a virtual network') ctx.logger.info('Network ' + network.name() + ' has created.') ctx.logger.info('Params: ' + repr(template_params)) ctx.instance.runtime_properties['params'] = template_params ctx.instance.runtime_properties['resource_id'] = network.name() ctx.instance.runtime_properties['use_external_resource'] = False active = network.isActive() if active == 1: ctx.logger.info('The new persistent virtual network is active') else: ctx.logger.info('The new persistent virtual network is not active') finally: conn.close()
def update(**kwargs): ctx.logger.info("set vcpu/memory values") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for update") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) # change memory values if template_params.get('memory_size'): ctx.logger.info("Set memory to {}".format( repr(template_params['memory_size']))) if dom.setMemory(template_params['memory_size']) < 0: raise cfy_exc.NonRecoverableError( "Can not change memory amount.") state, _ = dom.state() if state == libvirt.VIR_DOMAIN_RUNNING: ctx.logger.info("CPU/Maximum memory size count should be changed " "on stopped vm.") return # change vcpu values if template_params.get('vcpu'): ctx.logger.info("Set cpu count to {}".format( repr(template_params['vcpu']))) if dom.setVcpus(template_params['vcpu']) < 0: raise cfy_exc.NonRecoverableError("Can not change cpu count.") # change max memory values if template_params.get('memory_maxsize'): ctx.logger.info("Set max memory to {}".format( repr(template_params['memory_maxsize']))) if dom.setMaxMemory(template_params['memory_maxsize']) < 0: raise cfy_exc.NonRecoverableError( "Can not change max memory amount.") finally: conn.close()
def snapshot_create(**kwargs): ctx.logger.info("backup") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for backup.") snapshot_name = common.get_backupname(kwargs) libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) if kwargs.get("snapshot_incremental"): snapshot_type = kwargs.get('snapshot_type') params = { 'snapshot_name': snapshot_name, 'snapshot_description': snapshot_type } if template_params: params.update(template_params) xmlconfig = common.gen_xml_template(kwargs, params, 'snapshot') try: # will raise exception if unexist snapshot = dom.snapshotLookupByName(snapshot_name) raise cfy_exc.NonRecoverableError( "Snapshot {snapshot_name} already exists.".format( snapshot_name=snapshot.getName(), )) except libvirt.libvirtError: pass snapshot = dom.snapshotCreateXML(xmlconfig) ctx.logger.info("Snapshot name: {}".format(snapshot.getName())) else: _backup_create(conn, dom, resource_id, snapshot_name, template_params.get('full_dump', False), kwargs) ctx.logger.info("Backup {snapshot_name} is created.".format( snapshot_name=snapshot_name, )) finally: conn.close()
def start(**kwargs): ctx.logger.info("start") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for start") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') # wait for ip on start wait_for_ip = template_params.get('wait_for_ip', False) try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) for i in xrange(10): state, _ = dom.state() ctx.logger.info("Tring to start vm {}/10".format(i)) if wait_for_ip: ctx.logger.info("Waiting for ip.") if state == libvirt.VIR_DOMAIN_RUNNING: _update_network_list(dom) # wait for ip check if not wait_for_ip or ( wait_for_ip and ctx.instance.runtime_properties.get('ip')): ctx.logger.info("Looks as running.") return elif dom.create() < 0: raise cfy_exc.NonRecoverableError( 'Can not start guest domain.') time.sleep(30) # still no ip if wait_for_ip: raise cfy_exc.RecoverableError('No ip for now, try later') finally: conn.close()
def stop(**kwargs): ctx.logger.info("stop") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not raise exception on 'uninstall' workflow ctx.logger.info("No pools for stop") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: pool = conn.storagePoolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the pool: {}'.format(repr(e))) for i in xrange(10): if not pool.isActive(): ctx.logger.info("Looks as not active.") break ctx.logger.info("Tring to stop pool {}/10".format(i)) if pool.destroy() < 0: raise cfy_exc.NonRecoverableError('Can not destroy pool.') time.sleep(30) state, capacity, allocation, available = pool.info() ctx.logger.info( "State: {}, Capacity: {}, Allocation: {}, Available: {}".format( repr(state), repr(capacity), repr(allocation), repr(available))) if state != libvirt.VIR_STORAGE_POOL_INACTIVE: if pool.delete() < 0: raise cfy_exc.RecoverableError('Can not delete guest pool.') finally: conn.close()
def start(**kwargs): ctx.logger.info("start") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: ctx.logger.info("No volumes for zero") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor' ) try: # lookup the default volume by name try: pool = conn.storagePoolLookupByName(template_params["pool"]) volume = pool.storageVolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the volume: {}'.format(repr(e)) ) if ( template_params.get('zero_wipe') and template_params.get('allocation') ): _stream_wipe( ctx=ctx, conn=conn, volume=volume, allocation=int(template_params.get('allocation', 0)) ) if (template_params.get('url')): _stream_download( ctx=ctx, conn=conn, volume=volume, url=template_params.get('url') ) finally: conn.close()
def configure(**kwargs): ctx.logger.info("configure") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') _update_template_params(template_params) try: if ctx.instance.runtime_properties.get("use_external_resource"): # lookup the default domain by name resource_id = ctx.instance.runtime_properties["resource_id"] try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) # save settings ctx.instance.runtime_properties['params'] = template_params ctx.instance.runtime_properties['resource_id'] = dom.name() ctx.instance.runtime_properties['use_external_resource'] = True return resource_id = ctx.instance.runtime_properties.get('resource_id') if resource_id: ctx.logger.info("Domain is already alive, skip create.") try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) else: xmlconfig = common.gen_xml_template(kwargs, template_params, 'domain') dom = conn.defineXML(xmlconfig) if dom is None: raise cfy_exc.NonRecoverableError( 'Failed to define a domain from an XML definition.') ctx.instance.runtime_properties['resource_id'] = dom.name() ctx.instance.runtime_properties['params'] = template_params finally: conn.close()
def stop(**kwargs): ctx.logger.info("stop") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not raise exception on 'uninstall' workflow ctx.logger.info("No servers for stop") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) # reset ip on stop ctx.instance.runtime_properties['ip'] = None state, _ = dom.state() for i in xrange(10): if state != libvirt.VIR_DOMAIN_RUNNING: ctx.logger.info("Looks as not run.") return ctx.logger.info("Tring to stop vm {}/10".format(i)) if dom.shutdown() < 0: raise cfy_exc.NonRecoverableError( 'Can not shutdown guest domain.') time.sleep(30) state, _ = dom.state() finally: conn.close()
def create(**kwargs): ctx.logger.info("Creating new iso image.") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: # lookup the default volume by name try: pool = conn.storagePoolLookupByName(template_params["pool"]) volume = pool.storageVolLookupByName(template_params["volume"]) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the volume: {}'.format(repr(e))) outiso = iso9660.create_iso( vol_ident=template_params.get('vol_ident', 'cidata'), sys_ident=template_params.get('sys_ident', ""), get_resource=ctx.get_resource, files=template_params.get('files', {}), files_raw=template_params.get('files_raw', {})) outiso.seek(0, os.SEEK_END) iso_size = outiso.tell() outiso.seek(0, os.SEEK_SET) ctx.logger.info("ISO size: {}".format(repr(iso_size))) stream = conn.newStream(0) volume.upload(stream, 0, iso_size, 0) outiso.seek(0, os.SEEK_SET) read_size = iso_size while read_size > 0: buffer = outiso.read(read_size) read_size -= len(buffer) stream.send(buffer) stream.finish() finally: conn.close()
def delete(**kwargs): ctx.logger.info("delete") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: ctx.logger.info("No servers for delete") return libvirt_auth, _ = get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except Exception as e: dom = None ctx.logger.info("Non critical error: {}".format(str(e))) if dom is None: raise cfy_exc.NonRecoverableError('Failed to find the domain') state, reason = dom.state() if state != libvirt.VIR_DOMAIN_SHUTOFF: if dom.destroy() < 0: raise cfy_exc.NonRecoverableError( 'Can not destroy guest domain.') try: if dom.undefineFlags(libvirt.VIR_DOMAIN_UNDEFINE_NVRAM) < 0: raise cfy_exc.NonRecoverableError( 'Can not undefine guest domain with NVRAM.') except Exception as e: ctx.logger.info("Non critical error: {}".format(str(e))) if dom.undefine() < 0: raise cfy_exc.NonRecoverableError( 'Can not undefine guest domain.') finally: conn.close()
def snapshot_delete(**kwargs): ctx.logger.info("remove_backup") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for remove_backup.") snapshot_name = common.get_backupname(kwargs) libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) if kwargs.get("snapshot_incremental"): # raised exception if libvirt has not found any snapshot = dom.snapshotLookupByName(snapshot_name) if snapshot.numChildren(): subsnapshots = [ snap.getName() for snap in snapshot.listAllChildren() ] raise cfy_exc.NonRecoverableError( "Sub snapshots {subsnapshots} found for {snapshot_name}. " "You should remove subsnaphots before remove current.". format(snapshot_name=snapshot_name, subsnapshots=repr(subsnapshots))) snapshot.delete() else: _backup_delete(dom, resource_id, snapshot_name, template_params.get('full_dump', False), kwargs) ctx.logger.info("Backup deleted: {}".format(snapshot_name)) finally: conn.close()
def stop(**kwargs): ctx.logger.info("stop") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: ctx.logger.info("No servers for delete") return libvirt_auth, _ = get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except Exception as e: dom = None ctx.logger.info("Non critical error: {}".format(str(e))) if dom is None: raise cfy_exc.NonRecoverableError('Failed to find the domain') state, reason = dom.state() for i in xrange(10): state, reason = dom.state() if state != libvirt.VIR_DOMAIN_RUNNING: ctx.logger.info("Looks as not run.") return ctx.logger.info("Tring to stop vm {}/10".format(i)) if dom.shutdown() < 0: raise cfy_exc.NonRecoverableError( 'Can not shutdown guest domain.') time.sleep(30) state, reason = dom.state() finally: conn.close()
def create(**kwargs): ctx.logger.info("Creating new pool.") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') _update_template_params(template_params) try: if ctx.instance.runtime_properties.get("use_external_resource"): # lookup the default pool by name resource_id = ctx.instance.runtime_properties["resource_id"] try: pool = conn.storagePoolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the pool: {}'.format(repr(e))) # save settings ctx.instance.runtime_properties['params'] = template_params ctx.instance.runtime_properties['resource_id'] = pool.name() ctx.instance.runtime_properties['use_external_resource'] = True return xmlconfig = common.gen_xml_template(kwargs, template_params, 'pool') # create a persistent virtual pool pool = conn.storagePoolDefineXML(xmlconfig) if pool is None: raise cfy_exc.NonRecoverableError( 'Failed to create a virtual pool') ctx.logger.info('pool ' + pool.name() + ' has created.') ctx.logger.info('Params: ' + repr(template_params)) ctx.instance.runtime_properties['params'] = template_params ctx.instance.runtime_properties['resource_id'] = pool.name() ctx.instance.runtime_properties['use_external_resource'] = False finally: conn.close()
def delete(**kwargs): resource_id = ctx.instance.runtime_properties.get('resource_id') ctx.logger.info("Delete: {}".format(repr(resource_id))) if not resource_id: # not raise exception on 'uninstall' workflow ctx.logger.info("No volume for delete") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor' ) try: # lookup the default volume by name try: pool = conn.storagePoolLookupByName(template_params["pool"]) volume = pool.storageVolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the volume: {}'.format(repr(e)) ) if volume.delete(0) < 0: raise cfy_exc.NonRecoverableError( 'Can not undefine volume.' ) ctx.instance.runtime_properties['resource_id'] = None ctx.instance.runtime_properties['backups'] = {} ctx.instance.runtime_properties['params'] = {} finally: conn.close()
def start(**kwargs): ctx.logger.info("start") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No pool for start") if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: pool = conn.storagePoolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the pool: {}'.format(repr(e))) # pool create for i in xrange(10): if pool.isActive(): ctx.logger.info("Looks as active.") break ctx.logger.info("Tring to start pool {}/10".format(i)) if pool.create() < 0: raise cfy_exc.RecoverableError('Can not start pool.') time.sleep(30) else: raise cfy_exc.RecoverableError('Can not start pool.') finally: conn.close()
def stop(**kwargs): ctx.logger.info("stop") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not raise exception on 'uninstall' workflow ctx.logger.info("No volumes for stop") return if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor' ) try: # lookup the default volume by name try: pool = conn.storagePoolLookupByName(template_params["pool"]) volume = pool.storageVolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the volume: {}'.format(repr(e)) ) for i in xrange(10): ctx.logger.info("Tring to wipe volume {}/10".format(i)) if volume.wipe(0) == 0: break time.sleep(30) except libvirt.libvirtError as e: ctx.logger.info('Failed to wipe the volume: {}'.format(repr(e))) finally: conn.close()
def perfomance(**kwargs): ctx.logger.info("update statistics") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for statistics.") libvirt_auth, template_params = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) statistics = ctx.instance.runtime_properties.get('stat', {}) before_usage = _current_use(dom) # usage generated by compare cpu_time before # and after sleep for 5 seconds. ctx.logger.debug("Used: {} seconds.".format(before_usage)) time.sleep(5) statistics['cpu'] = 100 * (_current_use(dom) - before_usage) / 5 memory = dom.memoryStats() statistics['memory'] = memory.get('actual', 0) / 1024.0 ctx.instance.runtime_properties['stat'] = statistics ctx.logger.info("Statistics: {}".format(repr(statistics))) finally: conn.close()
def delete(**kwargs): resource_id = ctx.instance.runtime_properties.get('resource_id') ctx.logger.info("Delete: {}".format(repr(resource_id))) if not resource_id: ctx.logger.info("No network for delete") return libvirt_auth, _ = get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') # lookup the default network by name network = conn.networkLookupByName(resource_id) if network is None: raise cfy_exc.NonRecoverableError('Failed to find the network') if network.destroy() < 0: raise cfy_exc.NonRecoverableError('Can not undefine network.') conn.close()
def suspend(**kwargs): ctx.logger.info("suspend") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No servers for suspend") libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: dom = conn.lookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the domain: {}'.format(repr(e))) state, _ = dom.state() for i in xrange(10): if state != libvirt.VIR_DOMAIN_RUNNING: ctx.logger.info("Looks as not run.") return ctx.logger.info("Tring to suspend vm {}/10".format(i)) if dom.suspend() < 0: raise cfy_exc.NonRecoverableError( 'Can not suspend guest domain.') time.sleep(30) state, _ = dom.state() finally: conn.close()
def configure(**kwargs): ctx.logger.info("configure") resource_id = ctx.instance.runtime_properties.get('resource_id') if not resource_id: # not uninstall workflow, raise exception raise cfy_exc.NonRecoverableError("No pool for configure") if ctx.instance.runtime_properties.get('use_external_resource'): ctx.logger.info("External resource, skip") return libvirt_auth, _ = common.get_libvirt_params(**kwargs) conn = libvirt.open(libvirt_auth) if conn is None: raise cfy_exc.NonRecoverableError( 'Failed to open connection to the hypervisor') try: try: pool = conn.storagePoolLookupByName(resource_id) except libvirt.libvirtError as e: raise cfy_exc.NonRecoverableError( 'Failed to find the pool: {}'.format(repr(e))) state, capacity, allocation, available = pool.info() ctx.logger.info( "State: {}, Capacity: {}, Allocation: {}, Available: {}".format( repr(state), repr(capacity), repr(allocation), repr(available))) if state == libvirt.VIR_STORAGE_POOL_INACTIVE: if pool.build(0) < 0: raise cfy_exc.RecoverableError('Can not build guest pool.') finally: conn.close()
def test_get_libvirt_params(self): # no properties _ctx = MockCloudifyContext('node_name', properties={}, runtime_properties={}) current_ctx.set(_ctx) with mock.patch("cloudify_libvirt.common.uuid.uuid4", mock.Mock(return_value="some_uuid")): self.assertEqual(common.get_libvirt_params(), (None, { 'name': 'node_name', 'instance_uuid': 'some_uuid' })) self.assertEqual( _ctx.instance.runtime_properties, { 'libvirt_auth': None, 'params': { 'name': 'node_name', 'instance_uuid': 'some_uuid' } }) # overwrite _ctx = MockCloudifyContext('node_name', properties={ 'params': { 'a': "b", 'c': 'g' }, 'libvirt_auth': { 'a': 'c' } }, runtime_properties={ 'params': { 'c': "d", 'e': 'g' }, 'libvirt_auth': { 'a': 'd' } }) current_ctx.set(_ctx) with mock.patch("cloudify_libvirt.common.uuid.uuid4", mock.Mock(return_value="some_uuid")): self.assertEqual( common.get_libvirt_params(params={'z': 'y'}, libvirt_auth={'w': 'x'}), ( { 'w': 'x' }, { # default values 'instance_uuid': 'some_uuid', 'name': 'node_name', # combined 'a': 'b', 'c': 'd', 'e': 'g', 'z': 'y' })) self.assertEqual( _ctx.instance.runtime_properties, { 'libvirt_auth': { 'w': 'x' }, 'params': { # default values 'instance_uuid': 'some_uuid', 'name': 'node_name', # combined 'a': 'b', 'c': 'd', 'e': 'g', 'z': 'y' } })
def create(**kwargs): ctx.logger.info("create") common.get_libvirt_params(**kwargs)