Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add `DEPLOYMENT.md` & deployment-related files (#711)
Madison Swain-Bowdenbcda6e0bcff3
diff --git a/DEPLOY.md b/DEPLOY.mddeleted file mode 100644index ebed2a975..000000000--- a/DEPLOY.md+++ /dev/null@@ -1,41 +0,0 @@-# Deployment--This document describes the steps necessary for deploying the catalog on our AWS EC2 instance.-For convenience (and access frequency), instructions for deploying an update are provided first.-If you wish to deploy a new instance, see [Deploying a fresh instance](#deploying-a-fresh-instance).--**Note**: All steps assume that the environment variable `IS_PROD` is set to `true` in the bash session being used for deployment.-If it is not `export`ed for the session, `IS_PROD=true` will need to be prepended to all `just` commands (e.g. `IS_PROD=true just build`)--## Deploying an update to DAG files--1. `git pull` _(Yes, that's it! Airflow will take a few moments to populate the changes.)_--## Deploying an update to the Docker image or non-DAG code--1. Verify that any new Airflow Variables, Airflow Connections, or environment variables have been added or updated.-2. Run `just deploy` within the repository's folder.--## Deploying a fresh instance--1. Clone the repository onto the target machine.-2. Run `just dotenv` to create a new `.env` file from the template.-3. Generate a secure password for the `airflow` account on the Airflow Metastore DB, and update the account using `ALTER USER airflow WITH PASSWORD '<generated-password>';` _(Note: this process will be changed in [gh-260](https://github.com/WordPress/openverse-catalog/issues/260))_-4. Create an `AIRFLOW__CORE__FERNET_KEY` using the instructions described in the `.env` file-5. Change the following variables in the `.env` file:- - **(Required)**- - `AIRFLOW__CORE__SQL_ALCHEMY_CONN` (using the password created above)- - `AIRFLOW__CORE__HIDE_SENSITIVE_VAR_CONN_FIELDS`- - `AIRFLOW_VAR_ENVIRONMENT` (set to `prod`)- - `AIRFLOW_CONN_AWS_DEFAULT`- - `AIRFLOW_CONN_POSTGRES_OPENLEDGER_UPSTREAM`- - `AIRFLOW_CONN_POSTGRES_OPENLEDGER_TESTING`- - `AWS_ACCESS_KEY`- - `AWS_SECRET_KEY`- - **(Recommended)**- - `AIRFLOW_PORT`- - `LOADER_FILE_AGE`- - Any API key variables-6. Run `just build`, verify the image built correctly-7. Run `just up`, verify that the Airflow container has started successfully-8. Run `just airflow "users create -r Admin -u <username> -f <first-name> -l <last-name> -p <password> --email <email>"` with desired information to create an Administrator account for the Airflow UIdiff --git a/DEPLOYMENT.md b/DEPLOYMENT.mdnew file mode 100644index 000000000..e37b847cf--- /dev/null+++ b/DEPLOYMENT.md@@ -0,0 +1,66 @@+# Catalog Deployments++The Openverse Catalog consists of two core components: a **Postgres** instance and an **Airflow** instance.+This document describes the environments & deployment procedures for both components.++# Airflow++> **URL**: https://airflow.openverse.engineering++Airflow has two different deployment mechanisms: **service deployments** and **DAG deployments**.+Service deployments occur when the Airflow service itself or any dependencies needs to be updated.+DAG deployments occur when DAG definitions or operational code has changed.++## Service deployments++The Catalog's Airflow instance is hosted via AWS's Elastic Compute Cloud (EC2) service.+The EC2 instance is managed by Terraform.+If a change is made to the service initialization bash script, Terraform will recognize this change and can deploy a new EC2 instance with the updated configuration.+This change does not occur automatically and will need to be manually initiated.++Currently the webserver, scheduler, and worker(s) are all run within a single docker container on the EC2 instance as defined by [the Airflow `Dockerfile` and related files](./docker/airflow).+The [`docker-compose.yml`](./docker-compose.yml) is used to spin up Airflow in production.++**Note**: Service deployments are only necessary in the following conditions:++- The Docker image has been modified+- An environment variable needs to be added/removed/changed+- A change to the infrastructure has been made (e.g. networking/routing changes)++For DAG updates and code-only changes, see the [DAG deployments section](#dag-deployments).++### Deployment workflow++The "π" icon below notes that the action must be performed by a core maintainer with infrastructure access.++1. [Cut a new release on GitHub](https://github.com/WordPress/openverse-catalog/releases).+2. After the release GitHub action has run, [check that the new Docker tag corresponding to the release exists on ghcr.io](https://github.com/wordpress/openverse-catalog/pkgs/container/openverse-catalog).+3. Ensure that there are [no actively running DAGs](https://airflow.openverse.engineering/dagrun/list/?_flt_3_state=running).+4. π Update the `docker_image_tag` in Terraform's configuration.+5. π Initiate the deployment via `just plan prod catalog-airflow` (and subsequently `just apply prod catalog-airflow` if the plan looks correct).++## DAG deployments++Due to the [nature of Airflow](https://airflow.apache.org/docs/apache-airflow/stable/dag-serialization.html), changes made to DAG definitions or operational code (e.g. any files that are used by the DAG) will be immediately picked up on the next task run for a DAG.+This means that we can update the python code in-place and the next DAG run or task in a currently running DAG will use the updated code.+In these cases, a new EC2 instance _does not_ need to be deployed.++The [`dag-sync.sh`](./dag-sync.sh) script is used in production to regularly update the repository (and thus the DAG files) on the running EC2 instance.++### Deployment workflow++Since this process happens automatically, the only precursor to a DAG deployment is merging the relevant PR into `main`.++_**Note**: New DAGs are not automatically enabled and will need to be turned on once deployed._++# Postgres++The Catalog is also backed by a Postgres instance, which serves as the primary data warehouse for all ingested media data.++This instance is hosted on AWS using [RDS](https://aws.amazon.com/rds/).+The instance can be accessed using [`pgcli`](https://www.pgcli.com/) or a similar client with the jumphost defined in the infrastructure repository.+For more information on this process see the [documentation on connecting to the jumphost](https://github.com/WordPress/openverse-infrastructure/blob/main/docs/CONNECTING-TO-JUMPHOST.md) (note that you will need additional access to view this document, please reach out to the maintainers if you're interested).++## Migrations++Any migrations to the Catalog database must either be performed by hand or as part of a DAG's normal operation (see: [iNaturalist](openverse_catalog/dags/providers/provider_api_scripts/inaturalist.py)).diff --git a/README.md b/README.mdindex 957ab4b2e..3a253882f 100644--- a/README.md+++ b/README.md@@ -73,7 +73,7 @@ loaded into a database to be indexed for searching in the Openverse API. These rdifferent environment than the PySpark portion of the project, and so have theirown dependency requirements.-For instructions geared specifically towards production deployments, see [DEPLOY.md](DEPLOY.md).+For instructions geared specifically towards production deployments, see [DEPLOYMENT.md](DEPLOYMENT.md)[api_scripts]: openverse_catalog/dags/providers/provider_api_scriptsdiff --git a/dag-sync.sh b/dag-sync.shnew file mode 100755index 000000000..e52e3293a--- /dev/null+++ b/dag-sync.sh@@ -0,0 +1,39 @@+#!/bin/bash+# DAG sync script used in production to update the repo files when invoked.+# This can be run using cron at the desired DAG sync interval.+#+# Inputs:+# - The first and only argument to the script should be the Slack hook URL target+# for the output sync message.+#+# Inspired by https://stackoverflow.com/a/21005490/3277713 via torek CC BY-SA 3.0+set -e++SLACK_URL=$1+# https://stackoverflow.com/a/246128 via Dave Dopson CC BY-SA 4.0+SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )+cd "$SCRIPT_DIR"++# Get current commit hash+current=$(git rev-parse HEAD)+# Update origin+git fetch origin+# Get new commit hash *one commit ahead* of this one+new=$(git rev-list --reverse --topo-order HEAD..origin/main | head -1)+# If there is no new commit hash to move to, nothing has changed, quit early+[ -z $new ] && exit+# Move ahead to this new commit+git reset --hard $new+# Pull out the subject from the new commit+subject=$(git log -1 --format='%s')+++if [ -z "$SLACK_URL" ]+ then+ echo "Slack hook was not supplied! Updates will not be posted"+else+ curl $SLACK_URL \+ -X POST \+ -H 'Content-Type: application/json' \+ -d '{"text":"Deployed: '"$subject"'","username":"DAG Sync","icon_emoji":":recycle:","blocks":[{"type":"section","text":{"type":"mrkdwn","text":"Deployed: <https://github.com/WordPress/openverse-catalog/commit/'"$new"'|'"$subject"'>"}}]}'+fi