Skip to content

Commit

Permalink
feat: add var promtail_binary_local_dir (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Jahns <[email protected]>
  • Loading branch information
OuiSouss and patrickjahns authored Feb 10, 2023
1 parent 787b979 commit aa512cd
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions molecule/scenario_with_local_binary_directory/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions molecule/scenario_with_local_binary_directory/playbook.yml
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 26 additions & 0 deletions molecule/scenario_with_local_binary_directory/prepare.yml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions molecule/scenario_with_local_binary_directory/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
15 changes: 14 additions & 1 deletion tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
29 changes: 18 additions & 11 deletions tasks/preflight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aa512cd

Please sign in to comment.