Open main menu

CDOT Wiki β

Changes

OPS445 Online Lab8

77 bytes added, 15:06, 18 July 2022
m
Part 2: Create remote task for updating rpm packages
tmpfs 177960 0 177960 0% /run/user/1002
</source>
:#Logout from your VM and get back to matrix.:#The previous SSH command when executed successfully, created a login shell on the remote machine. If the previous SSH command is followed by a specific bash command, it will be executed on the remote host instead of creating a login shell. Consider the following:<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca 'hostname;id;df'
student@myvmlab.senecacollege.ca's password:
tmpfs 177960 0 177960 0% /run/user/1002
</source>
:#The three shell commands: hostname, id, and df were executed sequentially. Compare the outputs above with the previous results when executing the corresponding commands in the login shell.:#Please note that your VM was configured to asked ask for the user's password for every SSH connection attemp. We are going to change that behaviour next.
==PART 3: Set up SSH login with public key authentication ==
: In order for you to run multiple tasks on multiple remote machines without typing the in the password for each connection, you need to configure your VM to accept SSH public key authentication in addtion to password authentication. You've done this in both OPS235 OPS245 and OPS335OPS345, and here is a summary of how to do it between your account on matrix and your VM:
: # Create a new SSH key pair (one private, and one public) under your account on matrix.senecacollege.ca. : # Once you have both keys, you can use the '''ssh-copy-id''' command to copy your public key to the student account on your VM, replace the port number with the correct value for your VM:<source lang='bash'>
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 7200 student@myvmlab.senecacollege.ca
</source>
: # The above command should add the contents of your pub key to ~/.ssh/authorized_keys under your student account on your VM. ['''Note: ''' If you want to setup another controller workstation, you can either copy to the '''private key''' to it, or generate another SSH key pair, and copy the '''public key''' to the VM.]: # Verify and confirm that your account on matrix can SSH to your VM as 'student' without being prompted for a password:<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca
Last login: Fri Jul 3 12:46:19 2020 from mtrx-node05pd.dcm.senecacollege.ca
[raymond.chan@mtrx-node05pd lab8]$
</source>
: # If you got similar result as shown above, you have successfully configured your controller workstation and your VM to use public key authentication.
=INVESTIGATION 2 - : Running the fab command in ad-hoc mode =
: The fab command relies on SSH to make the connection to the remote machine before executing the intended commands. The fab command can run in ad-hoc mode:<source lang='bash'>
fab [options] -- [shell commands]
[raymond.chan@mtrx-node05pd lab8]$
</source>
: Note that there is no password prompting if you complete part "Part 3 " successfully, otherwise, the SSH server daemon on your VM will prompt you for a password. The output from the fab's ad-hoc mode is not much different from the SSH command with shell command attached at the end, however, please note that the additional information on the output from the fab command can be very useful for record keeping purpose - what has been done and whether the commands had been carried out successfully or not.
== PART 2: running privileged commands on remote machines ==
: <font color='red'><b>**WARNING** Run Runnig privileged commands incorrectly with sudo may cause irreparable damage to your remote machine.</b></font>
: We say that running an ad-hoc fab command is very similar to the SSH command with shell commands attached at the end. Let's try both with privileged commands, like the "yum" command.
=== Run the "yum" command on remote machine with SSH ===
: # By default, your VM doesn't have the "tree" rpm package installed. You can verify this with the following SSH command (remember to replace the port number with the correct value for your VM):<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca "yum list tree"
Loaded plugins: fastestmirror
</source>
: Please note that the tree package is "Available", but not yet installed.
: # Let't s try to install the "tree" package with the shell command "yum install tree -y":<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ ssh -p student@myvmlab.senecacollege.ca "yum install tree -y"
Loaded plugins: fastestmirror
You need to be root to perform this command.
</source>
: # Using the "yum" command to query rpm package doesn't need special privilege, however, it does when you try to install or remove rpm packages. : # Your "student" account on your VM was configured to allow you to run the "sudo" command to perform software management using the "yum" command. LetSo, let's login to your VM and try the following "sudo" command to install and then remove the "tree" rpm package:<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca
Last login: Fri Jul 3 16:51:07 2020 from mtrx-node05pd.dcm.senecacollege.ca
[student@centos7 ~]$
</source>
: # Please note that when you run the "sudo" command the first time, it asks you for the user's password (i.e. user student's password). Let's now remove the "tree" package:<source lang='bash'>
[student@centos7 ~]$ yum remove tree -y
Loaded plugins: fastestmirror
[student@centos7 ~]$
</source>
: # The above tests confirmed that the student user is allowed to execute the sudo command to run the yum command to install and remove rpm packages. Now , let's logout from the VM and go back to matrix. On matrix, try to run the sudo command using SSH:<source lang='bash'>
[student@centos7 ~]$ exit
logout
[raymond.chan@mtrx-node05pd lab8]$
</source>
: # The above error indicated that you need a tty for the SSH session to prompt you for the sudo password. Please look up the ssh man page to find out the option which turn on a tty for the SSH session.
=== Run the privileged yum command on remote machine using ad-hoc fab command ===
: # Let's try the corresponding ad-hoc fab command on your VM:<source lang='bash'>
fab --host=myvmlab.senecacollege.ca --port=7200 --user=student -- 'sudo yum install tree -y'
</source>
: # Type in your user student's password when prompted for "sudo password", the '''yum install''' command should be executed successfully and install the '''tree''' rpm package. If the tree rpm package is already installed, you can remove it with the following ad-hoc fab command: <source lang='bash'>
fab --host=myvmlab.senecacollege.ca --port=7200 --user=student -- 'sudo yum remove tree -y'
</source>
= INVESTIGATION 3: Running the fab command in script mode =
: From investigation Investigation 2, we can see that running '''fab''' in ad-hoc mode is quick, straight forwardstraightforward, and easy. However, the rich output generated can not be easily captured and processed. If you have a need to capture and process the output generated by the commands executed on the remote machines, the solution is to run the '''fab''' command in script mode.
: The first step in running the '''fab''' command in script mode is to create a fabric script file.
: Let's start with a simple fabric script file to demonstrate some basic concepts that use the API from the Fabric python library.
: On matrix, cd to your lab8 directory under ops445 and create a simple fabric script file named '''fabfile.py''' (this is the default filename used by the fab command when you invoke it without the '-f' optinooption):
== PART 1: Non-privileged task example ==
===Create non-privileged tasks: Getting the hostname of remote machines===
: # Add the following contents to the default fabric script called "fabfile.py" in your lab8 directory:<source lang="python">
from fabric.api import *
# set Set the name of the user login to the remote host
env.user = 'student'
</source>
: # To check for syntax error in the fabric script, run the following command in the lab8 directory where it contains the fabric script named "fabfile.py":<source lang="bash">
fab -l
</source>
: you # You should get a list of tasks defined in your fabfile.py:<source lang="bash">[rchanraymond.chan@centos7 mtrx-node05pd lab8]$ fab -l
Available commands:
getHostname
</source>
: # To perform the task of getHostname on your VM (replace with the actual port # for connecting to your VM), run the fab command on matrix:<source lang="bash">
[raymond.chan@mtrx-node05pd lab8]$ fab --hosts=myvmlab.senecacollege.ca --port=7200 getHostname
[myvmlab.senecacollege.ca] Executing task 'getHostname'
[raymond.chan@mtrx-node05pd lab8]$
</source>
: #Notice that there is no need to specify the user name at the '''fab''' command line since we defined it in the fabric script file (env.user = 'student'). Also notice that we can capture the host name hostname returned from the "hostname" command and print it out together with an a descriptive text in a line. :#In the above executed '''fab''' command, the fab program imports the fabric script named "fabfile.py" and execute the getHostname function on the VM connect at port 7200 on myvmlab.senecacollege.ca. Note that the port number for your VM will likely has have a different value. : <br> If you did all the setup right and you got a password prompt when execute executing the above command, read the prompt carefully and see who's password it was prompting you for. If it is not for the user student, verify that you have the following line in your fabfile.py and you can ssh to your VM as the user student without password:
:<source lang="python">env.user = 'student'</source>
:* Output generated on the controller workstation from your fab file (the print statement)
:You should get used to the above messages from the '''fab''' command. It's a lot of output but it's important to understand where each piece of information is coming from, so you are able to debug problems when they happen.
== PART 2: Privileged Tasks Examples ==
===Creat Create privileged tasks: install and remove rpm package on remote machines===: # Add the following two new functions to the end of the fabric script "fabfile.py" in your lab8 directory:<source lang='bash'>
def installPackage(pkg='dummy'):
print(status)
def removePackage(pkg=''):
if pkg == '':
cmd = 'yum remove dummy -y'
print(status)
</source>
: # Note that both functions take one function argument in different ways. However, if no function argument is passed when calling the function, both will default to a string value of "dummy". Both functions call the sudo() from the fabric.api to execute the command contained in the "cmd" object on the remote machine via sudo.: # To check for any syntax error in your updated fabric script, run the following command in the same directory as the fabfile.py:<source lang='bash'>
fab -l
</source>
: # You should get a list of tasks defined similar to the following:<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ fab -l
Available commands:
[raymond.chan@mtrx-node05pd lab8]$
</source>
: # If you only need to connect to the same remote machine, you can specify the host and port number in the fabfile.py to save some typing when executing the fab command. Add the following two lines after the env.user line in your fabfile.py:<source lang='bash'>
env.port = '7200' # <-- please replace with the actual value of your VM's port number
env.hosts =['myvmlab.senecacollege.ca']
</source>
: # You can also store the user's password in this file so that it will respond to the "sudo password" prompt for sudo() call. It is not safe to do so as you can configure the sudo module on the remote machine not to ask for sudo password.: # Now you can run the fab command without the "--host" and "--port" optionoptions.: # Run the following two fab commands, note the results and compare their difference:<source lang='bash'>
fab installPackage
fab installPackage:tree
</source>
: # Run the following two fab commands, note the results and compare their difference:<source lang='bash'>
fab removePackage
: The following output had been trimmed, only showing the first few lines:<source lang='bash'>
[myvmlab.senecacollege.ca] Executing task 'updatePackage'
[myvmlab.senecacollege.ca] sudo: yum update -y--skip-broken
[myvmlab.senecacollege.ca] out: sudo password:
[myvmlab.senecacollege.ca] out: Loaded plugins: fastestmirror
</source>
= Optional Lab Exercise: Create a Fabric task called makeUser() =: 1. Study the Fabric API run(), sudo(), local(), and put() and utilize them to create a new Fabric task called makeUser()<br>: 2. The makeUser() task should perform the following on a remote machine to:
:* create a new user called "ops445p" with home directory "/home/ops445p".
:* add the new user to the sudo group called "wheel".
:* from your instructor, get the ssh public key which is posted on the internet BB to your controller workstation.:* add the ssh public key obtained from your instructor to the file named "authorized_keys" in the ~ops445p/.ssh directory on the remote machine. (Note: Make sure that you set the proper ownership and permissions on both the directory ~ops445p/.ssh (700) and the file "~ops445p/.ssh/authorized_keys.(600)):3. Add the makeUser() to your final version of fabfile.py. <br>:4. Run the new task makeUser() on your VM.<br>:5. Verify and confirm that your new makeUser() task is working correctly.
= LAB 8 SIGN-OFF (SHOW INSTRUCTOR) =
: Complete each part of the lab and upload the version of your '''fabfile.py ''' to Blackboard by the due date.
[[Category:OPS445-Python]][[Category:rchan]]
572
edits