summaryrefslogtreecommitdiffstats
path: root/python-dockering/dockering/config_building.py
blob: 144131e1745ec6713d5954c524e61e12d9cedab4 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# org.onap.dcae
# ================================================================================
# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

"""
Abstraction in Docker container configuration
"""
import six
from dockering import utils
from dockering.exceptions import DockerConstructionError


#
# Methods to build container envs
#

def create_envs_healthcheck(docker_config, default_interval="15s",
        default_timeout="1s"):
    """Extract health check environment variables for Docker containers

    Parameters
    ----------
    docker_config: dict where there's an entry called "healthcheck"

    Returns
    -------
    dict of Docker envs for healthcheck
    """
    # TODO: This has been shamefully lifted from the dcae-cli and should probably
    # shared as a library. The unit tests are there. The difference is that
    # there are defaults that are passed in here but the defaults should really
    # come from the component spec definition. The issue is that nothing forwards
    # those defaults.

    envs = dict()
    hc = docker_config["healthcheck"]

    # NOTE: For the multiple port, schema scenario, you can explicitly set port
    # to schema. For example if image EXPOSE 8080, SERVICE_8080_CHECK_HTTP works.
    # https://github.com/gliderlabs/registrator/issues/311

    if hc["type"] == "http":
        envs["SERVICE_CHECK_HTTP"] = hc["endpoint"]
    elif hc["type"] == "https":
        # WATCH: HTTPS health checks don't work. Seems like Registrator bug.
        # Submitted issue https://github.com/gliderlabs/registrator/issues/516
        envs["SERVICE_CHECK_HTTPS"] = hc["endpoint"]
        # Went with one of the suggestiong from the posted issue above. This
        # author is skeptical whether https healthchecks will ever work with
        # server cert verification because the hostname is actually the ip
        # address.
        envs["SERVICE_CHECK_TLS_SKIP_VERIFY"] = "true"
        utils.logger.warning("Https-based health checks may not work because Registrator issue #516")
    elif hc["type"] == "script":
        envs["SERVICE_CHECK_SCRIPT"] = hc["script"]
    elif hc["type"] == "docker":
        # Note this is only supported in the AT&T open source version of registrator
        envs["SERVICE_CHECK_DOCKER_SCRIPT"] = hc["script"]
    else:
        # You should never get here but not having an else block feels weird
        raise DockerConstructionError("Unexpected health check type: {0}".format(hc["type"]))

    envs["SERVICE_CHECK_INTERVAL"] = hc.get("interval", default_interval)
    envs["SERVICE_CHECK_TIMEOUT"] = hc.get("timeout", default_timeout)

    return envs


def create_envs(service_component_name, *envs):
    """Merge all environment variables maps

    Creates a complete environment variables map that is to be used for creating
    the container.

    Args:
    -----
    envs: Arbitrary list of dicts where each dict is of the structure:

        {
            <environment variable name>: <environment variable value>,
            <environment variable name>: <environment variable value>,
            ...
        }

    Returns:
    --------
    Dict of all environment variable name to environment variable value
    """
    master_envs = { "HOSTNAME": service_component_name,
                    # For Registrator to register with generated name and not the
                    # image name
                    "SERVICE_NAME": service_component_name }
    for envs_map in envs:
        master_envs.update(envs_map)
    return master_envs


#
# Methods for volume bindings
#

def _parse_volumes_param(volumes):
    """Parse volumes details for Docker containers from blueprint

    Takes in a list of dicts that contains Docker volume info and
    transforms them into docker-py compliant (unflattened) data structures.
    Look for the `volumes` parameters under the `run` method on
    [this page](https://docker-py.readthedocs.io/en/stable/containers.html)

    Args:
        volumes (list): List of

            {
              "host": {
                "path": <target path on host>
                },
              "container": {
                "bind": <target path in container>,
                "mode": <read/write>
              }
            }

    Returns:
        dict of the form

            {
              <target path on host>: {
                "bind": <target path in container>,
                "mode": <read/write>
              }
            }

        if volumes is None then returns None
    """
    if volumes:
        return dict([ (vol["host"]["path"], vol["container"]) for vol in volumes ])
    else:
        return None


#
# Utility methods used to help build the inputs to create the host_config
#

def add_host_config_params_volumes(volumes=None, host_config_params=None):
    """Add volumes input params

    Args:
    -----
    volumes (list): List of

            {
              "host": {
                "path": <target path on host>
                },
              "container": {
                "bind": <target path in container>,
                "mode": <read/write>
              }
            }

    host_config_params (dict): Target dict to accumulate host config inputs

    Returns:
    --------
    Updated host_config_params
    """
# TODO: USE parse_volumes_param here!
    if host_config_params == None:
        host_config_params = {}

    host_config_params["binds"] = _parse_volumes_param(volumes)
    return host_config_params

def add_host_config_params_ports(ports=None, host_config_params=None):
    """Add ports input params

    Args:
    -----
    ports (list): Each port mapping entry is of the form

        "<container ports>:<host port>"

    host_config_params (dict): Target dict to accumulate host config inputs

    Returns:
    --------
    Updated host_config_params
    """
    if host_config_params == None:
        host_config_params = {}

    if ports:
        ports = [ port.split(":") for port in ports ]
        port_bindings = { port[0]: { "HostPort": port[1] }  for port in ports }
        host_config_params["port_bindings"] = port_bindings
        host_config_params["publish_all_ports"] = False
    else:
        host_config_params["publish_all_ports"] = True

    return host_config_params

def add_host_config_params_dns(docker_host, host_config_params=None):
    """Add dns input params

    This is not a generic implementation. This method will setup dns with the
    expectation that a local consul agent is running on the docker host and will
    service the dns requests.

    Args:
    -----
    docker_host (string): Docker host ip address which will be used as the dns server
    host_config_params (dict): Target dict to accumulate host config inputs

    Returns:
    --------
    Updated host_config_params
    """
    if host_config_params == None:
        host_config_params = {}

    host_config_params["dns"] = [docker_host]
    host_config_params["dns_search"] = ["service.consul"]
    host_config_params["extra_hosts"] = { "consul": docker_host }
    return host_config_params


def _get_container_ports(host_config_params):
    """Grab list of container ports from host config params"""
    if "port_bindings" in host_config_params:
        return list(six.iterkeys(host_config_params["port_bindings"]))
    else:
        return None

def create_container_config(client, image, envs, host_config_params, tty=False):
    """Create docker container config

    Args:
    -----
    envs (dict): dict of environment variables to pass into the docker containers.
        Gets passed into docker-py.create_container call
    host_config_params (dict): Dict of input parameters to the docker-py
        "create_host_config" method call
    """
    # This is the 1.10.6 approach to binding volumes
    # http://docker-py.readthedocs.io/en/1.10.6/volumes.html
    volumes = host_config_params.get("bind", None)
    target_volumes = [ target["bind"] for target in volumes.values() ] \
            if volumes else None

    host_config = client.create_host_config(**host_config_params)

    ports = _get_container_ports(host_config_params)

    command = "" # This is required...
    config = client.create_container_config(image, command, detach=True, tty=tty,
            host_config=host_config, ports=ports,
            environment=envs, volumes=target_volumes)

    utils.logger.info("Docker container config: {0}".format(config))

    return config