Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 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 | """ |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 20 | from __future__ import absolute_import |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 21 | from datetime import datetime |
| 22 | from concurrent.futures import ThreadPoolExecutor |
| 23 | |
| 24 | import os |
| 25 | import socket |
| 26 | from structlog import get_logger |
| 27 | |
| 28 | from docker import Client, errors |
| 29 | |
| 30 | |
| 31 | docker_socket = os.environ.get('DOCKER_SOCK', 'unix://tmp/docker.sock') |
| 32 | log = get_logger() |
| 33 | |
| 34 | def get_my_containers_name(): |
| 35 | """ |
| 36 | Return the docker containers name in which this process is running. |
| 37 | To look up the container name, we use the container ID extracted from the |
| 38 | $HOSTNAME environment variable (which is set by docker conventions). |
| 39 | :return: String with the docker container name (or None if any issue is |
| 40 | encountered) |
| 41 | """ |
| 42 | my_container_id = os.environ.get('HOSTNAME', None) |
| 43 | |
| 44 | try: |
| 45 | docker_cli = Client(base_url=docker_socket) |
| 46 | info = docker_cli.inspect_container(my_container_id) |
| 47 | |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 48 | except Exception as e: |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 49 | log.exception('failed', my_container_id=my_container_id, e=e) |
| 50 | raise |
| 51 | |
| 52 | name = info['Name'].lstrip('/') |
| 53 | |
| 54 | return name |
| 55 | |
| 56 | def get_all_running_containers(): |
| 57 | try: |
| 58 | docker_cli = Client(base_url=docker_socket) |
| 59 | containers = docker_cli.containers() |
| 60 | |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 61 | except Exception as e: |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 62 | log.exception('failed', e=e) |
| 63 | raise |
| 64 | |
| 65 | return containers |
| 66 | |
| 67 | def inspect_container(id): |
| 68 | try: |
| 69 | docker_cli = Client(base_url=docker_socket) |
| 70 | info = docker_cli.inspect_container(id) |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 71 | except Exception as e: |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 72 | log.exception('failed-inspect-container', id=id, e=e) |
| 73 | raise |
| 74 | |
| 75 | return info |
| 76 | |