Changes

Jump to: navigation, search

OPS435 Python3 Lab 8

1,968 bytes added, 14:23, 10 November 2019
PART 1: Simplest example
: We will start with some basics. Fabric runs python programs on the controller and the workers. You create an "instruction" file on your controller, and execute it on the controller using the '''fab''' program. When you do that - you specify which workers you want your instructions to be executed on.
: The instructions are stored in a python file. Let's start with a simple one named '''fabfile.py''' (the default filename for used by fabwithout the '-f' optino):
== PART 1: Simplest example ==
: <source lang="python">from fabric.api import *
 
# set the name of the user on the remote host
env.name = '[seneca_id]'
# Will get the hostname of this worker:
 
def getHostname():
name = run("hostname")
print(name)
</source>
: To check for syntax error, run the following command in the same directory as your fabfile.py:<source lang="bash">
fab -l
</source>
: you should get a list of tasks stored in your fabfile.py:<source lang="bash">
[rchan@centos7 lab8]$ fab -f fabfile.py -l
Available commands:
getHostname</source>:All this will do is get To perform the hostname task of getHostname on the worker and print it (on the controller)machine 192.168.122. We 169, we run it on the controller machine like this:<source lang="bash">[rchan@centos7 lab8]$ fab -f fabfile.py -H 192.168.122.169. getHostname[192.168.122.169] Executing task 'getHostname'[192.168.122.169] run: hostname[192.168.122.169] out: c7-rchan[192.168.122.169] out:
: <source lang="bash">fab c7--fabfile=fabfile.py -H 192.168.56.11 getHostname</source>rchan
Done.Disconnecting from 192.168.122.169... done.:All this has done is get the hostname of the worker and print it (on the controller).: In the command above we're using the fab program to read import the file fabfile.py and execute the getHostname function on the worker 192.168.56122.11169. Note that the IP address of your first worker will likely be different.
: If you did all the setup right and you try to execute the command above - you will get a password prompt. Read when execute the above command, read the prompt carefully and see who's password it prompts you. If it is not the same as your [seneca_id], try to fix it by adding verify that have the following line in your fabfile and you can ssh to before the first '''def''' line your fab fileworker vm without password:
:<source lang="python">env.user = '[seneca_id]'</source>
 
:That should have worked, and you'd get output like this:
 
<pre>$ fab --fabfile=fabfile.py -H 192.168.56.11 getHostname
[192.168.56.11] Executing task 'getHostname'
[192.168.56.11] run: hostname
[192.168.56.11] out: www
[192.168.56.11] out:
 
worker1
 
Done.
Disconnecting from 192.168.56.11... done.
</pre>
: In the above you have:
:You should get used to the above. It's a lot of output but it's important to understand where every part is coming from, so you are able to debug problems when they happen.
== Part 2: Set up web server more administrative tasks==
:Let's pretend that we needed to deploy a web server on several machines. We'll set up a simple example of such a deployment here.
=== Install Apache Getting the disk usage on remote worker ===
:Add a setupWebServergetDiskUsage() function to your python fabfile.py file:
:<source lang="python"># Will set up a working web server with a pre-built websiteto get the disk usage on remote workerdef setupWebServergetDiskUsage(): current_time = run("hostnamectl set-hostname www"'date') diskusage = run("yum install httpd"'df -H') runheader = 'Current Disk Usage at '+current_time print("systemctl enable httpd"header) runprint("systemctl start httpd"diskusage)
</source>
:Note that each call to "run()" will run a command on the worker. In this function we set get the hostname date/time of the machine to "www"remote work, install Apache, enable and then get the Apache service, and start that service nowdisk usage. Pretty simple commandsThe print() function print out both the values returned.
:If you try to run it the same way as before:
<pre>$ fab --fabfile=fabfile.py -H 192.168.56122.11 setupWebServer169 getDiskUsage</pre> :You should get the following output:<source lang="bash">[rchan@centos7 lab8]$ fab --fabfile=fabfile.py -H 192.168.122.169 getDiskUsage[192.168.122.169] Executing task 'getDiskUsage'[192.168.122.169] run: date[192.168.122.169] out: Sun Nov 10 13:17:16 EST 2019[192.168.122.169] out:  [192.168.122.169] run: df -H[192.168.122.169] out: Filesystem Size Used Avail Use% Mounted on[192.168.122.169] out: devtmpfs 947M 0 947M 0% /dev[192.168.122.169] out: tmpfs 964M 0 964M 0% /dev/shm[192.168.122.169] out: tmpfs 964M 9.7M 954M 2% /run[192.168.122.169] out: tmpfs 964M 0 964M 0% /sys/fs/cgroup[192.168.122.169] out: /dev/mapper/centos-root 7.7G 5.6G 2.1G 73% /[192.168.122.169] out: /dev/vda1 1.1G 298M 766M 29% /boot[192.168.122.169] out: tmpfs 193M 17k 193M 1% /run/user/42[192.168.122.169] out: tmpfs 193M 0 193M 0% /run/user/1000[192.168.122.169] out:  Current Disk Usage at Sun Nov 10 13:17:16 EST 2019Filesystem Size Used Avail Use% Mounted ondevtmpfs 947M 0 947M 0% /devtmpfs 964M 0 964M 0% /dev/shmtmpfs 964M 9.7M 954M 2% /runtmpfs 964M 0 964M 0% /sys/fs/cgroup/dev/mapper/centos-root 7.7G 5.6G 2.1G 73% //dev/vda1 1.1G 298M 766M 29% /boottmpfs 193M 17k 193M 1% /run/user/42tmpfs 193M 0 193M 0% /run/user/1000 Done.Disconnecting from 192.168.122.169... done.</source>
:You'll find that yum prompts you to answer questions, which you don't want to do in an automated environment. And also yum prints too much output, which also isn't helpful in an automated environment. We'll fix it by adding two switches to yum: "-y" and "-d1":
:Notice also that all of the four commands can be run as many times as you want, the result will be the same. This is not always so easy.
:At this point if you log in to worker1 - you should see a new hostname, and httpd installed and running (try with "systemctl status").
 === Deploy a website Update all the rpm packages on remote worker ===
:Now that we have a web server running, we also want to put a website on it. The website can be of any complexity, but to keep this demonstration simple we'll have a single HTML file. You can pretend that it's as complex as you like. Create an '''index.html''' file like this:
1,760
edits

Navigation menu