Ejemplo n.º 1
0
def is_container_running(container_id):
    docker_api = docker_client.api()
    try:
        inspect = docker_api.inspect_container(container_id)
        return inspect['State']['Running']
    except:
        return False
Ejemplo n.º 2
0
def run_command_in_container(command, container_id):
    docker_api = docker_client.api()
    try:
        exec_id = docker_api.exec_create(container_id, command)
        docker_api.exec_start(exec_id['Id'])
    except Exception as e:
        get_logger().exception(e)
Ejemplo n.º 3
0
 def GET(self, image_name):
     try:
         docker_api = docker_client.api()
         image_info = json.dumps(docker_api.images(image_name))
         return self.status_ok({'image_info': '{image_info}'.format(**locals())})
     except Exception as e:
         return self.status_exception("Cannot get info about image.", e)
Ejemplo n.º 4
0
def is_container_running(container_id):
    docker_api = docker_client.api()
    try:
        inspect = docker_api.inspect_container(container_id)
        return inspect['State']['Running']
    except:
        return False
Ejemplo n.º 5
0
def run_command_in_container(command, container_id):
    docker_api = docker_client.api()
    try:
        exec_id = docker_api.exec_create(container_id, command)
        docker_api.exec_start(exec_id['Id'])
    except:
        traceback.print_exc()
Ejemplo n.º 6
0
def run_command_in_container(command, container_id):
    docker_api = docker_client.api()
    try:
        exec_id = docker_api.exec_create(container_id, command)
        docker_api.exec_start(exec_id['Id'])
    except Exception as e:
        get_logger().exception(e)
Ejemplo n.º 7
0
 def GET(self, image_name):
     try:
         docker_api = docker_client.api()
         image_info = json.dumps(docker_api.images(image_name))
         return self.status_ok({'image_info': '{image_info}'.format(**locals())})
     except Exception as e:
         return self.status_exception("Cannot get info about image.", e)
Ejemplo n.º 8
0
def get_env(container_id, key):
    docker_api = docker_client.api()
    docker_inspect = docker_api.inspect_container(container_id)
    for env_var in docker_inspect['Config']['Env']:
        env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2]
        if env_key == key:
            return env_value
    return None
Ejemplo n.º 9
0
def get_container_ssh_address(container_id):
    docker_api = docker_client.api()

    docker_inspect = docker_api.inspect_container(container_id)
    ssh_port = docker_inspect['NetworkSettings']['Ports']['22/tcp'][0]['HostPort']

    agent_self_dict = consul_query('agent/self')
    service_ip = agent_self_dict['Config']['AdvertiseAddr']

    return '{service_ip}:{ssh_port}'.format(**locals())
Ejemplo n.º 10
0
def get_container_ports_mapping(container_id):
    docker_api = docker_client.api()
    docker_inspect = docker_api.inspect_container(container_id)
    container_ports = docker_inspect['NetworkSettings']['Ports']
    mapping = {}
    for local_port, host_ports in six.iteritems(container_ports):
        protocol = local_port.split('/')[-1]
        mapping[local_port] = '{}/{}'.format(host_ports[0]['HostPort'],
                                             protocol)
    return mapping
Ejemplo n.º 11
0
def get_container_ssh_address(container_id):
    docker_api = docker_client.api()

    docker_inspect = docker_api.inspect_container(container_id)
    ssh_port = docker_inspect['NetworkSettings']['Ports']['22/tcp'][0]['HostPort']

    agent_self_dict = consul_query('agent/self')
    service_ip = agent_self_dict['Config']['AdvertiseAddr']

    return '{service_ip}:{ssh_port}'.format(**locals())
Ejemplo n.º 12
0
def local_port_to_external_port(container_id, microservice_local_port):
    docker_api = docker_client.api()
    docker_inspect = docker_api.inspect_container(container_id)
    container_ports = docker_inspect['NetworkSettings']['Ports']
    if microservice_local_port not in container_ports:
        raise BadRequestException(
            'Container with id="{}" does not expose port "{}"'.format(
                container_id, microservice_local_port))
    external_port = int(
        container_ports[microservice_local_port][0]['HostPort'])
    return external_port
