这次很强了。就差最后一步,判断一下SVN的版本是否是真的库里的。
#!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys,commands,subprocess import re,time from optparse import OptionParser #SVN的仓库地址,其实都是同一个,但为了安全和区别,写入具体地址 svnUrl = "svn://XX" prefix = "/XX/" #定义远程更新文件的根目录,与测试和真实环境有关的判断,用字典简单实现 sitePathDic = {"X":"/XX", "X":"/XX"} #定义将SVN放入本地SALTSTACK的目录 updateFolder = "/srv/salt/XX/" #定义SALT-MINION客户端地址 saltMinionDic = {"rX":"X", "tX":"X"} #定义推送文件的saltstack-master的文件地址 saltMasterUrl = "salt://X/" #维护两个文件列表,一个本地,一个远程 localFileList = [] remoteFileList = [] def getSvnFile(rVersion): #清空上次更新的所有文件 delCmd = "rm -rf " + updateFolder + "*" os.system(delCmd) print '\033[0;32;40m' print "Delete old saltstack files..." #因为每次只更新固定版本的文件,所以,要先得到文件列表,再一个一个导出为和网站一样的标准目录结构 svnLogCmd ="svn log -v -r" + rVersion +" " + svnUrl + " --username " svnLogOut = subprocess.Popen(svnLogCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in svnLogOut.stdout.readlines(): #将输出命令的非标准行去掉 if len(line.strip())!=0 and line.startswith(" "): #将标准行中的A,C,M等字符去掉 svnFileName = line.split()[1] #替换为相对于网站的根目录 fileList = svnFileName.replace(prefix,'') #取出相对于根目录的文件夹,判断是否存在和建立 folderList = '/'.join(fileList.split('/')[:-1]) if not os.path.exists(updateFolder + folderList): mkdirCmd = "mkdir " + updateFolder + folderList + " -p" os.system(mkdirCmd) #print "Make a new dir: " + updateFolder + folderList #判断取出的最后的字段是目录还是文件,如果是文件夹,需要新建,这就需要规定文件夹不能包含.,而文件名必须包含.,否则无法进行这一步判定 #这个判断我很纠结,如果更新不涉及SVN新建目录,就省事多了。 if '.' not in fileList.split('/')[-1]: mkdirCmd = "mkdir " + updateFolder + fileList + " -p" os.system(mkdirCmd) #print "Make a new dir: " + updateFolder + fileList #将文件放入列表,方便在更新到正式网站之后,循环修改相关权限()。 localFileList.append(fileList) remoteFileList.append(sitePathDic[svnType] + '/' + fileList) svnExportCmd = "svn export " + svnUrl + '/' + fileList + " " + updateFolder + fileList subprocess.Popen(svnExportCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) print "Finished export Svn File into salt-master " def backupFile(bFileName, str_now): #加强安装性,备份后缀加PHP suffix = str_now + ".old.php" saltCpCmdPre = "salt '" + saltMinionDic[svnType] + "' cmd.run " + "' cp -p " cpCmd = saltCpCmdPre + bFileName + " " + bFileName + suffix +"'" subprocess.Popen(cpCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) def updateFile(localFile, remoteFile): #更新文件 saltGetFilePre = "salt '" + saltMinionDic[svnType] + "' cp.get_file " + saltMasterUrl saltCpCmd = saltGetFilePre + localFile + " " + remoteFile + " makedirs=True" subprocess.Popen(saltCpCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) #由于新建文件夹,SALTSTACK的权限所有者的更新在第一次不起作用,故而在CP好文件之后,单独进行权限更新 def chOwnMod(remoteFile): saltChownCmdPre = "salt '" + saltMinionDic[svnType] + "' cmd.run " + "'chown X" saltChownCmd = saltChownCmdPre + remoteFile + "'" subprocess.Popen(saltChownCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) saltChmodCmdPre = "salt '" + saltMinionDic[svnType] + "' cmd.run " + "'chmod X " saltChmodCmd = saltChmodCmdPre + remoteFile + "'" subprocess.Popen(saltChmodCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) def main(): getSvnFile(svnRev) #统一更新备份的时间 now = time.localtime() str_now = time.strftime("%Y-%m-%d-%H-%M-%S", now) for file in remoteFileList: backupFile(file, str_now) print "Finished back file-" + str_now print '\033[0m' for local,remote in zip(localFileList,remoteFileList): updateFile(local,remote) print "update file : " + remote #由于新建文件夹的权限更新执行太快,会不成功,故而停五秒之后再确认权限 print "Wait 5 seconds to start update permission" time.sleep(5) for remoteFile in remoteFileList: chOwnMod(remoteFile) print '\033[0;32;40m' print "Finished. update success!" print '\033[0m' if __name__=="__main__": #定义命令行参数 usage = "usage: %prog [options] arg :X.py -rX -tXe" parser = OptionParser(usage) parser.add_option("-r", "--rev", dest="rev", help="input the rev number.") parser.add_option("-t", "--type", dest="type", help="input the update type") (options, args) = parser.parse_args() if options.rev: svnRev = options.rev else: print '\033[0;31;40m' print "input no svn Reversion" print '\033[0m' sys.exit(1) if options.type: svnType = options.type regex=ur"(Xe|rX)" #判断是否是特定参数 if re.search(regex, svnType): pass else: print '\033[0;31;40m' print "svn type is wrong,it must 'Xe' or 'reX'" print '\033[0m' sys.exit(1) else: print '\033[0;31;40m' print "input no svn update type" print '\033[0m' sys.exit(1) main()
时间: 2025-01-27 04:21:41