示例#1
0
def table_kube(output_lines, merge_lines=False):
    """
    Parse single table from kubectl output.

    Args:
        output_lines (str|list): output of kubectl cmd
        merge_lines (bool): if a value spans on multiple lines, whether to
        join them or return as list

    Return dict with list of column names in 'headers' key and
    rows in 'values' key.
    """
    table_ = {'headers': [], 'values': []}

    if not isinstance(output_lines, list):
        output_lines = output_lines.split('\n')

    if not output_lines or len(output_lines) < 2:
        return table_

    if not output_lines[-1]:
        # skip last line if empty (just newline at the end)
        output_lines = output_lines[:-1]
    if not output_lines[0]:
        output_lines = output_lines[1:]

    if not output_lines:
        return table_

    for i in range(len(output_lines)):
        line = output_lines[i]
        if '  ' not in line:
            LOG.debug('Invalid kube table line: {}'.format(line))
        else:
            header_row = line
            output_lines = output_lines[i + 1:]
            break
    else:
        return table_

    table_['headers'] = re.split(r'\s[\s]+', header_row)

    m = re.finditer(kute_sep, header_row)
    starts = [0] + [sep.start() + 2 for sep in m]
    col_count = len(starts)
    for line in output_lines:
        row = []
        indices = list(starts) + [len(line)]
        for i in range(col_count):
            row.append(line[indices[i]:indices[i + 1]].strip())
        table_['values'].append(row)

    if table_['values'] and len(table_['values'][0]) != len(table_['headers']):
        raise exceptions.CommonError(
            'Unable to parse given lines: \n{}'.format(output_lines))

    table_['values'] = __convert_multilines_values(table_['values'],
                                                   merge_lines=merge_lines)

    return table_
示例#2
0
def get_custom_heat_files(file_name,
                          file_dir=HEAT_CUSTOM_TEMPLATES,
                          cli_client=None):
    """

    Args:
        file_name:
        file_dir:
        cli_client:

    Returns:

    """
    file_path = '{}/{}'.format(file_dir, file_name)

    if cli_client is None:
        cli_client = get_cli_client()

    if not cli_client.file_exists(file_path=file_path):
        LOG.debug('Create userdata directory if not already exists')
        cmd = 'mkdir -p {}'.format(file_dir)
        cli_client.exec_cmd(cmd, fail_ok=False)
        source_file = TestServerPath.CUSTOM_HEAT_TEMPLATES + file_name
        dest_path = common.scp_from_test_server_to_user_file_dir(
            source_path=source_file,
            dest_dir=file_dir,
            dest_name=file_name,
            timeout=300,
            con_ssh=cli_client)
        if dest_path is None:
            raise exceptions.CommonError(
                "Heat template file {} does not exist after download".format(
                    file_path))

    return file_path
示例#3
0
def get_image_id_from_name(name=None,
                           strict=False,
                           fail_ok=True,
                           con_ssh=None,
                           auth_info=None):
    """

    Args:
        name (list or str):
        strict:
        fail_ok (bool): whether to raise exception if no image found with
        provided name
        con_ssh:
        auth_info (dict:

    Returns:
        Return a random image_id that match the name. else return an empty
        string

    """
    if name is None:
        name = GuestImages.DEFAULT['guest']

    matching_images = get_images(name=name,
                                 auth_info=auth_info,
                                 con_ssh=con_ssh,
                                 strict=strict)
    if not matching_images:
        image_id = ''
        msg = "No existing image found with name: {}".format(name)
        LOG.warning(msg)
        if not fail_ok:
            raise exceptions.CommonError(msg)
    else:
        image_id = matching_images[0]
        if len(matching_images) > 1:
            LOG.warning('More than one glace image found with name {}. '
                        'Select {}.'.format(name, image_id))

    return image_id
