Example #1
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))
Example #2
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))