fab-user
[Top][All Lists]
Advanced

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

Re: [Fab-user] Not having much luck with your initial examples.


From: Bob Gustafson
Subject: Re: [Fab-user] Not having much luck with your initial examples.
Date: Sat, 24 Nov 2018 13:27:01 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0

Hi Nick

If you are really serious about pushing corrections to the Fabric/Python documentation, you could just grab a random fabric 'Getting Started' page and look at the first code line (more or less like I did below). Since there are so many variations (2 pythons X 3 Fabrics = 6 possible starting points), just specifying the environment of the first 'Hello World' example would be a big help.

This is not so easy. With two possibilities of python and three possibilities of the fabric library, there are six combinations of starting points before the '>>> from fabric import Connection' line.

The documentation for fabric is quite good looking. However, looking at Fabric Overview and Tutorial (http://docs.fabfile.org/en/1.14/tutorial.html), the 5th line into that document recommends looking at the 'usage documentation' (http://docs.fabfile.org/en/1.14/index.html#usage-docs). This is a short page and on the 4th line it recommends looking at the Overview and Tutorial (http://docs.fabfile.org/en/1.14/tutorial.html), which of course is the page I just came from. This 'going in circles' is a metaphor for a lot of the fabric documentation.

----------

Ok, I'm cheating now. I talked with my son for awhile this morning. He has several remote deployments going in Europe - all using Fabric. I mentioned that when I put in the @task decorator as you had recommended, I got an error complaining about "TypeError: Tasks must have an initial Context argument!". This was a new error for me!!!

My son explained that I needed a few lines at the top of the fabfile.py to define the environment 'Context' where the command (hello world) will execute. I said I already was using virtualenv and had the (env) in front of my shell prompt.

He said that the 'env' I was using was just for Python, not for Fabric. Fabric needs an *Additional* environment ('Context') set up. In the Getting Started document, there is no mention of 'Context' prior to the first code line.

With that new clue, I scrambled off into Google with a new series of searches - looking for 'Context' in combination with fabric, python, etc.

I found a different Overview and Tutorial (http://docs.fabfile.org/en/1.4.1/tutorial.html). There was a warning bar saying that I was not viewing the latest version - I should get 1.12.1 - which I did.

This much newer Overview and Tutorial looked quite familiar (http://docs.fabfile.org/en/1.12.1/tutorial.html) but it is not quite the same as the 1.14 referenced above.

The first code lines, to be typed into fabfile.py:

def hello():
    print("Hello world!")

of course do not work:

(env) air:fabric-test bobgus$ fab hello
No idea what 'hello' is!

Ok, I can put in the @task decorator as you suggested initially

@task
def hello():

    print("Hello world!")
(env) air:fabric-test bobgus$ fab hello
...
NameError: name 'task' is not defined

Ok, we have seen that before. By now (after days...) I know that @task must be imported.

I can add the line "from fabric.api import task" above the @task line.

from fabric.api import task

@task
def hello():
    print("Hello world!")

This doesn't work either

(env) air:fabric-test bobgus$ fab hello
...
ModuleNotFound: No module named 'fabric.api'

I have seen that error before too. It means I am trying to execute Fabric <2 code with Fabric 2+ library.

I can change the import statement to: "from fabric import task"

But then I get another series of errors:

(env) air:fabric-test bobgus$ fab hello
...
IndexError: pop from empty list
During handling of the above exception, another exception occurred:
...
TypeError: Tasks must have an initial Context argument!

----------------------------------------------------------------------------------

Looking with Google, I found this stackoverflow comment:

https://stackoverflow.com/questions/52822922/fabric-python-3-what-is-context-and-what-does-it-have-to-contain-and-why-do

It suggests using a dummy argument to fool Fabric into doing the right thing...

So, I change my code to:

from fabric import task

@task
def hello(dummy):
    print("Hello world!")

And, presto, I get a 'correct' output:

(env) air:fabric-test bobgus$ fab hello
Hello world!

Even though I get the correct answer, I don't think this is really the correct approach.

I haven't put in any Context definition, I just put in some characters that don't refer to anything else (unless the word 'dummy' is a reserved word in the Fabric/Python world).

I think the discussion has moved from incomplete documentation to buggy code.

Sincerely, Bob Gustafson

On 11/23/18 4:46 PM, Nick Timkovich wrote:

Yeah, the >>> implies you're at the Python REPL, and the page I linked you to is a few pages into learning to use Fabric; so it's going to assume you installed the library. If you already know Tcl/Expect, I'm sure you can bang out a script to do your task at hand, and if you need it now and don't have time to pick up Python and Fabric on top of it, that sounds like your best option. 

When you get some spare time, there are a bunch of great tutorials that can help you with the syntax (which may be easy), and also how Python is loading/running scripts/packages/modules (which is usually less obvious), which Fabric then uses to find a 'fabfile' package. I haven't heard of Tidelift, but yeah, I feel your pain that a lot of training sites which were once free are now pay-only (or very expertly hide the free options, *coughcodecademycough*). Fortunately, the gigantic popularity of Python means new, free content is always being created, so some searching may help (I'm hesitant to cite anything in particular because I haven't looked at any recently)

I was curious what examples/documentation you were following along with originally, however, because it seemed incomplete, even for the 3-4 lines that you had. It would be nice to correct it so others aren't misled in the future.

On Fri, Nov 23, 2018 at 11:39 AM Bob Gustafson <address@hidden> wrote:

Hi Nick

Yes, 'incomplete' is a charitable description.

I have zit knowledge of python. Perhaps this has been accidental good luck. My favorite languages over the decades have been Fortransit, Pascal, Ruby, and lately Nim.

Using your linked example ('Getting Started'..), starting with the first code line:

   >>> from fabric import Connection

There seems to be a lot that is assumed to have been done by the reader. The >>> probably means that the reader has already typed 'python' to jump into an interactive Python session, but this is not mentioned.

Also, just typing 'python' is not sufficient, as the following lines may not work under Python 2.7 or Python 3.7. Which Python is invoked depends on what has been typed before.

Also, 'fabric' is used in the code line. This library must have been loaded or the line will not work. Again, loading fabric beforehand is not mentioned. Also 'fabric' comes in several versions <2, 2, and a version not recommended - 3.

I see from the libraries, and my experience with Sage, that lots of good things have been done with Python. However, the state of python documentation available on the internet leaves a lot unsaid. Perhaps it is all a game to get new users to buy a subscription to Tidelift.

At this point, for me, coding up 'expect' and fabric in Nim seems easier than continuing this frustration.

Best regards

Bob G

On 11/22/18 9:04 PM, Nick Timkovich wrote:
For the `fab` tool to pick up functions in your fabfile module/package as tasks, you need to decorate them with address@hidden decorator (specifically fabric.task). See http://docs.fabfile.org/en/2.4/getting-started.html#addendum-the-fab-command-line-tool  What documentation are you following? It sounds fairly incomplete and could be fixed.

Nick

On Thu, Nov 22, 2018 at 2:06 PM Bob Gustafson <address@hidden> wrote:
I am running on a Mac air with Mojave os 10.14.1.

I installed python with homebrew

Also using virtualenv and the folder fabric-test

cd fabric-test

air:fabric-test bobgus$ ls
env     fabfile.py

air:fabric-test bobgus$ source env/bin/activate

(env) air:fabric-test bobgus$ which python
/Users/bobgus/fabric-test/env/bin/python

(env) air:fabric-test bobgus$ python -V
Python 3.7.0

(env) air:fabric-test bobgus$ fab -V
Fabric 2.4.0
Paramiko 2.4.2
Invoke 1.2.0

(env) air:fabric-test bobgus$ cat fabfile.py
#!/usr/bin/env python

def welcome():
   print("Welcome to fabric running on python 3")

def uptime():
   run("uptime")fab

--------------------- testing -------

(env) air:fabric-test bobgus$ fab uptime
No idea what 'uptime' is!

(env air:fabric-test bobgus$ fab welcome
No idea what 'welcome' is!

--------------------------------------

I must be doing something wrong here..


_______________________________________________
Fab-user mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/fab-user

reply via email to

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