fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] Simplification of contains?


From: Jeff Forcier
Subject: Re: [Fab-user] Simplification of contains?
Date: Thu, 28 May 2009 20:14:55 -0400

On Thu, May 28, 2009 at 7:13 PM, Jorge Vargas <address@hidden> wrote:
> I can't seem to find a way to simplify this, it looks to ugly for me.
>
> I basically need to know if env.domain is in a or b file and if it
> isn't put it in b and recreate C
>
>    with settings(warn_only=True):
>        check1 = contains(env.domain,'/etc/check1',exact=True)
>        if check1.failed:
>            check2 = contains(env.domain,'/etc/check2',exact=True):
>            if check2.failed:
>                append("%(domain)s" % env,'/etc/check2')
>                run('regenerate_config /etc/check3')
>
> As a related note, is there a simple way to provide a different
> message to contains (which call run, which calls abort) I'll like to
> have a "custom abort message, instead of the failed command something
> more human like. "This server failed check X"

Two things:

1. contains() uses grep, which returns the empty string when the input
isn't found, and since empty string evaluates to False in Python, it's
simplest/best to simply treat contains() as returning a boolean value.
No need to explicitly store the return value and then check .failed.
2. My intention thus far has been that "custom" output should be
handled by turning off Fabric's internal abort/warn messages, and
printing your own messages. The former can (as of alpha3) be
accomplished by using settings() to squash together warn_only=True and
hide('everything') or at least hide('warnings').

With just #1, you could save two or three lines, depending on your
line-length and wrapping tastes::

    with settings(warn_only=True):
        if not (contains(env.domain, '/etc/check1', exact=True)
            or contains(env.domain, '/etc/check2', exact=True)):
                append("%(domain)s" % env,'/etc/check2')
                run('regenerate_config /etc/check3')

With #2 (assuming for now that you only want to print a message if *both*
contains checks failed; easy enough to adapt otherwise) we use the ``hide``
context manager and a simple print statement::

    with settings(warn_only=True, hide('everything')):
        if not (contains(env.domain, '/etc/check1', exact=True)
            or contains(env.domain, '/etc/check2', exact=True)):
                print("[%s] /etc/checkN not found in domain file,
appending and regenerating" % env.host_string)
                append("%(domain)s" % env,'/etc/check2')
                run('regenerate_config /etc/check3')

One thing I'd like to do is make that "fake host prefix" bit easier, as I find
myself doing it a lot. Whether this is through a simple e.g. print_with_host
function, or funky monkeying with sys.stdout (I prefer the former to the
latter) I'm not sure yet. But having fabfile output easily associated
with the current host seems like a good idea (as long as it can also
be easily opted out of!)

Best,
Jeff




reply via email to

[Prev in Thread] Current Thread [Next in Thread]