def get_apps(field='status', application=None, con_ssh=None, auth_info=Tenant.get('admin_platform'), rtn_dict=False, **kwargs): """ Get applications values for give apps and fields via system application-list Args: application (str|list|tuple): field (str|list|tuple): con_ssh: auth_info: rtn_dict: **kwargs: extra filters other than application Returns (list|dict): list of list, or dict with app name(str) as key and values(list) for given fields for each app as value """ table_ = table_parser.table( cli.system('application-list', ssh_client=con_ssh, auth_info=auth_info)[ 1]) if application: kwargs['application'] = application return table_parser.get_multi_values(table_, fields=field, rtn_dict=rtn_dict, zip_values=True, **kwargs)
def get_aggregates(field='name', name=None, avail_zone=None, con_ssh=None, auth_info=Tenant.get('admin')): """ Get a list of aggregates Args: field (str|list|tuple): id or name name (str|list): filter out the aggregates with given name if specified avail_zone (str): filter out the aggregates with given availability zone if specified con_ssh (SSHClient): auth_info (dict): Returns (list): """ kwargs = {} if avail_zone: kwargs['Availability Zone'] = avail_zone if name: kwargs['Name'] = name aggregates_tab = table_parser.table( cli.openstack('aggregate list', ssh_client=con_ssh, auth_info=auth_info)[1]) return table_parser.get_multi_values(aggregates_tab, field, **kwargs)
def get_flavors(name=None, memory=None, disk=None, ephemeral=None, swap=None, vcpu=None, rxtx=None, is_public=None, flv_id=None, long=False, con_ssh=None, auth_info=None, strict=True, field='id'): """ Get a flavor id with given criteria. If no criteria given, a random flavor will be returned. Args: name (str): name of a flavor memory (int): memory size in MB disk (int): size of the disk in GB ephemeral (int): size of ephemeral disk in GB swap (int): size of swap disk in GB vcpu (int): number of vcpus rxtx (str): is_public (bool): flv_id (str) long (bool) con_ssh (SSHClient): auth_info (dict): strict (bool): whether or not to perform strict search on provided values field (str|list|tuple) Returns (list): """ args = '--long' if long else '' table_ = table_parser.table( cli.openstack('flavor list', args, ssh_client=con_ssh, auth_info=auth_info)[1]) req_dict = { 'Name': name, 'RAM': memory, 'Disk': disk, 'Ephemeral': ephemeral, 'Swap': '' if str(swap) == '0' else swap, 'VCPUs': vcpu, 'RXTX Factor': rxtx, 'Is Public': is_public, 'ID': flv_id, } final_dict = {k: str(v) for k, v in req_dict.items() if v is not None} return table_parser.get_multi_values(table_, field, strict=strict, **final_dict)
def get_helm_overrides(field='overrides namespaces', app_name='stx-openstack', charts=None, auth_info=Tenant.get('admin_platform'), con_ssh=None): """ Get helm overrides values via system helm-override-list Args: field (str): app_name charts (None|str|list|tuple): auth_info: con_ssh: Returns (list): """ table_ = table_parser.table( cli.system('helm-override-list', app_name, ssh_client=con_ssh, auth_info=auth_info)[1]) if charts: table_ = table_parser.filter_table(table_, **{'chart name': charts}) vals = table_parser.get_multi_values(table_, fields=field, evaluate=True) return vals
def get_stacks(name=None, field='id', con_ssh=None, auth_info=None, all_=True): """ Get the stacks list based on name if given for a given tenant. Args: con_ssh (SSHClient): If None, active controller ssh will be used. auth_info (dict): Tenant dict. If None, primary tenant will be used. all_ (bool): whether to display all stacks for admin user name (str): Given name for the heat stack field (str|list|tuple) Returns (list): list of heat stacks. """ args = '' if auth_info is not None: if auth_info['user'] == 'admin' and all_: args = '--a' table_ = table_parser.table( cli.openstack('stack list', positional_args=args, ssh_client=con_ssh, auth_info=auth_info)[1]) kwargs = {'Stack Name': name} if name else {} return table_parser.get_multi_values(table_, field, **kwargs)
def get_roles(field='ID', con_ssh=None, auth_info=Tenant.get('admin'), **kwargs): table_ = table_parser.table( cli.openstack('role list', ssh_client=con_ssh, auth_info=auth_info)[1]) return table_parser.get_multi_values(table_, field, **kwargs)
def get_role_assignments(field='Role', names=True, role=None, user=None, project=None, user_domain=None, group=None, group_domain=None, domain=None, project_domain=None, inherited=None, effective_only=None, con_ssh=None, auth_info=Tenant.get('admin')): """ Get values from 'openstack role assignment list' table Args: field (str|list|tuple): role assignment table header to determine which values to return names (bool): whether to display role assignment with name (default is ID) role (str): an existing role from openstack role list project (str): tenant name. When unset, the primary tenant name will be used user (str): an existing user that belongs to given tenant domain (str): Include <domain> (name or ID) group (str): Include <group> (name or ID) group_domain (str): Domain the group belongs to (name or ID). This can be used in case collisions between group names exist. project_domain (str): Domain the project belongs to (name or ID). This can be used in case collisions between project names exist. user_domain (str): Domain the user belongs to (name or ID). This can be used in case collisions between user names exist. inherited (bool): Specifies if the role grant is inheritable to the sub projects effective_only (bool): Whether to show effective roles only con_ssh (SSHClient): active controller ssh session auth_info (dict): auth info to use to executing the add role cli Returns (list): list of values """ optional_args = { 'role': role, 'user': user, 'project': project, 'domain': domain, 'group': group, 'group-domain': group_domain, 'project-domain': project_domain, 'user-domain': user_domain, 'names': True if names else None, 'effective': True if effective_only else None, 'inherited': True if inherited else None } args = common.parse_args(optional_args) role_assignment_tab = table_parser.table( cli.openstack('role assignment list', args, ssh_client=con_ssh, auth_info=auth_info)[1]) if not role_assignment_tab['headers']: LOG.info("No role assignment is found with criteria: {}".format(args)) return [] return table_parser.get_multi_values(role_assignment_tab, field)
def check_alarm_summary_match_subcloud(subcloud, timeout=400): LOG.info( "Ensure alarm summary on SystemController with subcloud {}".format( subcloud)) subcloud_auth = Tenant.get('admin_platform', dc_region=subcloud) central_auth = Tenant.get('admin_platform', dc_region='RegionOne') severities = [ "critical_alarms", "major_alarms", "minor_alarms", "warnings" ] central_alarms = subcloud_alarms = None end_time = time.time() + timeout while time.time() < end_time: output_central = cli.dcmanager('alarm summary', auth_info=central_auth, fail_ok=False)[1] output_sub = cli.fm("alarm-summary", auth_info=subcloud_auth, fail_ok=False)[1] central_alarms = table_parser.get_multi_values( table_parser.table(output_central), fields=severities, **{"NAME": subcloud}) subcloud_alarms = table_parser.get_multi_values( table_parser.table(output_sub), severities) if central_alarms == subcloud_alarms: LOG.info( "'dcmanager alarm summary' output for {} matches 'fm alarm-summary' on " "{}".format(subcloud, subcloud)) return time.sleep(30) assert central_alarms == subcloud_alarms, \ "'dcmanager alarm summary did not match 'fm alarm-summary' on {} " \ "within {}s".format(subcloud, timeout)
def add_routes_to_subcloud(subcloud, subcloud_table, fail_ok=False): LOG.debug("Add routes back to subcloud: {}".format(subcloud)) ssh_client = ControllerClient.get_active_controller(name=subcloud) for host_id in subcloud_table: comm_args = table_parser.get_multi_values( subcloud_table[host_id], ["ifname", "network", "prefix", "gateway"]) command = "host-route-add {} {} {} {} {}".format( host_id, comm_args[0][0], comm_args[1][0], comm_args[2][0], comm_args[3][0]) code, output = cli.system("host-route-list {}".format(host_id)) uuid_list = table_parser.get_values(table_parser.table(output), "uuid") if table_parser.get_values(subcloud_table[host_id], "uuid")[0] not in uuid_list: cli.system(command, ssh_client=ssh_client, fail_ok=fail_ok)
def get_keypairs(name=None, field='Name', con_ssh=None, auth_info=None): """ Args: name (str): Name of the key pair to filter for a given user field (str|list|tuple) con_ssh (SSHClient): auth_info (dict): Tenant to be used to execute the cli if none Primary tenant will be used Returns (list):return keypair names """ table_ = table_parser.table( cli.openstack('keypair list', ssh_client=con_ssh, auth_info=auth_info)[1]) return table_parser.get_multi_values(table_, field, Name=name)
def get_users(field='ID', con_ssh=None, auth_info=Tenant.get('admin'), **kwargs): """ Return a list of user id(s) with given user name. Args: field (str|list|tuple): con_ssh (SSHClient): auth_info Returns (list): list of user id(s) """ table_ = table_parser.table(cli.openstack('user list', ssh_client=con_ssh, auth_info=auth_info)[1]) return table_parser.get_multi_values(table_, field, **kwargs)
def get_docker_images(repo=None, tag=None, field='IMAGE ID', con_ssh=None, fail_ok=False): """ get values for given docker image via 'docker image ls <repo>' Args: repo (str): tag (str|None): field (str|tuple|list): con_ssh: fail_ok Returns (list|None): return None if no docker images returned at all due to cmd failure """ args = None if repo: args = repo if tag: args += ':{}'.format(tag) code, output = exec_docker_cmd(sub_cmd='image ls', args=args, fail_ok=fail_ok, con_ssh=con_ssh) if code != 0: return None table_ = table_parser.table_kube(output) if not table_['values']: if fail_ok: return None else: raise exceptions.ContainerError( "docker image {} does not exist".format(args)) values = table_parser.get_multi_values(table_, fields=field, zip_values=True) return values
def get_server_groups(field='ID', all_projects=True, long=False, strict=True, regex=False, auth_info=Tenant.get('admin'), con_ssh=None, **kwargs): """ Get server groups ids based on the given criteria Args: auth_info (dict): con_ssh (SSHClient): strict (bool): whether to do strict search for given name regex (bool): whether or not to use regex when for given name all_projects(bool): whether to list for all projects long field (str|list|tuple): **kwargs: filters Returns (list): list of server groups """ args_dict = {'--all-projects': all_projects, '--long': long} args = common.parse_args(args_dict) table_ = table_parser.table( cli.openstack('server group list', args, ssh_client=con_ssh, auth_info=auth_info)[1]) def _parse_list(value_str): return [val.strip() for val in value_str.split(',')] parsers = {_parse_list: ('Policies', 'Members')} return table_parser.get_multi_values(table_, field, strict=strict, regex=regex, parsers=parsers, **kwargs)
def get_projects(field='ID', auth_info=Tenant.get('admin'), con_ssh=None, strict=False, **filters): """ Get list of Project names or IDs Args: field (str|list|tuple): auth_info: con_ssh: strict (bool): used for filters filters Returns (list): """ table_ = table_parser.table( cli.openstack('project list', ssh_client=con_ssh, auth_info=auth_info)[ 1]) return table_parser.get_multi_values(table_, field, strict=strict, **filters)
def get_images(long=False, images=None, field='id', auth_info=Tenant.get('admin'), con_ssh=None, strict=True, exclude=False, **kwargs): """ Get a list of image id(s) that matches the criteria Args: long (bool) images (str|list): ids of images to filter from field(str|list|tuple): id or name auth_info (dict): con_ssh (SSHClient): strict (bool): match full string or substring for the value(s) given in kwargs. This is only applicable if kwargs key-val pair(s) are provided. exclude (bool): whether to exclude item containing the string/pattern in kwargs. e.g., search for images that don't contain 'raw' **kwargs: header-value pair(s) to filter out images from given image list. e.g., Status='active', Name='centos' Returns (list): list of image ids """ args = '--long' if long else '' table_ = table_parser.table( cli.openstack('image list', args, ssh_client=con_ssh, auth_info=auth_info)[1]) if images: table_ = table_parser.filter_table(table_, ID=images) return table_parser.get_multi_values(table_, field, strict=strict, exclude=exclude, **kwargs)
def get_nodes(hosts=None, status=None, field='STATUS', exclude=False, con_ssh=None, fail_ok=False): """ Get nodes values via 'kubectl get nodes' Args: hosts (None|str|list|tuple): table filter status (None|str|list|tuple): table filter field (str|list|tuple): any header of the nodes table exclude (bool): whether to exclude rows with given criteria con_ssh: fail_ok: Returns (None|list): None if cmd failed. """ code, output = exec_kube_cmd('get', args='nodes', con_ssh=con_ssh, fail_ok=fail_ok) if code > 0: return None table_ = table_parser.table_kube(output) if hosts or status: table_ = table_parser.filter_table(table_, exclude=exclude, **{ 'NAME': hosts, 'STATUS': status }) return table_parser.get_multi_values(table_, field)
def get_resources(field='NAME', namespace=None, all_namespaces=None, resource_names=None, resource_type='pod', labels=None, field_selectors=None, con_ssh=None, fail_ok=False, grep=None, exclude=False, strict=True, **kwargs): """ Get resources values for single resource type via kubectl get Args: field (str|tuple|list) namespace (None|str): e.g., kube-system, openstack, default. all_namespaces (bool|None): used only when namespace is unspecified resource_names (str|None|list|tuple): e.g., calico-typha resource_type (str): e.g., "deployments.apps", "pod", "service" labels (dict|str\list|tuple): Used only when resource_names are unspecified field_selectors (dict|str|list|tuple): Used only when resource_names are unspecified con_ssh: fail_ok: grep (str|None): grep on cmd output exclude strict **kwargs: table filters for post processing return values Returns (list): key is the name prefix, e.g., service, default, deployment.apps, replicaset.apps value is a list. Each item is a dict rep for a row with lowercase keys. e.g., [{'name': 'cinder-api', 'age': '4d19h', ... }, ...] """ name_filter = None if resource_names and ((all_namespaces and not namespace) or field_selectors or labels): name_filter = {'name': resource_names} resource_names = None code, tables = __get_resource_tables(namespace=namespace, all_namespaces=all_namespaces, resource_types=resource_type, resource_names=resource_names, labels=labels, field_selectors=field_selectors, con_ssh=con_ssh, fail_ok=fail_ok, grep=grep) if code > 0: output = tables if 'NAME ' not in output: # no resource returned return [] output = output.split('\nError from server')[0] tables = table_parser.tables_kube(output) final_table = tables[0] if len(tables) > 1: combined_values = final_table['values'] column_count = len(combined_values) for table_ in tables[1:]: table_values = table_['values'] combined_values = [ combined_values[i] + table_values[i] for i in range(column_count) ] final_table['values'] = combined_values if name_filter: final_table = table_parser.filter_table(final_table, **name_filter) return table_parser.get_multi_values(final_table, fields=field, zip_values=True, strict=strict, exclude=exclude, **kwargs)
def get_endpoints(field='ID', endpoint_id=None, service_name=None, service_type=None, enabled=None, interface="admin", region=None, url=None, strict=False, auth_info=Tenant.get('admin'), con_ssh=None, cli_filter=True): """ Get a list of endpoints with given arguments Args: field (str|list|tuple): valid header of openstack endpoints list table. 'ID' endpoint_id (str): id of the endpoint service_name (str): Service name of endpoint like novaav3, neutron, keystone. vim, heat, swift, etc service_type(str): Service type enabled (str): True/False interface (str): Interface of endpoints. valid entries: admin, internal, public region (str): RegionOne or RegionTwo url (str): url of endpoint strict(bool): auth_info (dict): con_ssh (SSHClient): cli_filter (bool): whether to filter out using cli. e.g., openstack endpoint list --service xxx Returns (list): """ pre_args_str = '' if cli_filter: pre_args_dict = { '--service': service_name, '--interface': interface, '--region': region, } pre_args = [] for key, val in pre_args_dict.items(): if val: pre_args.append('{}={}'.format(key, val)) pre_args_str = ' '.join(pre_args) output = cli.openstack('endpoint list', positional_args=pre_args_str, ssh_client=con_ssh, auth_info=auth_info)[1] if not output.strip(): LOG.warning("No endpoints returned with param: {}".format(pre_args_str)) return [] table_ = table_parser.table(output) kwargs = { 'ID': endpoint_id, 'Service Name': service_name, 'Service Type': service_type, 'Enabled': enabled, 'Interface': interface, 'URL': url, 'Region': region, } kwargs = {k: v for k, v in kwargs.items() if v} return table_parser.get_multi_values(table_, field, strict=strict, regex=True, merge_lines=True, **kwargs)
def get_subclouds(field='name', name=None, avail=None, sync=None, mgmt=None, deploy=None, auth_info=Tenant.get('admin_platform', 'RegionOne'), con_ssh=None, source_openrc=None, rtn_dict=False, evaluate=False, strict=True, regex=False, filter_subclouds=True): """ Get subclouds values Args: field (str | tuple): fields of value to get name (str): subcloud name avail (str): subcloud availability status sync (str): subcloud sync status mgmt (str): subcloud management status deploy (str): subcloud deploy status auth_info (dict): con_ssh (SSHClient): source_openrc (None|bool): rtn_dict (bool): whether to return dict of field/value pairs evaluate (bool): whether to convert value to python data type strict (bool): True to use re.match, False to use re.search regex (bool): whether to use regex to find value(s) filter_subclouds (bool): whether to filter out the subclouds that are not in the --subcloud_list arg Returns (list | dict): when rtn_dict is False, list of values when rtn_dict is True, dict of field/values pairs """ table_ = table_parser.table( cli.dcmanager('subcloud list', ssh_client=con_ssh, auth_info=auth_info, source_openrc=source_openrc)[1]) arg_map = { 'name': name, 'availability': avail, 'sync': sync, 'management': mgmt, 'deploy status': deploy } kwargs = {key: val for key, val in arg_map.items() if val} if filter_subclouds: filtered_subclouds = table_parser.get_values(table_, target_header=field, **kwargs) subcloud_list = ProjVar.get_var('SUBCLOUD_LIST') if subcloud_list: filtered_subclouds = [ subcloud for subcloud in filtered_subclouds if subcloud in subcloud_list ] LOG.info('filtered_subclouds: {}'.format(filtered_subclouds)) return filtered_subclouds else: return table_parser.get_multi_values(table_, field, rtn_dict=rtn_dict, evaluate=evaluate, strict=strict, regex=regex, **kwargs)