Ejemplo n.º 1
0
    def _is_instance_active(self, instance, host):
        if is_no_op(instance):
            return True

        client = docker_client()
        container = self.get_container(client, instance)
        return _is_running(client, container)
Ejemplo n.º 2
0
    def _is_instance_active(self, instance, host):
        if is_no_op(instance):
            return True

        client = docker_client()
        container = self.get_container(client, instance)
        return _is_running(client, container)
Ejemplo n.º 3
0
    def _is_instance_inactive(self, instance, host):
        if is_no_op(instance):
            return True

        c = docker_client()
        container = self.get_container(c, instance)

        return _is_stopped(c, container)
Ejemplo n.º 4
0
    def _is_instance_inactive(self, instance, host):
        if is_no_op(instance):
            return True

        c = docker_client()
        container = self.get_container(c, instance)

        return _is_stopped(c, container)
Ejemplo n.º 5
0
    def _do_image_activate(self, image, storage_pool, progress):
        if is_no_op(image):
            return

        if self._is_build(image):
            return self._image_build(image, progress)

        auth_config = None
        try:
            if "registryCredential" in image:
                if image.registryCredential is not None:
                    auth_config = {
                        "username": image.registryCredential["publicValue"],
                        "email": image.registryCredential["data"]["fields"]["email"],
                        "password": image.registryCredential["secretValue"],
                        "serveraddress": image.registryCredential["registry"]["data"]["fields"]["serverAddress"],
                    }
                    if auth_config["serveraddress"] == "https://docker.io":
                        auth_config["serveraddress"] = "https://index.docker.io"
                    log.debug("Auth_Config: [%s]", auth_config)
            else:
                log.debug("No Registry credential found. Pulling non-authed")
        except (AttributeError, KeyError, TypeError) as e:
            raise AuthConfigurationError(
                "Malformed Auth Config. \n\n" "error: [%s]\nregistryCredential:" " %s" % (e, image.registryCredential)
            )
        client = docker_client()
        data = image.data.dockerImage
        marshaller = get_type(MARSHALLER)
        temp = data.qualifiedName
        if data.qualifiedName.startswith("docker.io/"):
            temp = "index." + data.qualifiedName
        # Always pass insecure_registry=True to prevent docker-py
        # from pre-verifying the registry. Let the docker daemon handle
        # the verification of and connection to the registry.
        if progress is None:
            result = client.pull(repository=temp, tag=data.tag, auth_config=auth_config, insecure_registry=True)
            if "error" in result:
                raise ImageValidationError("Image [%s] failed to pull: %s" % (data.fullName, result["error"]))
        else:
            last_message = ""
            message = ""
            for status in client.pull(
                repository=temp, tag=data.tag, auth_config=auth_config, stream=True, insecure_registry=True
            ):
                try:
                    status = marshaller.from_string(status)
                    if "error" in status:
                        message = status["error"]
                        raise ImageValidationError("Image [%s] failed to pull:" " %s" % (data.fullName, message))
                    if "status" in status:
                        message = status["status"]
                except ImageValidationError, e:
                    raise e
                except:
                    # Ignore errors reading the status from Docker
                    continue
Ejemplo n.º 6
0
    def _do_instance_activate(self, instance, host, progress):
        if is_no_op(instance):
            return

        client = self._get_docker_client(host)

        image_tag = self._get_image_tag(instance)

        name = instance.uuid

        create_config = {"name": name, "detach": True}

        start_config = {
            "publish_all_ports": False,
            "privileged": self._is_true(instance, "privileged"),
            "read_only": self._is_true(instance, "readOnly"),
        }

        # These _setup_simple_config_fields calls should happen before all
        # other config because they stomp over config fields that other
        # setup methods might append to. Example: the environment field
        self._setup_simple_config_fields(create_config, instance, CREATE_CONFIG_FIELDS)

        self._setup_simple_config_fields(start_config, instance, START_CONFIG_FIELDS)

        add_label(create_config, {"io.rancher.container.uuid": instance.uuid})

        self._setup_logging(start_config, instance)

        self._setup_hostname(create_config, instance)

        self._setup_command(create_config, instance)

        self._setup_ports(create_config, instance)

        self._setup_volumes(create_config, instance, start_config, client)

        self._setup_links(start_config, instance)

        self._setup_networking(instance, host, create_config, start_config)

        self._flag_system_container(instance, create_config)

        self._setup_proxy(instance, create_config)

        setup_cattle_config_url(instance, create_config)

        create_config["host_config"] = create_host_config(**start_config)

        container = self._create_container(client, create_config, image_tag, instance, name, progress)
        container_id = container["Id"]

        log.info("Starting docker container [%s] docker id [%s] %s", name, container_id, start_config)

        client.start(container_id)

        self._record_state(client, instance, docker_id=container["Id"])
