summaryrefslogtreecommitdiffstats
path: root/cns-libs/cnslibs/common/docker_libs.py
blob: c47f8b774ed86fe1d64d8d3dcf1f43ccf3fa1c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from glusto.core import Glusto as g


DOCKER_FILE_PATH = "/etc/sysconfig/docker"


def _docker_update_registry(hostname, registry, registry_type):
    '''
      This function updates docker registry
      Args:
         hostname (str): hostname on which want to setup
                         the docker
         registry (str): add regsitry url that needs to be added
                         in docker file.
         ex: "ADD_REGISTRY='--add-registry registry.access.stage.redhat.com'"
         registry_type (str): type of registry
                              ex: add or insecure
    '''
    try:
        conn = g.rpyc_get_connection(hostname, user="root")
        if conn is None:
            g.log.error("Failed to get rpyc connection of node %s"
                        % hostname)
            return False

        if not conn.modules.os.path.exists(DOCKER_FILE_PATH):
            g.log.error("Unable to locate %s in node %s"
                        % (DOCKER_FILE_PATH, hostname))
            return False

        registry_flag = False
        lookup_str = "%s_REGISTRY=" % registry_type.upper()
        for line in conn.modules.fileinput.input(
                DOCKER_FILE_PATH, inplace=True):
            if lookup_str in line:
                registry_flag = True
                if registry not in line:
                    line = line if "#" in line else "#" + line
                    conn.modules.sys.stdout.write(line)
                    conn.modules.sys.stdout.write(registry)
                    continue
            conn.modules.sys.stdout.write(line)

        if not registry_flag:
            with conn.builtin.open(DOCKER_FILE_PATH, 'a+') as docker_file:
                docker_file.write(registry + '\n')

    except Exception as err:
        g.log.error("failed to edit docker file with %s-registry "
                    "%s on %s" % (registry_type, err, hostname))
        return False
    finally:
        g.rpyc_close_connection(hostname, user="root")

    g.log.info("Sucessfully edited docker file with %s-registry "
               "on %s" % (registry_type, hostname))
    return True


def docker_add_registry(hostname, registry_url):
    '''
     This function edits /etc/sysconfig/docker file with ADD_REGISTRY
     Args:
         hostname (str): hostname on which want to setup
                         the docker
         registry_url (str): add regsitry url that needs to be added
                             in docker file.
         ex: "ADD_REGISTRY='--add-registry registry.access.stage.redhat.com'"
     Returns:
         bool: True if successful,
               otherwise False
    '''
    return _docker_update_registry(hostname, registry_url, 'add')


def docker_insecure_registry(hostname, registry_url):
    '''
     This function edits /etc/sysconfig/docker file with INSECURE_REGISTRY
     Args:
         hostname (str): hostname on which want to setup
                         the docker
         registry_url (str): insecure registry url that needs to be added
                             in docker file.
         ex: "INSECURE_REGISTRY=
               '--insecure-registry registry.access.stage.redhat.com'"
     Returns:
         bool: True if successful,
               otherwise False

    '''
    return _docker_update_registry(hostname, registry_url, 'insecure')