Example #1
0
 def _stop_service(self, container_id):
     docker_api = docker_client.api()
     docker_api.stop(container_id)
     try:
         deregister_services(container_id)
     except Exception as e:
         traceback.print_exc()
Example #2
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)
Example #3
0
    def _get_docker_api(self, dockyard_address, dockyard_user, dockyard_password):
        if hasattr(self, '__docker_api') and self.__docker_api:
            return self.__docker_api

        docker_api = docker_client.api()

        if dockyard_user and dockyard_password:
            logged_in = False
            # Workaround for abrupt changes in docker-py library.
            login_exceptions = []
            registry_endpoints = ['https://{0}/v1/'.format(dockyard_address),
                                  'https://{0}'.format(dockyard_address),
                                  dockyard_address]
            for registry_endpoint in registry_endpoints:
                try:
                    docker_api.login(dockyard_user, dockyard_password, registry=registry_endpoint)
                    logged_in = True
                    break
                except Exception as e:
                    login_exceptions.append(e)
            if not logged_in:
                for e in login_exceptions:
                    print_err(e)
                raise login_exceptions[0]

        self.__docker_api = docker_api
        return docker_api
Example #4
0
    def _stop_service(self, container_id):
        ship = get_ship_name()
        service_dict = None
        service_list = kv_list('ships/{}/service/'.format(ship))
        if service_list:
            key = fnmatch.filter(service_list, '*/{}'.format(container_id))
            service_dict = kv_get(key[0]) if key else None
        if service_dict and service_dict['Status'] in ['crashed', 'not-recovered']:
            kv_remove(key[0])
        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:
                traceback.print_exc()
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                    kv_remove(key[0])
                except Exception as e:
                    last_exception = e
                    traceback.print_exc()
                if not is_container_running(container_id):
                    break
            if is_container_running(container_id):
                get_logger().error('Could not stop container: {}'.format(container_id))
                raise last_exception
Example #5
0
 def _stop_service(self, container_id):
     docker_api = docker_client.api()
     last_exception = None
     try:
         exec_id = docker_api.exec_create(
             container_id,
             'supervisorctl stop register_in_service_discovery')
         docker_api.exec_start(exec_id['Id'])
     except:
         traceback.print_exc()
     try:
         deregister_services(container_id)
     except:
         traceback.print_exc()
     for i in range(3):
         try:
             docker_api.stop(container_id)
         except Exception as e:
             last_exception = e
             traceback.print_exc()
         if not is_container_running(container_id):
             break
     if is_container_running(container_id):
         get_logger().error(
             'Could not stop container: {}'.format(container_id))
         raise last_exception
Example #6
0
 def GET(self, microservice_name):
     try:
         docker_api = docker_client.api()
         image_info = json.dumps(docker_api.images(microservice_name))
         return self.status_ok({'image_info': '{image_info}'.format(**locals())})
     except Exception as e:
         return self.status_error("Cannot inspect requested container. {exception_class} - {exception}".format(
             exception_class=type(e).__name__, exception=str(e)))
Example #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)
Example #8
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_error("Cannot get info about image. {exception_class} - {exception}".format(
             exception_class=type(e).__name__, exception=str(e)))
Example #9
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_error(
             "Cannot get info about image. {exception_class} - {exception}".
             format(exception_class=type(e).__name__, exception=str(e)))
Example #10
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api()
        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)

        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
Example #11
0
    def _restart_service(self, container_id, target_ship=None, force_restart=False):
        docker_api = docker_client.api()
        docker_inspect = docker_api.inspect_container(container_id)

        restart_parameters = {}
        for env_var in docker_inspect['Config']['Env']:
            env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2]
            if env_key == 'RESTART_CONTAINER_PARAMETERS':
                restart_parameters = json.loads(base64.b64decode(env_value))

        if target_ship:
            return self._restart_service_remote(container_id, restart_parameters,
                                                target_ship, force_restart)
        else:
            return self._restart_service_local(container_id, restart_parameters)