Ejemplo n.º 7
0
 def _is_image_active(self, image, storage_pool):
     if is_no_op(image):
         return True
     parsed_tag = DockerPool.parse_repo_tag(image.data.dockerImage.fullName)
     try:
         if len(docker_client().inspect_image(parsed_tag['uuid'])):
             return True
     except APIError:
         pass
     return False
Ejemplo n.º 8
0
 def _is_image_active(self, image, storage_pool):
     if is_no_op(image):
         return True
     parsed_tag = DockerPool.parse_repo_tag(image.data.dockerImage.fullName)
     try:
         if len(docker_client().inspect_image(parsed_tag['uuid'])):
             return True
     except APIError:
         pass
     return False
Ejemplo n.º 9
0
    def _do_image_activate(self, image, storage_pool, progress):
        if is_no_op(image):
            return

        auth_config = None
        try:
            if 'registryCredential' in image:
                if image.registryCredential is not None:
                    auth_config = {
                        'username': image.registryCredential['publicValue'],
                        'email': image.registryCredential['data']['fields']
                        ['email'],
                        'password': image.registryCredential['secretValue'],
                        'serveraddress': image.registryCredential['registry']
                        ['data']['fields']['serverAddress']
                    }
                    log.debug('Auth_Config: [%s]', auth_config)
            else:
                log.debug('No Registry credential found. Pulling non-authed')
        except (AttributeError, KeyError, TypeError) as e:
            raise AuthConfigurationError("Malformed Auth Config. \n\n"
                                         "error: [%s]\nregistryCredential:"
                                         " %s"
                                         % (e, image.registryCredential))
        client = docker_client()
        data = image.data.dockerImage
        marshaller = get_type(MARSHALLER)
        if progress is None:
            result = client.pull(repository=data.qualifiedName,
                                 tag=data.tag, auth_config=auth_config)
            if 'error' in result:
                raise ImageValidationError('Image [%s] failed to pull' %
                                           data.fullName)
        else:
            for status in client.pull(repository=data.qualifiedName,
                                      tag=data.tag,
                                      auth_config=auth_config,
                                      stream=True):
                log.info('Pulling [%s] status : %s', data.fullName, status)
                status = marshaller.from_string(status)
                try:
                    message = status['status']
                except KeyError:
                    message = status['error']
                    raise ImageValidationError('Image [%s] failed to pull '
                                               ': %s' % (data.fullName,
                                                         message))
                progress.update(message)
Ejemplo n.º 10
0
    def _do_instance_deactivate(self, instance, host, progress):
        if is_no_op(instance):
            return

        c = self._get_docker_client(host)
        timeout = 10

        try:
            timeout = int(instance.processData.timeout)
        except (TypeError, KeyError, AttributeError):
            pass

        container = self.get_container(c, instance)

        c.stop(container["Id"], timeout=timeout)

        container = self.get_container(c, instance)
        if not _is_stopped(c, container):
            c.kill(container["Id"])

        container = self.get_container(c, instance)
        if not _is_stopped(c, container):
            raise Exception("Failed to stop container {0}".format(instance.uuid))
Ejemplo n.º 11
0
    def _do_instance_deactivate(self, instance, host, progress):
        if is_no_op(instance):
            return

        c = docker_client()
        timeout = 10

        try:
            timeout = int(instance.processData.timeout)
        except (TypeError, KeyError, AttributeError):
            pass

        container = self.get_container(c, instance)

        c.stop(container['Id'], timeout=timeout)

        container = self.get_container(c, instance)
        if not _is_stopped(c, container):
            c.kill(container['Id'])

        container = self.get_container(c, instance)
        if not _is_stopped(c, container):
            raise Exception('Failed to stop container {0}'.format(
                instance.uuid))
