bug fix: cm

This commit is contained in:
Usman Nasir
2021-04-01 11:47:47 +05:00
parent d2c141a40f
commit a5b9a68955
3 changed files with 246 additions and 63 deletions

View File

@@ -1,3 +1,4 @@
import sys
import userManagment.views as um import userManagment.views as um
from backup.backupManager import BackupManager from backup.backupManager import BackupManager
from databases.databaseManager import DatabaseManager from databases.databaseManager import DatabaseManager
@@ -21,7 +22,8 @@ from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
from managePHP.phpManager import PHPManager from managePHP.phpManager import PHPManager
from managePHP.views import submitExtensionRequest, getRequestStatusApache from managePHP.views import submitExtensionRequest, getRequestStatusApache
from containerization.views import * from containerization.views import *
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
class CloudManager: class CloudManager:
def __init__(self, data=None, admin=None): def __init__(self, data=None, admin=None):
@@ -2683,7 +2685,7 @@ class CloudManager:
type = self.data['type'] type = self.data['type']
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function %s --type %s" % ('DetachCluster', type) execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function %s --type %s" % ('DetachCluster', type)
ProcessUtilities.popenExecutioner(execPath) ProcessUtilities.executioner(execPath)
final_json = json.dumps({'status': 1}) final_json = json.dumps({'status': 1})
return HttpResponse(final_json) return HttpResponse(final_json)
@@ -2701,9 +2703,103 @@ class CloudManager:
writeToFile.write(json.dumps(self.data)) writeToFile.write(json.dumps(self.data))
writeToFile.close() writeToFile.close()
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function %s --type %s" % ( execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function SetupCluster --type %s" % (self.data['type'])
'SetupCluster', self.data['type']) ProcessUtilities.executioner(execPath)
ProcessUtilities.popenExecutioner(execPath)
final_json = json.dumps({'status': 1})
return HttpResponse(final_json)
except BaseException as msg:
final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
def FetchMasterBootStrapStatus(self):
try:
from CyberCP import settings
data = {}
data['status'] = 1
## CyberPanel DB Creds
data['dbName'] = settings.DATABASES['default']['NAME']
data['dbUser'] = settings.DATABASES['default']['USER']
data['password'] = settings.DATABASES['default']['PASSWORD']
data['host'] = settings.DATABASES['default']['HOST']
data['port'] = settings.DATABASES['default']['PORT']
## Root DB Creds
data['rootdbName'] = settings.DATABASES['rootdb']['NAME']
data['rootdbdbUser'] = settings.DATABASES['rootdb']['USER']
data['rootdbpassword'] = settings.DATABASES['rootdb']['PASSWORD']
command = 'cat /var/lib/mysql/grastate.dat'
output = ProcessUtilities.outputExecutioner(command)
if output.find('No such file or directory') > -1:
data['safe'] = 1
elif output.find('safe_to_bootstrap: 1') > -1:
data['safe'] = 1
else:
data['safe'] = 0
final_json = json.dumps(data)
return HttpResponse(final_json)
except BaseException as msg:
final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
def FetchChildBootStrapStatus(self):
try:
data = {}
data['status'] = 1
command = 'cat /var/lib/mysql/grastate.dat'
output = ProcessUtilities.outputExecutioner(command)
if output.find('No such file or directory') > -1:
data['safe'] = 1
elif output.find('safe_to_bootstrap: 0') > -1:
data['safe'] = 1
else:
data['safe'] = 0
final_json = json.dumps(data)
return HttpResponse(final_json)
except BaseException as msg:
final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
def BootMaster(self):
try:
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function BootMaster --type Master"
ProcessUtilities.executioner(execPath)
final_json = json.dumps({'status': 1})
return HttpResponse(final_json)
except BaseException as msg:
final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
def BootChild(self):
try:
ChildData = '/home/cyberpanel/childaata'
writeToFile = open(ChildData, 'w')
writeToFile.write(json.dumps(self.data))
writeToFile.close()
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function BootChild --type Child"
ProcessUtilities.executioner(execPath)
final_json = json.dumps({'status': 1}) final_json = json.dumps({'status': 1})
return HttpResponse(final_json) return HttpResponse(final_json)

View File

