本篇文章给大家分享的是有关生产环境下的fabric代码是怎么样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
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
#==============登录远程主机变量设置====================
env.hosts=['opuser@10.0.0.111:22','opuser@10.0.0.112:22','opuser@10.0.0.115:22']
#env.password='1234'
#===============更新程序变量设置=======================
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()
注:生产中推荐使用ssh-key秘钥认证,不推荐密码
以上就是生产环境下的fabric代码是怎么样的,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。