程序員瑞士軍刀之 Fabric

EdgarTroutm 8年前發布 | 7K 次閱讀 程序員 Git Python開發

Fabric是一個Python庫, 也是一個命令行工具, 通過 SSH 來做應用程序的部署和系統管理任務

它可以執行本地的遠程的系統命令, 上傳下載文件, 以及其他能用Python編程完成的任務

其實它一個工具框架, 執行一個默認的 python 文件 fabfile.py

簡單寫個小例子

$vi fabfile

from fabric.api import *

env.hosts = ['10.224.64.106']
env.user   = "root"
env.password = "pass"

def freedisk(param='-h'):
    cmd = 'df ' + param
    run(cmd)

def listfile(folder='~'):
    cmd = 'ls -l ' + folder
    run(cmd)

def pullcodes(folder='/workspace/cpp/snippets'):
    with cd(folder):
        run("git pull origin master")

# 察看遠程服務器上的磁盤剩余空間
$ fab listfile:folder=/home/walter
  • 為安全起見, 不用在文件中存放密碼, 在命令行提示輸入

    $ fab -u root -I -H 10.224.64.106 freedisk

  • 更好的做法是把本機私鑰預先拷貝到目標服務器上, 這樣就不用輸入密碼了

1. 在本機上生成公鑰  ~/.ssh/id_rsa.pub
    ssh-keygen -t rsa

    2. 拷貝此公鑰到目標服務器 10.224.64.106 上
    scp id_rs.pub root@10.224.64.106:/root

    3. 目標服務器 10.224.64.106 上
    cat id_rsa.pub >> ~/.ssh/authorized_keys
    chmod 700 ~/.ssh/authorized_keys

常用方法

  • run (fabric.operations.run)
  • sudo (fabric.operations.sudo)
  • local (fabric.operations.local)
  • get (fabric.operations.get)
  • put (fabric.operations.put)
  • prompt (fabric.operations.prompt)
  • reboot (fabric.operations.reboot)

常用函數

  • cd (fabric.context_managers.cd)
  • lcd (fabric.context_managers.lcd)
  • path (fabric.context_managers.path)
  • settings (fabric.context_managers.settings)
  • prefix (fabric.context_managers.prefix)

例子:批量上傳下載文件

from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm 

env.user='root'
env.hosts=['10.224.64.106'] 
env.passwords = { 
    'root@10.224.64.106:22': 'password'
  } 

local_dir='/workspace/cpp/codelab'
remote_dir = '/home/walter/cpp/codelab'
file_list = [
    'src/FileUtils.cpp',
    'src/FileUtils.h',
    'src/Makefile.am',
    'src/StringUtils.cpp'
]

@task
def hostinfo():
    run('uname -s')

@task
def upload(): #upload file task 
    with cd(remote_dir) :
        for filename in file_list:
            local_file  = local_dir  + "/" + filename
            remote_file = remote_dir + "/" + filename
            #print local_file, " to ", remote_file
            with settings(warn_only=True):    #when upload error,continue 
                result = put(local_file, remote_file) 
            if result.failed and not confirm("put file failed,Continue[Y/N]?"): 
                abort("Aborting file put task!")


@task
def download(): #upload file task 
    with cd(remote_dir) :
        for filename in file_list:
            local_file  = local_dir  + "/" + filename
            remote_file = remote_dir + "/" + filename
            #print local_file, " to ", remote_file
            with settings(warn_only=True):    #when upload error,continue 
                result = get(remote_file,local_file)
            if result.failed and not confirm("put file failed,Continue[Y/N]?"): 
                abort("Aborting file put task!")

高階用法

設置角色role來指定遠程的服務器范圍

或者直接用字典由輸入參數指定, 例如:

# usage:  
# fab localpull:rtc
# fab checkfiles:hf2
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm 


env.user = 'root'
env.roledefs = {
    'qa': ['root@10.224.57.202:22'],
    'dev': ['root@10.224.64.106:22']
}

env.passwords = { 
    'root@10.224.57.202:22': 'pass',
    'root@10.224.64.106:22': 'pass',
    'root@10.224.64.107:22': 'pass'
  } 

@roles('dev')
@task
def localpull(app='web'):
    if app == 'web':
        code_dir = '/workspace/walter/hfweb'
        with lcd(code_dir):
            local("git pull origin master")
    elif app == 'rtc':
        code_dir = '/workspace/walter/hfrtc'
        with lcd(code_dir):
            local("git pull origin master")
            local("git branch -l")

test_servers = {'hf1':['root@10.224.64.46:22'],
    'hf2':['root@10.224.64.106:22'],
    'hf3':['root@10.224.64.107:22']}

@task
def listfiles():
    run("ls -l")

@task
def checkfiles(target_env='hf2'):
    execute("listfiles", hosts=test_servers[target_env])

FAQ

問題: fab put error: paramiko.ssh_exception.SSHException: Channel closed

解決方法:

  • 編輯 /etc/ssh/sshd_config:

    vi /etc/ssh/sshd_config
  • 加上一行 Subsystem sftp internal-sftp

    Port 22
      Protocol 2
      LogLevel INFO
      X11Forwarding no
      MaxAuthTries 4
      IgnoreRhosts yes
      HostbasedAuthentication no
      PermitRootLogin yes
      PermitEmptyPasswords no
      PermitUserEnvironment no
      Ciphers aes128-ctr,aes192-ctr,aes256-ctr
      ClientAliveInterval 600
      Banner /etc/issue
      Subsystem      sftp    internal-sftp
  • 保存并重啟 SSH server:

    service sshd restart

參考鏈接

 

 

來自:http://www.jianshu.com/p/e7623533e886

 

 本文由用戶 EdgarTroutm 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!