Changes

Jump to: navigation, search

OPS435 Python3 Lab 8

11,734 bytes added, 19:00, 4 July 2020
no edit summary
= LAB OBJECTIVES =
:0. Review SSH setup and remote shell execution:1. Use Explore the '''Fabric Python library and its command line tool "fab".:2. Create Fabric scripts utilizing Fabric''' program s API to execute administrative define tasks on remote host via Python functions under for the '''Fabricfab''' frameworkcommand.:23. Create python functions using Fabric API Use the '''fab''' command to execute fabric script to perform Linux system administrative pre-defined tasks on controlled remote Linux systemsmachines.
== Overview ==
: Completing this lab will give you Fabric is a taste Python library and command-line tool for streamlining the use of what is involved in automating remote SSH for application deployment or system/network administration tasks. It has two major components::# a command-line interface program called "fab" that lets you execute arbitrary Python functions :# a set of Python APIs that you can use and call in your Python functions to make executing shell commands over SSH much easier. : We will look at and are going use the Fabric API and its '''Fabricfab''' package command to define and execute Python functions (or tasks), to automate interactions with remote Linux machines in this lab. Using Fabric you can automate monitoring, deploying software, and updating many systems at the same time repeatedly.
== REFERENCE ==
| style="border: 2px solid black;" | Category
| style="border: 2px solid black;" | Resource Link
 
