前言:html
最近在研究docker持續集成發佈,從2015年8月份開始學習docker,兩個月的時間學習了docker的基礎理論,進行了項目經過Docker容器運行的屢次實驗。我想經過jenkins構建發佈docker容器,試驗了好久都沒成功,目前完成了一個簡單的發佈流程,即經過jenkins構建代碼,調用shell,更新到目標服務器的容器裏。java
1、Jenkins安裝部署python
1,部署java環境。(這裏用的是jdk7和tomcat7)linux
mkdir -p /data/webserver
cd /data/webserver
wget
rpm -ivh jdk-7u55-linux-x64.rpm
wget
tar zxvf apache-tomcat-7.0.53.tar.gz
cat > /etc/profile.d/development.sh <<EOF
export JAVA_HOME=/usr/java/jdk1.7.0_55
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
EOF
source /etc/profile.d/development.shgit
2,部署jenkins項目web
從官網下載jenkins程序包,上傳到服務器的程序目錄,刪除tomcat/webapps目錄下的全部文件,而後修改tomcat配置文件指定程序目錄便可docker
<Host name="localhost" appBase="/data/www/jenkins"
unpackWARs="true" autoDeploy="true">shell
啓動tomcat: ./startup.shapache
瀏覽器訪問:http://172.20.0.1:8080/jenkinsvim
2、Jenkins項目構建
1,安裝maven插件,因爲部分開發team使用了maven管理,這裏須要安裝maven插件,安裝的是3.2.2版本;
2,私服配置,以下
vim /root/.m2/settings.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--將此文件放在目錄(~/.m2)下作爲maven的設置文件-->
<servers>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>deployment</password>
</server>
<server>
<id>releases</id>
<username>deployment</username>
<password>deployment</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>internal nexus repository</name>
<url>http://172.20.135.19:8080/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://nisabi</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://nisabi</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
3,在jenkins操做界面---系統管理---插件管理中,安裝git、go插件,svn已經默認安裝,因爲開發項目有用到go語言,這裏須要安裝go插件;
4,在jenkins操做界面新建一個項目,能夠選擇自由風格軟件項目或maven項目,在配置中填寫源碼管理地址、受權賬號、Branches to build(分支名)、pom.xml(根據具體項目來寫)、add-post-build-step-----execute shell;
5,在execute shell框中填寫服務器腳本地址,保存便可
3、Python發佈腳本
#!/usr/bin/python
#coding=utf-8
'''
Created on 2014-6-19
@author: huangyishan
'''
import re,sys,os,time,datetime
import pexpect
import hashlib
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
prompt = '[$#>]'
ssh_newkey='Are you sure you want to continue connecting'
rsync_prompt='total size.*'
ip_list=["0.0.0,1","0.0.0.2"]
user="omadmin"
password="password"
local_path="/root/.jenkins/jobs/passport/workspace/wasabi-web-front/target/ROOT.war"
target_path="/data/program/front/ROOT.war"
log_path="/data/tmplog/passport/master/fabu_"
command="md5sum %s" %(target_path)
server_script_exec="sh /data/sh/program_update.sh"
cur_time = time.strftime('%Y_%m_%d_%H_%M')
log_file_name = log_path + cur_time + ".log"
recipientas_list=["huangyishan@admin.com"]
itheme= '賬號中心生產環境版本更新發布'
email_server_IP='x.x.x.x'
mail_user="product_release@xxx.com"
mail_pass="password"
def send_email_succeed(recipientas_list,addresser,itheme,email_server_IP,log_file_name,server_ip,local_file_md5,target_file_md5):
msg = MIMEMultipart()
msg['Subject'] = itheme
msg['From'] = addresser
msg['To'] = ";".join(recipientas_list)
html_succeed = """\
<html>
<head></head>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<body>
<p>Hi,各位夥伴<br>
<font color="green" size=5 ><====服務器IP %s ====> passport.coocaa.com 賬號中心生產環境版本更新發布成功.</font><br><br><br>
<font size=5 >如發現版本有問題,請及時聯繫黃怡善,電話:</font><font color="green" size=5 > 137XXXX6716</font><br><br><br>
<font size=5 >本地版本文件MD5: </font><font color="green" size=5 >%s</font><br>
<font size=5 >服務器版本文件MD5: </font><font color="green" size=5 >%s</font></font><br>
</p>
</body>
</html>"""%(server_ip, local_file_md5, target_file_md5)
part1 = MIMEText(html_succeed, 'html',_charset="utf-8")
#fp = open(log_file_name, 'rb')
#part2 = MIMEText(fp.read(), 'plain',_charset="utf-8")
#fp.close()
msg.attach(part1)
#msg.attach(part2)
try:
s = smtplib.SMTP()
s.connect(email_server_IP)
s.login(mail_user,mail_pass)
s.sendmail(addresser, recipientas_list, msg.as_string())
s.quit()
except Exception, e:
print str(e)
return False
def send_email_fail(recipientas_list,addresser,itheme,email_server_IP,log_file_name,server_ip,local_file_md5,target_file_md5):
msg = MIMEMultipart()
msg['Subject'] = itheme
msg['From'] = addresser
msg['To'] = ";".join(recipientas_list)
html_succeed = """\
<html>
<head></head>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<body>
<p>Hi,各位夥伴<br>
<font color="red" size=5 ><====服務器IP %s ====> 賬號中心生產環境版本更新發布失敗,</font><font size=5 >附件有詳細發佈日誌,請查看.</font><br><br><br>
<font color="red" size=5 >請及時聯繫黃怡善,電話:</font><font color="green" size=5 > 137XXXX6716</font><br><br><br>
<font color="red" size=5 >本地版本文件MD5: </font><font color="green" size=5 >%s</font><br>
<font color="red" size=5 >服務器版本文件MD5: </font><font color="green" size=5 >%s</font><br>
</p>
</body>
</html>
"""%(server_ip, local_file_md5, target_file_md5)
part1 = MIMEText(html_succeed, 'html',_charset="utf-8")
#fp = open(log_file_name, 'rb')
#part2 = MIMEText(fp.read(), 'plain',_charset="utf-8")
#fp.close()
msg.attach(part1)
#msg.attach(part2)
try:
s = smtplib.SMTP()
s.connect(email_server_IP)
s.login(mail_user,mail_pass)
s.sendmail(addresser, recipientas_list, msg.as_string())
s.quit()
except Exception, e:
print str(e)
return False
for server_ip in ip_list:
log_file = open(log_file_name, "a")
child=pexpect.spawn("/usr/bin/rsync -e 'ssh -p 22222' -avzcP %s %s@%s:%s" %(local_path, user, server_ip, target_path))
child.logfile=log_file
index = child.expect([ssh_newkey, 'password: ', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
child.sendline('yes')
child.expect ('password: ')
child.sendline(password)
child.expect(rsync_prompt,timeout=None)
elif index == 1:
child.sendline(password)
child.expect(rsync_prompt,timeout=None)
elif index == 2:
print "%s rsync: %s EOF ERROR %s" %('#'*10, server_ip, '#'*10)
elif index == 3:
print "%s rsync: %s TIMEOUT ERROR %s" %('#'*10, server_ip, '#'*10)
log_file.close()
child.close()
time.sleep(5)
log_file = open(log_file_name, "a")
child=pexpect.spawn("/usr/bin/ssh -p 22222 %s@%s" %(user, server_ip))
child.logfile=log_file
index = child.expect([ssh_newkey, 'password: ', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
child.sendline('yes')
child.expect ('password: ')
child.sendline(password)
child.expect (prompt)
child.sendline(command)
child.expect (prompt)
child.sendline('exit')
elif index == 1:
child.sendline(password)
child.expect (prompt)
child.sendline(command)
child.expect (prompt)
child.sendline('exit')
elif index == 2:
print "%s ssh: %s EOF ERROR %s" %('#'*10, server_ip, '#'*10)
elif index == 3:
print "%s ssh: %s TIMEOUT ERROR %s" %('#'*10, server_ip, '#'*10)
log_file.close()
child.close()
os.system("md5sum %s | awk '{print $1}' > /data/tmplog/passport/master/upload_file_local.log" %(local_path))
os.system("grep %s %s | awk '{print $1}' | egrep -v 'md5sum|omadmin' | tail -n 1 > /data/tmplog/passport/master/upload_file_remote.log" %(target_path, log_file_name))
for localmd5 in open("/data/tmplog/passport/master/upload_file_local.log"):
local_file_md5 = localmd5
for remotemd5 in open("/data/tmplog/passport/master/upload_file_remote.log"):
target_file_md5 = remotemd5
if target_file_md5 == local_file_md5:
log_file = open(log_file_name, "a")
child=pexpect.spawn("/usr/bin/ssh -p 22222 %s@%s" %(user, server_ip))
child.logfile=log_file
index = child.expect(['password: ', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
child.sendline(password)
child.expect (prompt)
child.sendline(server_script_exec)
child.expect (prompt)
child.sendline('exit')
elif index == 1:
print "%s script exec: %s EOF ERROR %s" %('#'*10, server_ip, '#'*10)
elif index == 2:
print "%s script exec: %s TIMEOUT ERROR %s" %('#'*10, server_ip, '#'*10)
send_email_succeed(recipientas_list,addresser,itheme,email_server_IP,log_file_name,server_ip,local_file_md5,target_file_md5)
else:
send_email_fail(recipientas_list,addresser,itheme,email_server_IP,log_file_name,server_ip,local_file_md5,target_file_md5)
此腳本的功能說明以下:
1,將構建好的代碼包,用rsync同步到生產環境服務器中轉目錄program;
2,經過ssh登陸生產環境服務器,調用program_update.sh腳本,將中轉目錄program下的代碼更新到站點程序目錄,更新完以後若是站點訪問有BUG,那麼能夠將program下的前一個版本代碼進行回滾;
3,構建的代碼包和上傳到生產環境的代碼包經過md5校驗一致性;
4,項目構建發佈成功後,發郵件給項目相關人員;
5,執行此腳本的日誌輸出到文件,能夠進行查看問題;
program_update.sh
#!/bin/sh cd /data/program/passport /usr/bin/docker cp ROOT.war passport-web:/data/www/skyid/ mv ROOT.war ROOT`date +"%Y-%m-%d_%H-%M-%S"`.war
關於jenkins平臺的項目構建操做很簡單,但願個人文章能夠給你們有所幫助,若有問題能夠諮詢我,也但願你們給予建議!