2018-11-13 21:55:59 +01:00
|
|
|
from cement.core.controller import CementBaseController, expose
|
|
|
|
|
from cement.core import handler, hook
|
|
|
|
|
from wo.core.mysql import WOMysql
|
|
|
|
|
from wo.core.logging import Log
|
|
|
|
|
from wo.core.variables import WOVariables
|
|
|
|
|
from wo.core.aptget import WOAptGet
|
|
|
|
|
from wo.core.shellexec import WOShellExec
|
|
|
|
|
from wo.core.apt_repo import WORepo
|
|
|
|
|
import configparser
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WOStackMigrateController(CementBaseController):
|
|
|
|
|
class Meta:
|
|
|
|
|
label = 'migrate'
|
|
|
|
|
stacked_on = 'stack'
|
|
|
|
|
stacked_type = 'nested'
|
|
|
|
|
description = ('Migrate stack safely')
|
|
|
|
|
arguments = [
|
|
|
|
|
(['--mariadb'],
|
|
|
|
|
dict(help="Migrate database to MariaDB",
|
|
|
|
|
action='store_true')),
|
2019-03-19 16:58:35 +01:00
|
|
|
]
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
@expose(hide=True)
|
|
|
|
|
def migrate_mariadb(self):
|
|
|
|
|
# Backup all database
|
|
|
|
|
WOMysql.backupAll(self)
|
|
|
|
|
|
|
|
|
|
# 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=WOVariables.wo_mysql_repo)
|
|
|
|
|
Log.debug(self, 'Adding key for {0}'
|
|
|
|
|
.format(WOVariables.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")
|
2019-03-07 13:15:15 +01:00
|
|
|
Log.debug(self, "echo \"mariadb-server-10.3 "
|
2018-11-13 21:55:59 +01:00
|
|
|
"mysql-server/root_password "
|
|
|
|
|
"password \" | "
|
|
|
|
|
"debconf-set-selections")
|
2019-03-07 13:15:15 +01:00
|
|
|
WOShellExec.cmd_exec(self, "echo \"mariadb-server-10.3 "
|
2018-11-13 21:55:59 +01:00
|
|
|
"mysql-server/root_password "
|
|
|
|
|
"password {chars}\" | "
|
|
|
|
|
"debconf-set-selections"
|
|
|
|
|
.format(chars=chars),
|
|
|
|
|
log=False)
|
2019-03-07 13:15:15 +01:00
|
|
|
Log.debug(self, "echo \"mariadb-server-10.3 "
|
2018-11-13 21:55:59 +01:00
|
|
|
"mysql-server/root_password_again "
|
|
|
|
|
"password \" | "
|
|
|
|
|
"debconf-set-selections")
|
2019-03-07 13:15:15 +01:00
|
|
|
WOShellExec.cmd_exec(self, "echo \"mariadb-server-10.3 "
|
2018-11-13 21:55:59 +01:00
|
|
|
"mysql-server/root_password_again "
|
|
|
|
|
"password {chars}\" | "
|
|
|
|
|
"debconf-set-selections"
|
|
|
|
|
.format(chars=chars),
|
|
|
|
|
log=False)
|
|
|
|
|
|
|
|
|
|
# Install MariaDB
|
|
|
|
|
apt_packages = WOVariables.wo_mysql
|
|
|
|
|
|
2018-11-30 19:33:29 +01:00
|
|
|
# If PHP is installed then install php7.2-mysql
|
|
|
|
|
if WOAptGet.is_installed(self, "php7.2-fpm"):
|
|
|
|
|
apt_packages = apt_packages + ["php7.2-mysql"]
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
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.auto_remove(self)
|
|
|
|
|
WOAptGet.install(self, apt_packages)
|
|
|
|
|
|
|
|
|
|
@expose(hide=True)
|
|
|
|
|
def default(self):
|
|
|
|
|
if ((not self.app.pargs.mariadb)):
|
|
|
|
|
self.app.args.print_help()
|
|
|
|
|
if self.app.pargs.mariadb:
|
|
|
|
|
if WOVariables.wo_mysql_host is not "localhost":
|
2019-03-19 16:58:35 +01:00
|
|
|
Log.error(
|
|
|
|
|
self, "Remote MySQL server in use, skipping local install")
|
2018-11-13 21:55:59 +01:00
|
|
|
|
2019-04-25 01:25:13 +02:00
|
|
|
if (WOShellExec.cmd_exec(self, "mysqladmin ping") and
|
|
|
|
|
(not WOAptGet.is_installed(self, 'mariadb-server'))):
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
self.migrate_mariadb()
|
|
|
|
|
else:
|
|
|
|
|
Log.error(self, "Your current MySQL is not alive or "
|
|
|
|
|
"you allready installed MariaDB")
|