Exemple #1
0
 def container_show(self, context, container_uuid):
     LOG.debug("container_show %s", container_uuid)
     with docker_utils.docker_for_container(context,
                                            container_uuid) as docker:
         container = objects.Container.get_by_uuid(context, container_uuid)
         try:
             docker_id = self._find_container_by_name(docker,
                                                      container_uuid)
             if not docker_id:
                 LOG.exception(_LE("Can not find docker instance with %s,"
                                   "set it to Error status"),
                               container_uuid)
                 container.status = fields.ContainerStatus.ERROR
                 container.save()
                 return container
             result = docker.inspect_container(docker_id)
             status = result.get('State')
             if status:
                 if status.get('Error') is True:
                     container.status = fields.ContainerStatus.ERROR
                 elif status.get('Paused'):
                     container.status = fields.ContainerStatus.PAUSED
                 elif status.get('Running'):
                     container.status = fields.ContainerStatus.RUNNING
                 else:
                     container.status = fields.ContainerStatus.STOPPED
                 container.save()
             return container
         except errors.APIError as api_error:
             error_message = str(api_error)
             if '404' in error_message:
                 container.status = fields.ContainerStatus.ERROR
                 container.save()
                 return container
             raise
Exemple #2
0
    def container_create(self, context, container):
        with docker_utils.docker_for_container(context, container) as docker:
            name = container.name
            container_uuid = container.uuid
            image = container.image
            LOG.debug('Creating container with image %s name %s', image, name)
            try:
                image_repo, image_tag = docker_utils.parse_docker_image(image)
                docker.pull(image_repo, tag=image_tag)
                docker.inspect_image(self._encode_utf8(container.image))
                kwargs = {'name': name,
                          'hostname': container_uuid,
                          'command': container.command,
                          'environment': container.environment}
                if docker_utils.is_docker_api_version_atleast(docker, '1.19'):
                    if container.memory is not None:
                        kwargs['host_config'] = {'mem_limit':
                                                 container.memory}
                else:
                    kwargs['mem_limit'] = container.memory

                docker.create_container(image, **kwargs)
                container.status = fields.ContainerStatus.STOPPED
                return container
            except errors.APIError:
                container.status = fields.ContainerStatus.ERROR
                raise
            finally:
                container.save()
Exemple #3
0
 def container_logs(self, context, container_uuid):
     LOG.debug("container_logs %s", container_uuid)
     with docker_utils.docker_for_container(context,
                                            container_uuid) as docker:
         docker_id = self._find_container_by_name(docker,
                                                  container_uuid)
         return {'output': docker.logs(docker_id)}
    def test_docker_for_container_tls_disabled(self, mock_get_bay_by_name,
                                               mock_get_baymodel_by_uuid,
                                               mock_docker_client):
        mock_container = mock.MagicMock()
        mock_bay = mock.MagicMock()
        mock_bay.api_address = 'tcp://1.2.3.4:2376'
        mock_get_bay_by_name.return_value = mock_bay
        mock_baymodel = mock.MagicMock()
        mock_baymodel.tls_disabled = True
        mock_get_baymodel_by_uuid.return_value = mock_baymodel
        mock_docker = mock.MagicMock()
        mock_docker_client.return_value = mock_docker

        with docker_utils.docker_for_container(mock.sentinel.context,
                                               mock_container) as docker:
            self.assertEqual(mock_docker, docker)

        mock_get_bay_by_name.assert_called_once_with(mock.sentinel.context,
                                                     mock_container.bay_uuid)
        mock_get_baymodel_by_uuid.assert_called_once_with(
            mock.sentinel.context, mock_bay.baymodel_id)
        mock_docker_client.assert_called_once_with(
            'tcp://1.2.3.4:2376',
            CONF.docker.docker_remote_api_version,
            CONF.docker.default_timeout)
Exemple #5
0
 def container_delete(self, context, container_uuid):
     LOG.debug("container_delete %s", container_uuid)
     with docker_utils.docker_for_container(context,
                                            container_uuid) as docker:
         docker_id = self._find_container_by_name(docker,
                                                  container_uuid)
         if not docker_id:
             return None
         return docker.remove_container(docker_id)
