Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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 | """ |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 20 | from datetime import datetime |
alshabib | 05fb71f | 2016-12-04 16:08:29 -0800 | [diff] [blame] | 21 | from concurrent.futures import ThreadPoolExecutor |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 22 | |
| 23 | import os |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 24 | import socket |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 25 | from structlog import get_logger |
| 26 | |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 27 | from docker import Client, errors |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 28 | |
| 29 | |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 30 | docker_socket = os.environ.get('DOCKER_SOCK', 'unix://tmp/docker.sock') |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 31 | log = get_logger() |
| 32 | |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 33 | def get_my_containers_name(): |
| 34 | """ |
| 35 | Return the docker containers name in which this process is running. |
| 36 | To look up the container name, we use the container ID extracted from the |
| 37 | $HOSTNAME environment variable (which is set by docker conventions). |
| 38 | :return: String with the docker container name (or None if any issue is |
| 39 | encountered) |
| 40 | """ |
| 41 | my_container_id = os.environ.get('HOSTNAME', None) |
| 42 | |
| 43 | try: |
alshabib | c61999a | 2016-10-27 16:44:27 -0700 | [diff] [blame] | 44 | docker_cli = Client(base_url=docker_socket) |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 45 | info = docker_cli.inspect_container(my_container_id) |
| 46 | |
| 47 | except Exception, e: |
| 48 | log.exception('failed', my_container_id=my_container_id, e=e) |
| 49 | raise |
| 50 | |
| 51 | name = info['Name'].lstrip('/') |
| 52 | |
| 53 | return name |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 54 | |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 55 | def get_all_running_containers(): |
| 56 | try: |
| 57 | docker_cli = Client(base_url=docker_socket) |
| 58 | containers = docker_cli.containers() |
| 59 | |
| 60 | except Exception, e: |
| 61 | log.exception('failed', e=e) |
| 62 | raise |
| 63 | |
| 64 | return containers |
| 65 | |
| 66 | def inspect_container(id): |
| 67 | try: |
| 68 | docker_cli = Client(base_url=docker_socket) |
| 69 | info = docker_cli.inspect_container(id) |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 70 | except Exception, e: |
Khen Nursimulu | 90fc35d | 2017-01-09 08:42:04 -0500 | [diff] [blame] | 71 | log.exception('failed-inspect-container', id=id, e=e) |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 72 | raise |
| 73 | |
| 74 | return info |
| 75 | |