commit d91d3133391236eefa05e1fbec3f2881a60ce4e9 Author: Robert Kaussow Date: Sat Mar 21 12:40:49 2020 +0100 initial commit diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..5967685 --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,155 @@ +local PipelineLinting = { + kind: 'pipeline', + name: 'linting', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'ansible-later', + image: 'xoxys/ansible-later', + commands: [ + 'ansible-later', + ], + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, +}; + +local PipelineDeployment(scenario='centos7') = { + kind: 'pipeline', + name: 'testing-' + scenario, + platform: { + os: 'linux', + arch: 'amd64', + }, + concurrency: { + limit: 1, + }, + workspace: { + base: '/drone/src', + path: 'xoxys.docker_tidy', + }, + steps: [ + { + name: 'ansible-molecule', + image: 'xoxys/molecule:3', + pull: 'always', + environment: { + DO_API_KEY: { from_secret: 'do_api_key' }, + MOLECULE_CUSTOM_MODULES_REPO: 'https://gitea.rknet.org/ansible/custom_modules', + MOLECULE_CUSTOM_FILTERS_REPO: 'https://gitea.rknet.org/ansible/custom_filters', + }, + commands: [ + 'molecule test -s' + scenario, + ], + }, + ], + depends_on: [ + 'linting', + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +local PipelineDocumentation = { + kind: 'pipeline', + name: 'documentation', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'generate', + image: 'xoxys/ansible-doctor', + environment: { + ANSIBLE_DOCTOR_LOG_LEVEL: 'INFO', + ANSIBLE_DOCTOR_FORCE_OVERWRITE: true, + ANSIBLE_DOCTOR_EXCLUDE_FILES: 'molecule/', + ANSIBLE_DOCTOR_TEMPLATE: 'hugo-book', + ANSIBLE_DOCTOR_ROLE_NAME: '${DRONE_REPO_NAME#*.}', + ANSIBLE_DOCTOR_OUTPUT_DIR: '_docs/', + }, + }, + { + name: 'publish', + image: 'plugins/gh-pages', + settings: { + remote_url: 'https://gitea.rknet.org/ansible/xoxys.docker_tidy', + netrc_machine: 'gitea.rknet.org', + username: { from_secret: 'gitea_username' }, + password: { from_secret: 'gitea_token' }, + pages_directory: '_docs/', + target_branch: 'docs', + }, + when: { + ref: ['refs/heads/master'], + }, + }, + { + name: 'trigger', + image: 'plugins/downstream', + settings: { + server: 'https://drone.rknet.org', + token: { from_secret: 'drone_token' }, + fork: true, + repositories: [ + 'ansible/ansible-galaxy', + ], + }, + when: { + ref: ['refs/heads/master'], + }, + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, + depends_on: [ + 'testing-centos7', + ], +}; + +local PipelineNotification = { + kind: 'pipeline', + name: 'notification', + platform: { + os: 'linux', + arch: 'amd64', + }, + clone: { + disable: true, + }, + steps: [ + { + name: 'matrix', + image: 'plugins/matrix', + settings: { + homeserver: { from_secret: 'matrix_homeserver' }, + roomid: { from_secret: 'matrix_roomid' }, + template: 'Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}', + username: { from_secret: 'matrix_username' }, + password: { from_secret: 'matrix_password' }, + }, + }, + ], + depends_on: [ + 'documentation', + ], + trigger: { + status: ['success', 'failure'], + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +[ + PipelineLinting, + PipelineDeployment(scenario='centos7'), + PipelineDocumentation, + PipelineNotification, +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a576f84 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# ---> Ansible +*.retry +filter/plugins/ +library + +# ---> Python +__pycache__/ +*.py[cod] +*$py.class diff --git a/.later.yml b/.later.yml new file mode 100644 index 0000000..45911c9 --- /dev/null +++ b/.later.yml @@ -0,0 +1,18 @@ +--- +ansible: + custom_modules: + - iptables_raw + - openssl_pkcs12 + - proxmox_kvm2 + - ucr + - yum_versionlock + +rules: + exclude_files: + - molecule/ + - "LICENSE*" + - "**/*.md" + - "**/*.ini" + + exclude_filter: + - LINT0009 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bb3e998 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Robert Kaussow + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f94697c --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# xoxys.docker_tidy + +[![Build Status](https://img.shields.io/drone/build/ansible/xoxys.docker_tidy?logo=drone&server=https%3A%2F%2Fdrone.rknet.org)](https://drone.rknet.org/ansible/xoxys.docker_tidy) +[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) + +Setup a docker garbage collector. + +You can find the full documentation at [https://galaxy.geekdocs.de](https://galaxy.geekdocs.de/roles/cloud/docker_tidy/). + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +## Maintainers and Contributors + +[Robert Kaussow](https://gitea.rknet.org/xoxys) diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..d8fc2e8 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,24 @@ +# Standards: 0.1 +--- +# @var custodian_image:description: Docker image to use +custodian_image: toolhippie/docker-custodian:latest + +# @var custodian_interval:description: Interval for the systemd timer +custodian_interval: daily + +# @var custodian_max_container_age:description: Max container age +custodian_max_container_age: 3days + +# @var custodian_max_image_age:description: Max image age +custodian_max_image_age: 3days + +# @var custodian_dangling_volumes:description: Remove dangling volumes +custodian_dangling_volumes: False + +# @var custodian_exclude_image:description: List of images to exclude +custodian_exclude_image: [] + +# @var custodian_exclude_container_label:description: List of labels to exclude+ +custodian_exclude_container_label: [] + +... diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..2ccf41c --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,9 @@ +# Standards: 0.1 +--- +- name: Restart custodian + systemd: + name: custodian.timer + state: restarted + daemon_reload: True + +... diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..582e979 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,25 @@ +# Standards: 0.1 +--- +galaxy_info: +# @meta author:value: [Robert Kaussow](https://gitea.rknet.org/xoxys) + author: Robert Kaussow +# @meta description: > +# [![Source Code](https://img.shields.io/badge/gitea-source%20code-blue?logo=gitea&logoColor=white)](https://gitea.rknet.org/ansible/xoxys.docker_tidy) +# [![Build Status](https://img.shields.io/drone/build/ansible/xoxys.docker_tidy?logo=drone&server=https%3A%2F%2Fdrone.rknet.org)](https://drone.rknet.org/ansible/xoxys.docker_tidy) +# [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) +# +# Setup docker-tidy garbage collector. Keep docker hosts small. +# @end + description: Setup docker-tidy garbage collector + license: MIT + min_ansible_version: 2.8 + platforms: + - name: EL + versions: + - 7 + - 8 + galaxy_tags: + - docker + - tidy + - gc +dependencies: diff --git a/molecule/centos7/converge.yml b/molecule/centos7/converge.yml new file mode 100644 index 0000000..d62fd14 --- /dev/null +++ b/molecule/centos7/converge.yml @@ -0,0 +1,11 @@ +--- +- name: Converge (Stage 1) + hosts: all + roles: + - role: xoxys.python3 + - role: xoxys.docker_engine + +- name: Converge (Stage 2) + hosts: all + roles: + - role: xoxys.docker_tidy diff --git a/molecule/centos7/create.yml b/molecule/centos7/create.yml new file mode 100644 index 0000000..41f112d --- /dev/null +++ b/molecule/centos7/create.yml @@ -0,0 +1,87 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + ssh_user: root + ssh_port: 22 + + keypair_name: molecule_key + keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" + tasks: + - name: Create local keypair + user: + name: "{{ lookup('env', 'USER') }}" + generate_ssh_key: true + ssh_key_file: "{{ keypair_path }}" + register: local_keypair + + - name: Create remote keypair + digital_ocean_sshkey: + name: "{{ keypair_name }}" + ssh_pub_key: "{{ local_keypair.ssh_public_key }}" + state: present + register: remote_keypair + + - name: Create molecule instance(s) + digital_ocean_droplet: + name: "{{ item.name }}" + unique_name: true + region: "{{ item.region_id }}" + image: "{{ item.image_id }}" + size: "{{ item.size_id }}" + ssh_keys: "{{ remote_keypair.data.ssh_key.id }}" + wait: true + wait_timeout: 300 + state: present + register: server + loop: "{{ molecule_yml.platforms }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) creation to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: digitalocean_jobs + until: digitalocean_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config dict + set_fact: + instance_conf_dict: { + 'instance': "{{ item.data.droplet.name }}", + 'address': "{{ item.data.ip_address }}", + 'user': "{{ ssh_user }}", + 'port': "{{ ssh_port }}", + 'identity_file': "{{ keypair_path }}", + 'droplet_id': "{{ item.data.droplet.id }}", + 'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}", + } + loop: "{{ digitalocean_jobs.results }}" + register: instance_config_dict + when: server.changed | bool + + - name: Convert instance config dict to a list + set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + when: server.changed | bool + + - name: Dump instance config + copy: + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool + + - name: Wait for SSH + wait_for: + port: "{{ ssh_port }}" + host: "{{ item.address }}" + search_regex: SSH + delay: 10 + timeout: 320 + loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" diff --git a/molecule/centos7/destroy.yml b/molecule/centos7/destroy.yml new file mode 100644 index 0000000..19c8c93 --- /dev/null +++ b/molecule/centos7/destroy.yml @@ -0,0 +1,54 @@ +--- +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + tasks: + - block: + - name: Populate instance config + set_fact: + instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" + skip_instances: false + rescue: + - name: Populate instance config when file missing + set_fact: + instance_conf: {} + skip_instances: true + + - name: Destroy molecule instance(s) + digital_ocean_droplet: + name: "{{ item.instance }}" + id: "{{ item.droplet_id }}" + state: absent + register: server + loop: "{{ instance_conf | flatten(levels=1) }}" + when: not skip_instances + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: digitalocean_jobs + until: digitalocean_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + - name: Delete remote keypair + digital_ocean_sshkey: + fingerprint: "{{ item.ssh_key_id }}" + state: absent + loop: "{{ instance_conf | flatten(levels=1) }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + set_fact: + instance_conf: {} + + - name: Dump instance config + copy: + content: "{{ instance_conf | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool diff --git a/molecule/centos7/molecule.yml b/molecule/centos7/molecule.yml new file mode 100644 index 0000000..0182866 --- /dev/null +++ b/molecule/centos7/molecule.yml @@ -0,0 +1,16 @@ +--- +dependency: + name: galaxy +driver: + name: delegated +platforms: + - name: centos7-bitwardenrs + region_id: fra1 + image_id: centos-7-x64 + size_id: s-1vcpu-1gb +provisioner: + name: ansible +verifier: + name: testinfra + lint: | + flake8 --max-line-length=120 diff --git a/molecule/centos7/prepare.yml b/molecule/centos7/prepare.yml new file mode 100644 index 0000000..4b18d48 --- /dev/null +++ b/molecule/centos7/prepare.yml @@ -0,0 +1,9 @@ +--- +- name: Prepare + hosts: all + gather_facts: false + tasks: + - name: Install python for Ansible + raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) + become: true + changed_when: false diff --git a/molecule/centos7/requirements.yml b/molecule/centos7/requirements.yml new file mode 100644 index 0000000..4c0386e --- /dev/null +++ b/molecule/centos7/requirements.yml @@ -0,0 +1,15 @@ +--- +- src: https://gitea.rknet.org/ansible/xoxys.python3.git + name: xoxys.python3 + scm: git + version: master + +- src: https://gitea.rknet.org/ansible/xoxys.docker_engine.git + name: xoxys.docker_engine + scm: git + version: master + +- src: https://gitea.rknet.org/ansible/xoxys.postgres.git + name: xoxys.postgres + scm: git + version: master diff --git a/molecule/centos7/tests/test_default.py b/molecule/centos7/tests/test_default.py new file mode 100644 index 0000000..ff09ece --- /dev/null +++ b/molecule/centos7/tests/test_default.py @@ -0,0 +1,16 @@ +import os + +import testinfra.utils.ansible_runner + +import warnings +warnings.filterwarnings("ignore", category=DeprecationWarning) + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_tidy_running_and_enabled(host): + service = host.service("custodian.timer") + + assert service.is_running + assert service.is_enabled diff --git a/molecule/default b/molecule/default new file mode 120000 index 0000000..2fdf3e8 --- /dev/null +++ b/molecule/default @@ -0,0 +1 @@ +centos7 \ No newline at end of file diff --git a/molecule/pytest.ini b/molecule/pytest.ini new file mode 100644 index 0000000..c24fe5b --- /dev/null +++ b/molecule/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..394c674 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,31 @@ +# Standards: 0.1 +--- +- name: Write timer file + notify: + - Restart custodian + template: + src: timer.j2 + dest: /etc/systemd/system/custodian.timer + tags: + - custodian + +- name: Write service file + notify: + - Restart custodian + template: + src: service.j2 + dest: /etc/systemd/system/custodian.service + tags: + - custodian + +- name: Start custodian timer + systemd: + name: custodian.timer + state: started + daemon_reload: True + masked: False + enabled: True + tags: + - custodian + +... diff --git a/templates/service.j2 b/templates/service.j2 new file mode 100644 index 0000000..1c5773c --- /dev/null +++ b/templates/service.j2 @@ -0,0 +1,33 @@ +[Unit] +Description=Custodian + +Wants=docker.service +After=docker.service + +[Service] +Type=oneshot +EnvironmentFile=/etc/environment + +ExecStartPre=/bin/sh -c '/usr/bin/docker ps | /bin/grep %p 1> /dev/null && /usr/bin/docker kill %p || true' +ExecStartPre=/bin/sh -c '/usr/bin/docker ps -a | /bin/grep %p 1> /dev/null && /usr/bin/docker rm %p || true' +ExecStartPre=/usr/bin/docker pull {{ custodian_image }} +ExecStart=/usr/bin/docker run --rm \ + --name %p \ + --hostname %p \ + --label traefik.enable=false \ + --volume /var/run/docker.sock:/var/run/docker.sock \ + {{ custodian_image }} \ +{% if custodian_dangling_volumes %} + --dangling-volumes \ +{% endif %} +{% for item in custodian_exclude_image %} + --exclude-image {{ item }} \ +{% endfor %} +{% for item in custodian_exclude_container_label %} + ----exclude-container-label {{ item }} \ +{% endfor %} + --max-container-age {{ custodian_max_container_age }} \ + --max-image-age {{ custodian_max_image_age }} + +[Install] +WantedBy=multi-user.target diff --git a/templates/timer.j2 b/templates/timer.j2 new file mode 100644 index 0000000..33f98c4 --- /dev/null +++ b/templates/timer.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Custodian + +[Timer] +OnCalendar={{ custodian_interval }} +Persistent=true + +[Install] +WantedBy=timers.target