finish initial stage of remote mysql

This commit is contained in:
Usman Nasir
2020-06-26 18:22:20 +05:00
parent f33e33850f
commit c345270585
3 changed files with 162 additions and 102 deletions

View File

@@ -95,13 +95,18 @@ class preFlightsChecks:
cyberPanelMirror = "mirror.cyberpanel.net/pip" cyberPanelMirror = "mirror.cyberpanel.net/pip"
cdn = 'cyberpanel.sh' 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.ipAddr = ip
self.path = path self.path = path
self.cwd = cwd self.cwd = cwd
self.server_root_path = rootPath self.server_root_path = rootPath
self.cyberPanelPath = cyberPanelPath self.cyberPanelPath = cyberPanelPath
self.distro = distro self.distro = distro
self.remotemysql = remotemysql
self.mysqlhost = mysqlhost
self.mysqluser = mysqluser
self.mysqlpassword = mysqlpassword
self.mysqlport = mysqlport
@staticmethod @staticmethod
def stdOut(message, log=0, do_exit=0, code=os.EX_OK): def stdOut(message, log=0, do_exit=0, code=os.EX_OK):
@@ -373,11 +378,15 @@ class preFlightsChecks:
### update password: ### update password:
if self.remotemysql == 'OFF':
passFile = "/etc/cyberpanel/mysqlPassword" passFile = "/etc/cyberpanel/mysqlPassword"
f = open(passFile) f = open(passFile)
data = f.read() data = f.read()
password = data.split('\n', 1)[0] password = data.split('\n', 1)[0]
else:
password = self.mysqlpassword
### Put correct mysql passwords in settings file! ### Put correct mysql passwords in settings file!
@@ -396,6 +405,7 @@ class preFlightsChecks:
SK = "SECRET_KEY = '%s'\n" % (generate_pass(50)) SK = "SECRET_KEY = '%s'\n" % (generate_pass(50))
writeDataToFile.writelines(SK) writeDataToFile.writelines(SK)
continue continue
if mysql == 'Two': if mysql == 'Two':
if items.find("'PASSWORD':") > -1: if items.find("'PASSWORD':") > -1:
if counter == 0: if counter == 0:
@@ -411,7 +421,6 @@ class preFlightsChecks:
if counter == 0: if counter == 0:
writeDataToFile.writelines(" 'PASSWORD': '" + mysqlPassword + "'," + "\n") writeDataToFile.writelines(" 'PASSWORD': '" + mysqlPassword + "'," + "\n")
counter = counter + 1 counter = counter + 1
else: else:
writeDataToFile.writelines(" 'PASSWORD': '" + password + "'," + "\n") writeDataToFile.writelines(" 'PASSWORD': '" + password + "'," + "\n")
elif items.find('127.0.0.1') > -1: elif items.find('127.0.0.1') > -1:
@@ -421,6 +430,17 @@ class preFlightsChecks:
else: else:
writeDataToFile.writelines(items) 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: if self.distro == ubuntu:
os.fchmod(writeDataToFile.fileno(), stat.S_IRUSR | stat.S_IWUSR) 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('--serial', help='Install LS Ent or OpenLiteSpeed')
parser.add_argument('--port', help='LSCPD Port') parser.add_argument('--port', help='LSCPD Port')
parser.add_argument('--redis', help='vHosts on Redis - Requires LiteSpeed Enterprise') 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() args = parser.parse_args()
logging.InstallLog.writeToFile("Starting CyberPanel installation..") logging.InstallLog.writeToFile("Starting CyberPanel installation..")
@@ -2135,12 +2160,22 @@ def main():
cwd = os.getcwd() cwd = os.getcwd()
distro = get_distro() if args.remotemsql == 'ON':
checks = preFlightsChecks("/usr/local/lsws/", args.publicip, "/usr/local", cwd, "/usr/local/CyberCP", distro) remotemysql = args.remotemysql
checks.mountTemp() mysqlhost = args.mysqlhost
mysqluser = args.mysqluser
mysqlpassword = args.mysqlpassword
mysqlport = args.mysqlport
else:
remotemysql = args.remotemysql
mysqlhost = ''
mysqluser = ''
mysqlpassword = ''
mysqlport = ''
# if distro == ubuntu: distro = get_distro()
# os.chdir("/etc/cyberpanel") 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: if args.port == None:
port = "8090" port = "8090"
@@ -2160,10 +2195,11 @@ def main():
import installCyberPanel import installCyberPanel
if ent == 0: 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: 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.setupPHPAndComposer()
checks.fix_selinux_issue() checks.fix_selinux_issue()

View File

