码迷,mamicode.com
首页 > 编程语言 > 详细

saltstack+svn+python发布

时间:2015-01-15 21:51:55      阅读:1386      评论:0      收藏:0      [点我收藏+]

标签:

  1 #!/usr/bin/python
  2 #-*- coding: utf-8 -*-
  3 ###############################################
  4 #                                             # 
  5 #         creator:    zenghui                 #
  6 #         datetime:   20/12/2014              #
  7 #                                             # 
  8 ###############################################
  9 
 10 import commands,subprocess,os,sys,time,logging,shutil
 11 from optparse import OptionParser
 12 import config
 13 
 14 #定义时间
 15 day=time.strftime(%Y%m%d%H%M%S)
 16 #定义日志文件
 17 logging.basicConfig(level=logging.INFO,
 18                 format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s,
 19                 datefmt=%a, %d %b %Y %H:%M:%S,
 20                 filename=upload.log,
 21                 filemode=a)
 22 
 23 
 24 #运行系统命令
 25 def cmd_share(cmd):
 26         (status,output)=commands.getstatusoutput(cmd)
 27         if status == 0:
 28                 print cmd+ is ok
 29                 logging.info(cmd + " ---is OK!")
 30                 #print output
 31         else:
 32                 print output
 33                 print cmd + " is  NO"
 34                 logging.info(cmd + " ---is NO!")
 35                 sys.exit(1)
 36 
 37 #1.获取svn版本信息并进行操作
 38 def svn_file_list(svn_hao,svn_dir,ip,name):
 39     svn_file=svn log -v  -r +svn_hao+ +svn_dir+|awk +""+{if ($1=="M" || $1=="A" || $1=="R") print $2}+""+|xargs
 40     (status,output)=commands.getstatusoutput(svn_file)
 41     file=output.split( )
 42     svn_cut_file=map(lambda x:/.join(x.split(/)[config.ipaddress[ip][svn_name][4]:]),file)
 43     svn_cut_dir=set(map(lambda x:/.join(x.split(/)[config.ipaddress[ip][name][5]:-1]),file))
 44     rm_dir=rm -rf +config.ipaddress[ip][name][2]+*    
 45     try:
 46         cmd_share(rm_dir)
 47     except:
 48         pass
 49     #提取svn文件,并进行备份
 50     for i in svn_cut_file:
 51         if . in i:
 52             svn_file_backup=salt +""+config.ipaddress[ip][ip]+""+ cmd.run +""+cp -rf +config.ipaddress[ip][name][1]+i+ +config.ipaddress[ip][name][1]+i+day+svnold.backup+""
 53             try:
 54                 cmd_share(svn_file_backup)
 55             except:
 56                 pass
 57     #提取svn目录,并进行创建
 58     for dir in svn_cut_dir:
 59         try:
 60             local_dir=mkdir -p +config.ipaddress[ip][name][2]+dir
 61             cmd_share(local_dir)
 62         except:
 63             pass
 64     #将svn文件export到本地目录
 65     filename_dir=[]
 66     for file_list in file:
 67         dir="/".join(file_list.split(/)[config.ipaddress[ip][name][5]:-1])
 68         filename=file_list.split(/)[-1].strip()
 69         filename_dir.append(dir+/+filename)
 70         svnfile="svn export %s %s" % (svn://192.168.11.28/repos1+file_list+@+svn_hao,config.ipaddress[ip][name][2]+dir+/+filename)
 71         try:
 72             cmd_share(svnfile)
 73         except:
 74             pass
 75     #对export的目录进行上传
 76     for cp_file in filename_dir:
 77         #upload_file=‘salt "‘+config.ipaddress[ip][ip]+‘"‘+‘ cp.get_dir ‘+config.ipaddress[ip][name][3]+cp_file+‘ ‘+config.ipaddress[ip][name][1]+‘/‘.join(cp_file.split(‘/‘)[:-1])
 78         upload_file=salt "+config.ipaddress[ip][ip]+"+ cp.get_file +config.ipaddress[ip][name][3]+cp_file+ +config.ipaddress[ip][name][1]+cp_file
 79         chown_file=salt "+config.ipaddress[ip][ip]+"+ cmd.run +"chown -R nginx.nginx +config.ipaddress[ip][name][1]+cp_file+"
 80         try:
 81             cmd_share(upload_file)
 82             cmd_share(chown_file)
 83         except:
 84             pass
 85 
 86 #2.获取svn版本将进行打包war
 87 def War_ant(svn_hao,name,ip):
 88     www_build_rt="sed -i ‘s/D:\/Program Files\/Java\/jre7\/lib\/rt.jar/\/opt\/jre1.7.0_67\/lib\/rt.jar/g‘"+ +config.ipaddress[ip][name][3]+version+/build.xml
 89     www_build_apache="sed -i ‘s/D:\/apache-tomcat-7.0.52\/lib/\/opt\/apache-tomcat-7.0.53\/lib/g‘"+ +config.ipaddress[ip][name][3]+version+/build.xml
 90     #www_build_apache2="sed -i ‘s/D:\/java\/apache-tomcat-7.0.52\/lib/\/opt\/apache-tomcat-7.0.53\/lib/g‘"+‘ ‘+config.ipaddress[ip][name][3]+version+‘/build.xml‘
 91     www_svn_ant="ant -f "+config.ipaddress[ip][name][3]+svn_hao+/build.xml
 92     checkout=svn co -r +svn_hao+ +config.ipaddress[ip][name][0]+ +config.ipaddress[ip][name][3]+svn_hao
 93     cp_war=cp -rf +config.ipaddress[ip][name][3]+svn_hao+/+config.ipaddress[ip][name][5]+ +config.ipaddress[ip][name][2]
 94     try:
 95         cmd_share(checkout)
 96         if name == www_war:
 97             cmd_share(www_build_rt)
 98         cmd_share(www_build_apache)
 99         #cmd_share(www_build_apache2)
100         cmd_share(www_svn_ant)
101         cmd_share(cp_war)
102     except:
103         pass
104 
105 #对线上war,项目目录打包备份
106 def War_name_backup(name,ip):
107     war_backup=salt "+config.ipaddress[ip][ip]+" cmd.run "cp -rf +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][5]+ +config.ipaddress[ip][name][6]+config.ipaddress[ip][name][5]+day+"
108     name_backup=salt "+config.ipaddress[ip][ip]+" cmd.script +config.tomcat_scripts+config.ipaddress[ip][name][7]
109     try:
110         cmd_share(war_backup)
111         if name == www_war:    
112             cmd_share(name_backup)
113     except:
114         pass
115 #对war进行上传操作
116 def War_upload(name,ip):
117     name_rm=salt "+config.ipaddress[ip][ip]+" cmd.run "rm -rf +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][10]+"
118     war_upload=salt "+config.ipaddress[ip][ip]+"+ cp.get_file +config.ipaddress[ip][name][1]+config.ipaddress[ip][name][5]+ +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][5]
119     war_unzip=salt "+config.ipaddress[ip][ip]+"  cmd.run  "unzip -qo +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][5]+ -d +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][10]+"
120     js_cp=salt "+config.ipaddress[ip][ip]+" cmd.run +"cp -rf +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][10]+js +config.ipaddress[ip][name][11]+"
121     css_cp=salt "+config.ipaddress[ip][ip]+" cmd.run +"cp -rf +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][10]+css +config.ipaddress[ip][name][11]+"
122     try:
123         cmd_share(name_rm)
124         cmd_share(war_upload)
125         cmd_share(war_unzip)
126         if name == www_war:
127             cmd_share(js_cp)
128             cmd_share(css_cp)
129     except:
130         pass
131 
132 #停止tomcat
133 def Tomcat_stop(name,ip):
134     #tomcat_shutdown=‘salt "‘+config.ipaddress[ip][ip]+‘" cmd.script ‘+config.ipaddress[ip][name][1]+config.ipaddress[ip][name][8]
135     tomcat_shutdown=salt "+config.ipaddress[ip][ip]+" cmd.script +config.tomcat_scripts+config.ipaddress[ip][name][8]
136     subprocess.Popen(tomcat_shutdown, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
137     print "tomcat shutdown done ---is ok"
138 
139 #启动tomcat
140 def Tomcat_start(name,ip):
141     #tomcat_start=‘salt "‘+config.ipaddress[ip][ip]+‘" cmd.script ‘+config.ipaddress[ip][name][1]+config.ipaddress[ip][name][9]
142     tomcat_start=salt "+config.ipaddress[ip][ip]+" cmd.script +config.tomcat_scripts+config.ipaddress[ip][name][9]
143     subprocess.Popen(tomcat_start, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
144     print "tomcat shutdown done ---is ok"
145     chown_tomcat=salt "+config.ipaddress[ip][ip]+" cmd.run +"chown -R tomcat.tomcat +config.ipaddress[ip][name][4]+config.ipaddress[ip][name][10]+"
146     try:
147         cmd_share(chown_tomcat)
148     except:
149         pass
150 
151 
152 
153 #判断svn版本号存不存在
154 def version_svn(svn_hao,svn_dir):
155     svn_file=svn log -v  -r +svn_hao+ + svn_dir + |awk +""+{if ($1=="M" || $1=="A" || $1=="R") print $2}+""+|xargs
156     (status,output)=commands.getstatusoutput(svn_file)
157     if ("Unable to find repository location for" in output or "No such revision" in output):
158                 print svn_hao+ version number  is ERROR
159                 logging.info(svn_hao+ version number  is ERROR)
160                 sys.exit(1)
161         if ( output == ""):
162                 print svn_hao+ version number  is ERROR
163                 logging.info(svn_hao+ version number  is ERROR)
164                 sys.exit(1)
165 
166 
167 if __name__=="__main__":
168         usage = "usage: %prog [options] arg :upload_service.py -r version -i ipaddress -n svn_name"
169         parser = OptionParser(usage)
170         parser.add_option("-r","--svn",dest="svn_version",help="input the svn version. 4686")
171         parser.add_option("-i","--ip",dest="ipaddress",help="input the svn version. 192.168.11.45")
172         parser.add_option("-n","--name",dest="svn_name",help="input the svn svn_name. test|real")
173 
174         (options, args) = parser.parse_args()
175         version = options.svn_version
176         ip = options.ipaddress
177         svn_name = options.svn_name
178 
179         if (version is None or config.ipaddress is None or svn_name is None):
180                 os.system(python upload_service.py -h)
181                 sys.exit(1)
182         elif ip in config.keys:
183         #对需要更新war包的项目名做判断
184         if svn_name != ‘war:
185             version_svn(version,config.ipaddress[ip][svn_name][0])
186             svn_file_list(version,config.ipaddress[ip][svn_name][0],config.ipaddress[ip][ip],svn_name)
187         else:
188             version_svn(version,config.ipaddress[ip][svn_name][0])
189             War_ant(version,svn_name,ip)
190             War_name_backup(svn_name,ip)
191             Tomcat_stop(svn_name,ip)
192             War_upload(svn_name,ip)
193             Tomcat_start(svn_name,ip)

 

saltstack+svn+python发布

标签:

原文地址:http://www.cnblogs.com/zenghui940/p/4227253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!