ajout de l'envoie de l'ordre de fermeture de l'appli intercom en cas de reboot ou poweroff

This commit is contained in:
Vincent BENOIT
2023-01-03 17:04:36 +01:00
parent 4202677498
commit 4812ef51ed
+27
View File
@@ -9,6 +9,7 @@
import sys, re, os import sys, re, os
import logging as log import logging as log
from datetime import datetime, timezone from datetime import datetime, timezone
import time
from flask import Flask, Blueprint, request, abort, jsonify, current_app from flask import Flask, Blueprint, request, abort, jsonify, current_app
from flask_api import status from flask_api import status
@@ -25,6 +26,8 @@ import hashlib
import subprocess import subprocess
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
from ConfBack.manager import Sock
######################################################### #########################################################
# Class et Methods # # Class et Methods #
@@ -166,6 +169,18 @@ def reboot():
if os.path.exists(userpath): if os.path.exists(userpath):
shutil.rmtree(userpath) shutil.rmtree(userpath)
conn = Sock(addr=current_app.config['UNIX_ADDR'], logger=current_app.logger)
if not conn.connect():
current_app.logger.error("impossible de se connecter au serveur")
else:
# send order to KineIntercom process
if not conn.send(b"SHUTDOWN\n"):
conn.disconnect()
current_app.logger.error("impossible de communiquer avec le serveur")
conn.disconnect()
time.sleep(3)
reboot_cmd = ['sudo', '/bin/systemctl', 'reboot'] reboot_cmd = ['sudo', '/bin/systemctl', 'reboot']
if not execute_cmd(reboot_cmd): if not execute_cmd(reboot_cmd):
abort(status.HTTP_500_INTERNAL_SERVER_ERROR, abort(status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -186,6 +201,18 @@ def shutdown():
if os.path.exists(userpath): if os.path.exists(userpath):
shutil.rmtree(userpath) shutil.rmtree(userpath)
conn = Sock(addr=current_app.config['UNIX_ADDR'], logger=current_app.logger)
if not conn.connect():
current_app.logger.error("impossible de se connecter au serveur")
else:
# send order to KineIntercom process
if not conn.send(b"SHUTDOWN\n"):
conn.disconnect()
current_app.logger.error("impossible de communiquer avec le serveur")
conn.disconnect()
time.sleep(3)
reboot_cmd = ['sudo', '/bin/systemctl', 'poweroff'] reboot_cmd = ['sudo', '/bin/systemctl', 'poweroff']
if not execute_cmd(reboot_cmd): if not execute_cmd(reboot_cmd):
abort(status.HTTP_500_INTERNAL_SERVER_ERROR, abort(status.HTTP_500_INTERNAL_SERVER_ERROR,