1,760
edits
Changes
no edit summary
= LAB OBJECTIVES =
:0. Review SSH setup and remote shell execution
:1. Explore the Fabric Python library and its command line tool "fab".
:2. Create Fabric scripts utilize utilizing Fabric's API to define tasks that can be executed by for the '''fab''' programcommand.:3. Use the '''fab''' command to execute fabric script to perform regular/administrative pre-defined tasks on remote Linux machines.
== Overview ==
:[http://www.fabfile.org/]
|}
: <font color='blue'>Please note that the version of Fabric we are going to use installed on matrix.senecacollege.ca for this lab is 1.14 and it supports only Python version 2.</font>The Fabric script files we are going to create in this lab have to meet Python version 2.x requirements. (e.g. print is a keyword, not a built-in function in Python 2.x)
:2. You should have some experience on the following topics in OPS235 and or OPS335. Please review them to prepare for the activities in this lab:
:* create and configure a regular user on a Linux system.
: The Fabric environment consists of the following components:
:# Controller workstation - the machine that has the Fabric package installed and runs the "fab" command
:## the Fabric Python Library - the fabric package (already installed on matrix)
:## the Fabric API - fabric.api
:## the Fabric command - '''fab''': run Fabric script, name of the script is default to fabfile.py in the current working directory unless specified otherwise with the '-f' option.
:## Fabric script: contains fabric environment object value and Python functions (or tasks) to be executed by the '''fab''' command.
:# Remote machine: the target machine on which one or more Fabric tasks will be executed.
:## running the ssh server daemon
:## use public key (or password based) authentication for ssh connection
== PART 1 - Configure and test your controller workstation ==
Last login: Fri Jul 3 11:06:24 2020 from mtrx-node05pd.dcm.senecacollege.ca
</pre>
: Once you are on your VM, try the following commands : hostname, id, and df, and record the results for later comparison with the results of other commands:<source lang='bash'>
[student@centos7 ~]$ hostname
centos7
</source>
:Logout from your VM and get back to matrix.
:The previous SSH command when execute executed successfully, with create 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 from when executing the corresponding commands in the login shell.:Please note that you were asked to provide the user's password for each every SSH connection.
==Part PART 3: Set up SSH login with public key authentication ==: In order for your controller workstation to automate tasks execution on your VM, you will need to be able to connect configure your VM to it using SSH public key authentication instead of using password authentication. You've done this in both OPS235 and OPS335, and here is a summary on 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>
logout
Connection to myvmlab.senecacollege.ca closed.
[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca 'date;hostname;id'
Fri Jul 3 12:55:22 EDT 2020
[raymond.chan@mtrx-node05pd lab8]$
</source>
: If you got similar result as above, you have successfully configure 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]</source>: When running the fab command in ad-hoc mode, it is very similar to running the SSH with commands attached at the end. == PART 3 1: running non- Clone the Workers privileged shell commands on remote machines ==: We're only simulating In the real world where you'd have hundreds of VMs in one or more cloudsfollowing example, but you can just imagine that we use the VMs you're creating ''fab''' to execute the "date", "hostname", and "id" command remotely on our VM. Try the following ad-hoc fab commands and record their results for later use, replace the port number with the correct value for your computer are actually being created on an Amazon or Microsoft CloudVM:<source lang='bash'>[raymond.chan@mtrx-node05pd lab8]$ fab --host=myvmlab.senecacollege.ca --port=7200 --user=student -- 'date;hostname;id'[myvmlab.senecacollege.ca] Executing task '<remainder>'[myvmlab.senecacollege.ca] run: date;hostname;id[myvmlab.senecacollege.ca] out: Fri Jul 3 13:05:39 EDT 2020[myvmlab.senecacollege.ca] out: centos7[myvmlab.senecacollege.ca] out: uid=1002(student) gid=1002(student) groups=1002(student),10(wheel)[myvmlab.senecacollege.ca] out:
Done.Disconnecting from myvmlab.senecacollege.ca: Make snapshots of all 7200... done.[raymond.chan@mtrx-node05pd lab8]$</source>: Note that there is no password prompting if you complete part 3 successfully, otherwise, the SSH server daemon on your workers so 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 you the additional information on the output from the fab command can easily restore them to be very useful for record keeping purpose - what has been done and whether the original state after you modify themcommands had been carried out successfully or not.
= INVESTIGATION = PART 2: Fabric practice running privileged commands on remote machines ==: We will start say that running an ad-hoc fab command is very similar to the SSH command with some basicsshell commands attached at the end. Fabric runs python programs on the controller and Let's try both with privileged commands, like the workers. You create an "instructionyum" file on your controller, and execute it on the controller using the '''fab''' program. When you do that - you specify which workers you want your instructions to be executed oncommand.
</source>
: To check for syntax errorPlease note that the tree package is "Available", run but not yet installed.: Let't try to install the "tree" package with the following shell command in the same directory as your fabfile.py"yum install tree -y":<source lang="'bash"'>fab [raymond.chan@mtrx-node05pd lab8]$ ssh -p student@myvmlab.senecacollege.ca "yum install tree -ly"Loaded plugins: fastestmirrorYou 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 should get a list of tasks stored in try to install or remove rpm packages. : Your "student" account on your fabfileVM was configured to allow you to run the "sudo" command to perform software management using the "yum" command.pyLet's login to your VM and try the following "sudo" command to install and then remove the "tree" rpm package:<source lang="'bash"'>[rchanraymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.caLast login: Fri Jul 3 16:51:07 2020 from mtrx-node05pd.dcm.senecacollege.ca[student@centos7 lab8~]$ fab sudo yum install tree -f fabfiley[sudo] password for student:Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: less.cogeco.net * extras: centos.mirror.colo-serv.net * updates: mirror.py calgah.comResolving Dependencies--l> Running transaction checkAvailable commands---> Package tree.x86_64 0:1.6.0-10.el7 will be installed--> Finished Dependency Resolution
Total download size: If you did all the setup right and you get a password prompt when execute the above command, read the prompt carefully and see who's password it prompted you for46 kInstalled size: 87 kDownloading packages:tree-1. If it is not the same as your [seneca_id], verify that you have the following line in your fabfile and you can ssh to your worker vm without password6.0-10.el7.x86_64.rpm | 46 kB 00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing :tree-1.6.0-10.el7.x86_64 1/1 Verifying : tree-1.6.0-10.el7.x86_64 1/1
Installed:<source lang="python">env tree.user = '[seneca_id]'</source>x86_64 0:1.6.0-10.el7
== Part 2: Set up more administrative tasks====================================================================================================================== Package Arch Version Repository Size========================================================================================================================Removing: tree x86_64 1.6.0-10.el7 @base 87 k
Removed:Add a getDiskUsage() function to your fabfile tree.py filex86_64 0:1.6.0-10.el7
</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 ===:Note that each call to 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 "run()sudo password" will run a , the yum install command on to install the workertree rpm package should be executed successfully. In this function we get the date/time of If the remote worktree rpm package is already installed, and then get you can remove it with the disk usagefollowing ad-hoc fab command: <source lang='bash'>fab --host=myvmlab. The print() function print out both senecacollege.ca --port=7200 --user=student -- 'sudo yum remove tree -y'</source>: Try remove the values returned"tree" rpm package with the appropriate ad-hoc fab command.
= INVESTIGATION 3:Running the fab command in script mode =: From investigation 2, we can see that running '''fab''' in ad-hoc mode is quick, straight forward, and easy. However, the rich output generated can not be easily captured and processed. If you try 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 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 same way as before'-f' optino):
</source>
: you should get a list of tasks defined in your fabfile.py:<source lang="bash">
[rchan@centos7 lab8]$ fab -l
Available commands:
</source>
: Do a syntax check To perform the task of getHostname on your VM (replace with the "fab -l" command.: When you try actual port # for connecting to your VM), run it the same way as before, you encounter some issue as shown belowfab command on matrix:<source lang="bash">[rchanraymond.chan@centos7 mtrx-node05pd lab8]$ fab --fabfilehosts=fabfilemyvmlab.py senecacollege.ca -H 192.168.122.169 performSoftwareUpdate-port=7200 getHostname[192myvmlab.168senecacollege.122.169ca] Executing task 'performSoftwareUpdategetHostname'[192.168myvmlab.122senecacollege.169ca] run: yum update -yhostname[192myvmlab.168senecacollege.122.169ca] out: Loaded plugins: fastestmirror, langpacks[192.168.122.169] out: You need to be root to perform this command.centos7[192.168myvmlab.122senecacollege.169ca] out:
The host name is: centos7
</source>
: As you already know, you Notice that there is no need superuser privilege in order to perform software update on a Linux system. There are two ways to do specify the user name at the '''fab''' command line since we defined it on Fabric. The first one is simple. Edit you fabfile.py and change in the fabric script file (env.user line as shown below:<source lang='student'). Also notice that we can capture the host name returned from the "pythonhostname">command and print it out together with an descriptive text in a line.
: Recall that in our OPS courses we've been using iptables instead of firewalld, which is installed by default in CentOS. Let's make sure that our workers have that set up as well. In the same above output from the '''fabfile.pyfab''' command, you have::* Lines with the FQDN of the remote machine you've been using all alongare working on.:* Messages from the controller workstation (e.g. "Executing task...", add a new function like thisand "run:...").:* Output from the remote machine ("out: ..."):* Output generated on the controller workstation from your fab file (the print statement)
: <source lang="python"># Will uninstall firewalld and replace You should get used to the above messages from the '''fab''' command. It's a lot of output but it with iptablesdef setupFirewall(): run("yum -y -d1 remove firewalld") run("yum -y -d1 install iptables-services") run("systemctl enable iptables") run("systemctl start iptables")</source>'s important to understand where every part is coming from, so you are able to debug problems when they happen.
== PART 2: That should by now look pretty obvious. On Privileged Tasks Examples =====Creat privileged tasks: install and remove rpm package on remote machines===: Add the worker you're going following two new functions to uninstall firewalld, install iptables, and make sure that the iptables service is runningend of the fabric script "fabfile.py" in your lab8 directory:<source lang='bash'>
def installPackage(pkg='dummy'): Execute the function for worker1 and double-check that it worked.: <font color cmd ='redyum install '>+ pkg + '-y''**Warning**''' </font>Do not do this on your vm on myvmlab. If you do, you may lock yourself out for good. status = sudo(cmd) print(status)
def removePackage(pkg): if pkg =='': cmd = Check firewall configuration 'yum remove dummy -y' else: cmd ='yum remove ' + pkg + ' -y' status =sudo(cmd) 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 -lAvailable commands:
getHostname installPackage removePackage[raymond.chan@mtrx-node05pd lab8]$</source>: To check your firewall configuration your If you only need to connect to the same remote workermachine, you can retrieve its current configuration by creating another Fabric task called "getFirewallConfigure()specify the host and port number in the fabfile.py to save some typing when executing the fab command. Let's put Add the following code to two lines after the env.user line in your fabfile.py:<source lang="python"'bash'>def getFirewallConfig(): fw_config env.port = run("iptables '7200' # <-L -n -v")please replace with the actual value of your VM's port number print(fw_config)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" option.
: Run the following two fab commands, note the results and compare their difference:<source lang='bash'>
fab installPackage
fab installPackage: Try to run tree</source>: Run the getFirewallConfig() task following two fab commands, note the same way as before.results and compare their difference:<source lang='bash'>: Troubleshoot if you encounter any issue.fab removePackage
== Part 2: After completing Create remote task for updating rpm packages ==: Add a new function called "updatePackage" to your fabfile.py according to the following requirements::* Accept optional function argument as the rpm package name:* If no function argument was given when called, default to all the previous parts packages installed: The output of the lab - you updatePackage when executed, should have produce similar output as shown below::1. Update a working fabfilesingle package:<source lang='bash'>fab updatePackage:tree</source>: Sample output:<source lang='bash'>[raymond.chan@mtrx-node05pd lab8]$ fab updatePackage:tree[myvmlab.senecacollege.ca] Executing task 'updatePackage'[myvmlab.senecacollege.ca] sudo: yum update tree -y[myvmlab.senecacollege.ca] out: sudo password:[myvmlab.senecacollege.ca] out: Loaded plugins: fastestmirror[myvmlab.senecacollege.ca] out: Loading mirror speeds from cached hostfile[myvmlab.senecacollege.ca] out: * base: less.cogeco.net[myvmlab.senecacollege.ca] out: * extras: centos.mirror.ca.planethoster.net[myvmlab.senecacollege.ca] out: * updates: less.cogeco.net[myvmlab.senecacollege.py with three working functionsca] out: getDiskUsage(), performSoftwareUpdate() and getFirewallConfig()No packages marked for update[myvmlab.senecacollege.ca] out:
Done.Disconnecting from myvmlab.senecacollege.ca:7200... done.[raymond.chan@mtrx-node05pd lab8]$</source>:2. Update all installed package:<source lang="'bash"'>fab --fabfileupdatePackage:</source>: The following output had been trimmed, only showing the first few lines:<source lang=fabfile'bash'>[myvmlab.senecacollege.ca] Executing task 'updatePackage'[myvmlab.py senecacollege.ca] sudo: yum update -H 192y[myvmlab.senecacollege.ca] out: sudo password:[myvmlab.senecacollege.ca] out: Loaded plugins: fastestmirror[myvmlab.senecacollege.168ca] out: Loading mirror speeds from cached hostfile[myvmlab.122senecacollege.169,192ca] out: * base: less.cogeco.net[myvmlab.senecacollege.ca] out: * extras: centos.mirror.ca.168planethoster.122net[myvmlab.170,192senecacollege.168ca] out: * updates: less.122cogeco.171,192net.168.122.172 getDiskUsage</source>
Verifying : Again systemd- your IP addresses will be different but the command will be the same219-73.el7_8.5.x86_64 53/54 Verifying : systemd-libs-219-73.el7_8.5.x86_64 54/54
Removed: You can also run all three tasks on all the workers at the same time, by adding any task to your fabfile kernel.py:<source lang="python">def doAllThree(): getDiskUsage() getFirewallConfig() performSoftwareUpdate()</source>: And run the following command on your controllerx86_64 0:3.10.0-862.el7
</source>
= Lab Exercise: Test your updated fabfile.py until you get the same result as when you apply it to your own worker VM. == Create a Fabric task called makeUser() ==: Study the Fabric API run(), sudo(), and put() and utilize them to create a new task called makeUser()
: The makeUser() function should perform the following:
:Add the makeUser() to your final version of fabfile.py.
:Test Run the new task makeUser() on your local VM first, and deploy to your vm on myvmlab.:After the successful deployment of the Verify and confirm that your new makeUser() task on your vm on myvmlab, ask your professor to verify and confirm that the new user account "ops435p" on myvmlab has been created is working correctly.
= LAB 8 SIGN-OFF (SHOW INSTRUCTOR) =
:'''Have Ready to Show Your Instructor:'''* Complete all the parts of the lab and upload the version of your fabfile.py which works on your vm on myvmlab to Blackboardby the due date.
[[Category:OPS435-Python]][[Category:rchan]]