Refactor systemctl services and categories due to alarm bugs

This commit restructures systemctl service definitions and category mappings.

Motivation: Alarm-related bugs revealed inconsistencies in service and role handling.

Preparation step: lays the groundwork for fixing the alarm issues by aligning categories, roles, and service templates.
This commit is contained in:
2025-08-18 13:35:43 +02:00
parent 29f50da226
commit 3a839cfe37
289 changed files with 975 additions and 948 deletions

View File

@@ -0,0 +1,30 @@
# Docker Auto Restart
## Description
This role automates the restart process for Docker Compose instances within a specified directory. It deploys a Python script that checks for the presence of docker-compose.yml files and restarts the associated services—using a hard restart for certain directories if needed.
## Overview
Optimized for containerized environments, this role:
- Sets up the necessary directories and scripts for restarting Docker Compose instances.
- Configures a systemd service (and optionally a timer) to execute the restart script.
- Handles both standard restarts and hard restarts for specific containers (e.g., for Mailu).
## Purpose
The primary purpose of this role is to ensure that all Docker Compose services are restarted consistently, resolving issues that may arise from partial restarts. This helps maintain overall service stability and minimizes downtime.
## Features
- **Automated Detection:** Scans a specified parent directory for docker-compose.yml files.
- **Service Restart:** Executes a Python script to restart Docker services via docker-compose.
- **Conditional Hard Restart:** Applies a hard restart procedure for specific directories (e.g., Mailu).
- **Systemd Integration:** Configures a systemd service and optionally a timer for scheduled restarts.
# Context
This role was implemented to address the classic issue: ["Have you tried turning it off and on again?"](https://www.youtube.com/watch?v=rksCTVFtjM4). The problem initially arose with the `fetchmail` container in [Mailu](.roles/docker/mailu), which fails if only some containers, and not the full docker-compose composition, are restarted.
## Credits 📝
This role was developed with the assistance of [ChatGPT](https://openai.com/chatgpt), including insights and optimizations from this [conversation](https://chatgpt.com/share/674c6870-fcc4-800f-a19e-b20621b24317). Special thanks for providing guidance on error handling, Ansible best practices, and Python integration.

View File

@@ -0,0 +1,53 @@
import os
import sys
import subprocess
def restart_docker_services(dir_path):
"""
Restart docker-compose services in the given directory.
"""
try:
print(f"Restarting docker-compose services in: {dir_path}")
subprocess.run(["docker-compose", "restart"], cwd=dir_path, check=True)
print(f"Services restarted successfully in: {dir_path}")
except subprocess.CalledProcessError as e:
print(f"Error restarting services in {dir_path}: {e}")
def hard_restart_docker_services(dir_path):
"""
Perform a hard restart of docker-compose services in the given directory
using docker-compose down and docker-compose up -d.
"""
try:
print(f"Performing hard restart for docker-compose services in: {dir_path}")
subprocess.run(["docker-compose", "down"], cwd=dir_path, check=True)
subprocess.run(["docker-compose", "up", "-d"], cwd=dir_path, check=True)
print(f"Hard restart completed successfully in: {dir_path}")
except subprocess.CalledProcessError as e:
print(f"Error during hard restart in {dir_path}: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please provide the path to the parent directory as a parameter.")
sys.exit(1)
parent_directory = sys.argv[1]
for dir_entry in os.scandir(parent_directory):
if dir_entry.is_dir():
dir_path = dir_entry.path
dir_name = os.path.basename(dir_path)
print(f"Checking directory: {dir_path}")
docker_compose_file = os.path.join(dir_path, "docker-compose.yml")
if os.path.isfile(docker_compose_file):
print(f"Found docker-compose.yml in {dir_path}.")
if dir_name == "web-app-mailu":
print(f"Directory {dir_name} detected. Performing hard restart...")
hard_restart_docker_services(dir_path)
else:
print(f"Restarting services in {dir_path}...")
restart_docker_services(dir_path)
else:
print(f"No docker-compose.yml found in {dir_path}. Skipping.")

View File

@@ -0,0 +1,5 @@
- name: "reload sys-ctl-rpr-docker-hard service"
systemd:
name: sys-ctl-rpr-docker-hard{{ SYS_SERVICE_SUFFIX }}
enabled: yes
daemon_reload: yes

View File

@@ -0,0 +1,26 @@
galaxy_info:
author: "Kevin Veen-Birkenbach"
description: "Automates the restart of Docker Compose instances by detecting docker-compose.yml files and executing a restart script, ensuring consistent service availability."
license: "Infinito.Nexus NonCommercial License"
license_url: "https://s.infinito.nexus/license"
company: |
Kevin Veen-Birkenbach
Consulting & Coaching Solutions
https://www.veen.world
min_ansible_version: "2.9"
platforms:
- name: Archlinux
versions:
- rolling
- name: Ubuntu
versions:
- all
galaxy_tags:
- docker
- restart
- update
- systemd
- automation
repository: "https://s.infinito.nexus/code"
issue_tracker_url: "https://s.infinito.nexus/issues"
documentation: "https://docs.infinito.nexus"

View File

@@ -0,0 +1,31 @@
- name: Include dependency 'sys-lock'
include_role:
name: sys-lock
when: run_once_sys_lock is not defined
- name: "create {{restart_docker_folder}}"
file:
path: "{{restart_docker_folder}}"
state: directory
mode: "0755"
- name: create {{restart_docker_script}}
copy:
src: sys-ctl-rpr-docker-hard.py
dest: "{{restart_docker_script}}"
- name: configure sys-ctl-rpr-docker-hard{{ SYS_SERVICE_SUFFIX }}
template:
src: sys-ctl-rpr-docker-hard.service.j2
dest: /etc/systemd/system/sys-ctl-rpr-docker-hard{{ SYS_SERVICE_SUFFIX }}
notify: "reload sys-ctl-rpr-docker-hard service"
- name: "set 'service_name' to '{{ role_name }}'"
set_fact:
service_name: "{{ role_name }}"
- name: "include role for sys-timer for {{ service_name }}"
include_role:
name: sys-timer
vars:
on_calendar: "{{SYS_SCHEDULE_REPAIR_DOCKER_HARD}}"

View File

@@ -0,0 +1,4 @@
- block:
- include_tasks: 01_core.yml
- include_tasks: utils/run_once.yml
when: run_once_sys_ctl_rpr_docker_hard is not defined

View File

@@ -0,0 +1,8 @@
[Unit]
Description=Restart Docker Instances
OnFailure=sys-ctl-alm-compose.{{ SOFTWARE_NAME }}@%n.service
[Service]
Type=oneshot
ExecStartPre=/bin/sh -c '/usr/bin/python {{ PATH_SYSTEM_LOCK_SCRIPT }} {{ SYS_SERVICE_GROUP_MANIPULATION | join(' ') }} --ignore {{SYS_SERVICE_GROUP_CLEANUP | join(' ') }} sys-ctl-rpr-docker-hard --timeout "{{SYS_TIMEOUT_RESTART_DOCKER}}"'
ExecStart=/bin/sh -c '/usr/bin/python {{restart_docker_script}} {{ PATH_DOCKER_COMPOSE_INSTANCES }}'

View File

@@ -0,0 +1,3 @@
restart_docker_folder: '{{ PATH_ADMINISTRATOR_SCRIPTS }}sys-ctl-rpr-docker-hard/'
restart_docker_script: '{{restart_docker_folder}}sys-ctl-rpr-docker-hard.py'