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

104 lines
3.7 KiB
Python
Raw Normal View History

2018-11-13 21:55:59 +01:00
"""Clean Plugin for WordOps."""
import os
2019-10-21 11:37:19 +02:00
import requests
2018-11-13 21:55:59 +01:00
2019-09-04 20:36:15 +02:00
from cement.core.controller import CementBaseController, expose
from wo.core.aptget import WOAptGet
from wo.core.logging import Log
from wo.core.services import WOService
from wo.core.shellexec import WOShellExec
2023-08-05 17:31:40 +02:00
from wo.core.variables import WOVar
2019-09-04 20:36:15 +02:00
2018-11-13 21:55:59 +01:00
def wo_clean_hook(app):
pass
class WOCleanController(CementBaseController):
class Meta:
label = 'clean'
stacked_on = 'base'
stacked_type = 'nested'
2019-03-20 12:55:26 +01:00
description = (
'Clean NGINX FastCGI cache, Opcache, Redis Cache')
2018-11-13 21:55:59 +01:00
arguments = [
(['--all'],
dict(help='Clean all cache', action='store_true')),
(['--fastcgi'],
dict(help='Clean FastCGI cache', action='store_true')),
(['--opcache'],
dict(help='Clean OpCache', action='store_true')),
(['--redis'],
dict(help='Clean Redis Cache', action='store_true')),
2019-03-20 12:55:26 +01:00
]
2018-11-13 21:55:59 +01:00
usage = "wo clean [options]"
@expose(hide=True)
def default(self):
2019-08-07 02:45:26 +02:00
pargs = self.app.pargs
if ((not pargs.all) and (not pargs.fastcgi) and
(not pargs.opcache) and (not pargs.redis)):
2018-11-13 21:55:59 +01:00
self.clean_fastcgi()
2019-08-07 02:45:26 +02:00
if pargs.all:
2018-11-13 21:55:59 +01:00
self.clean_fastcgi()
self.clean_opcache()
self.clean_redis()
2019-08-07 02:45:26 +02:00
if pargs.fastcgi:
2018-11-13 21:55:59 +01:00
self.clean_fastcgi()
2019-08-07 02:45:26 +02:00
if pargs.opcache:
2018-11-13 21:55:59 +01:00
self.clean_opcache()
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-13 21:55:59 +01:00
self.clean_redis()
2018-11-13 21:55:59 +01:00
@expose(hide=True)
def clean_redis(self):
"""This function clears Redis cache"""
2023-08-05 17:31:40 +02:00
if (WOAptGet.is_installed(self, "redis-server")):
2018-11-13 21:55:59 +01:00
Log.info(self, "Cleaning Redis cache")
WOShellExec.cmd_exec(self, "redis-cli flushall")
else:
Log.info(self, "Redis is not installed")
@expose(hide=True)
def clean_fastcgi(self):
2023-08-05 17:31:40 +02:00
if (os.path.isdir("/var/run/nginx-cache") and
os.path.exists('/usr/sbin/nginx')):
2018-11-13 21:55:59 +01:00
Log.info(self, "Cleaning NGINX FastCGI cache")
WOShellExec.cmd_exec(self, "rm -rf /var/run/nginx-cache/*")
2019-08-13 03:46:15 +02:00
WOService.restart_service(self, 'nginx')
2018-11-13 21:55:59 +01:00
else:
Log.error(self, "Unable to clean FastCGI cache", False)
@expose(hide=True)
def clean_opcache(self):
opcache_dir = '/var/www/22222/htdocs/cache/opcache/'
if (os.path.exists('/usr/sbin/nginx') and
os.path.exists(
'/var/www/22222/htdocs/cache/opcache')):
try:
Log.info(self, "Cleaning opcache")
2023-08-05 17:31:40 +02:00
wo_php_version = list(WOVar.wo_php_versions.keys())
for wo_php in wo_php_version:
if os.path.exists('{0}{1}.php'.format(opcache_dir, wo_php)):
requests.get(
"http://127.0.0.1/cache/opcache/{0}.php".format(wo_php))
except requests.HTTPError as e:
Log.debug(self, "{0}".format(e))
Log.debug(self, "Unable hit url, "
" http://127.0.0.1/cache/opcache/"
2024-05-29 02:00:50 +02:00
"phpXX.php,"
" please check you have admin tools installed")
Log.debug(self, "please check you have admin tools installed,"
" or install them with `wo stack install --admin`")
Log.error(self, "Unable to clean opcache", False)
2018-11-13 21:55:59 +01:00
2018-11-13 21:55:59 +01:00
def load(app):
# register the plugin class.. this only happens if the plugin is enabled
2019-09-24 00:01:20 +02:00
app.handler.register(WOCleanController)
2018-11-13 21:55:59 +01:00
# register a hook (function) to run after arguments are parsed.
2019-09-24 00:04:32 +02:00
app.hook.register('post_argument_parsing', wo_clean_hook)