Skip to content

Commit

Permalink
feat: download binaries to ansible controller and copy to nodes (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: Ram Rohit Gannavarapu <[email protected]>
  • Loading branch information
gannaramu and Ram Rohit Gannavarapu authored Apr 5, 2023
1 parent 249a26f commit 925de87
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
MOLECULE_SCENARIO: upgrade
- ANSIBLE: "6.6"
MOLECULE_SCENARIO: scenario_with_local_binary_directory
- ANSIBLE: "6.6"
MOLECULE_SCENARIO: download_and_propagate
- ANSIBLE: "6.6"
MOLECULE_SCENARIO: default
MOLECULE_DISTRO: ubuntu2004
Expand Down
1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ promtail_systemd_service_template_file: service.j2
promtail_systemd_service: promtail

promtail_binary_local_dir: ""
promtail_binary_propagate: False

promtail_install_dir: /opt/promtail
promtail_tmp_dir: /tmp
Expand Down
32 changes: 32 additions & 0 deletions molecule/download_and_propagate/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: download_and_propagate
verifier:
name: testinfra
8 changes: 8 additions & 0 deletions molecule/download_and_propagate/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- hosts: all
any_errors_fatal: true
#become: true
roles:
- role: ansible-role-promtail
vars:
promtail_binary_propagate: True
10 changes: 10 additions & 0 deletions molecule/download_and_propagate/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Prepare
hosts: all
gather_facts: true
tasks:
- name: Ensure that net-tools is available
package:
name: net-tools
state: present
when: ansible_os_family == 'RedHat'
23 changes: 23 additions & 0 deletions molecule/download_and_propagate/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
19 changes: 19 additions & 0 deletions tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
when:
- not promtail_binary.stat.exists
- promtail_binary_local_dir | length == 0
- not promtail_binary_propagate

- name: Propagate locally distributed promtail archive
copy:
Expand All @@ -77,6 +78,24 @@
when:
- not promtail_binary.stat.exists
- promtail_binary_local_dir | length > 0
- not promtail_binary_propagate

- name: Download promtail binaries to controller node
get_url:
url: "{{ promtail_dist_url }}"
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 }}"
delegate_to: localhost
run_once: True
when: promtail_binary_propagate

- name: Propagate promtail binaries
copy:
src: "{{ promtail_tmp_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip"
dest: "{{ promtail_tmp_dir }}/{{ promtail_version }}_promtail-linux-{{ go_arch }}.zip"
mode: 0755
when: promtail_binary_propagate

- name: Unpack promtail binaries
ignore_errors: "{{ ansible_check_mode }}"
Expand Down

0 comments on commit 925de87

Please sign in to comment.