python fabric implements remote deployment
python fabric实现远程部署
需求描述
在多人协同开发项目的过程中,几乎每天我们都要提交代码到git服务器,然后部署到测试服务器,每天都在敲那重复的几行命令,实在是无趣。怎么办?运维自动化!接下来就说说fabric这玩意儿,替我们完成一些重复繁杂的工作,相信你会跟我一样喜欢上它的!
本文项目背景
我们这次做的项目是用的django框架,每天我们提交代码到git服务器后,都要手动上传代码到测试服务器,然后执行一系列django框架的命令。每天都要浪费10多分钟的时间,做着重复的劳动,这些工作实在不是一个程序员该干的。。。
解决方案
借助Python的fabric模块可以将自动化部署或者多机操作的命令固化到一个脚本里,然后通过此脚本去执行。
安装fabric
注意:本机和目标服务器都要安装一下
sudo easy_install fabric
或者用pip安装:
pip install fabric
编写脚本
local 是在本机执行;run 是在远程机执行
from fabric.api import hosts, run, env, local, cd, get, lcd from fabric.tasks import execute env.hosts = ["fab@192.168.1.101:22", "root@192.168.1.101:22"] env.passwords = {"fab@192.168.1.101:22": "fab", "root@192.168.1.101:22": "tofabor"} @hosts("ktv@192.168.1.101:22") def update(): """更新测试服务器代码""" with cd("/opt/project/project"): # 进入测试服务器的项目目录 run("git pull origin master") # 从git服务器的master分支下拉最新代码 run("/usr/local/bin/python2.7 /opt/project/project/manage.py makemigrations") # 这是django框架检测数据库变动的命令 run("/usr/local/bin/python2.7 /opt/project/project/manage.py migrate") # 这是django框架执行数据库变更的命令 @hosts("ktv@192.168.1.101:22") def restart(): """重启服务""" execute('stop') execute('start') @hosts("root@192.168.1.101:22") def start(): """开始服务""" with cd("/opt/project/project"): run("supervisorctl start dev") @hosts("ktv@192.168.1.101:22") def stop(): """停止服务""" pids = run("ps -ef |grep '9001'| awk '{print $2}'") pid_list = pids.split('\r\n') for i in pid_list[:-2]: run('kill -9 %s' % i) # 杀掉运行服务进程
如上脚本保存为fabfile.py (也可保存为其他名称,只是运行命令不一样,下面会详述)
执行脚本
如果你的脚本名称为fabfile.py,那么可以在终端进入你fabfile.py的目录,敲入如下命令回车:
fab update
紧接着,你会看到终端提示你输入git账号及密码,待你输入成功后,将自动下拉git服务器的代码到测试服务器。
之后运行如下命令,重启服务:
fab restart
如果你的文件名为其他名称,比如ab.py, 那么执行 fab update /restart是错误的,怎么云运行呢?
fab -f ab update
fab -f ab restart
注:fabric相当强大,此文只是列举一小功能。如需深入学习,请参见官方文档http://docs.fabfile.org/en/1.6/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。
更多 python fabric实现远程部署相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H
