def container_action(container_name: str, start: bool, stop: bool, restart: bool) -> None:
    """Starts, Stops, or Restarts a defined container.

    Args:
        container_name: Name of container to start, stop, or restart.
        start: If true, start the container.
        stop: If true, stop the container.
        restart: If true, restart the container
    """
    try:
        client = Docker(CONFIG_PATH)
        container = client.containers.get(container_name)
    except DockerError as err:
        LOGGER.error("Docker Error: %s", err.message)
        raise SystemExit(-1)

    if start:
        run = ("Starting Container: %s", "Started Container: %s", container.start)
    elif stop:
        run = ("Stopping Container: %s", "Stopped Container: %s", container.stop)
    elif restart:
        run = ("Restarting Container: %s", "Restarted Container: %s", container.restart)
    else:
        raise DockerError("must specify start, stop, or restart")

    LOGGER.info(run[0], container_name)
    run[2]()
    LOGGER.info(run[1], container_name)
Пример #2
0
def group_update(group_name: str) -> None:
    """Updates all containers in a group and rebuilds them.

    Args:
        group_name: Group name to update and rebuild.
    """
    client = Docker(CONFIG_PATH)
    group = _group_get(client, group_name)
    master, containers = _group_list(client, group)

    LOGGER.info("Updating and rebuilding members of group '%s'", group_name)

    if master:
        LOGGER.info("Updating container image for '%s'", master.info.name)
        master.image.pull()
    for container in containers:
        LOGGER.info("Updating container image for '%s'", container.info.name)
        container.image.pull()

    _group_stop(master, containers, True)

    _group_start(master, containers)

    LOGGER.info("Finished updating and rebuilding members of group '%s'",
                group_name)
def containers_list() -> None:
    """Returns a list of currently defined containers."""
    client = Docker(CONFIG_PATH)
    containers = client.containers.list()
    if containers:
        LOGGER.info('Currently Defined Containers:')
        for container in sorted(containers):
            LOGGER.info("    %s", container)
    else:
        LOGGER.info('No Currently Defined Containers')
Пример #4
0
def groups_list() -> None:
    """Returns a list of currently defined groups."""
    client = Docker(CONFIG_PATH)
    groups = client.groups.list()

    if groups:
        LOGGER.info('Currently Defined Groups:')
        for group in groups:
            LOGGER.info("    %s", group)
    else:
        LOGGER.info('No Currently Defined Groups')
Пример #5
0
def group_define(group_name: str) -> None:
    """Define a new container group

    Args:
        group_name: Name of group to define.
    """
    LOGGER.info("Defining Group: %s", group_name)

    client = Docker(CONFIG_PATH)
    client.groups.add(group_name)

    LOGGER.info("Defined Group: %s", group_name)
Пример #6
0
def group_stop(group_name: str) -> None:
    """Stops all containers in the specified group.

    Args:
        group_name: Group name to stop.
    """
    client = Docker(CONFIG_PATH)
    group = _group_get(client, group_name)
    master, containers = _group_list(client, group)

    LOGGER.info("Stopping Containers Group: '%s'", group_name)
    _group_stop(master, containers)
    LOGGER.info("Stopped Containers Group: '%s'", group_name)
Пример #7
0
def group_start(group_name: str) -> None:
    """Starts the specified group of containers.

    Args:
        group_name: Group name to start.
    """
    client = Docker(CONFIG_PATH)
    group = _group_get(client, group_name)
    master, containers = _group_list(client, group)

    LOGGER.info("Starting Containers Group: '%s'", group_name)
    _group_start(master, containers)
    LOGGER.info("Finished updating and rebuilding members of group '%s'",
                group_name)
def container_dump(container_name: str) -> None:
    """Dumps a containers definition file to the current directory.

    Args:
        container_name: Name of container.
    """
    LOGGER.info("Dumping container definition for '%s'", container_name)

    try:
        client = Docker(CONFIG_PATH)
        container = client.containers.get(container_name)
        definition_file = container.dump()
        LOGGER.info("Wrote container definition: %s", definition_file)
    except DockerError as err:
        LOGGER.error("Docker Error: %s", err.message)
        raise SystemExit(1)
def container_define(definition_file: str) -> None:
    """Define a new container

    Args:
        definition_file: Path to container definition file.
    """
    LOGGER.info("Defining New Container")
    try:
        client = Docker(CONFIG_PATH)
        container_name = client.containers.define(definition_file)
        LOGGER.info("Defined New Container: %s", container_name)
    except ConfigError as err:
        LOGGER.error("Configuration Error: %s", err.message)
        raise SystemExit(1)
    except DockerError as err:
        LOGGER.error("Docker Error: %s", err.message)
        raise SystemExit(1)
Пример #10
0
def container_tag(container_name: str, image_tag: str) -> None:
    """Sets or Updates a containers image tag.

    Args:
        container_name: Name of container to tag image of.
        image_tag: Image tag to set.
    """
    LOGGER.info("Setting tag '%s' for Container: %s", image_tag, container_name)

    try:
        client = Docker(CONFIG_PATH)
        container = client.containers.get(container_name)
        container.tag(image_tag)
        LOGGER.info("Tagged Container: %s", container_name)
    except DockerError as err:
        LOGGER.error("Docker Error: %s", err.message)
        raise SystemExit(-1)
Пример #11
0
def container_update(container_name: str) -> None:
    """Updates a containers image and rebuilds the container.

    Args:
        container_name: Name of container to update and rebuild.
    """
    LOGGER.info("Updating Container: %s", container_name)

    try:
        client = Docker(CONFIG_PATH)
        container = client.containers.get(container_name)
        container.update()
        container.rebuild()
        LOGGER.info("Updated Container: %s", container_name)
    except DockerError as err:
        LOGGER.error("Docker Error: %s", err.message)
        raise SystemExit(-1)
Пример #12
0
def group_set_master(group_name: str, master_name: str) -> None:
    """Sets `group_names` master to `master_name`

    Args:
        group_name: Name of group to set master of.
        master_name: Name of container to set as master for group.
    """
    LOGGER.info("Setting master of '%s' to '%s'", group_name, master_name)
    client = Docker(CONFIG_PATH)
    group = client.groups.get(group_name)
    containers = client.containers.list()

    if master_name not in containers:
        LOGGER.error("Specified master_name '%s' is not defined.", master_name)
        raise SystemExit(-1)

    group.master = master_name
    client.groups.save()

    LOGGER.info("Set master of '%s' to '%s'", group_name, master_name)
Пример #13
0
def group_info(group_name: str) -> None:
    """Returns information for `group_name`

    Args:
        group_name: Group name to retrieve information for.
    """
    client = Docker(CONFIG_PATH)
    try:
        group = client.groups.get(group_name)
    except DockerError as err:
        LOGGER.error(err.message)
        raise SystemExit(-1)

    LOGGER.info("Group: %s", group_name)
    if group.master:
        LOGGER.info("    Master: %s", group.master)
    if len(group.members) <= 0:
        LOGGER.info("    Members: None Defined.")
    else:
        LOGGER.info("    Members:")
        for member_name in sorted(group.members):
            LOGGER.info("        %s", member_name)