Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 1 | # |
| 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 | """ |
| 18 | Some docker related convenience functions |
| 19 | """ |
| 20 | |
| 21 | import os |
| 22 | from structlog import get_logger |
| 23 | |
| 24 | from docker import Client |
| 25 | |
| 26 | |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 27 | docker_socket = os.environ.get('DOCKER_SOCK', 'unix://tmp/docker.sock') |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 28 | log = get_logger() |
| 29 | |
| 30 | |
| 31 | def 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: |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 42 | docker_cli = Client(base_url=docker_socket) |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 43 | 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 |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 52 | |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 53 | def 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 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 67 | def 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: |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 75 | docker_cli = Client(base_url=docker_socket) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 76 | docker_cli.create_network(name) |
| 77 | networking_config = docker_cli.create_networking_config({ |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 78 | name : docker_cli.create_endpoint_config(links=links) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 79 | }) |
| 80 | except Exception, e: |
| 81 | log.exception('failed network creation', name, e=e) |
| 82 | raise |
| 83 | |
| 84 | return networking_config |
| 85 | |
| 86 | |
| 87 | def 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: |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 95 | docker_cli = Client(base_url=docker_socket) |
| 96 | container = docker_cli.create_container(**args) |
| 97 | response = docker_cli.start(container=container.get('Id')) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 98 | except Exception, e: |
| 99 | log.exception('failed', e=e) |
| 100 | raise |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 101 | return response |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 102 | |