Skip to content

Commit

Permalink
fix install
Browse files Browse the repository at this point in the history
  • Loading branch information
bodsch committed Jul 13, 2024
1 parent 285005e commit c0b8965
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/configured.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
scenario:
- systemd-timer
- crond
- from-git
steps:
- name: check out the codebase.
uses: actions/checkout@v4
Expand Down
10 changes: 10 additions & 0 deletions molecule/from-git/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: converge
hosts: instance
any_errors_fatal: false

environment:
NETRC: ''

roles:
- role: ansible-automysqlbackup
3 changes: 3 additions & 0 deletions molecule/from-git/group_vars/all/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

automysqlbackup_source: git
52 changes: 52 additions & 0 deletions molecule/from-git/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---

role_name_check: 1

dependency:
name: galaxy

driver:
name: docker

platforms:
- name: instance
image: "bodsch/ansible-${DISTRIBUTION:-debian:12}"
command: ${MOLECULE_DOCKER_COMMAND:-""}
docker_host: "${DOCKER_HOST:-unix://run/docker.sock}"
privileged: true
pre_build_image: true
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
- /var/lib/containerd
tmpfs:
- /run
- /tmp

provisioner:
name: ansible
ansible_args:
- --diff
- -v
config_options:
defaults:
deprecation_warnings: true
stdout_callback: yaml
gathering: smart
fact_caching: jsonfile
fact_caching_timeout: 8640
fact_caching_connection: ansible_facts

scenario:
test_sequence:
- destroy
- dependency
- syntax
- create
- prepare
- converge
- verify
- destroy

verifier:
name: testinfra
49 changes: 49 additions & 0 deletions molecule/from-git/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---

- 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
register: pacman
changed_when: pacman.rc != 0
failed_when: pacman.rc != 0

- 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: make sure python3-apt is installed (only debian based)
ansible.builtin.package:
name:
- python3-apt
state: present
when:
- ansible_os_family | lower == 'debian'

- name: update package cache
become: true
ansible.builtin.package:
update_cache: true

- name: environment
ansible.builtin.debug:
msg:
- "os family : {{ ansible_distribution }} ({{ ansible_os_family }})"
- "distribution version : {{ ansible_distribution_major_version }}"
- "ansible version : {{ ansible_version.full }}"
- "python version : {{ ansible_python.version.major }}.{{ ansible_python.version.minor }}"
126 changes: 126 additions & 0 deletions molecule/from-git/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# coding: utf-8
from __future__ import unicode_literals

from ansible.parsing.dataloader import DataLoader
from ansible.template import Templar

import json
import pytest
import os

import testinfra.utils.ansible_runner

HOST = 'instance'

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(HOST)


def pp_json(json_thing, sort=True, indents=2):
if type(json_thing) is str:
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))
else:
print(json.dumps(json_thing, sort_keys=sort, indent=indents))
return None


def base_directory():
"""
"""
cwd = os.getcwd()

if 'group_vars' in os.listdir(cwd):
directory = "../.."
molecule_directory = "."
else:
directory = "."
molecule_directory = f"molecule/{os.environ.get('MOLECULE_SCENARIO_NAME')}"

return directory, molecule_directory


def read_ansible_yaml(file_name, role_name):
"""
"""
read_file = None

for e in ["yml", "yaml"]:
test_file = f"{file_name}.{e}"
if os.path.isfile(test_file):
read_file = test_file
break

return f"file={read_file} name={role_name}"


@pytest.fixture()
def get_vars(host):
"""
parse ansible variables
- defaults/main.yml
- vars/main.yml
- vars/${DISTRIBUTION}.yaml
- molecule/${MOLECULE_SCENARIO_NAME}/group_vars/all/vars.yml
"""
base_dir, molecule_dir = base_directory()
distribution = host.system_info.distribution
operation_system = None

if distribution in ['debian', 'ubuntu']:
operation_system = "debian"
elif distribution in ['redhat', 'ol', 'centos', 'rocky', 'almalinux']:
operation_system = "redhat"
elif distribution in ['arch', 'artix']:
operation_system = f"{distribution}linux"

# print(" -> {} / {}".format(distribution, os))
# print(" -> {}".format(base_dir))

file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
# file_host_molecule = read_ansible_yaml("{}/host_vars/{}/vars".format(base_dir, HOST), "host_vars")

defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
# host_vars = host.ansible("include_vars", file_host_molecule).get("ansible_facts").get("host_vars")

ansible_vars = defaults_vars
ansible_vars.update(vars_vars)
ansible_vars.update(distibution_vars)
ansible_vars.update(molecule_vars)
# ansible_vars.update(host_vars)

templar = Templar(loader=DataLoader(), variables=ansible_vars)
result = templar.template(ansible_vars, fail_on_undefined=False)

return result


def local_facts(host):
"""
return local facts
"""
return host.ansible("setup").get("ansible_facts").get("ansible_local").get("automysqlbackup")


@pytest.mark.parametrize("dirs", [
"/etc/automysqlbackup"
])
def test_directories(host, dirs):
d = host.file(dirs)
assert d.is_directory
assert d.exists


@pytest.mark.parametrize("files", [
"/etc/automysqlbackup/automysqlbackup.conf",
"/usr/local/bin/automysqlbackup",
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file
21 changes: 20 additions & 1 deletion tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,28 @@
- "{{ automysqlbackup_client_package }}"
state: present

- name: find automysqlbackup binary
ansible.builtin.find:
paths: /tmp/automysqlbackup
file_type: file
patterns:
- "automysqlbackup"
recurse: true
register: found_binary

- name: define automysqlbackup_source
ansible.builtin.set_fact:
automysqlbackup_source: "{{
found_binary.files |
sort(attribute='path', reverse=True) |
map(attribute='path') | list | first }}"
when:
- found_binary.files is defined
- found_binary.files | count > 0

- name: copy automysqlbackup
ansible.builtin.copy:
src: "{{ automysqlbackup_download_directory }}/automysqlbackup"
src: "{{ automysqlbackup_source }}"
dest: /usr/local/bin/
mode: "0755"
backup: true
Expand Down

0 comments on commit c0b8965

Please sign in to comment.