예제 #1
0
    def ensure(self):
        if self.external_name:
            try:
                self.inspect()
                log.debug('Network {0} declared as external. No new '
                          'network will be created.'.format(self.name))
            except NotFound:
                raise ConfigurationError(
                    'Network {name} declared as external, but could'
                    ' not be found. Please create the network manually'
                    ' using `{command} {name}` and try again.'.format(
                        name=self.external_name,
                        command='docker network create'))
            return

        try:
            data = self.inspect()
            check_remote_network_config(data, self)
        except NotFound:
            driver_name = 'the default driver'
            if self.driver:
                driver_name = 'driver "{}"'.format(self.driver)

            log.info('Creating network "{}" with {}'.format(
                self.full_name, driver_name))

            self.client.create_network(
                name=self.full_name,
                driver=self.driver,
                options=self.driver_opts,
                ipam=self.ipam,
                internal=self.internal,
                enable_ipv6=self.enable_ipv6,
                labels=self.labels,
                attachable=version_gte(self.client._version, '1.24') or None)
예제 #2
0
파일: network.py 프로젝트: dnephin/compose
    def ensure(self):
        if self.external:
            if self.driver == 'overlay':
                # Swarm nodes do not register overlay networks that were
                # created on a different node unless they're in use.
                # See docker/compose#4399
                return
            try:
                self.inspect()
                log.debug(
                    'Network {0} declared as external. No new '
                    'network will be created.'.format(self.name)
                )
            except NotFound:
                raise ConfigurationError(
                    'Network {name} declared as external, but could'
                    ' not be found. Please create the network manually'
                    ' using `{command} {name}` and try again.'.format(
                        name=self.full_name,
                        command='docker network create'
                    )
                )
            return

        self._set_legacy_flag()
        try:
            data = self.inspect(legacy=self.legacy)
            check_remote_network_config(data, self)
        except NotFound:
            driver_name = 'the default driver'
            if self.driver:
                driver_name = 'driver "{}"'.format(self.driver)

            log.info(
                'Creating network "{}" with {}'.format(self.full_name, driver_name)
            )

            self.client.create_network(
                name=self.full_name,
                driver=self.driver,
                options=self.driver_opts,
                ipam=self.ipam,
                internal=self.internal,
                enable_ipv6=self.enable_ipv6,
                labels=self._labels,
                attachable=version_gte(self.client._version, '1.24') or None,
                check_duplicate=True,
            )
예제 #3
0
파일: network.py 프로젝트: daimor/compose
    def ensure(self):
        if self.external:
            try:
                self.inspect()
                log.debug(
                    'Network {0} declared as external. No new '
                    'network will be created.'.format(self.name)
                )
            except NotFound:
                raise ConfigurationError(
                    'Network {name} declared as external, but could'
                    ' not be found. Please create the network manually'
                    ' using `{command} {name}` and try again.'.format(
                        name=self.full_name,
                        command='docker network create'
                    )
                )
            return

        try:
            data = self.inspect()
            check_remote_network_config(data, self)
        except NotFound:
            driver_name = 'the default driver'
            if self.driver:
                driver_name = 'driver "{}"'.format(self.driver)

            log.info(
                'Creating network "{}" with {}'
                .format(self.full_name, driver_name)
            )

            self.client.create_network(
                name=self.full_name,
                driver=self.driver,
                options=self.driver_opts,
                ipam=self.ipam,
                internal=self.internal,
                enable_ipv6=self.enable_ipv6,
                labels=self._labels,
                attachable=version_gte(self.client._version, '1.24') or None,
                check_duplicate=True,
            )
예제 #4
0
    def ensure(self):
        if self.external:
            if self.driver == 'overlay':
                # Swarm nodes do not register overlay networks that were
                # created on a different node unless they're in use.
                # See docker/compose#4399
                return
            try:
                self.inspect()
                log.debug('Network {0} declared as external. No new '
                          'network will be created.'.format(self.name))
            except NotFound:
                raise ConfigurationError(
                    'Network {name} declared as external, but could'
                    ' not be found. Please create the network manually'
                    ' using `{command} {name}` and try again.'.format(
                        name=self.full_name, command='docker network create'))
            return

        self._set_legacy_flag()
        try:
            data = self.inspect(legacy=self.legacy)
            check_remote_network_config(data, self)
        except NotFound:
            driver_name = 'the default driver'
            if self.driver:
                driver_name = 'driver "{}"'.format(self.driver)

            log.info('Creating network "{}" with {}'.format(
                self.full_name, driver_name))

            self.client.create_network(
                name=self.full_name,
                driver=self.driver,
                options=self.driver_opts,
                ipam=self.ipam,
                internal=self.internal,
                enable_ipv6=self.enable_ipv6,
                labels=self._labels,
                attachable=version_gte(self.client._version, '1.24') or None,
                check_duplicate=True,
            )
예제 #5
0
    def _get_container_create_options(self,
                                      override_options,
                                      number,
                                      one_off=False,
                                      previous_container=None):
        add_config_hash = (not one_off and not override_options)

        container_options = dict((k, self.options[k])
                                 for k in DOCKER_CONFIG_KEYS
                                 if k in self.options)
        override_volumes = override_options.pop('volumes', [])
        container_options.update(override_options)

        if not container_options.get('name'):
            container_options['name'] = self.get_container_name(
                number, one_off)

        container_options.setdefault('detach', True)

        # If a qualified hostname was given, split it into an
        # unqualified hostname and a domainname unless domainname
        # was also given explicitly. This matches behavior
        # until Docker Engine 1.11.0 - Docker API 1.23.
        if (version_lt(self.client.api_version, '1.23')
                and 'hostname' in container_options
                and 'domainname' not in container_options
                and '.' in container_options['hostname']):
            parts = container_options['hostname'].partition('.')
            container_options['hostname'] = parts[0]
            container_options['domainname'] = parts[2]

        if (version_gte(self.client.api_version, '1.25')
                and 'stop_grace_period' in self.options):
            container_options['stop_timeout'] = self.stop_timeout(None)

        if 'ports' in container_options or 'expose' in self.options:
            container_options['ports'] = build_container_ports(
                formatted_ports(container_options.get('ports', [])),
                self.options)

        if 'volumes' in container_options or override_volumes:
            container_options['volumes'] = list(
                set(container_options.get('volumes', []) + override_volumes))

        container_options['environment'] = merge_environment(
            self.options.get('environment'),
            override_options.get('environment'))

        binds, affinity = merge_volume_bindings(
            container_options.get('volumes') or [],
            self.options.get('tmpfs') or [], previous_container)
        override_options['binds'] = binds
        container_options['environment'].update(affinity)

        container_options['volumes'] = dict(
            (v.internal, {}) for v in container_options.get('volumes') or {})

        secret_volumes = self.get_secret_volumes()
        if secret_volumes:
            override_options['binds'].extend(v.repr() for v in secret_volumes)
            container_options['volumes'].update(
                (v.internal, {}) for v in secret_volumes)

        container_options['image'] = self.image_name

        container_options['labels'] = build_container_labels(
            container_options.get('labels', {}), self.labels(one_off=one_off),
            number, self.config_hash if add_config_hash else None)

        # Delete options which are only used in HostConfig
        for key in HOST_CONFIG_KEYS:
            container_options.pop(key, None)

        container_options['host_config'] = self._get_container_host_config(
            override_options, one_off=one_off)

        networking_config = self.build_default_networking_config()
        if networking_config:
            container_options['networking_config'] = networking_config

        container_options['environment'] = format_environment(
            container_options['environment'])
        return container_options