Пример #1
0
 def _flag_system_container(self, instance, create_config):
     try:
         if instance.systemContainer:
             add_label(create_config, {
                 'io.rancher.container.system': instance.systemContainer})
     except (KeyError, AttributeError):
         pass
Пример #2
0
 def _flag_system_container(self, instance, create_config):
     try:
         if instance.systemContainer:
             add_label(
                 create_config,
                 {'io.rancher.container.system': instance.systemContainer})
     except (KeyError, AttributeError):
         pass
Пример #3
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"])
Пример #4
0
def setup_mac_and_ip(instance, create_config, set_mac=True, set_hostname=True):
    """
    Configures the mac address and primary ip address for the the supplied
    container. The mac_address is configured directly as part of the native
    docker API. The primary IP address is set as an environment variable on the
    container. Another Rancher micro-service will detect this environment
    variable when the container is started and inject the IP into the
    container.

    Note: while an instance can technically have more than one nic based on the
    resource schema, this implementation assumes a single nic for the purpose
    of configuring the mac address and IP.
    """
    mac_address = None
    device_number = None
    for nic in instance.nics:
        if device_number is None:
            mac_address = nic.macAddress
            device_number = nic.deviceNumber
        elif device_number > nic.deviceNumber:
            mac_address = nic.macAddress
            device_number = nic.deviceNumber

    if set_mac:
        create_config["mac_address"] = mac_address

    if not set_hostname:
        del create_config['hostname']

    try:
        if instance.nics and instance.nics[0].ipAddresses:
            # Assume one nic
            nic = instance.nics[0]
            ip_address = None
            for ip in nic.ipAddresses:
                if ip.role == 'primary':
                    ip_address = '{0}/{1}'.format(ip.address,
                                                  ip.subnet.cidrSize)
                    break

            if ip_address:
                add_label(create_config,
                          {'io.rancher.container.ip': ip_address})
    except (KeyError, AttributeError):
        pass
Пример #5
0
def setup_mac_and_ip(instance, create_config, set_mac=True, set_hostname=True):
    """
    Configures the mac address and primary ip address for the the supplied
    container. The mac_address is configured directly as part of the native
    docker API. The primary IP address is set as an environment variable on the
    container. Another Rancher micro-service will detect this environment
    variable when the container is started and inject the IP into the
    container.

    Note: while an instance can technically have more than one nic based on the
    resource schema, this implementation assumes a single nic for the purpose
    of configuring the mac address and IP.
    """
    mac_address = None
    device_number = None
    for nic in instance.nics:
        if device_number is None:
            mac_address = nic.macAddress
            device_number = nic.deviceNumber
        elif device_number > nic.deviceNumber:
            mac_address = nic.macAddress
            device_number = nic.deviceNumber

    if set_mac:
        create_config["mac_address"] = mac_address

    if not set_hostname:
        del create_config['hostname']

    try:
        if instance.nics and instance.nics[0].ipAddresses:
            # Assume one nic
            nic = instance.nics[0]
            ip_address = None
            for ip in nic.ipAddresses:
                if ip.role == 'primary':
                    ip_address = '{0}/{1}'.format(ip.address,
                                                  ip.subnet.cidrSize)
                    break

            if ip_address:
                add_label(create_config,
                          {'io.rancher.container.ip': ip_address})
    except (KeyError, AttributeError):
        pass
Пример #6
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'])
Пример #7
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'])
Пример #8
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'])
Пример #9
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'])