fabric

Fabric是一个Python(2.5-2.7)库,用于简化使用SSH的应用程序部署或系统管理任务。

它提供的操作包括:执行本地或远程shell命令,上传/下载文件,以及其他辅助功能,如提示用户输入、中止执行等。

本文主要介绍CentOS 6.3上使用fabric进行自动部署的基本方法。

1.    环境部署

本节主要介绍python版本升级,pip及fabric部署方法。

1.1.    Python版本升级

CentOS 6.3自带的Python版本为2.6,首先需要升级到2.7版本。由于旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装。

1. 下载Pyhon,选择下载Gzipped source tar ball (2.7.6) (sig),网址:https://www.python.org/download/releases/2.7.6

2. 解压安装,命令如下

tar -xvf Python-2.7.6.tgz

cd Python-2.7.6

./configure --prefix=/usr/local/python2.7

make

make install

3. 创建链接来使系统默认python变为python2.7

ln -fs /usr/local/python2.7/bin/python2.7 /usr/bin/python

4. 查看Python版本

python –V

5. 修改yum配置(否则yum无法正常运行)

vi /usr/bin/yum

将第一行的#!/usr/bin/python修改为系统原有的python版本地址#!/usr/bin/python2.6

至此CentOS6.3系统Python已成功升级至2.7.6版本。

1.2.    安装pip

Pip是一个安装和管理python包的工具。

安装方法如下:

1. 下载pip,地址wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate

2. 执行安装命令

python get-pip.py

3. 创建连接(否则会报错提示“命令不存在”)

ln -s /usr/local/python2.7/bin/pip /usr/bin/pip

1.3.    安装fabric

1. 执行安装命令

pip install fabric

2. 创建连接(否则会报错提示“命令不存在”)

ln -s /usr/local/python2.7/bin/fab /usr/bin/fab

2.    脚本编写

本节对fabric用法进行简单介绍,并提供实例以供参考。

2.1.    Hello,fab

1. 在当前目录下新建文件fabfile.py,输入内容如下

1
2
3
def hello():
 
     print("Hello fab!")

2. 执行命令fab hello,结果如下

1
2
3
# fab hello
 
Hello fab!

3. 文件名不为fabfile.py时需进行指定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# mv fabfile.py test.py
 
# fab hello
 
  
 
Fatal error: Couldn't find any fabfiles!
 
  
 
Remember that -f can be used to specify fabfile path, and use -h for help.
 
  
 
# fab -f test.py hello
 
Hello fab!

4. 参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#vi fabfile.py
 
def hello(name):
 
         print 'Hello %s!'%name
 
  
 
# fab hello:name=fab
 
Hello fab!
 
  
 
  
 
# fab hello:fab
 
Hello fab!

2.2.    本地操作

执行本地操作命令使用local

1. fabfile.py脚本内容如下

1
2
3
4
5
6
7
8
9
from fabric.api import local
 
  
 
def test():
 
local('cd /home/')
 
local('ls -l|wc -l')

2. 执行命令fab test,结果如下

1
2
3
4
5
6
7
# fab test
 
[localhost] local: cd /home/
 
[localhost] local: ls -l|wc -l
 
8

2.3.    远程操作

执行远程操作命令使用run

1. fabfile.py脚本内容如下

1
2
3
4
5
6
7
8
9
10
11
from fabric.api import cd,run,env,hosts
 
env.hosts=['192.168.85.99:22','192.168.85.101:22']
 
env.password='test'
 
def test():
 
with cd('/home'):
 
run("du -sh")

2. 执行命令fab test,结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# fab test
 
[192.168.85.99:22] Executing task 'test'
 
[192.168.85.99:22] run: du -sh
 
[192.168.85.99:22] out: 392G      .
 
[192.168.85.99:22] out:
 
[192.168.85.101:22] Executing task 'test'
 
[192.168.85.101:22] run: du -sh
 
[192.168.85.101:22] out: 5.6G     .
 
[192.168.85.101:22] out:
 
Disconnecting from 192.168.85.99... done.
 
Disconnecting from 192.168.85.101... done.

3. 多服务器混合,需要在不同服务器进行不同操作时,可参考如下脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from fabric.api import env,roles,run,execute
 
env.roledefs = {
 
'server1': ['[email protected]:22',],
 
'server2': ['[email protected]:22', ]
 
}
 
env.password = 'test'
 
@roles('server1')
 
def task1():
 
run('ls /home/ -l | wc -l')
 
@roles('server2')
 
def task2():
 
run('du -sh /home')
 
def test():
 
execute(task1)
 
execute(task2)

结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# fab test
 
[[email protected]:22] Executing task 'task1'
 
[[email protected]:22] run: ls /home/ -l | wc -l
 
[[email protected]:22] out: 27
 
[[email protected]:22] out:
 
[[email protected]:22] Executing task 'task2'
 
[[email protected]:22] run: du -sh /home
 
[[email protected]:22] out: 1.4G   /home
 
[[email protected]:22] out:
 
Disconnecting from 192.168.85.99... done.
 
Disconnecting from 192.168.85.100... done.

你可能感兴趣的:(python)