python批量更新服务器上的文件
因为公司业务经常需要升级全国服务器的一些脚本,每次都要手工来上传文件,然后再执行,非常烦琐。所以写了一个小脚本来批量执行,顺便炼炼手。程序需求:
1、可以指定升级哪个模块,从配置文件中调出哪些省有此模块
2、指定升级web还是升级数据库
配置文件areamap.ini
[江苏电信]abc = 172.16.0.1def = 172.16.0.2[江西电信]abc = 172.16.2.1def = 172.16.2.2
import os,sysimport ConfigParserclass UpDate:def __init__(self,ModName):self.modname = ModNameself.conf = "areamap.ini"# 到到这个模块所有服务器的地区和IP地址def getmodip(self):conf=ConfigParser.ConfigParser()conf.read(self.conf)IP=[]for section in conf.sections():if self.modname in conf.options(section):IP.append((section,conf.get(section,self.modname)))return IP# 取得模块与程序包的对应关系def getwebfile(self):ModMap={'abc':'abc.war','def':'def.war',}return ModMap# 重启tomcatdef retomcat(self,IP):print 'Now restart tomcat:'cmd = "ssh " + IP + " -C 'killall -9 java;sleep 10;tomcat/bin/startup.sh'"print cmdos.system(cmd)if __name__ == "__main__":if(len(sys.argv) < 3):print 'Usage:./up.py modelname db/web sqlfilename'sys.exit()elif( sys.argv == 'db' and len(sys.argv) != 4):print 'Usage:./up.py modelname db sqlfilename'sys.exit()ModelName = sys.argvupdate = UpDate(ModName=ModelName)IP = update.getmodip()if(sys.argv == 'web'):for ip in IP:File = update.getwebfile()if(not os.path.isfile(File)):print "The File: %s is not exsit" % Filesys.exit()print "\n************"+ip+"************\n"update.copyfile(File,ip,'tomcat/webapps/')update.retomcat(ip)
页:
[1]