Ejemplo n.º 13
0
    def _get_restart_parameters(self, container_id):
        try:
            docker_api = docker_client.api()
            docker_inspect = docker_api.inspect_container(container_id)

            for env_var in docker_inspect['Config']['Env']:
                env_key, env_value = (env_var.strip('"').split('=', 1) +
                                      [''])[:2]
                if env_key == 'RESTART_CONTAINER_PARAMETERS':
                    return json.loads(base64.b64decode(env_value).decode())
        except NotFound:
            for service in get_services_by_ship(ship=None):
                if service.split('/')[-1] == container_id:
                    return kv_get(service).get('params')
Ejemplo n.º 14
0
    def _get_restart_parameters(self, container_id):
        try:
            docker_api = docker_client.api()
            docker_inspect = docker_api.inspect_container(container_id)

            for env_var in docker_inspect['Config']['Env']:
                env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2]
                if env_key == 'RESTART_CONTAINER_PARAMETERS':
                    return json.loads(base64.b64decode(env_value))
        except NotFound:
            service_list = kv_list('ships/')
            for service in service_list:
                if service.split('/')[-1] == container_id:
                    return kv_get(service).get('params')
Ejemplo n.º 15
0
 def on_get(self, req, resp, image_name_or_address, image_name=None):
     if image_name is None:
         dockyard_address = None
         image_name = image_name_or_address
     else:
         dockyard_address = image_name_or_address
     image = LocalArmadaImage(dockyard_address, image_name)
     try:
         docker_api = docker_client.api()
         image_info = json.dumps(docker_api.images(image.image_path))
         return self.status_ok(
             resp, {'image_info': '{image_info}'.format(**locals())})
     except Exception as e:
         return self.status_exception(resp, "Cannot get info about image.",
                                      e)
Ejemplo n.º 16
0
    def GET(self, container_id, key):
        try:
            docker_api = docker_client.api()
            docker_inspect = docker_api.inspect_container(container_id)
            value = None
            for env_var in docker_inspect['Config']['Env']:
                env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2]
                if env_key == key:
                    value = env_value
                    break

            if value is None:
                return self.status_error('Requested environment variable "{key}" does not exist.'.format(**locals()))
            return self.status_ok({'value': str(value)})
        except Exception as e:
            return self.status_exception("Cannot inspect requested container.", e)
Ejemplo n.º 17
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api(timeout=30)
        docker_api.start(long_container_id)

        service_endpoints = {}
        agent_self_dict = consul_query('agent/self')
        service_ip = agent_self_dict['Config']['AdvertiseAddr']

        docker_inspect = docker_api.inspect_container(long_container_id)

        ship = get_ship_name()
        container_id = shorten_container_id(long_container_id)
        kv.save_container(ship, container_id, status='started')

        for container_port, host_address in docker_inspect['NetworkSettings']['Ports'].items():
            service_endpoints['{0}:{1}'.format(service_ip, host_address[0]['HostPort'])] = container_port
        return service_endpoints
Ejemplo n.º 18
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api(timeout=30)
        docker_api.start(long_container_id)

        service_endpoints = {}
        agent_self_dict = consul_query('agent/self')
        service_ip = agent_self_dict['Config']['AdvertiseAddr']

        docker_inspect = docker_api.inspect_container(long_container_id)

        ship = get_ship_name()
        container_id = shorten_container_id(long_container_id)
        save_service(ship, container_id, status='started')

        for container_port, host_address in docker_inspect['NetworkSettings']['Ports'].items():
            service_endpoints['{0}:{1}'.format(service_ip, host_address[0]['HostPort'])] = container_port
        return service_endpoints
Ejemplo n.º 19
0
    def _stop_service(self, container_id, force=False):
        if force:
            service_list = get_services_by_ship()
        else:
            service_list = get_local_services_from_kv_store()

        try:
            keys = fnmatch.filter(service_list, '*/{}'.format(container_id))
        except (IndexError, TypeError) as e:
            get_logger().exception(e)
            keys = []

        if not is_container_running(container_id):
            for key in keys:
                kv_remove(key)
            try:
                deregister_services(container_id)
            except Exception as e:
                get_logger().exception(e)
        else:
            run_command_in_container('supervisorctl stop armada_agent',
                                     container_id)
            trigger_hook('pre-stop', container_id)

            docker_api = docker_client.api()
            last_exception = None
            try:
                deregister_services(container_id)
            except Exception as e:
                get_logger().exception(e)
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                except Exception as e:
                    get_logger().debug(e, exc_info=True)
                    last_exception = e
                if not is_container_running(container_id):
                    for key in keys:
                        kv_remove(key)
                    break
            if is_container_running(container_id):
                get_logger().error('Could not stop container: %s',
                                   container_id)
                raise last_exception
