blob: 6db4ebe61258656140ce9b83066da9385ddfe229 [file] [log] [blame]
Zsolt Haraszti86be6f12016-09-27 09:56:49 -07001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Some docker related convenience functions
19"""
20
21import os
22from structlog import get_logger
23
24from docker import Client
25
26
alshabibc61999a2016-10-27 16:44:27 -070027docker_socket = os.environ.get('DOCKER_SOCK', 'unix://tmp/docker.sock')
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070028log = get_logger()
29
30
31def get_my_containers_name():
32 """
33 Return the docker containers name in which this process is running.
34 To look up the container name, we use the container ID extracted from the
35 $HOSTNAME environment variable (which is set by docker conventions).
36 :return: String with the docker container name (or None if any issue is
37 encountered)
38 """
39 my_container_id = os.environ.get('HOSTNAME', None)
40
41 try:
alshabibc61999a2016-10-27 16:44:27 -070042 docker_cli = Client(base_url=docker_socket)
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070043 info = docker_cli.inspect_container(my_container_id)
44
45 except Exception, e:
46 log.exception('failed', my_container_id=my_container_id, e=e)
47 raise
48
49 name = info['Name'].lstrip('/')
50
51 return name
alshabibc67ee3a2016-10-25 23:24:03 -070052
alshabibc61999a2016-10-27 16:44:27 -070053def create_host_config(volumes, ports):
54 try:
55 port_bindings = { ports[0] : None }
56 binds = ['{0}:{1}'.format(k, v) for k, v in volumes.iteritems()]
57 docker_cli = Client(base_url=docker_socket)
58 host_config = docker_cli.create_host_config(binds=binds,
59 port_bindings=port_bindings)
60 except Exception, e:
61 log.exception('failed host config creation', volumes, ports, e=e)
62 raise
63
64 return host_config
65
66
alshabibc67ee3a2016-10-25 23:24:03 -070067def create_container_network(name, links):
68 """
69 Creates a container networks based on a set of containers.
70 :param name: the network name
71 :param links: the set of containers to link
72 :return: a network configuration
73 """
74 try:
alshabibc61999a2016-10-27 16:44:27 -070075 docker_cli = Client(base_url=docker_socket)
alshabibc67ee3a2016-10-25 23:24:03 -070076 docker_cli.create_network(name)
77 networking_config = docker_cli.create_networking_config({
alshabibc61999a2016-10-27 16:44:27 -070078 name : docker_cli.create_endpoint_config(links=links)
alshabibc67ee3a2016-10-25 23:24:03 -070079 })
80 except Exception, e:
81 log.exception('failed network creation', name, e=e)
82 raise
83
84 return networking_config
85
86
87def start_container(args):
88 """
89 Starts a requested container with the appropriate configuration.
90 :param args: contains arguments for container creation
91 (see https://docker-py.readthedocs.io/en/stable/api/#create_container)
92 :return: the containers name
93 """
94 try:
alshabibc61999a2016-10-27 16:44:27 -070095 docker_cli = Client(base_url=docker_socket)
96 container = docker_cli.create_container(**args)
97 response = docker_cli.start(container=container.get('Id'))
alshabibc67ee3a2016-10-25 23:24:03 -070098 except Exception, e:
99 log.exception('failed', e=e)
100 raise
alshabibc61999a2016-10-27 16:44:27 -0700101 return response
alshabibc67ee3a2016-10-25 23:24:03 -0700102