示例#4
0
def _get_stress_ng_heat(con_ssh=None):
    """
    copy the cloud-config userdata to TiS server.
    This userdata adds sysadmin/li69nux user to guest

    Args:
        con_ssh (SSHClient):

    Returns (str): TiS filepath of the userdata

    """
    file_dir = StxPath.CUSTOM_HEAT_TEMPLATES
    file_name = HeatTemplate.STRESS_NG
    file_path = file_dir + file_name

    if con_ssh is None:
        con_ssh = ControllerClient.get_active_controller()

    if not con_ssh.file_exists(file_path=file_path):
        LOG.debug('Create userdata directory if not already exists')
        cmd = 'mkdir -p {}'.format(file_dir)
        con_ssh.exec_cmd(cmd, fail_ok=False)

        source_file = TestServerPath.CUSTOM_HEAT_TEMPLATES + file_name

        dest_path = common.scp_from_test_server_to_active_controller(
            source_path=source_file,
            dest_dir=file_dir,
            dest_name=file_name,
            con_ssh=con_ssh)

        if dest_path is None:
            raise exceptions.CommonError(
                "Heat template file {} does not exist after download".format(
                    file_path))

    # tenant nets names were hardcoded in heat file. They need to be updated when systems don't have those networks.
    # Update heat file if less than 3 tenant-nets configured.
    tenant_nets = network_helper.get_tenant_net_ids(field='name')
    net_count = len(tenant_nets)
    if net_count <= 3:
        LOG.info(
            "Less than 3 tenant networks configured. Update heat template.")
        con_ssh.exec_cmd("sed -i 's/tenant2-net3/tenant2-net{}/g' {}".format(
            net_count - 1, file_path))
        if net_count <= 2:
            con_ssh.exec_cmd(
                "sed -i 's/tenant2-net2/tenant2-net{}/g' {}".format(
                    net_count - 1, file_path))
            if net_count <= 1:
                con_ssh.exec_cmd(
                    "sed -i 's/tenant2-net1/tenant2-net{}/g' {}".format(
                        net_count - 1, file_path))

    # update heat file for multi-region system
    from consts.proj_vars import ProjVar
    from consts.stx import MULTI_REGION_MAP
    region = ProjVar.get_var("REGION")
    if region != 'RegionOne' and region in MULTI_REGION_MAP:
        region_str = MULTI_REGION_MAP.get(region)
        con_ssh.exec_cmd("sed -i 's/tenant2-net/tenant2{}-net/g' {}".format(
            region_str, file_path))
        con_ssh.exec_cmd(
            "sed -i 's/tenant2-mgmt-net/tenant2{}-mgmt-net/g' {}".format(
                region_str, file_path))

    if not system_helper.is_avs():
        con_ssh.exec_cmd("sed -i 's/avp/virtio/g' {}".format(file_path))

    return file_path
示例#5
0
def scp_guest_image(img_os='ubuntu_14',
                    dest_dir=None,
                    timeout=3600,
                    con_ssh=None):
    """

    Args:
        img_os (str): guest image os type. valid values: ubuntu, centos_7,
            centos_6
        dest_dir (str): where to save the downloaded image. Default is
            '~/images'
        timeout (int)
        con_ssh (SSHClient):

    Returns (str): full file name of downloaded image. e.g.,
    '~/images/ubuntu_14.qcow2'

    """
    valid_img_os_types = list(GuestImages.IMAGE_FILES.keys())

    if img_os not in valid_img_os_types:
        raise ValueError(
            "Invalid guest image OS type provided. Valid values: {}".format(
                valid_img_os_types))

    if not dest_dir:
        dest_dir = GuestImages.DEFAULT['image_dir']

    LOG.info("Downloading guest image from test server...")
    dest_name = GuestImages.IMAGE_FILES[img_os][2]
    ts_source_name = GuestImages.IMAGE_FILES[img_os][0]
    if con_ssh is None:
        con_ssh = get_cli_client(central_region=True)

    if ts_source_name:
        # img saved on test server. scp from test server
        source_path = '{}/{}'.format(
            GuestImages.DEFAULT['image_dir_file_server'], ts_source_name)
        dest_path = common.scp_from_test_server_to_user_file_dir(
            source_path=source_path,
            dest_dir=dest_dir,
            dest_name=dest_name,
            timeout=timeout,
            con_ssh=con_ssh)
    else:
        # scp from tis system if needed
        dest_path = '{}/{}'.format(dest_dir, dest_name)
        if ProjVar.get_var(
                'REMOTE_CLI') and not con_ssh.file_exists(dest_path):
            tis_source_path = '{}/{}'.format(GuestImages.DEFAULT['image_dir'],
                                             dest_name)
            common.scp_from_active_controller_to_localhost(
                source_path=tis_source_path,
                dest_path=dest_path,
                timeout=timeout)

    if not con_ssh.file_exists(dest_path):
        raise exceptions.CommonError(
            "image {} does not exist after download".format(dest_path))

    LOG.info("{} image downloaded successfully and saved to {}".format(
        img_os, dest_path))
    return dest_path