def batchCmdOrShell(rev): ''' @命令或者脚本执行函数 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) if 'command_scriptfile' in citem: scmd = citem['command_scriptfile'] else: return "{'status':'ERROR','result':'Unknow error!'}" sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'"+result+"'}" except Exception as e: save_log('ERROR',str(e)) result = 'Cmd or Script is run Faild!' return "{'status':'ERROR','result':'"+result+"'}" else: return "{'status':'ERROR','result':'Unknow error!'}"
def batchServiceRestart(rev): ''' @服务操作函数 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) if 'service_scriptfile' in citem: script = citem['service_scriptfile'] else: script = 'service '+str(citem['service_name'])+' '+citem['service_type'] sr = osaBatchLib.scriptOrCmd(script) if sr: try: result = osaBatchLib.runCmdOrScript(script) return "{'status':'OK','result':'OK!'}" except Exception as e: save_log('ERROR','Cmd or Script is run Faild!'+str(e)) result = 'Cmd or Script is run Faild!'+str(e) return "{'status':'ERROR','result':'services error!'}" else: return "{'status':'ERROR','result':'Unknow error!'}"
def batchTopCheck(rev): ''' @负载检查 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) if citem['topstate_scriptfile'] == 'default': citem['topstate_scriptfile'] = 'uptime' #执行脚本或者指令 scmd = citem['topstate_scriptfile'] sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'"+result+"'}" except Exception as e: save_log('ERROR',str(e)) result = 'Cmd or Script is Faild!' return "{'status':'ERROR','result':'"+result+"'}" return "{'status':'OK','result':'OK'}" else: return "{'status':'ERROR','result':'Unknow error!'}"
def batchAppInstalltion(rev): ''' 安装执行函数,实为调用一个shell脚本 ''' value = osaBatchLib.getConfigItem(rev) script=value['install_scriptfile'] if script: try: result = osaBatchLib.runCmdOrScript(script) return "{'status':'OK','result':'"+result+"'}" except Exception ,e: save_log('ERROR',str(e)) result = 'Install Script run failed!' return "{'status':'ERROR','result':'"+result+"'}"
def batchFileRecv(rev): ''' @文件接收函数 ''' #文件保存目标位置 citem = osaBatchLib.getConfigItem(rev) dfile = citem['targetpath'] #文件名 dfilename = os.path.basename(dfile) #文件目录 fdir = os.path.dirname(dfile) #目标目录不存在则创建 if not os.path.exists(fdir): try: os.makedirs(fdir) except Exception as e: save_log('ERROR', 'mkdir is error:' + str(e)) return "{'status':'ERROR','result':'x0027'}" #端口号 fileport = osaBatchLib.getConfigPort(rev) #临时文件 tempfile = osaBatchLib.tempFilePath(rev) #开始接收文件 try: fsize = file_recv_main(host='0.0.0.0', port=int(fileport), filename=tempfile) except Exception as e: save_log('ERROR', 'file recv error:' + str(e)) return "{'status':'ERROR','result':'x0026'}" if fsize: #对文件进行处理 advance = citem['advance'].split('|') backfile = dfile + '.bak.' + str( time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) if advance[0] == 'cut': if os.path.exists(dfile): try: os.rename(dfile, backfile) os.rename(tempfile, dfile) except Exception as e: save_log('ERROR', str(e)) return "{'status':'ERROR','result':'x0022'}" else: try: os.rename(tempfile, dfile) except: shutil.copy(tempfile, dfile) elif advance[0] == 'copy': if os.path.exists(dfile): try: os.rename(dfile, backfile) os.rename(tempfile, dfile) os.remove(backfile) except Exception as e: save_log('ERROR', str(e)) return "{'status':'ERROR','result':'x0023'}" else: try: os.rename(tempfile, dfile) except: shutil.copy(tempfile, dfile) else: return "{'status':'ERROR','result':'x0021'}" if advance[1] == 'document_integrity': dict = osaBatchLib.revToDict(rev) if dict['md5'] != osaBatchLib.md5sum(dfile): return "{'status':'ERROR','result':'x0024'}" #执行脚本或者指令 scmd = citem['distribution_script'] sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'" + result + "'}" except Exception as e: save_log('ERROR', str(e)) result = 'Cmd or Script is Faild!' return "{'status':'ERROR','result':'" + result + "'}" return "{'status':'OK','result':'OK'}" return "{'status':'ERROR','result':'x0025'}"
def batchFileRecv(rev): ''' @文件接收函数 ''' #文件保存目标位置 citem = osaBatchLib.getConfigItem(rev) dfile = citem['targetpath'] #文件名 dfilename = os.path.basename(dfile) #文件目录 fdir = os.path.dirname(dfile) #目标目录不存在则创建 if not os.path.exists(fdir): try: os.makedirs(fdir) except Exception as e: save_log('ERROR','mkdir is error:'+str(e)) return "{'status':'ERROR','result':'x0027'}" #端口号 fileport = osaBatchLib.getConfigPort(rev) #临时文件 tempfile = osaBatchLib.tempFilePath(rev) #开始接收文件 try: fsize = file_recv_main(host='0.0.0.0',port=int(fileport),filename = tempfile) except Exception as e: save_log('ERROR','file recv error:'+str(e)) return "{'status':'ERROR','result':'x0026'}" if fsize: #对文件进行处理 advance = citem['advance'].split('|') backfile = dfile+'.bak.'+str(time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) if advance[0] == 'cut': if os.path.exists(dfile): try: os.rename(dfile,backfile) os.rename(tempfile,dfile) except Exception as e: save_log('ERROR',str(e)) return "{'status':'ERROR','result':'x0022'}" else: try: os.rename(tempfile,dfile) except: shutil.copy(tempfile,dfile) elif advance[0] == 'copy': if os.path.exists(dfile): try: os.rename(dfile,backfile) os.rename(tempfile,dfile) os.remove(backfile) except Exception as e: save_log('ERROR',str(e)) return "{'status':'ERROR','result':'x0023'}" else: try: os.rename(tempfile,dfile) except: shutil.copy(tempfile,dfile) else: return "{'status':'ERROR','result':'x0021'}" if advance[1] == 'document_integrity': dict = osaBatchLib.revToDict(rev) if dict['md5'] != osaBatchLib.md5sum(dfile): return "{'status':'ERROR','result':'x0024'}" #执行脚本或者指令 scmd = citem['distribution_script'] sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'"+result+"'}" except Exception as e: save_log('ERROR',str(e)) result = 'Cmd or Script is Faild!' return "{'status':'ERROR','result':'"+result+"'}" return "{'status':'OK','result':'OK'}" return "{'status':'ERROR','result':'x0025'}"