fabric是啥?
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
这东西在批量化处理一些远程命令、任务,比较方便,稍微会点python(他是一个python库么,而且一般任务都写成pyton函数的形式)就可以上手,比较简单。
一、安装python
yum -y install zlib openssl-devel ## (不执行有可能在安装pip时候报错)
wget --no-check-certificate
tar xf Python-2.7.8.tar.xz
cd Python-2.7.8
./configure --prefix=/usr/local/python
vim Modules/Setup
##找到下面这句,去掉注释#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz 否则安装pip时候报错
make
make altinstall
ln -s /usr/local/python/bin/python2.7 /usr/local/bin/python
二、安装pip
pip和easy_install类似是一个安装和管理python包的工具.
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python get-pip.py
ln -fs /usr/local/python/bin/pip /usr/bin/pip
三、安装fabric
pip install fabric
ln -fs /usr/local/python2.7/bin/fab /usr/bin/fab
测试:
[eric@localhost ~]$ python
Python 2.7.8 (default, May 11 2015, 01:43:48)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fabric
四、fabric使用实例
1、http://docs.fabfile.org/en/1.10/tutorial.html
2、批量做免密登录的一个例子fabfile.py:
#!/usr/bin/env python
# -- encoding: utf-8 --
from fabric.api import local, cd, run, env
from fabric.colors import red, green
from fabric.context_managers import cd
from fabric.operations import *
from fabric.api import *
env.roledefs = {
'localhost':['localhost'],
'vacproxy':['xx.xx.xx.xx','xx.x.x.x']
}
#env.user='xxxx'
#env.password = 'xxxxx'
@roles('vacproxy')
def checks():
run('uname -a')
### get the sshkey from localhost
@roles('localhost')
def get_sshkey():
run('cp /home/aimcpro/.ssh/id_rsa.pub /home/aimcpro')
### put the sshkey to remote server
@roles('vacproxy')
def put_sshkey():
with cd('/tmp'):
put('/home/aimcpro/.ssh/id_rsa.pub','id_rsa.pub.master')
run('cat id_rsa.pub.master >> /opt/aimcpro/.ssh/authorized_keys')
def do_task():
#execute(checks)
# execute(get_sshkey)
execute(put_sshkey)
执行方法及结果:
3、发布web包的一个例子deploy.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*
#添加中文注释的编码
#fabfile.py
from fabric.api import *
env.user = 'root'
env.hosts = ['192.168.1.100']
#远程服务器的密码,这个建议不要填写,宁愿在运行的过程输入,因为这是明文显示的,不安全
#也可以将机子的ssh打通,这样就可以避免输入密码的麻烦
#env.password = 'testtest'
#从版本库取出,并删除.svn文件
def download():
local('rm -rf /tmp/newer' )
local('mkdir /tmp/newer')
local('svn checkout svn://localhost/newer /tmp/newer')
local('find /tmp/newer -name ".svn" | xargs rm -rf')
#压缩打包
def pack():
with lcd('/tmp/'):
local('tar czvf newer.tar.gz ./newer')
#部署远程服务器目录结构
def display():
run('rm -rf /var/www/html/tmp')
run('mkdir /var/www/html/tmp')
# 将本地的压缩包发送到远程服务器
put('/tmp/newer.tar.gz','/var/www/html/tmp')
with cd('/var/www/html/tmp'):
run('tar xvf newer.tar.gz')
#部分目录进行特别处理
with cd('/var/www/html/newer/public/'):
run('cp -r ueditor ../../tmp')
with cd('/var/www/html/'):
run('rm -rf newer')
run('cp -r /var/www/html/tmp/newer ./')
with cd('/var/www/html/newer/public'):
run('rm -rf ueditor')
run('cp -r /var/www/html/tmp/ueditor ./')
# 执行部署
def go():
download()
pack()
display()
执行方法:
fab -f deploy.py go