Python语言学习-fabric(高效远程自动化部署工具)
小标 2019-01-17 来源 : 阅读 1424 评论 1

摘要:本文主要向大家介绍了Python语言学习-fabric(高效远程自动化部署工具),通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言学习-fabric(高效远程自动化部署工具),通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

最近公司给了台web测试服务器,最近正好学习python语言的flask框架,琢磨着搭个小博客玩玩,但是每次修改代码后,都需要重新打包代码,然后sftp到服务器,再解压,重启服务。作为一个懒人当然不能忍,于是乎发现了这货-fabric。fabric是python语言实现的一个利用ssh高效部署和管理系统的工具。安装省略python、pip管理工具安装过程

pip install fabric

验证是否安装成功进入python编辑模式,输入代码,无错即表示成功安装
from fabric.api import local
fabric版hello worldfabric 默认文件fabfile.py,当然如果不想用这个名字,需要加-f参数创建fabfile.py文件
vim fabrile.py
编辑代码
#coding:utf-8from fabric.api import local#def hello():# local函数用来执行本地命令local('echo "hello wolrd!"')
执行代码
fab hello
可以通过fab -l查看可以执行的任务,当前为hello函数运行结果[localhost] local: echo "hello world!"hello world!Done.fabric常用api函数解释例子local执行本地命令local('ls -l')lcd切换本地目录(需要with包裹)lcd('本地目录')run执行远程命令run('ls -l')cd切换远程目录(需要with包裹)cd('远程目录')put将本地文件上传至远程目录put('本地文件','远程目录')envfabric环境,配置远程ip、端口、密码见下文fabric远程执行例子将本地/tmp/local目录下创建hello.txt(内容“hello world!”)文件上传至服务器/tmp/remote上,并显示txt文件内容
coding:utf-8from fabric.api import local,cd,put,lcd,env,run# 目标hosts(可多个),用户@ip:22,使用sshenv.hosts = ['root@remoteip:22']# root密码env.password='pwd'def hello():# 切换到本地/tmp/local目录with lcd('/tmp/local'):# 执行本地命令local('echo "hello world!" > hello.txt')# 上传至服务器put('hello.txt','/tmp/remote')# 切换到服务器/tmp/remote目录with cd('/tmp/remote'):# 执行服务器命令run('cat hello.txt')
运行结果fab hello[root@remoteip:22] Executing task 'hello'[localhost] local: echo "hello world!" > hello.txt[root@remoteip:22] put: /tmp/local/hello.txt -> /tmp/remote/hello.txt[root@remoteip:22] run: cat hello.txt[root@remoteip:22] out: hello world!多个目标服务器相同密码或者手动输入:
env.hosts = ['root@ip1:22',root@ip2:22]

不同密码或者不想手动输入(此方法也可定义不角色一组服务器):

#coding:utf-8from fabric.api import local,cd,put,lcd,env,run,execute,rolesenv.roledefs = {'role1':['root@ip1:22',],'role2':['root@ip2:22',]}env.passwords={'root@ip1:22':'pwd1','root@ip2:22':'pwd2'}@roles('role1')def role1():with cd('/tmp'):run('ls -l')@roles('role2')def role2():with cd('/tmp'):run('ls')def task():execute(role1)execute(role2)

最后记录我部署代码,方便日后参考

-- coding:utf-

8 --from fabric.api import local, cd, put, lcd, settings, env, runimport osimport timeenv.hosts = ['root@remoteiip:22']env.password = 'pwd'tar_file = 'minibbs.tar.gz'loc_tar_path = 'dist'def _pack():# 当前目录在minibss/dist下include = ['../minibbs/static/', '../minibbs/templates/', '../minibbs/.py', '../minibbs/.db', '../run.py']# 发生错误仅仅给出警告,而不是退出with settings(warn_only=True):local('rm -f %s' % os.path.join(loc_tar_path, tar_file))with lcd(loc_tar_path):local('tar -czvf {tar_file} --exclude=\\'{tar_file}\\' {include}'.format(tar_file=tar_file, include=' '.join(include)))def _deploy():remote_tar_path = '/root/haort/venv/tmp/' + tar_filewith settings(warn_only=True):run('rm -f %s' % remote_tar_path)put(os.path.join(loc_tar_path, tar_file), remote_tar_path)remote_pro = 'minibbs' + time.strftime('%Y%m%d%H%M', time.localtime())remote_proj_dir = '/root/haort/venv/' + remote_prowith settings(warn_only=True):run('mkdir %s' % remote_proj_dir)with cd(remote_proj_dir):run('rm -rf %s' % '*')run('tar -xzvf %s' % remote_tar_path)with cd('/root/haort/venv/'):run('rm -f minibbs')run('ln -s %s minibbs' % remote_pro)run('supervisorctl stop minibbs')run('supervisorctl start minibbs')def task():_pack()_deploy()

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 1
看完这篇文章有何感觉?已经有1人表态,0%的人喜欢 快给朋友分享吧~
评论(1)
后参与评论
霸气佳宜姐
哪里拷贝过来的? 质量这么差!!
2019/01/21 00:29:47

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程