utils.docker

docker

Common code useful for docker.

Example Usage: from utils import docker from utils.docker import docker

 1#!/usr/bin/env python3
 2"""
 3docker
 4
 5Common code useful for docker.
 6
 7Example Usage:
 8    from utils import docker
 9    from utils.docker import docker
10"""
11
12try:
13    import loggy
14except ImportError:
15    from utils import loggy
16try:
17    from .common import subprocess_long as _long_run
18except ImportError:
19    from utils.common import subprocess_long as _long_run
20
21
22def docker(*args) -> bool:
23    """
24    Shell out to the Docker CLI.
25
26    It is expected that the commands to send are in a list.
27
28    Returns: True/False
29    """
30    cmd = ["docker"] + list(args)
31    output = _long_run(' '.join(cmd), check=False)
32    loggy.info(f"docker.docker(): stdout: {output.stdout}")
33    loggy.info(f"docker.docker(): stderr: {output.stderr}")
34    loggy.info(f"docker.docker(): return: {str(output.returncode)}")
35
36    if output.returncode != 0:
37        loggy.info("docker.docker(): Error.")
38        return False
39
40    return True
41
42
43def tag(container, tag) -> bool:
44    """
45    tag()
46
47    Tag a locally built container.
48    Example: tag(container="123456789.dkr.ecr.us-east-1.amazonaws.com/mirrored/timothy:1234", tag="latest")
49
50    container: String containing existing local container with tag "container:tag"
51    tag: String containing new tag to add to the local container
52
53    Returns: True/False
54    """
55    loggy.info(f"docker.tag(): Tagging {container} with {tag}")
56    if ':' not in container:
57        raise Exception("docker.tag(): container must include tag")
58
59    _c = container.split(':')[0]
60    return docker("tag", container, f"{_c}:{tag}")
61
62
63def login(username, password, repo) -> bool:
64    """
65    login()
66
67    Log in to an external repo (i.e. ECR)
68
69    username: String
70    password: String
71    repo: String
72    """
73    return docker("login", "--username", username, "--password", password, repo)
74
75
76def logout(repo=None) -> bool:
77    """
78    logout()
79
80    Log out of an external repo
81
82    ecr: String. If None, log out everywhere
83    """
84    return docker("logout", repo)
def docker(*args) -> bool:
23def docker(*args) -> bool:
24    """
25    Shell out to the Docker CLI.
26
27    It is expected that the commands to send are in a list.
28
29    Returns: True/False
30    """
31    cmd = ["docker"] + list(args)
32    output = _long_run(' '.join(cmd), check=False)
33    loggy.info(f"docker.docker(): stdout: {output.stdout}")
34    loggy.info(f"docker.docker(): stderr: {output.stderr}")
35    loggy.info(f"docker.docker(): return: {str(output.returncode)}")
36
37    if output.returncode != 0:
38        loggy.info("docker.docker(): Error.")
39        return False
40
41    return True

Shell out to the Docker CLI.

It is expected that the commands to send are in a list.

Returns: True/False

def tag(container, tag) -> bool:
44def tag(container, tag) -> bool:
45    """
46    tag()
47
48    Tag a locally built container.
49    Example: tag(container="123456789.dkr.ecr.us-east-1.amazonaws.com/mirrored/timothy:1234", tag="latest")
50
51    container: String containing existing local container with tag "container:tag"
52    tag: String containing new tag to add to the local container
53
54    Returns: True/False
55    """
56    loggy.info(f"docker.tag(): Tagging {container} with {tag}")
57    if ':' not in container:
58        raise Exception("docker.tag(): container must include tag")
59
60    _c = container.split(':')[0]
61    return docker("tag", container, f"{_c}:{tag}")

tag()

Tag a locally built container. Example: tag(container="123456789.dkr.ecr.us-east-1.amazonaws.com/mirrored/timothy:1234", tag="latest")

container: String containing existing local container with tag "container:tag" tag: String containing new tag to add to the local container

Returns: True/False

def login(username, password, repo) -> bool:
64def login(username, password, repo) -> bool:
65    """
66    login()
67
68    Log in to an external repo (i.e. ECR)
69
70    username: String
71    password: String
72    repo: String
73    """
74    return docker("login", "--username", username, "--password", password, repo)

login()

Log in to an external repo (i.e. ECR)

username: String password: String repo: String

def logout(repo=None) -> bool:
77def logout(repo=None) -> bool:
78    """
79    logout()
80
81    Log out of an external repo
82
83    ecr: String. If None, log out everywhere
84    """
85    return docker("logout", repo)

logout()

Log out of an external repo

ecr: String. If None, log out everywhere