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

96 lines
3.2 KiB
Python
Raw Normal View History

2018-11-13 21:55:59 +01:00
"""Clean Plugin for WordOps."""
import os
import urllib.request
2019-09-04 20:36:15 +02:00
from cement.core import handler, hook
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
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
2019-09-02 18:56:34 +02:00
if (not (pargs.all or pargs.fastcgi
or pargs.opcache or
2019-08-07 02:45:26 +02:00
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"""
if(WOAptGet.is_installed(self, "redis-server")):
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):
if(os.path.isdir("/var/run/nginx-cache")):
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):
try:
Log.info(self, "Cleaning opcache")
2019-08-13 03:46:15 +02:00
urllib.request.urlopen("https://127.0.0.1:22222/cache"
2019-07-29 15:08:49 +02:00
"/opcache/opgui.php?reset=1").read()
2018-11-13 21:55:59 +01:00
except Exception as e:
2019-03-20 12:55:26 +01:00
Log.debug(self, "{0}".format(e))
Log.debug(self, "Unable hit url, "
2019-04-05 10:02:39 +02:00
" https://127.0.0.1:22222/cache/opcache/"
"opgui.php?reset=1,"
2019-03-20 12:55:26 +01:00
" 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
handler.register(WOCleanController)
# register a hook (function) to run after arguments are parsed.
hook.register('post_argument_parsing', wo_clean_hook)