Python : Script pour modifier automatiquement une valeur dans un fichier de service SystemD

Ce script Python simple modifie automatiquement TimeoutStartSec dans n’importe quel fichier de service SystemD.

Appelez-le comme ceci :

ensure_timeout_start_usage.sh
python ensure_timeout_start.py /etc/systemd/system/mynodejs.service

Code source complet

ensure_timeout_start.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ensure_timeout_start.py

Ce travail est dédié au domaine public selon la déclaration de domaine public Creative Commons « CC0 1.0 Universal » (CC0 1.0).

Auteur : Uli Köhler

SPDX-License-Identifier: CC0-1.0

S'assurer que les fichiers d'unité systemd ont TimeoutStartSec=600 dans la section [Service].
- Si [Service] est absent, il sera ajouté.
- Si TimeoutStartSec existe (quelle que soit sa valeur), il sera défini à 600.
- Si TimeoutStartSec est absent, il sera ajouté à [Service].

Utilisation :
  python ensure_timeout_start.py my1.service my2.service
  python ensure_timeout_start.py /etc/systemd/system/*.service --dry-run
"""

import argparse
import os
import shutil
import sys
from configupdater import ConfigUpdater


def ensure_timeout_start(filepath: str, value: str = "600", dry_run: bool = False, backup_ext: str = ".bak") -> bool:
    """
    Renvoie True si une modification était nécessaire (et effectuée ou qui le serait en dry-run), False si déjà conforme.
    """
    updater = ConfigUpdater()
    try:
        updater.read(filepath, encoding="utf-8")
    except FileNotFoundError:
        print(f"ERREUR : Fichier introuvable : {filepath}", file=sys.stderr)
        return False

    changed = False

    # S'assurer que la section [Service] existe
    if not updater.has_section("Service"):
        updater.add_section("Service")
        changed = True

    service = updater["Service"]

    if service.has_option("TimeoutStartSec"):
        current_val = service.get("TimeoutStartSec").value.strip()
        if current_val != value:
            service["TimeoutStartSec"].value = value
            changed = True
    else:
        service["TimeoutStartSec"] = value
        changed = True

    if dry_run:
        return changed

    if changed:
        # Créer une sauvegarde avant d'écraser
        if backup_ext:
            backup_path = filepath + backup_ext
            shutil.copy2(filepath, backup_path)

        tmp_path = filepath + ".tmp"
        with open(tmp_path, "w", encoding="utf-8", newline="") as f:
            updater.write(f)

        try:
            st = os.stat(filepath)
            os.chmod(tmp_path, st.st_mode)
            try:
                os.chown(tmp_path, st.st_uid, st.st_gid)  # peut nécessiter root
            except PermissionError:
                pass
        except FileNotFoundError:
            pass

        os.replace(tmp_path, filepath)

    return changed


def main():
    parser = argparse.ArgumentParser(
        description="S'assurer que TimeoutStartSec=600 est présent dans la section [Service] d'un ou plusieurs fichiers d'unité systemd."
    )
    parser.add_argument("unit_files", nargs="+", help="Chemin(s) vers le(s) fichier(s) d'unité")
    parser.add_argument("--dry-run", action="store_true", help="Indiquer si une modification serait effectuée sans écrire")
    parser.add_argument("--no-backup", action="store_true", help="Ne pas créer de fichiers de sauvegarde")
    parser.add_argument("--value", default="600", help="Valeur à définir pour TimeoutStartSec (par défaut : 600)")

    args = parser.parse_args()
    backup_ext = "" if args.no_backup else ".bak"

    exit_code = 0
    for path in args.unit_files:
        changed = ensure_timeout_start(path, value=args.value, dry_run=args.dry_run, backup_ext=backup_ext)
        if args.dry_run:
            print(f"{path}: {'MODIFICATION NÉCESSAIRE' if changed else 'AUCUNE MODIFICATION NÉCESSAIRE'}")
        else:
            print(f"{path}: {'MIS À JOUR' if changed else 'DÉJÀ CONFORME'}")
        if not os.path.exists(path):
            exit_code = 2

    sys.exit(exit_code)


if __name__ == "__main__":
    main()

Check out similar posts by category: Linux, Python