78 lines
1.4 KiB
Python
78 lines
1.4 KiB
Python
import logging
|
|
import os
|
|
import subprocess
|
|
|
|
#ex de json accepté:
|
|
#{
|
|
# "action": "modifier_services",
|
|
# "creer": [
|
|
# "cloud"
|
|
# ],
|
|
# "enlever": [
|
|
# "paheko",
|
|
# "wp"
|
|
# ],
|
|
# "nom_court_orga": "monorga11",
|
|
# "emails": [
|
|
# "admin@kaz.bzh",
|
|
# "contact@kaz.bzh"
|
|
# ]
|
|
# }
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def run(message, encours):
|
|
|
|
nom_court_orga = message["nom_court_orga"]
|
|
|
|
services_creer = message.get("creer", [])
|
|
services_enlever = message.get("enlever", [])
|
|
|
|
script = "/kaz/config/orgaTmpl/orga-gen.sh"
|
|
|
|
mail_file = encours.removesuffix(".encours") + ".mail"
|
|
|
|
command = [script]
|
|
|
|
command.extend(
|
|
f"+{service}"
|
|
for service in services_creer
|
|
)
|
|
|
|
command.extend(
|
|
f"-{service}"
|
|
for service in services_enlever
|
|
)
|
|
|
|
command.append(nom_court_orga)
|
|
|
|
log.info(
|
|
f"Exécution : {' '.join(command)}"
|
|
)
|
|
|
|
env = os.environ.copy()
|
|
env["ORGA_GEN_MAIL_FILE"] = mail_file
|
|
|
|
result = subprocess.run(
|
|
command,
|
|
capture_output=True,
|
|
text=True,
|
|
env=env,
|
|
)
|
|
|
|
log.info(
|
|
f"Résultat du script :\n{result.stdout}"
|
|
)
|
|
|
|
if result.stderr:
|
|
log.warning(
|
|
f"Sortie stderr du script :\n{result.stderr}"
|
|
)
|
|
|
|
if result.returncode != 0:
|
|
raise RuntimeError(
|
|
f"Le script a échoué avec le code "
|
|
f"{result.returncode}"
|
|
)
|