六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 49|回复: 0

python批量更新服务器上的文件

[复制链接]

升级  30.67%

24

主题

24

主题

24

主题

秀才

Rank: 2

积分
96
 楼主| 发表于 2013-1-15 03:10:52 | 显示全部楼层 |阅读模式
因为公司业务经常需要升级全国服务器的一些脚本,每次都要手工来上传文件,然后再执行,非常烦琐。所以写了一个小脚本来批量执行,顺便炼炼手。

程序需求:
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[self.modname]# 重启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[2] == 'db' and len(sys.argv) != 4):print 'Usage:./up.py modelname db sqlfilename'sys.exit()ModelName = sys.argv[1]update = UpDate(ModName=ModelName)IP = update.getmodip()if(sys.argv[2] == '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[0]+"************\n"update.copyfile(File,ip[1],'tomcat/webapps/')update.retomcat(ip[1])
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表