@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .cloudManager import CloudManager from .cloudManager import CloudManager
import json import json
from loginSystem.models import Administrator from loginSystem.models import Administrator
@@ -33,12 +32,30 @@ def router(request):
else: else:
return cm.verifyLogin(request)[1] return cm.verifyLogin(request)[1]
## Debug Log
import os
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
from plogical.processUtilities import ProcessUtilities
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile('Current controller: %s' % (controller))
##
if controller == 'verifyLogin': if controller == 'verifyLogin':
return cm.verifyLogin(request)[1] return cm.verifyLogin(request)[1]
elif controller == 'RunServerLevelEmailChecks': elif controller == 'RunServerLevelEmailChecks':
return cm.RunServerLevelEmailChecks() return cm.RunServerLevelEmailChecks()
elif controller == 'DetachCluster': elif controller == 'DetachCluster':
return cm.DetachCluster() return cm.DetachCluster()
elif controller == 'FetchMasterBootStrapStatus':
return cm.FetchMasterBootStrapStatus()
elif controller == 'FetchChildBootStrapStatus':
return cm.FetchChildBootStrapStatus()
elif controller == 'BootMaster':
return cm.BootMaster()
elif controller == 'BootChild':
return cm.BootChild()
elif controller == 'SetupCluster': elif controller == 'SetupCluster':
return cm.SetupCluster() return cm.SetupCluster()
elif controller == 'ReadReport': elif controller == 'ReadReport':

View File

