Install Fabric on Raspberry Pi – Raspbian Jessie and Stretch

Install Fabric on Raspberry Pi – Raspbian Jessie and Stretch

This manual is for fabric and not for fabric2 or fabric3!

You have set up a Raspbian Pi cluster, but you don’t know how to manage it easily.
We use Fabric, so we can send commands very fast and easy to all computers in the cluster.

With this manual I assume that you have noted down all IP addresses of the Raspberries in the cluster.

We have a fresh Raspbian installation.

IP addresses given in this manual must of course be exchanged by you for your IP addresses.

My IP addresses are :

# Cluster
192.168.188.45 # Host 1 / User: pi
192.168.188.47 # Host 2 / User: pi
192.168.188.40 # Host 3 / User: pi
192.168.188.24 # Host 4 / User: pi

In total we have 5 Raspberry Pi, one of which takes control.
At this, is also connected a mouse, keyboard and a monitor.
All computers in the cluster have the same name (pi) and password.
Here we will also install Fabric.

First we update our system with the following command :

sudo apt-get update -y 
sudo apt-get upgrade -y

 

Now we still need some libraries, which we download from the net with the following command :

sudo apt-get install build-essential \
    tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev \
    libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev \ 
    libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y \

Now we install Fabric with the following command :

sudo apt-get install fabric

Fabric is now installed and we can create a command file named „fabfile.py“.
We enter the following commands :

# change to home directory
cd

# This command when we have loaded the Graphical User Interface
sudo leafpad fabfile.py

# Or this command if we have not loaded the Graphical User Interface
sudo nano fabfile.py

We fill the file with the following commands :
(This is just an example, how you do this later to make your work more efficient is up to you.)

# Import Fabric's API module
from fabric.api import *
from fabric.colors import green

# My User Name and my IP adresses
# You're gonna have to replace that with your own.
env.hosts = [
	'pi@192.168.188.45', 
	'pi@192.168.188.47', 
	'pi@192.168.188.40', 
	'pi@192.168.188.24',
	]

# All Raspberrys have the same password 
# Here too, replace it with your password
env.password = "yourPassword"
 
@parallel
def check():
    # check host type
    host_type()
 
    # Check diskspace
    diskspace()

@parallel
def cmd(command):
	sudo(command)
	
@parallel
def allshutdown():
	sudo("shutdown -t 1") #shutdown in 1 min

@parallel
def allreboot():
	sudo("shutdown -r -t 1") #reboot in 1 min

@parallel
def allcancel():
        sudo("shutdown -c") #cancel the last shutdown command

@parallel
def alldate():
        sudo("date +\"Date : %d/%m/%Y Time : %H.%M.%S\"") #show Time and Date

# You can customize the file according to your needs.
# If you're not so good with Pyhton yet, learn it, 
# it's a wonderful programming language.

Now you can send your commands from the terminal to all the computers you specified above, for example :

fab cmd:"dir"

Or this to shut down all the computers in 1 min.

fab allshutdown

Or this to cancel shutdown.

fab cmd:"shutdown -c"

You can find more information at :

http://docs.fabfile.org/en/1.14/tutorial.html

So that’s it, I hope I could help.
Write your own commands and your life will be easier.

Das könnte dich auch interessieren …

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.