fabric构建自动部署服务

1. fabric简介

fabric 是python 的一个第三方库,同时它也是一个命令行工具。它提供了丰富的同 SSH 交互的接口,可以用来在本地或远程机器上自动化、流水化地执行 shell 命令。使用 fabric 提供的命令行工具,可以很方便地执行应用部署和系统管理等操作。因此它非常适合用来做应用的远程部署及系统维护。

2. 安装及使用

安装命令:pip install fabric

使用参考官网: fabric英文官网fabric中文官网

3. 构建脚本

python 复制代码
import datetime
from fabric import task

@task
def startBuild(c, env="dev", force="0"):
    """
    构建项目自动部署
    :param c: 上下文content
    :param env: 环境变量,开发环境or生产环境
    :param force: 强制构建,"1" 强制构建
    """
    current_day = datetime.datetime.now().strftime("%Y%m%d")

    if env == "dev":
        code_branch = "develop"
        to_virtual_env_cmd = "workon user_predict"
    else:
        code_branch = "master"
        to_virtual_env_cmd = "source /export/venvs/user_predict/bin/activate"

    logger.info(">>>>>>>>>> 开始备份代码 <<<<<<<<<<")
    with c.cd("/export/services/"):
        c.run(f"zip -q -r /tmp/predict_bak/predict_code/user_predict_{current_day}.zip user_predict")
        c.run(f"cp -ra user_predict user_predict_old_{current_day}")

    logger.info(">>>>>>>>>> 开始准备从远程拉取代码 <<<<<<<<<<")
    with c.cd("/export/services/user_predict/"):
        c.run("git remote -v update")
        c.run(f"git checkout {code_branch}")
        local_commit_id = c.run("git rev-parse @{0}").stdout.strip()
        remote_commit_id = c.run("git rev-parse @{u}").stdout.strip()
        if local_commit_id == remote_commit_id and force == "0":
            logger.warning(f">>>>>>>>>> 远程{code_branch}分支最近未提交代码,本次不用构建 <<<<<<<<<<")
            return
        c.run("git pull")

    with c.prefix("source ~/.bashrc"), c.prefix(to_virtual_env_cmd):
        try:
            logging.info(">>>>>>>>>> 前端开始打包并收集静态文件 <<<<<<<<<<")
            c.run(f"cd /export/service/user_predict/frontend/; npm run build")
            c.run(f"cd /export/service/user_predict/; python manage.py collectstatic --noinput")
        except Exception as e:
            logger.error(f">>>>>>>>>> 收集静态文件失败,详细报错为{e} <<<<<<<<<<")
            return

        try:
            logging.info(">>>>>>>>>> 重启uwsgi拉起的pid进程文件 <<<<<<<<<<")
            c.run("cd /export/services/user_predict/; export DJANGO_SETTINGS_MODULE=predict.settings; uwsgi --reload /export/services/user_predict/predict.pid")
            logging.info(">>>>>>>>>> 重启被supervisor监护的进程 <<<<<<<<<<")
            c.run("supervisorctl status | grep 'user_predict_' | awk '{print $1}' | xargs supervisorctl restart")
        except Exception as e:
            logger.error(f">>>>>>>>>> uwsgi或supervisor重启失败,详细报错为{e} <<<<<<<<<<")
            return

    logger.info(">>>>>>>>>> 项目自动部署成功 <<<<<<<<<<")
相关推荐
消失的旧时光-194315 分钟前
Linux 入门核心命令清单(工程版)
linux·运维·服务器
艾莉丝努力练剑23 分钟前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
小天源26 分钟前
Cacti在Debian/Ubuntu中安装及其使用
运维·ubuntu·debian·cacti
Trouvaille ~1 小时前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
芷栀夏1 小时前
深度解析 CANN 异构计算架构:基于 ACL API 的算子调用实战
运维·人工智能·开源·cann
全栈工程师修炼指南1 小时前
Nginx | stream 四层反向代理:SSL、PREREAD 阶段模块指令浅析与实践
运维·网络·网络协议·nginx·ssl
威迪斯特2 小时前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
一方热衷.2 小时前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
玄同7652 小时前
Python 后端三剑客:FastAPI/Flask/Django 对比与 LLM 开发选型指南
人工智能·python·机器学习·自然语言处理·django·flask·fastapi
独自归家的兔2 小时前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu