bestu 发表于 2018-9-4 07:40:17

Jenkins 持续化部署实例

#!/usr/bin/env python  
#coding=utf-8
  

  
import urllib,urllib2
  
import os,sys
  
import hashlib,tarfile
  
import shutil
  

  
#定义分发服务器上用的到变量,最好配个dns服务器,供内部使用
  
URL_LASTVER = "http://192.168.1.107/wordpress/lastver"
  
URL_ONLINEVER = "http://192.168.1.107/wordpress/onlinever"
  
URL_PKG = "
  

  
#定义本地路径变量
  
LOCAL_DOWNLOAD = "/data/web/wordpress/local/download/" #客户端系下载的tar包
  
LOCAL_DEPLOY = "/data/web/wordpress/local/deploy/" #客户端解压使用版本目录
  
DOC_WWW = "/data/web/wordpress/local/www" #软链 至 worpress 版本
  
TOBE_KEEP = 2 #要保留的版本数量
  
WHITE_LST = []#禁止删除版本的白名单
  

  
#定义用到的文件名字
  
APP_NAME = "wordpress"
  
LASTVER = urllib2.urlopen(URL_LASTVER).read().strip() #读取最新版本号
  
URL_LAST_PKG = URL_PKG + "%s-%s.tar.gz" % (APP_NAME,LASTVER) #服务端tar包
  
LOCAL_LAST_PKG = os.path.join(LOCAL_DOWNLOAD + "%s-%s.tar.gz" % (APP_NAME,LASTVER)) #客户端tar包
  
ONLINEVER = urllib2.urlopen(URL_ONLINEVER).read().strip() #读取在线版本号
  
LOCAL_ONLINE_PKG = os.path.join(LOCAL_DEPLOY + "%s-%s" % (APP_NAME,ONLINEVER)) #在线使用的版本
  

  
#初始化本地目录
  
def init():
  
    if not os.path.exists(LOCAL_DOWNLOAD):
  
      os.makedirs(LOCAL_DOWNLOAD)
  
    if not os.path.exists(LOCAL_DEPLOY):
  
      os.makedirs(LOCAL_DEPLOY)
  

  
#检查最新的版本并下载
  
def checkMd5file(f):#由md5判断下载是否正确
  
    URL_LAST_PKG_MD5 = f + ".md5file"
  
    URL_MD5 = urllib2.urlopen(URL_LAST_PKG_MD5).read().strip()
  
    with open(LOCAL_LAST_PKG) as fd:
  
      m = hashlib.md5(fd.read()).hexdigest()
  
      if m == URL_MD5:
  
            return True
  
      return False
  
def downLoad(f):
  
#    req = urllib2.urlopen(f)
  
#    data = req.read()
  
#    with open(LOCAL_LAST_PKG,'wb') as fd:
  
#      fd.write(data)# 一次性读完,有点占内存
  
    req = urllib2.urlopen(f)
  
    n = 1
  
    while True:
  
      data = req.read(4096) #以4096为单位下载,节省内存
  
      if not data:
  
            break
  
      if n == 1:
  
            with open(LOCAL_LAST_PKG,'wb') as fd:
  
                fd.write(data)
  
                n += 1
  
      elif n > 1:
  
            with open(LOCAL_LAST_PKG,'a') as fd:
  
                fd.write(data)
  
                n += 1
  
    if checkMd5file(URL_LAST_PKG):
  
      return True
  
    return False
  
def checkLastVersion():
  
    WHITE_LST.append(LASTVER)#加入白名单,禁止删除
  
    if not os.path.exists(LOCAL_LAST_PKG):
  
      downLoad(URL_LAST_PKG)
  
    extract_dir = os.path.join(LOCAL_DEPLOY + "%s-%s" % (APP_NAME,LASTVER))
  
    if not os.path.exists(extract_dir): #解压本地download下的包
  
      tar = tarfile.open(LOCAL_LAST_PKG)
  
      tar.extractall(LOCAL_DEPLOY)
  
      #tarFile(LOCAL_LAST,LOCAL_DEPLOY)
  

  
#解压下载的tar包
  
#def tarFile(fn,d):
  
#    tar = tarfile.open(fn)
  
#    tar.extracall('d')
  

  
#检查在线的版本,替换软链
  
def checkOnlineVersion():
  
    WHITE_LST.append(ONLINEVER)#加入白名单,禁止删除
  
    if os.path.exists(LOCAL_ONLINE_PKG):
  
      if not os.path.exists(DOC_WWW): #此路径是指源目录
  
            os.symlink(LOCAL_ONLINE_PKG,DOC_WWW)
  
      else:
  
            target = os.readlink(DOC_WWW)
  
            if target != LOCAL_ONLINE_PKG:
  
                os.unlink(DOC_WWW)
  
                os.symlink(LOCAL_ONLINE_PKG,DOC_WWW)
  

  
#对版本号进行排序,删除多余不用的版本
  
def versionSort(l):
  
    from distutils.version import LooseVersion
  
    vs =
  
    vs.sort()
  
    return
  
def clear():
  
    DOWNLOAD_LST = [:-7] for i in os.listdir(LOCAL_DOWNLOAD)] #取download下的版本号
  
    DEPLOY_LST = for i in os.listdir(LOCAL_DEPLOY)]
  
    tobe_del_download = versionSort(DOWNLOAD_LST)[:-TOBE_KEEP]
  
    tobe_del_deploy = versionSort(DEPLOY_LST)[:-TOBE_KEEP]
  
    for d in tobe_del_download:
  
      fn = os.path.join(LOCAL_DOWNLOAD + "%s-%s.tar.gz" % (APP_NAME,d))
  
      if d not in WHITE_LST: #不删除使用的和最新的版本
  
            os.remove(fn)
  
    for d in tobe_del_deploy:
  
      fn = os.path.join(LOCAL_DEPLOY + "%s-%s" % (APP_NAME,d))
  
      if d not in WHITE_LST: #不删除使用的和最新的版本
  
            #os.remove(fn)   #这只能删除空目录
  
            shutil.rmtree(fn)
  

  
#执行时加个文件锁,避免重复执行
  
def lockFile(f):
  
    if os.path.exists(f):
  
      print "%s is runing......" % __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()
  
    print "checkLastVersion"
  
    checkOnlineVersion()
  
    print "checkOnlineVersion"
  
    clear()
  
    print "clear"
  
    #time.sleep(10)
  
    unlockFile('/tmp/.deploy.lock')


页: [1]
查看完整版本: Jenkins 持续化部署实例