fab-user
[Top][All Lists]
Advanced

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

Re: [Fab-user] Running fabric from another python script?


From: Jeff Forcier
Subject: Re: [Fab-user] Running fabric from another python script?
Date: Wed, 29 Jul 2009 09:41:23 -0400

Hi Erik,

On Tue, Jul 28, 2009 at 6:37 PM, Erik Wickstrom<address@hidden> wrote:
> Is it possible to use Fabric in "scripted mode" instead of from the
> command line with the "fab" utility?

It is, and the goal is for it to be as easy as using the command line.
However, so far I've focused on getting the common case -- CLI usage
-- working first, and have not put as much effort into ensuring a
smooth Fabric-as-library experience. So it's not quite there yet,
though it's still quite possible because everything is pretty
straightforward Python.

> Problem is, when I try to run "production(); blah();" from within my
> script I get "No hosts found. Please specify (single) host string for
> connection:". (the hosts are defined in production() and work fine
> from the command line.)

This is because individual invocations of run/sudo/etc do not look at
env.hosts -- the execution model is that fabric.main.main() figures
out what functions you asked for on the command line, then for each
function it builds a host list (by looking for CLI args, env.hosts,
@hosts and so forth) and iterates over that host list, setting
env.host_string each time and then calling the function.

So right now, because things aren't factored out yet to make that easy
to do this via a library (fabric.main.main() does a few other things
which probably won't work well as a lib call), you would have to mimic
that behavior, e.g.::

    import fabfile

    fabfile.production() # sets fabfile.env.hosts

    for host_str in fabfile.env.hosts:
        fabfile.env.host_string = host_str
        fabfile.blah()

(If it wasn't obvious I'm just using the fact that fabric.env has been
imported into your fabfile; because we use shared state you could just
as easily import fabric.api.env directly.)

As I said, I haven't put any serious thought into this yet, but offhand I'd not
be surprised if fabric.main.main() gets refactored further so you that the above
turns into something like e.g.::

    from fabric.main import execute

    fabfile.production() # still sets fabfile.env.hosts
    execute(fabfile.blah) # takes care of iterating over fabfile.env.hosts

There have actually been patches sent on along these rough lines and I'll be
looking at these and doing my own thinking to see what the best route is.

Best,
Jeff




reply via email to

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