Skip to content

Latest commit

 

History

History
241 lines (163 loc) · 8.21 KB

02-Troubleshoot-the-Development-Environment.md

File metadata and controls

241 lines (163 loc) · 8.21 KB

Troubleshoot the Development Environment

Troubleshoot the Development Environment

  1. Information only

  2. Information only

  3. Copy the file caleston-code.tar.gz from Bob's laptop to Bob's home directory on the webserver devapp01
    scp caleston-code.tar.gz devapp01:~/
  4. On the devapp01 webserver, unzip and extract the copied file in the directory /opt/.

    Note that the /opt directory is owned by root on devapp01 so we'll need sudo

    ssh 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

  5. Delete the tar file from devapp01 webserver.
    rm caleston-code.tar.gz
  6. Make sure that the directory is extracted in such a way that the path /opt/caleston-code/mercuryProject/ exists on the webserver.

    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/
  7. For the client demo, Bob has installed a postgres database in devdb01.
    SSH to the database devdb01 and check the status of the postgresql.service
    What is the state of the DB?

    At this point you are still logged into devapp01, so first return to Bob's laptop

    exit

    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

  8. 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.

  9. Start the postgres DB on the devdb01 server.
    sudo systemctl start postgresql.service
  10. What port is postgres running on? Check using the netstat 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!

  11. Back on the devapp01 webserver. Attempt to start the web application by:
    Navigate to the directory /opt/caleston-code/mercuryProject
    Next, run the command python3 manage.py runserver 0.0.0.0:8000

    At this point you are still logged into devdb01, so first return to Bob's laptop

    exit

    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

  12. 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!

  13. 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!

    1. Find the file in the directory under /opt/caleston-code that has a string matching DATABASES = {.
    2. Replace the value of localhost to devdb01
    3. 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 use grep 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.

  14. Now that has been set up, change the ownership of ALL files and directories under /opt/caleston-code to user mercury.

    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.

  15. Great! Everything should now be in order to restart the application.

    On the devapp01 server start the webserver again by running the command:

    1. Navigate to the directory /opt/caleston-code/mercuryProject
    2. 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 executing python3 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.

  16. Well done! Now, for the final task before the client presentation.

    Create a new service called mercury.service with the following requirements.

    1. Service name: - mercury.service, WorkingDirectory: - /opt/caleston-code/mercuryProject/, Command to run: /usr/bin/python3 manage.py runserver 0.0.0.0:8000.
    2. Restart on failure and enable for multi-user.target.
    3. Run as user mercury.
    4. 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 with systemctl commands. We can say mercury.service, or simply mercury

    sudo systemctl daemon-reload
    sudo systemctl enable mercury
    sudo systemctl start mercury
  17. Information only - browse the running application!