From bed285abc4ad15c9bcf2317c40dbe5b68f558fb2 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Fri, 23 Oct 2020 12:50:31 +0200 Subject: [PATCH] Use wo stack migrate for MariaDB major upgrade --- tests/travis.sh | 18 +++++++ wo/cli/controllers/base.py | 2 +- wo/cli/plugins/stack_migrate.py | 95 ++++++++------------------------- wo/cli/plugins/stack_pref.py | 16 +----- wo/cli/plugins/stack_upgrade.py | 15 +----- 5 files changed, 44 insertions(+), 102 deletions(-) diff --git a/tests/travis.sh b/tests/travis.sh index 41077c3..d0e825e 100644 --- a/tests/travis.sh +++ b/tests/travis.sh @@ -226,6 +226,24 @@ if [ -z "$1" ]; then fi done fi + +echo -e "${CGREEN}#############################################${CEND}" +echo -e ' wo stack migrate --mariadb ' +echo -e "${CGREEN}#############################################${CEND}" + + echo -ne " Upgrading mariadb [..]\r" + if { + wo stack migrate --mariadb --force + } >>/var/log/wo/test.log; then + echo -ne " Upgrading mariadb [${CGREEN}OK${CEND}]\\r" + echo -ne '\n' + else + echo -e " Upgrading mariadb [${CRED}FAIL${CEND}]" + echo -ne '\n' + exit_script + + fi + echo -e "${CGREEN}#############################################${CEND}" echo -e ' wo clean ' echo -e "${CGREEN}#############################################${CEND}" diff --git a/wo/cli/controllers/base.py b/wo/cli/controllers/base.py index 9558442..95be6a6 100644 --- a/wo/cli/controllers/base.py +++ b/wo/cli/controllers/base.py @@ -8,7 +8,7 @@ VERSION = WOVar.wo_version BANNER = """ WordOps v%s -Copyright (c) 2019 WordOps. +Copyright (c) 2020 WordOps. """ % VERSION diff --git a/wo/cli/plugins/stack_migrate.py b/wo/cli/plugins/stack_migrate.py index 3740a0a..be1dabb 100644 --- a/wo/cli/plugins/stack_migrate.py +++ b/wo/cli/plugins/stack_migrate.py @@ -1,10 +1,8 @@ -import configparser -import os - from cement.core.controller import CementBaseController, expose -from wo.core.apt_repo import WORepo +from wo.cli.plugins.stack_pref import post_pref, pre_pref from wo.core.aptget import WOAptGet +from wo.core.fileutils import WOFileUtils from wo.core.logging import Log from wo.core.mysql import WOMysql from wo.core.shellexec import WOShellExec @@ -19,7 +17,10 @@ class WOStackMigrateController(CementBaseController): description = ('Migrate stack safely') arguments = [ (['--mariadb'], - dict(help="Migrate database to MariaDB", + dict(help="Migrate/Upgrade database to MariaDB", + action='store_true')), + (['--force'], + dict(help="Force Packages upgrade without any prompt", action='store_true')), ] @@ -28,95 +29,45 @@ class WOStackMigrateController(CementBaseController): # Backup all database WOMysql.backupAll(self, fulldump=True) - if not WOVar.wo_distro == 'raspbian': - if (not WOVar.wo_platform_codename == 'jessie'): - wo_mysql = ["mariadb-server", "percona-toolkit", - "python3-mysqldb", "mariadb-backup"] - else: - wo_mysql = ["mariadb-server", "percona-toolkit", - "python3-mysql.connector"] - else: - wo_mysql = ["mariadb-server", "percona-toolkit", - "python3-mysqldb"] - # Add MariaDB repo Log.info(self, "Adding repository for MariaDB, please wait...") - - mysql_pref = ("Package: *\nPin: origin sfo1.mirrors.digitalocean.com" - "\nPin-Priority: 1000\n") - with open('/etc/apt/preferences.d/' - 'MariaDB.pref', 'w') as mysql_pref_file: - mysql_pref_file.write(mysql_pref) - - WORepo.add(self, repo_url=WOVar.wo_mysql_repo) - Log.debug(self, 'Adding key for {0}' - .format(WOVar.wo_mysql_repo)) - WORepo.add_key(self, '0xcbcb082a1bb943db', - keyserver="keyserver.ubuntu.com") - - config = configparser.ConfigParser() - if os.path.exists('/etc/mysql/conf.d/my.cnf'): - config.read('/etc/mysql/conf.d/my.cnf') - else: - config.read(os.path.expanduser("~")+'/.my.cnf') - - try: - chars = config['client']['password'] - except Exception as e: - Log.error(self, "Error: process exited with error %s" - % e) - - Log.debug(self, "Pre-seeding MariaDB") - Log.debug(self, "echo \"mariadb-server-10.3 " - "mysql-server/root_password " - "password \" | " - "debconf-set-selections") - WOShellExec.cmd_exec(self, "echo \"mariadb-server-10.3 " - "mysql-server/root_password " - "password {chars}\" | " - "debconf-set-selections" - .format(chars=chars), - log=False) - Log.debug(self, "echo \"mariadb-server-10.3 " - "mysql-server/root_password_again " - "password \" | " - "debconf-set-selections") - WOShellExec.cmd_exec(self, "echo \"mariadb-server-10.3 " - "mysql-server/root_password_again " - "password {chars}\" | " - "debconf-set-selections" - .format(chars=chars), - log=False) + pre_pref(self, WOVar.wo_mysql) # Install MariaDB - apt_packages = wo_mysql Log.info(self, "Updating apt-cache, hang on...") WOAptGet.update(self) Log.info(self, "Installing MariaDB, hang on...") - WOAptGet.remove(self, ["mysql-common", "libmysqlclient18"]) + WOAptGet.remove(self, ["mariadb-server"]) WOAptGet.auto_remove(self) - WOAptGet.install(self, apt_packages) + WOAptGet.install(self, WOVar.wo_mysql) + post_pref(self, WOVar.wo_mysql, []) + WOShellExec.cmd_exec(self, 'systemctl daemon-reload') + WOFileUtils.mvfile( + self, '/etc/mysql/my.cnf', '/etc/mysql/my.cnf.old') + WOFileUtils.create_symlink( + self, ['/etc/mysql/mariadb.cnf', '/etc/mysql/my.cnf']) @expose(hide=True) def default(self): - if ((not self.app.pargs.mariadb)): + pargs = self.app.pargs + if ((not pargs.mariadb)): self.app.args.print_help() - if self.app.pargs.mariadb: + if pargs.mariadb: if WOVar.wo_mysql_host != "localhost": Log.error( self, "Remote MySQL server in use, skipping local install") - if (WOShellExec.cmd_exec(self, "mysqladmin ping") and - (not WOAptGet.is_installed(self, 'mariadb-server'))): + if (WOShellExec.cmd_exec(self, "mysqladmin ping")): Log.info(self, "If your database size is big, " "migration may take some time.") Log.info(self, "During migration non nginx-cached parts of " "your site may remain down") - start_migrate = input("Type \"mariadb\" to continue:") - if start_migrate != "mariadb": - Log.error(self, "Not starting migration") + if 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") self.migrate_mariadb() else: Log.error(self, "Your current MySQL is not alive or " diff --git a/wo/cli/plugins/stack_pref.py b/wo/cli/plugins/stack_pref.py index a2de59c..ca619a9 100644 --- a/wo/cli/plugins/stack_pref.py +++ b/wo/cli/plugins/stack_pref.py @@ -37,17 +37,7 @@ def pre_pref(self, apt_packages): with open('/etc/apt/preferences.d/' 'MariaDB.pref', 'w') as mysql_pref_file: mysql_pref_file.write(mysql_pref) - if not os.path.exists('/etc/apt/sources.list.d/wo-repo.list'): - WORepo.add(self, repo_url=WOVar.wo_mysql_repo) - else: - if not WOFileUtils.grepcheck( - self, '/etc/apt/sources.list.d/wo-repo.list', - 'MariaDB/repo/10.3'): - WORepo.add(self, repo_url=WOVar.wo_mysql_repo) - else: - WOFileUtils.searchreplace( - self, '/etc/apt/sources.list.d/wo-repo.list', - '10.3', '10.5') + WORepo.add(self, repo_url=WOVar.wo_mysql_repo) WORepo.add_key(self, '0xcbcb082a1bb943db', keyserver='keyserver.ubuntu.com') WORepo.add_key(self, '0xF1656F24C74CD1D8', @@ -996,10 +986,6 @@ def post_pref(self, apt_packages, packages, upgrade=False): WOTemplate.deploy( self, '/etc/mysql/mariadb.conf.d/50-server.cnf', 'my.mustache', data) - WOFileUtils.mvfile( - self, '/etc/mysql/my.cnf', '/etc/mysql/my.cnf.old') - WOFileUtils.create_symlink( - self, ['/etc/mysql/mariadb.cnf', '/etc/mysql/my.cnf']) else: WOTemplate.deploy( self, '/etc/mysql/my.cnf', 'my.mustache', data) diff --git a/wo/cli/plugins/stack_upgrade.py b/wo/cli/plugins/stack_upgrade.py index ea4cd8b..5b5bd7b 100644 --- a/wo/cli/plugins/stack_upgrade.py +++ b/wo/cli/plugins/stack_upgrade.py @@ -294,12 +294,6 @@ class WOStackUpgradeController(CementBaseController): # additional pre_pref if "nginx-custom" in apt_packages: pre_pref(self, WOVar.wo_nginx) - if "mariadb-server" in apt_packages: - mariadbmajorupgrade = bool( - WOFileUtils.grepcheck( - self, '/etc/apt/sources.list.d/wo-repo.list', - 'MariaDB/repo/10.3')) - pre_pref(self, WOVar.wo_mysql) Log.wait(self, "Updating APT cache") # apt-get update WOAptGet.update(self) @@ -312,17 +306,10 @@ class WOStackUpgradeController(CementBaseController): # redis pre_pref if "redis-server" in apt_packages: pre_pref(self, WOVar.wo_redis) - # mariadb upgrade - if ("mariadb-server" in apt_packages and - mariadbmajorupgrade is True): - WOMysql.backupAll(self, fulldump=True) - WOAptGet.remove(self, ["mariadb-server"]) - # upgrade packages + # upgrade packages WOAptGet.install(self, apt_packages) Log.wait(self, "Configuring APT Packages") post_pref(self, apt_packages, [], True) - if "mariadb-server" in apt_packages: - WOShellExec.cmd_exec(self, 'mysql_upgrade') Log.valide(self, "Configuring APT Packages") # Post Actions after package updates