Example #12
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)
Example #13
0
    def POST(self):
        container_id, error = self.get_post_parameter('container_id')
        if error:
            return self.status_error(error)

        docker_api = docker_client.api()

        docker_inspect = docker_api.inspect_container(container_id)

        restart_parameters = {}
        for env_var in docker_inspect['Config']['Env']:
            env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2]
            if env_key == 'RESTART_CONTAINER_PARAMETERS':
                restart_parameters = json.loads(base64.b64decode(env_value))

        image_path = restart_parameters.get('image_path')
        dockyard_user = restart_parameters.get('dockyard_user')
        dockyard_password = restart_parameters.get('dockyard_password')
        dict_ports = restart_parameters.get('ports')
        dict_environment = restart_parameters.get('environment')
        dict_volumes = restart_parameters.get('volumes')
        run_command = restart_parameters.get('run_command')
        microservice_name = dict_environment.get('MICROSERVICE_NAME')
        dockyard_address, _, _ = self._split_image_path(image_path)
        docker_api = self._get_docker_api(dockyard_address, dockyard_user, dockyard_password)

        try:
            self._pull_latest_image(docker_api, image_path, microservice_name)
        except Exception as e:
            return self.status_error("Failed to pull image's newest version. {exception_class} - {exception}".format(
                exception_class=type(e).__name__, exception=str(e)))
        try:
            docker_api.stop(container_id)
        except Exception as e:
            return self.status_error("Cannot stop requested container. {exception_class} - {exception}".format(
                exception_class=type(e).__name__, exception=str(e)))

        try:
            deregister_services(container_id)
        except:
            traceback.print_exc()

        return self.run_container(image_path, dockyard_user, dockyard_password, dict_ports, dict_environment,
                                  dict_volumes, run_command)
Example #14
0
    def POST(self):
        container_id, error = self.get_post_parameter('container_id')
        if error:
            return self.status_error(error)

        try:
            docker_api = docker_client.api()

            docker_api.stop(container_id)
        except Exception as e:
            return self.status_error("Cannot stop requested container. {exception_class} - {exception}".format(
                exception_class=type(e).__name__, exception=str(e)))

        try:
            deregister_services(container_id)
        except Exception as e:
            traceback.print_exc()

        return self.status_ok()
Example #15
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)
Example #16
0
    def _stop_service(self, container_id):
        ship = get_ship_name()
        service_dict = None
        service_list = kv_list('ships/{}/service/'.format(ship))
        if service_list:
            key = fnmatch.filter(service_list, '*/{}'.format(container_id))
            service_dict = kv_get(key[0]) if key else None
        if service_dict and service_dict['Status'] in [
                'crashed', 'not-recovered'
        ]:
            kv_remove(key[0])
        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:
                traceback.print_exc()
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                    kv_remove(key[0])
                except Exception as e:
                    last_exception = e
                    traceback.print_exc()
                if not is_container_running(container_id):
                    break
            if is_container_running(container_id):
                get_logger().error(
                    'Could not stop container: {}'.format(container_id))
                raise last_exception
Example #17
0
 def _stop_service(self, container_id):
     docker_api = docker_client.api()
     last_exception = None
     try:
         exec_id = docker_api.exec_create(container_id, 'supervisorctl stop register_in_service_discovery')
         docker_api.exec_start(exec_id['Id'])
     except:
         traceback.print_exc()
     try:
         deregister_services(container_id)
     except:
         traceback.print_exc()
     for i in range(3):
         try:
             docker_api.stop(container_id)
         except Exception as e:
             last_exception = e
             traceback.print_exc()
         if not is_container_running(container_id):
             break
     if is_container_running(container_id):
         get_logger().error('Could not stop container: {}'.format(container_id))
         raise last_exception
Example #18
0
    def run_container(self, image_path, dockyard_user, dockyard_password, dict_ports, dict_environment, dict_volumes,
                      run_command):
        exception_msg = ""
        try:
            restart_parameters = {'image_path': image_path,
                                  'dockyard_user': dockyard_user,
                                  'dockyard_password': dockyard_password,
                                  'ports': dict_ports,
                                  'environment': dict_environment,
                                  'volumes': dict_volumes,
                                  'run_command': run_command,
                                  'microservice_name': dict_environment.get('MICROSERVICE_NAME')
                                  }
            dict_environment['RESTART_CONTAINER_PARAMETERS'] = base64.b64encode(json.dumps(restart_parameters))
            dict_environment['ARMADA_RUN_COMMAND'] = base64.b64encode(run_command)
            microservice_name = dict_environment.get('MICROSERVICE_NAME')

            ports = None
            port_bindings = None
            if dict_ports:
                ports = map(int, dict_ports.values())
                port_bindings = dict((int(port_container), int(port_host))
                                     for port_host, port_container in dict_ports.iteritems())

            environment = None
            if dict_environment:
                environment = dict_environment

            volumes = None
            volume_bindings = None
            if dict_volumes:
                volumes = dict_volumes.values()
                volume_bindings = dict((path_host, {'bind': path_container, 'ro': False}) for path_host, path_container in dict_volumes.iteritems())

            docker_api = docker_client.api()

            dockyard_address, image_name, image_tag = self.__split_image_path(image_path)

            if dockyard_user and dockyard_password:
                logged_in = False
                # Workaround for abrupt changes in docker-py library.
                login_exceptions = []
                registry_endpoints = ['https://{0}/v1/'.format(dockyard_address),
                                        'https://{0}'.format(dockyard_address),
                                        dockyard_address]
                for registry_endpoint in registry_endpoints:
                    try:
                        docker_api.login(dockyard_user, dockyard_password, registry=registry_endpoint)
                        logged_in = True
                        break
                    except Exception as e:
                        login_exceptions.append(e)
                if not logged_in:
                    for e in login_exceptions:
                        print_err(e)
                    raise login_exceptions[0]

            if dockyard_address:
                try:
                    docker_client.docker_pull(docker_api, dockyard_address, image_name, image_tag)
                    docker_api.tag(dockyard_address + '/' + image_name, microservice_name, tag=image_tag, force=True)
                except Exception as e:
                    if "ping attempt failed" in str(e):
                        exception_msg += INSECURE_REGISTRY_ERROR_MSG.format(header="ERROR!", address=dockyard_address)
                    raise
            else:
                docker_api.tag(image_name, microservice_name, tag=image_tag, force=True)

            container_info = docker_api.create_container(microservice_name,
                                                         ports=ports,
                                                         environment=environment,
                                                         volumes=volumes)
            long_container_id = container_info['Id']
            docker_api.start(long_container_id,
                             port_bindings=port_bindings,
                             publish_all_ports=True,
                             privileged=True,
                             binds=volume_bindings)

            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)

            for docker_port, host_address in docker_inspect['NetworkSettings']['Ports'].items():
                service_endpoints['{0}:{1}'.format(service_ip, host_address[0]['HostPort'])] = docker_port

        except Exception as e:
            traceback.print_exc()
            exception_msg = exception_msg + "Cannot create requested container. {exception_class} - {exception}".format(
                exception_class=type(e).__name__, exception=str(e))
            return self.status_error(exception_msg)

        short_container_id = long_container_id[:LENGTH_OF_SHORT_CONTAINER_ID]
        return self.status_ok({'container_id': short_container_id, 'endpoints': service_endpoints})
Example #19
0
 def _get_docker_api(self, dockyard_address, dockyard_user,
                     dockyard_password):
     docker_api = docker_client.api()
     self._login_to_dockyard(docker_api, dockyard_address, dockyard_user,
                             dockyard_password)
     return docker_api
Example #20
0
 def _get_docker_api(self, dockyard_address, dockyard_user, dockyard_password):
     docker_api = docker_client.api()
     self._login_to_dockyard(docker_api, dockyard_address, dockyard_user, dockyard_password)
     return docker_api