SQLAlchemy data models and configuration tools used in the CIDC API
Project description
CIDC API
Environment | Branch | Status | Maintainability | Test Coverage |
---|---|---|---|---|
production | production | |||
staging | master |
The next generation of the CIDC API, reworked to use Google Cloud-managed services. This API is built with the Eve REST API framework backed by Google Cloud SQL, running on Google App Engine.
Development
- Install Python dependencies
- Database Management
- Serving Locally
- Testing
- Code Formatting
- Deployment
- Connecting to the API
- Provisioning the system from scratch
Install Python dependencies
Install both the production and development dependencies.
pip install -r requirements.dev.txt
Install and configure pre-commit hooks.
pre-commit install
Database Management
Setting up a local development database
In production, the CIDC API connects to a PostgreSQL instance hosted by Google Cloud SQL, but for local development, you should generally use a local PostgreSQL instance.
To do so, first install and start PostgreSQL:
brew install postgresql
brew services start postgresql # launches the postgres service whenever your computer launches
or for Ubuntu/Debian
sudo apt install postgresql
service postgresql start
sudo update-rc.d postgres defaults # launches the postgres service whenever your computer launches
sudo -i -u postgres # used to access postgresql user
By default, the postgres service listens on port 5432. Next, create the cidcdev
user, your local cidc
development database, and a local cidctest
database that the unit/integration tests will use:
psql -c "create user cidcdev with password '1234'"
# Database to use for local development
psql -c "create database cidc"
psql -c "grant all privileges on database cidc to cidcdev"
psql cidc -c "create extension citext"
psql cidc -c "create extension pgcrypto"
# Database to use for automated testing
psql -c "create database cidctest"
psql -c "grant all privileges on database cidctest to cidcdev"
psql cidctest -c "create extension citext"
psql cidctest -c "create extension pgcrypto"
Now, you should be able to connect to your development database with the URI postgresql://cidcdev:1234@localhost:5432/cidc
. Or, in the postgres REPL:
psql cidc
Next, you'll need to set up the appropriate tables, indexes, etc. in your local database. To do so, cd
into the cidc_api
directory (as your local user), then run:
FLASK_APP=cidc_api.app:app flask db upgrade
You will also need to set the GOOGLE_APPLICATION_CREDENTIALS
environment variable to the local path of the credentials file for the staging environment's App Engine service account.
Connecting to a Cloud SQL database instance
Install the Cloud SQL Proxy:
curl -o /usr/local/bin/cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
chmod +x /usr/local/bin/cloud_sql_proxy
mkdir ~/.cloudsql
Proxy to the staging Cloud SQL instance:
cloud_sql_proxy -instances cidc-dfci-staging:us-central1:cidc-postgresql-staging -dir ~/.cloudsql
In your .env
file, comment out POSTGRES_URI
and uncomment all environment variables prefixed with CLOUD_SQL_
. Restart your local API instance, and it will connect to the staging Cloud SQL instance via the local proxy.
If you wish to connect to the staging Cloud SQL instance via the postgres REPL, download and run the CIDC sql proxy tool (a wrapper for cloud_sql_proxy
):
# Download the proxy
curl https://raw.githubusercontent.com/CIMAC-CIDC/cidc-devops/master/scripts/cidc_sql_proxy.sh -o /usr/local/bin/cidc_sql_proxy
chmod +x /usr/local/bin/cidc_sql_proxy
# Run the proxy
cidc_sql_proxy staging # or cidc_sql_proxy prod
Running database migrations
This project uses Flask Migrate
for managing database migrations. To create a new migration and upgrade the database specified in your .env
config:
export FLASK_APP=cidc_api/app.py
# Generate the migration script
flask db migrate -m "<a message describing the changes in this migration>"
# Apply changes to the database
flask db upgrade
To revert an applied migration, run:
flask db downgrade
If you're updating models.py
, you should create a migration and commit the resulting
Serving Locally
Once you have a development database set up and running, run the API server:
ENV=dev gunicorn cidc_api.app:app
Testing
This project uses pytest
for testing.
To run the tests, simply run:
pytest
Code Formatting
This project uses black
for code styling.
We recommend setting up autoformatting-on-save in your IDE of choice so that you don't have to worry about running black
on your code.
Deployment
CI/CD
This project uses GitHub Actions for continuous integration and deployment. To deploy an update to this application, follow these steps:
- Create a new branch locally, commit updates to it, then push that branch to this repository.
- Make a pull request from your branch into
master
. This will trigger GitHub Actions to run various tests and report back success or failure. You can't merge your PR until it passes the build, so if the build fails, you'll probably need to fix your code. - Once the build passes (and pending approval from collaborators reviewing the PR), merge your changes into
master
. This will trigger GitHub Actions to re-run tests on the code then deploy changes to the staging project. - Try out your deployed changes in the staging environment once the build completes.
- If you're satisfied that staging should be deployed into production, make a PR from
master
intoproduction
. - Once the PR build passes, merge
master
intoproduction
. This will trigger GitHub Actions to deploy the changes on staging to the production project.
For more information or to update the CI workflow, check out the configuration in .github/workflows/ci.yml
.
Deploying by hand
Should you ever need to deploy the application to Google App Engine by hand, you can do so by running the following:
gcloud app deploy <app.staging.yaml or app.prod.yaml> --project <gcloud project id>
That being said, avoid doing this! Deploying this way circumvents the safety checks built into the CI/CD pipeline and can lead to inconsistencies between the code running on GAE and the code present in this repository. Luckily, though, GAE's built-in versioning system makes it hard to do anything catastrophic :-)
Connecting to the API
Currently, the staging API is hosted at https://staging-api.cimac-network.org and the production instance is hosted at https://api.cimac-network.org.
To connect to the staging API with curl
or a REST API client like Insomnia, get an id token from stagingportal.cimac-network.org, and include the header Authorization: Bearer YOUR_ID_TOKEN
in requests you make to the staging API. If your token expires, generate a new one following this same procedure.
To connect to the production API locally, follow the same procedure, but instead get your token from portal.cimac-network.org.
Provisioning the system from scratch
For an overview of how to set up the CIDC API service from scratch, see the step-by-step guide in PROVISION.md
.
JIRA Integration
To set-up the git hook for JIRA integration, run:
ln -s ../../.githooks/commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
rm .git/hooks/commit-msg.sample
This symbolic link is necessary to correctly link files in .githooks
to .git/hooks
. Note that setting the core.hooksPath
configuration variable would lead to pre-commit failing. The commit-msg
hook runs after the pre-commit
hook, hence the two are de-coupled in this workflow.
To associate a commit with an issue, you will need to reference the JIRA Issue key (For eg 'CIDC-1111') in the corresponding commit message.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cidc_api_modules-0.27.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42171454a589e7ef2974ad40a0dd77230ed88ae934286bf5ebad6e79f45f742b |
|
MD5 | ece602c0bb86673cdd54b7c5480fb53d |
|
BLAKE2b-256 | 0c2aad722e2cef93bdd282c93f4b762860a3a34db1db3086142d96aae174a8fe |