Example #1
0
def upload_helm_charts(tar_file,
                       repo=None,
                       delete_first=False,
                       con_ssh=None,
                       timeout=120,
                       fail_ok=False):
    """
    Upload helm charts via helm-upload cmd
    Args:
        tar_file:
        repo
        delete_first:
        con_ssh:
        timeout:
        fail_ok:

    Returns (tuple):
        (0, <path_to_charts>)
        (1, <std_err>)
        (2, <hostname for host that does not have helm charts in expected dir>)

    """
    if not con_ssh:
        con_ssh = ControllerClient.get_active_controller()

    helm_dir = os.path.normpath(StxPath.HELM_CHARTS_DIR)
    if not repo:
        repo = 'starlingx'
    file_path = os.path.join(helm_dir, repo, os.path.basename(tar_file))
    current_host = con_ssh.get_hostname()
    controllers = [current_host]
    if not system_helper.is_aio_simplex(con_ssh=con_ssh):
        con_name = 'controller-1' if controllers[
                                         0] == 'controller-0' else \
            'controller-0'
        controllers.append(con_name)

    if delete_first:
        for host in controllers:
            with host_helper.ssh_to_host(hostname=host,
                                         con_ssh=con_ssh) as host_ssh:
                if host_ssh.file_exists(file_path):
                    host_ssh.exec_sudo_cmd('rm -f {}'.format(file_path))

    code, output = exec_helm_upload_cmd(tarball=tar_file,
                                        repo=repo,
                                        timeout=timeout,
                                        con_ssh=con_ssh,
                                        fail_ok=fail_ok)
    if code != 0:
        return 1, output

    file_exist = con_ssh.file_exists(file_path)
    if not file_exist:
        raise exceptions.ContainerError(
            "{} not found on {} after helm-upload".format(
                file_path, current_host))

    LOG.info("Helm charts {} uploaded successfully".format(file_path))
    return 0, file_path
Example #2
0
def tag_docker_image(source_image,
                     target_name,
                     source_tag=None,
                     target_tag=None,
                     con_ssh=None,
                     timeout=300,
                     fail_ok=False):
    """
    Tag docker image via docker image tag. Verify image is tagged via docker
    image list.
    Args:
        source_image:
        target_name:
        source_tag:
        target_tag:
        con_ssh:
        timeout:
        fail_ok:

    Returns:
        (0, <target_args>)
        (1, <std_err>)

    """
    source_args = source_image.strip()
    if source_tag:
        source_args += ':{}'.format(source_tag)

    target_args = target_name.strip()
    if target_tag:
        target_args += ':{}'.format(target_tag)

    LOG.info("Tag docker image {} as {}".format(source_args, target_args))
    args = '{} {}'.format(source_args, target_args)
    code, out = exec_docker_cmd('image tag',
                                args,
                                timeout=timeout,
                                fail_ok=fail_ok,
                                con_ssh=con_ssh)
    if code != 0:
        return 1, out

    if not get_docker_images(
            repo=target_name, tag=target_tag, con_ssh=con_ssh, fail_ok=False):
        raise exceptions.ContainerError(
            "Docker image {} is not listed after tagging {}".format(
                target_name, source_image))

    LOG.info('docker image {} successfully tagged as {}.'.format(
        source_args, target_args))
    return 0, target_args
Example #3
0
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
Example #4
0
def wait_for_apps_status(apps,
                         status,
                         timeout=360,
                         check_interval=5,
                         fail_ok=False,
                         con_ssh=None,
                         auth_info=Tenant.get('admin_platform')):
    """
    Wait for applications to reach expected status via system application-list
    Args:
        apps:
        status:
        timeout:
        check_interval:
        fail_ok:
        con_ssh:
        auth_info:

    Returns (tuple):

    """
    status = '' if not status else status
    if isinstance(apps, str):
        apps = [apps]
    apps_to_check = list(apps)
    check_failed = []
    end_time = time.time() + timeout

    LOG.info("Wait for {} application(s) to reach status: {}".format(
        apps, status))
    while time.time() < end_time:
        apps_status = get_apps(application=apps_to_check,
                               field=('application', 'status'),
                               con_ssh=con_ssh,
                               auth_info=auth_info)
        apps_status = {item[0]: item[1] for item in apps_status if item}

        checked = []
        for app in apps_to_check:
            current_app_status = apps_status.get(app, '')
            if current_app_status == status:
                checked.append(app)
            elif current_app_status.endswith('ed'):
                check_failed.append(app)
                checked.append(app)

        apps_to_check = list(set(apps_to_check) - set(checked))
        if not apps_to_check:
            if check_failed:
                msg = '{} failed to reach status - {}'.format(
                    check_failed, status)
                LOG.warning(msg)
                if fail_ok:
                    return False, check_failed
                else:
                    raise exceptions.ContainerError(msg)

            LOG.info("{} reached expected status {}".format(apps, status))
            return True, None

        time.sleep(check_interval)

    check_failed += apps_to_check
    msg = '{} did not reach status {} within {}s'.format(
        check_failed, status, timeout)
    LOG.warning(msg)
    if fail_ok:
        return False, check_failed
    raise exceptions.ContainerError(msg)