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

445 lines
19 KiB
Python
Raw Normal View History

import os
import shutil
2019-09-02 04:37:13 +02:00
from cement.core.controller import CementBaseController, expose
2019-09-04 20:36:15 +02:00
from wo.cli.plugins.stack_pref import post_pref, pre_pref, pre_stack
from wo.core.aptget import WOAptGet
from wo.core.download import WODownload
from wo.core.extract import WOExtract
2018-11-13 21:55:59 +01:00
from wo.core.fileutils import WOFileUtils
from wo.core.logging import Log
2018-11-13 21:55:59 +01:00
from wo.core.shellexec import WOShellExec
2019-10-02 13:13:32 +02:00
from wo.core.variables import WOVar
2020-01-14 16:26:25 +01:00
from wo.core.services import WOService
2018-11-13 21:55:59 +01:00
class WOStackUpgradeController(CementBaseController):
class Meta:
label = 'upgrade'
stacked_on = 'stack'
stacked_type = 'nested'
description = ('Upgrade stack safely')
arguments = [
(['--all'],
dict(help='Upgrade all stack', action='store_true')),
(['--web'],
dict(help='Upgrade web stack', action='store_true')),
(['--admin'],
dict(help='Upgrade admin tools stack', action='store_true')),
(['--security'],
dict(help='Upgrade security stack', action='store_true')),
2018-11-13 21:55:59 +01:00
(['--nginx'],
dict(help='Upgrade Nginx stack', action='store_true')),
(['--php'],
2019-08-05 12:38:50 +02:00
dict(help='Upgrade PHP 7.2 stack', action='store_true')),
(['--php72'],
dict(help='Upgrade PHP 7.2 stack', action='store_true')),
2019-08-05 12:38:50 +02:00
(['--php73'],
dict(help='Upgrade PHP 7.3 stack', action='store_true')),
(['--php74'],
dict(help='Upgrade PHP 7.4 stack', action='store_true')),
2022-01-25 17:15:15 +01:00
(['--php80'],
2022-01-23 15:14:24 -03:00
dict(help='Upgrade PHP 8.0 stack', action='store_true')),
2022-01-25 17:15:15 +01:00
(['--php81'],
2022-01-23 15:14:24 -03:00
dict(help='Upgrade PHP 8.1 stack', action='store_true')),
2018-11-13 21:55:59 +01:00
(['--mysql'],
dict(help='Upgrade MySQL stack', action='store_true')),
2020-10-28 12:25:09 +01:00
(['--mariadb'],
dict(help='Upgrade MySQL stack alias',
action='store_true')),
2018-11-13 21:55:59 +01:00
(['--wpcli'],
dict(help='Upgrade WPCLI', action='store_true')),
(['--redis'],
dict(help='Upgrade Redis', action='store_true')),
2019-07-13 15:15:52 +02:00
(['--netdata'],
dict(help='Upgrade Netdata', action='store_true')),
(['--fail2ban'],
dict(help='Upgrade Fail2Ban', action='store_true')),
2019-08-16 14:53:39 +02:00
(['--dashboard'],
dict(help='Upgrade WordOps Dashboard', action='store_true')),
2019-07-17 03:08:03 +02:00
(['--composer'],
dict(help='Upgrade Composer', action='store_true')),
(['--mysqltuner'],
dict(help='Upgrade Composer', action='store_true')),
2019-07-14 22:50:34 +02:00
(['--phpmyadmin'],
dict(help='Upgrade phpMyAdmin', action='store_true')),
(['--adminer'],
dict(help='Upgrade Adminer', action='store_true')),
2019-10-30 12:06:42 +01:00
(['--ngxblocker'],
dict(help='Upgrade phpMyAdmin', action='store_true')),
2018-11-13 21:55:59 +01:00
(['--no-prompt'],
dict(help="Upgrade Packages without any prompt",
action='store_true')),
2019-08-16 00:15:34 +02:00
(['--force'],
dict(help="Force Packages upgrade without any prompt",
action='store_true')),
2019-03-19 16:58:35 +01:00
]
2018-11-13 21:55:59 +01:00
@expose(hide=True)
2019-08-31 00:59:11 +02:00
def default(self, disp_msg=False):
2018-11-13 21:55:59 +01:00
# All package update
2018-11-30 17:04:15 +01:00
apt_packages = []
packages = []
2019-08-31 00:59:11 +02:00
self.msg = []
pargs = self.app.pargs
wo_phpmyadmin = WODownload.pma_release(self)
if not (pargs.web or pargs.nginx or pargs.php or
2022-01-23 15:14:24 -03:00
pargs.php72 or pargs.php73 or pargs.php74 or
pargs.php80 or pargs.php81 or pargs.mysql or
2022-01-25 17:15:15 +01:00
pargs.mariadb or pargs.ngxblocker or pargs.all or
pargs.netdata or pargs.wpcli or pargs.composer or
2020-10-28 12:25:09 +01:00
pargs.phpmyadmin or pargs.adminer or pargs.dashboard or
pargs.mysqltuner or pargs.redis or
pargs.fail2ban or pargs.security):
2019-08-07 02:45:26 +02:00
pargs.web = True
2019-10-30 12:06:42 +01:00
pargs.admin = True
pargs.security = True
2018-11-30 17:04:15 +01:00
2020-10-28 12:25:09 +01:00
if pargs.mariadb:
pargs.mysql = True
if pargs.php:
pargs.php72 = True
2019-08-07 02:45:26 +02:00
if pargs.all:
pargs.web = True
2019-10-30 12:06:42 +01:00
pargs.admin = True
pargs.security = True
2019-08-29 16:19:27 +02:00
pargs.redis = True
2018-11-30 17:04:15 +01:00
2019-08-07 02:45:26 +02:00
if pargs.web:
2019-10-30 12:06:42 +01:00
pargs.nginx = True
pargs.php72 = True
pargs.php73 = True
pargs.php74 = True
2022-01-23 15:14:24 -03:00
pargs.php80 = True
pargs.php81 = True
2019-08-07 02:45:26 +02:00
pargs.mysql = True
pargs.wpcli = True
2018-11-30 17:04:15 +01:00
2019-10-30 12:06:42 +01:00
if pargs.admin:
pargs.netdata = True
pargs.composer = True
pargs.dashboard = True
pargs.phpmyadmin = True
pargs.wpcli = True
pargs.adminer = True
pargs.mysqltuner = True
2019-10-30 12:06:42 +01:00
if pargs.security:
pargs.ngxblocker = True
pargs.fail2ban = True
2019-10-30 12:06:42 +01:00
# nginx
2019-08-07 02:45:26 +02:00
if pargs.nginx:
2018-11-30 17:04:15 +01:00
if WOAptGet.is_installed(self, 'nginx-custom'):
2019-10-02 13:13:32 +02:00
apt_packages = apt_packages + WOVar.wo_nginx
2018-11-30 17:04:15 +01:00
else:
2019-10-08 02:19:22 +02:00
if os.path.isfile('/usr/sbin/nginx'):
2019-09-27 14:15:00 +02:00
Log.info(self, "Updating Nginx templates")
2019-10-02 13:13:32 +02:00
post_pref(self, WOVar.wo_nginx, [])
2019-09-27 14:15:00 +02:00
else:
Log.info(self, "Nginx Stable is not already installed")
2018-11-30 17:04:15 +01:00
2019-10-30 12:06:42 +01:00
# php 7.2
if pargs.php72:
2018-11-30 17:04:15 +01:00
if WOAptGet.is_installed(self, 'php7.2-fpm'):
apt_packages = apt_packages + WOVar.wo_php72 + \
WOVar.wo_php_extra
2018-11-30 17:04:15 +01:00
2019-10-30 12:06:42 +01:00
# php 7.3
2019-08-07 02:45:26 +02:00
if pargs.php73:
2019-08-05 12:38:50 +02:00
if WOAptGet.is_installed(self, 'php7.3-fpm'):
apt_packages = apt_packages + WOVar.wo_php73 + \
WOVar.wo_php_extra
# php 7.4
if pargs.php74:
if WOAptGet.is_installed(self, 'php7.4-fpm'):
apt_packages = apt_packages + WOVar.wo_php74 + \
WOVar.wo_php_extra
2019-08-05 12:38:50 +02:00
2022-01-23 15:14:24 -03:00
# php 8.0
if pargs.php80:
if WOAptGet.is_installed(self, 'php8.0-fpm'):
apt_packages = apt_packages + WOVar.wo_php80 + \
WOVar.wo_php_extra
# php 8.1
if pargs.php81:
if WOAptGet.is_installed(self, 'php8.1-fpm'):
apt_packages = apt_packages + WOVar.wo_php81 + \
WOVar.wo_php_extra
2019-10-30 12:06:42 +01:00
# mysql
2019-08-07 02:45:26 +02:00
if pargs.mysql:
if WOShellExec.cmd_exec(self, 'mysqladmin ping'):
2019-09-04 16:55:58 +02:00
apt_packages = apt_packages + ['mariadb-server']
2018-11-30 17:04:15 +01:00
2019-10-30 12:06:42 +01:00
# redis
2019-08-07 02:45:26 +02:00
if pargs.redis:
2018-11-30 17:04:15 +01:00
if WOAptGet.is_installed(self, 'redis-server'):
2019-09-04 16:55:58 +02:00
apt_packages = apt_packages + ['redis-server']
2018-11-30 17:04:15 +01:00
# fail2ban
if pargs.fail2ban:
if WOAptGet.is_installed(self, 'fail2ban'):
apt_packages = apt_packages + ['fail2ban']
2019-10-30 12:06:42 +01:00
# wp-cli
2019-08-07 02:45:26 +02:00
if pargs.wpcli:
2019-03-04 07:12:57 +01:00
if os.path.isfile('/usr/local/bin/wp'):
2019-10-28 09:09:28 +01:00
packages = packages + [[
"https://github.com/wp-cli/wp-cli/"
"releases/download/v{0}/"
"wp-cli-{0}.phar".format(WOVar.wo_wp_cli),
"/usr/local/bin/wp",
"WP-CLI"]]
2018-11-30 17:04:15 +01:00
else:
Log.info(self, "WPCLI is not installed with WordOps")
2019-10-30 12:06:42 +01:00
# netdata
2019-08-07 02:45:26 +02:00
if pargs.netdata:
2019-10-30 12:06:42 +01:00
# detect static binaries install
if os.path.isdir('/opt/netdata'):
packages = packages + [[
2022-07-21 16:42:42 +02:00
'https://my-netdata.io/kickstart.sh',
2019-10-30 12:06:42 +01:00
'/var/lib/wo/tmp/kickstart.sh', 'Netdata']]
# detect install from source
elif os.path.isdir('/etc/netdata'):
packages = packages + [[
'https://my-netdata.io/kickstart.sh',
'/var/lib/wo/tmp/kickstart.sh', 'Netdata']]
else:
2019-11-18 13:30:43 +01:00
Log.info(self, 'Netdata is not installed')
2019-08-16 14:53:39 +02:00
2019-10-30 12:06:42 +01:00
# wordops dashboard
2019-08-16 14:53:39 +02:00
if pargs.dashboard:
2019-09-22 14:11:12 +02:00
if (os.path.isfile('/var/www/22222/htdocs/index.php') or
os.path.isfile('/var/www/22222/htdocs/index.html')):
2019-10-30 12:06:42 +01:00
packages = packages + [[
"https://github.com/WordOps/wordops-dashboard/"
"releases/download/v{0}/wordops-dashboard.tar.gz"
.format(WOVar.wo_dashboard),
"/var/lib/wo/tmp/wo-dashboard.tar.gz",
"WordOps Dashboard"]]
else:
Log.info(self, 'WordOps dashboard is not installed')
2019-08-16 14:53:39 +02:00
2019-10-30 12:06:42 +01:00
# phpmyadmin
2019-08-07 02:45:26 +02:00
if pargs.phpmyadmin:
2019-07-14 22:50:34 +02:00
if os.path.isdir('/var/www/22222/htdocs/db/pma'):
2019-10-30 12:06:42 +01:00
packages = packages + [[
"https://files.phpmyadmin.net"
"/phpMyAdmin/{0}/phpMyAdmin-{0}-"
"all-languages.tar.gz"
.format(wo_phpmyadmin),
2019-10-30 12:06:42 +01:00
"/var/lib/wo/tmp/pma.tar.gz",
"PHPMyAdmin"]]
2019-07-14 22:50:34 +02:00
else:
Log.info(self, "phpMyAdmin isn't installed")
2019-07-13 15:15:52 +02:00
# adminer
if pargs.adminer:
if os.path.isfile("{0}22222/htdocs/db/"
"adminer/index.php"
.format(WOVar.wo_webroot)):
Log.debug(self, "Setting packages variable for Adminer ")
packages = packages + [[
"https://www.adminer.org/latest.php",
"{0}22222/"
"htdocs/db/adminer/index.php"
.format(WOVar.wo_webroot),
"Adminer"],
["https://raw.githubusercontent.com"
"/vrana/adminer/master/designs/"
"pepa-linha/adminer.css",
"{0}22222/"
"htdocs/db/adminer/adminer.css"
.format(WOVar.wo_webroot),
"Adminer theme"]]
else:
Log.debug(self, "Adminer isn't installed")
Log.info(self, "Adminer isn't installed")
2019-10-30 12:06:42 +01:00
# composer
2019-08-07 02:45:26 +02:00
if pargs.composer:
2019-07-16 17:15:17 +02:00
if os.path.isfile('/usr/local/bin/composer'):
2019-10-28 09:09:28 +01:00
packages = packages + [[
"https://getcomposer.org/installer",
"/var/lib/wo/tmp/composer-install",
"Composer"]]
2019-07-16 17:15:17 +02:00
else:
Log.info(self, "Composer isn't installed")
2019-09-06 02:34:33 +02:00
# mysqltuner
if pargs.mysqltuner:
if WOAptGet.is_exec(self, 'mysqltuner'):
Log.debug(self, "Setting packages variable "
"for MySQLTuner ")
packages = packages + [["https://raw."
"githubusercontent.com/"
"major/MySQLTuner-perl"
"/master/mysqltuner.pl",
"/usr/bin/mysqltuner",
"MySQLTuner"]]
2019-10-30 12:06:42 +01:00
# ngxblocker
if pargs.ngxblocker:
2019-11-05 16:11:43 +01:00
if os.path.exists('/usr/local/sbin/install-ngxblocker'):
2019-10-30 12:06:42 +01:00
packages = packages + [[
'https://raw.githubusercontent.com/mitchellkrogza/'
'nginx-ultimate-bad-bot-blocker/master/update-ngxblocker',
'/usr/local/sbin/update-ngxblocker',
'ngxblocker'
]]
2022-09-13 15:22:09 +02:00
if not apt_packages and not packages:
2019-09-06 02:34:33 +02:00
self.app.args.print_help()
else:
pre_stack(self)
2022-09-13 15:22:09 +02:00
if apt_packages:
if not ("php7.2-fpm" in apt_packages or
"php7.3-fpm" in apt_packages or
"php7.4-fpm" in apt_packages or
2022-01-23 15:14:24 -03:00
"php8.0-fpm" in apt_packages or
"php8.1-fpm" in apt_packages or
"redis-server" in apt_packages or
"nginx-custom" in apt_packages or
"mariadb-server" in apt_packages):
2019-09-06 02:34:33 +02:00
pass
else:
Log.warn(
2019-10-08 02:19:22 +02:00
self, "Your sites may be down for few seconds if "
2019-09-06 02:34:33 +02:00
"you are upgrading Nginx, PHP-FPM, MariaDB or Redis")
2019-08-20 13:53:41 +02:00
# Check prompt
if not (pargs.no_prompt or pargs.force):
2019-08-20 13:53:41 +02:00
start_upgrade = input("Do you want to continue:[y/N]")
if start_upgrade != "Y" and start_upgrade != "y":
Log.error(self, "Not starting package update")
2020-10-22 14:34:03 +02:00
# additional pre_pref
if "nginx-custom" in apt_packages:
pre_pref(self, WOVar.wo_nginx)
Log.wait(self, "Updating APT cache")
2019-08-20 13:53:41 +02:00
# apt-get update
WOAptGet.update(self)
Log.valide(self, "Updating APT cache")
2019-09-06 02:34:33 +02:00
# check if nginx upgrade is blocked
if os.path.isfile(
'/etc/apt/preferences.d/nginx-block'):
2019-10-02 13:13:32 +02:00
post_pref(self, WOVar.wo_nginx, [], True)
# redis pre_pref
if "redis-server" in apt_packages:
pre_pref(self, WOVar.wo_redis)
# upgrade packages
2019-08-20 13:53:41 +02:00
WOAptGet.install(self, apt_packages)
2019-09-06 02:34:33 +02:00
Log.wait(self, "Configuring APT Packages")
2019-09-03 19:02:00 +02:00
post_pref(self, apt_packages, [], True)
2019-09-06 02:34:33 +02:00
Log.valide(self, "Configuring APT Packages")
2019-08-20 13:53:41 +02:00
# Post Actions after package updates
2019-07-16 17:15:17 +02:00
2022-09-13 15:22:09 +02:00
if packages:
if WOAptGet.is_selected(self, 'WP-CLI', packages):
2019-08-20 13:53:41 +02:00
WOFileUtils.rm(self, '/usr/local/bin/wp')
2018-11-30 17:04:15 +01:00
if WOAptGet.is_selected(self, 'Netdata', packages):
2019-08-20 13:53:41 +02:00
WOFileUtils.rm(self, '/var/lib/wo/tmp/kickstart.sh')
2018-11-30 17:04:15 +01:00
if WOAptGet.is_selected(self, 'ngxblocker', packages):
2019-10-30 12:06:42 +01:00
WOFileUtils.rm(self, '/usr/local/sbin/update-ngxblocker')
if WOAptGet.is_selected(self, 'WordOps Dashboard', packages):
2019-09-22 14:11:12 +02:00
if os.path.isfile('/var/www/22222/htdocs/index.php'):
WOFileUtils.rm(self, '/var/www/22222/htdocs/index.php')
if os.path.isfile('/var/www/22222/htdocs/index.html'):
WOFileUtils.rm(
self, '/var/www/22222/htdocs/index.html')
2019-08-16 14:53:39 +02:00
2019-08-20 13:53:41 +02:00
Log.debug(self, "Downloading following: {0}".format(packages))
WODownload.download(self, packages)
2019-07-13 15:15:52 +02:00
if WOAptGet.is_selected(self, 'WP-CLI', packages):
2019-08-20 13:53:41 +02:00
WOFileUtils.chmod(self, "/usr/local/bin/wp", 0o775)
2018-11-30 17:04:15 +01:00
if WOAptGet.is_selected(self, 'ngxblocker', packages):
2019-12-05 20:36:18 +01:00
if os.path.exists('/etc/nginx/conf.d/variables-hash.conf'):
WOFileUtils.rm(
self, '/etc/nginx/conf.d/variables-hash.conf')
2019-10-30 12:06:42 +01:00
WOFileUtils.chmod(
self, '/usr/local/sbin/update-ngxblocker', 0o775)
2019-10-30 12:44:36 +01:00
WOShellExec.cmd_exec(
self, '/usr/local/sbin/update-ngxblocker -nq')
if WOAptGet.is_selected(self, 'MySQLTuner', packages):
WOFileUtils.chmod(self, "/usr/bin/mysqltuner", 0o775)
if os.path.exists('/usr/local/bin/mysqltuner'):
WOFileUtils.rm(self, '/usr/local/bin/mysqltuner')
2019-10-30 12:06:42 +01:00
# Netdata
if WOAptGet.is_selected(self, 'Netdata', packages):
2020-01-14 16:26:25 +01:00
WOService.stop_service(self, 'netdata')
2019-09-06 02:34:33 +02:00
Log.wait(self, "Upgrading Netdata")
2019-10-30 12:06:42 +01:00
# detect static binaries install
2020-01-14 16:26:25 +01:00
WOShellExec.cmd_exec(
self,
"bash /var/lib/wo/tmp/kickstart.sh "
"--dont-wait --no-updates",
errormsg='', log=False)
2019-09-06 02:34:33 +02:00
Log.valide(self, "Upgrading Netdata")
2018-11-30 17:04:15 +01:00
if WOAptGet.is_selected(self, 'WordOps Dashboard', packages):
2019-09-22 14:11:12 +02:00
post_pref(
self, [], [["https://github.com/WordOps"
"/wordops-dashboard/"
"releases/download/v{0}/"
"wordops-dashboard.tar.gz"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_dashboard),
2019-09-22 14:11:12 +02:00
"/var/lib/wo/tmp/wo-dashboard.tar.gz",
"WordOps Dashboard"]])
2019-07-16 17:15:17 +02:00
if WOAptGet.is_selected(self, 'Composer', packages):
2019-09-22 14:11:12 +02:00
Log.wait(self, "Upgrading Composer")
2019-10-30 12:06:42 +01:00
if WOShellExec.cmd_exec(
self, '/usr/bin/php -v'):
WOShellExec.cmd_exec(
self, "php -q /var/lib/wo"
"/tmp/composer-install "
"--install-dir=/var/lib/wo/tmp/")
2019-08-20 13:53:41 +02:00
shutil.copyfile('/var/lib/wo/tmp/composer.phar',
'/usr/local/bin/composer')
WOFileUtils.chmod(self, "/usr/local/bin/composer", 0o775)
2019-09-06 02:34:33 +02:00
Log.valide(self, "Upgrading Composer ")
2019-08-16 14:53:39 +02:00
if WOAptGet.is_selected(self, 'PHPMyAdmin', packages):
2019-09-22 14:11:12 +02:00
Log.wait(self, "Upgrading phpMyAdmin")
2019-08-20 13:53:41 +02:00
WOExtract.extract(self, '/var/lib/wo/tmp/pma.tar.gz',
'/var/lib/wo/tmp/')
shutil.copyfile(('{0}22222/htdocs/db/pma'
'/config.inc.php'
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot)),
2019-08-20 13:53:41 +02:00
('/var/lib/wo/tmp/phpMyAdmin-{0}'
'-all-languages/config.inc.php'
.format(wo_phpmyadmin))
2019-08-20 13:53:41 +02:00
)
WOFileUtils.rm(self, '{0}22222/htdocs/db/pma'
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot))
2019-08-20 13:53:41 +02:00
shutil.move('/var/lib/wo/tmp/phpMyAdmin-{0}'
'-all-languages/'
.format(wo_phpmyadmin),
2019-08-20 13:53:41 +02:00
'{0}22222/htdocs/db/pma/'
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot))
2019-08-20 13:53:41 +02:00
WOFileUtils.chown(self, "{0}22222/htdocs"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot),
2019-09-06 02:34:33 +02:00
'www-data',
'www-data', recursive=True)
2019-09-22 14:11:12 +02:00
Log.valide(self, "Upgrading phpMyAdmin")
if os.path.exists('{0}22222/htdocs'.format(WOVar.wo_webroot)):
WOFileUtils.chown(self, "{0}22222/htdocs"
.format(WOVar.wo_webroot),
'www-data',
'www-data', recursive=True)
2019-07-13 15:15:52 +02:00
2018-11-30 17:04:15 +01:00
Log.info(self, "Successfully updated packages")