简体   繁体   English

使用 Fabric 2 运行时如何忽略命令失败

[英]How to ignore command failure when running it using Fabric 2

In Fabric 1 it looks like this:Fabric 1中,它看起来像这样:

with settings(warn_only=True):
    run_commands_that_may_fail()

It is unclear how to implement it in Fabric 2 , pyinvoke via context manager.目前还不清楚如何在Fabric 2中实现它,通过上下文管理器进行pyinvoke The upgrade docs recommend replacing warn_only with run.warn .升级文档建议将warn_only替换为run.warn I've come up with:我想出了:

old_warn = c.config.run.warn
c.config.run.warn = True
try:
    run_commands_that_may_fail(c)
finally:
    c.config.run.warn = old_warn

Perhaps, there is a nicer way that is similar to Fabric's 1.也许,有一种更好的方法,类似于 Fabric 的 1。

As Artemis said, I think a context manager would be best here - unless you're happy to provide the kwarg for every specific call.正如Artemis所说,我认为这里最好使用上下文管理器 - 除非您乐于为每个特定调用提供 kwarg。 For example:例如:

if c.run('test -f /opt/mydata/myfile', warn=True).failed:
    c.put('myfiles.tgz', '/opt/mydata')

Concerning the project manager, you could create a whole new config so you don't have to modify the original:关于项目经理,您可以创建一个全新的配置,因此您不必修改原始配置:

import contextlib

import fabric

config = fabric.Config()
# set options


@contextlib.contextmanager
def run_with_warnings(old_config):
    new_config = old_config.clone()
    new_config.config.run.warn = True
    yield new_config


with run_with_warnings(config) as config_allowing_warning:
    run_commands_that_may_fail(config_allowing_warning)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM