Python下Fabric的简单部署方法
作者:oceandmp 时间:2023-11-08 07:17:11
Fabric是一个用Python开发的部署工具,最大特点是不用登录远程服务器,在本地运行远程命令,几行Python脚本就可以轻松部署。
文档入口
简单安装
sudo easy_install fabric
部署脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
from fabric.api import *
# 登录用户和主机名:
env.user = 'root'
env.hosts = ['www.example.com'] # 如果有多个主机,fabric会自动依次部署
def pack():
' 定义一个pack任务 '
# 打一个tar包:
tar_files = ['*.py', 'static/*', 'templates/*', 'favicon.ico']
local('rm -f example.tar.gz')
local('tar -czvf example.tar.gz --exclude=\'*.tar.gz\' --exclude=\'fabfile.py\' %s' % ' '.join(tar_files))
def deploy():
' 定义一个部署任务 '
# 远程服务器的临时文件:
remote_tmp_tar = '/tmp/example.tar.gz'
tag = datetime.now().strftime('%y.%m.%d_%H.%M.%S')
run('rm -f %s' % remote_tmp_tar)
# 上传tar文件至远程服务器:
put('shici.tar.gz', remote_tmp_tar)
# 解压:
remote_dist_dir = '/srv/www.example.com@%s' % tag
remote_dist_link = '/srv/www.example.com'
run('mkdir %s' % remote_dist_dir)
with cd(remote_dist_dir):
run('tar -xzvf %s' % remote_tmp_tar)
# 设定新目录的www-data权限:
run('chown -R www-data:www-data %s' % remote_dist_dir)
# 删除旧的软链接:
run('rm -f %s' % remote_dist_link)
# 创建新的软链接指向新部署的目录:
run('ln -s %s %s' % (remote_dist_dir, remote_dist_link))
run('chown -R www-data:www-data %s' % remote_dist_link)
# 重启fastcgi:
fcgi = '/etc/init.d/py-fastcgi'
with settings(warn_only=True):
run('%s stop' % fcgi)
run('%s start' % fcgi)
以上定义了pack和deploy两个任务,如果我们用Fabric部署,只需简单地输入两条命令:
$ fab pack
$ fab deploy
Fabric提供几个简单的API来完成所有的部署,最常用的是local()和run(),分别在本地和远程执行命令,put()可以把本地文件上传到远程,当需要在远程指定当前目录时,只需用with cd(‘/path/to/dir/'):即可。
默认情况下,当命令执行失败时,Fabric会停止执行后续命令。有时,我们允许忽略失败的命令继续执行,比如run(‘rm /tmp/abc')在文件不存在的时候有可能失败,这时可以用with settings(warn_only=True):执行命令,这样Fabric只会打出警告信息而不会中断执行。
Fabric是如何在远程执行命令的呢?其实Fabric所有操作都是基于SSH执行的,必要时它会提示输入口令,所以非常安全。更好的办法是在指定的部署服务器上用证书配置无密码的ssh连接。
如果是基于团队开发,可以让Fabric利用版本库自动检出代码,自动执行测试、打包、部署的任务。由于Fabric运行的命令都是基本的Linux命令,所以根本不需要用Fabric本身来扩展,会敲Linux命令就能用Fabric部署。
利用Fabric部署Python、Ruby、PHP这样的非编译型网站应用非常方便,而对于编译型的Java、C#等就麻烦了,编译本身就是一个极其复杂的大工程,需要依赖特定工具或者IDE,很难做到自动化。
标签:Python,Fabric
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python爬虫使用正则爬取网站的实现
2021-09-10 10:39:11
![](https://img.aspxhome.com/file/2023/1/135111_0s.jpg)
TensorFlow变量管理详解
2023-11-23 07:11:18
![](https://img.aspxhome.com/file/2023/4/107204_0s.jpg)
JavaScript实现简单计算器小程序
2024-05-02 16:28:39
![](https://img.aspxhome.com/file/2023/4/130484_0s.jpg)
Python实现读取文件最后n行的方法
2023-08-02 10:33:32
手把手教你在Python里使用ChatGPT
2023-11-10 02:55:58
![](https://img.aspxhome.com/file/2023/9/75339_0s.jpg)
tesseract-ocr使用以及训练方法
2022-07-06 17:23:37
![](https://img.aspxhome.com/file/2023/0/117610_0s.jpg)
最炫Python烟花代码全解析
2022-02-16 13:07:53
![](https://img.aspxhome.com/file/2023/0/85030_0s.jpg)
Python中生成Epoch的方法
2021-06-27 15:21:15
python爬虫之urllib3的使用示例
2023-01-24 07:40:03
python打印直角三角形与等腰三角形实例代码
2023-12-30 04:17:47
数据库复制性能测试 推送模式性能测试
2012-07-11 16:13:52
下载golang.org/x包的操作方法
2023-07-11 16:54:04
Python操作MySQL模拟银行转账
2022-05-19 21:23:11
Python中断言Assertion的一些改进方案
2023-03-05 15:07:57
pycharm 如何查看某一函数源码的快捷键
2023-10-25 02:41:28
![](https://img.aspxhome.com/file/2023/4/69604_0s.png)
python采集天气数据并做数据可视化
2022-07-16 15:26:39
![](https://img.aspxhome.com/file/2023/7/85047_0s.jpg)
Python格式化日期时间操作示例
2022-04-23 23:07:19
影响SQL Server性能的三个关键点
2009-03-09 13:11:00
Python的string模块中的Template类字符串模板用法
2023-02-02 10:53:05
如何利用Redis作为Mybatis的二级缓存
2023-07-05 10:51:11
![](https://img.aspxhome.com/file/2023/3/63153_0s.png)