diff --git a/compose.yml b/compose.yml index e653746..0639609 100644 --- a/compose.yml +++ b/compose.yml @@ -1,8 +1,28 @@ services: - airwatchConnector: + sync: image: emitlinks/airwatchConnector restart: unless-stopped volumes: - ./conf:/airwatchConnector/conf - ./certs:/airwatchConnector/certs - - ./logs:/airwatchConnector/logs \ No newline at end of file + - ./logs:/airwatchConnector/logs + healthcheck: + test: if [ -f $(ls /airwatchConnector/*_SyncGLPI.lock) ]; then exit 0; else exit 1; fi + interval: 5m + start_period: 3h + environment: + TASK: "syncGLPI" + + stagingAssignment: + image: emitlinks/airwatchConnector + restart: unless-stopped + volumes: + - ./conf:/airwatchConnector/conf + - ./certs:/airwatchConnector/certs + - ./logs:/airwatchConnector/logs + healthcheck: + test: if [ -f $(ls /airwatchConnector/*_StagingUserAssignation.lock) ]; then exit 0; else exit 1; fi + interval: 15s + start_period: 5m + environment: + TASK: "stagingAssignment" \ No newline at end of file diff --git a/create_services.sh b/create_services.sh deleted file mode 100644 index 2f1bbe6..0000000 --- a/create_services.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -python3 /services.py - -systemctl daemon-reload - -systemctl enable *-sync.timer *-staging.timer - -systemctl start *-sync.timer *-staging.timer - -tail -f /dev/null \ No newline at end of file diff --git a/dockerfile b/dockerfile index 738ad8e..ec7f7d9 100644 --- a/dockerfile +++ b/dockerfile @@ -1,10 +1,10 @@ -FROM debian +FROM python RUN <<-EOF apt-get update -apt-get install -y systemd python3 python3-cryptography python3-requests python3-toml +apt-get install -y python3-cryptography python3-requests python3-toml EOF ADD scripts\* /airwatchConnector -ADD services.py / -ADD create_services.sh / -RUN chmod u+x /create_services.sh -ENTRYPOINT ["/create_services.sh"] \ No newline at end of file +ADD pre-start.py / +ADD start.sh / +RUN chmod u+x /start.sh +ENTRYPOINT ["/start.sh"] \ No newline at end of file diff --git a/pre-start.py b/pre-start.py new file mode 100644 index 0000000..b4ec0c1 --- /dev/null +++ b/pre-start.py @@ -0,0 +1,26 @@ +import os +import toml + +baseDir = "/airwatchConnector" +logDir = f"{baseDir}/logs" +confDir = f"{baseDir}/conf" +confFiles = os.listdir(confDir) + +# On récupère que les fichiers .conf +confFiles = [conf for conf in confFiles if os.path.isfile(f"{confDir}/{conf}") and not conf.endswith(".conf")] + +for conf in confFiles: + # On forme un nom à partir du nom du fichier de conf sans l'extension + # et on enlève les espaces + confName = conf[:5].replace(' ', '') + + with open(f"{confDir}/{conf}", "r") as f: + settings = toml.load(f) + + # Create log folder + if(settings["LOGS"]["Enabled"]): + logPath = settings["LOGS"].get(Path) + if not os.path.exists(f"{logDir}/{logPath}"): + os.makedirs(f"{logDir}/{logPath}") + + diff --git a/services.py b/services.py deleted file mode 100644 index 038367e..0000000 --- a/services.py +++ /dev/null @@ -1,75 +0,0 @@ -import os -import toml - -baseDir = "/airwatchConnector" -logDir = f"{baseDir}/logs" -confDir = f"{baseDir}/conf" -confFiles = os.listdir(confDir) - -# On récupère que les fichiers .conf -confFiles = [conf for conf in confFiles if os.path.isfile(f"{confDir}/{conf}") and not conf.endswith(".conf")] - -for conf in confFiles: - # On forme un nom à partir du nom du fichier de conf sans l'extension - # et on enlève les espaces - confName = conf[:5].replace(' ', '') - - with open(f"{confDir}/{conf}", "r") as f: - settings = toml.load(f) - - # Create log folder - if(settings["LOGS"]["Enabled"]): - logPath = settings["LOGS"].get(Path) - if not os.path.exists(f"{logDir}/{logPath}"): - os.makedirs(f"{logDir}/{logPath}") - - - with open(f"/etc/systemd/system/{confName}-sync.service", "w") as f: - f.write(f"""[Unit] -Description=Script Airwatch pour la synchronisation des données Airwatch et GLPI. - -[Service] -Type=oneshot -WorkingDirectory=/airwatchConnector -ExecStart=/airwatchConnector/syncGLPI.py -c {confDir}/{conf} -s -f -User=root -""") - - with open(f"/etc/systemd/system/{confName}-sync.timer", "w") as f: - f.write(f"""[Unit] -Description=Script Airwatch pour la synchronisation des données Airwatch et GLPI. - -[Timer] -OnUnitInactiveSec=1h -AccuracySec=1us -Unit={confName}-sync.service - -[Install] -WantedBy=timers.target -""") - - with open(f"/etc/systemd/system/{confName}-staging.service", "w") as f: - f.write(f"""[Unit] -Description=Script qui assigne les appareils en staging aux utilisateurs en se basant sur GLPI. - -[Service] -Type=oneshot -WorkingDirectory=/airwatchConnector -ExecStart=/airwatchConnector/StagingUserAssignation.py -c {confDir}/{conf} -s -f -User=root -""") - - with open(f"/etc/systemd/system/{confName}-staging.timer", "w") as f: - f.write(f"""[Unit] -Description=Script qui assigne les appareils en staging aux utilisateurs en se basant sur GLPI. - -[Timer] -OnUnitInactiveSec=1m -AccuracySec=1us -Unit={confName}-staging.service - -[Install] -WantedBy=timers.target -""") - - diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..7caa3e6 --- /dev/null +++ b/start.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +python /pre-start.py + +if [ $CONF != '' ]; then + case $TASK in + + syncGLPI) + python /airwatchConnector/sync.py -c $CONF -f + ;; + + stagingAssignment) + python /airwatchConnector/stagingUserAssignation.py -c $CONF -f + ;; + + *) + exit 0 + ;; + esac +else + confFiles = $(ls /airwatchConnector/conf/*.conf) + for CONF in $confFiles; do + case $TASK in + + syncGLPI) + python /airwatchConnector/sync.py -c $CONF -for + ;; + + stagingAssignment) + python /airwatchConnector/stagingUserAssignation.py -c $CONF -f + ;; + + *) + exit 0 + ;; + esac + done +fi \ No newline at end of file