Files
WPIQ/wo/cli/plugins/stack_services.py

446 lines
16 KiB
Python
Raw Normal View History

2019-09-02 04:37:13 +02:00
import os
2018-11-13 21:55:59 +01:00
from cement.core import handler, hook
2019-09-02 04:37:13 +02:00
from cement.core.controller import CementBaseController, expose
2019-09-04 20:36:15 +02:00
2019-09-02 04:37:13 +02:00
from wo.core.aptget import WOAptGet
2018-11-13 21:55:59 +01:00
from wo.core.logging import Log
2019-09-02 04:37:13 +02:00
from wo.core.services import WOService
2019-10-02 13:13:32 +02:00
from wo.core.variables import WOVar
2018-11-13 21:55:59 +01:00
class WOStackStatusController(CementBaseController):
class Meta:
label = 'stack_services'
stacked_on = 'stack'
stacked_type = 'embedded'
description = 'Check the stack status'
@expose(help="Start stack services")
def start(self):
"""Start services"""
services = []
2019-08-27 22:25:18 +02:00
pargs = self.app.pargs
2019-08-07 02:45:26 +02:00
if not (pargs.nginx or pargs.php or
pargs.php73 or
pargs.mysql or
pargs.redis or
pargs.fail2ban or
pargs.proftpd or
pargs.netdata):
pargs.nginx = True
pargs.php = True
pargs.mysql = True
2019-08-16 14:53:39 +02:00
pargs.fail2ban = True
pargs.netdata = True
2019-08-07 02:45:26 +02:00
if pargs.nginx:
2019-03-25 11:47:56 +01:00
if (WOAptGet.is_installed(self, 'nginx-custom')):
2018-11-13 21:55:59 +01:00
services = services + ['nginx']
else:
Log.info(self, "Nginx is not installed")
2019-08-07 02:45:26 +02:00
if pargs.php:
if WOAptGet.is_installed(self, 'php7.2-fpm'):
services = services + ['php7.2-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.2-FPM is not installed")
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
else:
Log.info(self, "PHP7.3-FPM is not installed")
2019-08-07 02:45:26 +02:00
if pargs.php73:
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-08-07 02:45:26 +02:00
if pargs.mysql:
2019-10-02 13:13:32 +02:00
if ((WOVar.wo_mysql_host == "localhost") or
(WOVar.wo_mysql_host == "127.0.0.1")):
2018-11-13 21:55:59 +01:00
if (WOAptGet.is_installed(self, 'mysql-server') or
WOAptGet.is_installed(self, 'percona-server-server-5.6') or
WOAptGet.is_installed(self, 'mariadb-server')):
2018-11-13 21:55:59 +01:00
services = services + ['mysql']
else:
Log.info(self, "MySQL is not installed")
else:
Log.warn(self, "Remote MySQL found, "
"Unable to check MySQL service status")
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'redis-server'):
services = services + ['redis-server']
else:
Log.info(self, "Redis server is not installed")
2019-08-07 02:45:26 +02:00
if pargs.fail2ban:
2019-04-25 01:38:14 +02:00
if WOAptGet.is_installed(self, 'fail2ban'):
2019-07-18 14:32:41 +02:00
services = services + ['fail2ban']
2019-04-25 01:38:14 +02:00
else:
Log.info(self, "fail2ban is not installed")
2019-07-19 13:47:29 +02:00
# proftpd
2019-08-07 02:45:26 +02:00
if pargs.proftpd:
2019-07-19 13:47:29 +02:00
if WOAptGet.is_installed(self, 'proftpd-basic'):
services = services + ['proftpd']
else:
Log.info(self, "ProFTPd is not installed")
# netdata
2019-08-07 02:45:26 +02:00
if pargs.netdata:
2019-08-28 14:22:11 +02:00
if (os.path.isdir("/opt/netdata") or
os.path.isdir("/etc/netdata")):
2019-07-19 13:47:29 +02:00
services = services + ['netdata']
else:
Log.info(self, "Netdata is not installed")
2018-11-13 21:55:59 +01:00
for service in services:
Log.debug(self, "Starting service: {0}".format(service))
WOService.start_service(self, service)
@expose(help="Stop stack services")
def stop(self):
"""Stop services"""
services = []
2019-08-07 02:45:26 +02:00
pargs = self.app.pargs
if not (pargs.nginx or pargs.php or
pargs.php73 or
pargs.mysql or
pargs.fail2ban or
pargs.netdata or
pargs.proftpd or
pargs.redis):
pargs.nginx = True
pargs.php = True
pargs.mysql = True
2018-11-13 21:55:59 +01:00
2019-07-19 13:47:29 +02:00
# nginx
2019-08-07 02:45:26 +02:00
if pargs.nginx:
2019-03-25 11:47:56 +01:00
if (WOAptGet.is_installed(self, 'nginx-custom')):
2018-11-13 21:55:59 +01:00
services = services + ['nginx']
else:
Log.info(self, "Nginx is not installed")
2019-07-19 13:47:29 +02:00
# php7.2
2019-08-07 02:45:26 +02:00
if pargs.php:
if WOAptGet.is_installed(self, 'php7.2-fpm'):
services = services + ['php7.2-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.2-FPM is not installed")
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
else:
Log.info(self, "PHP7.3-FPM is not installed")
2019-07-19 13:47:29 +02:00
# php7.3
2019-08-07 02:45:26 +02:00
if pargs.php73:
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-07-19 13:47:29 +02:00
# mysql
2019-08-07 02:45:26 +02:00
if pargs.mysql:
2019-10-02 13:13:32 +02:00
if ((WOVar.wo_mysql_host == "localhost") or
(WOVar.wo_mysql_host == "127.0.0.1")):
2018-11-13 21:55:59 +01:00
if (WOAptGet.is_installed(self, 'mysql-server') or
WOAptGet.is_installed(self, 'percona-server-server-5.6') or
WOAptGet.is_installed(self, 'mariadb-server')):
2018-11-13 21:55:59 +01:00
services = services + ['mysql']
else:
Log.info(self, "MySQL is not installed")
else:
Log.warn(self, "Remote MySQL found, "
"Unable to check MySQL service status")
2019-07-19 13:47:29 +02:00
# redis
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'redis-server'):
services = services + ['redis-server']
else:
Log.info(self, "Redis server is not installed")
2019-07-19 13:47:29 +02:00
# fail2ban
2019-08-07 02:45:26 +02:00
if pargs.fail2ban:
2019-04-25 01:38:14 +02:00
if WOAptGet.is_installed(self, 'fail2ban'):
2019-07-18 14:32:41 +02:00
services = services + ['fail2ban']
2019-04-25 01:38:14 +02:00
else:
Log.info(self, "fail2ban is not installed")
2019-07-19 13:47:29 +02:00
# proftpd
2019-08-07 02:45:26 +02:00
if pargs.proftpd:
2019-07-19 13:47:29 +02:00
if WOAptGet.is_installed(self, 'proftpd-basic'):
services = services + ['proftpd']
else:
Log.info(self, "ProFTPd is not installed")
# netdata
2019-08-07 02:45:26 +02:00
if pargs.netdata:
2019-08-28 14:22:11 +02:00
if (os.path.isdir("/opt/netdata") or
os.path.isdir("/etc/netdata")):
2019-07-19 13:47:29 +02:00
services = services + ['netdata']
else:
Log.info(self, "Netdata is not installed")
2018-11-13 21:55:59 +01:00
for service in services:
Log.debug(self, "Stopping service: {0}".format(service))
WOService.stop_service(self, service)
@expose(help="Restart stack services")
def restart(self):
"""Restart services"""
services = []
2019-08-07 02:45:26 +02:00
pargs = self.app.pargs
if not (pargs.nginx or pargs.php or
pargs.php73 or
pargs.mysql or
pargs.netdata or
pargs.proftpd or
pargs.redis or
pargs.fail2ban):
pargs.nginx = True
pargs.php = True
pargs.mysql = True
2019-08-16 14:53:39 +02:00
pargs.netdata = True
2019-08-07 02:45:26 +02:00
if pargs.nginx:
2019-03-25 11:47:56 +01:00
if (WOAptGet.is_installed(self, 'nginx-custom')):
2018-11-13 21:55:59 +01:00
services = services + ['nginx']
else:
Log.info(self, "Nginx is not installed")
2019-08-07 02:45:26 +02:00
if pargs.php:
if WOAptGet.is_installed(self, 'php7.2-fpm'):
services = services + ['php7.2-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.2-FPM is not installed")
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-08-07 02:45:26 +02:00
if pargs.php73:
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-08-07 02:45:26 +02:00
if pargs.mysql:
2019-10-02 13:13:32 +02:00
if ((WOVar.wo_mysql_host == "localhost") or
(WOVar.wo_mysql_host == "127.0.0.1")):
2019-03-15 17:52:13 +01:00
if ((WOAptGet.is_installed(self, 'mysql-server') or
2019-04-14 19:43:23 +02:00
WOAptGet.is_installed(self,
'percona-server-server-5.6') or
2019-03-15 17:52:13 +01:00
WOAptGet.is_installed(self, 'mariadb-server'))):
2018-11-13 21:55:59 +01:00
services = services + ['mysql']
else:
Log.info(self, "MySQL is not installed")
else:
Log.warn(self, "Remote MySQL found, "
"Unable to check MySQL service status")
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'redis-server'):
services = services + ['redis-server']
else:
Log.info(self, "Redis server is not installed")
2019-08-07 02:45:26 +02:00
if pargs.fail2ban:
2019-04-25 01:38:14 +02:00
if WOAptGet.is_installed(self, 'fail2ban'):
2019-07-18 14:32:41 +02:00
services = services + ['fail2ban']
2019-04-25 01:38:14 +02:00
else:
Log.info(self, "fail2ban is not installed")
2019-07-19 13:47:29 +02:00
# proftpd
2019-08-07 02:45:26 +02:00
if pargs.proftpd:
2019-07-19 13:47:29 +02:00
if WOAptGet.is_installed(self, 'proftpd-basic'):
services = services + ['proftpd']
else:
Log.info(self, "ProFTPd is not installed")
# netdata
2019-08-07 02:45:26 +02:00
if pargs.netdata:
2019-08-28 14:22:11 +02:00
if (os.path.isdir("/opt/netdata") or
os.path.isdir("/etc/netdata")):
2019-07-19 13:47:29 +02:00
services = services + ['netdata']
else:
Log.info(self, "Netdata is not installed")
2018-11-13 21:55:59 +01:00
for service in services:
Log.debug(self, "Restarting service: {0}".format(service))
WOService.restart_service(self, service)
@expose(help="Get stack status")
def status(self):
"""Status of services"""
services = []
2019-08-07 02:45:26 +02:00
pargs = self.app.pargs
if not (pargs.nginx or pargs.php or
pargs.php73 or
pargs.mysql or
pargs.netdata or
pargs.proftpd or
pargs.redis or
pargs.fail2ban):
pargs.nginx = True
pargs.php = True
pargs.mysql = True
2019-08-16 14:53:39 +02:00
pargs.fail2ban = True
pargs.netdata = True
2019-08-07 02:45:26 +02:00
if pargs.nginx:
2019-03-25 11:47:56 +01:00
if (WOAptGet.is_installed(self, 'nginx-custom')):
2018-11-13 21:55:59 +01:00
services = services + ['nginx']
else:
Log.info(self, "Nginx is not installed")
2019-08-07 02:45:26 +02:00
if pargs.php:
if WOAptGet.is_installed(self, 'php7.2-fpm'):
services = services + ['php7.2-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.2-FPM is not installed")
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
else:
Log.info(self, "PHP7.3-FPM is not installed")
2019-08-07 02:45:26 +02:00
if pargs.php73:
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-08-07 02:45:26 +02:00
if pargs.mysql:
2019-10-02 13:13:32 +02:00
if ((WOVar.wo_mysql_host == "localhost") or
(WOVar.wo_mysql_host == "127.0.0.1")):
2018-11-13 21:55:59 +01:00
if (WOAptGet.is_installed(self, 'mysql-server') or
WOAptGet.is_installed(self, 'percona-server-server-5.6') or
WOAptGet.is_installed(self, 'mariadb-server')):
2018-11-13 21:55:59 +01:00
services = services + ['mysql']
else:
Log.info(self, "MySQL is not installed")
else:
Log.warn(self, "Remote MySQL found, "
"Unable to check MySQL service status")
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'redis-server'):
services = services + ['redis-server']
else:
Log.info(self, "Redis server is not installed")
2019-08-07 02:45:26 +02:00
if pargs.fail2ban:
2019-04-25 01:38:14 +02:00
if WOAptGet.is_installed(self, 'fail2ban'):
2019-07-18 14:32:41 +02:00
services = services + ['fail2ban']
2019-04-25 01:38:14 +02:00
else:
Log.info(self, "fail2ban is not installed")
2019-07-19 13:47:29 +02:00
# proftpd
2019-08-07 02:45:26 +02:00
if pargs.proftpd:
2019-07-19 13:47:29 +02:00
if WOAptGet.is_installed(self, 'proftpd-basic'):
services = services + ['proftpd']
else:
Log.info(self, "ProFTPd is not installed")
# netdata
2019-08-07 02:45:26 +02:00
if pargs.netdata:
2019-08-28 14:22:11 +02:00
if (os.path.isdir("/opt/netdata") or
os.path.isdir("/etc/netdata")):
2019-07-19 13:47:29 +02:00
services = services + ['netdata']
else:
Log.info(self, "Netdata is not installed")
2018-11-13 21:55:59 +01:00
for service in services:
if WOService.get_service_status(self, service):
Log.info(self, "{0:10}: {1}".format(service, "Running"))
@expose(help="Reload stack services")
def reload(self):
"""Reload service"""
services = []
2019-08-07 02:45:26 +02:00
pargs = self.app.pargs
if not (pargs.nginx or pargs.php or
pargs.php73 or
pargs.mysql or
pargs.netdata or
pargs.proftpd or
pargs.redis or
pargs.fail2ban):
pargs.nginx = True
pargs.php = True
pargs.mysql = True
2019-08-16 14:53:39 +02:00
pargs.fail2ban = True
2019-08-07 02:45:26 +02:00
if pargs.nginx:
2019-03-15 17:52:13 +01:00
if (WOAptGet.is_installed(self, 'nginx-custom') or
WOAptGet.is_installed(self, 'nginx-mainline')):
2018-11-13 21:55:59 +01:00
services = services + ['nginx']
else:
Log.info(self, "Nginx is not installed")
2019-08-07 02:45:26 +02:00
if pargs.php:
if WOAptGet.is_installed(self, 'php7.2-fpm'):
services = services + ['php7.2-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.2-FPM is not installed")
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
2019-03-07 13:26:25 +01:00
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-08-07 02:45:26 +02:00
if pargs.php73:
if WOAptGet.is_installed(self, 'php7.3-fpm'):
services = services + ['php7.3-fpm']
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "PHP7.3-FPM is not installed")
2018-11-13 21:55:59 +01:00
2019-08-07 02:45:26 +02:00
if pargs.mysql:
2019-10-02 13:13:32 +02:00
if ((WOVar.wo_mysql_host == "localhost") or
(WOVar.wo_mysql_host == "127.0.0.1")):
2018-11-13 21:55:59 +01:00
if (WOAptGet.is_installed(self, 'mysql-server') or
WOAptGet.is_installed(self, 'percona-server-server-5.6') or
WOAptGet.is_installed(self, 'mariadb-server')):
2018-11-13 21:55:59 +01:00
services = services + ['mysql']
else:
Log.info(self, "MySQL is not installed")
else:
Log.warn(self, "Remote MySQL found, "
"Unable to check MySQL service status")
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-13 21:55:59 +01:00
if WOAptGet.is_installed(self, 'redis-server'):
services = services + ['redis-server']
else:
Log.info(self, "Redis server is not installed")
2019-08-07 02:45:26 +02:00
if pargs.fail2ban:
2019-04-25 01:38:14 +02:00
if WOAptGet.is_installed(self, 'fail2ban'):
2019-07-18 14:32:41 +02:00
services = services + ['fail2ban']
2019-04-25 01:38:14 +02:00
else:
Log.info(self, "fail2ban is not installed")
2019-07-19 13:47:29 +02:00
# proftpd
2019-08-07 02:45:26 +02:00
if pargs.proftpd:
2019-07-19 13:47:29 +02:00
if WOAptGet.is_installed(self, 'proftpd-basic'):
services = services + ['proftpd']
else:
Log.info(self, "ProFTPd is not installed")
# netdata
2019-08-07 02:45:26 +02:00
if pargs.netdata:
2019-08-28 14:22:11 +02:00
if (os.path.isdir("/opt/netdata") or
os.path.isdir("/etc/netdata")):
2019-07-19 13:47:29 +02:00
services = services + ['netdata']
else:
Log.info(self, "Netdata is not installed")
2018-11-13 21:55:59 +01:00
for service in services:
Log.debug(self, "Reloading service: {0}".format(service))
WOService.reload_service(self, service)