Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 8

356 bytes removed, 13:53, 10 November 2019
Part 2: Set up more administrative tasks
== Part 2: Set up more administrative tasks==
:Let's pretend that we needed to deploy a web server need collect the disk usage on several machinesso that we can plan for storage maintenance. We'll set up a simple example of such a deployment here.
=== Getting the disk usage on remote worker ===
</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.
=== Update all the rpm packages on remote worker ===
:Let's pretend that we need to update software packages installed on several machines due to security patches. Let's name the task as 'performSoftwareUpdate()':<source lang="python">
# to perform software update on remote worker
def performSoftwareUpdate():
status = run('yum update -y')
print(status)
</source>
: Do a syntax check with the "fab -l" command.
: When you try to run it the same way as before, you encounter some issue as shown below:<source lang="bash">
[rchan@centos7 lab8]$ fab --fabfile=fabfile.py -H 192.168.122.169 performSoftwareUpdate
[192.168.122.169] Executing task 'performSoftwareUpdate'
[192.168.122.169] run: yum update -y
[192.168.122.169] out: Loaded plugins: fastestmirror, langpacks
[192.168.122.169] out: You need to be root to perform this command.
[192.168.122.169] out:
: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:
Fatal error:<source lang="html"><h1>My fancy web server</h1></source>run() received nonzero return code 1 while executing!
Requested: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.yum update -yExecuted: /bin/bash -l -c "yum update -y"
:Once you have your archive, make sure it's in the same directory as your fab fileAborting. Then add the following to your setupWebServer() function: :<source lang="python"> with cd("/var/www/html/"): put("webcontentsDisconnecting from 192.tar168.bz2", "122.") run("tar xvf webcontents169.tar.bz2") run("rm webcontents.tardone.bz2")
</source>
: As you already know, you need superuser privilege in order to perform software update on a Linux system. There are two ways to do it on Fabric. The first one is simple. Edit you fabfile.py and change the env.user line as shown below:<source lang="python">
: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'env.user = 'root' statement.
:The problem is that separate '''run''' commands each 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.</source>:That means if you run a cd command and then a tar command separately - the tar command will not run in Save the directory where you think it willfabfile. In order to fix this you have to nest commands inside a '''py with''' - the change and run it's like a '''run''' but with persistant resultsagain. :The code we added to the function will cd to If you see the default web site directory on the workerpassword prompt again, upload your web contents tarball make sure that you can ssh from your controller as a regular user to that directory on the your 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 sectionvm as root without password.
== Part 2: Set up the firewall ==
1,760
edits