Files
CyberPanel/plogical/ClusterManager.py

129 lines
4.2 KiB
Python
Raw Normal View History

2021-03-29 17:23:18 +05:00
import json
import os.path
import sys
import argparse
import requests
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
2021-03-29 17:38:54 +05:00
from plogical.processUtilities import ProcessUtilities
2021-03-29 17:23:18 +05:00
class ClusterManager:
LogURL = "http://cloud.cyberpanel.net:8000/HighAvailability/RecvData"
ClusterFile = '/home/cyberpanel/cluster'
def __init__(self):
##
ipFile = "/etc/cyberpanel/machineIP"
f = open(ipFile)
ipData = f.read()
self.ipAddress = ipData.split('\n', 1)[0]
##
self.config = json.loads(open(ClusterManager.ClusterFile, 'r').read())
def PostStatus(self):
finalData = json.dumps(self.config)
resp = requests.post(ClusterManager.LogURL, data=finalData, verify=False)
print (resp.text)
def FetchMySQLConfigFile(self):
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
return '/etc/mysql/conf.d/cluster.cnf'
else:
return '/etc/mysql/conf.d/cluster.cnf'
def DetechFromCluster(self, type):
try:
2021-03-30 22:08:01 +05:00
2021-03-29 17:23:18 +05:00
command = 'rm -rf %s' % (self.FetchMySQLConfigFile())
ProcessUtilities.normalExecutioner(command)
command = 'systemctl stop mysql'
ProcessUtilities.normalExecutioner(command)
command = 'systemctl restart mysql'
ProcessUtilities.executioner(command)
if type == 'Child':
self.config['failoverServerMessage'] = 'Successfully detached. [200]'
self.PostStatus()
else:
self.config['masterServerMessage'] = 'Successfully detached. [200]'
self.PostStatus()
except BaseException as msg:
if type == 'Child':
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()
2021-03-30 16:16:59 +05:00
def SetupCluster(self, type):
try:
ClusterPath = self.FetchMySQLConfigFile()
ClusterConfigPath = '/home/cyberpanel/cluster'
config = json.loads(open(ClusterConfigPath, 'r').read())
command = 'systemctl stop mysql'
ProcessUtilities.normalExecutioner(command)
2021-03-30 22:08:01 +05:00
command = 'rm -rf %s' % (ClusterConfigPath)
ProcessUtilities.executioner(command)
2021-03-30 16:16:59 +05:00
2021-03-30 22:08:01 +05:00
if type == 'Child':
2021-03-30 22:21:50 +05:00
2021-03-30 16:16:59 +05:00
writeToFile = open(ClusterPath, 'w')
writeToFile.write(config['ClusterConfigFailover'])
2021-03-30 22:21:50 +05:00
writeToFile.close()
2021-03-30 16:16:59 +05:00
command = 'systemctl start mysql'
ProcessUtilities.normalExecutioner(command)
self.config['failoverServerMessage'] = 'Successfully attached to cluster. [200]'
self.PostStatus()
2021-03-30 22:21:50 +05:00
2021-03-30 16:16:59 +05:00
else:
writeToFile = open(ClusterPath, 'w')
writeToFile.write(config['ClusterConfigMaster'])
2021-03-30 22:21:50 +05:00
writeToFile.close()
2021-03-30 16:16:59 +05:00
command = 'galera_new_cluster'
ProcessUtilities.normalExecutioner(command)
self.config['masterServerMessage'] = 'Successfully attached to cluster. [200]'
self.PostStatus()
2021-03-30 22:08:01 +05:00
###
command = 'rm -rf %s' % (ClusterConfigPath)
ProcessUtilities.executioner(command)
2021-03-30 16:16:59 +05:00
except BaseException as msg:
if type == 'Child':
self.config['failoverServerMessage'] = 'Failed to attach, error %s [404].' % (str(msg))
self.PostStatus()
else:
self.config['masterServerMessage'] = 'Failed to attach, error %s [404].' % (str(msg))
self.PostStatus()
2021-03-29 17:23:18 +05:00
def main():
parser = argparse.ArgumentParser(description='CyberPanel Installer')
parser.add_argument('--function', help='Function to run.')
parser.add_argument('--type', help='Type of detach.')
args = parser.parse_args()
uc = ClusterManager()
if args.function == 'DetachCluster':
2021-03-30 16:16:59 +05:00
uc.DetechFromCluster(args.type)
elif args.function == 'SetupCluster':
uc.SetupCluster(args.type)
2021-03-29 17:23:18 +05:00
if __name__ == "__main__":
main()