Open main menu

CDOT Wiki β

Changes

OSL740 Lab 2

420 bytes added, 18:46, 2 February 2023
INVESTIGATION 4: USING PYTHON TO AUTOMATE MANAGING VIRTUAL MACHINES: - Updating investigation 4 to match the lecture content.
|- valign="top"
|
{{Admon/tip|Python Scripting Tips:|<br>'''<u>String Methods</u>'''<br><ul><li>The string data type has a number of methods that we can use on it to get a modified version of the data a string variable holds.</li><li>Note that these don't change the existing string, just return a copy of it with some change. You can store this updated copy in a variable, or use it in a command.</li><li>Some examples include:<ul><li>find() - locates a value (e.g. another string) if it is in this string.</li><li>lower() - convert the entire string to lower-case</li><li>replace() - finds a value in the string, and replaces it with a different value.</li><li>split() - breaks the string up on a value, and returns a list.</li><li>strip() - removes leading and trailing whitespace</li><li>upper() - convert the entire string to upper-case</li></ul></li><li>There are [https://www.w3schools.com/python/python_ref_string.asp many more options] than this, but you will get a lot of utility out of these.</li></ul><br />'''<u>Methods in General</u>'''<br><ul><li>Python is an object oriented programming language. In this course, that won't actually affect us much, but it does change how you run some commands.</li><li>In bash, every command ran on its own (while you might provide the command some arguments, or pipe different data into it). In python, the modules we import, and the types of variables we use can (will) have commands built into them. For example: converting all letters in a string to UPPER CASE).</li><li>While you could write a command in bash that would do that and pass it a variable holding a series of letters as an argument, in python that behaviour (and more) is built into the string type of variable.</li><li>The actions that are built into types are called '''methods'''. To use a method on a variable just add .methodname() to the end of the variable. Instead of getting back the value, you'll get something else (based on what that method does). Suppose we had a variable called course that currently had the value 'osl740ops245'. Course codes are usually written in ALL-CAPS, so instead of just saying print(course), we could say print(course.upper())</li></ul>}}
|}
&nbsp;&nbsp;exit()<br />
else:<br />
&nbsp;&nbsp;for machine in print('Backing up centos1','centos2','centos3'):<br />&nbsp;&nbsp;os.system('gzip < /var/lib/libvirt/images/centos1.qcow2 > ~YourRegularUsername/backups/centos1.qcow2.gz')<br />&nbsp;&nbsp;print('Backing up centos2' + machine)<br />&nbsp;&nbsp;os.system('gzip < /var/lib/libvirt/images/centos2.qcow2 > ~YourRegularUsername/backups/centos2.qcow2.gz')<br />&nbsp;&nbsp;print('Backing up centos3')<br />&nbsp;&nbsp;os.system('gzip < /var/lib/libvirt/images/' + machine + 'centos3.qcow2 > ~YourRegularUsername/backups/' + machine + 'centos3.qcow2.gz')<br />
</code>
<li>It should print out root, but with an extra new-line. You may have noticed this in your other python scripts so far: the data we get from os.popen() has an extra new-line on the end. We will need to modify the string(s) it gives us a bit. See the side-bar for hints on how to do so.</li>
<li>Modify the if statement so it is just getting the current username, not the username and a newline. You can do this using several steps and several variables, but it can also be done in a single line.</li>
<li>Now that the script recognizes you as being root (or at least running the script with root permissions), it should work. Notice how we've used the + to combine several strings together to pass to the os.system command. We did this because this script needs the python variable to be evaluated before the whole line gets handed over to os.system. If you left the variable names inside the quotes, python will ignore them as just being part of a string. By putting them outside of a string, and concatenating their value to that string, we can evaluate them and feed them into that command.</li>
<li>Test your script to make sure it works. If it doesn't, go back and fix it. '''Do not continue until it successfully makes backups of your VMs'''.</li>
<li>There is a weakness to this script as written. Every time you run it, it will make a backup of all three VMs. But what if you only made a change to one of them? Do we really need to wait through a full backup cycle for two machines that didn't change? As the script is currently written, we do. But we can make it better. We've provided the scripts with some comments below.</li>
&nbsp;&nbsp;print("You must be root")<br />
&nbsp;&nbsp;exit()<br />
else<br />
<br />
&#35;The rest of this script identifies steps with comments 'Step <something>'.<br />
&#35;Step A: Find out if user wants to back up all VMs<br />
&#35;Step B-1:use the existing loop to back up all the VMs<br />
for machine in &nbsp;&nbsp;print('Backing up centos1',)<br />&nbsp;&nbsp;os.system('gzip < /var/lib/libvirt/images/centos1.qcow2 > ~YourRegularUsername/backups/centos1.qcow2.gz')<br />&nbsp;&nbsp;print('Backing up centos2',)<br />&nbsp;&nbsp;os.system('centos3gzip < /var/lib/libvirt/images/centos2.qcow2 > ~YourRegularUsername/backups/centos2.qcow2.gz'):<br />&nbsp;&nbsp;print('Backing up centos3' + machine)<br />&nbsp;&nbsp;os.system('gzip < /var/lib/libvirt/images/' + machine + 'centos3.qcow2 > ~YourRegularUsername/backups/' + machine + 'centos3.qcow2.gz')<br />
&#35;Step B-2: They don't want to back up all VMs, prompt them for which VM they want to back up<br />
&#35;Step C: Prompt the user for the name of the VM they want to back up<br />
&#35;Step C-3: If the user chose Centos3, back up that machine.<br />
</code></li>
<li>Before the for loop block that backs up each machine add a prompt to ask the user if they want to back up all machines. Use an if statement to check if they said yes (See comment 'Step A').<ul><li>if they did say yes, back up all the VMs using your existing for loop block (Comment step B-1).</li><li>If they didn't say yes, do nothing for now(you could even use python's pass statement).</li></ul></li>
<li>Test your script to make sure it works. Check what happens if you say 'yes' to the prompt, and check what happens if you say things other than 'yes'.</li>
<li>Now we have a script that asks the user if they want to back up all VMS, and if they say they do , it does. But if they don't want to back up every VM, it currently does nothing.</li>
<li>Add an else statement at comment Step B-2 to handle the user not wanting to back up every VM. Inside that else clause (Comment step C) ask the user which VM they would like to back up (you can even give them the names of available VMs (Centos1, Centos2, Centos3).</li>
<li>Now nest an if statement inside that else (Comments C-1, C-2, and C-3) so that your script can handle what your user just responded with. If they asked for Centos1, back up Centos1. If they want to back up Centos2, only back up Centos2, etc. Hint: You might want to use elif for this.</li>
<li>Test your script again. You should now have a script that:<ul><li>Makes sure the user is running the script with elevated permissions.</li><li>Asks the user if they want to back up every VM.</li><li>If they want to back up every VM, it backs up every VM.</li><li>If the user does not want to back up every VM, the script asks them which VM they do want to back up.</li><li>If the user selected a single VM, the script will back up that one VM.</li><li>Now you may notice another issue with the script: The gzip lines are almost identical. The only difference in them is the name of the VM file being backed up. In the portion of code where you back up each machine individually (comment steps C-1, C-2, and C-3) try replacing the machine name in the gzip command with a string variable that holds the machine's name instead. Note that you will have to make us of string concatenation for this to work correctly.</li></ul></li>
</ol>
932
edits