Complete conversion of the WordOps stack from Nginx + PHP-FPM to OpenLiteSpeed + LSPHP + LSCache. This is a full rewrite across all 7 phases of the codebase: - Foundation: OLS paths, variables, services, removed pynginxconfig dep - Templates: 11 new OLS mustache templates, removed nginx-specific ones - Stack: stack_pref, stack, stack_services, stack_upgrade, stack_migrate - Site: site_functions, site, site_create, site_update - Plugins: debug, info, log, clean rewritten for OLS - SSL/ACME: acme.sh deploy uses lswsctrl, OLS vhssl blocks - Other: secure, backup, clone, install script Additional features: - Debian 13 (trixie) support - PHP 8.5 support - WP Fort Knox mu-plugin integration (wo secure --lockdown/--unlock) - --nginx CLI flag preserved for backward compatibility Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
460 lines
22 KiB
Python
460 lines
22 KiB
Python
"""Debug Plugin for WordOps"""
|
|
|
|
import glob
|
|
import os
|
|
import signal
|
|
|
|
from cement.core.controller import CementBaseController, expose
|
|
|
|
from wo.cli.plugins.site_functions import logwatch
|
|
from wo.core.fileutils import WOFileUtils
|
|
from wo.core.logging import Log
|
|
from wo.core.mysql import WOMysql
|
|
from wo.core.services import WOService
|
|
from wo.core.shellexec import WOShellExec, CommandExecutionError
|
|
from wo.core.variables import WOVar
|
|
|
|
|
|
def wo_debug_hook(app):
|
|
pass
|
|
|
|
|
|
class WODebugController(CementBaseController):
|
|
class Meta:
|
|
label = 'debug'
|
|
description = 'Used for server level debugging'
|
|
stacked_on = 'base'
|
|
stacked_type = 'nested'
|
|
arguments = [
|
|
(['--stop'],
|
|
dict(help='Stop debug', action='store_true')),
|
|
(['--start'],
|
|
dict(help='Start debug', action='store_true')),
|
|
(['--import-slow-log'],
|
|
dict(help='Import MySQL slow log to Anemometer database',
|
|
action='store_true')),
|
|
(['--nginx'],
|
|
dict(help='start/stop debugging OpenLiteSpeed server '
|
|
'configuration',
|
|
action='store' or 'store_const',
|
|
choices=('on', 'off'), const='on', nargs='?')),
|
|
(['--mysql'],
|
|
dict(help='start/stop debugging MySQL server',
|
|
action='store' or 'store_const',
|
|
choices=('on', 'off'), const='on', nargs='?')),
|
|
(['--wp'],
|
|
dict(help='start/stop wordpress debugging for site',
|
|
action='store' or 'store_const', choices=('on', 'off'),
|
|
const='on', nargs='?')),
|
|
(['--all'],
|
|
dict(help='start/stop debugging all server parameters',
|
|
action='store' or 'store_const', choices=('on', 'off'),
|
|
const='on', nargs='?')),
|
|
(['-i', '--interactive'],
|
|
dict(help='Interactive debug', action='store_true')),
|
|
(['--import-slow-log-interval'],
|
|
dict(help='Import MySQL slow log to Anemometer',
|
|
action='store', dest='interval')),
|
|
(['site_name'],
|
|
dict(help='Website Name', nargs='?', default=None))
|
|
]
|
|
usage = "wo debug [<site_name>] [options] "
|
|
|
|
@expose(hide=True)
|
|
def debug_ols(self):
|
|
"""Start/Stop OpenLiteSpeed debug"""
|
|
ols_conf = "{0}/httpd_config.conf".format(WOVar.wo_ols_conf_dir)
|
|
|
|
# start global debug
|
|
if (self.app.pargs.nginx == 'on' and not self.app.pargs.site_name):
|
|
if not WOFileUtils.grepcheck(self, ols_conf, 'logLevel DEBUG'):
|
|
Log.info(self, "Setting up OpenLiteSpeed debug log level")
|
|
WOFileUtils.searchreplace(
|
|
self, ols_conf,
|
|
'logLevel NOTICE', 'logLevel DEBUG')
|
|
self.trigger_ols = True
|
|
else:
|
|
Log.info(self, "OpenLiteSpeed debug already enabled")
|
|
|
|
self.msg = self.msg + [
|
|
'/usr/local/lsws/logs/error.log']
|
|
|
|
# stop global debug
|
|
elif (self.app.pargs.nginx == 'off' and
|
|
not self.app.pargs.site_name):
|
|
if WOFileUtils.grepcheck(self, ols_conf, 'logLevel DEBUG'):
|
|
Log.info(self, "Disabling OpenLiteSpeed debug log level")
|
|
WOFileUtils.searchreplace(
|
|
self, ols_conf,
|
|
'logLevel DEBUG', 'logLevel NOTICE')
|
|
self.trigger_ols = True
|
|
else:
|
|
Log.info(self, "OpenLiteSpeed debug already disabled")
|
|
|
|
# start site-specific debug
|
|
elif (self.app.pargs.nginx == 'on' and self.app.pargs.site_name):
|
|
vhconf = "{0}/{1}/vhconf.conf".format(
|
|
WOVar.wo_ols_vhost_dir, self.app.pargs.site_name)
|
|
if os.path.isfile(vhconf):
|
|
if not WOFileUtils.grepcheck(
|
|
self, vhconf, 'logLevel DEBUG'):
|
|
Log.info(self, "Starting OpenLiteSpeed debug for "
|
|
"{0}".format(self.app.pargs.site_name))
|
|
WOFileUtils.searchreplace(
|
|
self, vhconf,
|
|
'logLevel NOTICE', 'logLevel DEBUG')
|
|
self.trigger_ols = True
|
|
else:
|
|
Log.info(self, "OpenLiteSpeed debug for site "
|
|
"already enabled")
|
|
|
|
self.msg = self.msg + ['{0}{1}/logs/error.log'
|
|
.format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name)]
|
|
else:
|
|
Log.info(self, "{0} domain not valid"
|
|
.format(self.app.pargs.site_name))
|
|
|
|
# stop site-specific debug
|
|
elif (self.app.pargs.nginx == 'off' and self.app.pargs.site_name):
|
|
vhconf = "{0}/{1}/vhconf.conf".format(
|
|
WOVar.wo_ols_vhost_dir, self.app.pargs.site_name)
|
|
if os.path.isfile(vhconf):
|
|
if WOFileUtils.grepcheck(
|
|
self, vhconf, 'logLevel DEBUG'):
|
|
Log.info(self, "Stopping OpenLiteSpeed debug for "
|
|
"{0}".format(self.app.pargs.site_name))
|
|
WOFileUtils.searchreplace(
|
|
self, vhconf,
|
|
'logLevel DEBUG', 'logLevel NOTICE')
|
|
self.trigger_ols = True
|
|
else:
|
|
Log.info(self, "OpenLiteSpeed debug for site "
|
|
"already disabled")
|
|
else:
|
|
Log.info(self, "{0} domain not valid"
|
|
.format(self.app.pargs.site_name))
|
|
|
|
@expose(hide=True)
|
|
def debug_mysql(self):
|
|
"""Start/Stop MySQL debug"""
|
|
# MySQL start global debug
|
|
if (self.app.pargs.mysql == 'on' and not self.app.pargs.site_name):
|
|
if not WOShellExec.cmd_exec(self, "mysql -e \"show variables like"
|
|
" \'slow_query_log\';\" | "
|
|
"grep ON"):
|
|
Log.info(self, "Setting up MySQL slow log")
|
|
WOMysql.execute(self, "set global slow_query_log = "
|
|
"\'ON\';")
|
|
WOMysql.execute(self, "set global slow_query_log_file = "
|
|
"\'/var/log/mysql/mysql-slow.log\';")
|
|
WOMysql.execute(self, "set global long_query_time = 2;")
|
|
WOMysql.execute(self, "set global log_queries_not_using"
|
|
"_indexes = \'ON\';")
|
|
else:
|
|
Log.info(self, "MySQL slow log is already enabled")
|
|
|
|
self.msg = self.msg + ['/var/log/mysql/mysql-slow.log']
|
|
|
|
# MySQL stop global debug
|
|
elif (self.app.pargs.mysql == 'off' and not self.app.pargs.site_name):
|
|
if WOShellExec.cmd_exec(self, "mysql -e \"show variables like \'"
|
|
"slow_query_log\';\" | grep ON"):
|
|
Log.info(self, "Disabling MySQL slow log")
|
|
WOMysql.execute(self, "set global slow_query_log = \'OFF\';")
|
|
WOMysql.execute(self, "set global slow_query_log_file = \'"
|
|
"/var/log/mysql/mysql-slow.log\';")
|
|
WOMysql.execute(self, "set global long_query_time = 10;")
|
|
WOMysql.execute(self, "set global log_queries_not_using_index"
|
|
"es = \'OFF\';")
|
|
WOShellExec.cmd_exec(self, "crontab -l | sed \'/#WordOps "
|
|
"start/,/#WordOps end/d\' | crontab -")
|
|
else:
|
|
Log.info(self, "MySQL slow log already disabled")
|
|
|
|
@expose(hide=True)
|
|
def debug_wp(self):
|
|
"""Start/Stop WordPress debug"""
|
|
if (self.app.pargs.wp == 'on' and self.app.pargs.site_name):
|
|
wp_config = ("{0}/{1}/wp-config.php"
|
|
.format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name))
|
|
webroot = "{0}{1}".format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name)
|
|
# Check wp-config.php file into htdocs folder
|
|
if not os.path.isfile(wp_config):
|
|
wp_config = ("{0}/{1}/htdocs/wp-config.php"
|
|
.format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name))
|
|
if os.path.isfile(wp_config):
|
|
if not WOShellExec.cmd_exec(self, "grep \"\'WP_DEBUG\'\" {0} |"
|
|
" grep true".format(wp_config)):
|
|
Log.info(self, "Starting WordPress debug")
|
|
open("{0}/htdocs/wp-content/debug.log".format(webroot),
|
|
encoding='utf-8', mode='a').close()
|
|
WOShellExec.cmd_exec(self, "chown {1}: {0}/htdocs/wp-"
|
|
"content/debug.log"
|
|
"".format(webroot,
|
|
WOVar.wo_php_user))
|
|
WOShellExec.cmd_exec(self, "sed -i \"s/define(\'WP_DEBUG\'"
|
|
".*/define(\'WP_DEBUG\', true);\\n"
|
|
"define(\'WP_DEBUG_DISPLAY\', false);"
|
|
"\\ndefine(\'WP_DEBUG_LOG\', true);"
|
|
"\\ndefine(\'SAVEQUERIES\', true);/\""
|
|
" {0}".format(wp_config))
|
|
WOShellExec.cmd_exec(self, "cd {0}/htdocs/ && wp"
|
|
" plugin --allow-root install "
|
|
"developer query-monitor"
|
|
.format(webroot))
|
|
WOShellExec.cmd_exec(self, "chown -R {1}: {0}/htdocs/"
|
|
"wp-content/plugins"
|
|
.format(webroot,
|
|
WOVar.wo_php_user))
|
|
|
|
self.msg = self.msg + ['{0}{1}/htdocs/wp-content'
|
|
'/debug.log'
|
|
.format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name)]
|
|
|
|
else:
|
|
Log.info(self, "Unable to find wp-config.php for site: {0}"
|
|
.format(self.app.pargs.site_name))
|
|
|
|
elif (self.app.pargs.wp == 'off' and self.app.pargs.site_name):
|
|
wp_config = ("{0}{1}/wp-config.php"
|
|
.format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name))
|
|
webroot = "{0}{1}".format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name)
|
|
# Check wp-config.php file into htdocs folder
|
|
if not os.path.isfile(wp_config):
|
|
wp_config = ("{0}/{1}/htdocs/wp-config.php"
|
|
.format(WOVar.wo_webroot,
|
|
self.app.pargs.site_name))
|
|
if os.path.isfile(wp_config):
|
|
if WOShellExec.cmd_exec(self, "grep \"\'WP_DEBUG\'\" {0} | "
|
|
"grep true".format(wp_config)):
|
|
Log.info(self, "Disabling WordPress debug")
|
|
WOShellExec.cmd_exec(self, "sed -i \"s/define(\'WP_DEBUG\'"
|
|
", true);/define(\'WP_DEBUG\', "
|
|
"false);/\" {0}".format(wp_config))
|
|
WOShellExec.cmd_exec(self, "sed -i \"/define(\'"
|
|
"WP_DEBUG_DISPLAY\', false);/d\" {0}"
|
|
.format(wp_config))
|
|
WOShellExec.cmd_exec(self, "sed -i \"/define(\'"
|
|
"WP_DEBUG_LOG\', true);/d\" {0}"
|
|
.format(wp_config))
|
|
WOShellExec.cmd_exec(self, "sed -i \"/define(\'"
|
|
"SAVEQUERIES\', "
|
|
"true);/d\" {0}".format(wp_config))
|
|
else:
|
|
Log.info(self, "WordPress debug all already disabled")
|
|
else:
|
|
Log.error(self, "Missing argument site name")
|
|
|
|
@expose(hide=True)
|
|
def signal_handler(self, app, signal, frame):
|
|
"""Handle Ctrl+c event for -i option of debug"""
|
|
self.start = False
|
|
if self.app.pargs.nginx:
|
|
self.app.pargs.nginx = 'off'
|
|
self.debug_ols()
|
|
if self.app.pargs.mysql:
|
|
# MySQL debug will not work for remote MySQL
|
|
if WOVar.wo_mysql_host == "localhost":
|
|
self.app.pargs.mysql = 'off'
|
|
self.debug_mysql()
|
|
else:
|
|
Log.warn(self, "Remote MySQL found, WordOps does not support "
|
|
"debugging remote servers")
|
|
if self.app.pargs.wp:
|
|
self.app.pargs.wp = 'off'
|
|
self.debug_wp()
|
|
|
|
# Reload OpenLiteSpeed
|
|
if self.trigger_ols:
|
|
WOService.reload_service(self, 'lsws')
|
|
|
|
self.app.close(0)
|
|
|
|
@expose(hide=True)
|
|
def default(self):
|
|
"""Default function of debug"""
|
|
# self.start = True
|
|
self.interactive = False
|
|
self.msg = []
|
|
self.trigger_ols = False
|
|
|
|
if ((not self.app.pargs.nginx) and (not self.app.pargs.mysql) and
|
|
(not self.app.pargs.wp) and
|
|
(not self.app.pargs.all) and (not self.app.pargs.site_name) and
|
|
(not self.app.pargs.import_slow_log) and
|
|
(not self.app.pargs.interval)):
|
|
if self.app.pargs.stop or self.app.pargs.start:
|
|
print("--start/stop option is deprecated since wo v3.0.5")
|
|
self.app.args.print_help()
|
|
else:
|
|
self.app.args.print_help()
|
|
|
|
if self.app.pargs.import_slow_log:
|
|
self.import_slow_log()
|
|
|
|
if self.app.pargs.interval:
|
|
try:
|
|
cron_time = int(self.app.pargs.interval)
|
|
except Exception as e:
|
|
Log.debug(self, "{0}".format(e))
|
|
cron_time = 5
|
|
|
|
try:
|
|
if not WOShellExec.cmd_exec(self, "crontab -l | grep "
|
|
"'wo debug --import-slow-log'"):
|
|
if not cron_time == 0:
|
|
Log.info(self, "setting up crontab entry,"
|
|
" please wait...")
|
|
WOShellExec.cmd_exec(self, "/bin/bash -c \"crontab -l "
|
|
"2> /dev/null | {{ cat; echo -e"
|
|
" \\\"#WordOps start MySQL "
|
|
"slow log \\n*/{0} * * * * "
|
|
"/usr/local/bin/wo debug"
|
|
" --import-slow-log\\n"
|
|
"#WordOps end MySQL slow log"
|
|
"\\\"; }} | crontab -\""
|
|
.format(cron_time))
|
|
else:
|
|
if not cron_time == 0:
|
|
Log.info(self, "updating crontab entry,"
|
|
" please wait...")
|
|
if not WOShellExec.cmd_exec(self, "/bin/bash -c "
|
|
"\"crontab "
|
|
"-l | sed '/WordOps "
|
|
"start MySQL slow "
|
|
"log/!b;n;c\*\/{0} "
|
|
"\* \* \* "
|
|
"\* \/usr"
|
|
"\/local\/bin\/wo debug "
|
|
"--import\-slow\-log' "
|
|
"| crontab -\""
|
|
.format(cron_time)):
|
|
Log.error(self, "failed to update crontab entry")
|
|
else:
|
|
Log.info(self, "removing crontab entry,"
|
|
" please wait...")
|
|
if not WOShellExec.cmd_exec(self, "/bin/bash -c "
|
|
"\"crontab "
|
|
"-l | sed '/WordOps "
|
|
"start MySQL slow "
|
|
"log/,+2d'"
|
|
"| crontab -\""):
|
|
Log.error(self, "failed to remove crontab entry")
|
|
except CommandExecutionError as e:
|
|
Log.debug(self, str(e))
|
|
|
|
if self.app.pargs.all == 'on':
|
|
if self.app.pargs.site_name:
|
|
self.app.pargs.wp = 'on'
|
|
self.app.pargs.nginx = 'on'
|
|
self.app.pargs.mysql = 'on'
|
|
|
|
if self.app.pargs.all == 'off':
|
|
if self.app.pargs.site_name:
|
|
self.app.pargs.wp = 'off'
|
|
self.app.pargs.nginx = 'off'
|
|
self.app.pargs.mysql = 'off'
|
|
|
|
if ((not self.app.pargs.nginx) and (not self.app.pargs.mysql) and
|
|
(not self.app.pargs.wp) and
|
|
self.app.pargs.site_name):
|
|
self.app.args.print_help()
|
|
|
|
if self.app.pargs.nginx:
|
|
self.debug_ols()
|
|
if self.app.pargs.mysql:
|
|
# MySQL debug will not work for remote MySQL
|
|
if WOVar.wo_mysql_host == "localhost":
|
|
self.debug_mysql()
|
|
else:
|
|
Log.warn(self, "Remote MySQL found, WordOps does not support "
|
|
"debugging remote servers")
|
|
if self.app.pargs.wp:
|
|
self.debug_wp()
|
|
|
|
if self.app.pargs.interactive:
|
|
self.interactive = True
|
|
|
|
# Reload OpenLiteSpeed
|
|
if self.trigger_ols:
|
|
WOService.reload_service(self, 'lsws')
|
|
|
|
if len(self.msg) > 0:
|
|
if not self.app.pargs.interactive:
|
|
disp_msg = ' '.join(self.msg)
|
|
Log.info(self, "Use following command to check debug logs:\n" +
|
|
Log.ENDC + "tail -f {0}".format(disp_msg))
|
|
else:
|
|
signal.signal(signal.SIGINT, self.signal_handler)
|
|
watch_list = []
|
|
for w_list in self.msg:
|
|
watch_list = watch_list + glob.glob(w_list)
|
|
|
|
logwatch(self, watch_list)
|
|
|
|
@expose(hide=True)
|
|
def import_slow_log(self):
|
|
"""Default function for import slow log"""
|
|
if os.path.isdir("{0}22222/htdocs/db/anemometer"
|
|
.format(WOVar.wo_webroot)):
|
|
if os.path.isfile("/var/log/mysql/mysql-slow.log"):
|
|
# Get Anemometer user name and password
|
|
Log.info(self, "Importing MySQL slow log to Anemometer")
|
|
host = os.popen("grep -e \"\'host\'\" {0}22222/htdocs/"
|
|
.format(WOVar.wo_webroot) +
|
|
"db/anemometer/conf/config.inc.php "
|
|
"| head -1 | cut -d\\\' -f4 | "
|
|
"tr -d '\n'").read()
|
|
user = os.popen("grep -e \"\'user\'\" {0}22222/htdocs/"
|
|
.format(WOVar.wo_webroot) +
|
|
"db/anemometer/conf/config.inc.php "
|
|
"| head -1 | cut -d\\\' -f4 | "
|
|
"tr -d '\n'").read()
|
|
password = os.popen("grep -e \"\'password\'\" {0}22222/"
|
|
.format(WOVar.wo_webroot) +
|
|
"htdocs/db/anemometer/conf"
|
|
"/config.inc.php "
|
|
"| head -1 | cut -d\\\' -f4 | "
|
|
"tr -d '\n'").read()
|
|
|
|
# Import slow log Anemometer using pt-query-digest
|
|
try:
|
|
WOShellExec.cmd_exec(self, "pt-query-digest --user={0} "
|
|
"--password={1} "
|
|
"--review D=slow_query_log,"
|
|
"t=global_query_review "
|
|
"--history D=slow_query_log,t="
|
|
"global_query_review_history "
|
|
"--no-report --limit=0% "
|
|
"--filter=\" \\$event->{{Bytes}} = "
|
|
"length(\\$event->{{arg}}) "
|
|
"and \\$event->{{hostname}}=\\\""
|
|
"{2}\\\"\" "
|
|
"/var/log/mysql/mysql-slow.log"
|
|
.format(user, password, host))
|
|
except CommandExecutionError as e:
|
|
Log.debug(self, str(e))
|
|
Log.error(self, "MySQL slow log import failed.")
|
|
else:
|
|
Log.error(self, "MySQL slow log file not found,"
|
|
" so not imported slow logs")
|
|
else:
|
|
Log.error(self, "Anemometer is not installed." +
|
|
Log.ENDC + "\n Install Anemometer with:" +
|
|
Log.BOLD + "\n `wo stack install --utils`" +
|
|
Log.ENDC)
|
|
|
|
|
|
def load(app):
|
|
# register the plugin class.. this only happens if the plugin is enabled
|
|
app.handler.register(WODebugController)
|
|
# register a hook (function) to run after arguments are parsed.
|
|
app.hook.register('post_argument_parsing', wo_debug_hook)
|