コード例 #1
0
ファイル: container_helper.py プロジェクト: ashishshah1/test
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
コード例 #2
0
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)