@@ -38,7 +38,7 @@ class InstallCyberPanel:
mysql_Root_password = "" mysql_Root_password = ""
mysqlPassword = "" 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.server_root_path = rootPath
self.cwd = cwd self.cwd = cwd
self.distro = distro self.distro = distro
@@ -48,6 +48,11 @@ class InstallCyberPanel:
self.ftp = None self.ftp = None
self.dns = dns self.dns = dns
self.publicip = publicip self.publicip = publicip
self.remotemysql = remotemysql
self.mysqlhost = mysqlhost
self.mysqluser = mysqluser
self.mysqlpassword = mysqlpassword
self.mysqlport = mysqlport
@staticmethod @staticmethod
def stdOut(message, log=0, exit=0, code=os.EX_OK): def stdOut(message, log=0, exit=0, code=os.EX_OK):
@@ -230,7 +235,7 @@ class InstallCyberPanel:
subprocess.call(command, shell=True) subprocess.call(command, shell=True)
def installMySQL(self, mysql): def installMySQL(self, mysql):
if self.remotemysql == 'OFF':
############## Install mariadb ###################### ############## Install mariadb ######################
if self.distro == ubuntu: if self.distro == ubuntu:
@@ -289,6 +294,7 @@ class InstallCyberPanel:
self.startMariaDB() self.startMariaDB()
def changeMYSQLRootPassword(self): def changeMYSQLRootPassword(self):
if self.remotemysql == 'OFF':
if self.distro == ubuntu: 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;" 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: else:
@@ -601,12 +607,14 @@ class InstallCyberPanel:
install.preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) 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.mysqlPassword = randomPassword.generate_pass()
InstallCyberPanel.mysql_Root_password = randomPassword.generate_pass() InstallCyberPanel.mysql_Root_password = randomPassword.generate_pass()
file_name = '/etc/cyberpanel/mysqlPassword' file_name = '/etc/cyberpanel/mysqlPassword'
if remotemysql == 'OFF':
if os.access(file_name, os.F_OK): if os.access(file_name, os.F_OK):
password = open(file_name, 'r') password = open(file_name, 'r')
InstallCyberPanel.mysql_Root_password = password.readline() InstallCyberPanel.mysql_Root_password = password.readline()
@@ -614,9 +622,17 @@ def Main(cwd, mysql, distro, ent, serial = None, port = "8090", ftp = None, dns
else: else:
password = open(file_name, "w") password = open(file_name, "w")
password.writelines(InstallCyberPanel.mysql_Root_password) password.writelines(InstallCyberPanel.mysql_Root_password)
command = 'chmod 640 %s' % (file_name)
password.close() password.close()
else:
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: try:
command = 'chmod 640 %s' % (file_name)
install.preFlightsChecks.call(command, distro, '[chmod]', install.preFlightsChecks.call(command, distro, '[chmod]',
'', '',
1, 0, os.EX_OSERR) 1, 0, os.EX_OSERR)
@@ -627,15 +643,12 @@ def Main(cwd, mysql, distro, ent, serial = None, port = "8090", ftp = None, dns
except: except:
pass pass
if distro == centos: if distro == centos:
InstallCyberPanel.mysqlPassword = randomPassword.generate_pass() InstallCyberPanel.mysqlPassword = randomPassword.generate_pass()
else: else:
InstallCyberPanel.mysqlPassword = InstallCyberPanel.mysql_Root_password 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() installer.installLiteSpeed()
if ent == 0: if ent == 0:
@@ -646,9 +659,10 @@ def Main(cwd, mysql, distro, ent, serial = None, port = "8090", ftp = None, dns
installer.installMySQL(mysql) installer.installMySQL(mysql)
installer.changeMYSQLRootPassword() installer.changeMYSQLRootPassword()
#installer.changeMYSQLRootPasswordCyberPanel(mysql)
installer.startMariaDB() installer.startMariaDB()
if remotemysql == 'OFF':
if distro == ubuntu: if distro == ubuntu:
installer.fixMariaDB() installer.fixMariaDB()

View File

@@ -7,6 +7,14 @@ class mysqlUtilities:
try: try:
try:
from json import loads
mysqlData = loads(open("/etc/cyberpanel/mysqlPassword", 'r').read())
createDB = "CREATE DATABASE " + dbname
initCommand = 'mysql -h %s --port %s -u %s -p%s -e "' % (mysqlData['mysqlhost'], mysqlData['mysqlport'], mysqlData['mysqluser'], mysqlData['mysqlpassword'])
except:
passFile = "/etc/cyberpanel/mysqlPassword" passFile = "/etc/cyberpanel/mysqlPassword"
f = open(passFile) f = open(passFile)
@@ -14,8 +22,10 @@ class mysqlUtilities:
password = data.split('\n', 1)[0] password = data.split('\n', 1)[0]
createDB = "CREATE DATABASE " + dbname 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) cmd = shlex.split(command)
res = subprocess.call(cmd) res = subprocess.call(cmd)
@@ -24,7 +34,7 @@ class mysqlUtilities:
createUser = "CREATE USER '" + dbuser + "'@'localhost' IDENTIFIED BY '" + dbpassword + "'" createUser = "CREATE USER '" + dbuser + "'@'localhost' IDENTIFIED BY '" + dbpassword + "'"
command = 'mysql -u root -p' + password + ' -e "' + createUser + '"' command = initCommand + createUser + '"'
cmd = shlex.split(command) cmd = shlex.split(command)
res = subprocess.call(cmd) res = subprocess.call(cmd)
@@ -33,7 +43,7 @@ class mysqlUtilities:
return 0 return 0
else: else:
dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'localhost'" dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'localhost'"
command = 'mysql -u root -p' + password + ' -e "' + dropDB + '"' command = initCommand + dropDB + '"'
cmd = shlex.split(command) cmd = shlex.split(command)
res = subprocess.call(cmd) res = subprocess.call(cmd)