1.1 Jenkins安装与下载应用代码
应用部署
jenkins为分发服务器
[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 添加公网代码库
Local module directory wordpress-$WP_VER
点击 保存
WP_VER3.6 (3.6为其中一个版本)
点击开始构建
点击构建历史
有 console output
控制台输出 显示结果
[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 上
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环境,在这一集里,我们说一说如何管理节点. 进入“系统管理”中的“管理节点”. 创建Windos系统的奴隶节点 先创建一台安装了Win7系统的虚拟机,作为Jen ...
- 测试思想-流程规范 SVN代码管理与版本控制
SVN代码管理与版本控制 by:授客 QQ:1033553122 欢迎加入软件性能测试交流群(QQ群):7156436 目录 一. 二. 三. 四. 五. 六. 七. 一. 创建根目录 创建一 ...
- GIt+jenkins代码自动上线
代码自动上线功能 企业部署代码上线是件比较麻烦的事情,还好我们有jenkins这个持续集成的软件可以帮助我们做很多的事情,现在我们就 来测试用jenkins推送代码上线. 我们这里测试的是一个html ...
- jenkins代码自动部署
jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各种语言(比如:java, ...
- Jenkins源代码管理(SVN)
Subversion 安装插件 1.首先将本地的自动化用例打包上传svn 2.配置jenkins源代码管理(每次执行jenkins时,会自动check-out配置地址中的代码到Jenkins的工作空间 ...
- 我与Git的那些破事--代码管理实践
1. Git是什么? 作为一名程序猿,我相信大家都或多或少接触过git--分布式版本控制软件. 有人说,它是目前世界上最先进的分布式版本控制系统,我想说,是否最先进不知道,但确实好用,实用. 作为一款 ...
- Jenkins 源代码管理(SVN)
Subversion 安装插件 1.首先将本地的自动化用例打包上传 svn 2.配置 jenkins 源代码管理(每次执行 jenkins 时,会自动 check-ou t配置地址中的代码到 Jenk ...
- 我与Git的那些破事(上)--代码管理
1. Git是什么? 作为一名程序猿,我相信大家都或多或少接触过git--分布式版本控制软件. 有人说,它是目前世界上最先进的分布式版本控制系统,我想说,是否最先进不知道,但确实好用,实用. 作为一款 ...
- Jenkins代码自动部署相关文档
环境 centos 7.0+ Java JDK 1.8+ jenkins 2.220 maven 3.0+ git 1.8+ 注意事项 一. linux 安装 JDK (jdk-8u201-linux ...
随机推荐
- 现代 PHP 新特性系列(三) —— Trait 概览
Trait是PHP 5.4引入的新概念,看上去既像类又像接口,其实都不是,Trait可以看做类的部分实现,可以混入一个或多个现有的PHP类中,其作用有两个:表明类可以做什么:提供模块化实现.Trait ...
- dede织梦栏目页和文章页中获取当前栏目名称方法
一般情况下,在dede织梦系统中列表页.栏目页和文章页中获取当前所在栏目名称只需要代码:{dede:type}[field:typename]{/dede:type}即可,不需要定义ID,默认的就是当 ...
- Anaconda入门教程
Anaconda入门教程 [TOC] Anaconda是什么 Anaconda附带了 conda.Python 和 150 多个科学包及其依赖项.应用程序 conda 是包和环境管理器.Anacond ...
- eclipse出现错误:he type java.util.Map$Entry cannot be resolved. It is indirectly referenced
eclipse出现错误:he type java.util.Map$Entry cannot be resolved. It is indirectly referenced jre 换成6的就好了选 ...
- 数据结构 链式哈希表(Hash Table)的接口定义与实现分析(完整代码)
链式哈希表的接口定义 关于哈希表与链式哈希表的描述可以参阅:http://www.cnblogs.com/idreamo/p/7990860.html 链式哈希表的操作与属性有:初始化.销毁.插入元素 ...
- ArcGIS字段计算器分割字段中的字符串
ArcGIS字段计算器分割字段中的字符串 实例:在一个字段中存有长宽高三个属性数据,以x分割.例如sss为字段名(字段属性为文本类型),数据格式为:100x200x300,利用字段计算器分别将三个数存 ...
- JQ滚动特效
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- SQL Server中的变更捕获技术--简单部署
------准备------ CREATE DATABASE db_test_cdc ,) ,name )); INSERT INTO t1(name)VALUES('test') ------开始- ...
- MS SQL 日志记录管理
MS SQL的日志信息/日志记录,可能对你来说,既熟悉又陌生,熟悉是因为你可能一直都在使用,查看.关注一些日志信息/记录,例如,作业历史记录:陌生是因为你可能从不关注日志信息/记录的管理,这里我一直用 ...
- zabbix-proxy搭建
环境: 因为公司需要监控远程客户机,但server端无法主动连接agent端,客户端可以连接公司ip 公司有固定ip,可以开放某个端口给zabbixserver,客户机agent端可以主动通过外网连接 ...