• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import logging
7try:
8    import docker
9except ImportError:
10    logging.info("Docker API is not installed in this environment")
11
12env_vars = os.environ
13
14# Default docker socker.
15DOCKER_SOCKET = env_vars.get('DOCKER_SOCKET', '/var/run/docker.sock')
16
17# This the default IP where the docker daemon is running on the Satlab.
18DEFAULT_DOCKER_SERVER_IP = '192.168.231.1'
19# This the default IP where the docker daemon is listening on the Satlab.
20DEFAULT_DOCKER_TCP_SERVER_PORT = '2375'
21# Optional docker tcp ip address/port dockerd listens to.
22DOCKER_TCP_SERVER_IP = env_vars.get('DOCKER_TCP_SERVER_IP',
23                                    DEFAULT_DOCKER_SERVER_IP)
24DOCKER_TCP_SERVER_PORT = env_vars.get('DOCKER_TCP_SERVER_PORT',
25                                      DEFAULT_DOCKER_TCP_SERVER_PORT)
26
27
28def get_docker_client(timeout=300):
29    """
30    Get the client of the host Docker server either via default Docker socket or TCP connection.
31    """
32    # Use default TCP connection IP to create docker client if docker socket(
33    # /var/run/docker.sock) doesn't exists on the machine or when TCP connection IP
34    # is not default IP, otherwise use docker socket file to create docker client.
35    if os.path.exists(DOCKER_SOCKET
36                      ) and DEFAULT_DOCKER_SERVER_IP == DOCKER_TCP_SERVER_IP:
37        client = docker.from_env(timeout=timeout)
38    else:
39        tcp_connection = "tcp://{}:{}".format(DOCKER_TCP_SERVER_IP,
40                                              DOCKER_TCP_SERVER_PORT)
41        client = docker.DockerClient(base_url=tcp_connection, timeout=timeout)
42    return client
43
44
45def get_running_containers(client=None):
46    """
47    Return the names of running containers
48    """
49    if client is None:
50        client = get_docker_client()
51    containers = client.containers.list()
52    return [c.name for c in containers]
53
54
55def get_container_networks(container_name, client=None):
56    """
57    Return the list of networks of the container. Return [] if container is not found.
58    """
59    if client is None:
60        client = get_docker_client()
61    containers = get_running_containers(client)
62    if container_name not in containers:
63        return []
64    else:
65        container = client.containers.get(container_name)
66        return container.attrs['NetworkSettings']['Networks'].keys()
67
68
69def get_container_ip(container_name, client=None):
70    """
71    Return the IP Address of networks of the container. Return None if container is not found.
72    """
73    if client is None:
74        client = get_docker_client()
75    try:
76        container = client.containers.get(container_name)
77        if container and container.status == 'running':
78            container_network = os.environ.get("DOCKER_DEFAULT_NETWORK",
79                                               "default_satlab")
80            return container.attrs['NetworkSettings']['Networks'][
81                    container_network]['IPAddress']
82        logging.exception("Servod container %s found but not running",
83                          container_name)
84    except docker.errors.APIError:
85        logging.exception("Failed to access servod container.")
86    except docker.errors.NotFound:
87        logging.exception("Servod container %s Not Found", container_name)
88    return None
89