@@ -6,13 +6,14 @@ import requests
sys.path.append('/usr/local/CyberCP') sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
from plogical.processUtilities import ProcessUtilities from plogical.processUtilities import ProcessUtilities
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
class ClusterManager: class ClusterManager:
LogURL = "http://cloud.cyberpanel.net:8000/HighAvailability/RecvData" LogURL = "http://de-a.cyberhosting.org:8000/HighAvailability/RecvData"
ClusterFile = '/home/cyberpanel/cluster' ClusterFile = '/home/cyberpanel/cluster'
def __init__(self): def __init__(self, type):
## ##
ipFile = "/etc/cyberpanel/machineIP" ipFile = "/etc/cyberpanel/machineIP"
f = open(ipFile) f = open(ipFile)
@@ -20,15 +21,15 @@ class ClusterManager:
self.ipAddress = ipData.split('\n', 1)[0] self.ipAddress = ipData.split('\n', 1)[0]
## ##
self.config = json.loads(open(ClusterManager.ClusterFile, 'r').read()) self.config = json.loads(open(ClusterManager.ClusterFile, 'r').read())
self.type = type
def PostStatus(self): def PostStatus(self, message):
import os try:
if os.path.exists(ProcessUtilities.debugPath): finalData = {'name': self.config['name'], 'type': self.type, 'message': message, 'token': self.config['token']}
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging resp = requests.post(ClusterManager.LogURL, data=json.dumps(finalData), verify=False)
logging.writeToFile(str(self.config)) logging.writeToFile(resp.text + '[info]')
finalData = json.dumps(self.config) except BaseException as msg:
resp = requests.post(ClusterManager.LogURL, data=finalData, verify=False) logging.writeToFile('%s. [31:404]' % (str(msg)))
print (resp.text)
def FetchMySQLConfigFile(self): def FetchMySQLConfigFile(self):
@@ -37,81 +38,146 @@ class ClusterManager:
else: else:
return '/etc/mysql/conf.d/cluster.cnf' return '/etc/mysql/conf.d/cluster.cnf'
def DetechFromCluster(self, type): def DetechFromCluster(self):
try: try:
command = 'rm -rf %s' % (self.FetchMySQLConfigFile()) command = 'rm -rf %s' % (self.FetchMySQLConfigFile())
ProcessUtilities.normalExecutioner(command) ProcessUtilities.normalExecutioner(command)
command = 'systemctl stop mysql' command = 'systemctl stop mysql'
ProcessUtilities.normalExecutioner(command) #ProcessUtilities.normalExecutioner(command)
command = 'systemctl restart mysql' command = 'systemctl restart mysql'
ProcessUtilities.executioner(command) #ProcessUtilities.executioner(command)
if type == 'Child': self.PostStatus('Successfully detached. [200]')
self.config['failoverServerMessage'] = 'Successfully detached. [200]'
self.PostStatus()
else:
self.config['masterServerMessage'] = 'Successfully detached. [200]'
self.PostStatus()
except BaseException as msg: except BaseException as msg:
if type == 'Child': self.PostStatus('Failed to detach, error %s [404].' % (str(msg)))
self.config['failoverServerMessage'] = 'Failed to detach, error %s [404].' % (str(msg))
self.PostStatus()
else:
self.config['masterServerMessage'] = 'Failed to detach, error %s [404].' % (str(msg))
self.PostStatus()
def SetupCluster(self, type): def SetupCluster(self):
try: try:
ClusterPath = self.FetchMySQLConfigFile() ClusterPath = self.FetchMySQLConfigFile()
ClusterConfigPath = '/home/cyberpanel/cluster' ClusterConfigPath = '/home/cyberpanel/cluster'
config = json.loads(open(ClusterConfigPath, 'r').read()) config = json.loads(open(ClusterConfigPath, 'r').read())
command = 'systemctl stop mysql' if self.type == 'Child':
ProcessUtilities.normalExecutioner(command)
command = 'rm -rf %s' % (ClusterConfigPath)
ProcessUtilities.executioner(command)
if type == 'Child':
writeToFile = open(ClusterPath, 'w') writeToFile = open(ClusterPath, 'w')
writeToFile.write(config['ClusterConfigFailover']) writeToFile.write(config['ClusterConfigFailover'])
writeToFile.close() writeToFile.close()
command = 'systemctl start mysql'
ProcessUtilities.normalExecutioner(command)
self.config['failoverServerMessage'] = 'Successfully attached to cluster. [200]'
self.PostStatus()
else: else:
writeToFile = open(ClusterPath, 'w') writeToFile = open(ClusterPath, 'w')
writeToFile.write(config['ClusterConfigMaster']) writeToFile.write(config['ClusterConfigMaster'])
writeToFile.close() writeToFile.close()
command = 'galera_new_cluster' self.PostStatus('Successfully attached to cluster. [200]')
ProcessUtilities.normalExecutioner(command)
self.config['masterServerMessage'] = 'Successfully attached to cluster. [200]'
self.PostStatus()
### ###
command = 'rm -rf %s' % (ClusterConfigPath) except BaseException as msg:
ProcessUtilities.executioner(command) self.PostStatus('Failed to attach, error %s [404].' % (str(msg)))
def BootMaster(self):
try:
command = 'systemctl stop mysql'
ProcessUtilities.normalExecutioner(command)
command = 'galera_new_cluster'
ProcessUtilities.normalExecutioner(command)
self.PostStatus('Master server successfully booted. [200]')
###
except BaseException as msg: except BaseException as msg:
if type == 'Child': self.PostStatus('Failed to boot, error %s [404].' % (str(msg)))
self.config['failoverServerMessage'] = 'Failed to attach, error %s [404].' % (str(msg))
self.PostStatus() def BootChild(self):
else: try:
self.config['masterServerMessage'] = 'Failed to attach, error %s [404].' % (str(msg))
self.PostStatus() ChildData = '/home/cyberpanel/childaata'
data = json.loads(open(ChildData, 'r').read())
## CyberPanel DB Creds
## Update settings file using the data fetched from master
dbName = data['dbName']
dbUser = data['dbUser']
password = data['password']
host = data['host']
port = data['port']
## Root DB Creds
rootdbName = data['rootdbName']
rootdbdbUser = data['rootdbdbUser']
rootdbpassword = data['rootdbpassword']
completDBString = """\nDATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '%s',
'USER': '%s',
'PASSWORD': '%s',
'HOST': '%s',
'PORT':'%s'
},
'rootdb': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '%s',
'USER': '%s',
'PASSWORD': '%s',
'HOST': '%s',
'PORT': '%s',
},
}\n""" % (dbName, dbUser, password, host, port, rootdbName, rootdbdbUser, rootdbpassword, host, port)
settingsFile = '/usr/local/CyberCP/CyberCP/settings.py'
settingsData = open(settingsFile, 'r').readlines()
DATABASESCHECK = 0
writeToFile = open(settingsFile, 'w')
for items in settingsData:
if items.find('DATABASES = {') > -1:
DATABASESCHECK = 1
if DATABASESCHECK == 0:
writeToFile.write(items)
if items.find('DATABASE_ROUTERS = [') > -1:
DATABASESCHECK = 0
writeToFile.write(completDBString)
writeToFile.write(items)
writeToFile.close()
## new settings file restored
command = 'systemctl stop mysql'
ProcessUtilities.normalExecutioner(command)
command = 'systemctl start mysql'
ProcessUtilities.normalExecutioner(command)
## Restart lscpd
command = 'systemctl restart lscpd'
ProcessUtilities.normalExecutioner(command)
self.PostStatus('Fail over server successfully booted. [200]')
###
except BaseException as msg:
self.PostStatus('Failed to boot, error %s [404].' % (str(msg)))
def main(): def main():
@@ -121,12 +187,16 @@ def main():
args = parser.parse_args() args = parser.parse_args()
uc = ClusterManager() uc = ClusterManager(args.type)
if args.function == 'DetachCluster': if args.function == 'DetachCluster':
uc.DetechFromCluster(args.type) uc.DetechFromCluster()
elif args.function == 'SetupCluster': elif args.function == 'SetupCluster':
uc.SetupCluster(args.type) uc.SetupCluster()
elif args.function == 'BootMaster':
uc.BootMaster()
elif args.function == 'BootChild':
uc.BootChild()
if __name__ == "__main__": if __name__ == "__main__":