def delete_vm_contexts(uri_list, vm_id, disk_id): """ Tries to delete all generated vm context images. First queries linstor for all context images for the vm and then deletes one by one ignoring any failed attempts. :param str uri_list: linstor uri list string :param int vm_id: Opennebula id of the VM :param int disk_id: Opennebula disk id of the VM :return: Dict, where key is the resource name and either None(Success) or LinstorError on failure. :rtype: dict[str, Optional[LinstorError]] """ del_result = {} with MultiLinstor(MultiLinstor.controller_uri_list(uri_list)) as lin: rsc_dfn_list_resp = lin.resource_dfn_list(query_volume_definitions=False) if rsc_dfn_list_resp: rsc_dfn_list = rsc_dfn_list_resp[0] # type: ResourceDefinitionResponse delete_list = [x.name for x in rsc_dfn_list.resource_definitions if x.name.startswith(consts.CONTEXT_PREFIX + "-vm{vm_id}-disk{disk_id}" .format(vm_id=vm_id, disk_id=disk_id))] for rsc_name in delete_list: try: delete(rsc_name, uri_list) del_result[rsc_name] = None except LinstorError as le: del_result[rsc_name] = le return del_result
def get_current_context_id(uri_list, vm_id, disk_id): """ :param str uri_list: linstor uri list string :param int vm_id: Opennebula id of the VM :param int disk_id: Opennebula disk id of the VM :return: :rtype: Optional[int] """ with MultiLinstor(MultiLinstor.controller_uri_list(uri_list)) as lin: rsc_dfn_list_resp = lin.resource_dfn_list(query_volume_definitions=False) if rsc_dfn_list_resp: rsc_dfn_list = rsc_dfn_list_resp[0] # type: ResourceDefinitionResponse prefix = consts.CONTEXT_PREFIX + "-vm{vm_id}-disk{disk_id}".format(vm_id=vm_id, disk_id=disk_id) contexts = [x.name for x in rsc_dfn_list.resource_definitions if x.name.startswith(prefix)] index_dict = {} for context in contexts: strindex = context[len(prefix):] if strindex: # e.g. strindex = '-2' numindex = int(strindex[1:]) else: numindex = 0 index_dict[numindex] = context return sorted(index_dict.keys())[-1] if index_dict else None return None
def delete(resource_name, uri_list): """ Deletes a resource with all it's snapshots :param str resource_name: name of the resource :param str uri_list: linstor uris string :return: True """ with MultiLinstor(MultiLinstor.controller_uri_list(uri_list)) as lin: snapshots = lin.snapshot_dfn_list()[0] for snap in [ x for x in snapshots.snapshots if x.rsc_name == resource_name ]: util.log_info("Deleting snapshot '{r}/{s}'".format( r=resource_name, s=snap.snapshot_name)) lin.snapshot_delete(rsc_name=resource_name, snapshot_name=snap.snapshot_name) # there is a regression in python-linstor 0.9.5, were it isn't possible to try to delete # non existing resources (exception with None value) # so deleting it with the low level api still works, also opennebula doesn't need the external name feature util.log_info("Deleting resource '{r}'".format(r=resource_name)) rs = lin.resource_dfn_delete(name=resource_name) if not rs[0].is_success(): raise LinstorError('Could not delete resource {}: {}'.format( resource_name, rs[0])) return True
def wait_resource_ready(resource, timeout=1200): """ waits for the drbd resource to be in sync :param Resource resource: Resource object to check :param int timeout: timeout in seconds :return: return code from command """ with MultiLinstor(resource.client.uri_list) as lin: util.log_info( "Waiting for resource '{r}' to be ready.".format(r=resource.name)) lin.resource_dfn_wait_synced(resource.name, timeout=timeout) util.log_info("Resource '{r}' ready.".format(r=resource.name)) return True
def get_in_use_node(resource): """ Returns the node that currently has the resource primary. :param Resource resource: resource object to check for in use(primary). :return: node name of the primary node, or None if all secondary :rtype: bool """ with MultiLinstor(resource.client.uri_list) as lin: lst = lin.resource_list(filter_by_resources=[resource.name]) # type: list[ResourceResponse] if lst: nodes = [x for x in lst[0].resource_states if x.in_use] if nodes: return nodes[0].node_name return None
def delete(resource): """ Deletes a resource with all it's snapshots :param Resource resource: :return: True """ with MultiLinstor(resource.client.uri_list) as lin: snapshots = lin.snapshot_dfn_list()[0] for snap in [ x for x in snapshots.proto_msg.snapshot_dfns if x.rsc_name == resource.name ]: util.log_info("Deleting snapshot '{r}/{s}'".format( r=resource.name, s=snap.snapshot_name)) lin.snapshot_delete(rsc_name=resource.name, snapshot_name=snap.snapshot_name) util.log_info("Deleting resource '{r}'".format(r=resource.name)) resource.delete() return True