Files
WPIQ/wo/core/services.py

177 lines
7.6 KiB
Python
Raw Normal View History

2018-11-13 21:55:59 +01:00
"""WordOps Service Manager"""
import subprocess
2019-09-04 20:36:15 +02:00
2018-11-13 21:55:59 +01:00
from wo.core.logging import Log
2019-07-29 04:10:47 +02:00
2018-11-13 21:55:59 +01:00
class WOService():
"""Intialization for service"""
def ___init__():
pass
def start_service(self, service_name):
"""
start service
Similar to `service xyz start`
"""
try:
2019-08-27 10:20:25 +02:00
if service_name in ['nginx']:
2019-09-04 19:25:09 +02:00
Log.wait(self, "Testing Nginx configuration ")
# Check Nginx configuration before executing command
sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
output, error_output = sub.communicate()
if 'emerg' not in str(error_output):
2019-09-04 19:25:09 +02:00
Log.valide(self, "Testing Nginx configuration ")
Log.wait(self, "Starting Nginx ")
service_cmd = ('service {0} start'.format(service_name))
2019-08-27 10:20:25 +02:00
retcode = subprocess.getstatusoutput(service_cmd)
2019-08-27 12:32:48 +02:00
if retcode[0] == 0:
2019-09-04 19:25:09 +02:00
Log.valide(self, "Starting Nginx ")
2019-08-27 12:32:48 +02:00
return True
2019-08-27 10:20:25 +02:00
else:
2019-09-04 19:25:09 +02:00
Log.failed(self, "Testing Nginx configuration ")
2019-08-27 10:20:25 +02:00
return False
2018-11-13 21:55:59 +01:00
else:
service_cmd = ('service {0} start'.format(service_name))
2019-08-27 10:20:25 +02:00
Log.info(self, "Start : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput(service_cmd)
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
2018-11-13 21:55:59 +01:00
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to start service {0}"
.format(service_name))
def stop_service(self, service_name):
"""
Stop service
Similar to `service xyz stop`
"""
try:
Log.info(self, "Stop : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} stop'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to stop service : {0}"
.format(service_name))
def restart_service(self, service_name):
"""
Restart service
Similar to `service xyz restart`
"""
try:
2019-08-27 10:20:25 +02:00
if service_name in ['nginx']:
2019-09-04 19:25:09 +02:00
Log.wait(self, "Testing Nginx configuration ")
# Check Nginx configuration before executing command
sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
output, error_output = sub.communicate()
if 'emerg' not in str(error_output):
2019-09-04 19:25:09 +02:00
Log.valide(self, "Testing Nginx configuration ")
Log.wait(self, "Restarting Nginx ")
service_cmd = ('service {0} restart'.format(service_name))
2019-08-27 10:20:25 +02:00
retcode = subprocess.getstatusoutput(service_cmd)
2019-08-27 12:32:48 +02:00
if retcode[0] == 0:
2019-09-04 19:25:09 +02:00
Log.valide(self, "Restarting Nginx ")
2019-08-27 12:32:48 +02:00
return True
2019-08-27 10:20:25 +02:00
else:
2019-09-04 19:25:09 +02:00
Log.failed(self, "Testing Nginx configuration ")
2019-08-27 10:20:25 +02:00
return False
2018-11-13 21:55:59 +01:00
else:
service_cmd = ('service {0} restart'.format(service_name))
2019-09-04 19:25:09 +02:00
Log.wait(self, "Restarting {0:10}".format(
service_name))
2019-08-27 10:20:25 +02:00
retcode = subprocess.getstatusoutput(service_cmd)
if retcode[0] == 0:
2019-09-04 19:25:09 +02:00
Log.valide(self, "Restarting {0:10}".format(
service_name))
2019-08-27 10:20:25 +02:00
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
2019-09-04 19:25:09 +02:00
Log.failed(self, "Restarting {0:10}".format(
service_name))
2019-08-27 10:20:25 +02:00
return False
2018-11-13 21:55:59 +01:00
except OSError as e:
Log.debug(self, "{0} {1}".format(e.errno, e.strerror))
Log.error(self, "\nFailed to restart service : {0}"
.format(service_name))
def reload_service(self, service_name):
"""
2019-08-27 13:40:43 +02:00
Reload service
Similar to `service xyz reload`
2018-11-13 21:55:59 +01:00
"""
try:
2019-08-27 10:20:25 +02:00
if service_name in ['nginx']:
# Check Nginx configuration before executing command
2019-09-04 19:25:09 +02:00
Log.wait(self, "Testing Nginx configuration ")
sub = subprocess.Popen('nginx -t', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
output, error_output = sub.communicate()
if 'emerg' not in str(error_output):
2019-09-04 19:25:09 +02:00
Log.valide(self, "Testing Nginx configuration ")
Log.wait(self, "Reloading Nginx ")
2019-08-27 12:32:48 +02:00
service_cmd = ('service {0} reload'.format(service_name))
2019-08-27 10:20:25 +02:00
retcode = subprocess.getstatusoutput(service_cmd)
2019-08-27 12:32:48 +02:00
if retcode[0] == 0:
2019-09-04 19:25:09 +02:00
Log.valide(self, "Reloading Nginx ")
2019-08-27 12:32:48 +02:00
return True
2019-08-27 10:20:25 +02:00
else:
2019-09-04 19:25:09 +02:00
Log.failed(self, "Testing Nginx configuration ")
2019-08-27 10:20:25 +02:00
return False
2018-11-13 21:55:59 +01:00
else:
service_cmd = ('service {0} reload'.format(service_name))
2019-09-04 19:25:09 +02:00
Log.wait(self, "Reloading {0:10}".format(
service_name))
2019-08-27 10:20:25 +02:00
retcode = subprocess.getstatusoutput(service_cmd)
if retcode[0] == 0:
2019-09-04 19:25:09 +02:00
Log.valide(self, "Reloading {0:10}".format(
service_name))
2019-08-27 10:20:25 +02:00
else:
Log.debug(self, "{0}".format(retcode[1]))
2019-09-04 19:25:09 +02:00
Log.failed(self, "Reloading {0:10}".format(
service_name))
2019-08-27 10:20:25 +02:00
return False
2018-11-13 21:55:59 +01:00
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to reload service {0}"
.format(service_name))
def get_service_status(self, service_name):
try:
is_exist = subprocess.getstatusoutput('which {0}'
.format(service_name))
2019-04-29 02:06:32 +02:00
if is_exist[0] == 0 or service_name in ['php7.2-fpm',
'php7.3-fpm']:
2018-11-13 21:55:59 +01:00
retcode = subprocess.getstatusoutput('service {0} status'
.format(service_name))
if retcode[0] == 0:
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
return False
else:
return False
except OSError as e:
Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
Log.error(self, "Unable to get services status of {0}"
.format(service_name))
return False