Ejemplo n.º 12
0
    def _do_instance_activate(self, instance, host, progress):
        if is_no_op(instance):
            return

        client = docker_client()

        image_tag = self._get_image_tag(instance)

        name = instance.uuid
        if instance.name and re.match(r'^[a-zA-Z0-9][a-zA-Z0-9_.-]+$',
                                      instance.name):
            try:
                client.inspect_container('r-{}'.format(instance.name))
            except NotFound:
                name = 'r-{}'.format(instance.name)

        create_config = {
            'name': name,
            'detach': True
        }

        start_config = {
            'publish_all_ports': False,
            'privileged': self._is_true(instance, 'privileged'),
            'read_only': self._is_true(instance, 'readOnly'),
        }

        # These _setup_simple_config_fields calls should happen before all
        # other config because they stomp over config fields that other
        # setup methods might append to. Example: the environment field
        self._setup_simple_config_fields(create_config, instance,
                                         CREATE_CONFIG_FIELDS)

        self._setup_simple_config_fields(start_config, instance,
                                         START_CONFIG_FIELDS)

        add_label(create_config, {UUID_LABEL: instance.uuid})
        if instance.name:
            add_label(create_config,
                      {'io.rancher.container.name': instance.name})

        self._setup_logging(start_config, instance)

        self._setup_hostname(create_config, instance)

        self._setup_command(create_config, instance)

        self._setup_ports(create_config, instance, start_config)

        self._setup_volumes(create_config, instance, start_config, client)

        self._setup_links(start_config, instance)

        self._setup_networking(instance, host, create_config, start_config)

        self._flag_system_container(instance, create_config)

        self._setup_proxy(instance, create_config)

        setup_cattle_config_url(instance, create_config)

        create_config['host_config'] = \
            client.create_host_config(**start_config)

        container = self._create_container(client, create_config,
                                           image_tag, instance, name,
                                           progress)
        container_id = container['Id']

        log.info('Starting docker container [%s] docker id [%s] %s', name,
                 container_id, start_config)

        client.start(container_id)

        self._record_state(client, instance, docker_id=container['Id'])
Ejemplo n.º 13
0
    def _do_image_activate(self, image, storage_pool, progress):
        if is_no_op(image):
            return

        if self._is_build(image):
            return self._image_build(image, progress)

        auth_config = None
        try:
            if 'registryCredential' in image:
                if image.registryCredential is not None:
                    auth_config = {
                        'username':
                        image.registryCredential['publicValue'],
                        'email':
                        image.registryCredential['data']['fields']['email'],
                        'password':
                        image.registryCredential['secretValue'],
                        'serveraddress':
                        image.registryCredential['registry']['data']['fields']
                        ['serverAddress']
                    }
                    if auth_config['serveraddress'] == "https://docker.io":
                        auth_config['serveraddress'] =\
                            "https://index.docker.io"
                    log.debug('Auth_Config: [%s]', auth_config)
            else:
                log.debug('No Registry credential found. Pulling non-authed')
        except (AttributeError, KeyError, TypeError) as e:
            raise AuthConfigurationError("Malformed Auth Config. \n\n"
                                         "error: [%s]\nregistryCredential:"
                                         " %s" % (e, image.registryCredential))
        client = docker_client()
        data = image.data.dockerImage
        marshaller = get_type(MARSHALLER)
        temp = data.qualifiedName
        if data.qualifiedName.startswith('docker.io/'):
            temp = 'index.' + data.qualifiedName
        # Always pass insecure_registry=True to prevent docker-py
        # from pre-verifying the registry. Let the docker daemon handle
        # the verification of and connection to the registry.
        if progress is None:
            result = client.pull(repository=temp,
                                 tag=data.tag,
                                 auth_config=auth_config,
                                 insecure_registry=True)
            if 'error' in result:
                raise ImageValidationError('Image [%s] failed to pull: %s' %
                                           (data.fullName, result['error']))
        else:
            last_message = ''
            message = ''
            for status in client.pull(repository=temp,
                                      tag=data.tag,
                                      auth_config=auth_config,
                                      stream=True,
                                      insecure_registry=True):
                try:
                    status = marshaller.from_string(status)
                    if 'error' in status:
                        message = status['error']
                        raise ImageValidationError('Image [%s] failed to pull:'
                                                   ' %s' %
                                                   (data.fullName, message))
                    if 'status' in status:
                        message = status['status']
                except ImageValidationError, e:
                    raise e
                except:
                    # Ignore errors reading the status from Docker
                    continue
