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

308 lines
13 KiB
Python
Raw Normal View History

import os
import shutil
2019-08-05 04:56:33 +02:00
from cement.core import handler, hook
2019-09-02 04:37:13 +02:00
from cement.core.controller import CementBaseController, expose
2019-09-04 20:36:15 +02:00
2019-09-02 04:37:13 +02:00
from wo.cli.plugins.stack_pref import post_pref, pre_pref
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
from wo.core.services import WOService
2018-11-13 21:55:59 +01:00
from wo.core.shellexec import WOShellExec
from wo.core.variables import WOVariables
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')),
(['--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')),
(['--php73'],
dict(help='Upgrade PHP 7.3 stack', action='store_true')),
2018-11-13 21:55:59 +01:00
(['--mysql'],
dict(help='Upgrade MySQL stack', action='store_true')),
(['--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')),
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')),
2019-07-14 22:50:34 +02:00
(['--phpmyadmin'],
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
2018-11-30 17:04:15 +01:00
2019-08-07 02:45:26 +02:00
if ((not pargs.web) and (not pargs.nginx) and
(not pargs.php) and (not pargs.php73) and
(not pargs.mysql) and
(not pargs.all) and (not pargs.wpcli) and
(not pargs.netdata) and (not pargs.composer) and
2019-08-16 14:53:39 +02:00
(not pargs.phpmyadmin) and (not pargs.dashboard) and
2019-08-07 02:45:26 +02:00
(not pargs.redis)):
pargs.web = True
2018-11-30 17:04:15 +01:00
2019-08-07 02:45:26 +02:00
if pargs.all:
pargs.web = True
2019-08-29 16:19:27 +02:00
pargs.netdata = True
pargs.composer = True
pargs.dashboard = True
pargs.phpmyadmin = True
pargs.redis = True
pargs.wpcli = True
pargs.php73 = True
2018-11-30 17:04:15 +01:00
2019-08-07 02:45:26 +02:00
if pargs.web:
2018-11-30 17:04:15 +01:00
if WOAptGet.is_installed(self, 'nginx-custom'):
2019-08-07 02:45:26 +02:00
pargs.nginx = True
pargs.php = True
pargs.mysql = True
pargs.wpcli = True
2018-11-30 17:04:15 +01:00
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-09-06 02:34:33 +02:00
apt_packages = apt_packages + WOVariables.wo_nginx
2018-11-30 17:04:15 +01:00
else:
Log.info(self, "Nginx Stable is not already installed")
2019-08-07 02:45:26 +02:00
if pargs.php:
2018-11-30 17:04:15 +01:00
if WOAptGet.is_installed(self, 'php7.2-fpm'):
2019-03-27 12:49:17 +01:00
if not WOAptGet.is_installed(self, 'php7.3-fpm'):
2019-04-29 02:06:32 +02:00
apt_packages = apt_packages + WOVariables.wo_php + \
WOVariables.wo_php_extra
2019-03-27 12:49:17 +01:00
else:
apt_packages = apt_packages + WOVariables.wo_php
2018-11-30 17:04:15 +01:00
else:
Log.info(self, "PHP 7.2 is not installed")
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'):
if not WOAptGet.is_installed(self, 'php7.2-fpm'):
apt_packages = apt_packages + WOVariables.wo_php73 + \
WOVariables.wo_php_extra
else:
apt_packages = apt_packages + WOVariables.wo_php73
else:
Log.info(self, "PHP 7.3 is not installed")
2019-08-07 02:45:26 +02:00
if pargs.mysql:
2018-11-30 17:04:15 +01:00
if WOAptGet.is_installed(self, 'mariadb-server'):
2019-09-04 16:55:58 +02:00
apt_packages = apt_packages + ['mariadb-server']
2018-11-30 17:04:15 +01:00
else:
Log.info(self, "MariaDB is not installed")
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
else:
Log.info(self, "Redis is not installed")
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'):
2018-11-30 17:04:15 +01:00
packages = packages + [["https://github.com/wp-cli/wp-cli/"
"releases/download/v{0}/"
"wp-cli-{0}.phar"
"".format(WOVariables.wo_wp_cli),
2019-03-04 07:12:57 +01:00
"/usr/local/bin/wp",
2018-11-30 17:04:15 +01:00
"WP-CLI"]]
else:
Log.info(self, "WPCLI is not installed with WordOps")
2019-08-07 02:45:26 +02:00
if pargs.netdata:
if (os.path.isdir('/opt/netdata') or
os.path.isdir('/etc/netdata')):
2019-07-13 15:15:52 +02:00
packages = packages + [['https://my-netdata.io/'
'kickstart-static64.sh',
'/var/lib/wo/tmp/kickstart.sh',
'Netdata']]
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-08-16 14:53:39 +02:00
packages = packages + \
[["https://github.com/WordOps/wordops-dashboard/"
"releases/download/v{0}/wordops-dashboard.tar.gz"
.format(WOVariables.wo_dashboard),
"/var/lib/wo/tmp/wo-dashboard.tar.gz",
2019-08-20 06:17:32 +02:00
"WordOps Dashboard"]]
2019-08-16 14:53:39 +02:00
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'):
packages = packages + \
[["https://files.phpmyadmin.net"
"/phpMyAdmin/{0}/"
"phpMyAdmin-{0}-"
"all-languages"
2019-07-24 11:03:50 +02:00
".tar.gz".format(WOVariables.wo_phpmyadmin),
2019-07-14 22:50:34 +02:00
"/var/lib/wo/tmp/pma.tar.gz",
"PHPMyAdmin"]]
else:
Log.error(self, "phpMyAdmin isn't installed")
2019-07-13 15:15:52 +02:00
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'):
packages = packages + [["https://getcomposer.org/installer",
"/var/lib/wo/tmp/composer-install",
"Composer"]]
else:
Log.error(self, "Composer isn't installed")
2019-09-06 02:34:33 +02:00
if ((not (apt_packages)) and (not(packages))):
self.app.args.print_help()
else:
if (apt_packages):
if not (set(["php7.2-fpm"]).issubset(set(apt_packages)) and
set(["php7.3-fpm"]).issubset(set(apt_packages)) and
set(["nginx-custom",
"nginx-wo"]).issubset(set(apt_packages)) and
set(['mariadb-server']).issubset(set(apt_packages))):
pass
else:
Log.info(
self, "Your site may be down for few seconds if "
"you are upgrading Nginx, PHP-FPM, MariaDB or Redis")
2019-08-20 13:53:41 +02:00
# Check prompt
if ((not pargs.no_prompt) and (not pargs.force)):
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")
2019-09-06 02:34:33 +02:00
Log.wait(self, "Updating APT packages")
2019-08-20 13:53:41 +02:00
# apt-get update
WOAptGet.update(self)
2019-09-06 02:34:33 +02:00
Log.valide(self, "Updating APT packages")
Log.wait(self, "Upgrading APT Packages")
# additional pre_pref
if ["nginx-custom"] in apt_packages:
pre_pref(self, WOVariables.wo_nginx)
if ["php7.2-fpm"] in apt_packages:
2019-08-20 13:53:41 +02:00
WOAptGet.remove(self, ['php7.2-fpm'],
auto=False, purge=True)
2019-09-06 02:34:33 +02:00
if ["php7.3-fpm"] in apt_packages:
2019-08-20 13:53:41 +02:00
WOAptGet.remove(self, ['php7.3-fpm'],
auto=False, purge=True)
2019-09-06 02:34:33 +02:00
# check if nginx upgrade is blocked
if os.path.isfile(
'/etc/apt/preferences.d/nginx-block'):
apt_packages.remove(WOVariables.wo_nginx)
post_pref(self, WOVariables.wo_nginx, [], True)
# upgrade packages
2019-08-20 13:53:41 +02:00
WOAptGet.install(self, apt_packages)
2019-09-06 02:34:33 +02:00
Log.valide(self, "Upgrading APT Packages")
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
2019-09-06 02:34:33 +02:00
if (packages):
2019-08-20 13:53:41 +02:00
if pargs.wpcli:
WOFileUtils.rm(self, '/usr/local/bin/wp')
2018-11-30 17:04:15 +01:00
2019-08-20 13:53:41 +02:00
if pargs.netdata:
WOFileUtils.rm(self, '/var/lib/wo/tmp/kickstart.sh')
2018-11-30 17:04:15 +01:00
2019-08-20 13:53:41 +02:00
if pargs.dashboard:
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
2019-08-20 13:53:41 +02:00
if pargs.wpcli:
WOFileUtils.chmod(self, "/usr/local/bin/wp", 0o775)
2018-11-30 17:04:15 +01:00
2019-08-20 13:53:41 +02:00
if pargs.netdata:
2019-09-06 02:34:33 +02:00
Log.wait(self, "Upgrading Netdata")
if os.path.isdir('/opt/netdata'):
WOShellExec.cmd_exec(
self, "bash /opt/netdata/usr/"
"libexec/netdata/netdata-"
"updater.sh")
elif os.path.isdir('/etc/netdata'):
WOShellExec.cmd_exec(
self, "bash /usr/"
"libexec/netdata/netdata-"
"updater.sh")
2019-09-06 02:34:33 +02:00
Log.valide(self, "Upgrading Netdata")
2018-11-30 17:04:15 +01:00
2019-08-20 13:53:41 +02:00
if pargs.dashboard:
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"
.format(WOVariables.wo_dashboard),
"/var/lib/wo/tmp/wo-dashboard.tar.gz",
"WordOps Dashboard"]])
2019-07-16 17:15:17 +02:00
2019-08-20 13:53:41 +02:00
if pargs.composer:
2019-09-22 14:11:12 +02:00
Log.wait(self, "Upgrading Composer")
2019-09-06 02:34:33 +02:00
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
2019-08-20 13:53:41 +02:00
if pargs.phpmyadmin:
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'
.format(WOVariables.wo_webroot)),
('/var/lib/wo/tmp/phpMyAdmin-{0}'
'-all-languages/config.inc.php'
.format(WOVariables.wo_phpmyadmin))
)
WOFileUtils.rm(self, '{0}22222/htdocs/db/pma'
.format(WOVariables.wo_webroot))
shutil.move('/var/lib/wo/tmp/phpMyAdmin-{0}'
'-all-languages/'
.format(WOVariables.wo_phpmyadmin),
'{0}22222/htdocs/db/pma/'
.format(WOVariables.wo_webroot))
WOFileUtils.chown(self, "{0}22222/htdocs"
.format(WOVariables.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")
2019-07-13 15:15:52 +02:00
2018-11-30 17:04:15 +01:00
Log.info(self, "Successfully updated packages")