Fabric hello world

I’m using Xubuntu 12.04.03 32bit
What is Fabric?

In this tutorial I’m using fabric to:
– make whoami command to every user
– method that puts file/folder to user
– method that gets file/folder from user

I have done 4 users and created public key authentication to them. You can see how I have done it from this tutorial:
Public key authentication over SSH

Install Fabric from apt

$ sudo apt-get update && sudo apt-get -y install fabric

Or you can get it from pip.

$ sudo apt-get update && sudo apt-get -y install python-pip
$ sudo pip install fabric

Whoami

Create fabric folder and fabfile.py

$ mkdir fabric
$ cd fabric/
$ nano fabfile.py

Create fabfile what runs command whoami to every user over ssh.

from fabric.api import env, run

env.hosts=["[email protected]", "[email protected]",
                "[email protected]", "[email protected]"]

#env.password="password"
env.parallel=True

def whoami_check():
    run("whoami")

With fab -l you can see all commands

$ fab -l

Available commands:
    whoami_check

Run whoami_check with fabric.

$ fab whoami_check

[[email protected]] Executing task 'whoami_check'
...
[[email protected]] run: whoami
...
[[email protected]] out: soivite01
[[email protected]] out: soivite02
[[email protected]] out: soivite03
[[email protected]] out: soivite04
Done.

Using time before the command you can see how much time is used to run fabric command.

$ time fab whoami_check

...
real	0m2.234s
user	0m0.692s
sys	0m0.060s

Put and get files

Create example file that you can put to users and get that same file from users.

$ nano sendFile.txt

Hello World

Modify fabfile. Add file_put and file_get methods.

$ nano fabfile.py

Add put and get to imports

from fabric.api import env, run, put, get

If you don’t want to give every import by hand you can use this too

from fabric.api import *

Add file_put and file_get methods to end of the class.

...

def file_put():
    put("sendFile.txt")

def file_get():
    get("sendFile.txt")

Run file_put so your adding files to users.

$ fab file_put

[[email protected]] Executing task 'file_put'
[[email protected]] put: sendFile.txt -> /home/soivite01/sendFile.txt
...
Done.
Disconnecting from [email protected] done.
...

Run file_get to get same files what you have added.

$ fab file_get

[[email protected]] Executing task 'file_get'
[[email protected]] download: /home/xubuntu/fabric/[email protected]ost/sendFile.txt <- /home/soivite01/sendFile.txt
...
Done.
Disconnecting from [email protected] done.
...

Your fabric folder now looks like this.

fabric/
├── fabfile.py
├── fabfile.pyc
├── sendFile.txt
├── [email protected]
│   └── sendFile.txt
├── [email protected]
│   └── sendFile.txt
├── [email protected]
│   └── sendFile.txt
└── [email protected]
    └── sendFile.txt

Put and get folders

Create folder and couple files in there

$ mkdir testFolder && touch testFolder/file1 testFolder/file2

Modify fabfile

$ nano fabfile.py

Create method that puts folder to users and gets it.

...

def folder():
    put("testFolder")
    get("testFolder")

Run command

$ fab folder

[[email protected]] Executing task 'folder'
[[email protected]] put: testFolder/file2 -> /home/soivite01/testFolder/file2
[[email protected]] put: testFolder/file1 -> /home/soivite01/testFolder/file1
[[email protected]] download: /home/xubuntu/fabric/[email protected]/testFolder/file1 <- /home/soivite01/testFolder/file1
[[email protected]] download: /home/xubuntu/fabric/[email protected]/testFolder/file2 <- /home/soivite01/testFolder/file2
...

Your folder tree looks like this.

fabric/
├── fabfile.py
├── fabfile.pyc
├── sendFile.txt
├── [email protected]
│   ├── sendFile.txt
│   └── testFolder
│       ├── file1
│       └── file2
├── [email protected]
│   ├── sendFile.txt
│   └── testFolder
│       ├── file1
│       └── file2
├── [email protected]
│   ├── sendFile.txt
│   └── testFolder
│       ├── file1
│       └── file2
├── [email protected]
│   ├── sendFile.txt
│   └── testFolder
│       ├── file1
│       └── file2
└── testFolder
    ├── file1
    └── file2

Your fabfile.py looks like this

from fabric.api import env, run, put, get

env.hosts=["[email protected]", "[email protected]",
                "[email protected]", "[email protected]"]

#env.password="password"
env.parallel=True

def whoami_check():
    run("whoami")

def file_put():
    put("sendFile.txt")

def file_get():
    get("sendFile.txt")

def folder():
    put("testFolder")
    get("testFolder")

Next tutorial you learn how to use sudo:
Using sudo in Fabric

Source:
Fabric tutorial 1 – Take command of your network

This post is part of course