python自动化部署工具_python学习-fabric(高效远程自动化部署工具)

最近公司给了台web测试服务器,最近正好学习python语言的flask框架,琢磨着搭个小博客玩玩,但是每次修改代码后,都需要重新打包代码,然后sftp到服务器,再解压,重启服务。作为一个懒人当然不能忍,于是乎发现了这货-fabric。

fabric是python语言实现的一个利用ssh高效部署和管理系统的工具。

安装

省略python、pip管理工具安装过程

pip install fabric

验证是否安装成功

进入python编辑模式,输入代码,无错即表示成功安装

from fabric.api import local

fabric版hello world

fabric 默认文件fabfile.py,当然如果不想用这个名字,需要加-f参数创建fabfile.py文件

vim fabrile.py

编辑代码

#coding:utf-8

from 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-8

from fabric.api import local,cd,put,lcd,env,run

# 目标hosts(可多个),用户@ip:22,使用ssh

env.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-8

from fabric.api import local,cd,put,lcd,env,run,execute,roles

env.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, run

import os

import time

env.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_file

with 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_pro

with 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()

作者:haort

链接:https://www.jianshu.com/p/db91d33121d5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值