Troubleshoot the Development Environment
-
Information only
-
Information only
-
Copy the file
caleston-code.tar.gz
from Bob's laptop to Bob's home directory on the webserverdevapp01
scp caleston-code.tar.gz devapp01:~/
-
On the
devapp01
webserver, unzip and extract the copied file in the directory/opt/
.Note that the
/opt
directory is owned by root ondevapp01
so we'll need sudossh devapp01 sudo tar -zxf caleston-code.tar.gz -C /opt
Don't exit from the ssh session just yet. We need to remain on
devapp01
-
Delete the tar file from
devapp01
webserver.rm caleston-code.tar.gz
-
Make sure that the directory is extracted in such a way that the path
/opt/caleston-code/mercuryProject/
exists on thewebserver
.Nothing to do here. If you got Q4 right, then this will work too - press OK.
You can verify if you like:
ls -l /opt/caleston-code/mercuryProject/
-
For the client demo, Bob has installed a
postgres
database indevdb01
.
SSH to the databasedevdb01
and check the status of thepostgresql.service
What is the state of the DB?At this point you are still logged into
devapp01
, so first return to Bob's laptopexit
Now go to the db node
ssh devdb01 systemctl status postgresql.service
Note the status:
Active: inactive (dead) ...
Don't exit from the ssh session just yet. We need to remain on
devdb01
-
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.
This will allow the web application to connect to the DB.sudo vi /etc/postgresql/10/main/pg_hba.conf
Make the change as directed, save and exit
vi
. -
Start the
postgres
DB on thedevdb01
server.sudo systemctl start postgresql.service
-
What port is
postgres
running on? Check using thenetstat
command?sudo netstat -ptean
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!
-
Back on the
devapp01
webserver. Attempt to start the web application by:
Navigate to the directory/opt/caleston-code/mercuryProject
Next, run the commandpython3 manage.py runserver 0.0.0.0:8000
At this point you are still logged into
devdb01
, so first return to Bob's laptopexit
Now go to the app node
ssh devapp01 cd /opt/caleston-code/mercuryProject python3 manage.py runserver 0.0.0.0:8000
Note it dumps a stack trace on the screen, i.e. it crashed! Thus the answer is
No
.Press
CRTL + C
-
Why did the command not work?
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!
-
It appears that Bob did not configure his app to connect a
postgres
database running on a different server.That explains why things are working on his laptop and not in the
DEV
servers.It also appears that he is using the
wrong
port for postgres!- Find the file in the directory under
/opt/caleston-code
that has a string matching DATABASES = {. - Replace the value of
localhost
todevdb01
- In the same file fix the postgres port to match the port being used on
devdb01
For this, we need to first find the file we need to edit, so we're going to use
find
to find files and usegrep
to find the specific text, with the-l
switch to print the file path the text was found in.find . -type f -exec grep -l 'DATABASES = {' "{}" \;
Edit the file that was returned by the above
vi ./mercury/settings.py
Scroll down to
DATABASES = {
and beneath this set the correct host and port. Save and exit. - Find the file in the directory under
-
Now that has been set up, change the ownership of ALL files and directories under
/opt/caleston-code
to usermercury
.You'll need to be root to reassign ownership
sudo chown -R mercury /opt/caleston-code
Note that sometimes this step is marked incorrect even though it is correct! Ignore this and move on. It has been reported to the lab team.
-
Great! Everything should now be in order to restart the application.
On the devapp01 server start the webserver again by running the command:
- Navigate to the directory
/opt/caleston-code/mercuryProject
- Next, run the command
python3 manage.py runserver 0.0.0.0:8000
Note:- Make sure to activate the virtual environment using
source ../venv/bin/activate
within the current project before executingpython3 manage.py migrate
.If you've followed all the above steps, you should still be in directory
/opt/caleston-code/mercuryProject
Run the migration.
source ../venv/bin/activate python3 manage.py migrate
Start the app again so the question will validate.
python3 manage.py runserver 0.0.0.0:8000
What is this venv stuff?
If you're considering learning Python (highly recommended as it is required in many 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. This is especially useful on your development environment where you may have multiple Python projects all with different package requirements.
- Navigate to the directory
-
Well done! Now, for the final task before the client presentation.
Create a new service called mercury.service with the following requirements.
- Service name: -
mercury.service
, WorkingDirectory: -/opt/caleston-code/mercuryProject/
, Command to run:/usr/bin/python3 manage.py runserver 0.0.0.0:8000
. - Restart
on failure
and enable formulti-user.target
. - Run as user
mercury
. - Set description:
Project Mercury Web Application
.
Here we have to create a systemd unit file to make the Python app be runnable as a service.
First quit the running webapp by pressing
CTRL-C
Now create the unit file
sudo vi /etc/systemd/system/mercury.service
And put the following in to satisfy the question requirements
[Unit] Description=Project Mercury Web Application [Service] ExecStart=/usr/bin/python3 manage.py runserver 0.0.0.0:8000 Restart=on-failure WorkingDirectory=/opt/caleston-code/mercuryProject/ User=mercury [Install] WantedBy=multi-user.target
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 withsystemctl
commands. We can saymercury.service
, or simplymercury
sudo systemctl daemon-reload sudo systemctl enable mercury sudo systemctl start mercury
- Service name: -
-
Information only - browse the running application!