Discussion:
[Fab-user] fabric2 cd with multiple servers
Lamont Nelson
2017-10-11 02:05:04 UTC
Permalink
I'm learning the fabric2 api and wanted to know the best way to run a
command with a particular working directory on a set of servers.

The README indicates that there is a 'execute' method on Group, but this
doesn't seem to be the case. I was able to get code below to work by
calling run on each connection in the group. Is there a more succinct way
to do this besides the "cd foo && cmd" method? Or am I overlooking
something?

-----
from fabric import ThreadingGroup
g=ThreadingGroup('host1','host2')
g.run('cd /tmp && ls foo')

-- or --

from fabric import ThreadingGroup
g=ThreadingGroup('host1','host2')
for c in g:
with c.cd('/tmp'):
c.run('ls foo')
-----

Regards,
Lamont
Ben Abrams
2017-10-13 03:21:41 UTC
Permalink
Lamont,

It's honestly been a while since I used fabric and have not really stayed
up on it so apologies if my research was too hastily done and incorrect.
There does seem to be the functionality you are looking for in both fabric
1 and 2 (alpha) though they have some slight differences.

In fabric 1.x it came from here:
https://github.com/fabric/fabric/blob/1.14/fabric/context_managers.py like
this:
https://github.com/fabric/fabric/blob/1.14/fabric/context_managers.py#L20-L21

In fabric 2.x they broke out that into the invoke library here:
https://github.com/pyinvoke/invoke/blob/master/invoke/context.py like this:
https://github.com/pyinvoke/invoke/blob/master/invoke/context.py#L328-L329

Hope this helps,
Post by Lamont Nelson
I'm learning the fabric2 api and wanted to know the best way to run a
command with a particular working directory on a set of servers.
The README indicates that there is a 'execute' method on Group, but this
doesn't seem to be the case. I was able to get code below to work by
calling run on each connection in the group. Is there a more succinct way
to do this besides the "cd foo && cmd" method? Or am I overlooking
something?
-----
from fabric import ThreadingGroup
g=ThreadingGroup('host1','host2')
g.run('cd /tmp && ls foo')
-- or --
from fabric import ThreadingGroup
g=ThreadingGroup('host1','host2')
c.run('ls foo')
-----
Regards,
Lamont
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
--
Ben Abrams
Computer Consultant
702-900-7926
benabrams.it
***@benabrams.it
Lamont Nelson
2017-10-13 06:51:55 UTC
Permalink
Thanks for the help. I did see the functionality in the invoke project, but
it seems this is focused on running commands locally.

I am trying to figure out the best way to chdir and run commands on a
remote server using the Group objects
<https://github.com/fabric/fabric/blob/v2/fabric/group.py#L9>. It seems
that right now we need to call cd on each connection object in a group, and
then call run on that connection. This works but it seems redundant for
such a simple use case --- run a command in a directory on a group of
servers. Since I will run complex commands I wanted to avoid the "cd foo &&
cmd" syntax.

It would be nice if we could specify the current directory (or provide a
context) when calling the run method for Group objects. Alternatively, it
seems that the execute
<https://github.com/fabric/fabric/blob/v2/fabric/group.py#L124> method will
pass in a context object to a @task, but this is not implemented yet.

I haven't tried the command line tool, so maybe I am overlooking something.

Regards,

Lamont
Post by Ben Abrams
Lamont,
It's honestly been a while since I used fabric and have not really stayed
up on it so apologies if my research was too hastily done and incorrect.
There does seem to be the functionality you are looking for in both fabric
1 and 2 (alpha) though they have some slight differences.
In fabric 1.x it came from here: https://github.com/
https://github.com/fabric/fabric/blob/1.14/fabric/context_managers.py#
L20-L21
https://github.com/pyinvoke/invoke/blob/master/invoke/context.py like
this: https://github.com/pyinvoke/invoke/blob/master/
invoke/context.py#L328-L329
Hope this helps,
Post by Lamont Nelson
I'm learning the fabric2 api and wanted to know the best way to run a
command with a particular working directory on a set of servers.
The README indicates that there is a 'execute' method on Group, but this
doesn't seem to be the case. I was able to get code below to work by
calling run on each connection in the group. Is there a more succinct way
to do this besides the "cd foo && cmd" method? Or am I overlooking
something?
-----
from fabric import ThreadingGroup
g=ThreadingGroup('host1','host2')
g.run('cd /tmp && ls foo')
-- or --
from fabric import ThreadingGroup
g=ThreadingGroup('host1','host2')
c.run('ls foo')
-----
Regards,
Lamont
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
--
Ben Abrams
Computer Consultant
702-900-7926 <(702)%20900-7926>
benabrams.it
Loading...