#!/usr/bin/env python
import os
import time
import fabric
from fabric.api import *
from fabric.context_managers import *
from fabric.colors import *
from fabric.contrib.console import confirm
env.hosts=[‘XXXX.XXXX.XXXX.XXXX‘]
env.user=‘user‘
env.password=‘xxxxxxxx‘
env.port=‘22‘
env.remote_deploy_dir="/app/dubbo/server"
env.remote_backup_dir="/data/backup/java"
env.remote_upload_dir="/home/www/upload"
env.local_dir="/app/webapps/torch/dubbo"
env.local_upload_dir="/home/www/upload"
env.deploy_date=time.strftime("%Y%m%d%H%M")
env.por_name=prompt("please input your project name:")
@task
def backup_remote():
env.por_name = prompt("please input your project name:")
print red("start backup remote project" + "env.pro_name")
rem = run("cd %s/%s" % (env.remote_deploy_dir,env.por_name))
if rem.failed and not confirm("cd deployfailed,continue?"):
abort(‘Abort‘)
#with settings(warn_only = True):
with cd("%s/%s" % (env.remote_deploy_dir,env.por_name)):
run("tar -czvf %s%s.tar.gz lib" % (env.por_name,env.deploy_date))
time.sleep(5)
run("mv *.tar.gz %s" % (env.remote_backup_dir))
print green("backup remote project success!")
@task
def tar_source():
print red("start tar source package......")
with lcd("%s/%s" % (env.local_dir,env.por_name)):
local("tar -xvf %s-assembly.tar.gz" % (env.por_name))
time.sleep(5)
with lcd("%s/%s/%s" % (env.local_dir,env.por_name,env.por_name)):
local("tar -czvf %s.tar.gz lib" % (env.por_name))
time.sleep(5)
local("mv %s.tar.gz %s" % (env.por_name,env.local_upload_dir))
print green("tar source packge success!")
@task
def put_packge():
print red ("start put packge......")
with settings(warn_only = True):
result = put(env.local_upload_dir +"/"+ env.por_name +".tar.gz",env.remote_upload_dir)
if result.failed and not confirm("put file failed,confitnue [Y/N]?"):
abort("Abort file put task!")
local("rm -rf %s/%s.tar.gz" %(env.local_upload_dir,env.por_name))
@task
def check_packge():
print red ("start check packge......")
with settings(warn_only = True):
lmd5 = local("md5sum %s/%s.tar.gz" % (env.local_upload_dir,env.por_name), capture=True).split(‘ ‘)[0]
print ("lmd5 is %s" %(lmd5))
rmd5 = run("md5sum %s/%s.tar.gz" % (env.remote_upload_dir, env.por_name)).split(‘ ‘)[0]
print ("rmd5 is %s" %(rmd5))
if lmd5 == rmd5:
local("rm -rf %s/%s.tar.gz " %(env.local_upload_dir,env.por_name))
print "OK"
else:
print "ERROR"
run("rm -rf %s/%s.tar.gz" %(env.remote_upload_dir, env.por_name))
print yellow("put packge failed......")
@task
def deploy_packge():
print red("start deploy packge ......")
#with settings(warn_only = True):
#run("mv %s/%s.tar.gz %s/%s" %(env.remote_upload_dir,env.por_name,env.remote_deploy_dir,env.por_name))
#with cd(‘%s/%s‘ % (env.remote_deploy_dir,env.por_name)):
#run("./bin/stop.sh")
#time.sleep(5)
with settings(warn_only = True):
env.remote_pro_pid = run("ps -ef |grep %s |grep -v grep |awk ‘{print $2}‘|tail -1" % (env.por_name))
#project stop, start project
if len(env.remote_pro_pid)==0:
print red("env.por_name is stop,start deploy packge......")
run("mv %s/%s.tar.gz %s/%s" %(env.remote_upload_dir,env.por_name,env.remote_deploy_dir,env.por_name))
with cd(‘%s/%s‘ % (env.remote_deploy_dir,env.por_name)):
run("rm -rf lib && tar -xvf %s.tar.gz" %(env.por_name))
time.sleep(3)
run("./bin/start.sh")
time.sleep(5)
env.remote_pro_pid = run("ps -ef |grep %s |grep -v grep |awk ‘{print $2}‘|tail -1" % (env.por_name))
if len(env.remote_pro_pid)!=0:
print green("%s packge deploy is sucessfull..." %(env.por_name))
run("rm -rf %s/%s/%s.tar.gz" %(env.remote_deploy_dir,env.por_name,env.por_name))
else:
print yellow("%s packge deploy faild" %(env.por_name))
run("rm -rf %s/%s/%s.tar.gz" %(env.remote_deploy_dir,env.por_name,env.por_name))
#project start,stop project and start project
else:
with settings(warn_only = True):
with cd(‘%s/%s‘ % (env.remote_deploy_dir,env.por_name)):
run("./bin/stop.sh")
time.sleep(5)
env.remote_pro_pid = run("ps -ef |grep %s |grep -v grep |awk ‘{print $2}‘|tail -1" % (env.por_name))
if len(env.remote_pro_pid)==0:
print("env.por_name is stop,start deploy packge......")
run("mv %s/%s.tar.gz %s/%s" %(env.remote_upload_dir,env.por_name,env.remote_deploy_dir,env.por_name))
with cd(‘%s/%s‘ % (env.remote_deploy_dir,env.por_name)):
run("rm -rf lib && tar -xvf %s.tar.gz" %(env.por_name))
time.sleep(3)
run("./bin/start.sh")
time.sleep(5)
env.remote_pro_pid = run("ps -ef |grep %s |grep -v grep |awk ‘{print $2}‘|tail -1" % (env.por_name))
if len(env.remote_pro_pid)!=0:
print green("%s packge deploy is sucessfull..." %(env.por_name))
run("rm -rf %s/%s/%s.tar.gz" %(env.remote_deploy_dir,env.por_name,env.por_name))
else:
print yellow("%s packge deploy faild" %(env.por_name))
run("rm -rf %s/%s/%s.tar.gz" %(env.remote_deploy_dir,env.por_name,env.por_name))
else:
run("kill -9 %d" % int(env.remote_pro_pid))
print("env.por_name is stop,start deploy packge......")
run("mv %s/%s.tar.gz %s/%s" % (env.remote_upload_dir, env.por_name, env.remote_deploy_dir, env.por_name))
time.sleep(5)
with cd(‘%s/%s‘ % (env.remote_deploy_dir, env.por_name)):
run("rm -rf lib && tar -xvf %s.tar.gz" % (env.por_name))
run("./bin/start.sh")
time.sleep(5)
env.remote_pro_pid = run("ps -ef |grep %s |grep -v grep |awk ‘{print $2}‘|tail -1" % (env.por_name))
if len(env.remote_pro_pid) != 0:
print green("%s packge deploy sucessfull...."%(env.por_name))
run("rm -rf %s/%s/%s.tar.gz" %(env.remote_deploy_dir,env.por_name,env.por_name))
else:
print yellow("%s packge deploy faild" % (env.por_name))
run("rm -rf %s/%s/%s.tar.gz" %(env.remote_deploy_dir,env.por_name,env.por_name))
@task
def go():
backup_remote()
tar_source()
put_packge()
check_packge()
deploy_packge()