def createVolume(clusterName,data): serverName = getServerName(clusterName) cmd, options, volumeName = preCreateVolume(data) volume_info = Globals.db.select('volume_info', what='*', where='name=$volumeName', vars=locals()) if len(volume_info) > 0: params = [] params.append(volumeName) result = Utils.errorCode('23015','error while adding a volume:volume ' + volumeName + ' already exists', params) logger.error('error while adding a volume:volume ' + volumeName + ' already exists') raise web.HTTPError(status = "400 Bad Request", data = result) #################################execute on localhost or remote host status,output = Utils.executeOnServer(serverName, cmd) if status == -1: code, reval = '26104', 'error when connecting to remote host ' + serverName + ' from localhost:' + output elif status == -2: code,reval = '26059', 'Error when using pub key to connect remote server ' + serverName + '.' + output elif status == 1: if re.match('exists', output): code, reval = '20103', output else: code, reval = '23015', 'Error when creating volume' + volumeName + '.' + output if status: result = Utils.errorCode(code, reval, []) logger.error(reval) raise web.HTTPError(status = "400 Bad Request", data = result) optionToken = options.split(",") hostAllowed = "\*" for option in optionToken: if option.split("=")[0] == "auth.allow": hostAllowed = option.split("=")[1] nasProtocols = data.accessProtocols if "CIFS" in nasProtocols: isShared = data.isShared owner = data.owner if not createVolumeCIFSUsers(volumeName, isShared, owner, hostAllowed): params = [] params.append(volumeName) result = Utils.errorCode('23201', 'failed to configure cifs for volume: %s' % volumeName, params) Utils.executeOnServer(serverName, 'gluster --mode=script volume delete ' + volumeName) logger.error("failed to configure cifs for volume:" + volumeName) raise web.HTTPError(status = '400 Bad Request',data = result) try: cluster = Globals.db.select('cluster_info',where='name=$clusterName',vars=locals()) clusterid=cluster[0].id Globals.db.insert('volume_info',name=volumeName,cluster_id=clusterid) except: params = [] params.append(volumeName) result = Utils.errorCode('23015','error while adding a volume:error while inserting volume_info into DB', params) deleteVolumeCIFSUser(volumeName) Utils.executeOnServer(serverName, 'gluster --mode=script volume delete ' + volumeName) #postDelete(volumeName, brickList,deleteFlag) logger.error('error while adding a volume:error while inserting volume_info into DB') raise web.HTTPError(status = '400 Bad Request',data = result) return None
def downloadLogs(clusterName, volumeName): params = [] web.header('Content-Type','application/octet-stream') serverName = getServerName(clusterName) hostName = os.popen('hostname').read() cmd = 'python ' + BACKEND_SCRIPT + 'get_volume_bricks.py ' + volumeName (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist\n') or (list[-1] == 'exist'): params.append(volumeName) result = Utils.errorCode('20053', 'volume ' + volumeName + ' does not exist', params) raise web.HTTPError(status='400 Bad Request', data=result) isExecSucess(status, output, serverName, volumeName, cmd, '23019',[]) bricklist = output.split('\n') if status != 0: return output result = '' if os.path.exists(TMP_LOG_DIR) == False: os.mkdir(TMP_LOG_DIR) logs = '' for brick in bricklist: server_dir = brick.split(':/') if len(server_dir) != 2: break file = server_dir[1].strip() log = file.replace('/', '-') + '.log' status,files = Utils.executeOnServer(server_dir[0].strip(),'ls ' + BRICK_LOG_DIR +'|grep ^' + log) files = files.split('\n') for file in files: if file.strip() is '': break cmd = 'scp ' + str(BRICK_LOG_DIR) + str(file) + \ ' root@' + str(hostName).strip().split('\n')[0] + \ ':' + TMP_LOG_DIR + '/' + str(server_dir[0].strip()) + '-' + file (status,output) = Utils.executeOnServer(server_dir[0].strip(), cmd) logs += ' ' + server_dir[0].strip() + '-' + file if output[:7] == 'Warning': continue isExecSucess(status, output, serverName, volumeName, cmd, '23019', []) if os.path.exists(TMP_LOG_DIR + volumeName + '.tar.gz'): os.remove(TMP_LOG_DIR + volumeName + '.tar.gz') os.chdir(TMP_LOG_DIR) log = TMP_LOG_DIR + volumeName + '.tar.gz' if os.path.exists(log): os.remove(log) os.popen('tar cvzf ' + volumeName + '.tar.gz ' + logs) os.popen('rm -rf ' + logs) try: f=open(volumeName+'.tar.gz') stream = f.read() except Exception,e: params.append(volumeName) result = Utils.errorCode('23019', 'error when downloading logs', params) raise web.HTTPError(status='500 Internal Error', data=result)
def run(self): while not self.thread_stop: status,local_IP = Utils.getLocal_IP() localIP = local_IP.strip() commandWithArgs = self.operation + "status" status,message = Utils.executeOnServer(localIP,commandWithArgs) code,message = taskService.getMigrateStatus(message) if code == "6": commandWithArgs = self.operation + "commit" status,message = Utils.executeOnServer(localIP, commandWithArgs) self.thread_stop = True sleep(self.interval)
def run(self): while not self.thread_stop: cluster_info = Globals.db.select('cluster_info') if len(cluster_info) != 0: server = None for cluster in cluster_info: clusterid = cluster.id clusterName = cluster.name server_info = Globals.db.select('server_info', where='cluster_id=$clusterid', vars=locals()) if len(server_info) != 0: server = None for server in server_info: if Utils.isOnline(str(server.name)) == True: break if server is not None: volumes = Globals.db.select('volume_info', where='cluster_id=$clusterid', vars=locals()) if len(volumes) != 0: for v in volumes: # volumeInfo = volumeService.getVolume(clusterName, str(v.name).strip(),False) cmd = "python " + Globals.BACKEND_SCRIPT + "get_volumes.py " + str(v.name).strip() status, output = Utils.executeOnServer(str(server.name), cmd) if status: Globals.mc.delete(str(v.name)+'_volume',1) continue dom = ResponseXml() dom.parseString(output) fetchVolumeCifsUsers(dom) Globals.mc.set(str(v.name)+'_volume', dom.toxml()) sleep(self.interval)
def postDelete(volumeName, brickList,deleteFlag = False): params = [] params.append(volumeName) params.append(brickList) params.append(deleteFlag) for brick in brickList: if brick.strip() is '': continue cmd = 'python ' + BACKEND_SCRIPT + 'clear_volume_directory.py' server_dir = brick.split(":/") if len(server_dir) != 2: break cmd += ' /' + server_dir[1].strip() status,output = Utils.executeOnServer(server_dir[0].strip(), cmd) if status == -1: params = [] params.append(volumeName) params.append(server_dir[0].strip()) params.append(output) code, reval = '26104', 'Volume {0} deleted from cluster, however following error(s) occurred:\nerror when connecting to remote host {1} from localhost:{2}' elif status == -2: code,reval = '26059', 'Volume {0} deleted from cluster, however following error(s) occurred:\nError when using pub key to connect remote server {1}.{2}' elif status == 1: if re.match('exist', output) or re.match('exists', output) or re.match('exist\n',output): code, reval = '20053', 'volume {0} does not exist.\n' else: code, reval = '23101', 'Volume {0} deleted from cluster on server {1}, however following error(s) occurred:\n{2}' if status: result = Utils.errorCode(code, reval, params) raise web.HTTPError(status = "500 Internal Server Error", data = result) return ''
def run(self): while not self.thread_stop: cluster_info = Globals.db.select('cluster_info',what='*') if len(cluster_info) != 0: onlineNum = 0 totalNum = 0 volumes = [] for cluster in cluster_info: clusterid = cluster.id clusterName = cluster.name server_info = Globals.db.select('server_info', where='cluster_id=$clusterid',vars=locals()) if len(server_info) != 0: for server in server_info: if Utils.isOnline(str(server.name)) == False: continue status, message = Utils.executeOnServer(str(server.name), 'gluster volume list') if message.strip() != 'No volumes present in cluster': volumeList = message.split('\n') onlineNum = 0 totalNum = 0 volumes = [] for v in volumeList: message = Globals.mc.get(str(v).strip()+"_volume") if message is None: if v.strip() != '': cmd = "python " + Globals.BACKEND_SCRIPT + "get_volumes.py " + v status,message = Utils.executeOnServer(str(server.name), cmd) totalNum += 1 volumes.append(v) if status != 0: continue if message is not None: weblog = xml.etree.ElementTree.fromstring(message) for entry in weblog.findall('status'): status = entry.text if status.strip().lower() == 'online': onlineNum += 1 clusterVolumeInfo = [] clusterVolumeInfo.append(totalNum) clusterVolumeInfo.append(onlineNum) clusterVolumeInfo.append(volumes) key = str(clusterName) + '_volumes' Globals.mc.set(key,clusterVolumeInfo) sleep(self.interval)
def stopVolume(serverName, volumeName, force = False): cmd = 'gluster --mode=script volume stop ' + volumeName params = [] if force: cmd += ' force' status,output = Utils.executeOnServer(serverName, cmd) list = output.split(' ') isExecSucess(status, output, serverName, volumeName, cmd, '23131', params) web.HTTPError(status='200 OK', data='')
def logRotate(serverName, volumeName, force = False): cmd = 'gluster volume log rotate ' + volumeName params = [] if force: cmd += ' force' status,output = Utils.executeOnServer(serverName, cmd) list = output.split(' ') isExecSucess(status, output, serverName, volumeName, cmd, '23132', params) web.HTTPError(status='200 OK', data='')
def resetOptions(clusterName, volumeName): serverName = getServerName(clusterName) cmd = 'gluster volume reset ' + volumeName (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist\n') or (list[-1] == 'exist'): result = Utils.errorCode('20053', 'volume ' + volumeName + ' does not exist', []) raise web.HTTPError(status='400 Bad Request', data=result) isExecSucess(status, output, serverName, volumeName, cmd, '23018',[]) return ''
def getOptions(clusterName, volumeName): serverName = getServerName(clusterName) cmd = 'python ' + BACKEND_SCRIPT + 'get_volume_options.py ' + volumeName ################ execute cmd on local host or remote host (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist\n') or (list[-1] == 'exist'): result = Utils.errorCode('20053', 'volume ' + volumeName + ' does not exist', []) raise web.HTTPError(status='400 Bad Request', data=result) isExecSucess(status, output, serverName, volumeName, cmd, '23016', []) return output
def rebalanceStop(serverName, volumeName, force=False): cmd = 'gluster volume rebalance ' + volumeName + ' stop' if force: cmd += ' force' status,output = Utils.executeOnServer(serverName, cmd) params = [] list = output.split(' ') if (list[-1] == 'exist') or (list[-1] == 'exist\n'): params.append(volumeName) result = Utils.errorCode('20053', 'volume {0} does not exist.\n', params) raise web.HTTPError(status = '400 Bad Request', data = result) isExecSucess(status, output, serverName, volumeName, cmd, '23133', params) web.HTTPError(status='200 OK', data='')
def run(self): while not self.thread_stop: server_info = Globals.db.select('server_info') for server in server_info: serverName = str(server.name) cmd = 'python ' + Globals.BACKEND_SCRIPT + 'get_server_details.py 2>/dev/null' status,message = Utils.executeOnServer(serverName, cmd) if status == 0: serverName = serverName.strip() Globals.mc.set(serverName+'_server',message) else: Globals.mc.delete(serverName+'_server',1) sleep(self.interval)
def getVolume(clusterName,volumeName): serverName = getServerName(clusterName) message = Globals.mc.get(str(volumeName)+'_volume') dom = ResponseXml() if (message is not None) and (message.strip()!='<response/>'): logger.info("Get volume info from memcache:" + message) return message.replace('<?xml version="1.0" ?>','') cmd = "python " + BACKEND_SCRIPT + "get_volumes.py " + volumeName (status,output) = Utils.executeOnServer(serverName, cmd) isExecSucess(status, output, serverName, volumeName, cmd, '23014', []) dom.parseString(output) fetchVolumeCifsUsers(dom) output = dom.toxml().replace('<?xml version="1.0" ?>','').replace('<response/>','') Globals.mc.set(str(volumeName)+'_volume',output) logger.info("Get volume info by get_volumes.py:" + output) return output
def rebalanceStart(clusterName, serverName, volumeName, fixLayout = False, migrateData = False, forcedDataMigrate = True, force = False): (status,output) = Utils.rebalanceTaskStart(clusterName, volumeName) if status != 0: return output params = [] cmd = 'gluster volume rebalance ' + volumeName if (fixLayout == True) and (migrateData == True): cmd += ' start' elif fixLayout == True: cmd += ' fix-layout start' else: cmd += ' start' if force == True: cmd += 'force' status,output = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist') or (list[-1] == 'exist\n'): params.append(volumeName) result = Utils.errorCode('20053', 'volume {0} does not exist.\n', params) raise web.HTTPError(status = '400 Bad Request', data = result) isExecSucess(status, output, serverName, volumeName, cmd, '23134', params) web.HTTPError(status='200 OK', data='')
def run(self): while not self.thread_stop: cluster_info = Globals.db.select('cluster_info') if len(cluster_info) != 0: for cluster in cluster_info: serversInfo = [] sum = 0 online = 0 servers = [] totalSpace = Decimal("0.0") usedSpace = Decimal("0.0") cluster_id = cluster.id server_info = Globals.db.select('server_info',where='cluster_id=$cluster_id' ,what="name",vars=locals()) if len(server_info) != 0: sum = len(server_info) for server in server_info: serverName = str(server.name) servers.append(serverName) if Utils.isOnline(serverName) == True: cmd = 'python ' + Globals.BACKEND_SCRIPT + 'get_server_bricks.py' status,message = Utils.executeOnServer(serverName, cmd) if status == 0: if message.strip() != '[]': message = message.replace("['",'') message = message.replace("']",'') info = message.split(':') usedSpace = usedSpace + Decimal(info[2]) totalSpace = totalSpace + Decimal(info[3]) online = online + 1 serversInfo.append(online) serversInfo.append(sum) serversInfo.append(usedSpace) serversInfo.append(totalSpace) serversInfo.append(servers) strs = str(cluster.name) + "_servers" Globals.mc.set(strs,serversInfo) sleep(self.interval)
def getLogs(clusterName,volumeName, brickName,linecount='100', severity=None, fromTimeStamp=None, toTimeStamp=None): params = [] params.append(clusterName) params.append(volumeName) params.append(brickName) params.append(linecount) params.append(severity) params.append(fromTimeStamp) params.append(toTimeStamp) web.header('Content-Type', 'application/xml') print severity if severity not in Globals.VOLUME_LOG_TYPE: code,reval = '23501','bad severity type:%s' % severity result = Utils.errorCode(code, reval, params) web.HTTPError(status = "400 Bad request", data = result) return result volLogList = [] if (brickName is not None) and (brickName.strip() is not ''): server_dir = brickName.split(':/') if len(server_dir) == 2: file = server_dir[1].strip() log = file.replace('/', '-') LOGFILE = BRICK_LOG_DIR + log + '.log*' cmd = 'python ' + BACKEND_SCRIPT + 'get_volume_brick_log.py ' + LOGFILE + ' ' + str(linecount) (status,output) = Utils.executeOnServer(server_dir[0].strip(), cmd) output.replace('\n',' ') if status == -1: code, reval = '26104', 'error when connecting to remote host ' + server_dir[0].strip() + ' from localhost:' + output elif status == -2: code,reval = '26059', 'Error when using pub key to connect remote server ' + server_dir[0].strip() + '.' + output elif status == 1: if re.match('volume log not found',output): code, reval = '20053', 'volume ' + volumeName + ' does not exist.\n' + output result = Utils.errorCode(code, reval, params) web.HTTPError(status = "400 Bad request", data = result) return result else: code, reval = '23015', 'Error when executing "' + cmd + '".\n' + output if status: result = Utils.errorCode(code, reval, params) web.HTTPError(status = "400 Bad Request", data = '') return result weblog = xml.etree.ElementTree.fromstring(output) for entry in weblog.findall("logMessage"): volumeLogMessage = VolumeLogMessage() volumeLogMessage.setBrick(brickName) volumeLogMessage.setMessage(entry.find('message').text) volumeLogMessage.setSeverity(entry.find('severity').text) volumeLogMessage.setTimestamp(entry.find('timestamp').text) volLogList.append(volumeLogMessage) else: cmd = 'python ' + BACKEND_SCRIPT + 'get_volume_bricks.py ' + volumeName serverName = volumeService.getServerName(clusterName) (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if list[-1] == 'exist': result = Utils.errorCode('20053', 'volume ' + volumeName + ' does not exist', params) raise web.HTTPError(status='400 Bad Request', data=result) bricklist = output.split('\n') VolumeUtils.isExecSucess(status, output, serverName, volumeName, cmd, '23130', params) #if status == -1: # code, reval = '26104', 'error when connecting to remote host [' + serverName + '] from localhost:' + output #elif status == -2: # code,reval = '26059', 'Error when using pub key to connect remote server [' + serverName + '].' + output #elif status == 1: # code, reval = '23019','Error when getting bricks from server [' + serverName + '].' #result = '' #if status: # web.header('Content-Type', 'text/xml') # result = Utils.errorCode(code, reval, []) # web.HTTPError(status = "400 Bad Request", data = '') # return result for brick in bricklist: server_dir = brick.split(':/') if len(server_dir) != 2: break file = server_dir[1].strip() log = file.replace('/', '-') LOGFILE = BRICK_LOG_DIR + log + '.log' cmd = 'python ' + BACKEND_SCRIPT + 'get_volume_brick_log.py ' + LOGFILE + ' ' + str(linecount) status,output = Utils.executeOnServer(server_dir[0].strip(), cmd) if status == -1: code, reval = '26104', 'error when connecting to remote host ' + server_dir[0].strip() + ' from localhost:' + output elif status == -2: code,reval = '26059', 'Error when using pub key to connect remote server ' + server_dir[0].strip() + '.' + output elif status == 1: if re.match('exist', output): code, reval = '20053', 'volume ' + volumeName + ' does not exist.\n' + output else: code, reval = '23021', 'Error when executing "' + cmd + '".\n' + output if status: result = Utils.errorCode(code, reval, []) raise web.HTTPError(status = "400 Bad Request", data = result) weblog = xml.etree.ElementTree.fromstring(output) for entry in weblog.findall("logMessage"): volumeLogMessage = VolumeLogMessage() volumeLogMessage.setBrick(brick) volumeLogMessage.setMessage(entry.find('message').text) volumeLogMessage.setSeverity(entry.find('severity').text) volumeLogMessage.setTimestamp(entry.find('timestamp').text) volLogList.append(volumeLogMessage) if (severity is not None) and (severity.strip() != '') and (severity.lower() != 'all'): volLogList = filterServerity(volLogList, severity) if (fromTimeStamp is not None) and (toTimeStamp is not None) and (fromTimeStamp is not '') and (toTimeStamp is not ''): volLogList = filterTimeStamp(volLogList, fromTimeStamp, toTimeStamp) responseDom = XmlHandler.ResponseXml() logMessagesTag = responseDom.appendTagRoute("logMessages") for volumeLogMessage in volLogList: logMessageTag = responseDom.createTag("logMessage",None) logMessagesTag.appendChild(logMessageTag) logMessageTag.appendChild(responseDom.createTag("brick",volumeLogMessage.getBrick())) logMessageTag.appendChild(responseDom.createTag("message",volumeLogMessage.getMessage())) logMessageTag.appendChild(responseDom.createTag("severity",volumeLogMessage.getSeverity())) logMessageTag.appendChild(responseDom.createTag("timestamp",volumeLogMessage.getTimeStamp())) return logMessagesTag.toxml()
def setOptions(clusterName, volumeName): serverName = getServerName(clusterName) data = web.input() key = None value = None try: key = data.key.strip() value = data.value.strip() except Exception,e: result = Utils.errorCode('20001', 'key and value are required') raise web.HTTPError(status = '400 Bad Request', data = result) params = [] params.append(key) params.append(value) cmd = 'gluster volume set ' + volumeName + ' ' + key + ' "' + value +'"' (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist\n') or (list[-1] == 'exist'): result = Utils.errorCode('20053', 'volume ' + volumeName + ' does not exist', params) raise web.HTTPError(status='400 Bad Request', data=result) isExecSucess(status, list[0], serverName, volumeName, cmd, '23017', params) return '' def resetOptions(clusterName, volumeName): serverName = getServerName(clusterName) cmd = 'gluster volume reset ' + volumeName (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist\n') or (list[-1] == 'exist'): result = Utils.errorCode('20053', 'volume ' + volumeName + ' does not exist', []) raise web.HTTPError(status='400 Bad Request', data=result)
def removeVolume(clusterName,volumeName): ################################## get servers in the cluster to create a volume serverName = getServerName(clusterName) volumeInfo = getVolume(clusterName, volumeName) # if enableCifs is true, then stop and delete it's cifs configure first root = ET.fromstring(volumeInfo) enableCifs = "false" for subnode in root.getchildren(): if subnode.tag == "enableCifs": enableCifs = subnode.text if subnode.tag == "status": status = subnode.text if enableCifs == "true": if status == "ONLINE": params = [] params.append(volumeName) result = Utils.errorCode('23101', 'Volume ' + volumeName + ' has been started.Volume needs to be stopped before deletion.', params) logger.error('Volume ' + volumeName + ' has been started.Volume needs to be stopped before deletion.') raise web.HTTPError(status = '400 Bad Request', data = result) if not deleteVolumeCIFSUser(volumeName): params = [] params.append(volumeName) result = Utils.errorCode('23204', 'failed to delete cifs configuration while deleting volume: ' + volumeName,params) logger.error("failed to delete cifs configuration while deleting volume: " + volumeName) raise web.HTTPError(status = '400 Bad Request', data = result) cmd = 'python ' + BACKEND_SCRIPT + 'get_volume_bricks.py ' + volumeName (status,output) = Utils.executeOnServer(serverName, cmd) list = output.split(' ') if (list[-1] == 'exist\n') or (list[-1] == 'exist'): params = [] params.append(volumeName) result = Utils.errorCode('20053', 'volume {0} does not exist', params) logger.error("Remove volume: volume[" + volumeName + "] does not exist") raise web.HTTPError(status='400 Bad Request', data=result) isExecSucess(status, output, serverName, volumeName, cmd, '23019',[]) ################################## prepare the command brickList = output.split('\n') cmd = 'gluster --mode=script volume delete ' + volumeName ################################## execute on remote host or local host status,output = Utils.executeOnServer(serverName, cmd) params = [] params.append(clusterName) params.append(volumeName) isExecSucess(status, output, serverName, volumeName, cmd, '23101', params) deleteFlag = False postDelete(volumeName, brickList,deleteFlag) try: postDeleteTask(clusterName, volumeName) except: code, reval = '23101', 'Volume ' + volumeName +' deleted from cluster, however following error(s) occurred:\n' + output if status: result = Utils.errorCode(code, reval, params) logger.error(reval) raise web.HTTPError(status = "400 Bad Request", data = result) try: Globals.db.delete('volume_info',where='name=$volumeName', vars=locals()) except Exception,e: result = Utils.errorCode('23101','error while deleting volume from DB',parseString) logger.error(reval) raise web.HTTPError(status = "400 Bad Request", data = result)