def delete_keypairs(keypairs, check_first=True, fail_ok=False, con_ssh=None, auth_info=None): """ Delete keypair(s) Args: keypairs (list/str): keypair(s) to delete check_first (bool) fail_ok (bool) con_ssh (SSHClient): auth_info (dict): Returns (tuple): """ if isinstance(keypairs, str): keypairs = (keypairs, ) if check_first: existing_keypairs = get_keypairs(con_ssh=con_ssh, auth_info=auth_info) keypairs = list(set(keypairs) & set(existing_keypairs)) if not keypairs: msg = 'Give keypair(s) not exist. Do nothing.' LOG.info(msg) return -1, msg LOG.info('Deleting keypairs: {}'.format(keypairs)) code, out = cli.openstack('keypair delete', ' '.join(keypairs), ssh_client=con_ssh, fail_ok=fail_ok, auth_info=auth_info) if code > 0: return code, out post_keypairs = get_keypairs(con_ssh=con_ssh, auth_info=auth_info) undeleted_kp_names = list(set(keypairs) & set(post_keypairs)) if undeleted_kp_names: raise exceptions.NovaError( "keypair(s) still exist after deletion: {}".format( undeleted_kp_names)) msg = 'keypairs deleted successfully: {}'.format(keypairs) LOG.info(msg) return 0, msg
def delete_server_groups(srv_grp_ids=None, check_first=True, fail_ok=False, auth_info=Tenant.get('admin'), con_ssh=None): """ Delete server group(s) Args: srv_grp_ids (list|str): id(s) for server group(s) to delete. check_first (bool): whether to check existence of given server groups before attempt to delete. Default: True. fail_ok (bool): auth_info (dict|None): con_ssh (SSHClient): Returns (tuple): (rtn_code(int), msg(str)) # rtn_code 1,2 only returns when fail_ok=True (-1, 'No server group(s) to delete.') (-1, 'None of the given server group(s) exists on system.') (0, "Server group(s) deleted successfully.") (1, <stderr>) # Deletion rejected for all of the server groups. Return CLI stderr. (2, "Some deleted server group(s) still exist on system:: <srv_grp_ids>") """ existing_sgs = None if not srv_grp_ids: existing_sgs = srv_grp_ids = get_server_groups(con_ssh=con_ssh, auth_info=auth_info) elif isinstance(srv_grp_ids, str): srv_grp_ids = [srv_grp_ids] srv_grp_ids = [sg for sg in srv_grp_ids if sg] if not srv_grp_ids: LOG.info("No server group(s) to delete. Do Nothing") return -1, 'No server group(s) to delete.' if check_first: if existing_sgs is None: existing_sgs = get_server_groups(con_ssh=con_ssh, auth_info=auth_info) srv_grp_ids = list(set(srv_grp_ids) & set(existing_sgs)) if not srv_grp_ids: msg = "None of the given server group(s) exists on system. Do nothing" LOG.info(msg) return -1, msg LOG.info("Deleting server group(s): {}".format(srv_grp_ids)) code, output = cli.openstack('server group delete', ' '.join(srv_grp_ids), ssh_client=con_ssh, fail_ok=True, auth_info=auth_info, timeout=60) if code == 1: return 1, output existing_sgs = get_server_groups(con_ssh=con_ssh, auth_info=auth_info) grps_undeleted = list(set(srv_grp_ids) & set(existing_sgs)) if grps_undeleted: msg = "Some server group(s) still exist on system after deletion: {}".format( grps_undeleted) LOG.warning(msg) if fail_ok: return 2, msg raise exceptions.NovaError(msg) msg = "Server group(s) deleted successfully." LOG.info(msg) return 0, "Server group(s) deleted successfully."
def __remove_or_add_hosts_in_aggregate(aggregate, hosts=None, remove=False, check_first=True, fail_ok=False, con_ssh=None, auth_info=Tenant.get('admin')): """ Remove/Add hosts from/to given aggregate Args: aggregate (str): name of the aggregate to add/remove hosts. cgcsauto aggregate can be added via add_cgcsauto_zone session fixture hosts (list|str): remove (bool): True if remove hosts from given aggregate, otherwise add hosts to aggregate check_first (bool): fail_ok (bool): con_ssh (SSHClient): auth_info (dict): Returns (tuple): (0, "Hosts successfully removed from aggregate") (1, <stderr>) cli rejected on at least one host (2, "Host(s) still exist in aggregate <aggr> after aggregate-remove-host: <unremoved_hosts>) """ hosts_in_aggregate = get_hosts_in_aggregate(aggregate, con_ssh=con_ssh) if hosts is None: if remove: hosts = hosts_in_aggregate else: from keywords import host_helper hosts = host_helper.get_hypervisors() if isinstance(hosts, str): hosts = [hosts] msg_str = 'Remov' if remove else 'Add' LOG.info("{}ing hosts {} in aggregate {}".format(msg_str, hosts, aggregate)) if check_first: if remove: hosts_to_rm_or_add = list(set(hosts) & set(hosts_in_aggregate)) else: hosts_to_rm_or_add = list(set(hosts) - set(hosts_in_aggregate)) else: hosts_to_rm_or_add = list(hosts) if not hosts_to_rm_or_add: warn_str = 'No' if remove else 'All' msg = "{} given host(s) in aggregate {}. Do nothing. Given hosts: {}; hosts in " \ "aggregate: {}".format(warn_str, aggregate, hosts, hosts_in_aggregate) LOG.warning(msg) return -1, msg failed_res = {} cmd = 'aggregate remove host' if remove else 'aggregate add host' for host in hosts_to_rm_or_add: args = '{} {}'.format(aggregate, host) code, output = cli.openstack(cmd, args, ssh_client=con_ssh, fail_ok=True, auth_info=auth_info) if code > 0: failed_res[host] = output if failed_res: err_msg = "'{}' is rejected for following host(s) in aggregate {}: {}".format( cmd, aggregate, failed_res) if fail_ok: LOG.warning(err_msg) return 1, err_msg else: raise exceptions.NovaError(err_msg) post_hosts_in_aggregate = get_hosts_in_aggregate(aggregate, con_ssh=con_ssh) if remove: failed_hosts = list(set(hosts) & set(post_hosts_in_aggregate)) else: failed_hosts = list(set(hosts) - set(post_hosts_in_aggregate)) if failed_hosts: err_msg = "{} accepted, but some host(s) are not {}ed in aggregate {}: {}".format( cmd, msg_str, aggregate, failed_hosts) if fail_ok: LOG.warning(err_msg) return 2, err_msg else: raise exceptions.NovaError(err_msg) succ_msg = "Hosts successfully {}ed in aggregate {}: {}".format( msg_str.lower(), aggregate, hosts) LOG.info(succ_msg) return 0, succ_msg
def delete_aggregates(names, check_first=True, remove_hosts=True, fail_ok=False, con_ssh=None, auth_info=Tenant.get('admin')): """ Add a aggregate with given name and availability zone. Args: names (str|list): name for aggregate to delete check_first (bool) remove_hosts (bool) fail_ok (bool): con_ssh (SSHClient): auth_info (dict): Returns (tuple): (0, "Aggregate <name> is successfully deleted") # aggregate successfully deleted (1, <stderr>) -- cli rejected (2, "Aggregate <name> still exists in aggregate-list after deletion") # Failed although cli accepted """ if check_first: names = get_aggregates(name=names, con_ssh=con_ssh, auth_info=auth_info) if not names: msg = 'Aggregate {} does not exists. Do nothing.'.format(names) LOG.warning(msg) return -1, msg elif isinstance(names, str): names = [names] if remove_hosts: for name in names: remove_hosts_from_aggregate(aggregate=name, check_first=True) LOG.info("Deleting aggregate {}".format(names)) res, out = cli.openstack('aggregate delete', ' '.join(names), ssh_client=con_ssh, fail_ok=fail_ok, auth_info=auth_info) if res == 1: return res, out post_aggregates = get_aggregates(name=names, con_ssh=con_ssh, auth_info=auth_info) if post_aggregates: err_msg = "Aggregate {} still exists in openstack aggregate list after " \ "deletion.".format(post_aggregates) LOG.warning(err_msg) if fail_ok: return 2, err_msg else: raise exceptions.NovaError(err_msg) succ_msg = "Aggregate(s) successfully deleted: {}".format(names) LOG.info(succ_msg) return 0, succ_msg