def setUpDataNode(self): try: userID = self.request.session['userID'] currentACL = ACLManager.loadedACL(userID) proc = httpProc(self.request, None, None) if currentACL['admin'] == 0: return proc.ajax(0, 'Only administrators can create clusters.') composePath = '/home/cyberpanel/composePath' if not os.path.exists(composePath): os.mkdir(composePath) composeFile = composePath + '/docker-compose.yml' compose = open(composeFile, 'w') for items in self.data['composeData']: compose.writelines(items) compose.close() return proc.ajax(1, None) except BaseException, msg: logging.writeToFile(str(msg)) proc = httpProc(self.request, None, None) return proc.ajax(0, str(msg))
def submitEditCluster(self): try: userID = self.request.session['userID'] currentACL = ACLManager.loadedACL(userID) proc = httpProc(self.request, None, None) if currentACL['admin'] == 0: return proc.ajax(0, 'Only administrators can create clusters.') composePath = '/home/cyberpanel/composePath' composeFile = composePath + '/docker-compose.yml' data = open(composeFile, 'r').readlines() compose = open(composeFile, 'w') for items in data: if items.find('replicas') > -1: compose.writelines(' replicas: ' + str(self.data['containers']) + '\n') elif items.find('memory') > -1: compose.writelines(' memory: ' + self.data['containerRam'] + '\n') elif items.find('cpus:') > -1: compose.writelines(' cpus: "' + self.data['containerCPU'] + '"\n') else: compose.writelines(items) compose.close() return proc.ajax(1, None) except BaseException, msg: logging.writeToFile(str(msg)) proc = httpProc(self.request, None, None) return proc.ajax(0, str(msg))
def killProcess(request): try: userID = request.session['userID'] try: currentACL = ACLManager.loadedACL(userID) if currentACL['admin'] == 1: pass else: return ACLManager.loadErrorJson('status', 0) data = json.loads(request.body) pid = data['pid'] ProcessUtilities.executioner('sudo kill ' + pid) proc = httpProc(request, None) return proc.ajax(1, None) except BaseException, msg: final_dic = {'status': 0, 'erroMessage': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) except KeyError, msg: final_dic = {'status': 0, 'erroMessage': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json)
def fetchManagerTokens(self): try: proc = httpProc(self.request, None, None) userID = self.request.session['userID'] currentACL = ACLManager.loadedACL(userID) if currentACL['admin'] == 0: return proc.ajax(0, 'Only administrators can create clusters.') ipFile = "/etc/cyberpanel/machineIP" f = open(ipFile) ipData = f.read() ipAddress = ipData.split('\n', 1)[0] command = 'sudo docker swarm init --advertise-addr ' + ipAddress ProcessUtilities.executioner(command) managerToken = '' workerToken = '' command = "sudo docker swarm join-token manager" output = subprocess.check_output(shlex.split(command)).splitlines() for items in output: if items.find('--token') > -1: managerToken = items.split(' ')[-2] command = "sudo docker swarm join-token worker" output = subprocess.check_output(shlex.split(command)).splitlines() for items in output: if items.find('--token') > -1: workerToken = items.split(' ')[-2] data = {} data['managerToken'] = managerToken data['workerToken'] = workerToken return proc.ajax(1, None, data) except BaseException, msg: proc = httpProc(self.request, None, None) return proc.ajax(0, None, str(msg))
def addWorker(self): try: proc = httpProc(self.request, None, None) userID = self.request.session['userID'] currentACL = ACLManager.loadedACL(userID) if currentACL['admin'] == 0: return proc.ajax(0, 'Only administrators can create clusters.') token = self.data['token'] ipAddress = self.data['ipAddress'] command = 'sudo docker swarm join --token ' + token + ' ' + ipAddress + ':2377' if ProcessUtilities.executioner(command) == 0: return proc.ajax(0, 'Failed to join as worker.') return proc.ajax(1, None) except BaseException, msg: proc = httpProc(self.request, None, None) return proc.ajax(0, None, str(msg))
def setupManager(self, request): try: request.session['userID'] = self.admin.pk tempStatusPath = "/home/cyberpanel/" + str(randint(1000, 9999)) self.data['tempStatusPath'] = tempStatusPath ham = HAManager(request, self.data, 'setupNode') ham.start() data = {} data['tempStatusPath'] = tempStatusPath proc = httpProc(request, None) return proc.ajax(1, None, data) except BaseException, msg: return self.ajaxPre(0, str(msg))
def topProcesses(request): try: userID = request.session['userID'] currentACL = ACLManager.loadedACL(userID) if currentACL['admin'] == 1: pass else: return ACLManager.loadError() templateName = "serverStatus/topProcesses.html" proc = httpProc(request, templateName) return proc.renderPre() except KeyError, msg: logging.CyberCPLogFileWriter.writeToFile( str(msg) + "[litespeedStatus]") return redirect(loadLoginPage)
def leaveSwarm(self): try: userID = self.request.session['userID'] currentACL = ACLManager.loadedACL(userID) proc = httpProc(self.request, None, None) if currentACL['admin'] == 0: return proc.ajax(0, 'Only administrators can create clusters.') commands = self.data['commands'] for command in commands: try: result = subprocess.call(command, shell=True) if result != 0: logging.writeToFile(command + ' Failed.') except BaseException, msg: logging.writeToFile(command + 'Failed.') return 0 except BaseException, msg: logging.writeToFile(str(msg))