Ejemplo n.º 14
0
    def _do_instance_activate(self, instance, host, progress):
        if is_no_op(instance):
            return

        client = docker_client()

        image_tag = self._get_image_tag(instance)

        name = instance.uuid
        if instance.name and re.match(r'^[a-zA-Z0-9][a-zA-Z0-9_.-]+$',
                                      instance.name):
            try:
                client.inspect_container('r-{}'.format(instance.name))
            except NotFound:
                name = 'r-{}'.format(instance.name)

        create_config = {'name': name, 'detach': True}

        start_config = {
            'publish_all_ports': False,
            'privileged': self._is_true(instance, 'privileged'),
            'read_only': self._is_true(instance, 'readOnly'),
        }

        # These _setup_simple_config_fields calls should happen before all
        # other config because they stomp over config fields that other
        # setup methods might append to. Example: the environment field
        self._setup_simple_config_fields(create_config, instance,
                                         CREATE_CONFIG_FIELDS)

        self._setup_simple_config_fields(start_config, instance,
                                         START_CONFIG_FIELDS)

        add_label(create_config, {UUID_LABEL: instance.uuid})
        if instance.name:
            add_label(create_config,
                      {'io.rancher.container.name': instance.name})
        self._setup_dns_search(start_config, instance)

        self._setup_logging(start_config, instance)

        self._setup_hostname(create_config, instance)

        self._setup_command(create_config, instance)

        self._setup_ports(create_config, instance, start_config)

        self._setup_volumes(create_config, instance, start_config, client)

        self._setup_links(start_config, instance)

        self._setup_networking(instance, host, create_config, start_config)

        self._flag_system_container(instance, create_config)

        self._setup_proxy(instance, create_config)

        setup_cattle_config_url(instance, create_config)

        create_config['host_config'] = \
            client.create_host_config(**start_config)

        self._setup_device_options(create_config['host_config'], instance)

        container = self.get_container(client, instance)
        created = False
        if container is None:
            container = self._create_container(client, create_config,
                                               image_tag, instance, name,
                                               progress)
            created = True

        container_id = container['Id']

        log.info('Starting docker container [%s] docker id [%s] %s', name,
                 container_id, start_config)

        try:
            client.start(container_id)
        except Exception as e:
            if created:
                remove_container(client, container)
            raise e

        self._record_state(client, instance, docker_id=container['Id'])
Ejemplo n.º 15
0
    def _do_instance_activate(self, instance, host, progress):
        if is_no_op(instance):
            return

        client = self._get_docker_client(host)

        try:
            image_tag = instance.image.data.dockerImage.fullName
        except KeyError:
            raise Exception('Can not start container with no image')

        name = instance.uuid

        create_config = {
            'name': name,
            'detach': True
        }

        start_config = {
            'publish_all_ports': False,
            'privileged': self._is_privileged(instance)
        }

        # These _setup_simple_config_fields calls should happen before all
        # other config because they stomp over config fields that other
        # setup methods might append to. Example: the environment field
        self._setup_simple_config_fields(create_config, instance,
                                         CREATE_CONFIG_FIELDS)

        self._setup_simple_config_fields(start_config, instance,
                                         START_CONFIG_FIELDS)

        add_label(create_config, RANCHER_UUID=instance.uuid)

        self._setup_hostname(create_config, instance)

        self._setup_command(create_config, instance)

        self._setup_ports(create_config, instance)

        self._setup_volumes(create_config, instance, start_config, client)

        self._setup_restart_policy(instance, start_config)

        self._setup_links(start_config, instance)

        self._setup_networking(instance, host, create_config, start_config)

        setup_cattle_config_url(instance, create_config)

        container = self._create_container(client, create_config,
                                           image_tag, instance, name,
                                           progress)
        container_id = container['Id']

        log.info('Starting docker container [%s] docker id [%s] %s', name,
                 container_id, start_config)

        client.start(container_id, **start_config)

        self._record_state(client, instance, docker_id=container['Id'])
