Fabric业务部署示例

#  coding:utf-8

from fabric.api import *
import getpass


env.user = "用户名"
env.hosts = ["服务器IP或域名1", "服务器IP或域名2"]
env.port = "22"


# 只需输入一次密码
@runs_once
def get_passwd():
    return getpass.getpass('请输入服务器登录密码: ')


# 打包
@task
def pack():
    local("rm -f ../project.tar.gz")
    local("tar czvf ../project.tar.gz  --exclude=*.pyc ../project")


# 部署
@task
def deploy():
    env.password = get_passwd()
    with settings(warn_only=True):
        # 前面是源,后面是目标
        put("/home/lee/Code/project/project.tar.gz", "/home/lee/project.tar.gz")
        run("sudo /usr/local/bin/supervisorctl stop all")
        run("/bin/rm -rf /home/lee/project")
        with cd("/home/lee"):
            run("/bin/tar zxvf project.tar.gz")
        run("/bin/rm -f /home/lee/project.tar.gz")
        run("sudo /usr/local/bin/supervisorctl start all")
        run("sudo /usr/local/bin/supervisorctl status")

# 先打包后部署
@task
def go():
    env.password = get_passwd()
    pack()
    deploy()
  • 只打包
fab pack
  • 只部署
fab deploy
  • 先打包后部署
fab go

你可能感兴趣的:(Fabric业务部署示例)