|- style="background-color:white;border:none;"
| style="border: 2px solid black;" valign="top"|
:Official '''Fabric''' website
| style="border: 2px solid black;" valign="top"|
:[http://www.fabfile.org/]
|- style="background-color:white;border:none;"
:[https://www.digitalocean.com/community/tutorials/how-to-use-fabric-to-automate-administration-tasks-and-deployments]
|- style="background-color:white;border:none;"
| style="border: 2px solid black;" valign="top"|
:Official '''Fabric''' website
| style="border: 2px solid black;" valign="top"|
:[http://www.fabfile.org/]
|}
: <font color='blue'>Please note that the version of Fabric we are using in installed on matrix.senecacollege.ca for this lab is 1.14 and it supports on 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 learned some experience on the following topics in OPS235 and or OPS335. Please review them to prepare for some of the tasks activities in this lab::* Configure create and allow configure a regular user to run the sudo commandon a Linux system. :* The man page on configure and manage sudoprivilege for a regular user:* Configure sudoers using the configuration file: /etc/sudoers.visudo command:* Managing critical system log files: /var/log/messagesusing the yum command to install, /var/log/maillogremove, /var/log/secureand update rpm packages
:* Retrieve current firewall setting using the iptables -L -n -v command
= INVESTIGATION 1: Extra VM Setup The Fabric Environment =
: In order to experience 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 Fabriccommand - '''fab''s features ': run Fabric script, name of the script is default to fabfile.py in a realistic way, wethe current working directory unless specified otherwise with the '-f're going option.:## Fabric script: contains fabric environment object value and Python functions (or tasks) to set up several virtual machines (You need at least be executed by the '''fab''' command.:# Remote machine: the target machine on which one or more VM)Fabric tasks will be executed. To begin with they are all going to have :## running the same configuration. Please make sure that each VM has direct network ssh server daemon:## use public key (or password based) authentication for ssh connection with other VMs you wish to control and configure.
== PART 1 - Set up Configure and test your controller workstation ==: In this lab you will use your login account on matrix.senecacollege.ca as your Fabric controller workstation.
: In this lab you will use your existing vm The Fabric package version 1.14.0 has already been installed on matrix.senecacollege.ca. You should have access to the '''centos7fab''' as a workstation command on matrix. Login to control other VMs which we'll call workersmatrix.senecacollege. Later in ca and run the lab, we will try following command to control and monitor your vm in myvmlab using confirm the version of the fabfile we are going to develop. fabric package: Install '''fabric''' using yum. Once it's installed you should have a '<source lang='bash'>fab''' command available. --version</source>: Type the following command to get the command line optionoptions of the fab command:<source lang='bash'>
fab --help
</source>
:You should get something similar to the following:<source lang="bash">
Usage: fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...
number of concurrent processes to use in parallel mode
</source>
<font color='green'><b>Please note and study the following command-line options as they will be used in some of the activities in this lab::# -H, :# -f, :# -i, :# -l, and :# --port options.:# --user:# --initial-sudo-password-prompt </b></font>
== PART 2 : Connect to VM in myvmlab.senecacollege.ca ==: You should have received an email from ITS containing the following information::* account name: (usually 'student'):* password: (let's assume it is 'P@ssw0rd' for the following instruction in this lab):* port number for SSH access via myvmlab.senecacollege.ca (e.g. 7200): This VM will be used as the remote Linux machine in our Fabric environment. Login to matrix and try the following SSH command to test the connectivity between matrix and your assignment VM:<pre> [raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca student@myvmlab.senecacollege.ca's password: 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 ~]$ hostnamecentos7[student@centos7 ~]$ iduid=1002(student) gid=1002(student) groups=1002(student),10(wheel)[student@centos7 ~]$ dfFilesystem 1K-blocks Used Available Use% Mounted ondevtmpfs 878260 0 878260 0% /devtmpfs 889792 0 889792 0% /dev/shmtmpfs 889792 9492 880300 2% /runtmpfs 889792 0 889792 0% /sys/fs/cgroup/dev/mapper/centos- Create master Worker image root 38680112 1745524 36934588 5% //dev/sda2 1038336 331228 707108 32% /boot/dev/sda1 204580 11296 193284 6% /boot/efi/dev/mapper/centos-home 18880512 33160 18847352 1% /hometmpfs 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:centos7uid=1002(student) gid=1002(student) groups=1002(student),10(wheel)Filesystem 1K-blocks Used Available Use% Mounted ondevtmpfs 878260 0 878260 0% /devtmpfs 889792 0 889792 0% /dev/shmtmpfs 889792 9492 880300 2% /runtmpfs 889792 0 889792 0% /sys/fs/cgroup/dev/mapper/centos-root 38680112 1745608 36934504 5% //dev/sda2 1038336 331228 707108 32% /boot/dev/sda1 204580 11296 193284 6% /boot/efi/dev/mapper/centos-home 18880512 33160 18847352 1% /hometmpfs 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 you were asked to provide the user's password for every SSH connection.
==PART 3: Create a new virtual machineSet up SSH login with public key authentication ==: In order for your controller workstation to automate tasks execution on your VM, you need to configure your VM to SSH public key authentication instead of password authentication. You've done this in both OPS235 and OPS335, and allocate for here is a summary on how to do it 1GB of RAM between your account on matrix and 8GB of disk space. Install a Basic Web Server configuration of CentOS in that your VM using the same CentOS .iso file you used for your first machine in this course.:
: Make sure that::* The hostname of the system is '''worker1'''.:* It has Create a static IP address appropriate for new SSH key pair (one private, and one public) under your virtual networkaccount on matrix.senecacollege.:* Create a regular user using your Seneca email name as the user name: [seneca_id]ca. :* Add this new regular user to Once you have both keys, you can use the '''wheelssh-copy-id''' group using command to copy your public key to the student account on your VM, replace the port number with the following commandcorrect value for your VM:<source lang="'bash"'> usermod ssh-copy-id -a i ~/.ssh/id_rsa.pub -G wheel [seneca_id]p 7200 student@myvmlab.senecacollege.ca</source>This will allow : The above command should add the user contents of your pub key to run the '''sudo''' command~/.ssh/authorized_keys under your student account on your VM.:* After installation ensure Verify and confirm that you your account on matrix can access SSH to your VM as 'student'without prompting for a password:<source lang='worker1''bash' >[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.caLast login: Fri Jul 3 12:46:19 2020 from your main vm using the static IP address you've assigned mtrx-node05pd.dcm.senecacollege.ca[student@centos7 ~]$ exitlogoutConnection to itmyvmlab.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 2020centos7uid=1002(student) gid=1002(student) groups= Set up SSH key login ===1002(student),10(wheel)[raymond.chan@mtrx-node05pd lab8]$</source>: In order for an automated system to be able to connect to If you got similar result as above, you have successfully configure your controller workstation and your VM and administer it - you will need to be able to connect to it using SSH keys. You've done this in both OPS235 and OPS335use public key authentication.
=INVESTIGATION 2 - Running the fab command in ad-hoc mode =: Create a new The fab command relies on SSH key pair (one private, and one public) on your main VM with your 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''regular user''' (don't do >fab [options] -- [shell commands]</source>: When running the fab command in ad-hoc mode, it under root)is very similar to running the SSH with commands attached at the end. Once you have both keys, set things up so that== PART 1:* your regular user running non-privileged shell commands on your remote machines ==: In the following example, we use the '''controller VMfab''' can SSH to execute the worker "date", "hostname", and "id" command remotely on our VM as . Try the same regular user without prompting following ad-hoc fab commands and record their results for a password. (ie. add later use, replace the port number with the contents of correct value for your pub key to ~/.ssh/authorized_keys)VM:* your regular user on your <source lang='bash'>[raymond.chan@mtrx-node05pd lab8]$ fab --host=myvmlab.senecacollege.ca --port=7200 --user=student -- 'controller VMdate;hostname;id'[myvmlab.senecacollege.ca] Executing task '<remainder>' can SSH to the worker VM as root without propmting for a password[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(iestudent) groups=1002(student),10(wheel)[myvmlab. add the contents of your pub key to /root/senecacollege.ssh/authorized_keys)ca] out:
== PART 3 - Clone the Workers ==
: We're only simulating the real world where you'd have hundreds of VMs in one or more clouds, but you can just imagine that the VMs you're creating on your computer are actually being created on an Amazon or Microsoft server.
Done.Disconnecting from myvmlab.senecacollege.ca:7200... done.[raymond.chan@mtrx-node05pd lab8]$</source>: '''** Optional **''' Make four clones of Note that there is no password prompting if you complete part 3 successfully, otherwise, the master worker image SSH server daemon on your VM will prompt you've just created. Then make sure that each of them has for a unique IP addresspassword. ThatThe output from the fab's all you're required to change manually. All ad-hoc mode is not much different from the SSH command with shell command attached at the end, however, please note that the other configuration additional information on the workers (inlcuding output from the hostnames) will fab command can be set by Fabric. Normally you would have some kind of automation doing all this cloning very useful for record keeping purpose - what has been done and IP address assignment as well, but we don't have time for that this semesterwhether the commands had been carried out successfully or not.
== PART 2: Make snapshots of all your workers so running privileged commands on remote machines ==: We say that you can easily restore them running an ad-hoc fab command is very similar to the original state after you modify themSSH command with shell commands attached at the end. Let's try both with privileged commands, like the "yum" command.
= INVESTIGATION 2: Fabric practice =: We will start with some basics. Fabric runs python programs on = Run the controller and the workers. You create an "instructionyum" file command on remote machine with SSH ===: By default, your controller, and execute it on VM doesn't have the controller using the '''fab''' program"tree" rpm package installed. When you do that - you specify which workers you want your instructions to be executed on. : The instructions are stored in a python file. Let's start You can verify this with a simple one named '''fabfile.py''' the following SSH command (remember to replace the default filename used by fab without port number with the '-f' optinocorrect value for your VM): == PART 1: Simplest example =====Getting the hostname on the remote worker===: <source lang='bash'>[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7200 student@myvmlab.senecacollege.ca "pythonyum list tree">from fabric.api import *Loaded plugins: fastestmirror# set the name of the user on the remote hostLoading mirror speeds from cached hostfileenv * base: centos.name = '[seneca_id]'mirror.colo-serv.net * extras: centos.mirror.colo-serv.net# Will get the hostname of this worker * updates:centos.mirror.ca.planethoster.netAvailable Packagesdef getHostname():tree.x86_64 1.6.0-10.el7 base name = run("hostname") print(name)[raymond.chan@mtrx-node05pd lab8]$
</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
getHostname</source>: To perform the task of getHostname on the worker machine 192.168.122.169, we run it on the controller machine like this:<source lang="bash">[rchan@centos7 lab8]$ fab -f fabfile.py -H 192.168.122.169. getHostname[192.168.122.169] Executing task 'getHostname'[192.168.122.169] run: hostname[192.168.122.169] out: c7-rchan[192.168.122.169] out: Dependencies Resolved
c7======================================================================================================================== Package Arch Version Repository Size========================================================================================================================Installing: tree x86_64 1.6.0-rchan10.el7 base 46 k
Done.Transaction SummaryDisconnecting from 192.168.122.169... done.========================================================================================================================</source>:All this has done is get the hostname of the worker and print it (on the controller).:In the command above we're using the fab program to import the file fabfile.py and execute the getHostname function on the worker 192.168.122.169. Note that the IP address of your first worker will likely be different.Install 1 Package
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
: In the above you have:Complete![student@centos7 ~]$</source>:* Lines with an IP address telling Please note that when you which worker run the "sudo" command the output is first time, it asks you for/from.:* Messages from the controller user's password (i.e.g. "Executing task..user student's password).Let's now remote the ", and tree"runpackage:<source lang='bash'>[student@centos7 ~]$ yum remove tree -yLoaded plugins: fastestmirrorYou need to be root to perform this command...").[student@centos7 ~]$ sudo yum remove tree -yLoaded plugins:* Output from the worker ("outfastestmirrorResolving Dependencies--> Running transaction check---> Package tree.x86_64 0: 1.6.0-10.")el7 will be erased:* Output on the controller from your fab file ("worker1" which came from the "print()" call)--> Finished Dependency Resolution
:You should get used to the above. It's a lot of output but it's important to understand where every part is coming from, so you are able to debug problems when they happen.Dependencies Resolved
== Part 2: Set up more administrative tasks====================================================================================================================== Package Arch Version Repository Size========================================================================================================================Removing: tree x86_64 1.6.0-10.el7 @base 87 k
:Let's pretend that we need collect the disk usage on several machines so that we can plan for storage maintenance. We'll set up a simple example of such a deployment here.Transaction Summary========================================================================================================================Remove 1 Package
=== Getting the disk usage on remote worker ===Installed size: 87 kDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transaction Erasing : tree-1.6.0-10.el7.x86_64 1/1 Verifying : tree-1.6.0-10.el7.x86_64 1/1
Removed:Add a getDiskUsage() function to your fabfile tree.py filex86_64 0:1.6.0-10.el7
:<source lang="python"># to get the disk usage on remote workerComplete!def getDiskUsage(): current_time = run('date') diskusage = run('df -H') header = 'Current Disk Usage at '+current_time print(header) print(diskusage)[student@centos7 ~]$
</source>
: The above tests confirm that the student user is allowed to run the yum command to install and remove rpm package. 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
Connection to myvmlab.senecacollege.ca closed.
[raymond.chan@mtrx-node05pd lab8]$ ssh -p 7211 student@myvmlab.senecacollege.ca "sudo yum install tree -y"
sudo: no tty present and no askpass program specified
[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 ===: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):
<pre>$ fab == PART 1: Non-privileged task example =====Create non-fabfileprivileged tasks: Getting the hostname of remote machines===: Add the following contents to the default fabric script called "fabfile.py -H 192.168.122.169 getDiskUsage" in your lab8 directory:</presource lang="python">from fabric.api import *
:You should get # set the following output:<source lang="bash">name of the user login to the remote host[rchan@centos7 lab8]$ fab --fabfileenv.user =fabfile.py -H 192.168.122.169 getDiskUsage[192.168.122.169] Executing task 'getDiskUsagestudent'[192.168.122.169] run: date[192.168.122.169] out: Sun Nov 10 13:17:16 EST 2019[192.168.122.169] out:
[192.168.122.169] run# Define the task to get the hostname of remote machines: df -H[192.168.122.169] outdef getHostname(): Filesystem Size Used Avail Use% Mounted on[192.168.122.169] out: devtmpfs 947M 0 947M 0% /dev[192.168.122.169] out: tmpfs 964M 0 964M 0% /dev/shm[192.168.122.169] out: tmpfs 964M 9.7M 954M 2% /name = run("hostname")[192.168.122.169] out: tmpfs 964M 0 964M 0% /sys/fs/cgroup[192.168.122.169] out: /dev/mapper/centos-root 7.7G 5.6G 2.1G 73% /[192.168.122.169] outprint("The host name is: /dev/vda1 1.1G 298M 766M 29% /boot",name)[192.168.122.169] out: tmpfs 193M 17k 193M 1% </run/user/42[192.168.122.169] out: tmpfs 193M 0 193M 0% /run/user/1000[192.168.122.169] out: source>
Current Disk Usage at Sun Nov 10 13:17To 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":16 EST 2019Filesystem Size Used Avail Use% Mounted ondevtmpfs 947M 0 947M 0% /devtmpfs 964M 0 964M 0% /dev/shmtmpfs 964M 9.7M 954M 2% /run<source lang="bash">tmpfs 964M 0 964M 0% /sys/fs/cgroup/dev/mapper/centosfab -root 7.7G 5.6G 2.1G 73% //dev/vda1 1.1G 298M 766M 29% /boottmpfs 193M 17k 193M 1% /run/user/42tmpfs 193M 0 193M 0% /run/user/1000 Done.Disconnecting from 192.168.122.169... done.l
</source>
: you should get a list of tasks defined in your fabfile.py:<source lang="bash">
[rchan@centos7 lab8]$ fab -l
Available commands:
=== 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 workerdef performSoftwareUpdate(): status = run('yum update -y') print(status)getHostname
</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
Fatal error: run() received nonzero return code 1 while executing! Requested: yum update -yExecuted: /bin/bash -l -c "yum update -y" AbortingDone.Disconnecting from 192myvmlab.168senecacollege.122ca:7200.169..done. done[raymond.chan@mtrx-node05pd lab8]$
</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.
env.user = :In the above executed '''fab''root'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 first will likely be of a different value.
</source>: Save the fabfile.py with If you did all the change setup right and run it again.: If you see the got a password prompt againwhen execute the above command, make sure that read the prompt carefully and see who's password it was prompting you can ssh from your controller as a regular user to your worker vm as root without passwordfor.: The other way If it is to replace all not for the run() function calls for commands user student, verify that need superuser privilege by you have the sudo() function calls following line in your fabfile.py. You are asked and you can ssh to investigate this in your VM as the final investigation of this lab.user student without password:
== Part 3: Setting and Checking Security Configuration <source lang="python">env.user ='student'</source>
: 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:
: To check your firewall configuration your remote worker, you can retrieve its current configuration by creating another Fabric task called "getFirewallConfigure(). Let's put the following code to your fabfile.py:<source lang="python"> getHostnamedef getFirewallConfig(): installPackage fw_config = run("iptables removePackage[raymond.chan@mtrx-L -n -v") print(fw_config)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" 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
= INVESTIGATION 3fab removePackage: Multiplying your work =tree</source>
== 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:
'''Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfile *base: less.cogeco.net * Optional extras: centos.mirror.ca.planethoster.net **'''You were asked to test them on worker1updates: less. Now let's run these three functions on all your workers at the same timecogeco. The command is almost the same, except netNo packages marked for the list of IP addresses:update
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() preformSoftwareUpdate()</source>: And run the following command on your controllerx86_64 0:3.10.0-862.el7
<source lang="bash">fab --fabfile=fabfile.py -H 192Installed: kernel.168x86_64 0:3.12210.169,192.168.122.170,192.168.122.171,1920-1127.16813.1221.172 doAllThree</source>el7
And imagine that you might have Updated: bind-export-libs.x86_64 32:9.11.4-16.P2.el7_8.6 binutils.x86_64 0:2.27-43.base.el7_8.1 ca-certificates.noarch 0:2020.2.41-70.0.el7_8 device-mapper.x86_64 7:1.02.164-7.el7_8.2 device-mapper-event.x86_64 7:1.02.164-7.el7_8.2 device-mapper-event-libs.x86_64 7:1.02.164-7.el7_8.2 device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2 kernel-tools.x86_64 0:3.10.0-1127.13.1.el7 kernel-tools-libs.x86_64 0:3.10.0-1127.13.1.el7 lvm2.x86_64 7:2.02.186-7.el7_8.2 lvm2-libs.x86_64 7:2.02.186-7.el7_8.2 microcode_ctl.x86_64 2:2.1-61.10 tasks to be done on .el7_8 net-snmp.x86_64 1:5.7.2-48.el7_8.1 net-snmp-agent-libs.x86_64 1:5.7.2-48.el7_8.1 net-snmp-libs.x86_64 1:5.7.2-48.el7_8.1 net-snmp-utils.x86_64 1:5.7.2-48.el7_8.1 ntp.x86_64 0:4.2.6p5-29.el7.centos.2 ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2 python-perf.x86_64 0:3.10, 50, 100 servers .0-1127.13.1.el7 rsyslog.x86_64 0:8.24.0-52.el7_8.2 selinux-policy.noarch 0:3.13.1-266.el7_8.1 selinux-policy-targeted.noarch 0:3.13.1-266.el7_8.1 systemd.x86_64 0:219-73.el7_8.8 systemd-libs.x86_64 0:219-73.el7_8.8 systemd-sysv.x86_64 0:219-73.el7_8.8 yum-plugin-fastestmirror.noarch 0:1.1.31- could you do it without the automation?54.el7_8
= Investigation 4 - Apply fabfile.py to your VM on myvmlab === Replace run() function calls with sudo() ==: Since your account on your vm on myvmlab is a regular user with sudo privilege. You need to make the following changes to your fabfile.py before applying it to your vm on myvmlab::* Change env.user from 'root' to your account on your vm in myvmlab.:* Change all the commands that need super user privilege from calling the run() function to instead calling the sudo() function. Here is an example on replacing run() with sudo():<source lang="python">Complete!
def getFirewallConfig()Done.Disconnecting from myvmlab.senecacollege.ca:7200... done. fw_config = sudo("iptables -L -n [raymond.chan@mtrx-v") print(fw_config)node05pd lab8]$
</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:
::* create a new user called "ops435p" with home directory "/home/ops435p".::* add it to the sudo group called "wheel". ::* add ask your professor's for a ssh public key and add it to the file named "authorized_keys" in the ~ops435p/.ssh directory. Make sure that you set the proper permissions on both the directory ~ops435p/.ssh and the file "~ops435p/.ssh/authorized_keys.
: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 you mv 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]]
1,760
edits

Navigation menu