Fix nginx restart service

This commit is contained in:
VirtuBox
2019-08-27 10:20:25 +02:00
parent b9d9969093
commit 378c9a4820

View File

@@ -1,10 +1,9 @@
"""WordOps Service Manager""" """WordOps Service Manager"""
import os
import subprocess import subprocess
from subprocess import Popen
from wo.core.logging import Log from wo.core.logging import Log
class WOService(): class WOService():
"""Intialization for service""" """Intialization for service"""
def ___init__(): def ___init__():
@@ -16,25 +15,31 @@ class WOService():
Similar to `service xyz start` Similar to `service xyz start`
""" """
try: try:
if service_name in ['nginx', 'php5-fpm']: if service_name in ['nginx']:
# Check Nginx configuration before executing command # Check Nginx configuration before executing command
sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE, sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) stderr=subprocess.PIPE, shell=True)
output, error_output = sub.communicate() output, error_output = sub.communicate()
if 'emerg' not in str(error_output): if 'emerg' not in str(error_output):
service_cmd = ('service {0} start'.format(service_name)) service_cmd = ('service {0} start'.format(service_name))
retcode = subprocess.getstatusoutput(service_cmd)
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
else: else:
service_cmd = ('service {0} start'.format(service_name)) service_cmd = ('service {0} start'.format(service_name))
Log.info(self, "Start : {0:10}" .format(service_name), end='') Log.info(self, "Start : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput(service_cmd) retcode = subprocess.getstatusoutput(service_cmd)
if retcode[0] == 0: if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]") Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True return True
else: else:
Log.debug(self, "{0}".format(retcode[1])) Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]") Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False return False
except OSError as e: except OSError as e:
Log.debug(self, "{0}".format(e)) Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to start service {0}" Log.error(self, "\nFailed to start service {0}"
@@ -67,25 +72,30 @@ class WOService():
Similar to `service xyz restart` Similar to `service xyz restart`
""" """
try: try:
if service_name in ['nginx', 'php5-fpm']: if service_name in ['nginx']:
# Check Nginx configuration before executing command # Check Nginx configuration before executing command
sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE, sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) stderr=subprocess.PIPE, shell=True)
output, error_output = sub.communicate() output, error_output = sub.communicate()
if 'emerg' not in str(error_output): if 'emerg' not in str(error_output):
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
service_cmd = ('service {0} restart'.format(service_name)) service_cmd = ('service {0} restart'.format(service_name))
retcode = subprocess.getstatusoutput(service_cmd)
return True
else:
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
else: else:
service_cmd = ('service {0} restart'.format(service_name)) service_cmd = ('service {0} restart'.format(service_name))
Log.info(self, "Restart : {0:10}".format(service_name), end='')
Log.info(self, "Restart : {0:10}".format(service_name), end='') retcode = subprocess.getstatusoutput(service_cmd)
retcode = subprocess.getstatusoutput(service_cmd) if retcode[0] == 0:
if retcode[0] == 0: Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]") return True
return True else:
else: Log.debug(self, "{0}".format(retcode[1]))
Log.debug(self, "{0}".format(retcode[1])) Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]") return False
return False
except OSError as e: except OSError as e:
Log.debug(self, "{0} {1}".format(e.errno, e.strerror)) Log.debug(self, "{0} {1}".format(e.errno, e.strerror))
Log.error(self, "\nFailed to restart service : {0}" Log.error(self, "\nFailed to restart service : {0}"
@@ -97,25 +107,32 @@ class WOService():
Similar to `service xyz stop` Similar to `service xyz stop`
""" """
try: try:
if service_name in ['nginx', 'php5-fpm']: if service_name in ['nginx']:
# Check Nginx configuration before executing command # Check Nginx configuration before executing command
sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE, sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) stderr=subprocess.PIPE, shell=True)
output, error_output = sub.communicate() output, error_output = sub.communicate()
if 'emerg' not in str(error_output): if 'emerg' not in str(error_output):
service_cmd = ('service {0} restart'.format(service_name)) service_cmd = ('service {0} restart'.format(service_name))
retcode = subprocess.getstatusoutput(service_cmd)
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
else: else:
service_cmd = ('service {0} reload'.format(service_name)) service_cmd = ('service {0} reload'.format(service_name))
Log.info(self, "Reload : {0:10}".format(service_name), end='') Log.info(self, "Reload : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput(service_cmd) retcode = subprocess.getstatusoutput(service_cmd)
if retcode[0] == 0: if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]") Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True return True
else: else:
Log.debug(self, "{0}".format(retcode[1])) Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]") Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False return False
except OSError as e: except OSError as e:
Log.debug(self, "{0}".format(e)) Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to reload service {0}" Log.error(self, "\nFailed to reload service {0}"