我在我的fabfile中有一些結構任務,我需要在它們執行前初始化env變量。我試圖使用裝飾器,它的作品,但織物總是說:「找不到主機請指定(單)」但是,如果我打印我的變量「env」的內容似乎都很好。 另外我從另一個python腳本調用我的任務。Python結構裝飾器
from fabric.api import *
from instances import find_instances
def init_env(func):
def wrapper(*args, **kwargs):
keysfolder = 'keys/'
env.user = 'admin'
env.key_filename = '%skey_%s_prod.pem'%(keysfolder, args[0])
env.hosts = find_instances(args[1])
return func(args[0], args[1])
return wrapper
@init_env
def restart_apache2(region, groupe):
print(env.hosts)
run('/etc/init.d/apache2 restart')
return True
我的腳本調用fabfile:
from fabfile import init_env, restart_apache2
restart_apache2('eu-west-1', 'apache2')
打印輸出在重啓的Apache2:
[u'10.10.0.1' ,u'10.10.0.2' ]
任何想法爲什麼我的任務restart_apache2不使用env變量?
感謝
編輯:
這是有趣的是,如果在我的腳本調用fabfile,我使用fabric.api設置,並設置主機IP,它的工作原理。這表明我的裝飾器很好地初始化了env變量,因爲密鑰和用戶被髮送到結構。這只是這不是由織物閱讀env.hosts ...
EDIT2:
我可以使用從fabric.api設置,這樣達到我的目標:
@init_env
def restart_apache2(region, groupe):
for i in env.hosts:
with settings(host_string = '%[email protected]%s' % (env.user, i)):
run('/etc/init.d/apache2 restart')
return True
獎金的問題,有沒有解決方案直接使用env.hosts沒有設置?
看起來像'init_env'中有一個縮進錯誤。兩個返回語句看起來應該縮進一個級別。 – millerdev
這是一個壞的複製粘貼,我原來的縮進是好的。我修改了我的帖子。 – Matt
您提到您正在調用另一個python腳本的任務。你是用'fab'命令還是其他方式調用另一個腳本? – millerdev