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

869 查看

最近公司给了台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参数

  1. 创建fabfile.py文件

    vim fabrile.py
  2. 编辑代码

    #coding:utf-8
    from fabric.api import local#
    def hello():
    # local函数用来执行本地命令
    local('echo "hello wolrd!"')
  3. 执行代码

    fab hello

    可以通过fab -l查看可以执行的任务,当前为hello函数
  4. 运行结果

    [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('本地文件','远程目录')
env fabric环境,配置远程ip、端口、密码 见下文

fabric远程执行例子

将本地/local/tmp目录下创建hello.txt(内容“hello world!”)文件上传至服务器/remote/tmp上,并显示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!

多个目标服务器

  1. 相同密码或者手动输入:

    env.hosts = ['root@ip1:22',root@ip2:22]
  2. 不同密码或者不想手动输入(此方法也可定义不角色一组服务器):

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