Пример #1
0
def remove_container(params, daemon_client=None, **_):
    """ cloudify.docker.container type delete lifecycle operation.
        Any properties and runtime_properties set in the create,
        start, and stop lifecycle operations also available in
        delete.
        Remove a container. Similar to the docker rm command.

    :param v: Remove the volumes associated with the container.
    :param link: Remove the specified link and not the underlying container.
    :param force: force the removal of a running container (uses SIGKILL)
    :param daemon_client: optional configuration for client creation
    """
    daemon_client = daemon_client or {}
    client = docker_client.get_client(daemon_client)

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

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

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

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

    del(ctx.instance.runtime_properties['container_id'])

    ctx.logger.info('Removed container {}'.format(container_id))
Пример #2
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))
Пример #3
0
def start(params,
          processes_to_wait_for,
          retry_interval,
          daemon_client=None,
          **_):
    """ cloudify.docker.container type start lifecycle operation.
        Any properties and runtime_properties set in the create
        lifecycle operation also available in start.
        Similar to the docker start command, but doesn't support
        attach options.

    :param daemon_client: optional configuration for client creation
    :param retry_interval: The number of seconds between retries during
        the wait_for_processes bit.
    """

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

    if ctx.node.properties['use_external_resource']:
        if utils.get_container_dictionary(client) is None:
            raise NonRecoverableError('{} does not exist.'.format(
                ctx.instance.runtime_properties.get('container_id')))

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

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

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

    ctx.logger.info('Container started: {}.'.format(response))

    if params.get('processes_to_wait_for'):
        utils.wait_for_processes(processes_to_wait_for, retry_interval, client)

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

    if utils.get_container_dictionary(client):
        inspect_output = utils.inspect_container(client)
        if inspect_output is not None:
            ctx.instance.runtime_properties['ports'] = \
                inspect_output.get('Ports', None)
            ctx.instance.runtime_properties['network_settings'] = \
                inspect_output.get('NetworkSettings', None)
        else:
            ctx.logger.info(
                'Failed to get inspect_output: {0}'.format(arguments))

    top_info = utils.get_top_info(client)

    ctx.logger.info('Container: {0} Forwarded ports: {1} Top: {2}.'.format(
        ctx.instance.runtime_properties['container_id'], 'Ports', top_info))
Пример #4
0
def start(params, processes_to_wait_for, retry_interval,
          daemon_client=None, **_):
    """ cloudify.docker.container type start lifecycle operation.
        Any properties and runtime_properties set in the create
        lifecycle operation also available in start.
        Similar to the docker start command, but doesn't support
        attach options.

    :param daemon_client: optional configuration for client creation
    :param retry_interval: The number of seconds between retries during
        the wait_for_processes bit.
    """

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

    if ctx.node.properties['use_external_resource']:
        if utils.get_container_dictionary(client) is None:
            raise NonRecoverableError('{} does not exist.'.format(
                ctx.instance.runtime_properties.get('container_id')))

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

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

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

    ctx.logger.info('Container started: {}.'.format(response))

    if params.get('processes_to_wait_for'):
        utils.wait_for_processes(processes_to_wait_for, retry_interval,
                                 client)

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

    if utils.get_container_dictionary(client):
        inspect_output = utils.inspect_container(client)
        if inspect_output is not None:
            ctx.instance.runtime_properties['ports'] = \
                inspect_output.get('Ports', None)
            ctx.instance.runtime_properties['network_settings'] = \
                inspect_output.get('NetworkSettings', None)
        else:
            ctx.logger.info('Failed to get inspect_output: {0}'.format(arguments))

    top_info = utils.get_top_info(client)

    ctx.logger.info('Container: {0} Forwarded ports: {1} Top: {2}.'.format(
        ctx.instance.runtime_properties['container_id'],
        'Ports', top_info))
Пример #5
0
def create_container(params, daemon_client=None, **_):
    """ cloudify.docker.container type create lifecycle operation.
        Creates a container that can then be .start() ed.

    :node_property use_external_resource: True or False. Use existing
        instead of creating a new resource.
    :node_property resource_id:  The container name.
    :node_property image: The image to run.
    :node_property ports: A dictionary with pairs of port bindings
        as provided to the start function. The create function
        will pass only the dict keys as the ports parameter and
        the start function will pass the pairs as port bindings.
    :param daemon_client: optional configuration for client creation
    """

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

    if ctx.node.properties['use_external_resource']:
        if 'name' not in ctx.node.properties:
            raise NonRecoverableError('Use external resource, but '
                                      'no resource id provided.')
        ctx.instance.runtime_properties['container_id'] = \
            utils.get_container_id_from_name(
                ctx.node.properties['name'], client)
        return

    arguments = dict()
    arguments['name'] = ctx.node.properties['name']
    arguments['image'] = get_image(client)
    arguments.update(params)

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

    try:
        container = client.create_container(**arguments)
    except APIError as e:
        raise NonRecoverableError('Error while creating container: {0}'.format(
            str(e)))

    container_id = container.get('Id')
    if ctx.node.properties['connect_container_to_network']:
        for network_id in ctx.node.properties['connect_container_to_network']:
            if network_id.find("=") == -1:
                client.connect_container_to_network(container_id, network_id)
            else:
                name = network_id.split("=")[0]
                ipaddr = network_id.split("=")[1]
                client.connect_container_to_network(container_id,
                                                    name,
                                                    ipv4_address=ipaddr)

    ctx.instance.runtime_properties['container_id'] = container_id
    ctx.logger.info('Container created: {0}.'.format(container_id))
Пример #6
0
def create_container(params, daemon_client=None, **_):
    """ cloudify.docker.container type create lifecycle operation.
        Creates a container that can then be .start() ed.

    :node_property use_external_resource: True or False. Use existing
        instead of creating a new resource.
    :node_property resource_id:  The container name.
    :node_property image: The image to run.
    :node_property ports: A dictionary with pairs of port bindings
        as provided to the start function. The create function
        will pass only the dict keys as the ports parameter and
        the start function will pass the pairs as port bindings.
    :param daemon_client: optional configuration for client creation
    """

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

    if ctx.node.properties['use_external_resource']:
        if 'name' not in ctx.node.properties:
            raise NonRecoverableError(
                'Use external resource, but '
                'no resource id provided.')
        ctx.instance.runtime_properties['container_id'] = \
            utils.get_container_id_from_name(
                ctx.node.properties['name'], client)
        return

    arguments = dict()
    arguments['name'] = ctx.node.properties['name']
    arguments['image'] = get_image(client)
    arguments.update(params)

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

    try:
        container = client.create_container(**arguments)
    except APIError as e:
        raise NonRecoverableError(
            'Error while creating container: {0}'.format(str(e)))

    container_id = container.get('Id')
    if ctx.node.properties['connect_container_to_network']:
        for network_id in ctx.node.properties['connect_container_to_network']:
            if network_id.find("=") == -1:
                client.connect_container_to_network(container_id, network_id)
            else: 
                name = network_id.split("=")[0]
                ipaddr = network_id.split("=")[1]
                client.connect_container_to_network(container_id, name, ipv4_address=ipaddr)
        
    ctx.instance.runtime_properties['container_id'] = container_id
    ctx.logger.info('Container created: {0}.'.format(container_id))