From aa512cd20173127ec17e3f7d58a295445cc81353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C3=A9lie=20R?= Date: Fri, 10 Feb 2023 18:05:55 +0100 Subject: [PATCH] feat: add var promtail_binary_local_dir (#104) Co-authored-by: Patrick Jahns --- .github/workflows/test.yml | 2 + README.md | 1 + defaults/main.yml | 2 + .../molecule.yml | 32 +++++++++++ .../playbook.yml | 9 ++++ .../prepare.yml | 26 +++++++++ .../tests/conftest.py | 23 ++++++++ ...st_scenario_with_local_binary_directory.py | 54 +++++++++++++++++++ tasks/install.yml | 15 +++++- tasks/preflight.yml | 29 ++++++---- 10 files changed, 181 insertions(+), 12 deletions(-) create mode 100644 molecule/scenario_with_local_binary_directory/molecule.yml create mode 100644 molecule/scenario_with_local_binary_directory/playbook.yml create mode 100644 molecule/scenario_with_local_binary_directory/prepare.yml create mode 100644 molecule/scenario_with_local_binary_directory/tests/conftest.py create mode 100644 molecule/scenario_with_local_binary_directory/tests/test_scenario_with_local_binary_directory.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bf85ce3..2cd24b7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,6 +36,8 @@ jobs: MOLECULE_SCENARIO: latest - ANSIBLE: "6.6" MOLECULE_SCENARIO: upgrade + - ANSIBLE: "6.6" + MOLECULE_SCENARIO: scenario_with_local_binary_directory - ANSIBLE: "6.6" MOLECULE_SCENARIO: default MOLECULE_DISTRO: ubuntu2004 diff --git a/README.md b/README.md index 0d6de32..d1487a1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults |--------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------| | `promtail_version` | "2.7.3" | promtail package version. Also accepts *latest* as parameter. | | `promtail_custom_checksum` | "" | Custom checksum for custom build promtail binaries | +| `promtail_binary_local_dir` | "" | Allows to use local packages instead of ones distributed on github. As parameter it takes the path where zip archive of promtail is stored on host on which ansible is ran. | | `promtail_config_dir` | /etc/promtail | Directory for storing promtail configuration file | | `promtail_config_expand_env` | "false" | value of promtail [-config.expand-env](https://grafana.com/docs/loki/latest/clients/promtail/configuration/#use-environment-variables-in-the-configuration) option | | `promtail_config_file_sd_dir` | "{{ promtail_config_dir }}/file_sd" | Default directory for `file_sd` discovery | diff --git a/defaults/main.yml b/defaults/main.yml index eda440e..d642c78 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -14,6 +14,8 @@ promtail_user_additional_groups: "adm" promtail_systemd_service_template_file: service.j2 promtail_systemd_service: promtail +promtail_binary_local_dir: "" + promtail_install_dir: /opt/promtail promtail_tmp_dir: /tmp diff --git a/molecule/scenario_with_local_binary_directory/molecule.yml b/molecule/scenario_with_local_binary_directory/molecule.yml new file mode 100644 index 0000000..4c3e651 --- /dev/null +++ b/molecule/scenario_with_local_binary_directory/molecule.yml @@ -0,0 +1,32 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: | + set -e + yamllint . + flake8 + +platforms: + - name: instance + image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + cgroupns_mode: host + pre_build_image: true + +provisioner: + name: ansible + playbooks: + converge: playbook.yml + inventory: + group_vars: + python3: + ansible_python_interpreter: /usr/bin/python3 +scenario: + name: scenario_with_local_binary_directory +verifier: + name: testinfra diff --git a/molecule/scenario_with_local_binary_directory/playbook.yml b/molecule/scenario_with_local_binary_directory/playbook.yml new file mode 100644 index 0000000..4892294 --- /dev/null +++ b/molecule/scenario_with_local_binary_directory/playbook.yml @@ -0,0 +1,9 @@ +--- +- hosts: all + any_errors_fatal: true + #become: true + roles: + - role: ansible-role-promtail + vars: + promtail_version: "2.7.3" + promtail_binary_local_dir: "/tmp" diff --git a/molecule/scenario_with_local_binary_directory/prepare.yml b/molecule/scenario_with_local_binary_directory/prepare.yml new file mode 100644 index 0000000..0249d35 --- /dev/null +++ b/molecule/scenario_with_local_binary_directory/prepare.yml @@ -0,0 +1,26 @@ +--- +- name: Prepare + hosts: all + gather_facts: true + vars: + promtail_version: "2.7.3" + promtail_binary_local_dir: "/tmp" + go_arch_map: + x86_64: 'amd64' + aarch64: 'arm64' + armv7l: 'arm' + armv6l: 'arm' + go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}" + + tasks: + - name: Ensure that net-tools is available + package: + name: net-tools + state: present + when: ansible_os_family == 'RedHat' + - name: Download zip archive on controler + get_url: + url: "https://github.com/grafana/loki/releases/download/v{{ promtail_version }}/promtail-linux-{{ go_arch }}.zip" + dest: "/tmp/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip" + force: True + delegate_to: localhost diff --git a/molecule/scenario_with_local_binary_directory/tests/conftest.py b/molecule/scenario_with_local_binary_directory/tests/conftest.py new file mode 100644 index 0000000..501654e --- /dev/null +++ b/molecule/scenario_with_local_binary_directory/tests/conftest.py @@ -0,0 +1,23 @@ +"""PyTest Fixtures.""" +from __future__ import absolute_import + +import os + +import pytest + + +def pytest_runtest_setup(item): + """Run tests only when under molecule with testinfra installed.""" + try: + import testinfra + except ImportError: + pytest.skip("Test requires testinfra", allow_module_level=True) + if "MOLECULE_INVENTORY_FILE" in os.environ: + pytest.testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ["MOLECULE_INVENTORY_FILE"] + ).get_hosts("all") + else: + pytest.skip( + "Test should run only from inside molecule.", + allow_module_level=True + ) diff --git a/molecule/scenario_with_local_binary_directory/tests/test_scenario_with_local_binary_directory.py b/molecule/scenario_with_local_binary_directory/tests/test_scenario_with_local_binary_directory.py new file mode 100644 index 0000000..68e459f --- /dev/null +++ b/molecule/scenario_with_local_binary_directory/tests/test_scenario_with_local_binary_directory.py @@ -0,0 +1,54 @@ +import os +import pytest +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +@pytest.mark.parametrize("dir", [ + "/opt/promtail", + "/etc/promtail", + "/etc/promtail/file_sd", + "/var/lib/promtail", +]) +def test_directories(host, dir): + d = host.file(dir) + assert d.is_directory + assert d.exists + + +@pytest.mark.parametrize("files", [ + "/etc/systemd/system/promtail.service", + "/usr/local/bin/promtail", + "/etc/promtail/promtail.yml" +]) +def test_files(host, files): + f = host.file(files) + assert f.exists + assert f.is_file + + +def test_user(host): + assert host.group("promtail").exists + assert host.user("promtail").exists + + +def test_service(host): + s = host.service("promtail") + assert s.is_running + + +def test_http_socket(host): + s = host.socket("tcp://0.0.0.0:9080") + assert s.is_listening + + +def test_grpc_socket(host): + s = host.socket("tcp://0.0.0.0:9095") + assert s.is_listening + + +def test_version(host): + result = host.run("/usr/local/bin/promtail --version") + assert result.rc == 0 diff --git a/tasks/install.yml b/tasks/install.yml index f95033c..23a1192 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -63,7 +63,20 @@ dest: "{{ promtail_tmp_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip" force: True checksum: "{{ promtail_custom_checksum if promtail_custom_checksum else 'sha256:' + __promtail_checksum }}" - when: not promtail_binary.stat.exists + when: + - not promtail_binary.stat.exists + - promtail_binary_local_dir | length == 0 + +- name: Propagate locally distributed promtail archive + copy: + src: "{{ promtail_binary_local_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip" + dest: "{{ promtail_tmp_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip" + mode: 0755 + owner: root + group: root + when: + - not promtail_binary.stat.exists + - promtail_binary_local_dir | length > 0 - name: Unpack promtail binaries ignore_errors: "{{ ansible_check_mode }}" diff --git a/tasks/preflight.yml b/tasks/preflight.yml index d65cdd1..eb065f7 100644 --- a/tasks/preflight.yml +++ b/tasks/preflight.yml @@ -25,19 +25,26 @@ promtail_version: "{{ _latest_release.json.tag_name[1:] }}" when: - promtail_version == "latest" + +- name: Test if zip local archive exists + stat: + path: "{{ promtail_binary_local_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip" + register: promtail_binary_local_archive delegate_to: localhost run_once: True +- block: + - name: "Get checksum list" + set_fact: + __promtail_checksums: "{{ lookup('url', 'https://github.com/grafana/loki/releases/download/v' + promtail_version + '/SHA256SUMS', wantlist=True) | list }}" + run_once: True -- name: "Get checksum list" - set_fact: - __promtail_checksums: "{{ lookup('url', 'https://github.com/grafana/loki/releases/download/v' + promtail_version + '/SHA256SUMS', wantlist=True) | list }}" - run_once: True - delegate_to: localhost - -- name: "Get checksum for {{ go_arch }} architecture" - set_fact: - __promtail_checksum: "{{ item.split(' ')[0] }}" - with_items: "{{ __promtail_checksums }}" + - name: "Get checksum for {{ go_arch }} architecture" + set_fact: + __promtail_checksum: "{{ item.split(' ')[0] }}" + with_items: "{{ __promtail_checksums }}" + when: + - "('promtail-linux-' + go_arch + '.zip') in item" when: - - "('promtail-linux-' + go_arch + '.zip') in item" + - promtail_binary_local_dir | length == 0 + - not promtail_binary_local_archive.stat.exists