2016-04-15 17 views
0

ファブリック(1.11.1)(Paramiko(1.16.0)、Python(2.7.11))の最新バージョンを実行していて、もっとも不思議なエラーが発生しました。やってみて。ファブリック設定の問題

from fabric.api import run, sudo, task 
from fabric.context_managers import settings 


@task 
def test(): 
    print('regular run') 
    run('whoami') 

    print('regular sudo') 
    sudo('whoami') 

    print('sudo with user arg') 
    sudo('whoami', user='www-data') 

    with settings(user='www-data'): 
     print('run inside settings') 
     run('whoami') 

出力:

$ fab -f test.py -H [email protected]:2222 test 
[[email protected]:2222] Executing task 'test' 
regular run 
[[email protected]:2222] run: whoami 
[[email protected]:2222] out: vagrant  # <--- good 
[[email protected]:2222] out: 

regular sudo 
[[email protected]:2222] sudo: whoami 
[[email protected]:2222] out: root   # <--- good 
[[email protected]:2222] out: 

sudo with user arg 
[[email protected]:2222] sudo: whoami 
[[email protected]:2222] out: www-data  # <--- good 
[[email protected]:2222] out: 

run inside settings 
[[email protected]:2222] run: whoami 
[[email protected]:2222] out: vagrant  # <--- WHAT THE HECK!? this used to work 
[[email protected]:2222] out: 


Done. 

が何かを変更しましたか?それとも、私は何か間違っているだけですか?

答えて

0

-H [email protected]:2222のようなかわいいことをすると、もう1つのファブリックの問題が発生します。env.user = 'vagrant'; env.host = '127.0.0.1'; env.port = '2222'に内部的には分解されませんが、host_stringに保持されます。 だから...ぐふ、世界の最も醜いハックです:

from fabric.api import run, sudo, task, env 
from fabric.context_managers import settings as _settings 


def settings(*args, **kwargs): 
    """ 
    Helper function because Fabric's setting() is broken 

    Checks to see if there is a '@' in the host_string and if there is, it 
    will then append it to the host_string since that will be how it changes 
    users. Otherwise if the host_string is not being used, it will use the 
    default "swap user" functionality 
    """ 
    if 'user' in kwargs and '@' in env.host_string: 
     kwargs['host_string'] = '{}@{}'.format(
      kwargs.pop('user'), 
      env.host_string.split('@')[1] 
     ) 
    return _settings(*args, **kwargs) 


@task 
def test(): 
    print('regular run') 
    run('whoami') 
    print('regular sudo') 
    sudo('whoami') 

    print('sudo with user arg') 
    sudo('whoami', user='www-data') 
    with settings(user='www-data'): 
     print('run inside settings') 
     run('whoami') 

はこれが唯一の彼らを実現するために、かなり「クリーン」なコードを維持し、すべての場所でwith settings(host_string='[email protected]' + env.host_string.split('@')[1]):の束を持っていないのに役立ちますファブリックコマンドを定義した場合は、fab .. --user=vagrant --host=127.0.0.1 --port=2222のようにブレークします。このソリューションは、次のように動作します:

fab -f test.py --user=vagrant --host=127.0.0.1 --port=2222 test

fab -f test.py -H [email protected]:2222 test


旧ソリューション

from fabric.api import run, sudo, task, env 
from fabric.context_managers import settings 


@task 
def test(): 
    print('regular run') 
    run('whoami') 
    print('regular sudo') 
    sudo('whoami') 

    print('sudo with user arg') 
    sudo('whoami', user='www-data') 
    with settings(host_string='[email protected]' + env.host_string.split('@')[1]): 
     print('run inside settings') 
     run('whoami') 

出力:

$ fab -f test.py -H [email protected]:2222 test 
[[email protected]:2222] Executing task 'test' 
regular run 
[[email protected]:2222] run: whoami 
[[email protected]:2222] out: vagrant  # <--- good 
[[email protected]:2222] out: 

regular sudo 
[[email protected]:2222] sudo: whoami 
[[email protected]:2222] out: root   # <--- good 
[[email protected]:2222] out: 

sudo with user arg 
[[email protected]:2222] sudo: whoami 
[[email protected]:2222] out: www-data  # <--- good 
[[email protected]:2222] out: 

run inside settings 
[[email protected]:2222] run: whoami 
[[email protected]:2222] out: www-data  # <--- good 
[[email protected]:2222] out: 


Done. 

誰もがこのために良い仕事を知っているなら、私に知らせてください、私はすべての耳です!