Ejemplo n.º 20
0
    def _stop_service(self, container_id):
        ship = get_ship_name()
        service_list = kv_list('ships/{}/service/'.format(ship))
        try:
            key = fnmatch.filter(service_list, '*/{}'.format(container_id))[0]
        except (IndexError, TypeError):
            key = None

        if not is_container_running(container_id):
            if key:
                kv_remove(key)
            try:
                deregister_services(container_id)
            except Exception as e:
                get_logger().exception(e)
        else:
            run_command_in_container('supervisorctl stop armada_agent',
                                     container_id)

            # TODO: Compatibility with old microservice images. Should be removed in future armada version.
            run_command_in_container(
                'supervisorctl stop register_in_service_discovery',
                container_id)

            docker_api = docker_client.api()
            last_exception = None
            try:
                deregister_services(container_id)
            except Exception as e:
                get_logger().exception(e)
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                except Exception as e:
                    get_logger().debug(e, exc_info=True)
                    last_exception = e
                if not is_container_running(container_id):
                    if key:
                        kv_remove(key)
                    break
            if is_container_running(container_id):
                get_logger().error('Could not stop container: %s',
                                   container_id)
                raise last_exception
Ejemplo n.º 21
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api(timeout=30)
        docker_api.start(long_container_id)

        service_endpoints = {}
        service_ip, ship_name = get_ship_ip_and_name()

        docker_inspect = docker_api.inspect_container(long_container_id)

        container_id = shorten_container_id(long_container_id)
        save_container(ship_name,
                       container_id,
                       status='started',
                       ship_ip=service_ip)

        for container_port, host_address in docker_inspect['NetworkSettings'][
                'Ports'].items():
            service_endpoints['{0}:{1}'.format(
                service_ip, host_address[0]['HostPort'])] = container_port
        return service_endpoints
Ejemplo n.º 22
0
    def _stop_service(self, container_id):
        ship = get_ship_name()
        service_list = kv_list("ships/{}/service/".format(ship))
        try:
            key = fnmatch.filter(service_list, "*/{}".format(container_id))[0]
        except (IndexError, TypeError):
            key = None

        if not is_container_running(container_id):
            if key:
                kv_remove(key)
            try:
                deregister_services(container_id)
            except Exception as e:
                get_logger().exception(e)
        else:
            run_command_in_container("supervisorctl stop armada_agent", container_id)

            # TODO: Compatibility with old microservice images. Should be removed in future armada version.
            run_command_in_container("supervisorctl stop register_in_service_discovery", container_id)

            docker_api = docker_client.api()
            last_exception = None
            try:
                deregister_services(container_id)
            except Exception as e:
                get_logger().exception(e)
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                except Exception as e:
                    get_logger().debug(e, exc_info=True)
                    last_exception = e
                if not is_container_running(container_id):
                    if key:
                        kv_remove(key)
                    break
            if is_container_running(container_id):
                get_logger().error("Could not stop container: %s", container_id)
                raise last_exception
Ejemplo n.º 23
0
def _get_running_container_ids():
    docker_api = docker_client.api()
    return set(shorten_container_id(container['Id']) for container in docker_api.containers())
Ejemplo n.º 24
0
 def _get_docker_api(self, dockyard_address, dockyard_user, dockyard_password):
     docker_api = docker_client.api(timeout=30)
     self._login_to_dockyard(docker_api, dockyard_address, dockyard_user, dockyard_password)
     return docker_api
Ejemplo n.º 25
0
 def _get_docker_api(self, dockyard_address, dockyard_user,
                     dockyard_password):
     docker_api = docker_client.api(timeout=30)
     self._login_to_dockyard(docker_api, dockyard_address, dockyard_user,
                             dockyard_password)
     return docker_api
Ejemplo n.º 26
0
def get_running_container_ids():
    docker_api = docker_client.api()
    return set(container['Id'][:LENGTH_OF_SHORT_CONTAINER_ID] for container in docker_api.containers())
Ejemplo n.º 27
0
def get_running_container_ids():
    docker_api = docker_client.api()
    return set(container['Id'][:LENGTH_OF_SHORT_CONTAINER_ID]
               for container in docker_api.containers())
Ejemplo n.º 28
0
def get_running_container_ids():
    docker_api = docker_client.api()
    return set(
        shorten_container_id(container['Id'])
        for container in docker_api.containers())