Exemple #6
0
 def _container_action(self, context, container_uuid, status, docker_func):
     LOG.debug("%s container %s ...", docker_func, container_uuid)
     with docker_utils.docker_for_container(context,
                                            container_uuid) as docker:
         docker_id = self._find_container_by_name(docker,
                                                  container_uuid)
         result = getattr(docker, docker_func)(docker_id)
         container = objects.Container.get_by_uuid(context,
                                                   container_uuid)
         container.status = status
         container.save()
         return result
Exemple #7
0
 def container_exec(self, context, container_uuid, command):
     LOG.debug("container_exec %s command %s",
               container_uuid, command)
     with docker_utils.docker_for_container(context,
                                            container_uuid) as docker:
         docker_id = self._find_container_by_name(docker,
                                                  container_uuid)
         if docker_utils.is_docker_library_version_atleast('1.2.0'):
             create_res = docker.exec_create(docker_id, command, True,
                                             True, False)
             exec_output = docker.exec_start(create_res, False, False,
                                             False)
         else:
             exec_output = docker.execute(docker_id, command)
         return {'output': exec_output}
    def test_docker_for_container_uuid(self, mock_get_container_by_uuid,
                                       mock_get_bay_by_name,
                                       mock_get_baymodel_by_uuid,
                                       mock_cert_manager,
                                       mock_docker_client):
        mock_container = mock.MagicMock()
        mock_container.uuid = '8e48ffb1-754d-4f21-bdd0-1a39bf796389'
        mock_get_container_by_uuid.return_value = mock_container
        mock_bay = mock.MagicMock()
        mock_bay.api_address = 'https://1.2.3.4:2376'
        mock_get_bay_by_name.return_value = mock_bay
        mock_baymodel = mock.MagicMock()
        mock_baymodel.tls_disabled = False
        mock_get_baymodel_by_uuid.return_value = mock_baymodel
        mock_ca_cert = mock.MagicMock()
        mock_magnum_key = mock.MagicMock()
        mock_magnum_cert = mock.MagicMock()
        mock_cert_manager.create_client_files.return_value = (
            mock_ca_cert, mock_magnum_key, mock_magnum_cert
        )

        mock_docker = mock.MagicMock()
        mock_docker_client.return_value = mock_docker

        with docker_utils.docker_for_container(
                mock.sentinel.context, mock_container.uuid) as docker:
            self.assertEqual(mock_docker, docker)

        mock_get_container_by_uuid.assert_called_once_with(
            mock.sentinel.context, mock_container.uuid
        )
        mock_get_bay_by_name.assert_called_once_with(mock.sentinel.context,
                                                     mock_container.bay_uuid)
        mock_get_baymodel_by_uuid.assert_called_once_with(
            mock.sentinel.context, mock_bay.baymodel_id)
        mock_docker_client.assert_called_once_with(
            'https://1.2.3.4:2376',
            CONF.docker.docker_remote_api_version,
            CONF.docker.default_timeout,
            ca_cert=mock_ca_cert.name,
            client_key=mock_magnum_key.name,
            client_cert=mock_magnum_cert.name)
Exemple #9
0
 def container_create(self, context, container):
     with docker_utils.docker_for_container(context, container) as docker:
         name = container.name
         container_uuid = container.uuid
         image = container.image
         LOG.debug('Creating container with image %s name %s'
                   % (image, name))
         try:
             image_repo, image_tag = docker_utils.parse_docker_image(image)
             docker.pull(image_repo, tag=image_tag)
             docker.inspect_image(self._encode_utf8(container.image))
             docker.create_container(image, name=name,
                                     hostname=container_uuid,
                                     command=container.command,
                                     mem_limit=container.memory)
             container.status = fields.ContainerStatus.STOPPED
             return container
         except errors.APIError:
             container.status = fields.ContainerStatus.ERROR
             raise
         finally:
             container.save()