[5b1449d] | 1 | #!/usr/bin/env python3 |
---|
| 2 | |
---|
| 3 | import os |
---|
| 4 | import subprocess |
---|
| 5 | import sys |
---|
| 6 | |
---|
| 7 | """ |
---|
| 8 | @file poweroff.py |
---|
| 9 | @brief Script de inicio para cargar el proceso comprobación de clientes inactivos. |
---|
| 10 | @note Arranca y configura el proceso "cron". |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | def main(): |
---|
| 14 | opengnsys = os.getenv('OPENGNSYS') |
---|
| 15 | if opengnsys: |
---|
| 16 | msg_poweroffconf = os.getenv('MSG_POWEROFFCONF', '.') |
---|
| 17 | print(msg_poweroffconf) |
---|
| 18 | |
---|
| 19 | ogntp = os.getenv('ogntp') |
---|
| 20 | status = os.getenv('status') |
---|
| 21 | |
---|
| 22 | # Sincronización horaria con servidor NTP. |
---|
| 23 | if ogntp and status != "offline": |
---|
| 24 | subprocess.run(['ntpdate', ogntp]) |
---|
| 25 | |
---|
| 26 | # Crear fichero de configuración por defecto (30 min. de espera). |
---|
| 27 | poweroff_conf = '/etc/poweroff.conf' |
---|
| 28 | with open(poweroff_conf, 'w') as f: |
---|
| 29 | f.write("POWEROFFSLEEP=30\nPOWEROFFTIME=\n") |
---|
| 30 | |
---|
| 31 | # Incluir zona horaria en el fichero de configuración. |
---|
| 32 | with open('/proc/cmdline') as f: |
---|
| 33 | cmdline = f.read() |
---|
| 34 | tz = ' '.join([x for x in cmdline.split() if x.startswith('TZ=')]) |
---|
| 35 | with open(poweroff_conf, 'a') as f: |
---|
| 36 | f.write(tz + '\n') |
---|
| 37 | |
---|
| 38 | # Lanzar el proceso "cron". |
---|
| 39 | subprocess.run(['cron', '-l']) |
---|
| 40 | |
---|
| 41 | # Definir la "crontab" lanzando el proceso de comprobación cada minuto. |
---|
| 42 | ogbin = os.getenv('OGBIN') |
---|
| 43 | crontab_line = f"* * * * * [ -x {ogbin}/poweroffconf ] && {ogbin}/poweroffconf\n" |
---|
| 44 | subprocess.run(['crontab', '-'], input=crontab_line, text=True) |
---|
| 45 | |
---|
| 46 | else: |
---|
| 47 | # FIXME Error: entorno de OpenGnsys no configurado. |
---|
| 48 | print("Error: OpenGnsys environment is not configured.") # FIXME: definir mensaje. |
---|
| 49 | sys.exit(1) |
---|
| 50 | |
---|
| 51 | if __name__ == "__main__": |
---|
| 52 | main() |
---|