Files
CyberPanel/install/mysqlUtilities.py

78 lines
2.3 KiB
Python
Raw Normal View History

import subprocess, shlex
2020-07-05 13:26:46 +05:00
import install
import time
2017-10-24 19:16:36 +05:00
class mysqlUtilities:
@staticmethod
2020-07-05 21:04:38 +05:00
def createDatabase(dbname, dbuser, dbpassword, publicip):
2017-10-24 19:16:36 +05:00
try:
2020-07-05 13:26:46 +05:00
createDB = "CREATE DATABASE " + dbname
2017-10-24 19:16:36 +05:00
2020-06-26 18:22:20 +05:00
try:
from json import loads
mysqlData = loads(open("/etc/cyberpanel/mysqlPassword", 'r').read())
2017-10-24 19:16:36 +05:00
2020-07-05 13:26:46 +05:00
2020-06-26 18:22:20 +05:00
initCommand = 'mysql -h %s --port %s -u %s -p%s -e "' % (mysqlData['mysqlhost'], mysqlData['mysqlport'], mysqlData['mysqluser'], mysqlData['mysqlpassword'])
2020-07-05 21:04:38 +05:00
remote = 1
2020-06-26 18:22:20 +05:00
except:
passFile = "/etc/cyberpanel/mysqlPassword"
f = open(passFile)
data = f.read()
password = data.split('\n', 1)[0]
initCommand = 'mysql -u root -p' + password + ' -e "'
2020-07-05 21:04:38 +05:00
remote = 0
2020-06-26 18:22:20 +05:00
command = initCommand + createDB + '"'
2017-10-24 19:16:36 +05:00
2020-07-05 13:26:46 +05:00
if install.preFlightsChecks.debug:
print(command)
time.sleep(10)
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-10-24 19:16:36 +05:00
if res == 1:
return 0
2017-10-24 19:16:36 +05:00
2020-07-05 21:04:38 +05:00
if remote:
createUser = "CREATE USER '" + dbuser + "'@'%s' IDENTIFIED BY '" % (publicip) + dbpassword + "'"
else:
createUser = "CREATE USER '" + dbuser + "'@'localhost' IDENTIFIED BY '" + dbpassword + "'"
2017-10-24 19:16:36 +05:00
2020-06-26 18:22:20 +05:00
command = initCommand + createUser + '"'
2017-10-24 19:16:36 +05:00
2020-07-05 13:26:46 +05:00
if install.preFlightsChecks.debug:
print(command)
time.sleep(10)
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-10-24 19:16:36 +05:00
if res == 1:
return 0
2017-10-24 19:16:36 +05:00
else:
2020-07-05 21:04:38 +05:00
if remote:
dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'%s'" % (publicip)
else:
dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'localhost'"
2020-07-06 11:28:41 +05:00
2020-06-26 18:22:20 +05:00
command = initCommand + dropDB + '"'
2020-07-05 13:26:46 +05:00
if install.preFlightsChecks.debug:
print(command)
time.sleep(10)
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-10-24 19:16:36 +05:00
if res == 1:
return 0
2017-10-24 19:16:36 +05:00
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
return 0