天天看點

fabric上下文管理器(context mangers)

1.如何調用該方法:

from fabric import context_managers

ps:上下文管理需要用with關鍵字啟用:

    如:

        with context_managers.cd('/tmp'):

          run(...)

    這樣才能保證是在該目錄下操作。      

2.方法:

(1)with context_managers.cd(一個目錄的路徑):

    指定進入一個目錄,然後在該目錄下執行操作。

    例:

    def cd(path):

      with context_managers.cd(path):

        run('touch a.txt')

    在path的值的目錄下建立一個a.txt檔案。    

(2)with context_managers.hide(['warnings','running','stdout','stderr','debug','everything']):

    在hide中指定要隐藏的類型,warnings:警告、running:運作、stdout:标準輸出、stderr:标準錯誤、debug:調試資訊

    #1.不适用hide

    def no_hide(cmd):

      run('echo nimei')

    顯示:

        [192.168.1.219] Executing task 'no_hide'

        [192.168.1.219] run: echo nimei

        [192.168.1.219] Login password for 'root': 

        [192.168.1.219] out: nimei

        [192.168.1.219] out: 

        Done.

        Disconnecting from 192.168.1.219... done.    

    #2.running

    def hide_running(cmd):

      with context_managers.hide('running'):

        run('echo nimei') 

        [192.168.1.219] Executing task 'running_hide'

        [192.168.1.219] Login password for 'root': #此行本是[192.168.1.219] run: echo nimei,但被hide過濾掉了

    #3.stdout

    def stdout_hide(cmd):

      with context_managers.hide('stdout'):

        run(cmd)

    顯示

        [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f hide_test.py stdout_hide:'echo nimei'

        [192.168.1.219] Executing task 'stdout_hide'

                                                    #這一行的[192.168.1.219] out: nimei和[192.168.1.219] out: 沒有了,這就是過濾标準輸出

(3)with context_managers.show('debug'):        

    這個方法和hide正相反,hide是掩藏,而show是顯示,它的作用隻是為了開啟預設掩藏的debug模式。

(4)with context_managers.path(path,behavior='[append|prepend|replace]'):

    這個方法類似于在系統中執行:export PATH=$PATH:path1:path2,是指定指令路徑的。

    而path方法除了傳入一個path字元串參數以外,還要換入一個behavior=參數,參數有3個:

    1.append添加在$PATH的最後

    2.prepend添加在$PATH的最前方(個人認為,這個參數是為了讓你寫的執行檔案不至于和系統指令重名,它會優先執行你的執行檔案)

    3.replace将$PATH中的内容清空,然後将你将的path變量賦給它

    #腳本1:exec_file_echo.sh

    ##!/bin/bash

    #echo nimei

    #腳本2:ls

    #echo nimei    

    #1.append

    def append_path(path,exec_file):

      with context_managers.path(path,behavior='append'):

        run(exec_file)

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py append_path:'/gyk_fab/','exec_file_echo.sh'

    [192.168.1.219] Executing task 'append_path'

    [192.168.1.219] run: exec_file_echo.sh

    [192.168.1.219] Login password for 'root': 

    [192.168.1.219] out: nimei

    [192.168.1.219] out: 

    Done.

    Disconnecting from 192.168.1.219... done.

    #2.prepend

    def prepend_path(path,exec_file):

      with context_managers.path(path,behavior='prepend'):

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py prepend_path:'/gyk_fab/','ls'#ls本身不執行,而執行我寫的ls腳本,這就是将路徑添加在最前端的用處

    [192.168.1.219] Executing task 'prepend_path'

    [192.168.1.219] run: ls

    Disconnecting from 192.168.1.219... done.    

    #replace

    #範例1

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py replace_path:'/gyk_fab/','which pwd'

    [192.168.1.219] Executing task 'replace_path'

    [192.168.1.219] run: which pwd

    [192.168.1.219] out: /bin/bash: which: command not found

    Fatal error: run() received nonzero return code 127 while executing!

    Requested: which pwd

    Executed: /bin/bash -l -c "export PATH=\"\"/gyk_fab/\"\" && which pwd"

    Aborting.

    run() received nonzero return code 127 while executing!

    [root@salt-monion-1 gyk_fab]# which pwd

    /bin/pwd

    #範例2

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f path_test.py replace_path:'/gyk_fab/','ls'

    [192.168.1.219] out: /gyk_fab/ls: line 4: find: command not found

    Requested: ls

    Executed: /bin/bash -l -c "export PATH=\"\"/gyk_fab/\"\" && ls"

    [root@salt-monion-1 gyk_fab]# cat ls

    #!/bin/bash

    find / -name tmp

(5)with context_managers.prefix(系統指令):    

    該方法的作用和cmd1&&cmd2相同,就是當本指令執行成功後才能執行其他操作。

    #!/bin/env python2.7

    from fabric import context_managers

    from fabric.operations import run

    def no_prefix(prefix_cmd,run_cmd):

      run(prefix_cmd)

      run(run_cmd)

    def prefix_test(prefix_cmd,run_cmd):

      with context_managers.prefix(prefix_cmd):

        run(run_cmd)

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py prefix_test:'cd /tmp','ls'

    [192.168.1.219] Executing task 'prefix_test'

    [192.168.1.219] out: dir  pip_build_root  timer_creategFkcio.c  yum.log  yum_save_tx-2015-03-16-23-09uxAPw7.yumtx

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py prefix_test:'cd tmp','ls'

    [192.168.1.219] out: /bin/bash: line 0: cd: tmp: 沒有那個檔案或目錄

    Fatal error: run() received nonzero return code 1 while executing!

    Executed: /bin/bash -l -c "cd tmp && ls"

    run() received nonzero return code 1 while executing!

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py no_prefix:'cd /tmp','ls'

    [192.168.1.219] Executing task 'no_prefix'

    [192.168.1.219] run: cd /tmp

    [192.168.1.219] out: anaconda-ks.cfg  install.log  install.log.syslog

    [root@salt-monion-1 gyk_fab]# fab -H 192.168.1.219 -f test_prefix.py no_prefix:'cd tmp','ls'

    [192.168.1.219] run: cd tmp

    Requested: cd tmp

    Executed: /bin/bash -l -c "cd tmp"

(6)with context_managers.setting(env的key=env相應的值,...):

    暫時(在with的作用域裡),覆寫或更新任何提到的關鍵字的env變量的值,用env.鍵設定是全局變量,用這個可以設定局部變量。

    範例暫時不寫,等整理到env的時候在說。

(7)with context_managers.quiet():

    它等同于with context_managers.settings(context_managers.hide('everything'),warn_only=True):

    from fabric import colors

    from fabric.api import env

    env.hosts=['192.168.1.219']

    def quiet_test(cmd):

      with context_managers.quiet():

        status=run(cmd)

        if status.succeeded:

          print colors.green('nimei')

    [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py quiet_test:'ls'

    [192.168.1.219] Executing task 'quiet_test'

    nimei

    [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py quiet_test:'lasdddd'

    同功能代碼:

    env.password='659171'

    def quiet_test(cmd):        #context_managers.quiet()

    def equest_quiet(cmd):        #context_managers.settings()

      with context_managers.settings(context_managers.hide('everything'),warn_only=True):

        if status.succeeded :

        else:

          print colors.red('cao nimei')

    [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py equest_quiet:'lsaaa'

    [192.168.1.219] Executing task 'equest_quiet'

    cao nimei

    [root@salt-monion-1 gyk_fab]# fab -f quiet_test.py equest_quiet:'ls'

(7)with context_managers.warn_only():

    等同于context_mangers.setting(warn_only=True):

(8)context_managers.shell_env(鍵=值):

    為shell指令設定環境變量。比如說安裝ACE時需要制定ACE_HOME=安裝包路徑,這時候就可以使用shell_env

本文轉自 msj0905 51CTO部落格,原文連結:http://blog.51cto.com/sky66/1684901