From 4fab7772957f750540b98db52444cbbe3ac273dd Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Fri, 7 Sep 2018 16:20:41 -0400 Subject: [PATCH] Make role support multiple container runtimes - add variable to be used for container runtime - correct Docker container removal task based on new output from docker_facts.py - add validation for supported container runtimes - update README Change-Id: If9f8488192d0cc04783f9069dc43113da204ca57 --- README.md | 29 +++++++++++++++--------- defaults/main.yml | 3 ++- tasks/container_cleanup.yml | 7 ++++++ tasks/{cleanup_docker.yml => docker.yml} | 2 +- tasks/podman.yml | 2 ++ 5 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 tasks/container_cleanup.yml rename tasks/{cleanup_docker.yml => docker.yml} (88%) create mode 100644 tasks/podman.yml diff --git a/README.md b/README.md index 1f2d970..3701f74 100644 --- a/README.md +++ b/README.md @@ -17,20 +17,25 @@ To fetch logs with this role, use the `fetch_logs.yml` tasks file. By default, e See `defaults/main.yml` for the dictionary of options to control logs that are fetched. -## Cleanup Docker ## +## Cleanup Container Items ## **WARNING:** This will delete images, containers, and volumes from the target system(s). -To perform the most common cleanup tasks -- delete dangling images and volumes and delete exited or dead containers -- use the `cleanup_docker.yml` tasks file. This role includes a `docker_facts` module for enumerating images, volumes, and containers. The filtered lists (one each for images, containers, and volumes) returned by this module is used to determine which items to remove. The module accepts a list of `k=v` filter arguments that will be passed to the `-f` option of Docker. Specifying multiple filters creates an `and` match, so all filters must match. +To perform the most common cleanup tasks --- delete dangling images and volumes and delete exited or dead containers --- use the `container_cleanup.yml` tasks file. -See Docker guides for [images](https://docs.docker.com/engine/reference/commandline/images/#filtering), [containers](https://docs.docker.com/engine/reference/commandline/ps/#filtering), and [volumes](https://docs.docker.com/engine/reference/commandline/volume_ls/#filtering) for filter options. +This role includes modules for listing image, volume, and container IDs. The filtered lists (one each for images, containers, and volumes) returned by this module are used to determine which items to remove. Specifying multiple filters creates an `and` match, so all filters must match. + +If using Docker, see these guides for [images](https://docs.docker.com/engine/reference/commandline/images/#filtering), [containers](https://docs.docker.com/engine/reference/commandline/ps/#filtering), and [volumes](https://docs.docker.com/engine/reference/commandline/volume_ls/#filtering) for filter options. ## Requirements ## - - ansible >= 2.4 - - docker-py >= 1.7.0 - - Docker API >= 1.20 + - ansible >= 2.4 + +If using Docker: + + - docker-py >= 1.7.0 + - Docker API >= 1.20 ## Role Variables ## @@ -39,6 +44,7 @@ See Docker guides for [images](https://docs.docker.com/engine/reference/commandl | Name | Default Value | Description | |-------------------|---------------------|----------------------| +| `operations_container_runtime` | `docker` | Container runtime to use. Currently supports `docker` and `podman`. | | `operations_image_filter` | `['dangling=true']` | List of image filters. | | `operations_volume_filter` | `['dangling=true']` | List of volume filters. | | `operations_container_filter` | `['status=exited', 'status=dead']` | List of container filters. | @@ -80,23 +86,23 @@ None - mariadb -### Cleanup Docker ### +### Cleanup Container Items ### - name: Cleanup dangling and dead images, containers, and volumes hosts: all tasks: - - name: Cleanup unused Docker images, containers, and volumes + - name: Cleanup unused images, containers, and volumes import_role: name: openstack-operations - tasks_from: cleanup_docker.yml + tasks_from: container_cleanup.yml - name: Use custom filters for cleaning hosts: all tasks: - - name: Cleanup unused Docker images, containers, and volumes + - name: Cleanup unused images, containers, and volumes import_role: name: openstack-operations - tasks_from: cleanup_docker.yml + tasks_from: container_cleanup.yml vars: operations_image_filters: - before=image1 @@ -108,6 +114,7 @@ None ### Fetch Logs ### + - hosts: all tasks: - name: Fetch logs diff --git a/defaults/main.yml b/defaults/main.yml index e831889..df9a997 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,5 @@ -# Cleanup Docker +# Cleanup Container +operations_container_runtime: docker operations_image_filter: - dangling=true operations_volume_filter: diff --git a/tasks/container_cleanup.yml b/tasks/container_cleanup.yml new file mode 100644 index 0000000..1826c1f --- /dev/null +++ b/tasks/container_cleanup.yml @@ -0,0 +1,7 @@ +- name: Ensure a valid container runtime is used + assert: + msg: Invalid container runtime specified. Only 'docker' and 'podman' are valid. + that: + - operations_container_runtime in ['docker', 'podman'] + +- include_tasks: "{{ operations_container_runtime }}.yml" diff --git a/tasks/cleanup_docker.yml b/tasks/docker.yml similarity index 88% rename from tasks/cleanup_docker.yml rename to tasks/docker.yml index 4b4c458..cf87704 100644 --- a/tasks/cleanup_docker.yml +++ b/tasks/docker.yml @@ -14,7 +14,7 @@ docker_container: name: "{{ item }}" state: absent - loop: "{{ docker.containers_filtered | map(attribute='id') | list }}" + loop: "{{ docker.containers_filtered }}" - name: Remove dangling volumes docker_volume: diff --git a/tasks/podman.yml b/tasks/podman.yml new file mode 100644 index 0000000..20d7ef0 --- /dev/null +++ b/tasks/podman.yml @@ -0,0 +1,2 @@ +- debug: + msg: Podman tasks here