Another big round of refactoring and cleaning...

This commit is contained in:
2025-07-11 17:55:26 +02:00
parent aa61bf2a44
commit 168c5c0da6
323 changed files with 761 additions and 811 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 == "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-rpr-docker-hard.cymais.service"
systemd:
name: sys-rpr-docker-hard.cymais.service
enabled: yes
daemon_reload: yes

View File

@@ -0,0 +1,29 @@
---
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: "CyMaIS NonCommercial License (CNCL)"
license_url: "https://s.veen.world/cncl"
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.veen.world/cymais"
issue_tracker_url: "https://s.veen.world/cymaisissues"
documentation: "https://s.veen.world/cymais"
dependencies:
- sys-lock

View File

@@ -0,0 +1,33 @@
- block:
- name: "create {{restart_docker_folder}}"
file:
path: "{{restart_docker_folder}}"
state: directory
mode: 0755
- name: create {{restart_docker_script}}
copy:
src: sys-rpr-docker-hard.py
dest: "{{restart_docker_script}}"
- name: configure sys-rpr-docker-hard.cymais.service
template:
src: sys-rpr-docker-hard.service.j2
dest: /etc/systemd/system/sys-rpr-docker-hard.cymais.service
notify: "reload sys-rpr-docker-hard.cymais.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: "{{on_calendar_restart_docker}}"
- name: run the restart_docker_volumes tasks once
set_fact:
run_once_restart_docker: true
when: run_once_restart_docker is not defined

View File

@@ -0,0 +1,8 @@
[Unit]
Description=Restart Docker Instances
OnFailure=sys-alm-compose.cymais@%n.service
[Service]
Type=oneshot
ExecStartPre=/bin/sh -c '/usr/bin/python {{ path_system_lock_script }} {{ system_maintenance_services | join(' ') }} --ignore {{system_maintenance_cleanup_services | join(' ') }} sys-rpr-docker-hard --timeout "{{system_maintenance_lock_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-rpr-docker-hard/'
restart_docker_script: '{{restart_docker_folder}}sys-rpr-docker-hard.py'