您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)生產(chǎn)環(huán)境下的fabric代碼是怎么樣的,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
PHP代碼更新:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
from fabric.colors import *
import time,sys,os
#==============登錄遠(yuǎn)程主機(jī)變量設(shè)置====================
env.hosts=['opuser@10.0.0.111:22','opuser@10.0.0.112:22','opuser@10.0.0.115:22']
#env.password='1234'
#===============更新程序變量設(shè)置=======================
env.dev_dir='/app/webapps/'
env.pro_backup_date=time.strftime("%Y%m%d%H%M")
env.deploy_dir='/opt/app/webapps/'
env.pre_deploy_dir='/opt/app/prewebapps/'
env.deploy_tmp_dir='/home/opuser/upload/'
env.deploy_backup_dir='/data01/backup/php'
env.pro_name=prompt("please input programe name:",default="opuser.test.com")
env.pro_versionID=prompt("please input versionID:",default="0.0.0.0")
env.sourceProdRoot='/opt/app/webapps2/'
@task
def backup_task():
print red("Start to backup programe...")
env.pro_backup_name=prompt("Please input backup project name:",default="opuser.test.com")
with settings(warn_only=True):
run("cd %s && tar -czf %s.%s.backup.tar.gz --exclude %s/logs %s" % (env.deploy_dir,env.pro_backup_name,env.pro_backup_date,env.pro_backup_name,env.pro_backup_name))
run("cd %s && mv %s.%s.backup.tar.gz %s" % (env.deploy_dir,env.pro_backup_name,env.pro_backup_date,env.deploy_backup_dir))
print green("Backuped project packages success! ")
#@runs_once
@task
def tar_task():
print yellow("Start to tar source package...")
local("cd %s%s && rm -rf %s/index.php && tar -czf %s.tar.gz --exclude=logs --exclude .svn %s" % ( env.dev_dir,env.pro_name,env.pro_versionID,env.pro_name,env.pro_versionID))
print green("Creating source packages success!")
@task
def put_update_task():
run("mkdir -p /home/opuser/upload/%s" % (env.pro_name))
env.deploy_full_path=env.deploy_tmp_dir + env.pro_name
with settings(warn_only=True):
result = put(env.dev_dir + env.pro_name +"/"+ env.pro_name +".tar.gz",env.deploy_full_path)
if result.failed and not confirm("put file failed,Continue[Y/N]?"):
abort("Aborting file put task!")
with settings(warn_only=True):
lmd5=local("md5sum /app/webapps/%s/%s.tar.gz" % (env.pro_name,env.pro_name),capture=True).split(' ')[0]
rmd5=run("md5sum /home/opuser/upload/%s/%s.tar.gz" % (env.pro_name,env.pro_name)).split(' ')[0]
if lmd5==rmd5:
print "OK"
else:
print "ERROR"
with cd(env.deploy_full_path):
run("tar xf %s.tar.gz && cd %s && \cp -rf * %s%s/" % (env.pro_name,env.pro_versionID,env.deploy_dir,env.pro_name))
run("tar xf %s.tar.gz && cd %s && \cp -rf * %s%s/" % (env.pro_name,env.pro_versionID,env.pre_deploy_dir,env.pro_name))
run("mv %s %s" % (env.pro_versionID,env.pro_backup_date))
run("rm -rf %s*" % (env.pro_name))
print red("delete source tar file....")
#time.sleep(2)
with cd (env.dev_dir):
local("cd /app/webapps/%s && rm -rf %s.tar.gz" % (env.pro_name,env.pro_name))
@runs_once
@task
def rollback():
confirm("you are sure to rollback this project? Continue")
print yellow("Start to rollback project...")
with cd("/data/backup/php"):
run("ls -l")
env.rollback_pro_name=prompt("rollback project name:")
run("cd %s && tar -xf %s -C %s" % (env.deploy_backup_dir,env.rollback_pro_name,env.deploy_dir))
print green("rollback project success!")
@task
def checkcode():
#env.sourceLatestVersion=run("ls -lt /home/opuser/upload/%s/|awk '{print $9}'|grep -v ^$|head -n 1" % (env.pro_name))
#env.staticLatestVersion=run("ls -lt /home/opuser/upload/res.spp.com|awk '{print $9}'|grep -v ^$|head -n 1")
with cd("/server/scripts"):
# run("./deploy -alsologtostderr=true -bakLimit=7 -sourceProdRoot=%s%s -sourceDevRoot=%s%s -staticDevRoot=%s/ -staticProdRoot=%s -staticRelativeDirs=%s" % (env.sourceProdRoot,env.pro_name,env.sourceDevRoot,env.pro_name,env.staticDevRoot,env.staticProdRoot,env.staticRelativeDirs))
run("./deploy -alsologtostderr=true")
@task
def go():
#backup_task()
tar_task()
put_update_task()
#checkcode()
JAVA代碼更新:
#!/usr/bin/env python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
import time,os
env.hosts=['10.0.0.13','10.0.0.11','10.0.0.12']
env.user="opuser"
#env.password='Passw0d@!W'
env.port="52113"
env.app_local_dir="/app/webapps/app/java/"
env.backup_date=time.strftime("%Y%m%d%H%M")
env.pro_backup_dir="/data01/backup/java"
env.pro_name=prompt("please input app name:")
env.remote_app_dir="/opt/app/server/"
env.upload_temp_dir="/home/opuser/upload/"
@task
def backup_task():
run("cd %s%s/webapps && tar -czf %s.%s.tar.gz *.war && mv %s.%s.tar.gz %s" % (env.remote_app_dir,env.pro_name,env.pro_name,env.backup_date,\
env.pro_name,env.backup_date,env.pro_backup_dir))
print "backup app success!!!"
time.sleep(3)
print red("Ready to upload Java update package.....")
time.sleep(3)
@task
def upload_task():
run("mkdir -p /home/opuser/upload/%s" % (env.pro_name))
env.deploy_full_path=env.upload_temp_dir + env.pro_name
with settings(warn_only=True):
result = put(env.app_local_dir + env.pro_name +"/"+ "*.war",env.deploy_full_path)
lmd5=local("md5sum /app/webapps/app/java/%s/*.war" % (env.pro_name),capture=True).split(' ')[0]
rmd5=run("md5sum /home/opuser/upload/%s/*.war" %(env.pro_name)).split(' ')[0]
if lmd5==rmd5:
print "OK"
else:
print "ERROR"
#time.sleep(5)
print green("Start off Tomcat service and update service packs.....")
time.sleep(5)
@task
def shutdown_and_update():
#Close Tomcat
print red("start shutdown the %s...." % (env.pro_name))
time.sleep(3)
env.remote_app_pid=run("ps xua|grep %s|grep -v grep|awk '{print $2}'|tail -1" %(env.pro_name))
if len(env.remote_app_pid)==0:
#Delete the old version
print red("start delete war packge.....")
time.sleep(3)
run("cd %s%s/webapps && rm -rf *" % (env.remote_app_dir,env.pro_name))
#Update the new version
print green("Start update app ....")
run("cd /home/opuser/upload/%s && cp *.war %s%s/webapps" % (env.pro_name,env.remote_app_dir,env.pro_name))
time.sleep(3)
run("cd /home/opuser/upload/%s && mv *.war %s-%s-backup " % (env.pro_name,env.pro_name,env.backup_date))
time.sleep(5)
print green("Update successfull.....please run to the app")
else:
run("cd %s%s && ./bin/shutdown.sh" % (env.remote_app_dir,env.pro_name))
env.remote_app_pid=run("ps xua|grep %s|grep -v grep|awk '{print $2}'|tail -1" %(env.pro_name))
if len(env.remote_app_pid)==0:
print "%s is shutdown % (env.pro_name)"
run("cd %s%s/webapps && rm -rf *" % (env.remote_app_dir,env.pro_name))
#Update the new version
print green("Start update app ....")
run("cd /home/opuser/upload/%s && cp *.war %s%s/webapps" % (env.pro_name,env.remote_app_dir,env.pro_name))
time.sleep(3)
run("cd /home/opuser/upload/%s && mv *.war %s-%s-backup " % (env.pro_name,env.pro_name,env.backup_date))
time.sleep(5)
print green("Update successfull.....please run to the app")
else:
env.remote_app_pid=run("ps xua|grep %s|grep -v grep|awk '{print $2}'|tail -1" %(env.pro_name))
print "%s pid will be killed next .... "% (env.pro_name)
run("/bin/kill -9 %s" % (env.remote_app_pid))
time.sleep(5)
print green("the app %s has been shutdown" % (env.pro_name))
run("cd %s%s/webapps && rm -rf *" % (env.remote_app_dir,env.pro_name))
#Update the new version
print green("Start update app ....")
run("cd /home/opuser/upload/%s && cp *.war %s%s/webapps" % (env.pro_name,env.remote_app_dir,env.pro_name))
time.sleep(3)
run("cd /home/opuser/upload/%s && mv *.war %s-%s-backup " % (env.pro_name,env.pro_name,env.backup_date))
time.sleep(5)
print green("Update successfull.....please run to the app")
print yellow("###################################################")
print green(" \n")
@task
def start_task():
print green("starting app ....")
time.sleep(3)
run("set -m;%s%s/bin/startup.sh" % (env.remote_app_dir,env.pro_name))
time.sleep(5)
env.remote_app_pid=run("ps xua|grep %s|grep -v grep|awk '{print $2}'|tail -1" %(env.pro_name))
#print env.remote_app_pid
if len(env.remote_app_pid)!=0:
print blue("%s is startup successfull the pid is %s....."% (env.pro_name,env.remote_app_pid))
@task
def go():
backup_task()
upload_task()
shutdown_and_update()
start_task()
注:生產(chǎn)中推薦使用ssh-key秘鑰認(rèn)證,不推薦密碼
以上就是生產(chǎn)環(huán)境下的fabric代碼是怎么樣的,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。