diff --git a/install/install.py b/install/install.py index 7652947f7..c2c8fe6cc 100755 --- a/install/install.py +++ b/install/install.py @@ -95,13 +95,18 @@ class preFlightsChecks: cyberPanelMirror = "mirror.cyberpanel.net/pip" cdn = 'cyberpanel.sh' - def __init__(self, rootPath, ip, path, cwd, cyberPanelPath, distro): + def __init__(self, rootPath, ip, path, cwd, cyberPanelPath, distro, remotemysql = None , mysqlhost = None, mysqluser = None, mysqlpassword = None, mysqlport = None): self.ipAddr = ip self.path = path self.cwd = cwd self.server_root_path = rootPath self.cyberPanelPath = cyberPanelPath self.distro = distro + self.remotemysql = remotemysql + self.mysqlhost = mysqlhost + self.mysqluser = mysqluser + self.mysqlpassword = mysqlpassword + self.mysqlport = mysqlport @staticmethod def stdOut(message, log=0, do_exit=0, code=os.EX_OK): @@ -373,11 +378,15 @@ class preFlightsChecks: ### update password: - passFile = "/etc/cyberpanel/mysqlPassword" + if self.remotemysql == 'OFF': - f = open(passFile) - data = f.read() - password = data.split('\n', 1)[0] + passFile = "/etc/cyberpanel/mysqlPassword" + + f = open(passFile) + data = f.read() + password = data.split('\n', 1)[0] + else: + password = self.mysqlpassword ### Put correct mysql passwords in settings file! @@ -396,6 +405,7 @@ class preFlightsChecks: SK = "SECRET_KEY = '%s'\n" % (generate_pass(50)) writeDataToFile.writelines(SK) continue + if mysql == 'Two': if items.find("'PASSWORD':") > -1: if counter == 0: @@ -411,7 +421,6 @@ class preFlightsChecks: if counter == 0: writeDataToFile.writelines(" 'PASSWORD': '" + mysqlPassword + "'," + "\n") counter = counter + 1 - else: writeDataToFile.writelines(" 'PASSWORD': '" + password + "'," + "\n") elif items.find('127.0.0.1') > -1: @@ -421,6 +430,17 @@ class preFlightsChecks: else: writeDataToFile.writelines(items) + + if self.remotemysql == 'ON': + command = "sed -i 's|'HOST': 'localhost',|'HOST': '%s',|g' %s" % (self.mysqlhost, path) + preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) + + command = "sed -i 's|'USER': 'root',|'USER': '%s',|g' %s" % (self.mysqluser, path) + preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) + + command = "sed -i 's|'PORT':''|'PORT':'%s'|g' %s" % (self.mysqlport, path) + preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) + if self.distro == ubuntu: os.fchmod(writeDataToFile.fileno(), stat.S_IRUSR | stat.S_IWUSR) @@ -2100,6 +2120,11 @@ def main(): parser.add_argument('--serial', help='Install LS Ent or OpenLiteSpeed') parser.add_argument('--port', help='LSCPD Port') parser.add_argument('--redis', help='vHosts on Redis - Requires LiteSpeed Enterprise') + parser.add_argument('--remotemysql', help='Opt to choose local or remote MySQL') + parser.add_argument('--mysqlhost', help='MySQL host if remote is chosen.') + parser.add_argument('--mysqluser', help='MySQL user if remote is chosen.') + parser.add_argument('--mysqlpassword', help='MySQL password if remote is chosen.') + parser.add_argument('--mysqlport', help='MySQL port if remote is chosen.') args = parser.parse_args() logging.InstallLog.writeToFile("Starting CyberPanel installation..") @@ -2135,12 +2160,22 @@ def main(): cwd = os.getcwd() - distro = get_distro() - checks = preFlightsChecks("/usr/local/lsws/", args.publicip, "/usr/local", cwd, "/usr/local/CyberCP", distro) - checks.mountTemp() + if args.remotemsql == 'ON': + remotemysql = args.remotemysql + mysqlhost = args.mysqlhost + mysqluser = args.mysqluser + mysqlpassword = args.mysqlpassword + mysqlport = args.mysqlport + else: + remotemysql = args.remotemysql + mysqlhost = '' + mysqluser = '' + mysqlpassword = '' + mysqlport = '' -# if distro == ubuntu: -# os.chdir("/etc/cyberpanel") + distro = get_distro() + checks = preFlightsChecks("/usr/local/lsws/", args.publicip, "/usr/local", cwd, "/usr/local/CyberCP", distro, remotemysql, mysqlhost, mysqluser, mysqlpassword, mysqlport) + checks.mountTemp() if args.port == None: port = "8090" @@ -2160,10 +2195,11 @@ def main(): import installCyberPanel + if ent == 0: - installCyberPanel.Main(cwd, mysql, distro, ent, None, port, args.ftp, args.powerdns, args.publicip) + installCyberPanel.Main(cwd, mysql, distro, ent, None, port, args.ftp, args.powerdns, args.publicip, remotemysql, mysqlhost, mysqluser, mysqlpassword, mysqlport) else: - installCyberPanel.Main(cwd, mysql, distro, ent, serial, port, args.ftp, args.powerdns, args.publicip) + installCyberPanel.Main(cwd, mysql, distro, ent, serial, port, args.ftp, args.powerdns, args.publicip, remotemysql, mysqlhost, mysqluser, mysqlpassword, mysqlport) checks.setupPHPAndComposer() checks.fix_selinux_issue() diff --git a/install/installCyberPanel.py b/install/installCyberPanel.py index 3b3c50991..76777c2e2 100755 --- a/install/installCyberPanel.py +++ b/install/installCyberPanel.py @@ -38,7 +38,7 @@ class InstallCyberPanel: mysql_Root_password = "" mysqlPassword = "" - def __init__(self, rootPath, cwd, distro, ent, serial = None, port = None, ftp = None, dns = None, publicip = None): + def __init__(self, rootPath, cwd, distro, ent, serial = None, port = None, ftp = None, dns = None, publicip = None, remotemysql = None , mysqlhost = None, mysqluser = None, mysqlpassword = None, mysqlport = None): self.server_root_path = rootPath self.cwd = cwd self.distro = distro @@ -48,6 +48,11 @@ class InstallCyberPanel: self.ftp = None self.dns = dns self.publicip = publicip + self.remotemysql = remotemysql + self.mysqlhost = mysqlhost + self.mysqluser = mysqluser + self.mysqlpassword = mysqlpassword + self.mysqlport = mysqlport @staticmethod def stdOut(message, log=0, exit=0, code=os.EX_OK): @@ -230,73 +235,74 @@ class InstallCyberPanel: subprocess.call(command, shell=True) def installMySQL(self, mysql): + if self.remotemysql == 'OFF': + ############## Install mariadb ###################### - ############## Install mariadb ###################### + if self.distro == ubuntu: + command = "apt-get -y install mariadb-server" + elif self.distro == centos: + command = 'yum --enablerepo=CyberPanel -y install mariadb-server' + elif self.distro == cent8: + command = 'dnf -y install mariadb-server' - if self.distro == ubuntu: - command = "apt-get -y install mariadb-server" - elif self.distro == centos: - command = 'yum --enablerepo=CyberPanel -y install mariadb-server' - elif self.distro == cent8: - command = 'dnf -y install mariadb-server' - - install.preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) - - ## Fix configurations if two MYSQL are used - - if mysql == 'Two': - logging.InstallLog.writeToFile("Setting up MariaDB configurations!") - InstallCyberPanel.stdOut("Setting up MariaDB configurations!") - - pathConf = "/etc/my.cnf" - pathServiceFile = "/etc/systemd/system/mysqld@.service" - - if os.path.exists(pathConf): - os.remove(pathConf) - - if os.path.exists(pathServiceFile): - os.remove(pathServiceFile) - - os.chdir(self.cwd) - - shutil.copy("mysql/my.cnf", pathConf) - shutil.copy("mysql/mysqld@.service", pathServiceFile) - - logging.InstallLog.writeToFile("MariaDB configurations set!") - InstallCyberPanel.stdOut("MariaDB configurations set!") - - ## - - command = "mysql_install_db --user=mysql --datadir=/var/lib/mysql1" - install.preFlightsChecks.call(command, self.distro, '[installMySQL]', - 'Install MySQL', - 1, 1, os.EX_OSERR) - - - ## - - command = "systemctl start mysqld@1" install.preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) - ## + ## Fix configurations if two MYSQL are used - command = "systemctl enable mysqld@1" - install.preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) + if mysql == 'Two': + logging.InstallLog.writeToFile("Setting up MariaDB configurations!") + InstallCyberPanel.stdOut("Setting up MariaDB configurations!") + + pathConf = "/etc/my.cnf" + pathServiceFile = "/etc/systemd/system/mysqld@.service" + + if os.path.exists(pathConf): + os.remove(pathConf) + + if os.path.exists(pathServiceFile): + os.remove(pathServiceFile) + + os.chdir(self.cwd) + + shutil.copy("mysql/my.cnf", pathConf) + shutil.copy("mysql/mysqld@.service", pathServiceFile) + + logging.InstallLog.writeToFile("MariaDB configurations set!") + InstallCyberPanel.stdOut("MariaDB configurations set!") + + ## + + command = "mysql_install_db --user=mysql --datadir=/var/lib/mysql1" + install.preFlightsChecks.call(command, self.distro, '[installMySQL]', + 'Install MySQL', + 1, 1, os.EX_OSERR) - ############## Start mariadb ###################### + ## - self.startMariaDB() + command = "systemctl start mysqld@1" + install.preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) + + ## + + command = "systemctl enable mysqld@1" + install.preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) + + + ############## Start mariadb ###################### + + self.startMariaDB() def changeMYSQLRootPassword(self): - if self.distro == ubuntu: - passwordCMD = "use mysql;update user set password=PASSWORD('" + InstallCyberPanel.mysql_Root_password + "') where User='root';UPDATE user SET plugin='' WHERE User='root';flush privileges;" - else: - passwordCMD = "use mysql;update user set password=PASSWORD('" + InstallCyberPanel.mysql_Root_password + "') where User='root';flush privileges;" + if self.remotemysql == 'OFF': + if self.distro == ubuntu: + passwordCMD = "use mysql;update user set password=PASSWORD('" + InstallCyberPanel.mysql_Root_password + "') where User='root';UPDATE user SET plugin='' WHERE User='root';flush privileges;" + else: + passwordCMD = "use mysql;update user set password=PASSWORD('" + InstallCyberPanel.mysql_Root_password + "') where User='root';flush privileges;" - command = 'mysql -u root -e "' + passwordCMD + '"' + command = 'mysql -u root -e "' + passwordCMD + '"' - install.preFlightsChecks.call(command, self.distro, command, command, 0, 0, os.EX_OSERR) + install.preFlightsChecks.call(command, self.distro, command, command, 0, 0, os.EX_OSERR) def startMariaDB(self): @@ -601,41 +607,48 @@ class InstallCyberPanel: install.preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) -def Main(cwd, mysql, distro, ent, serial = None, port = "8090", ftp = None, dns = None, publicip = None): +def Main(cwd, mysql, distro, ent, serial = None, port = "8090", ftp = None, dns = None, publicip = None, remotemysql = None , mysqlhost = None, mysqluser = None, mysqlpassword = None, mysqlport = None): InstallCyberPanel.mysqlPassword = randomPassword.generate_pass() InstallCyberPanel.mysql_Root_password = randomPassword.generate_pass() file_name = '/etc/cyberpanel/mysqlPassword' - if os.access(file_name, os.F_OK): - password = open(file_name, 'r') - InstallCyberPanel.mysql_Root_password = password.readline() - password.close() + + if remotemysql == 'OFF': + if os.access(file_name, os.F_OK): + password = open(file_name, 'r') + InstallCyberPanel.mysql_Root_password = password.readline() + password.close() + else: + password = open(file_name, "w") + password.writelines(InstallCyberPanel.mysql_Root_password) + password.close() else: - password = open(file_name, "w") - password.writelines(InstallCyberPanel.mysql_Root_password) + mysqlData = {'remotemysql': remotemysql, 'mysqlhost': mysqlhost, 'mysqluser': mysqluser, 'mysqlpassword': mysqlpassword, 'mysqlport': mysqlport} + from json import dumps + writeToFile = open(file_name, 'w') + writeToFile.write(dumps(mysqlData)) + writeToFile.close() + + + try: command = 'chmod 640 %s' % (file_name) - password.close() - try: - install.preFlightsChecks.call(command, distro, '[chmod]', - '', - 1, 0, os.EX_OSERR) - command = 'chown root:cyberpanel %s' % (file_name) - install.preFlightsChecks.call(command, distro, '[chmod]', - '', - 1, 0, os.EX_OSERR) - except: - pass - - - + install.preFlightsChecks.call(command, distro, '[chmod]', + '', + 1, 0, os.EX_OSERR) + command = 'chown root:cyberpanel %s' % (file_name) + install.preFlightsChecks.call(command, distro, '[chmod]', + '', + 1, 0, os.EX_OSERR) + except: + pass if distro == centos: InstallCyberPanel.mysqlPassword = randomPassword.generate_pass() else: InstallCyberPanel.mysqlPassword = InstallCyberPanel.mysql_Root_password - installer = InstallCyberPanel("/usr/local/lsws/",cwd, distro, ent, serial, port, ftp, dns, publicip) + installer = InstallCyberPanel("/usr/local/lsws/",cwd, distro, ent, serial, port, ftp, dns, publicip, remotemysql, mysqlhost, mysqluser, mysqlpassword, mysqlport) installer.installLiteSpeed() if ent == 0: @@ -646,11 +659,12 @@ def Main(cwd, mysql, distro, ent, serial = None, port = "8090", ftp = None, dns installer.installMySQL(mysql) installer.changeMYSQLRootPassword() - #installer.changeMYSQLRootPasswordCyberPanel(mysql) installer.startMariaDB() - if distro == ubuntu: - installer.fixMariaDB() + + if remotemysql == 'OFF': + if distro == ubuntu: + installer.fixMariaDB() mysqlUtilities.createDatabase("cyberpanel","cyberpanel",InstallCyberPanel.mysqlPassword) diff --git a/install/mysqlUtilities.py b/install/mysqlUtilities.py index 9ed065680..f8eb6c7de 100755 --- a/install/mysqlUtilities.py +++ b/install/mysqlUtilities.py @@ -7,15 +7,25 @@ class mysqlUtilities: try: - passFile = "/etc/cyberpanel/mysqlPassword" + try: + from json import loads + mysqlData = loads(open("/etc/cyberpanel/mysqlPassword", 'r').read()) - f = open(passFile) - data = f.read() - password = data.split('\n', 1)[0] + createDB = "CREATE DATABASE " + dbname + initCommand = 'mysql -h %s --port %s -u %s -p%s -e "' % (mysqlData['mysqlhost'], mysqlData['mysqlport'], mysqlData['mysqluser'], mysqlData['mysqlpassword']) - createDB = "CREATE DATABASE " + dbname + except: + passFile = "/etc/cyberpanel/mysqlPassword" + + f = open(passFile) + data = f.read() + password = data.split('\n', 1)[0] + + createDB = "CREATE DATABASE " + dbname + initCommand = 'mysql -u root -p' + password + ' -e "' + + command = initCommand + createDB + '"' - command = 'mysql -u root -p' + password + ' -e "' + createDB + '"' cmd = shlex.split(command) res = subprocess.call(cmd) @@ -24,7 +34,7 @@ class mysqlUtilities: createUser = "CREATE USER '" + dbuser + "'@'localhost' IDENTIFIED BY '" + dbpassword + "'" - command = 'mysql -u root -p' + password + ' -e "' + createUser + '"' + command = initCommand + createUser + '"' cmd = shlex.split(command) res = subprocess.call(cmd) @@ -33,7 +43,7 @@ class mysqlUtilities: return 0 else: dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'localhost'" - command = 'mysql -u root -p' + password + ' -e "' + dropDB + '"' + command = initCommand + dropDB + '"' cmd = shlex.split(command) res = subprocess.call(cmd)