http://my.oschina.net/guol/blog/97607 Python fabric实践操作


A.

安装python2.7.5

1. 下载python2.7.5,保存到 /data/qtongmon/software

http://www.python.org/ftp/python/


2. 解压文件

tar xvf Python-2.7.5.tar.bz2


3. 创建安装目录

mkdir /usr/local/python27


4. 安装python

./configure --prefix=/usr/local/python27

make

make install


5. 修改老版本的ln指向(注意:这里修改后,可能会影响yum的使用)

mv /usr/bin/python /usr/bin/python2.4.3

ln -s /usr/local/python27/bin/python2.7 /usr/bin/python


B.

yum出现“No module named yum”错误


安装了Python2.7,发现yum无法使用,报错信息如下:

There was a problem importing one of the Python modules

required to run yum. The error leading to this problem was:

 No module named yum


解决办法:

修改yum文件

#vi /usr/bin/yum

将 #!/usr/bin/python 修改为 #!/usr/bin/python2.4


C.

安装pip


看情况(需要先安装Setuptools

1.wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

2.python ez_setup.py)


安装pip

1.wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py

2.python get-pip.py

报错:

python: ImportError: No module named bz2


很明显这个python中没有装bz2的库导致的

解决方法:

yum install bzip2-devel

然后需要重新编译一下python,之后再import bz2就不会提示错误了


D.

安装fabric

1.ln -s /usr/local/python27/bin/pip /usr/bin/pip

2.pip install fabric


——————————————————

fabric实例操作


twmg httpd start的方式远程启动httpd:

注:3个脚本在同一个目录下

1.vim httpd.py

from fabric.api import *

from fabric.colors import *

env.hosts = ['192.168.1.122']

env.password = '10241010'

def httpdstart():

       print green('start 122 httpd')

       run('/soft/start.sh')


2.vim httpd.sh

#!/bin/bash

start() {

        /usr/bin/fab -f /soft/httpd.py httpdstart

       }


case "$1" in

start)

     start

       ;;

    *)

    echo $"Usage:  start"

       exit 1

esac


3.vim twmg

#!/bin/sh

case "$1" in

 httpd)

     /soft/httpd.sh $2

     ;;

 *)

 echo $"Usage: start"

exit 0

esac


4.

ln -s /soft/twmg /usr/bin/twmg(或者/bin/twmg!)


5.

twmg httpd start