【问题标题】:Supress abort messages in python fabric with non zero exit status抑制具有非零退出状态的python结构中的中止消息
【发布时间】:2016-02-16 11:08:22
【问题描述】:

我的程序在远程主机上运行一个命令,并且一些秘密参数作为命令行传递给结构运行函数。但如果命令返回非零状态,它会在标准错误中打印完整的命令。

我试过下面的代码

class FabricException(Exception):
    def __init__(self, message):
        self.message = message
        print self.message

env.abort_exception = FabricException

try:
    execute(fabric_run, intepreter=intepreter, script_name=script_name, command_line=command_line, hosts=hosts, sudo=sudo, warn_only=warn_only)
except FabricException("Something"):
    sys.exit(1)

但在任何时候,如果程序返回非零退出状态,我会在控制台上打印我想要避免的回溯。无论如何,我可以在织物上实现这一点吗?我希望它在 warn_only 为 True 以及 warn_only 为 False 时工作

【问题讨论】:

    标签: python python-2.7 fabric traceback


    【解决方案1】:

    当 warn_only = True 时,您可以使用结构 context_managers 抑制消息。使用 warn_only = False ,即使使用 context_managers 隐藏所有内容,它似乎也会显示错误。 quiet() context_manager 是 settings(hide('everything'), warn_only=True) 的别名。请看下面的例子:

    >>> from fabric.api import run, execute
    >>> from fabric.context_managers import quiet
    >>> def task_a():
    ...  return run('sdfsdgfsd')
    ...
    >>> with quiet():
    ...  a = execute(task_a,hosts=['testhost'])
    ...
    >>> a
    {'testhost': '/bin/bash: sdfsdgfsd: command not found'}
    

    使用默认的 warn_only = False:

    >>> from fabric.context_managers import hide
    >>> def task_a():
    ...  return run('sdfsdgfsd')
    ...
    >>> with hide('everything'):
    ...  a = execute(task_a,hosts=['testhost'])
    ...
    
    Fatal error: run() received nonzero return code 127 while executing!
    
    Requested: sdfsdgfsd
    Executed: /bin/bash -l -c "sdfsdgfsd"
    
    ============================================================================================= Standard output =============================================================================================
    
    /bin/bash: sdfsdgfsd: command not found
    
    ============================================================================================================================================================================================================
    
    Aborting.
    run() received nonzero return code 127 while executing!
    
    Requested: sdfsdgfsd
    Executed: /bin/bash -l -c "sdfsdgfsd"
    
    ============================================================================================= Standard output =============================================================================================
    
    /bin/bash: sdfsdgfsd: command not found
    
    ===========================================================================================================================================================================================================
    

    【讨论】:

    • warn_only = False 时,我仍然可以看到打印到 stderr 的错误,这是我想要避免的。如果我将一些秘密/密码作为命令行传递,它会显示在不安全的控制台中。
    猜你喜欢
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多