Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local commands unsupported in fabric >=2? #1919

Open
jondkelley opened this issue Dec 21, 2018 · 9 comments
Open

Local commands unsupported in fabric >=2? #1919

jondkelley opened this issue Dec 21, 2018 · 9 comments

Comments

@jondkelley
Copy link

jondkelley commented Dec 21, 2018

Do you have a local example?

Is local support totally unsupported since version 1 fabric? There's no good docs by example, and the methods / support is non-existent now.

fab -f fabfile.py  log_local
No idea what 'log_local' is!

python

from fabric import Connection
from invoke import task, Exit
from invocations.console import confirm

# pip install invocations

@task
def log_local(c):
    """[Run ls -la on localhost]

    Arguments:
        c {[Connection]} -- [Explicit initial context argument, whose value will be a Connection object at runtime.]
    """
    c.local('ls -la')

This was following example here #1747

@thrownblown
Copy link

thrownblown commented Dec 21, 2018

i'm wrestling with this too, and this is what i got

from fabric import task
from fabric import Connection

user = 'ubuntu'
c = Connection(host='ip.ip.ip.ip', user=user)

@task
def pack(local):
    # Creates a tar gzipped archive of the current directory, the name of the
    # archive must be tarball.tar.gz and be place in the local dir
    local.run("tar --exclude='*.tar.gz' -cvzf tarball.tar.gz .")


@task
def deploy(local):
    # Uploads the archive to the remote server in the directory /tmp/
    # Creates the directory /tmp/tars
    # Untars the tarball.tar.gz archive in /tmp/tars
    c.run('mkdir -p /tmp/tars')
    c.put('tarball.tar.gz', remote='/tmp/')
    c.run('tar -C /tmp/tarball -xzf /tmp/tarball.tar.gz')


@task
def clean(local):
    # Deletes the tarball.tar.gz on your local machine
    local.sudo('rm ./tarball.tar.gz')

@thrownblown
Copy link

@jondkelley try
fab log_local

@JamesRamm
Copy link

Hey
@jondkelley @thrownblown

tldr; use run instead of local and don't specify the --hosts flag

local commands are supported but now there is no difference between run and local.
If you don't specify --hosts, then the Connection object falls back to being a regular Context object from invoke, and run works locally

Try this fabfile:

@task
def test(c):
  print(c)
  c.run("echo HELLO")

Running fab test will print 'HELLO', plus c, which will be a Context object.

Running fab --hosts=<host connection string> test will print 'HELLO' plus c which will be a Connection object

@rectalogic
Copy link

fab -f fabfile.py  log_local
No idea what 'log_local' is!

fabric tasks.auto_dash_names is True by default so you need to do fab -f fabfile.py log-local instead of log_local

@exhuma
Copy link

exhuma commented Sep 9, 2019

This issue is also something which stumped me when upgrading to Fabric 2. It is very confusing to have an object behaving differently depending on how fabric is called.

In one project I bypassed this issue by just defining an invoke tasks.py and then create fabric Connection instances inside this file. This way it becomes way more predictable and explicit. I might continue doing this in the future in other projects as well.

@christian-intra2net
Copy link

christian-intra2net commented Sep 27, 2019

You can ensure c is always a fabric Connection by giving a default host name to task():

@task(host='localhost')
def test(c):
    c.local('hostname')
    c.run('hostname')

But I also wonder why the author chose this behaviour (changing the type of c depending on host value). I would much prefer if c was always a fabric Connection, with c.local == c.run if no host is given.

@KantiCodes
Copy link

Have any of you tried to wrap Fabric into your own library (just as it's explained here for invoke)

@exhuma
Copy link

exhuma commented Jun 25, 2021

@KantiCodes how is this related to this issue?

@nafanz
Copy link

nafanz commented Apr 13, 2023

I constantly get confused with this, so I decided to leave a simple example without functions and tasks.
I hope it will be useful to someone.

from invoke import run

run('pwd')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants