1. 程式人生 > >fabric免輸入密碼部署腳本

fabric免輸入密碼部署腳本

text 輸入密碼 admin hup lena only pat int abr

#!/usr/bin/python
# encoding: utf-8
from fabric.api import *  #fabric.api為低版本庫,需安裝對應的版本
from fabric.context_managers import *
from fabric.contrib.console import confirm
from fabric.contrib.files import *
import time,os
host='[email protected]:22' #22端口不能省略,否則還會提示輸入密碼
env.hosts=[host]
env.roledefs = {
    'all':[host]
}
env.passwords = {
 host:'admin111'
}
#可以不使用密碼,使用秘鑰更安全
#env.key_filename = "~/.ssh/id_rsa"

env.profile='dev'
env.project_basepath='/opt/deploy/gitpro/'
env.project_parent_dir = env.project_basepath
env.postfix='-1.0-SNAPSHOT-exec.jar'
env.deploy_basepath='/opt/deploy/pro/'
env.exec_dir_name = 'exec'


def project_update_master(dir_path):
    with lcd(dir_path):
        local("git fetch origin")
        local("git checkout master")
        local("git pull origin master")


def project_package(dir_path, project_name):
    with lcd(dir_path+project_name):
        local("mvn clean package -Dmaven.test.skip=true -P "+env.profile+","+project_name)


def project_init_dir(deploy_path):
    if not exists(env.deploy_basepath):
        run("mkdir -p "+env.deploy_basepath+"logs")
    if not exists("/data/logs"):
        run("sudo mkdir -p /data/logs")
        run("sudo ln -s "+(env.deploy_basepath+"logs")+" /data/logs")
    if not exists(deploy_path):
        run("mkdir -p "+deploy_path+env.exec_dir_name)
        run("mkdir -p "+deploy_path+"bak")


def project_put(source_dir, target_dir):
    result = put(source_dir, target_dir)
    if result.failed and not confirm("put file failed, Continue[Y/NO]?"):
        abort("aborting!")


def project_port_stop(port):
    with settings(warn_only=True):
        result = run("sudo netstat -lnp|grep "+port+"|awk '{print $7}'|awk -F '/' '{print $1}'")
        if result:
            run("sudo kill "+result)

def project_bak(exec_dir, exec_jar,  bak_dir):
    print(exec_jar)
    print(run("find " + exec_dir + " -name " + exec_jar))
    if run("find " + exec_dir + " -name " + exec_jar):
        run('mv '+exec_dir+'/'+exec_jar+' '+ bak_dir)


def project_clean(exec_dir):
    run('rm -rf '+exec_dir+'/*')


def project_start(exec_dir, jar_name):
    with settings(warn_only=True):
        with cd(exec_dir):
           run("set -m; nohup java -jar -Xmx128M -Xms128M "+jar_name+" >/dev/null 2>&1 &")


'''
打包base服務
'''
def project_deploy(project_name, port):
    deploy_dir = env.deploy_basepath + project_name+'/'
    exec_dir = deploy_dir + env.exec_dir_name
    jar_name =  project_name + env.postfix
    bak_dir = deploy_dir + 'bak'
    project_init_dir(deploy_dir)
    project_update_master(env.project_parent_dir+project_name)
    project_package(env.project_parent_dir, project_name)
    project_port_stop(port)
    project_bak(exec_dir, jar_name, bak_dir)
    project_clean(exec_dir)
    project_put(env.project_parent_dir+project_name+'/target/'+jar_name, exec_dir+'')
    project_start(exec_dir, jar_name)

'''

@task
@roles('all')
def webadmin():
   project_deploy('web-admin', '20000')

fabric免輸入密碼部署腳本