Jenkins代码管理

1.1  Jenkins安装与下载应用代码

应用部署

http://jenkins-ci.org

http://wordpress.org/

http://core.svn.wordpress.org/tags/

jenkins为分发服务器

[[email protected] ~]# wget http://pkg.jenkins-ci.org/redhat-stable/jenkins-2.7.2-1.1.noarch.rpm

http://pkg.jenkins-ci.org/redhat-stable/ 查看新的版本

[[email protected] ~]# rpm -ivh jenkins-2.7.2-1.1.noarch.rpm

[[email protected] local]# yum install -y java

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html     下载新版jdk

[[email protected] ~]# /etc/init.d/jenkins restart

[[email protected] ~]# /etc/init.d/jenkins status

jenkins (pid  21371) is running...

[[email protected] ~]# 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

点击 保存

Build with Parameters

WP_VER3.6   (3.6为其中一个版本)

点击开始构建

点击构建历史

有 console output

控制台输出 显示结果

Finished: SUCCESS 表示成功

[[email protected] wordpress-build]# pwd

/var/lib/jenkins/workspace/wordpress-build

[[email protected] 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

需先创建目录

[[email protected] wordpress-build]# mkdir -p /var/www/html/deploy/packages/

[[email protected] wordpress-build]# cd /var/www/html

[[email protected] html]# chown jenkins.jenkins deploy/ -R

[[email protected] html]# ll

total 4

drwxr-xr-x 3 jenkins jenkins 4096 Dec  4 01:24 deploy

重新构建含 execute shell 脚本命令

Build with Parameters

WP_VER3.6

点击开始构建

控制台输出 显示

Finished: SUCCESS  表示成功。

[[email protected] html]# cd deploy/

[[email protected] deploy]# cd packages/

[[email protected] packages]# ls

wordpress-3.6.tar.gz

[[email protected] packages]# ll

total 10712

-rw-r--r-- 1 jenkins jenkins 10968855 Dec  4 01:32 wordpress-3.6.tar.gz

已生成tar的压缩文件,真实服务器real server可下载这个文件。下载完文件跟网上文件进行md5的对比,是否一样。

[[email protected] 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

[[email protected] packages]# ls

wordpress-3.6.tar.gz  wordpress-3.6.tar.gz.md5

[[email protected] packages]# cat wordpress-3.6.tar.gz.md5

cdb5b03bb5cf87f97fb3ceb3b1a27de1

[[email protected] packages]# md5sum wordpress-3.6.tar.gz

cdb5b03bb5cf87f97fb3ceb3b1a27de1  wordpress-3.6.tar.gz

[[email protected] 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

[[email protected] deploy]# ls

lastver  packages

[[email protected] deploy]# cat lastver

3.6

重新创建一个新的项目

wordpress-live

构建一个自由风格的软件项目

选择 :

参数化构建过程   名字 WP_VER

构建: execute shell

command: echo $WP_VER > /var/www/html/deploy/livever

构建3.6版本

Finished: SUCCESS

[[email protected] deploy]# ls

lastver  livever  packages

[[email protected] deploy]# cat livever

3.6

lastver 可供客户端下载的,livever 供客户端应用的

1.3 客户端下载应用代码

客户端检查ivever和lastver

[[email protected] day14]# service httpd start

启动httpd 服务

http://192.168.4.90/deploy/packages/

可查看到相关文档

[[email protected] 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()

执行:

[[email protected] day14]# python deploy.py

[[email protected] day14]# ls /var/www/download/

wordpress-3.6.tar.gz

[[email protected] day14]# cat /var/www/html/deploy/lastver

3.6

[[email protected] day14]# md5sum /var/www/download/wordpress-3.6.tar.gz

1e0403c4590eee50c16b7fbb0fb64175  /var/www/download/wordpress-3.6.tar.gz

[[email protected] day14]# cat /var/www/html/deploy/packages/wordpress-3.6.tar.gz.md5

1e0403c4590eee50c16b7fbb0fb64175

下载tar压缩文件进行校验,显示与原文件一致。

1.4 对下载文件进行校验

修改下载方式每次读取4097,减少对内存的占用

[[email protected] 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()

执行结果

[[email protected] day14]# rm -rf /var/www/download/

[[email protected] day14]# ls

deploy.py

[[email protected] day14]# vim deploy.py

[[email protected] day14]# python deploy.py

Sucess

[[email protected] day14]# ls /var/www/download/

wordpress-3.6.tar.gz

检验大小

[[email protected] day14]# ls /var/www/download/ -l

total 10732

-rw-r--r--. 1 root root 10988498 Nov 13 11:08 wordpress-3.6.tar.gz

[[email protected] day14]# !vim

vim deploy.py

[[email protected] 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值

[[email protected] day14]# md5sum /var/www/download/wordpress-3.6.tar.gz

1e0403c4590eee50c16b7fbb0fb64175  /var/www/download/wordpress-3.6.tar.gz

[[email protected] day14]# cd /var/www/html/deploy/packages

[[email protected] packages]# ls

wordpress-3.6.tar.gz  wordpress-3.6.tar.gz.md5

[[email protected] packages]# cat wordpress-3.6.tar.gz.md5

1e0403c4590eee50c16b7fbb0fb64175

1.5 tarfile模块使用

用于解压

https://docs.python.org/2/library/tarfile.html?highlight=tarfile#module-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()

[[email protected] ~]# cd /tmp

[[email protected] tmp]# ls

1.tar  1.txt  etc  hsperfdata_root  tmp.6xnaKaSM91

[[email protected] tmp]# ll 1.tar

-rw-r--r--. 1 root root 10240 Jan 22 04:39 1.tar

[[email protected] 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‘)

[[email protected] tmp]# ls

1.tar  1.txt  etc  hsperfdata_root  tmp.6xnaKaSM91

[[email protected] tmp]# cd etc/

[[email protected] 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()

[[email protected] ~]# 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服务器端文件目录

[[email protected] day14]# ls /var/www/download/

wordpress-3.6.tar.gz

[[email protected] day14]# ls /var/www/deploy/

wordpress-3.6

[[email protected] day14]# du -sh /var/www/deploy/wordpress-3.6/

43M    /var/www/deploy/wordpress-3.6/

将文件解压分发到 deploy 目录下

1.6 使用符号链接发布应用

[[email protected] day14]# ls /var/www/deploy/

wordpress-3.6

[[email protected] day14]# curl http://192.168.1.125/deploy/livever

3.6

[[email protected] day14]# curl http://192.168.1.125/deploy/lastver

3.6

[[email protected] day14]# cd /var/www/html/

[[email protected] 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/[email protected]

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‘)         取消链接文件

代码:

[[email protected] 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()

[[email protected] day14]# python deploy2.py

[[email protected] day14]# cd /var/www/html

[[email protected] html]# ls

current  deploy  zabbix

[[email protected] 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

[[email protected] day14]# cd /etc/httpd/conf.d

[[email protected] conf.d]# ls

README  welcome.conf

[[email protected] conf.d]# vim wp.conf

Alias /wp "/var/www/html/current"

[[email protected] conf.d]# service httpd restart

打开  http://192.168.1.125/wp/  显示有文件

jenkins 上

wordpress-live 上

Build with Parameters 构建3.7 的版本

WP_VER 3.7   开始构建

[[email protected] day14]# curl http://192.168.1.125/deploy/livever

3.7

livever 变成 了 3.7

[[email protected] 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

[[email protected] day14]# python deploy2.py

[[email protected] 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的版本不能被删除

[[email protected] ~]# mkdir /var/www/deploy/wordpess-3.8

[[email protected] ~]# ls /var/www/deploy/

wordpess-3.8  wordpress-3.5.2  wordpress-3.6  wordpress-3.7

[[email protected] ~]# touch /var/www/download/wordpress-3.8.tar.gz

[[email protected] ~]#

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# curl http://192.168.1.125/deploy/livever

3.7

[[email protected] ~]# 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()

[[email protected] day14]# python deploy2.py

[[email protected] ~]# ls /var/www/download/

wordpress-3.5.2.tar.gz  wordpress-3.7.tar.gz  wordpress-3.8.tar.gz

[[email protected] ~]# 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‘)

执行结果:

[[email protected] day14]# python deploy2.py

另外一台终端操作

[[email protected] day14]# python deploy2.py

deploy2.py is running....

增加crontab

[[email protected] day14]# crontab -e

* * * * * python /root/day14/deploy2.py

每分钟执行一次

1.9 使用fabirc状态检查

http://www.fabfile.org/

Fabric 版本

pip install "pycrypto==2.3"

pip install paramiko==1.12.4

pip install fabric==1.8.3

[[email protected] day14]# vim fabfile.py

from fabric.api import run

def host_type():

run(‘uname -s‘)

[[email protected] 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 用户

[[email protected] day14]# useradd test

[[email protected] 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

[[email protected] day14]# grep test /etc/passwd

badtest:x:501:501::/home/badtest:/bin/bash

test:x:502:502::/home/test:/bin/bash

增加用户, 主机等信息

[[email protected] 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‘)

再执行命令 显示相关信息

[[email protected] 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 脚本

[[email protected] day14]# cd /var/www/html/

You have new mail in /var/spool/mail/root

[[email protected] html]# ls

current  deploy  zabbix

[[email protected] html]# mkdir scripts

[[email protected] html]# ls

current  deploy  scripts  zabbix

创建 scripts 文件夹

[[email protected] html]# cp /root/day14/fabfile.py .

[[email protected] html]# ls

current  deploy  fabfile.py  scripts  zabbix

[[email protected] 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"

""")

执行测试下

[[email protected] 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自动执行

[[email protected] 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

[[email protected] html]# ls /var/www/deploy/

wordpess-3.8  wordpress-3.7  wordpress-4.4

下一步检测

wordpress-check  构建项目 控制台输出 success

再执行 wordpress-live

显示 控制台输出 success

[[email protected] html]# ls /var/www/html/ -l

total 20

lrwxrwxrwx   1 root    root      29 Jan 20 15:05 current -> /var/www/deploy/wordpress-4.4

已经进行更改

[[email protected] html]# curl http://192.168.1.125/deploy/livever

4.4

You have new mail in /var/spool/mail/root

[[email protected] html]# curl http://192.168.1.125/deploy/lastver

4.4

时间: 2024-07-29 02:52:05

Jenkins代码管理的相关文章

[转]Jenkins使用 管理节点

现在我们已经搭建好了基本的Jenkins环境,在这一集里,我们说一说如何管理节点. 进入“系统管理”中的“管理节点”. 创建Windos系统的奴隶节点 先创建一台安装了Win7系统的虚拟机,作为Jenkins构建的奴隶节点.比如我这台机器IP为172.16.12.81,用户名为user1,密码为123456.然后在C盘创建路径 C:\ci_jenkins 作为Jenkins操作的根目录.另外还需要在这台机器上安装JDK或JRE,我以前配置时这里如果使用64位的JDK会有问题,所以我也不再实验了,

我与Git的那些破事--代码管理

1. Git是什么? 作为一名程序猿,我相信大家都或多或少接触过git--分布式版本控制软件. 有人说,它是目前世界上最先进的分布式版本控制系统,我想说,是否最先进不知道,但确实好用,实用. 作为一款风靡全球的软件,不得不提提它的历史: --由Linus Torvalds创作,并与2005首次发布,最初仅是为更好的管理Linux核心开发而设计,不曾想太优秀,如今已被广为使用. 2. 我们可用Git来干什么? 作为一款分布式版本控制软件,听上去高端大气上档次,但说白了,就是一款项目代码管理工具.

[BI项目记]-搭建代码管理环境之客户端

 前面已经介绍了如何搭建代码管理环境的服务器端安装和配置,这里介绍对于客户端的几个场景. 首先对于开发人员来说,可以直接使用Visual Studio来连接,这里主要演示Visual Studio 2013. 打开Visual Studio 2013,点击VIEW下的Team Explorer. 在Team Explorer里,点击Select Team Projects- 在弹出的界面中,点击Servers- 点击Add-连接TFS服务器. 填写TFS服务器所在位置. 这个地址可以在TFS

技术团队代码管理和部署

主流公司使用svn和git作为代码版本管理,当然也不排除直接copy或者ftp.公司经历了的svn到git的变迁,也深刻体会到不同的版本管理服务,使得技术团队的协作方式变得更为流畅. 简单介绍下背景,有一个项目V5,从版本V1一直演变到现在V5,可见历史之久,想从svn切换到git,其中的代码管理和上线部署迁移,都会是经历很长一段时间的不稳定,尤其是一些开发同学对新的版本管理和部署理解不透彻,很容易引发事故. 在svn的主干开发流程 开发同学更新主干代码,提交代码 部署测试环境 检查每一个要上线

代码管理工具 --- git的学习笔记四《重新整理git(1)》

1.创建版本库 mkdir  创建目录 cd  地址,到该地址下 pwd 显示当前目录 1.创建目录 $ mkdir startGit $ cd startGit $ pwd 显示当前目录 或者cd到桌面,然后再创建目录 2.初始化版本库 $ git init 初始化仓库 提示信息:Initialized empty Git repository in /Users/xingzai/Desktop/startGit/.git/ 建立一个空的git仓库在/Users/xingzai/Desktop

Coding 代码管理快速入门(转)

当项目创建好了之后,我们该如何上传代码到 coding 上呢? Coding 网站使用“ Git 仓库”(类似 github )来管理代码. 其操作原理在于:利用 git 服务,将本地的项目目录下的文件同步到 coding 的“ Git 仓库” 本文主要包含如下内容: 命令行上传代码 图形界面上传代码 常见问题 注:以下演示均在 windows8 系统上进行 若在创建项目时,并未选择“启用 readme.md 文件初始化项目”,“开源许可证”和“ gitignore 文件 ”这三个选项(当然我们

产品研发管理(二):使用SubVersion进行代码管理

概述 这是产品研发管理系列文章的第二篇:使用SubVersion进行代码管理. 介绍怎样使用SubVersion的资料已经很多,这里不准备介绍怎样使用SubVersion.这篇文章主要介绍怎样进行代码版本管理. 使用SubVersion进行代码管理 时间点(1) (1)的起始时间是3.0开发的开始. 在(1)期间,没有任何用户使用3.0(因为它还没有发布),所以所有开发人员直接在3.0Trunk上开发. (1)的结束时间是3.0开发的结束时间.结束时发布3.0产品,在SVN上创建3.0 Tag,

在Windows Server 2008上部署SVN代码管理总结

这段时间在公司开发Flex程序,所以使用TortoiseSVN作为团队代码管理器,今天在公司服务器上部署SVN服务器,并实验成功,总结如下: 服务器环境: 操作系统:Windows Server 2008: SVN服务器端程序:VisualSVN Server 2.1.5:(http://www.visualsvn.com/ ) 客户端环境: 操作系统:Windows 7 32Bit: SVN客户端程序:TortoiseSVN-1.6.12.20536-win32-svn-1.6.15:(htt

Java版代码管理器

最新学习javaSE,自己照着一个软件写了一下,代码管理器已经完全实现,基本上可以与原软件如假包换了,使用SQLite数据库,mybatis数据库操作框架,基本上初级阶段很多知识点都涵盖到了,带语法显示,文件读写,树的动态操作,等等...