From c83e667106aab7d05e294566f628bfc188804368 Mon Sep 17 00:00:00 2001 From: Bodo Schulz Date: Wed, 10 May 2023 19:56:21 +0200 Subject: [PATCH] update build hooks - update github workflows - add dependecy to ansible collections - bodsch.scm - bodsch.core - update molecule config - update doc --- .github/workflows/linter.yml | 35 ++++++++++++++++++++++++----- .github/workflows/main.yml | 26 ++++++++++++---------- Makefile | 6 +++-- README.md | 16 +++++++++++++ collections.yml | 6 +++++ filter_plugins/types.py | 28 ----------------------- hooks/converge | 4 +--- hooks/destroy | 4 +--- hooks/lint | 4 +--- hooks/{_tox_base => molecule.rc} | 0 hooks/test | 3 +++ hooks/tox.sh | 26 ++++++++++++++++++++++ hooks/verify | 4 +--- molecule/default/converge.yml | 5 ++++- molecule/default/molecule.yml | 10 ++++++--- molecule/default/prepare.yml | 34 +++++++++++++++++++--------- molecule/default/requirements.yml | 4 ++-- tasks/install.yml | 10 ++++----- tasks/prepare.yml | 37 +++++++++---------------------- templates/ansible_facts.j2 | 8 ------- templates/trickster.d/caches.j2 | 12 +++++----- test-requirements.txt | 3 ++- vars/archlinux.yml | 3 ++- vars/debian.yml | 3 +-- vars/main.yml | 4 +++- vars/redhat.yml | 2 +- 26 files changed, 169 insertions(+), 128 deletions(-) create mode 100644 collections.yml delete mode 100644 filter_plugins/types.py rename hooks/{_tox_base => molecule.rc} (100%) create mode 100755 hooks/test create mode 100755 hooks/tox.sh delete mode 100644 templates/ansible_facts.j2 diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 5377b53..6f78e40 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -3,13 +3,38 @@ name: code linter on: + schedule: + - cron: "40 0 * * 0" workflow_dispatch: - pull_request: + inputs: + logLevel: + description: 'Log level' + required: true + default: 'warning' + type: choice + options: + - info + - warning + - debug push: branches: - - main - - testing - - feature/** + - 'main' + - 'feature/**' + - '!doc/**' + paths: + - "!Makefile" + - "!README.md" + - "tasks/**" + pull_request: + branches: + - 'main' + - 'feature/**' + - '!doc/**' + paths: + - "!Makefile" + - "!README.md" + - "tasks/**" + jobs: lint: name: linting @@ -17,8 +42,6 @@ jobs: steps: - name: 🛎 Checkout uses: actions/checkout@v3 - with: - fetch-depth: 0 - name: lint uses: docker://ghcr.io/github/super-linter:slim-v4 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 687bd91..9c9b760 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,15 +2,9 @@ name: CI on: - schedule: - - cron: "0 1 * * 0" - workflow_dispatch: workflow_run: workflows: - "code linter" - branches: - - main - - feature/** types: - completed @@ -29,8 +23,9 @@ jobs: image: - archlinux:latest ansible-version: - - '5.1' - '6.1' + scenario: + - default steps: - name: check out the codebase. @@ -50,8 +45,11 @@ jobs: - name: test with tox run: | - tox -e ansible_$(printf "${{ matrix.ansible-version }}") \ - -- molecule test --scenario-name default + make \ + test \ + -e TOX_SCENARIO="${{ matrix.scenario }}" \ + -e TOX_ANSIBLE="ansible_${{ matrix.ansible-version }}" \ + -e DISTRIBUTION="${{ matrix.image }}" env: PY_COLORS: '1' ANSIBLE_FORCE_COLOR: '1' @@ -69,8 +67,9 @@ jobs: - debian:11 - ubuntu:20.04 ansible-version: - - '5.1' - '6.1' + scenario: + - default steps: - name: check out the codebase. @@ -90,8 +89,11 @@ jobs: - name: test with tox run: | - tox -e ansible_$(printf "${{ matrix.ansible-version }}") \ - -- molecule test --scenario-name default + make \ + test \ + -e TOX_SCENARIO="${{ matrix.scenario }}" \ + -e TOX_ANSIBLE="ansible_${{ matrix.ansible-version }}" \ + -e DISTRIBUTION="${{ matrix.image }}" env: PY_COLORS: '1' ANSIBLE_FORCE_COLOR: '1' diff --git a/Makefile b/Makefile index 40857c8..3abaf48 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,8 @@ # export TOX_SCENARIO ?= default -# export TOX_PYTHON ?= py310 export TOX_ANSIBLE ?= ansible_6.1 -.PHONY: converge destroy verify lint +.PHONY: converge destroy verify test lint default: converge @@ -16,5 +15,8 @@ destroy: verify: @hooks/verify +test: + @hooks/test + lint: @hooks/lint diff --git a/README.md b/README.md index c4d5941..f605284 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,22 @@ This ansible role installs and configure Trickster. [releases]: https://github.com/bodsch/ansible-trickster/releases [quality]: https://galaxy.ansible.com/bodsch/trickster +## Requirements & Dependencies + +Ansible Collections + +- [bodsch.core](https://github.com/bodsch/ansible-collection-core) +- [bodsch.scm](https://github.com/bodsch/ansible-collection-scm) + +```bash +ansible-galaxy collection install bodsch.core +ansible-galaxy collection install bodsch.scm +``` +or +```bash +ansible-galaxy collection install --requirements-file collections.yml +``` + ## Usage diff --git a/collections.yml b/collections.yml new file mode 100644 index 0000000..9297677 --- /dev/null +++ b/collections.yml @@ -0,0 +1,6 @@ +--- + +collections: + - name: bodsch.core + version: ">=1.0.17" + - name: bodsch.scm diff --git a/filter_plugins/types.py b/filter_plugins/types.py deleted file mode 100644 index f835637..0000000 --- a/filter_plugins/types.py +++ /dev/null @@ -1,28 +0,0 @@ -# python 3 headers, required if submitting to Ansible - -from __future__ import (absolute_import, print_function) -__metaclass__ = type - -from ansible.utils.display import Display - -# https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html -# https://blog.oddbit.com/post/2019-04-25-writing-ansible-filter-plugins/ - -display = Display() - - -class FilterModule(object): - """ - Ansible file jinja2 tests - """ - - def filters(self): - return { - 'type': self.var_type, - } - - def var_type(self, var): - ''' - Get the type of a variable - ''' - return type(var).__name__ diff --git a/hooks/converge b/hooks/converge index 5df5ad6..0c50932 100755 --- a/hooks/converge +++ b/hooks/converge @@ -1,5 +1,3 @@ #!/usr/bin/env bash -. hooks/_tox_base - -tox ${TOX_OPTS} -- molecule converge ${TOX_ARGS} +hooks/tox.sh "converge" diff --git a/hooks/destroy b/hooks/destroy index 98fcf16..b4a3f8d 100755 --- a/hooks/destroy +++ b/hooks/destroy @@ -1,5 +1,3 @@ #!/usr/bin/env bash -. hooks/_tox_base - -tox ${TOX_OPTS} -- molecule destroy ${TOX_ARGS} +hooks/tox.sh "destroy" diff --git a/hooks/lint b/hooks/lint index 6cf7ff3..ef226a0 100755 --- a/hooks/lint +++ b/hooks/lint @@ -1,5 +1,3 @@ #!/usr/bin/env bash -. hooks/_tox_base - -tox ${TOX_OPTS} -- molecule lint ${TOX_ARGS} +hooks/tox.sh "lint" diff --git a/hooks/_tox_base b/hooks/molecule.rc similarity index 100% rename from hooks/_tox_base rename to hooks/molecule.rc diff --git a/hooks/test b/hooks/test new file mode 100755 index 0000000..2869139 --- /dev/null +++ b/hooks/test @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +hooks/tox.sh "test" diff --git a/hooks/tox.sh b/hooks/tox.sh new file mode 100755 index 0000000..62bb777 --- /dev/null +++ b/hooks/tox.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +. hooks/molecule.rc + +TOX_TEST="${1}" + +if [ -f "./collections.yml" ] +then + for collection in $(grep -v "#" collections.yml | grep "^ - name: " | awk -F ': ' '{print $2}') + do + collections_installed="$(ansible-galaxy collection list | grep ${collection} 2> /dev/null)" + + if [ -z "${collections_installed}" ] + then + echo "Install the required collection '${collection}'" + ansible-galaxy collection install ${collection} + else + collection_version=$(echo "${collections_installed}" | awk -F ' ' '{print $2}') + + echo "The required collection '${collection}' is installed in version ${collection_version}." + fi + done + echo "" +fi + +tox ${TOX_OPTS} -- molecule ${TOX_TEST} ${TOX_ARGS} diff --git a/hooks/verify b/hooks/verify index 79a38d4..5f436af 100755 --- a/hooks/verify +++ b/hooks/verify @@ -1,5 +1,3 @@ #!/usr/bin/env bash -. hooks/_tox_base - -tox ${TOX_OPTS} -- molecule verify ${TOX_ARGS} +hooks/tox.sh "verify" diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 7fe2491..3286955 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -1,6 +1,9 @@ --- -- hosts: instance + +- name: converge + hosts: instance any_errors_fatal: false + become: false environment: NETRC: '' diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 15faebe..59d2a68 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -23,7 +23,7 @@ platforms: docker_networks: - name: prometheus ipam_config: - - subnet: "10.11.0.0/16" + - subnet: "10.11.0.0/24" gateway: "10.11.0.254" networks: - name: prometheus @@ -57,12 +57,16 @@ provisioner: name: ansible ansible_args: - --diff - # - -vv + - -v config_options: defaults: - deprecation_warnings: True + deprecation_warnings: true stdout_callback: yaml callbacks_enabled: profile_tasks + gathering: smart + fact_caching: jsonfile + fact_caching_timeout: 8640 + fact_caching_connection: ansible_facts scenario: test_sequence: diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index db7b40d..d499c9e 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -1,22 +1,36 @@ --- -- name: prepare container + +- name: information hosts: all gather_facts: true pre_tasks: + - name: arch- / artixlinux + when: + - ansible_distribution | lower == 'archlinux' or + ansible_os_family | lower == 'artix linux' + block: + - name: update pacman system + ansible.builtin.command: | + pacman --refresh --sync --sysupgrade --noconfirm + + - name: create depends service + ansible.builtin.copy: + mode: 0755 + dest: /etc/init.d/net + content: | + #!/usr/bin/openrc-run + true + when: + - ansible_os_family | lower == 'artix linux' - name: update package cache - package: + become: true + ansible.builtin.package: update_cache: true - - name: install netstat - package: - name: [ net-tools, iproute2 ] - state: present - when: - - ansible_os_family | lower in [ 'archlinux', 'debian' ] - - - debug: + - name: environment + ansible.builtin.debug: msg: - "os family : {{ ansible_distribution }} ({{ ansible_os_family }})" - "distribution version : {{ ansible_distribution_major_version }}" diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml index d13519f..1dcc6d9 100644 --- a/molecule/default/requirements.yml +++ b/molecule/default/requirements.yml @@ -2,10 +2,10 @@ - name: prometheus src: bodsch.prometheus - version: 1.1.3 + version: 1.5.0 - name: redis src: bodsch.redis - version: 1.2.0 + version: 1.4.0 ... diff --git a/tasks/install.yml b/tasks/install.yml index 28e3c9c..cad946a 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -21,11 +21,11 @@ notify: - reload trickster -- name: create local facts - ansible.builtin.template: - src: ansible_facts.j2 - dest: /etc/ansible/facts.d/trickster.fact - mode: 0755 +- name: create custom fact file + bodsch.core.facts: + name: trickster + facts: + version: "{{ trickster_version }}" - name: create link to binary ansible.builtin.file: diff --git a/tasks/prepare.yml b/tasks/prepare.yml index 2b65b1d..1240958 100644 --- a/tasks/prepare.yml +++ b/tasks/prepare.yml @@ -18,10 +18,6 @@ - default.yaml skip: true -- name: update package cache - ansible.builtin.package: - update_cache: true - - name: define delegate instance for download handling ansible.builtin.set_fact: trickster_delegate_to: "{{ ansible_host }}" @@ -33,13 +29,15 @@ - name: install dependency ansible.builtin.package: - name: "{{ trickster_dependencies }}" + name: "{{ trickster_requirements }}" state: present when: - - trickster_dependencies is defined - - trickster_dependencies | count > 0 + - trickster_requirements | default([]) | count > 0 -- name: detect latest release +- name: get latest release + delegate_to: localhost + become: false + run_once: true when: - trickster_version == "latest" block: @@ -47,23 +45,16 @@ delegate_to: localhost become: false run_once: true - ansible.builtin.uri: - url: "https://api.github.com/repos/tricksterproxy/trickster/releases/latest" - method: GET - return_content: true - status_code: 200 - body_format: json - validate_certs: false + bodsch.scm.github_latest: + project: tricksterproxy + repository: trickster user: "{{ lookup('env', 'GH_USER') | default(omit) }}" password: "{{ lookup('env', 'GH_TOKEN') | default(omit) }}" - # no_log: "{{ not lookup('env', 'ANSIBLE_DEBUG') | bool }}" register: _latest_release - until: _latest_release.status == 200 - retries: 5 - name: re-define trickster_version ansible.builtin.set_fact: - trickster_version: "{{ _latest_release.json.tag_name[1:] }}" + trickster_version: "{{ _latest_release.latest_release }}" - name: detect installed trickster binary ansible.builtin.stat: @@ -106,14 +97,6 @@ group: "{{ trickster_system_group }}" mode: 0755 -- name: create ansible local facts directory - ansible.builtin.file: - dest: /etc/ansible/facts.d - state: directory - owner: root - group: root - mode: 0775 - - name: merge trickster configuration between defaults and custom ansible.builtin.set_fact: trickster_service_config: "{{ trickster_defaults_service_config | combine(trickster_service_config, recursive=True) }}" diff --git a/templates/ansible_facts.j2 b/templates/ansible_facts.j2 deleted file mode 100644 index 7338793..0000000 --- a/templates/ansible_facts.j2 +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -# {{ ansible_managed }} -cat < 0 %} index: {% if values.index.reap_interval_ms is defined and @@ -50,7 +50,7 @@ caches: provider: {{ _default_cache_provider }} {% endif %} {% if values.index is defined and - values.index | type == "dict" and + values.index | bodsch.core.type == "dict" and values.index | count > 0 and _default_cache_provider in ["bbolt", "filesystem", "memory"] %} index: @@ -83,7 +83,7 @@ caches: {% endif %} {# REDIS #} {% if values.redis is defined and - values.redis | type == "dict" and + values.redis | bodsch.core.type == "dict" and values.redis | count > 0 and _default_cache_provider == "redis" %} redis: @@ -99,7 +99,7 @@ caches: {% if values.redis.client_type is defined and values.redis.client_type | default('standard') == "cluster" and values.redis.endpoints is defined and - values.redis.endpoints | type == "list" and + values.redis.endpoints | bodsch.core.type == "list" and values.redis.endpoints | count > 0 %} endpoints: {% for e in values.redis.endpoints %} @@ -175,7 +175,7 @@ caches: {% endif %} {# FILESYSTEM #} {% if values.filesystem is defined and - values.filesystem | type == "dict" and + values.filesystem | bodsch.core.type == "dict" and values.filesystem | count > 0 and _default_cache_provider == "filesystem" %} filesystem: @@ -186,7 +186,7 @@ caches: {% endif %} {# BBOLT #} {% if values.bbolt is defined and - values.bbolt | type == "dict" and + values.bbolt | bodsch.core.type == "dict" and values.bbolt | count > 0 and _default_cache_provider == "bbolt" %} bbolt: diff --git a/test-requirements.txt b/test-requirements.txt index 45e18eb..e72be0a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,8 +1,9 @@ ansible-lint docker +dnspython flake8 molecule -molecule-docker +molecule-plugins[docker] netaddr pytest pytest-testinfra diff --git a/vars/archlinux.yml b/vars/archlinux.yml index a53247f..5c7d431 100644 --- a/vars/archlinux.yml +++ b/vars/archlinux.yml @@ -1,4 +1,5 @@ --- -trickster_dependencies: +trickster_requirements: - curl + - iproute diff --git a/vars/debian.yml b/vars/debian.yml index a53247f..c81cf5b 100644 --- a/vars/debian.yml +++ b/vars/debian.yml @@ -1,4 +1,3 @@ --- -trickster_dependencies: - - curl +... diff --git a/vars/main.yml b/vars/main.yml index e2c79af..d7b18ec 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -8,7 +8,9 @@ trickster_local_tmp_directory: "{{ lookup('env', 'CUSTOM_LOCAL_TMP_DIRECTORY') | default(lookup('env', 'HOME') ~ '/.cache/ansible/trickster', true) }}/{{ trickster_version }}" -trickster_dependencies: [] +trickster_requirements: + - curl + - iproute2 trickster_defaults_directory: /etc/default diff --git a/vars/redhat.yml b/vars/redhat.yml index 32e6119..9b8c4f6 100644 --- a/vars/redhat.yml +++ b/vars/redhat.yml @@ -1,5 +1,5 @@ --- -trickster_dependencies: [] +trickster_requirements: [] trickster_defaults_directory: "/etc/sysconfig"