我想直接從Python運行我現有的fabfile任務,而不使用cli。 如何捕獲「執行」異常並捕獲輸出以記錄發生任何異常?如何直接從Python運行Fabric任務來捕獲異常和輸出?
@roles(['localhost'])
def my_sudo_task():
sudo('ls -l /root/')
from fabfile import my_sudo_task
from fabric.tasks import execute
execute(my_sudo_task)
這裏是我的確切問題:
try:
# is there a way to leave out stderr and capture it somehow?
with settings(hide('stdout', 'stderr', 'aborts', 'warnings', 'running')):
execute(my_sudo_task)
except Exception as e: # doesnt catch error
print ('ERROR')
raise
except:
print ('ERROR') # catches but lacks the error message that i can log
所有我需要的是執行(任務)來觸發任何異常,當須藤(「不是一個有效的CMD」)失敗。工廠的存在方式1.肯定有比重寫每個任務「提高」更好的方法。 – jayshilling
從文檔:「但是,Fabric默認爲」fail-fast「行爲模式:如果出現任何問題,例如返回非零返回值的遠程程序或您的fabfile的Python代碼遇到異常,執行將立即停止。 – jumbopap
我澄清了這個問題,「除了異常作爲e'沒有捕獲任何東西,只是'除了'工作,但是沒用,因爲它不提供錯誤消息記錄 – jayshilling