Open main menu

CDOT Wiki β

Changes

OPS245 Scripting Exercises Lab 2 dev

6,357 bytes removed, 21:06, 29 April 2023
no edit summary
= Key Concepts =
== Conditional Logic ==
Condition Logic aka Control Flow statements aka If statements are an important scripting and programming concept. They allow us to make scripts more flexible by building in decision making. You learned how to use these in BASH scripting in ULI101, but lets review the basics. You use an if statement to check whether a condition is met (or not, depending on how you write it) and execute some code (or not) based on that condition. Here is a sample if statement in Bash:
<pre>
#!/bin/bash
# Author: Jason Carman
# Date: January 26, 2023
# Purpose: Check to see if user is root, if they are not inform them they should run the script as root and exit with an error (non-zero) status code
# Usage: ./checkuser.bash
#
 
# Check the output of the whoami command to determine if they are root
if [[ $(whoami) == "root" ]]; then
echo "You are root!"
# Else, they are not root. Display a message and exit with a non-zero status code
else
 
# Display a message indicating the script should be run as root
echo "You are not root. Please run the script with root permissions."
 
# Exit with a non-zero status code indicating an error
exit 1
fi
</pre>
 
=== To do: sample Bash script ===
* Copy the above code and give it execute permission using the chmod command.
* Try running it as a regular user. What happens?
* After running it as a regular user, from the command line type the following and observer the output. <pre>echo $?</pre>
* Now try running the script using sudo. What happens?
* From the command line type the following and observe the output. What is different from the previous output? <pre>echo $?</pre>
 
Now let's try doing the same thing in Python. Here's a sample if statement in Python.
<pre>
# Author: Jason Carman
# Date: January 26, 2023
# Purpose: Check to see if the user is root, if they are not inform them they should run the script as root and exit with an error (non-zero) status code
# Usage: ./checkuser.py
#
 
# Import the Operating System and sys modules
import os, sys
 
# Check if the user is root using an if statement
if os.geteuid() != 0:
 
# Display a message indicating the script should be run as root and exit
sys.exit("You must be root. Please run the script with root permissions")
 
# Else
else:
 
# User is root, print a message
print("You are root!")
</pre>
 
=== To do: sample Python script ===
* Copy the above code and give it execute permission using the chmod command.
* Try running it as a regular user. What happens?
* After running it as a regular user, from the command line type the following and observer the output. <pre>echo $?</pre>
* Now try running the script using sudo. What happens?
* From the command line type the following and observe the output. What is different from the previous output? <pre>echo $?</pre>
 
There is one interesting difference between the two scripts. In BASH you can control what number the script uses to indicate an error. In Python (with sys.exit anyway) you cannot. In Python you can exit indicating success by calling sys.exit() with nothing in the parentheses.
 
= Virtual Machine Backup Script =
== Overview ==
In Lab 2 Investigation 3 you performed a manual backup of all 3 virtual machines. While this is something both important and necessary, it's a tedious process. This is something you could automate through scripting. In this exercise you are going to write a script that will backup all three of your virtual machines. We will improve on this backup script in a later exercise. To begin, open the script backupVM.py (that you imported using git clone in Week 2) in your text editor (vi) on c7host and copy the following template inc7hostl. Be sure to modify Author and Date with the correct information.<pre>#!/usr/bin/env python3# Author: Your Name; youremail@myseneca.ca# Date:# Purpose: Backs up virtual Machines# Usage: ./backupVM.py# # Import the Operating System (os) module, Subprocess module and Sys module  # Store the output of the whoami command as a variable called whoami  # Check the current user (who is executing the script) and store the information in the variable currentuser. The os.geteuid() command requests the current user ID from the operating system. Root has a UID of (0). Users have non-zero values, usually starting at 1000. We can use this to check if the current user is root or not.  # Using an if statement check if currentuser is not equal to 0   # Print a message telling the user who they are. Use , to concatenate (join) the message and the whoami variable   # Exit with an error message indicating they must be root  # Else (they are root, we can continue)   # Prompt the user asking if they wish to back all VMs, accepting options y for yes or n for no. Store the answer in the variable backupAll   # If backupAll equals "n" # Prompt the user asking which VM they wish to backup. Store the answer in the variable backupVM # If backupVM equals "centos1"   # Print "Backing up centos1"   # Use os.system to call the virsh dumpxml command you used for centos1 in Lab 2 Investigation 3   # Use os.system to compress (using gzip) the .qcow2 for centos1, using the same syntax as in Lab 2 Investigation 3   # Else if (elif) backupVM equals "centos2"   # Print "Backing up centos2"   # Use os.system to call the virsh dumpxml command you used for centos2 in Lab 2 Investigation 3   # Use os.system to compress (using gzip) the .qcow2 for centos2, using the same syntax as in Lab 2 Investigation 3   # Else if (elif) backupVM equals "centos3"   # Print "Backing up centos2"   # Use os.system to call the virsh dumpxml command you used for centos3 in Lab 2 Investigation 3   # Use os.system to compress (using gzip) the .qcow2 for centos3, using the same syntax as in Lab 2 Investigation 3   # Else, user has entered an invalid choice # Display a message indicating an invalid choice has been made and give the valid options (centos1, centos2, centos3) and exit   # Else, we're doing a full backup (backing up all 3 virtual machines: centos1, centos2, centos3)   # Print "Backing up centos1"   # Use os.system to call the virsh dumpxml command you used for centos1 in Lab 2 Investigation 3   # Use os.system to compress (using gzip) the .qcow2 for centos1, using the same syntax as in Lab 2 Investigation 3   # Print "Backing up centos2"   # Use os.system to call the virsh dumpxml command you used for centos2 in Lab 2 Investigation 3   # Use os.system to compress (using gzip) the .qcow2 for centos2, using the same syntax as in Lab 2 Investigation 3 # Print "Backing up centos3"   # Use os.system to call the virsh dumpxml command you used for centos3 in Lab 2 Investigation 3   # Use os.system to compress (using gzip) the .qcow2 for centos2, using the same syntax as in Lab 2 Investigation 3   # Exit successfully </pre>
== Filling in the code ==