2016-04-15 4 views
0

Запуск последней версии Fabric (1.11.1) (Paramiko (1.16.0), Python (2.7.11)) и im, получивший самую странную ошибку, я сделал небольшое доказательство концепции для попробуйте.Проблема с настройками Fabric()

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

Другой Fabric вопрос, оказывается, если вы делаете симпатичные вещи, как: -H [email protected]:2222 внутренне не разорвать его к тому, что можно было бы ожидать: 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]): повсюду, только чтобы понять, что они break, если вы определяете команду ткани как: 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. 

Если кто-то знает хорошую работу для этого, пожалуйста, дайте мне знать, им все уши!