<commit_before>'''
The LXD modules manage LXD containers
'''

from __future__ import unicode_literals

from pyinfra.api import operation


def get_container_named(name, containers):
    for container in containers:
        if container['name'] == name:
            return container
    else:
        return None


@operation
def container(
    state, host, name,
    present=True, image='ubuntu:16.04',
):
    '''
    Add/remove LXD containers.

    Note: does not check if an existing container is based on the specified
    image.

    + name: name of the container
    + image: image to base the container on
    + present: whether the container should be present or absent

    Example:

    .. code:: python

        lxd.container(
            name='Add an ubuntu container',
            id='ubuntu19',
            image='ubuntu:19.10',
        )
    '''

    container = get_container_named(name, host.fact.lxd_containers)

    # Container exists and we don't want it
    if container and not present:
        if container['status'] == 'Running':
            yield 'lxc stop {0}'.format(name)

        # Command to remove the container:
        yield 'lxc delete {0}'.format(name)

    # Container doesn't exist and we want it
    if not container and present:
        # Command to create the container:
        yield 'lxc launch {image} {name}'.format(name=name, image=image)<commit_msg>Update `lxd.container` argument: `name` -> `id`.<commit_after>'''
The LXD modules manage LXD containers
'''

from __future__ import unicode_literals

from pyinfra.api import operation


def get_container_named(name, containers):
    for container in containers:
        if container['name'] == name:
            return container
    else:
        return None


@operation
def container(
    state, host, id,
    present=True, image='ubuntu:16.04',
):
    '''
    Add/remove LXD containers.

    Note: does not check if an existing container is based on the specified
    image.

    + id: name/identifier for the container
    + image: image to base the container on
    + present: whether the container should be present or absent

    Example:

    .. code:: python

        lxd.container(
            name='Add an ubuntu container',
            id='ubuntu19',
            image='ubuntu:19.10',
        )
    '''

    container = get_container_named(id, host.fact.lxd_containers)

    # Container exists and we don't want it
    if container and not present:
        if container['status'] == 'Running':
            yield 'lxc stop {0}'.format(id)

        # Command to remove the container:
        yield 'lxc delete {0}'.format(id)

    # Container doesn't exist and we want it
    if not container and present:
        # Command to create the container:
        yield 'lxc launch {image} {id}'.format(id=id, image=image)
{'fraction_non_alphanumeric': 0.09435970202199362, 'fraction_numerical': 0.008513657325292657, 'mean_word_length': 2.5516372795969775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5292745', 'n_tokens_mistral': 822, 'n_tokens_neox': 764, 'n_words': 283}