|
1 | 1 | # Troubleshoot the Development Environment
|
2 | 2 |
|
3 |
| - - [Troubleshoot](https://kodekloud.com/topic/lab-final-troubleshooting-scenario/) the Development Environment |
| 3 | +[Troubleshoot](https://kodekloud.com/topic/lab-troubleshoot-the-development-environment/) the Development Environment |
| 4 | + |
| 5 | + |
| 6 | +1. Information only |
| 7 | +1. Information only |
| 8 | +1. <details> |
| 9 | + <summary>Copy the file caleston-code.tar.gz from Bob's laptop to Bob's home directory on the webserver devapp01</summary> |
| 10 | + |
| 11 | + ```bash |
| 12 | + scp caleston-code.tar.gz devapp01:~/ |
| 13 | + ``` |
| 14 | + |
| 15 | + </details> |
| 16 | +1. <details> |
| 17 | + <summary>On the devapp01 webserver, unzip and extract the copied file in the directory /opt/.</summary> |
| 18 | + |
| 19 | + Note that the `/opt` directory is owned by root on `devapp01` so we'll need sudo |
| 20 | +
|
| 21 | + ```bash |
| 22 | + ssh devapp01 |
| 23 | +
|
| 24 | + sudo tar -zxf caleston-code.tar.gz -C /opt |
| 25 | + ``` |
| 26 | +
|
| 27 | + Don't exit from the ssh session just yet. We need to remain on `devapp01` |
| 28 | + |
| 29 | + </details> |
| 30 | +1. <details> |
| 31 | + <summary>Delete the tar file from devapp01 webserver.</summary> |
| 32 | + |
| 33 | + ```bash |
| 34 | + rm caleston-code.tar.gz |
| 35 | + ``` |
| 36 | + |
| 37 | + </details> |
| 38 | +1. <details> |
| 39 | + <summary>Make sure that the directory is extracted in such a way that the path /opt/caleston-code/mercuryProject/ exists on the webserver.</summary> |
| 40 | + |
| 41 | + Nothing to do here. If you got Q4 right, then this will work too - press OK. |
| 42 | + |
| 43 | + You can verify if you like: |
| 44 | + |
| 45 | + ```bash |
| 46 | + ls -l /opt/caleston-code/mercuryProject/ |
| 47 | + ``` |
| 48 | + |
| 49 | + </details> |
| 50 | +1. <details> |
| 51 | + <summary>For the client demo, Bob has installed a postgres database in devdb01.</br>SSH to the database devdb01 and check the status of the postgresql.service</br>What is the state of the DB?</summary> |
| 52 | + |
| 53 | + At this point you are still logged into `devapp01`, so first return to Bob's laptop |
| 54 | +
|
| 55 | + ```bash |
| 56 | + exit |
| 57 | + ``` |
| 58 | +
|
| 59 | + Now go to the db node |
| 60 | +
|
| 61 | + ```bash |
| 62 | + ssh devdb01 |
| 63 | +
|
| 64 | + systemctl status postgresql.service |
| 65 | + ``` |
| 66 | +
|
| 67 | + Note the status: `Active: inactive (dead) ...` |
| 68 | +
|
| 69 | + Don't exit from the ssh session just yet. We need to remain on `devdb01` |
| 70 | + </details> |
| 71 | +1. <details> |
| 72 | + <summary>Add an entry host all all 0.0.0.0/0 md5 to the end of the file /etc/postgresql/10/main/pg_hba.conf on the DB server.</br>This will allow the web application to connect to the DB.</summary> |
| 73 | + |
| 74 | + ```bash |
| 75 | + sudo vi /etc/postgresql/10/main/pg_hba.conf |
| 76 | + ``` |
| 77 | + |
| 78 | + Make the change as directed, save and exit `vi`. |
| 79 | + |
| 80 | + </details> |
| 81 | +1. <details> |
| 82 | + <summary>Start the postgres DB on the devdb01 server.</summary> |
| 83 | + |
| 84 | + ```bash |
| 85 | + sudo systemctl start postgresql.service |
| 86 | + ``` |
| 87 | + |
| 88 | + </details> |
| 89 | +1. <details> |
| 90 | + <summary>What port is postgres running on? Check using the netstat command?</summary> |
| 91 | + |
| 92 | + ```bash |
| 93 | + sudo netstat -ptean |
| 94 | + ``` |
| 95 | + |
| 96 | + There's 2 entries for postgres, one each for IPv4 and IPv6, but both are using the same port. Note this port down - you will need it later! |
| 97 | +
|
| 98 | + </details> |
| 99 | +1. <details> |
| 100 | + <summary>Back on the devapp01 webserver. Attempt to start the web application by:</br>Navigate to the directory /opt/caleston-code/mercuryProject</br>Next, run the command python3 manage.py runserver 0.0.0.0:8000</summary> |
| 101 | +
|
| 102 | + At this point you are still logged into `devdb01`, so first return to Bob's laptop |
| 103 | + |
| 104 | + ```bash |
| 105 | + exit |
| 106 | + ``` |
| 107 | + |
| 108 | + Now go to the app node |
| 109 | + |
| 110 | + ```bash |
| 111 | + ssh devapp01 |
| 112 | +
|
| 113 | + cd /opt/caleston-code/mercuryProject |
| 114 | + python3 manage.py runserver 0.0.0.0:8000 |
| 115 | + ``` |
| 116 | + |
| 117 | + Note it dumps a stack trace on the screen, i.e. it crashed! Thus the answer is `No`. |
| 118 | + |
| 119 | + Press `CRTL + C` |
| 120 | + |
| 121 | + </details> |
| 122 | +1. <details> |
| 123 | + <summary>Why did the command not work?</summary> |
| 124 | + |
| 125 | + The answer to this is in the stack trace at the end. The app cannot connect to the database on the address and port indicated. Also remember the earlier question where you were asked to find the port that the database server is listening on! |
| 126 | + |
| 127 | + </details> |
| 128 | +1. <details> |
| 129 | + <summary>It appears that Bob did not configure his app to connect a postgres database running on a different server.</summary> |
| 130 | + |
| 131 | + For this, we need to first find the file we need to edit, so we're going to use `find` to find _files_ and use `grep` to find the specific text, with the `-l` switch to print the file path the text was found in. |
| 132 | +
|
| 133 | + ```bash |
| 134 | + find . -type f -exec grep -l 'DATABASES = {' "{}" \; |
| 135 | + ``` |
| 136 | +
|
| 137 | + Edit the file that was returned by the above |
| 138 | +
|
| 139 | + ```bash |
| 140 | + vi ./mercury/settings.py |
| 141 | + ``` |
| 142 | +
|
| 143 | + Scroll down to `DATABASES = {` and beneath this set the correct host and port. Save and exit. |
| 144 | +
|
| 145 | + </details> |
| 146 | +1. <details> |
| 147 | + <summary>Now that has been set up, change the ownership of ALL files and directories under /opt/caleston-code to user mercury.</summary> |
| 148 | +
|
| 149 | + You'll need to be root to reassign ownership |
| 150 | + |
| 151 | + ```bash |
| 152 | + sudo chown -R mercury /opt/caleston-code |
| 153 | + ``` |
| 154 | + |
| 155 | + </details> |
| 156 | +1. <details> |
| 157 | + <summary>Great! Everything should now be in order to restart the application.</summary> |
| 158 | + |
| 159 | + If you've followed all the above steps, you should still be in directory `/opt/caleston-code/mercuryProject` |
| 160 | +
|
| 161 | + Start the app as directed and verify it works, then `CTRL-C` to exit. |
| 162 | +
|
| 163 | + Now run the migration. Note that the venv directory is not beneath the current directory as the question suggests. It is actually in the *parent* directory, hence `../venv` below. |
| 164 | +
|
| 165 | + ```bash |
| 166 | + source ../venv/bin/activate |
| 167 | + python3 manage.py migrate |
| 168 | + ``` |
| 169 | +
|
| 170 | + Start the app again so the question will vaildate. |
| 171 | +
|
| 172 | + **What is this venv stuff?** |
| 173 | +
|
| 174 | + If you're considering learning Python (highly recommended as it is required in most DevOps jobs), this means Virtual ENVironment. It allows you to install python packages on a project-by-project basis, thus not polluting the main Python installation. |
| 175 | + |
| 176 | + </details> |
| 177 | +1. <details> |
| 178 | + <summary>Well done! Now, for the final task before the client presentation.</summary> |
| 179 | + |
| 180 | + Here we have to create a [systemd unit file](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/assembly_working-with-systemd-unit-files_configuring-basic-system-settings) to make the python app be runnable as a service. |
| 181 | + |
| 182 | + First quit the running webapp by pressing `CTRL-C` |
| 183 | + |
| 184 | + Note that in unit files, the process to execute (in this case `python3`) we must use its fully qualified path, as `systemd` does not have a search path. Get this like this |
| 185 | + |
| 186 | + ```bash |
| 187 | + which python3 |
| 188 | + ``` |
| 189 | + |
| 190 | + Now create the unit file |
| 191 | + |
| 192 | + ```bash |
| 193 | + sudo vi /etc/systemd/system/mercury.service |
| 194 | + ``` |
| 195 | + |
| 196 | + And put the following in to satisfy the question requirements |
| 197 | + |
| 198 | + ``` |
| 199 | + [Unit] |
| 200 | + Description=Project Mercury Web Application |
| 201 | +
|
| 202 | + [Service] |
| 203 | + ExecStart=/usr/bin/python3 manage.py runserver 0.0.0.0:8000 |
| 204 | + Restart=on-failure |
| 205 | + WorkingDirectory=/opt/caleston-code/mercuryProject/ |
| 206 | + User=mercury |
| 207 | +
|
| 208 | + [Install] |
| 209 | + WantedBy=multi-user.target |
| 210 | + ``` |
| 211 | + |
| 212 | + Now enable and start the service. We must run a `daemon-reload` whenever we have created, edited or deleted a unit file. Note that the `.service` extension is optional with `systemctl` commands. We can say `mercury.service`, or simply `mercury` |
| 213 | + |
| 214 | + ```bash |
| 215 | + sudo systemctl daemon-reload |
| 216 | + sudo systemctl enable mercury |
| 217 | + sudo systemctl start mercury |
| 218 | + ``` |
| 219 | + |
| 220 | + </details> |
| 221 | +1. Information only - browse the running application! |
0 commit comments