def install_mariadb(self, version): """ Install official MariaDB """ pkgs = ('MariaDB-server', 'MariaDB-client', 'MariaDB-shared', 'MariaDB-devel', 'MariaDB-compat',) # prepare repo data print 'Preparing official MariaDB repository...' repo_data = """[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/{maria_ver}/centos{cl_ver}-{arch} gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 """ num = version.split('mariadb')[-1] mariadb_version = '{base}.{suffix}'.format(base=num[:-1], suffix=num[-1]) arch = 'amd64' if os.uname()[-1] == 'x86_64' else 'x86' with open('/etc/yum.repos.d/MariaDB.repo', 'wb') as repo_file: repo_file.write( repo_data.format(maria_ver=mariadb_version, cl_ver=self.cl_version, arch=arch)) # install MariaDB packages print 'Installing packages' exec_command( "yum install -y --disableexcludes=all --disablerepo=cl-mysql* --disablerepo=mysqclient* {pkgs}".format( pkgs=' '.join(pkgs)))
def _after_install_new_packages(self): """ Specific actions after new packages installation """ # call parent after_install InstallManager._after_install_new_packages(self) # install MySQL-python module exec_command("yum install -y MySQL-python --disableexcludes=all") print "Rebuild php please... /usr/local/directadmin/custombuild/build php"
def get_mysql_user(self): """ Retrieve MySQL user name and password and save it into self attributes """ self.MYSQLUSER = "******" self.MYSQLPASSWORD = "******" if os.path.exists("/usr/local/ispmgr/etc/ispmgr.conf"): self.MYSQLPASSWORD = exec_command( """cat /usr/local/ispmgr/etc/ispmgr.conf | sed -n '/DbServer "MySQL"/,/SupportCenterServer/p' | sed -n '/Password /p' | sed '/Change/d' | tr -d '\n' | cut -d" " -f2""", True, silent=True) elif os.path.exists("/usr/local/mgr5/etc/ispmgr.conf.d/db.conf"): try: self.MYSQLUSER = \ grep("/usr/local/mgr5/etc/ispmgr.conf.d/db.conf", "DBUser")[ 0].split(" ")[1].strip() except IndexError: self.MYSQLUSER = None try: self.MYSQLPASSWORD = \ grep("/usr/local/mgr5/etc/ispmgr.conf.d/db.conf", "DBPassword")[ 0].split(" ")[1].strip() except IndexError: self.MYSQLPASSWORD = None
def _check_mysql_version(): """ Retrieve MySQL version from mysql --version command :return: dictionary with version of form { short: two numbers of version (e.g. 5.5) extended: all numbers of version (e.g. 5.5.52) mysql_type: type flag (mysql or mariadb) full: mysql_type + short version (e.g. mariadb55) } """ try: version_string = exec_command('mysql --version') version_info = re.findall(r'(?<=Distrib\s)[^,]+', version_string[0]) parts = version_info[0].split('-') version = { 'short': '.'.join(parts[0].split('.')[:-1]), 'extended': parts[0], 'mysql_type': parts[1].lower() if len(parts) > 1 else 'mysql' } version.update({ 'full': '{m_type}{m_version}'.format( m_type=version['mysql_type'], m_version=version['short'].replace('.', '')) }) except Exception: return {} return version
def _detect_version_if_auto(self): """ Detect vesrion of MySQL if mysql.type is auto """ print "Detect MySQL version for AUTO" check_file("/usr/local/directadmin/custombuild/build") check_file("/usr/local/directadmin/custombuild/options.conf") MYSQL_DA_VER = "" # MYSQL_DA_TYPE=`cat /usr/local/directadmin/custombuild/options.conf | grep mysql_inst= | cut -d= -f2` try: MYSQL_DA_VER = grep("/usr/local/directadmin/custombuild/options.conf", "mysql=")[0].split("=")[1].strip() MYSQL_DA_TYPE = grep("/usr/local/directadmin/custombuild/options.conf", "mysql_inst=")[0].split("=")[1].strip() except IndexError: MYSQL_DA_VER = "" MYSQL_DA_TYPE = "" if MYSQL_DA_TYPE == "no": if os.path.exists("/usr/share/lve/dbgovernor/da.tp.old"): MYSQL_DA_TYPE = read_file("/usr/share/lve/dbgovernor/da.tp.old") elif os.path.exists("/usr/bin/mysql"): result = exec_command("/usr/bin/mysql -V | grep -c 'MariaDB' -i || true", True) if result == "0": MYSQL_DA_TYPE = "mysql" else: MYSQL_DA_TYPE = "mariadb" print "I got %s and %s" % (MYSQL_DA_VER, MYSQL_DA_TYPE) mysql_version_map = { "5.0": "mysql50", "5.1": "mysql51", "5.5": "mysql55", "5.6": "mysql56", "5.7": "mysql57", "10.0.0": "mariadb100", "10.1.1": "mariadb101" } mariadb_version_map = { "10.1": "mariadb101", "10.0": "mariadb100", "5.6": "mariadb100", "5.5": "mariadb100", "10.0.0": "mariadb100", "10.1.1": "mariadb100" } if MYSQL_DA_TYPE == "mysql": MYSQL_DA_VER = mysql_version_map[MYSQL_DA_VER] elif MYSQL_DA_TYPE == "mariadb": MYSQL_DA_VER = mariadb_version_map[MYSQL_DA_VER] return MYSQL_DA_VER
def _custom_download_of_rpm(self, package_name): """ How we should to download installed MySQL package """ if package_name == "+": return "yes" result = parse_rpm_name(package_name) if len(result) == 4: return exec_command(self._rel( "scripts/cpanel-mysql-url-detect.pm %s %s-%s" % ( result[0], result[1], result[2])), True) return ""
def _detect_version_if_auto(self): """ Detect vesrion of MySQL if mysql.type is auto """ if os.path.exists(self._rel("scripts/detect-cpanel-mysql-version.pm")): mysqlname_array = exec_command( self._rel("scripts/detect-cpanel-mysql-version.pm")) mysqlname = "" if len(mysqlname_array) > 0: mysqlname = mysqlname_array[0] if "mysql" in mysqlname or "mariadb" in mysqlname: return mysqlname.strip() return ""
def _set_mysql_access(self): """ Set mysql admin login and password and save it to governor config """ self.get_mysql_user() if self.MYSQLUSER and self.MYSQLPASSWORD: print "Patch governor configuration file" check_file("/etc/container/mysql-governor.xml") patch_governor_config(self.MYSQLUSER, self.MYSQLPASSWORD) if exec_command("rpm -qa governor-mysql", True): service("restart", "db_governor") print "DB-Governor restarted..."
def _custom_download_of_rpm(self, package_name): """ How we should to download installed MySQL package """ if package_name == "+": return "yes" pkg_name_real = "" list_of_rpm = glob("/usr/local/directadmin/custombuild/mysql/*.rpm") for found_package in list_of_rpm: result = exec_command("/bin/rpm -qp %s" % found_package, True) if package_name in result: pkg_name_real = found_package break if pkg_name_real != "" and os.path.exists(pkg_name_real): return "file:%s" % pkg_name_real return ""
def install_mysql57(self, version): """ Install official MySQL 5.7, not managed by cPanel """ pkgs = ('mysql-community-server', 'mysql-community-client', 'mysql-community-common', 'mysql-community-libs') # prepare mysql repo if not exec_command('rpm -qa | grep mysql57-community', silent=True): self.download_and_install_mysql_repo() # select MySQL version print 'Selected version %s' % version exec_command('yum-config-manager --disable mysql*-community') exec_command('yum-config-manager --enable {version}-community'.format(version=version)) # install mysql-community packages print 'Installing packages' exec_command( "yum install -y --disableexcludes=all --disablerepo=cl-mysql* --disablerepo=mysqclient* {pkgs}".format( pkgs=' '.join(pkgs)))
def detect_percona(force, install_manager_instance): """ Detect unsupported Percona packages :param force: :param install_manager_instance: """ if force: return None packages = exec_command("""rpm -qa|grep -iE "^percona-" """, silent=True) if len(packages): print "Percona packages deteced:" + ",".join(packages) print "You are running Percona, which is not supported by " \ "MySQL Governor. If you want to run MySQL governor, " \ "we would have to uninstall Percona, and substitute it " \ "for MariaDB or MySQL. Run installator next commands for install:" print install_manager_instance.rel("mysqlgovernor.py") + \ " --mysql-version=mysql56 (or mysql50, mysql51, mysql55, " \ "mysql57, mariadb55, mariadb100, mariadb101)" print install_manager_instance.rel("mysqlgovernor.py") + \ " --install --force" sys.exit(2)
def _check_mysql_version(): """ Retrieve MySQL version from mysql --version command :return: dictionary with version of form { short: two numbers of version (e.g. 5.5) extended: all numbers of version (e.g. 5.5.52) mysql_type: type flag (mysql or mariadb) full: mysql_type + short version (e.g. mariadb55) } """ try: version_string = exec_command('mysql --version') version_info = re.findall(r'(?<=Distrib\s)[^,]+', version_string[0]) parts = version_info[0].split('-') version = { 'short': '.'.join(parts[0].split('.')[:-1]), 'extended': parts[0], 'mysql_type': parts[1].lower() if len(parts) > 1 else 'mysql' } version.update({'full': '{m_type}{m_version}'.format(m_type=version['mysql_type'], m_version=version['short'].replace('.', ''))}) except Exception: return {} return version
def restore_mysql_packages(self, current_version): """ Install legacy packages after --delete procedure """ print 'Restoring known packages for {}'.format(current_version['full']) targets = { 'mysql55': 'MySQL55', 'mysql56': 'MySQL56', 'mariadb100': 'MariaDB100', 'mariadb101': 'MariaDB101', 'mariadb102': 'MariaDB102', } # clear rpm management for all known targets for t in targets.values(): exec_command('/usr/local/cpanel/scripts/update_local_rpm_versions --del target_settings.%(target)s' % {'target': t}) # disable mysql targets for upcp not to fix them! for k in filter(lambda x: 'mariadb' not in x and x != current_version['full'], targets.keys()): exec_command('/usr/local/cpanel/scripts/update_local_rpm_versions --edit target_settings.%(target)s uninstalled' % {'target': targets[k]}) if current_version['mysql_type'] == 'mariadb': # add repo, yum install mariadb pkgs self.install_mariadb(current_version['full']) elif current_version['full'] == 'mysql57': # add repo, yum install mysql57 self.install_mysql57(current_version['full']) # create mysql alias for mysqld service self.mysql_service_symlink() else: # enable current mysql target to rpm management t = targets.get(current_version['full']) if not t: raise RuntimeError('unknown target for RPM management: {}'.format(current_version['full'])) exec_command('/usr/local/cpanel/scripts/update_local_rpm_versions --edit target_settings.%(target)s installed' % {'target': t}) # fix legacy RPMs (works for mysql55 and mysql56 only) if os.path.exists("/scripts/check_cpanel_rpms"): exec_command_out("/scripts/check_cpanel_rpms --fix --targets=" "MySQL50,MySQL51,MySQL55,MySQL56,MariaDB," "MariaDB100,MariaDB101")
except Exception, e: print >> sys.stderr, "Can`t download repo file: %s" % e sys.exit(2) else: if os.path.exists("/etc/yum.repos.d/cl-mysql.repo"): shutil.copy2("/etc/yum.repos.d/cl-mysql.repo", "/etc/yum.repos.d/cl-mysql.repo.bak") write_file("/etc/yum.repos.d/cl-mysql.repo", content) # update repositories exec_command_out("yum clean all") # Add requires to packages list for name in requires: # query only for non-installed packages packages += exec_command("repoquery --requires %s" % name) # query for installed package # exec_command("rpm -q --requires cl-MySQL-meta") if not download_packages(packages, folder, beta): self.ALL_PACKAGES_NEW_NOT_DOWNLOADED = True return packages def get_mysql_user(self): """ Retrieve MySQL user name and password and save it into self attributes """ @staticmethod def _check_mysql_version():
def _load_new_packages(self, beta, sql_version=None, folder="new"): """ detect and download packages for new installation """ print "Start download packages for new installation" # based on sql_version get packages names list and repo name packages, requires = [], [] arch = ".x86_64" if os.uname()[-1] == "x86_64" else "" sql_version = self._get_result_mysql_version(sql_version) if "auto" == sql_version: repo = "mysql-common.repo" if 7 != self.cl_version: packages = [ "mysql", "mysql-server", "mysql-libs", "mysql-devel", "mysql-bench" ] else: packages = [ "mariadb", "mariadb-server", "mariadb-libs", "mariadb-devel", "mariadb-bench" ] # download and install only need arch packages packages = ["%s%s" % (x, arch) for x in packages] for line in exec_command("yum info %s" % packages[0]): if line.startswith("Version"): sql_version = "%s%s" % (packages[0].split('.')[0], "".join( line.split(":")[1].split(".")[:2]).strip()) else: repo = "cl-%s-common.repo" % self.REPO_NAMES.get(sql_version, None) if sql_version in [ "mysql50", "mysql51", "mysql55", "mysql56", "mysql57" ]: packages = [ "cl-MySQL-meta", "cl-MySQL-meta-client", "cl-MySQL-meta-devel" ] requires = list(packages) # if sql_version in ["mysql56", "mysql57"]: # packages.append("libaio%s" % arch) elif sql_version in [ "mariadb55", "mariadb100", "mariadb101", "mariadb102" ]: packages = [ "cl-MariaDB-meta", "cl-MariaDB-meta-client", "cl-MariaDB-meta-devel" ] requires = packages[:3] elif sql_version in ["percona56"]: packages = [ "cl-Percona-meta", "cl-Percona-meta-client", "cl-Percona-meta-devel" ] requires = packages[:3] else: print >> sys.stderr, "Unknown SQL VERSION" sys.exit(2) if sql_version == "mysql50": packages += ["mysqlclient18", "mysqlclient16"] elif sql_version == "mysql51": packages += ["mysqlclient18", "mysqlclient15"] elif sql_version in ["mysql55", "mysql56", "mysql57"]: packages += ["mysqlclient16", "mysqlclient15"] if sql_version in ["mysql57"]: packages += [ "numactl-devel%s" % arch, "numactl%s" % arch, "mysqlclient18" ] elif sql_version.startswith("mariadb"): packages += ["mysqlclient16", "mysqlclient15"] if sql_version == 'mariadb102': packages += ["mysqlclient18-compat"] elif sql_version.startswith("percona"): packages += ["mysqlclient16", "mysqlclient15"] packages.append("libaio%s" % arch) repo_url = "http://repo.cloudlinux.com/other/cl%s/mysqlmeta/%s" % ( self.cl_version, repo) try: content = urllib2.urlopen(repo_url).read() except Exception, e: print >> sys.stderr, "Can`t download repo file: %s" % e sys.exit(2)
def _load_current_packages(self, download=True, folder="old"): """ here we download current installed packages @param `download` bool: download rpm files or only return list of installed packages """ print "Start download current installed packages" PATTERNS = [ "cl-mysql", "cl-mariadb", "cl-percona", "mysql", "mariadb", "compat-mysql5", "Percona" ] mysqld_path = exec_command("which mysqld", True, silent=True) pkg_name = False if mysqld_path: # print "No mysql presents on system" # return None # server package name # pkg_name = exec_command("""rpm -qf --qf "%%{name} # %%{version}\n" %s """ % mysqld_path, True, silent=True) check_if_mysql_installed = exec_command("""rpm -qf %s """ % mysqld_path, True, silent=True, return_code=True) if check_if_mysql_installed == "no": print "No mysql packages installed, " \ "but mysqld file presents on system" pkg_name = None else: pkg_name = exec_command("""rpm -qf %s """ % mysqld_path, True, silent=True) # grep cl-MySQL packages in installed list # packages = exec_command("""rpm -qa --qf "%%{name} # %%{version}\n"|grep -iE "^(%s)" """ % "|".join(PATTERNS), silent=True) packages = exec_command("""rpm -qa|grep -iE "^(%s)" """ % "|".join(PATTERNS), silent=True) if not len(packages): print "No installed DB packages found" return False if pkg_name: found = False for pkg in packages: if pkg.startswith(pkg_name): found = True break if not found: packages.append(pkg_name) # print "Can`t find package with mysqld file" # self._old_packages = packages if download: # arch = ".x86_64" if os.uname()[-1] == "x86_64" else "" # download_pkgs = ["%s%s" % (x.split(" ")[0], arch) # for x in packages] IS_CL_MYSQL = False for package_item in packages: if "server" in package_item and package_item[:3] == "cl-": IS_CL_MYSQL = True if IS_CL_MYSQL == True: if not download_packages(packages, folder, True): self.ALL_PACKAGES_OLD_NOT_DOWNLOADED = True else: if not download_packages(packages, folder, True, self._custom_download_of_rpm): print "Trying to load custom packages from yum" if not download_packages(packages, folder, True): self.ALL_PACKAGES_OLD_NOT_DOWNLOADED = True return packages
def _load_current_packages(self, download=True, folder="old"): """ here we download current installed packages @param `download` bool: download rpm files or only return list of installed packages """ print "Start download current installed packages" PATTERNS = ["cl-mysql", "cl-mariadb", "cl-percona", "mysql", "mariadb", "compat-mysql5", "Percona"] mysqld_path = exec_command("which mysqld", True, silent=True) pkg_name = False if mysqld_path: # print "No mysql presents on system" # return None # server package name # pkg_name = exec_command("""rpm -qf --qf "%%{name} # %%{version}\n" %s """ % mysqld_path, True, silent=True) check_if_mysql_installed = exec_command("""rpm -qf %s """ % mysqld_path, True, silent=True, return_code=True) if check_if_mysql_installed == "no": print "No mysql packages installed, " \ "but mysqld file presents on system" pkg_name = None else: pkg_name = exec_command("""rpm -qf %s """ % mysqld_path, True, silent=True) # grep cl-MySQL packages in installed list # packages = exec_command("""rpm -qa --qf "%%{name} # %%{version}\n"|grep -iE "^(%s)" """ % "|".join(PATTERNS), silent=True) packages = exec_command("""rpm -qa|grep -iE "^(%s)" """ % "|".join(PATTERNS), silent=True) if not len(packages): print "No installed DB packages found" return False if pkg_name: found = False for pkg in packages: if pkg.startswith(pkg_name): found = True break if not found: packages.append(pkg_name) # print "Can`t find package with mysqld file" # self._old_packages = packages if download: # arch = ".x86_64" if os.uname()[-1] == "x86_64" else "" # download_pkgs = ["%s%s" % (x.split(" ")[0], arch) # for x in packages] IS_CL_MYSQL = False for package_item in packages: if "server" in package_item and package_item[:3] == "cl-": IS_CL_MYSQL = True if IS_CL_MYSQL == True: if not download_packages(packages, folder, True): self.ALL_PACKAGES_OLD_NOT_DOWNLOADED = True else: if not download_packages(packages, folder, True, self._custom_download_of_rpm): print "Trying to load custom packages from yum" if not download_packages(packages, folder, True): self.ALL_PACKAGES_OLD_NOT_DOWNLOADED = True return packages
def _load_new_packages(self, beta, sql_version=None, folder="new"): """ detect and download packages for new installation """ print "Start download packages for new installation" # based on sql_version get packages names list and repo name packages, requires = [], [] arch = ".x86_64" if os.uname()[-1] == "x86_64" else "" sql_version = self._get_result_mysql_version(sql_version) if "auto" == sql_version: repo = "mysql-common.repo" if 7 != self.cl_version: packages = ["mysql", "mysql-server", "mysql-libs", "mysql-devel", "mysql-bench"] else: packages = ["mariadb", "mariadb-server", "mariadb-libs", "mariadb-devel", "mariadb-bench"] # download and install only need arch packages packages = ["%s%s" % (x, arch) for x in packages] for line in exec_command("yum info %s" % packages[0]): if line.startswith("Version"): sql_version = "%s%s" % ( packages[0].split('.')[0], "".join(line.split(":")[1].split(".")[:2]).strip()) else: repo = "cl-%s-common.repo" % self.REPO_NAMES.get(sql_version, None) if sql_version in ["mysql50", "mysql51", "mysql55", "mysql56", "mysql57"]: packages = ["cl-MySQL-meta", "cl-MySQL-meta-client", "cl-MySQL-meta-devel"] requires = list(packages) # if sql_version in ["mysql56", "mysql57"]: # packages.append("libaio%s" % arch) elif sql_version in ["mariadb55", "mariadb100", "mariadb101", "mariadb102"]: packages = ["cl-MariaDB-meta", "cl-MariaDB-meta-client", "cl-MariaDB-meta-devel"] requires = packages[:3] elif sql_version in ["percona56"]: packages = ["cl-Percona-meta", "cl-Percona-meta-client", "cl-Percona-meta-devel"] requires = packages[:3] else: print >> sys.stderr, "Unknown SQL VERSION" sys.exit(2) if sql_version == "mysql50": packages += ["mysqlclient18", "mysqlclient16"] elif sql_version == "mysql51": packages += ["mysqlclient18", "mysqlclient15"] elif sql_version in ["mysql55", "mysql56", "mysql57"]: packages += ["mysqlclient16", "mysqlclient15"] if sql_version in ["mysql57"]: packages += ["numactl-devel%s" % arch, "numactl%s" % arch, "mysqlclient18"] elif sql_version.startswith("mariadb"): packages += ["mysqlclient16", "mysqlclient15"] if sql_version == 'mariadb102': packages += ["mysqlclient18-compat"] elif sql_version.startswith("percona"): packages += ["mysqlclient16", "mysqlclient15"] packages.append("libaio%s" % arch) repo_url = "http://repo.cloudlinux.com/other/cl%s/mysqlmeta/%s" % ( self.cl_version, repo) try: content = urllib2.urlopen(repo_url).read() except Exception, e: print >> sys.stderr, "Can`t download repo file: %s" % e sys.exit(2)