Jenkins代码管理
Posted Oops!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins代码管理相关的知识,希望对你有一定的参考价值。
1.1 Jenkins安装与下载应用代码
应用部署
jenkins为分发服务器
[root@master ~]# wget http://pkg.jenkins-ci.org/redhat-stable/jenkins-2.7.2-1.1.noarch.rpm
[root@master ~]# rpm -ivh jenkins-2.7.2-1.1.noarch.rpm
[root@weifeng02 local]# yum install -y java
[root@master ~]# /etc/init.d/jenkins restart
[root@master ~]# /etc/init.d/jenkins status
jenkins (pid 21371) is running...
[root@weifeng01 ~]# netstat -lnp|grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1460/java
tcp 0 0 :::8009 :::* LISTEN 1460/java
tcp 0 0 :::8080 :::* LISTEN 4816/java
tcp 0 0 :::80 :::* LISTEN 1460/java
tcp 0 0 :::48599 :::* LISTEN 4816/java
http://192.168.100.125:8080/
可进入登录页面
创建一个新任务
item name: wordpress-build
构建一个自由风格的软件项目
选择 参数化构建过程
string parameter
名字: WP_VER
源码管理
Subversion (没有选择安装插件 subversion)
Repository URL 添加公网代码库
wordpress svn代码库网址 http://core.svn.wordpress.org/tags/
Repository URL http://core.svn.wordpress.org/tags/$WP_VER
Local module directory wordpress-$WP_VER
点击 保存
WP_VER3.6 (3.6为其中一个版本)
点击开始构建
点击构建历史
有 console output
控制台输出 显示结果
Finished: SUCCESS 表示成功
[root@weifeng01 wordpress-build]# pwd
/var/lib/jenkins/workspace/wordpress-build
[root@weifeng01 wordpress-build]# ls
wordpress-3.6
1.2 Jenkins管理应用代码
点击 wordpress-bulid 配置
选择 构建 Execute shell (可执行一些脚本命令)
command: (进行打包备份)
target=/var/www/html/deploy/packages/wordpress-$WP_VER.tar.gz
tar zcf $target wordpress-$WP_VER
需先创建目录
[root@weifeng01 wordpress-build]# mkdir -p /var/www/html/deploy/packages/
[root@weifeng01 wordpress-build]# cd /var/www/html
[root@weifeng01 html]# chown jenkins.jenkins deploy/ -R
[root@weifeng01 html]# ll
total 4
drwxr-xr-x 3 jenkins jenkins 4096 Dec 4 01:24 deploy
重新构建含 execute shell 脚本命令
WP_VER3.6
点击开始构建
控制台输出 显示
Finished: SUCCESS 表示成功。
[root@weifeng01 html]# cd deploy/
[root@weifeng01 deploy]# cd packages/
[root@weifeng01 packages]# ls
wordpress-3.6.tar.gz
[root@weifeng01 packages]# ll
total 10712
-rw-r--r-- 1 jenkins jenkins 10968855 Dec 4 01:32 wordpress-3.6.tar.gz
已生成tar的压缩文件,真实服务器real server可下载这个文件。下载完文件跟网上文件进行md5的对比,是否一样。
[root@weifeng01 packages]# md5sum wordpress-3.6.tar.gz
7f34ecda22782820fd4b933292cadb3a wordpress-3.6.tar.gz
继续配置 excute shell command
命令行
target=/var/www/html/deploy/packages/wordpress-$WP_VER.tar.gz
tar zcf $target wordpress-$WP_VER
md5file=$target.md5
md5sum $target | cut -d \' \' -f 1 > $md5file
再重新构建
开始构建后:
Finished: SUCCESS
[root@weifeng01 packages]# ls
wordpress-3.6.tar.gz wordpress-3.6.tar.gz.md5
[root@weifeng01 packages]# cat wordpress-3.6.tar.gz.md5
cdb5b03bb5cf87f97fb3ceb3b1a27de1
[root@weifeng01 packages]# md5sum wordpress-3.6.tar.gz
cdb5b03bb5cf87f97fb3ceb3b1a27de1 wordpress-3.6.tar.gz
[root@weifeng01 packages]# md5sum wordpress-3.6.tar.gz |cut -d\\ -f 1
cdb5b03bb5cf87f97fb3ceb3b1a27de1
再将版本号写入文件里
target=/var/www/html/deploy/packages/wordpress-$WP_VER.tar.gz
tar zcf $target wordpress-$WP_VER
md5file=$target.md5
md5sum $target | cut -d \' \' -f 1 > $md5file
echo $WP_VER > /var/www/html/deploy/lastver
重新构建
Finished: SUCCESS
[root@weifeng01 deploy]# ls
lastver packages
[root@weifeng01 deploy]# cat lastver
3.6
重新创建一个新的项目
wordpress-live
构建一个自由风格的软件项目
选择 :
参数化构建过程 名字 WP_VER
构建: execute shell
command: echo $WP_VER > /var/www/html/deploy/livever
构建3.6版本
Finished: SUCCESS
[root@weifeng01 deploy]# ls
lastver livever packages
[root@weifeng01 deploy]# cat livever
3.6
lastver 可供客户端下载的,livever 供客户端应用的
1.3 客户端下载应用代码
客户端检查ivever和lastver
[root@localhost day14]# service httpd start
启动httpd 服务
可查看到相关文档
[root@weifeng01 day14]# vim deploy.py
#!/usr/bin/env python
import os
import sys
import urllib, urllib2
import hashlib
import tarfile
import shutil
URL_LASTVER = "http://192.168.100.125/deploy/lastver"
URL_LIVEVER = "http://192.168.100.125/deploy/livever"
URL_PKG = "http://192.168.100.125/deploy/packages/"
DOWNLOAD_DIR = "/var/www/download"
DEPLOY_DIR = "/var/www/deploy"
APP_NAME = "wordpress"
DOC_ROOT = \'/var/www/html/current\'
TOBE_KEEP = 2
WHITE = []
def init():
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
if not os.path.exists(DEPLOY_DIR):
os.makedirs(DEPLOY_DIR)
def getURL(url):
return urllib2.urlopen(url).read().strip()
def checkLastVersion():
lastver = getURL(URL_LASTVER)
url_pkg_path = URL_PKG + "%s-%s.tar.gz" % (APP_NAME, lastver)
pkg_path = os.path.join(DOWNLOAD_DIR, "%s-%s.tar.gz" % (APP_NAME, lastver))
if not os.path.exists(pkg_path):
data = getURL(url_pkg_path)
with open(pkg_path, \'w\') as fd:
fd.write(data)
if __name__ == "__main__":
init()
checkLastVersion()
执行:
[root@weifeng01 day14]# python deploy.py
[root@weifeng01 day14]# ls /var/www/download/
wordpress-3.6.tar.gz
[root@weifeng01 day14]# cat /var/www/html/deploy/lastver
3.6
[root@weifeng01 day14]# md5sum /var/www/download/wordpress-3.6.tar.gz
1e0403c4590eee50c16b7fbb0fb64175 /var/www/download/wordpress-3.6.tar.gz
[root@weifeng01 day14]# cat /var/www/html/deploy/packages/wordpress-3.6.tar.gz.md5
1e0403c4590eee50c16b7fbb0fb64175
下载tar压缩文件进行校验,显示与原文件一致。
1.4 对下载文件进行校验
修改下载方式每次读取4097,减少对内存的占用
[root@weifeng01 day14]# vim deploy.py
#!/usr/bin/env python
import os
import sys
import urllib, urllib2
import hashlib
import tarfile
import shutil
URL_LASTVER = "http://192.168.1.125/deploy/lastver"
URL_LIVEVER = "http://192.168.1.125/deploy/livever"
URL_PKG = "http://192.168.1.125/deploy/packages/"
DOWNLOAD_DIR = "/var/www/download"
DEPLOY_DIR = "/var/www/deploy"
APP_NAME = "wordpress"
DOC_ROOT = \'/var/www/html/current\'
TOBE_KEEP = 2
WHITE = []
def init():
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
if not os.path.exists(DEPLOY_DIR):
os.makedirs(DEPLOY_DIR)
def getURL(url):
return urllib2.urlopen(url).read().strip()
def checkLastVersion():
lastver = getURL(URL_LASTVER)
url_pkg_path = URL_PKG + "%s-%s.tar.gz" % (APP_NAME, lastver)
pkg_path = os.path.join(DOWNLOAD_DIR, "%s-%s.tar.gz" % (APP_NAME, lastver))
if not os.path.exists(pkg_path):
if not download(pkg_path, url_pkg_path):
return False
print "Sucess"
def download(fn, url_pkg_path):
url_pkg_path_md5 = url_pkg_path + \'.md5\'
md5 = getURL(url_pkg_path_md5)
req = urllib2.urlopen(url_pkg_path)
n = 1
while True:
data = req.read(4096)
if not data:break
if n == 1:
with open(fn, \'wb\') as fd:
fd.write(data)
n += 1
elif n > 1:
with open(fn, \'a\') as fd:
fd.write(data)
n += 1
if checkFileSum(fn, md5):
return True
return False
def checkFileSum(fn, md5):
with open(fn) as fd:
m = hashlib.md5(fd.read()).hexdigest()
if m == md5:
return True
return False
if __name__ == "__main__":
init()
checkLastVersion()
执行结果
[root@weifeng01 day14]# rm -rf /var/www/download/
[root@weifeng01 day14]# ls
deploy.py
[root@weifeng01 day14]# vim deploy.py
[root@weifeng01 day14]# python deploy.py
Sucess
[root@weifeng01 day14]# ls /var/www/download/
wordpress-3.6.tar.gz
检验大小
[root@weifeng01 day14]# ls /var/www/download/ -l
total 10732
-rw-r--r--. 1 root root 10988498 Nov 13 11:08 wordpress-3.6.tar.gz
[root@weifeng01 day14]# !vim
vim deploy.py
[root@weifeng01 day14]# ll /var/www/html/deploy/packages/wordpress-3.6.tar.gz
-rw-r--r--. 1 jenkins jenkins 10988498 Nov 12 22:23 /var/www/html/deploy/packages/wordpress-3.6.tar.gz
检验md5值
[root@weifeng01 day14]# md5sum /var/www/download/wordpress-3.6.tar.gz
1e0403c4590eee50c16b7fbb0fb64175 /var/www/download/wordpress-3.6.tar.gz
[root@weifeng01 day14]# cd /var/www/html/deploy/packages
[root@weifeng01 packages]# ls
wordpress-3.6.tar.gz wordpress-3.6.tar.gz.md5
[root@weifeng01 packages]# cat wordpress-3.6.tar.gz.md5
1e0403c4590eee50c16b7fbb0fb64175
1.5 tarfile模块使用
用于解压
创建归档文件 tar文件
In [1]: import tarfile
In [2]: tarfile.open(\'/tmp/1.tar\',\'w\')
Out[2]: <tarfile.TarFile at 0x34b6290>
In [3]: t = tarfile.open(\'/tmp/1.tar\',\'w\')
In [4]: t.add(\'/etc/passwd\')
In [5]: t.add(\'/etc/hosts\')
In [6]: t.close()
[root@weifeng08 ~]# cd /tmp
[root@weifeng08 tmp]# ls
1.tar 1.txt etc hsperfdata_root tmp.6xnaKaSM91
[root@weifeng08 tmp]# ll 1.tar
-rw-r--r--. 1 root root 10240 Jan 22 04:39 1.tar
[root@weifeng08 tmp]# tar tf 1.tar
etc/passwd
etc/hosts
解压
In [8]: t = tarfile.open(\'/tmp/1.tar\')
In [9]: t.extractall()
In [10]: t.extractall(path=\'/tmp\')
[root@weifeng08 tmp]# ls
1.tar 1.txt etc hsperfdata_root tmp.6xnaKaSM91
[root@weifeng08 tmp]# cd etc/
[root@weifeng08 etc]# ls
hosts passwd
创建压缩文件
In [12]: t = tarfile.open(\'test.tar.gz\',\'w:gz\')
In [13]: t.add(\'/etc/shadow\')
In [16]: t.add(\'/etc/host.conf\')
In [17]: t.close()
[root@weifeng08 ~]# tar zft test.tar.gz
etc/shadow
etc/host.conf
#!/usr/bin/env python
import os
import sys
import urllib, urllib2
import hashlib
import tarfile
import shutil
URL_LASTVER = "http://192.168.1.125/deploy/lastver"
URL_LIVEVER = "http://192.168.1.125/deploy/livever"
URL_PKG = "http://192.168.1.125/deploy/packages/"
DOWNLOAD_DIR = "/var/www/download"
DEPLOY_DIR = "/var/www/deploy"
APP_NAME = "wordpress"
DOC_ROOT = \'/var/www/html/current\'
TOBE_KEEP = 2
WHITE = []
def init():
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
if not os.path.exists(DEPLOY_DIR):
os.makedirs(DEPLOY_DIR)
def getURL(url):
return urllib2.urlopen(url).read().strip()
def checkLastVersion():
lastver = getURL(URL_LASTVER)
url_pkg_path = URL_PKG + "%s-%s.tar.gz" % (APP_NAME, lastver)
pkg_path = os.path.join(DOWNLOAD_DIR, "%s-%s.tar.gz" % (APP_NAME, lastver))
if not os.path.exists(pkg_path):
if not download(pkg_path, url_pkg_path):
return False
extract_dir = os.path.join(DEPLOY_DIR, "%s-%s" % (APP_NAME, lastver))
if not os.path.exists(extract_dir):
pkg_deploy(pkg_path, DEPLOY_DIR)
def download(fn, url_pkg_path):
url_pkg_path_md5 = url_pkg_path + \'.md5\'
md5 = getURL(url_pkg_path_md5)
req = urllib2.urlopen(url_pkg_path)
n = 1
while True:
data = req.read(4096)
if not data:break
if n == 1:
with open(fn, \'wb\') as fd:
fd.write(data)
n += 1
elif n > 1:
with open(fn, \'a\') as fd:
fd.write(data)
n += 1
if checkFileSum(fn, md5):
return True
return False
def checkFileSum(fn, md5):
with open(fn) as fd:
m = hashlib.md5(fd.read()).hexdigest()
if m == md5:
return True
return False
def pkg_deploy(fn,d):
tar = tarfile.open(fn)
tar.extractall(path=d)
if __name__ == "__main__":
init()
checkLastVersion()
程序用到url 必须开启httpd, lastver, livever 为jenkisn服务器端文件目录
[root@weifeng01 day14]# ls /var/www/download/
wordpress-3.6.tar.gz
[root@weifeng01 day14]# ls /var/www/deploy/
wordpress-3.6
[root@weifeng01 day14]# du -sh /var/www/deploy/wordpress-3.6/
43M /var/www/deploy/wordpress-3.6/
将文件解压分发到 deploy 目录下
1.6 使用符号链接发布应用
[root@weifeng01 day14]# ls /var/www/deploy/
wordpress-3.6
[root@weifeng01 day14]# curl http://192.168.1.125/deploy/livever
3.6
[root@weifeng01 day14]# curl http://192.168.1.125/deploy/lastver
3.6
[root@weifeng01 day14]# cd /var/www/html/
[root@weifeng01 html]# ls
deploy zabbix
使用符合链接将文件链接到 html目录下
符合链接的用法:
In [18]: import os
In [19]: os.symlink(\'/etc/passwd\', \'/tmp/p.txt\')
In [20]: ls /tmp/p.txt
/tmp/p.txt@
In [21]: ls -l /tmp/p.txt
lrwxrwxrwx 1 root root 11 Jan 22 06:36 /tmp/p.txt -> /etc/passwd
In [22]: os.readlink(\'/tmp/p.txt\') python下 读链接文件
Out[22]: \'/etc/passwd\'
In [23]: os.unlink(\'/tmp/p.txt\') 取消链接文件
代码:
[root@weifeng01 day14]# vim deploy2.py
#!/usr/bin/env python
import os
import sys
import urllib, urllib2
import hashlib
import tarfile
import shutil
URL_LASTVER = "http://192.168.1.125/deploy/lastver"
URL_LIVEVER = "http://192.168.1.125/deploy/livever"
URL_PKG = "http://192.168.1.125/deploy/packages/"
DOWNLOAD_DIR = "/var/www/download"
DEPLOY_DIR = "/var/www/deploy"
APP_NAME = "wordpress"
DOC_ROOT = \'/var/www/html/current\'
TOBE_KEEP = 2
WHITE = []
def init():
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
if not os.path.exists(DEPLOY_DIR):
os.makedirs(DEPLOY_DIR)
def getURL(url):
return urllib2.urlopen(url).read().strip()
def checkLastVersion():
lastver = getURL(URL_LASTVER)
url_pkg_path = URL_PKG + "%s-%s.tar.gz" % (APP_NAME, lastver)
pkg_path = os.path.join(DOWNLOAD_DIR, "%s-%s.tar.gz" % (APP_NAME, lastver))
if not os.path.exists(pkg_path):
if not download(pkg_path, url_pkg_path):
return False
extract_dir = os.path.join(DEPLOY_DIR, "%s-%s" % (APP_NAME, lastver))
if not os.path.exists(extract_dir):
pkg_deploy(pkg_path, DEPLOY_DIR)
def download(fn, url_pkg_path):
url_pkg_path_md5 = url_pkg_path + \'.md5\'
md5 = getURL(url_pkg_path_md5)
req = urllib2.urlopen(url_pkg_path)
n = 1
while True:
data = req.read(4096)
if not data:break
if n == 1:
with open(fn, \'wb\') as fd:
fd.write(data)
n += 1
elif n > 1:
with open(fn, \'a\') as fd:
fd.write(data)
n += 1
if checkFileSum(fn, md5):
return True
return False
def checkFileSum(fn, md5):
with open(fn) as fd:
m = hashlib.md5(fd.read()).hexdigest()
if m == md5:
return True
return False
def pkg_deploy(fn,d):
tar = tarfile.open(fn)
tar.extractall(path=d)
def checkLiveVersion():
livever = getURL(URL_LIVEVER)
pkg_path = os.path.join(DEPLOY_DIR, "%s-%s" % (APP_NAME, livever))
if os.path.exists(pkg_path):
if os.path.exists(DOC_ROOT):
target = os.readlink(DOC_ROOT)
if target != pkg_path:
os.unlink(DOC_ROOT)
os.symlink(pkg_path, DOC_ROOT)
else:
os.symlink(pkg_path, DOC_ROOT)
if __name__ == "__main__":
init()
checkLastVersion()
checkLiveVersion()
[root@weifeng01 day14]# python deploy2.py
[root@weifeng01 day14]# cd /var/www/html
[root@weifeng01 html]# ls
current deploy zabbix
[root@weifeng01 html]# ll
total 8
lrwxrwxrwx 1 root root 29 Jan 19 22:14 current -> /var/www/deploy/wordpress-3.6
drwxr-xr-x. 3 jenkins jenkins 4096 Nov 12 22:25 deploy
drwxr-xr-x. 13 root root 4096 Dec 10 22:35 zabbix
[root@weifeng01 day14]# cd /etc/httpd/conf.d
[root@weifeng01 conf.d]# ls
README welcome.conf
[root@weifeng01 conf.d]# vim wp.conf
Alias /wp "/var/www/html/current"
[root@weifeng01 conf.d]# service httpd restart
打开 http://192.168.1.125/wp/ 显示有文件
jenkins 上
wordpress-live 上
Build with Parameters 构建3.7 的版本
WP_VER 3.7 开始构建
[root@weifeng01 day14]# curl http://192.168.1.125/deploy/livever
3.7
livever 变成 了 3.7
[root@weifeng01 day14]# ls /var/www/html/ -l
total 8
lrwxrwxrwx 1 root root 29 Jan 19 22:14 current -> /var/www/deploy/wordpress-3.6
drwxr-xr-x. 3 jenkins jenkins 4096 Nov 12 22:25 deploy
drwxr-xr-x. 13 root root 4096 Dec 10 22:35 zabbix
[root@weifeng01 day14]# python deploy2.py
[root@weifeng01 day14]# ls /var/www/html/ -l
total 8
lrwxrwxrwx 1 root root 29 Jan 19 22:37 current -> /var/www/deploy/wordpress-3.7
执行脚本后链接文件改成了 3.7
如发现新版本 3.7有bug , 操作回复到旧版本 3.6
jenkins里 重新构建一个3.6 的 livever , 再执行脚本 deploy.py (实际环境可以 cron) , 版本可回滚到 3.6
1.7 版本控制
对版本进行排序
In [7]: from distutils.version import LooseVersion
In [10]: a = LooseVersion(\'3.6\')
In [11]: print a
3.6
In [12]: a = LooseVersion(\'3.7\')
In [13]: print a
3.7
In [14]: a
Out[14]: LooseVersion (\'3.7\')
In [15]: l
Out[15]: [\'3.11\', \'3.5.2\', \'3.6\', \'3.6a\', \'3.7\']
In [16]: [i for i in l]
Out[16]: [\'3.11\', \'3.5.2\', \'3.6\', \'3.6a\', \'3.7\']
In [17]: [LooseVersion(i) for i in l]
Out[17]:
[LooseVersion (\'3.11\'),
LooseVersion (\'3.5.2\'),
LooseVersion (\'3.6\'),
LooseVersion (\'3.6a\'),
LooseVersion (\'3.7\')]
In [18]: vs = [LooseVersion(i) for i in l]
In [19]: vs
Out[19]:
[LooseVersion (\'3.11\'),
LooseVersion (\'3.5.2\'),
LooseVersion (\'3.6\'),
LooseVersion (\'3.6a\'),
LooseVersion (\'3.7\')]
In [20]: vs.sort()
In [21]: vs
Out[21]:
[LooseVersion (\'3.5.2\'),
LooseVersion (\'3.6\'),
LooseVersion (\'3.6a\'),
LooseVersion (\'3.7\'),
LooseVersion (\'3.11\')]
In [22]: vs[0].vstring
Out[22]: \'3.5.2\'
删除一个目录
In [23]: import shutil
In [24]: shutil.rmtree(\'/tmp/abc\')
最少保留2个版本, lastver,livever的版本不能被删除
[root@weifeng01 ~]# mkdir /var/www/deploy/wordpess-3.8
[root@weifeng01 ~]# ls /var/www/deploy/
wordpess-3.8 wordpress-3.5.2 wordpress-3.6 wordpress-3.7
[root@weifeng01 ~]# touch /var/www/download/wordpress-3.8.tar.gz
[root@weifeng01 ~]#
[root@weifeng01 ~]# ls /var/www/download/
wordpress-3.5.2.tar.gz wordpress-3.6.tar.gz wordpress-3.7.tar.gz wordpress-3.8.tar.gz
[root@weifeng01 ~]# ll /var/www/html
total 8
lrwxrwxrwx 1 root root 29 Jan 19 22:37 current -> /var/www/deploy/wordpress-3.7
drwxr-xr-x. 3 jenkins jenkins 4096 Nov 12 22:25 deploy
drwxr-xr-x. 13 root root 4096 Dec 10 22:35 zabbix
[root@weifeng01 ~]# curl http://192.168.1.125/deploy/livever
3.7
[root@weifeng01 ~]# curl http://192.168.1.125/deploy/lastver
3.5.2
执行脚本后应该保留最新的2个版本 3.7,3.8, 3.5.2版本为lastver ,所以最好将删除3.6版本
#!/usr/bin/env python
import os
import sys
import urllib, urllib2
import hashlib
import tarfile
import shutil
URL_LASTVER = "http://192.168.1.125/deploy/lastver"
URL_LIVEVER = "http://192.168.1.125/deploy/livever"
URL_PKG = "http://192.168.1.125/deploy/packages/"
DOWNLOAD_DIR = "/var/www/download"
DEPLOY_DIR = "/var/www/deploy"
APP_NAME = "wordpress"
DOC_ROOT = \'/var/www/html/current\'
TOBE_KEEP = 2
WHITE = []
def init():
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
if not os.path.exists(DEPLOY_DIR):
os.makedirs(DEPLOY_DIR)
def getURL(url):
return urllib2.urlopen(url).read().strip()
def checkLastVersion():
lastver = getURL(URL_LASTVER)
WHITE.append(lastver)
url_pkg_path = URL_PKG + "%s-%s.tar.gz" % (APP_NAME, lastver)
pkg_path = os.path.join(DOWNLOAD_DIR, "%s-%s.tar.gz" % (APP_NAME, lastver))
if not os.path.exists(pkg_path):
if not download(pkg_path, url_pkg_path):
return False
extract_dir = os.path.join(DEPLOY_DIR, "%s-%s" % (APP_NAME, lastver))
if not os.path.exists(extract_dir):
pkg_deploy(pkg_path, DEPLOY_DIR)
def download(fn, url_pkg_path):
url_pkg_path_md5 = url_pkg_path + \'.md5\'
md5 = getURL(url_pkg_path_md5)
req = urllib2.urlopen(url_pkg_path)
n = 1
while True:
data = req.read(4096)
if not data:break
if n == 1:
with open(fn, \'wb\') as fd:
fd.write(data)
n += 1
elif n > 1:
with open(fn, \'a\') as fd:
fd.write(data)
n += 1
if checkFileSum(fn, md5):
return True
return False
def checkFileSum(fn, md5):
with open(fn) as fd:
m = hashlib.md5(fd.read()).hexdigest()
if m == md5:
return True
return False
def pkg_deploy(fn,d):
tar = tarfile.open(fn)
tar.extractall(path=d)
def checkLiveVersion():
livever = getURL(URL_LIVEVER)
WHITE.append(livever)
pkg_path = os.path.join(DEPLOY_DIR, "%s-%s" % (APP_NAME, livever))
if os.path.exists(pkg_path):
if os.path.exists(DOC_ROOT):
target = os.readlink(DOC_ROOT)
if target != pkg_path:
os.unlink(DOC_ROOT)
os.symlink(pkg_path, DOC_ROOT)
else:
os.symlink(pkg_path, DOC_ROOT)
def versionSort(l):
from distutils.version import LooseVersion
vs = [LooseVersion(i) for i in l]
vs.sort()
return [i.vstring for i in vs]
def clean():
download_list = [i.split(\'-\')[1][:-7] for i in os.listdir(DOWNLOAD_DIR)]
deploy_list = [i.split(\'-\')[1] for i in os.listdir(DEPLOY_DIR)]
tobe_del_download = versionSort(download_list)[:-TOBE_KEEP]
tobe_del_deploy = versionSort(deploy_list)[:-TOBE_KEEP]
for d in tobe_del_download:
fn = os.path.join(DOWNLOAD_DIR, "%s-%s.tar.gz" % (APP_NAME, d))
if d not in WHITE:
os.remove(fn)
for d in tobe_del_deploy:
fn = os.path.join(DEPLOY_DIR, "%s-%s" % (APP_NAME, d))
if d not in WHITE:
shutil.rmtree(fn)
if __name__ == "__main__":
init()
checkLastVersion()
checkLiveVersion()
clean()
[root@weifeng01 day14]# python deploy2.py
[root@weifeng01 ~]# ls /var/www/download/
wordpress-3.5.2.tar.gz wordpress-3.7.tar.gz wordpress-3.8.tar.gz
[root@weifeng01 ~]# ls /var/www/deploy/
wordpess-3.8 wordpress-3.5.2 wordpress-3.7
删除了版本 3.6
文件锁 : deploy.py 脚本执行时,进行锁定
脚本中增加 函数
def lockfile(f):
if os.path.exists(f):
print "%s is running...." % __file__
sys.exit()
with open(f, \'w\') as fd:
fd.write(str(os.getpid()))
def unlockfile(f):
if os.path.exists(f):
os.remove(f)
if __name__ == "__main__":
lockfile(\'/tmp/deploy.lock\')
init()
checkLastVersion()
checkLiveVersion()
clean()
import time
time.sleep(60)
unlockfile(\'/tmp/deploy.lock\')
执行结果:
[root@weifeng01 day14]# python deploy2.py
另外一台终端操作
[root@weifeng01 day14]# python deploy2.py
deploy2.py is running....
增加crontab
[root@weifeng01 day14]# crontab -e
* * * * * python /root/day14/deploy2.py
每分钟执行一次
1.9 使用fabirc状态检查
|
Fabric 版本
pip install "pycrypto==2.3"
pip install paramiko==1.12.4
pip install fabric==1.8.3
[root@weifeng01 day14]# vim fabfile.py
from fabric.api import run
def host_type():
run(\'uname -s\')
[root@weifeng01 day14]# fab -H localhost host_type
[localhost] Executing task \'host_type\'
[localhost] run: uname -s
[localhost] Login password for \'root\':
[localhost] out: Linux
[localhost] out:
Done.
Disconnecting from localhost... done.
创建一个test 用户
[root@weifeng01 day14]# useradd test
[root@weifeng01 day14]# passwd test
Changing password for user test.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
You have new mail in /var/spool/mail/root
[root@weifeng01 day14]# grep test /etc/passwd
badtest:x:501:501::/home/badtest:/bin/bash
test:x:502:502::/home/test:/bin/bash
增加用户, 主机等信息
[root@weifeng01 day14]# vim fabfile.py
from fabric.api import run
from fabric.api import env
env.user = \'test\'
env.password = \'test\'
env.hosts = [\'localhost\',\'192.168.1.125\']
def host_type():
run(\'uname -s\')
run(\'date\')
再执行命令 显示相关信息
[root@weifeng01 day14]# fab host_type
[localhost] Executing task \'host_type\'
[localhost] run: uname -s
[localhost] out: Linux
[localhost] out:
[localhost] run: date
[localhost] out: Fri Jan 20 12:25:05 CST 2017
[localhost] out:
[192.168.1.125] Executing task \'host_type\'
[192.168.1.125] run: uname -s
[192.168.1.125] out: Linux
[192.168.1.125] out:
[192.168.1.125] run: date
[192.168.1.125] out: Fri Jan 20 12:25:06 CST 2017
[192.168.1.125] out:
Done.
Disconnecting from 192.168.1.125... done.
Disconnecting from localhost... done.
将farbric 函数用到jenkins, 执行有提示
Jenkins 新建 Item名称 wordpress-check
选中 构建一个自由风格的软件项目
不需要参数,只需要构建 shell 脚本
[root@weifeng01 day14]# cd /var/www/html/
You have new mail in /var/spool/mail/root
[root@weifeng01 html]# ls
current deploy zabbix
[root@weifeng01 html]# mkdir scripts
[root@weifeng01 html]# ls
current deploy scripts zabbix
创建 scripts 文件夹
[root@weifeng01 html]# cp /root/day14/fabfile.py .
[root@weifeng01 html]# ls
current deploy fabfile.py scripts zabbix
[root@weifeng01 html]# vim fabfile.py
修改脚本
from fabric.api import run
from fabric.api import env
env.user = \'test\'
env.password = \'test\'
env.hosts = [\'localhost\',\'192.168.1.125\']
def check_var():
run("""LIVE_VER=`curl -s http://192.168.1.125/deploy/livever`
LIVE_WP=/var/www/deploy/wordpress-$LIVE_VER
test -d $LIVE_WP && echo "$LIVE_WP is exists"
""")
执行测试下
[root@weifeng01 html]# fab check_var
[localhost] Executing task \'check_var\'
[localhost] run: LIVE_VER=`curl -s http://192.168.1.125/deploy/livever`
LIVE_WP=/var/www/deploy/wordpress-$LIVE_VER
test -d $LIVE_WP && echo "$LIVE_WP is exists"
[localhost] out: /var/www/deploy/wordpress-3.7 is exists
[localhost] out:
[192.168.1.125] Executing task \'check_var\'
[192.168.1.125] run: LIVE_VER=`curl -s http://192.168.1.125/deploy/livever`
LIVE_WP=/var/www/deploy/wordpress-$LIVE_VER
test -d $LIVE_WP && echo "$LIVE_WP is exists"
[192.168.1.125] out: /var/www/deploy/wordpress-3.7 is exists
[192.168.1.125] out:
Done.
Disconnecting from 192.168.1.125... done.
Disconnecting from localhost... done.
在jenkins 测试下
wordpress_check 立即构建
控制台输出
[192.168.1.125] out: /var/www/deploy/wordpress-3.7 is exists
[192.168.1.125] out:
Done.
Disconnecting from 192.168.1.125... done.
Disconnecting from localhost... done.
Finished: SUCCESS
~
2.0 整体测试
先构建项目 wordpress-build
如 4.4
构建项目会保存到 lastver
控制台输出
Finished: SUCCESS后
脚本会cron自动执行
[root@weifeng01 html]# ls /var/www/download/
wordpress-3.7.tar.gz wordpress-3.8.tar.gz wordpress-4.4.tar.gz
You have new mail in /var/spool/mail/root
[root@weifeng01 html]# ls /var/www/deploy/
wordpess-3.8 wordpress-3.7 wordpress-4.4
下一步检测
wordpress-check 构建项目 控制台输出 success
再执行 wordpress-live
显示 控制台输出 success
[root@weifeng01 html]# ls /var/www/html/ -l
total 20
lrwxrwxrwx 1 root root 29 Jan 20 15:05 current -> /var/www/deploy/wordpress-4.4
已经进行更改
[root@weifeng01 html]# curl http://192.168.1.125/deploy/livever
4.4
You have new mail in /var/spool/mail/root
[root@weifeng01 html]# curl http://192.168.1.125/deploy/lastver
4.4
以上是关于Jenkins代码管理的主要内容,如果未能解决你的问题,请参考以下文章
持续集成+自动化部署[代码流水线管理及Jenkins和gitlab集成]
Jenkins+Ant+Gitlab+Sonarqube+Docker实现持续集成,质量管理