2018-06-05 00:53:45 +05:00
|
|
|
import os,sys
|
|
|
|
|
sys.path.append('/usr/local/CyberCP')
|
|
|
|
|
import django
|
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
|
|
|
|
|
django.setup()
|
2017-10-24 19:16:36 +05:00
|
|
|
import CyberCPLogFileWriter as logging
|
|
|
|
|
import subprocess
|
|
|
|
|
import shlex
|
2018-06-05 00:53:45 +05:00
|
|
|
from websiteFunctions.models import Websites
|
|
|
|
|
from databases.models import Databases
|
2018-12-06 15:03:43 +05:00
|
|
|
import MySQLdb as mysql
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class mysqlUtilities:
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2018-12-06 15:03:43 +05:00
|
|
|
def setupConnection():
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
passFile = "/etc/cyberpanel/mysqlPassword"
|
|
|
|
|
|
|
|
|
|
f = open(passFile)
|
|
|
|
|
data = f.read()
|
|
|
|
|
password = data.split('\n', 1)[0]
|
|
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
conn = mysql.connect(user='root', passwd=password)
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
return conn, cursor
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
|
return 0, 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
@staticmethod
|
|
|
|
|
def createDatabase(dbname,dbuser,dbpassword):
|
|
|
|
|
try:
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
connection, cursor = mysqlUtilities.setupConnection()
|
2017-11-05 21:07:12 +05:00
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
if connection == 0:
|
2017-11-05 21:07:12 +05:00
|
|
|
return 0
|
|
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
cursor.execute("CREATE DATABASE " + dbname)
|
|
|
|
|
cursor.execute("CREATE USER '" +dbuser+ "'@'localhost' IDENTIFIED BY '"+dbpassword+"'")
|
|
|
|
|
cursor.execute("GRANT ALL PRIVILEGES ON " +dbname+ ".* TO '" +dbuser+ "'@'localhost'")
|
|
|
|
|
connection.close()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
return 1
|
2017-11-05 21:07:12 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
2018-12-06 15:03:43 +05:00
|
|
|
mysqlUtilities.deleteDatabase(dbname, dbuser)
|
2017-10-24 19:16:36 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabase]")
|
2017-11-05 21:07:12 +05:00
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def deleteDatabase(dbname, dbuser):
|
|
|
|
|
try:
|
|
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
connection, cursor = mysqlUtilities.setupConnection()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
if connection == 0:
|
2017-11-05 21:07:12 +05:00
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
cursor.execute("DROP DATABASE " + dbname)
|
|
|
|
|
cursor.execute("DROP USER '"+dbuser+"'@'localhost'")
|
|
|
|
|
connection.close()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
return 1
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
except BaseException, msg:
|
2017-11-05 21:07:12 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[deleteDatabase]")
|
2017-10-24 19:16:36 +05:00
|
|
|
return str(msg)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def createDatabaseBackup(databaseName,tempStoragePath):
|
|
|
|
|
try:
|
|
|
|
|
passFile = "/etc/cyberpanel/mysqlPassword"
|
|
|
|
|
|
|
|
|
|
f = open(passFile)
|
|
|
|
|
data = f.read()
|
|
|
|
|
password = data.split('\n', 1)[0]
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
command = 'sudo mysqldump -u root -p'+password+' '+databaseName
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
|
|
|
|
|
with open(tempStoragePath+"/"+databaseName+'.sql', 'w') as f:
|
|
|
|
|
res = subprocess.call(cmd,stdout=f)
|
|
|
|
|
|
|
|
|
|
if res == 1:
|
2018-02-16 00:57:46 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Database: "+databaseName + "could not be backed! [createDatabaseBackup]")
|
2018-02-16 21:05:15 +05:00
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
except BaseException, msg:
|
2018-12-06 15:03:43 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabaseBackup]")
|
2018-02-16 21:05:15 +05:00
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def restoreDatabaseBackup(databaseName, tempStoragePath,dbPassword):
|
|
|
|
|
try:
|
|
|
|
|
passFile = "/etc/cyberpanel/mysqlPassword"
|
|
|
|
|
|
|
|
|
|
f = open(passFile)
|
|
|
|
|
data = f.read()
|
|
|
|
|
password = data.split('\n', 1)[0]
|
|
|
|
|
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
command = 'sudo mysql -u root -p' + password + ' ' + databaseName
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open(tempStoragePath + "/" + databaseName + '.sql', 'r') as f:
|
|
|
|
|
res = subprocess.call(cmd, stdin=f)
|
|
|
|
|
|
|
|
|
|
if res == 1:
|
2018-02-16 00:57:46 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Could not restore MYSQL database: " +databaseName +"! [restoreDatabaseBackup]")
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
|
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
connection, cursor = mysqlUtilities.setupConnection()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
if connection == 0:
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
|
|
|
|
|
2018-12-06 15:03:43 +05:00
|
|
|
passwordCMD = "use mysql;SET PASSWORD FOR '" + databaseName + "'@'localhost' = '" + dbPassword + "';FLUSH PRIVILEGES;"
|
|
|
|
|
|
|
|
|
|
cursor.execute(passwordCMD)
|
|
|
|
|
connection.close()
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
return 1
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[restoreDatabaseBackup]")
|
2018-06-05 00:53:45 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def submitDBCreation(dbName, dbUsername, dbPassword, databaseWebsite):
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
if len(dbName) > 16 or len(dbUsername) > 16:
|
|
|
|
|
raise BaseException("Length of Database name or Database user should be 16 at max.")
|
|
|
|
|
|
|
|
|
|
website = Websites.objects.get(domain=databaseWebsite)
|
|
|
|
|
|
|
|
|
|
if website.package.dataBases == 0:
|
|
|
|
|
pass
|
|
|
|
|
elif website.package.dataBases > website.databases_set.all().count():
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise BaseException("Maximum database limit reached for this website.")
|
|
|
|
|
|
|
|
|
|
if Databases.objects.filter(dbName=dbName).exists() or Databases.objects.filter(dbUser=dbUsername).exists():
|
|
|
|
|
raise BaseException("This database or user is already taken.")
|
|
|
|
|
|
|
|
|
|
result = mysqlUtilities.createDatabase(dbName, dbUsername, dbPassword)
|
|
|
|
|
|
|
|
|
|
if result == 1:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise BaseException(result)
|
|
|
|
|
|
|
|
|
|
db = Databases(website=website, dbName=dbName, dbUser=dbUsername)
|
|
|
|
|
db.save()
|
|
|
|
|
|
|
|
|
|
return 1,'None'
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
|
return 0,str(msg)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def submitDBDeletion(dbName):
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
databaseToBeDeleted = Databases.objects.get(dbName=dbName)
|
|
|
|
|
result = mysqlUtilities.deleteDatabase(dbName, databaseToBeDeleted.dbUser)
|
|
|
|
|
|
|
|
|
|
if result == 1:
|
|
|
|
|
databaseToBeDeleted.delete()
|
|
|
|
|
return 1,'None'
|
|
|
|
|
else:
|
|
|
|
|
return 0,result
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
|
return 0, str(msg)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def getDatabases(virtualHostName):
|
|
|
|
|
try:
|
|
|
|
|
website = Websites.objects.get(domain=virtualHostName)
|
|
|
|
|
return website.databases_set.all()
|
|
|
|
|
except:
|
|
|
|
|
0
|