Esempio n. 1
0
def stop(retry_interval, params, daemon_client=None, **_):
    """ cloudify.docker.container type stop lifecycle operation.
        Stops a container. Similar to the docker stop command.
        Any properties and runtime_properties set in the create
        and start lifecycle operations also available in stop.

    :param daemon_client: optional configuration for client creation
    :param timeout: Timeout in seconds to wait for the container to stop before
        sending a SIGKILL.
    """

    daemon_client = daemon_client or {}
    client = docker_client.get_client(daemon_client)

    container_id = ctx.instance.runtime_properties['container_id']
    ctx.logger.info('Stopping container: {}'.format(container_id))

    container_id = ctx.instance.runtime_properties['container_id']
    arguments = {'container': container_id}
    arguments.update(params)

    ctx.logger.info('Stop arguments: {0}'.format(arguments))

    try:
        client.stop(**arguments)
    except APIError as e:
        raise NonRecoverableError(
            'Failed to start container: {0}.'.format(str(e)))

    if 'Exited' not in utils.check_container_status(client):
        raise RecoverableError('Container still running. Retyring.',
                               retry_after=retry_interval)

    ctx.logger.info('Stopped container: {}'.format(container_id))
    def test_check_container_status(self):

        name = 'test_check_container_status'
        client = self.get_docker_client()
        self.pull_image(client)
        ctx = self.get_mock_context(name)

        for image in self.get_docker_images(client):
            if 'docker-test-image:latest' in \
                    self.get_tags_for_docker_image(image):
                image_id = self.get_id_from_image(image)

        container = self.create_container(client, name, image_id)

        ctx.instance.runtime_properties['container_id'] = container.get('Id')
        client.start(name)
        out = utils.check_container_status(client, ctx=ctx)
        client.stop(container=container, timeout=1)
        client.remove_container(container=container)
        self.assertIn('Up', out)
Esempio n. 3
0
    def test_check_container_status(self):

        name = 'test_check_container_status'
        client = self.get_docker_client()
        self.pull_image(client)
        ctx = self.get_mock_context(name)

        for image in self.get_docker_images(client):
            if 'docker-test-image:latest' in \
                    self.get_tags_for_docker_image(image):
                image_id = self.get_id_from_image(image)

        container = self.create_container(client, name, image_id)

        ctx.instance.runtime_properties['container_id'] = container.get('Id')
        client.start(name)
        out = utils.check_container_status(client, ctx=ctx)
        client.stop(container=container, timeout=1)
        client.remove_container(
            container=container)
        self.assertIn('Up', out)
    def test_check_container_status(self):

        name = 'test_check_container_status'
        client = self.get_docker_client()
        self.pull_image(client)
        ctx = self.get_mock_context(name)
        current_ctx.set(ctx=ctx)

        for image in self.get_docker_images(client):
            if '{0}:latest'.format(TEST_IMAGE) in \
                    self.get_tags_for_docker_image(image):
                image_id = self.get_id_from_image(image)

        container = self.create_container(client, name, image_id)
        self.addCleanup(client.remove_container, container=container)
        self.addCleanup(client.stop, container=container, timeout=1)

        ctx.instance.runtime_properties['container_id'] = container.get('Id')
        client.start(container)
        out = utils.check_container_status(client)
        self.assertIn('Up', out)