1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
''' 此脚本的作用是读取server下各个模块信息,将要发版的模块生成如下配置,代替手动粘贴的过程 sh remote_restart.sh 10.101.178.13 oauth2 curl 10.101.178.13:8004/check.do ''' import os import toml import time
now = time.strftime("%Y%m%d%H%M", time.localtime()) allpath=[] allfile=[] fileByModule={} def getallfileByModule(path): allfilelist=os.listdir(path) for file in allfilelist: filepath=os.path.join(path,file) if os.path.isdir(filepath): getallfileByModule(filepath) elif os.path.isfile(filepath): module=filepath.split("/")[-2] if module in fileByModule: fileByModule[module].append(filepath) else: fileByModule[module]=[filepath]
def readallfile(file): serverinfo=toml.load(file) modulename=(serverinfo['server']) moduleip = (serverinfo['addr']) moduleport = (serverinfo['port']) return modulename,moduleip,moduleport
def writeallfile(server,addr,port): opremote=open(remotepath,"a+") operatingfile=opremote.write("sh remote_restart.sh {} {}\n".format(addr,server) ) opremote.close() opcurl=open(curlpath,"a+") checkfile=opcurl.write("curl {}:{}/check.do\n".format(addr,port)) opcurl.close()
if __name__ == '__main__': local='kr' dir_path = '/Users/zhaodan/Downloads/LumiWorking/AIOT/online-operating/servers/{}/'.format(local) modulefile = './OpModules' remotepath = './{}_remotefile_{}'.format(local,now) curlpath = './{}_checkfile_{}'.format(local,now) moduleSet=set() with open(modulefile) as file: for line in file: moduleSet.add(line.strip('\n')) getallfileByModule(dir_path) t=0 while t!=-1: flag=False for module,pathList in fileByModule.items(): if len(pathList)>t: modulename, moduleip, moduleport=readallfile(pathList[t]) if modulename in moduleSet: writeallfile(modulename, moduleip, moduleport) flag=True if not flag: break else: t+=1 RemoteWrap=open(remotepath,"a+") wrap1=RemoteWrap.write("\n") RemoteWrap.close() CheckWrap=open(curlpath,"a+") wrap2=CheckWrap.write("\n") CheckWrap.close()
|