Changes

Jump to: navigation, search

OPS435 Python Lab 8

3,495 bytes added, 13:01, 30 December 2017
INVESTIGATION 2: Fabric practice
The instructions are stored in a python file. Let's start with a simple one named '''fabfile.py''' (the default filename for fab):
 
== PART 1: Simplest example ==
<source lang="python">from fabric.api import *
* Output from the worker ("out: ...")
* Output on the controller from your fab file ("worker1" which came from the "print()" call)
 
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 ==
 
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 ===
 
Add a setupWebServer() function to your python file:
 
<source lang="python"># Will set up a working web server with a pre-built website
def setupWebServer():
run("hostnamectl set-hostname www")
run("yum install httpd")
run("systemctl enable httpd")
run("systemctl start httpd")
</source>
 
Note that each call to "run()" will run a command on the worker. In this function we set the hostname of the machine to "www", install Apache, enable the Apache service, and start that service now. Pretty simple commands.
 
If you try to run it the same way as before:
 
<pre>$ fab --fabfile=fabfile.py -H 192.168.56.11 setupWebServer</pre>
 
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 ===
 
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:
 
<source lang="html"><h1>My fancy web server</h1></source>
 
And since we're pretending that it's a large website with many files and directories, we'll compress it into an archive named '''webcontents.tar.bz2''' using a tar command. You've done this since OPS235.
 
Once you have your archive, make sure it's in the same directory as your fab file. Then add the following to your setupWebServer() function:
 
<source lang="python">
with cd("/var/www/html/"):
put("webcontents.tar.bz2", ".")
run("tar xvf webcontents.tar.bz2")
run("rm webcontents.tar.bz2")
</source>
 
There is something weird in the code above that you haven't seen before but it's required for some uses of Fabric: the '''with''' statement.
 
The problem is that separate '''run''' commands execute in a brand new session, each with its own shell. They are not like separate lines in a single shell script even though they look like they should be.
 
That means if you run a cd command and then a tar command separately - the tar command will not run in the directory where you think it will. In order to fix this you have to nest commands inside a '''with''' - it's like a '''run''' but with persistant results.
 
The code we added to the function will cd to the default web site directory on the worker, upload your web contents tarball from your controller to that directory on the worker, extract it, and delete the tarball.
 
After it's done - you should have a working web server and simple website on your worker1.
 
Except you won't be able to access it because of the firewall. We'll deal with that in the next section.
= LAB 7 SIGN-OFF (SHOW INSTRUCTOR) =

Navigation menu