ogclone-engine/client/etc/preinit/poweroff.py

52 lines
1.7 KiB
Python

#!/usr/bin/env python3
import os
import subprocess
import sys
"""
@file poweroff.py
@brief Script de inicio para cargar el proceso comprobación de clientes inactivos.
@note Arranca y configura el proceso "cron".
"""
def main():
opengnsys = os.getenv('OPENGNSYS')
if opengnsys:
msg_poweroffconf = os.getenv('MSG_POWEROFFCONF', '.')
print(msg_poweroffconf)
ogntp = os.getenv('ogntp')
status = os.getenv('status')
# Sincronización horaria con servidor NTP.
if ogntp and status != "offline":
subprocess.run(['ntpdate', ogntp])
# Crear fichero de configuración por defecto (30 min. de espera).
poweroff_conf = '/etc/poweroff.conf'
with open(poweroff_conf, 'w') as f:
f.write("POWEROFFSLEEP=30\nPOWEROFFTIME=\n")
# Incluir zona horaria en el fichero de configuración.
with open('/proc/cmdline') as f:
cmdline = f.read()
tz = ' '.join([x for x in cmdline.split() if x.startswith('TZ=')])
with open(poweroff_conf, 'a') as f:
f.write(tz + '\n')
# Lanzar el proceso "cron".
subprocess.run(['cron', '-l'])
# Definir la "crontab" lanzando el proceso de comprobación cada minuto.
ogbin = os.getenv('OGBIN')
crontab_line = f"* * * * * [ -x {ogbin}/poweroffconf ] && {ogbin}/poweroffconf\n"
subprocess.run(['crontab', '-'], input=crontab_line, text=True)
else:
# FIXME Error: entorno de OpenGnsys no configurado.
print("Error: OpenGnsys environment is not configured.") # FIXME: definir mensaje.
sys.exit(1)
if __name__ == "__main__":
main()