Skip to content
hannahlatourette edited this page Jun 15, 2018 · 5 revisions

API Deployment

Run the latest installation bash script in Ubuntu 16.04. Located here.

Before beginning, if you have a legacy data dump, you should have it handy.

Installation requires an internet connection.

Running the installation script:

  1. Make it executable

    chmod u+x install_metpetdb.sh
    
  2. Run as super user

    sudo ./install_metpetdb.sh
    

Installation Stages

The script is interactive and operates in the following stages:
(** -- user input required)
(^^ -- this may take a while)
(op -- an optional step)

  1. Create user metpetdb
  2. Update system packages (^^)
  3. Install additional required packages (^^)
  4. Install PostgreSQL
  5. Install PostGIS
  6. Setup PostgreSQL
  7. Setup PostGIS templates
  8. Create metpetdb databases
    • metpetdb_legacy - for compatibility with older data dumps
    • metpetdb - main database
  9. Set Postgres Password for the metpetdb user (**)
    • This is the password used to connect to the metpetdb databases.
    • The appropriate fields in api.env are automatically filled.
  10. Set ownership of metpetdb and metpetdb_legacy databases to metpetdb user
  11. Download latest codebase to /home/metpetdb/api
  12. Create the "api" virtual environment
  13. Install required Python packages
  14. Create "api.env" config
  15. Populate the legacy database from a data dump (op)
    • Provide the absolute system path to the data dump. e.g /home/metpetdb/Desktop/metpetdb_may2017.dmp
  16. Perform data model migrations from Django to postgreSQL
  17. Perform data migrations from metpetdb_legacy to metpetdb database (**)(^^)(op)
  18. Collect static
  19. Setup Nginx (**)(op)

As the metpetdb user run:

source ~/.bashrc

After installation is completed, the api is located at /home/metpetdb/api.

Configure email settings in api.env

EMAIL_HOST_USER: The email account used to send account activation and password reset emails.
EMAIL_HOST_PASSWORD: The corresponding EMAIL_HOST_USER password.
FRONT_END_URL: The url to the front end, prepended to account activation and password reset links.

Local Development:

To run the api

cd /home/metpetdb/api/metpetdb_api/
workon api 
python manage.py runserver

The API should now be running and browsable at http://127.0.0.1:8000/

Production:

Nginx:

Ensure nginx is running

sudo service --status-all | grep nginx

should yield

[+] nginx

If not, run:

sudo service nginx restart

As the metpetdb user run:

start_api

Press enter to continue.

The API should now be running at <your_ip>/api

Security Note: When deploying the API on the production server, ensure that DEBUG is set to False in settings file in API_SETTINGS. When debug is enabled, all environment variables are exposed, including those for the email host username and password.

Important Note: Ensure the secret key used in api.env matches the secret key used in the metpetdb interface.

Redeployment:

As the metpetdb user run:

redeploy_api

The previous version of the API is kept on the server under the directory 'old_api'. The latest version of the API replaces the old and the existing api.env is retained.

Next, restart the server

sudo reboot now

Finally, reconnect to the server and restart the API

start_api	

Troubleshooting

  • Installation failed during PostgreSQL setup

    • Likely caused by a version compatibility issue with the installation script
    • Before running the script:
      sudo apt-get install postgresql-9.5-postgis-2.2 postgresql-9.5-postgis-scripts
      
  • Installation failed during data migrations, database corrupted during install, etc.

    • Delete the database and try running the installation script again

      As the metpetdb user:

      dropdb metpetdb  
      dropdb metpetdb_legacy
      sudo ./install_metpetdb.sh
      
  • Email configuration with Gmail:

  • Cannot find commands workon, start_api, redeploy_api etc.

    • As the metpetdb user:

      source ~/.bashrc
      

Future Improvements

  • Remove existing start_api() and redeploy_api() commands from the .bashrc, before appending new version of these commands.
  • Uninstallation script / flag

Clone this wiki locally