From dbb7dfc956bc9b5b39e367b8b880cbe6e5ae41ca Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Thu, 25 Jul 2019 08:24:02 +0200 Subject: [PATCH] Improve debug log & maintenance * refactored command `wo maintenance` --- .travis.yml | 3 +- install | 41 ++++++++------- wo/cli/plugins/maintenance.py | 16 ++---- wo/cli/plugins/site.py | 2 +- wo/cli/plugins/site_functions.py | 14 +++-- wo/cli/plugins/stack.py | 90 +++++++++++++++++++------------- 6 files changed, 90 insertions(+), 76 deletions(-) diff --git a/.travis.yml b/.travis.yml index 341ef4d..b46774b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,4 +52,5 @@ script: - sudo wo stack upgrade --phpmyadmin --no-prompt - sudo wo stack upgrade --composer --no-prompt - sudo wo update --travis - - sudo wo stack status \ No newline at end of file + - sudo wo stack status + - sudo wo maintenance \ No newline at end of file diff --git a/install b/install index 4d0a4e5..c5d7473 100755 --- a/install +++ b/install @@ -413,7 +413,7 @@ wo_install() { >> "$wo_install_log" 2>&1 if [ "$wo_force_install" = "y" ]; then - [ ! -f "$HOME/.gitconfig" ] && { bash -c 'echo -e "[user]\n\tname = $USER\n\temail = root@$HOSTNAME.local" > $HOME/.gitconfig'; } + [ ! -f "$HOME/.gitconfig" ] && { bash -c 'echo -e "[user]\n\tname = $USER\n\temail = root@$HOSTNAME.local" > $HOME/.gitconfig'; } fi if [ -f "$HOME/.gitconfig" ]; then @@ -449,7 +449,7 @@ wo_upgrade_nginx() { # add new Nginx repository if [ "$wo_linux_distro" = "Ubuntu" ]; then if [ ! -f /etc/apt/sources.list.d/wordops-ubuntu-nginx-wo-"$(lsb_release -sc)".list ]; then - add-apt-repository ppa:wordops/nginx-wo -y -u + add-apt-repository ppa:wordops/nginx-wo -y -u fi else if [ "$wo_distro_version" == "jessie" ]; then @@ -469,28 +469,29 @@ wo_upgrade_nginx() { } # install new nginx package - if [ -x /usr/local/bin/wo ]; then + if [ -n "$CHECK_NGINX_EE" ] || [ -n "$CHECK_NGINX_WO" ]; then + if [ -x /usr/local/bin/wo ]; then + if [ -n "$CHECK_NGINX_EE" ]; then + # remove previous package + apt-mark unhold nginx-ee nginx-common nginx-custom + apt-get --assume-yes purge nginx-ee nginx-common nginx-custom --allow-change-held-packages + # remove previous php-fpm pool configuration + if [ -n "$CHECK_PHP72" ]; then + apt-get purge php7.2-fpm -y -qq + rm -f /etc/php/7.2/fpm/pool.d/{www.conf,www-two.conf,debug.conf} + fi + elif [ -n "$CHECK_NGINX_WO" ]; then + apt-mark unhold nginx-wo nginx-common nginx-custom + apt-get --assume-yes purge nginx-wo nginx-common nginx-custom --allow-change-held-packages - if [ -n "$CHECK_NGINX_EE" ]; then - # remove previous package - apt-mark unhold nginx-ee nginx-common nginx-custom - apt-get -y purge nginx-ee nginx-common nginx-custom --allow-change-held-packages - # remove previous php-fpm pool configuration - if [ -n "$CHECK_PHP72" ]; then - apt-get purge php7.2-fpm -y -qq - rm -f /etc/php/7.2/fpm/pool.d/{www.conf,www-two.conf,debug.conf} fi - elif [ -n "$CHECK_NGINX_WO" ]; then - apt-mark unhold nginx-wo nginx-common nginx-custom - apt-get -y purge nginx-wo nginx-common nginx-custom --allow-change-held-packages + if [ -d /etc/nginx ]; then + rm -rf /etc/nginx + fi + /usr/local/bin/wo stack install --nginx --php + rm -f /etc/nginx/common/acl.conf /etc/nginx/conf.d/{map-wp-cache.conf,map-wp.conf,fascgi.conf,blockips.conf} /etc/nginx/htpasswd-wo fi - if [ -d /etc/nginx ]; then - rm -rf /etc/nginx - fi - - /usr/local/bin/wo stack install --nginx --php - rm -f /etc/nginx/common/acl.conf /etc/nginx/conf.d/{map-wp-cache.conf,map-wp.conf,fascgi.conf,blockips.conf} /etc/nginx/htpasswd-wo fi # restore sites and configuration diff --git a/wo/cli/plugins/maintenance.py b/wo/cli/plugins/maintenance.py index c787ef2..06a9071 100644 --- a/wo/cli/plugins/maintenance.py +++ b/wo/cli/plugins/maintenance.py @@ -3,11 +3,7 @@ from cement.core.controller import CementBaseController, expose from cement.core import handler, hook from wo.core.logging import Log -from wo.core.variables import WOVariables from wo.core.aptget import WOAptGet -from wo.core.apt_repo import WORepo -from wo.core.services import WOService -from wo.core.shellexec import WOShellExec def wo_maintenance_hook(app): @@ -27,16 +23,12 @@ class WOMaintenanceController(CementBaseController): try: Log.info(self, "updating apt-cache, please wait...") - WOShellExec.cmd_exec(self, "apt-get update") + WOAptGet.update(self) Log.info(self, "updating packages, please wait...") - WOShellExec.cmd_exec(self, "DEBIAN_FRONTEND=noninteractive " - "apt-get -o " - "Dpkg::Options::='--force-confmiss' " - "-o Dpkg::Options::='--force-confold' " - "-y dist-upgrade") + WOAptGet.dist_upgrade(self) Log.info(self, "cleaning-up packages, please wait...") - WOShellExec.cmd_exec(self, "apt-get -y --purge autoremove") - WOShellExec.cmd_exec(self, "apt-get -y autoclean") + WOAptGet.auto_remove(self) + WOAptGet.auto_clean(self) except OSError as e: Log.debug(self, str(e)) Log.error(self, "Package updates failed !") diff --git a/wo/cli/plugins/site.py b/wo/cli/plugins/site.py index d0fc061..3054133 100644 --- a/wo/cli/plugins/site.py +++ b/wo/cli/plugins/site.py @@ -619,7 +619,7 @@ class WOSiteCreateController(CementBaseController): if (data['wp'] and (self.app.pargs.vhostonly)): try: - data = setupdatabase(self, data) + wo_wp_creds = setupwordpress(self, data) # Add database information for site into database updateSiteInfo(self, wo_domain, db_name=data['wo_db_name'], db_user=data['wo_db_user'], diff --git a/wo/cli/plugins/site_functions.py b/wo/cli/plugins/site_functions.py index 597c197..564209a 100644 --- a/wo/cli/plugins/site_functions.py +++ b/wo/cli/plugins/site_functions.py @@ -1,7 +1,7 @@ from wo.cli.plugins.stack import WOStackController from wo.core.fileutils import WOFileUtils from wo.core.mysql import * -from wo.core.shellexec import * +from wo.core.shellexec import WOShellExec, CommandExecutionError from wo.core.sslutils import SSL from wo.core.variables import WOVariables from wo.cli.plugins.sitedb import * @@ -670,6 +670,7 @@ def installwp_plugin(self, plugin_name, data): else '' )) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("plugin activation failed") return 1 @@ -693,6 +694,7 @@ def uninstallwp_plugin(self, plugin_name, data): "--allow-root uninstall " "{0}".format(plugin_name)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("plugin uninstall failed") @@ -710,6 +712,7 @@ def setupwp_plugin(self, plugin_name, plugin_option, plugin_data, data): "{0} \'{1}\' --format=json" .format(plugin_option, plugin_data)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("plugin setup failed") else: try: @@ -720,6 +723,7 @@ def setupwp_plugin(self, plugin_name, plugin_option, plugin_data, data): .format(plugin_option, plugin_data )) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("plugin setup failed") @@ -1001,6 +1005,7 @@ def updatewpuserpassword(self, wo_domain, wo_site_webroot): is_wp = WOShellExec.cmd_exec(self, "wp --allow-root core" " version") except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("is WordPress site? check command failed ") # Exit if wo_domain is not wordpress install @@ -1020,6 +1025,7 @@ def updatewpuserpassword(self, wo_domain, wo_site_webroot): WOShellExec.cmd_exec(self, "wp --allow-root user list " "--fields=user_login | grep -v user_login") except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("fetch wp userlist command failed") if not wo_wp_user: @@ -1030,6 +1036,7 @@ def updatewpuserpassword(self, wo_domain, wo_site_webroot): "--fields=user_login | grep {0}$ " .format(wo_wp_user)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("if wp user exists check command failed") if is_user_exist: @@ -1308,6 +1315,7 @@ def removeAcmeConf(self, domain): "-d {0} --ecc" .format(domain)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error(self, "Cert removal failed") WOFileUtils.rm(self, '/etc/letsencrypt/renewal/{0}_ecc' @@ -1651,8 +1659,6 @@ def archivedCertificateHandle(self, domain): .format(WOVariables.wo_ssl_live, domain)) sslconf.close() - updateSiteInfo(self, domain, ssl=True) - except IOError as e: Log.debug(self, str(e)) Log.debug(self, "Error occured while generating " @@ -1667,8 +1673,6 @@ def archivedCertificateHandle(self, domain): "\n\t/etc/letsencrypt/live/{0}/fullchain.pem\n\t" "/etc/letsencrypt/live/{0}/key.pem".format(domain)) - updateSiteInfo(self, domain, ssl=True) - elif (check_prompt == "3"): Log.info(self, "Issuing SSL cert with acme.sh") ssl = WOShellExec.cmd_exec(self, "/etc/letsencrypt/acme.sh " diff --git a/wo/cli/plugins/stack.py b/wo/cli/plugins/stack.py index bbd036a..c7134f7 100644 --- a/wo/cli/plugins/stack.py +++ b/wo/cli/plugins/stack.py @@ -1,36 +1,39 @@ """Stack Plugin for WordOps""" -from cement.core.controller import CementBaseController, expose from cement.core import handler, hook -from wo.cli.plugins.site_functions import * -from wo.core.variables import WOVariables -from wo.core.aptget import WOAptGet -from wo.core.download import WODownload -from wo.core.shellexec import WOShellExec, CommandExecutionError -from wo.core.fileutils import WOFileUtils -from wo.core.apt_repo import WORepo -from wo.core.extract import WOExtract -from wo.core.mysql import WOMysql -from wo.core.addswap import WOSwap -from wo.core.git import WOGit -from wo.core.checkfqdn import check_fqdn -from pynginxconfig import NginxConfig -from wo.core.services import WOService -import random -import string -import configparser -import shutil -import os -import pwd -import grp +from cement.core.controller import CementBaseController, expose + import codecs +import configparser +import grp +import os import platform +import pwd +import random +import shutil +import string + import psutil -from wo.cli.plugins.stack_services import WOStackStatusController -from wo.cli.plugins.stack_migrate import WOStackMigrateController -from wo.cli.plugins.stack_upgrade import WOStackUpgradeController -from wo.core.logging import Log +# from pynginxconfig import NginxConfig +from wo.cli.plugins.site_functions import * from wo.cli.plugins.sitedb import * +from wo.cli.plugins.stack_migrate import WOStackMigrateController +from wo.cli.plugins.stack_services import WOStackStatusController +from wo.cli.plugins.stack_upgrade import WOStackUpgradeController +from wo.core.addswap import WOSwap +from wo.core.apt_repo import WORepo +from wo.core.aptget import WOAptGet +from wo.core.cron import WOCron +from wo.core.checkfqdn import check_fqdn +from wo.core.download import WODownload +from wo.core.extract import WOExtract +from wo.core.fileutils import WOFileUtils +from wo.core.git import WOGit +from wo.core.logging import Log +from wo.core.mysql import WOMysql +from wo.core.services import WOService +from wo.core.shellexec import CommandExecutionError, WOShellExec +from wo.core.variables import WOVariables def wo_stack_hook(app): @@ -129,6 +132,7 @@ class WOStackController(CementBaseController): .format(chars=chars), log=False) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error("Failed to initialize MySQL package") Log.debug(self, "echo \"mariadb-server-10.3 " @@ -143,6 +147,7 @@ class WOStackController(CementBaseController): .format(chars=chars), log=False) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error("Failed to initialize MySQL package") else: Log.debug(self, "Pre-seeding MySQL") @@ -158,6 +163,7 @@ class WOStackController(CementBaseController): .format(chars=chars), log=False) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error("Failed to initialize MySQL package") Log.debug(self, "echo \"mariadb-server-10.1 " @@ -172,6 +178,7 @@ class WOStackController(CementBaseController): .format(chars=chars), log=False) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error("Failed to initialize MySQL package") # generate my.cnf root credentials mysql_config = """ @@ -473,6 +480,7 @@ class WOStackController(CementBaseController): "2>/dev/null" .format(password=passwd)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error(self, "Failed to save HTTP Auth") # Create Symbolic link for 22222 @@ -552,6 +560,7 @@ class WOStackController(CementBaseController): .format(WOVariables.wo_webroot)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error( self, "Failed to generate HTTPS " "certificate for 22222") @@ -1076,10 +1085,14 @@ class WOStackController(CementBaseController): "query_cache_type = 1 \" " "/etc/mysql/my.cnf") except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to update MySQL file") WOFileUtils.chmod(self, "/usr/bin/mysqltuner", 0o775) - + WOCron.setcron_weekly(self, 'mysqlcheck -Aos --auto-repair ' + '> /dev/null 2>&1', + comment='MySQL optimization cronjob ' + 'added by WordOps') WOGit.add(self, ["/etc/mysql"], msg="Adding MySQL into Git") WOService.reload_service(self, 'mysql') @@ -1138,6 +1151,7 @@ class WOStackController(CementBaseController): WOShellExec.cmd_exec(self, "ufw allow " "49000:50000/tcp") except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to add UFW rule") if os.path.isfile("/etc/fail2ban/jail.d/custom.conf"): @@ -1265,6 +1279,7 @@ class WOStackController(CementBaseController): "flush privileges;", log=False) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) Log.info( self, "fail to setup mysql user for netdata") WOService.restart_service(self, 'netdata') @@ -1386,6 +1401,7 @@ class WOStackController(CementBaseController): '/anemometer/install.sql' .format(WOVariables.wo_webroot)) except CommandExecutionError as e: + Log.debug(self, "{0}".format(e)) raise SiteError("Unable to import Anemometer database") WOMysql.execute(self, 'grant select on' @@ -1657,16 +1673,16 @@ class WOStackController(CementBaseController): Log.debug(self, "Setting packages variable for Adminer ") packages = packages + [["https://github.com/vrana/adminer/" "releases/download/v{0}" - "/adminer-{0}.php" - .format(WOVariables.wo_adminer), - "{0}22222/" - "htdocs/db/adminer/index.php" - .format(WOVariables.wo_webroot), - "Adminer"], - ["https://raw.githubusercontent.com" - "/vrana/adminer/master/designs/" - "pepa-linha/adminer.css", - "{0}22222/" + "/adminer-{0}.php" + .format(WOVariables.wo_adminer), + "{0}22222/" + "htdocs/db/adminer/index.php" + .format(WOVariables.wo_webroot), + "Adminer"], + ["https://raw.githubusercontent.com" + "/vrana/adminer/master/designs/" + "pepa-linha/adminer.css", + "{0}22222/" "htdocs/db/adminer/adminer.css" .format(WOVariables.wo_webroot), "Adminer theme"]]