此脚本实测通过,在生产环境中应用。


#!/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']

env.user='username'

env.password='passwd'

env.port='xx'


env.remote_deploy_dir="/app/dubbo/server"

env.remote_backup_dir="/data/backup/dubbo"

env.remote_upload_dir="/home/www/upload"

env.local_dir="/opt/webapps/dubbo"

env.local_upload_dir="/home/www/upload"

env.deploy_date=time.strftime("%Y%m%d%H%M")

env.local_date=time.strftime("%Y%m%d")

env.app_name=[]

#env.por_name=prompt("please input your project name:")



@task


def delete_old_pro():

    with lcd("%s" %(env.local_dir)):

        local("rm -rf ocorper-account-provider-1.0-SNAPSHOT/* && rm -rf ocorper-application-provider-1.0-SNAPSHOT/* && rm -rf ocorper-common-core-provider-1.0-SNAPSHOT/*")

        local("rm -rf ocorper-organization-provider-1.0-SNAPSHOT/* && rm -rf ocorper-support-work-wechat-provider-1.0-SNAPSHOT/* && rm -rf ocorper-workflow-provider-1.0-SNAPSHOT/*")

        local("rm -rf ocorper-server/* && rm -rf ocorper-manager-server/*")


@task

def local_ready():

    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep account", capture=True)

            if len(local_pro) != 0:

                env.app_name.append("ocorper-account-provider-1.0-SNAPSHOT")

                print red("start copy packge ......")

                local("cp %s ../ocorper-account-provider-1.0-SNAPSHOT" % (local_pro))


            else:

                print yellow("packge is exist......")


    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep application", capture=True)

            if len(local_pro) != 0:

                env.app_name.append("ocorper-application-provider-1.0-SNAPSHOT")

                print red("start copy packge ......")

                local("cp %s ../ocorper-application-provider-1.0-SNAPSHOT" % (local_pro))

            else:

                print yellow("packge is exist......")


    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep common", capture=True)

            if len(local_pro) != 0:

                env.app_name.append("ocorper-common-core-provider-1.0-SNAPSHOT")

                print red("start copy packge ......")

                local("cp %s ../ocorper-common-core-provider-1.0-SNAPSHOT" % (local_pro))

            else:

                print yellow("packge is exist......")


    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep organization", capture=True)

            if len(local_pro) != 0:

                env.app_name.append("ocorper-organization-provider-1.0-SNAPSHOT")

                print red("start copy packge ......")

                local("cp %s ../ocorper-organization-provider-1.0-SNAPSHOT" % (local_pro))

            else:

                print yellow("packge is exist......")


    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep support-work", capture=True)

            if len(local_pro) != 0:

                env.app_name.append("ocorper-support-work-wechat-provider-1.0-SNAPSHOT")

                print red("start copy packge ......")

                local("cp %s ../ocorper-support-work-wechat-provider-1.0-SNAPSHOT" % (local_pro))

            else:

                print yellow("packge is exist......")


    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep workflow", capture=True)

            if len(local_pro) != 0:

                env.app_name.append("ocorper-workflow-provider-1.0-SNAPSHOT")

                print red("start copy packge ......")

                local("cp %s ../ocorper-workflow-provider-1.0-SNAPSHOT" % (local_pro))

            else:

                print yellow("packge is exist......")

    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep ocorper-webapps", capture=True)

            if len(local_pro) != 0:

                print red("start copy packge ......")

                local("cp %s ../ocorper-server/ROOT.war" % (local_pro))

            else:

                print yellow("packge is exist......")


    with settings(warn_only=True):

        with lcd("%s/%s" % (env.local_dir, env.local_date)):

            local_pro = local("ls |grep ocorper-manager", capture=True)

            if len(local_pro) != 0:

                print red("start copy packge ......")

                local("cp %s ../ocorper-manager-server/ROOT.war" % (local_pro))

            else:

                print yellow("packge is exist......")




@task

def backup_remote(project_name):

    env.por_name = 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():

    delete_old_pro()

    local_ready()

    for var in env.app_name:

        backup_remote(var)

        tar_source()

        put_packge()

        check_packge()

        deploy_packge()