Ejemplo n.º 16
0
    def _do_instance_activate(self, instance, host, progress):
        if is_no_op(instance):
            return

        client = self._get_docker_client(host)

        image_tag = self._get_image_tag(instance)

        name = instance.uuid

        create_config = {'name': name, 'detach': True}

        start_config = {
            'publish_all_ports': False,
            'privileged': self._is_true(instance, 'privileged'),
            'read_only': self._is_true(instance, 'readOnly'),
        }

        # These _setup_simple_config_fields calls should happen before all
        # other config because they stomp over config fields that other
        # setup methods might append to. Example: the environment field
        self._setup_simple_config_fields(create_config, instance,
                                         CREATE_CONFIG_FIELDS)

        self._setup_simple_config_fields(start_config, instance,
                                         START_CONFIG_FIELDS)

        add_label(create_config, {'io.rancher.container.uuid': instance.uuid})

        self._setup_logging(start_config, instance)

        self._setup_hostname(create_config, instance)

        self._setup_command(create_config, instance)

        self._setup_ports(create_config, instance)

        self._setup_volumes(create_config, instance, start_config, client)

        self._setup_links(start_config, instance)

        self._setup_networking(instance, host, create_config, start_config)

        self._flag_system_container(instance, create_config)

        self._setup_proxy(instance, create_config)

        setup_cattle_config_url(instance, create_config)

        create_config['host_config'] = create_host_config(**start_config)

        container = self._create_container(client, create_config, image_tag,
                                           instance, name, progress)
        container_id = container['Id']

        log.info('Starting docker container [%s] docker id [%s] %s', name,
                 container_id, start_config)

        client.start(container_id)

        self._record_state(client, instance, docker_id=container['Id'])
Ejemplo n.º 17
0
    def _do_image_activate(self, image, storage_pool, progress):
        if is_no_op(image):
            return

        if self._is_build(image):
            return self._image_build(image, progress)

        auth_config = None
        try:
            if 'registryCredential' in image:
                if image.registryCredential is not None:
                    auth_config = {
                        'username': image.registryCredential['publicValue'],
                        'email': image.registryCredential['data']['fields']
                        ['email'],
                        'password': image.registryCredential['secretValue'],
                        'serveraddress': image.registryCredential['registry']
                        ['data']['fields']['serverAddress']
                    }
                    if auth_config['serveraddress'] == "https://docker.io":
                        auth_config['serveraddress'] =\
                            "https://index.docker.io"
                    log.debug('Auth_Config: [%s]', auth_config)
            else:
                log.debug('No Registry credential found. Pulling non-authed')
        except (AttributeError, KeyError, TypeError) as e:
            raise AuthConfigurationError("Malformed Auth Config. \n\n"
                                         "error: [%s]\nregistryCredential:"
                                         " %s"
                                         % (e, image.registryCredential))
        client = docker_client()
        data = image.data.dockerImage
        marshaller = get_type(MARSHALLER)
        temp = data.qualifiedName
        if data.qualifiedName.startswith('docker.io/'):
            temp = 'index.' + data.qualifiedName
        # Always pass insecure_registry=True to prevent docker-py
        # from pre-verifying the registry. Let the docker daemon handle
        # the verification of and connection to the registry.
        if progress is None:
            result = client.pull(repository=temp,
                                 tag=data.tag, auth_config=auth_config,
                                 insecure_registry=True)
            if 'error' in result:
                raise ImageValidationError('Image [%s] failed to pull' %
                                           data.fullName)
        else:
            for status in client.pull(repository=temp,
                                      tag=data.tag,
                                      auth_config=auth_config,
                                      stream=True,
                                      insecure_registry=True):
                status = marshaller.from_string(status)
                try:
                    message = status['status']
                except KeyError:
                    message = status['error']
                    raise ImageValidationError('Image [%s] failed to pull '
                                               ': %s' % (data.fullName,
                                                         message))
                progress.update(message)
Ejemplo n.º 18
0
    def _is_image_active(self, image, storage_pool):
        if is_no_op(image):
            return True

        image_obj = self._get_image_by_label(image.data.dockerImage.fullName)
        return image_obj is not None