1. 程式人生 > >Jenkins代碼管理

Jenkins代碼管理

相關信息 pac 執行 oca -c 是否 echo 備份 版本號

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]g01 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

Jenkins代碼管理