Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Improve `.env` documentation & structure, update values (#251)
Madison Swain-Bowdend5d39f1ab65a
diff --git a/.gitignore b/.gitignoreindex 28aec127b..6afe4b7d4 100644--- a/.gitignore+++ b/.gitignore@@ -98,7 +98,7 @@ celerybeat-schedule*.sage.py# Environments-.env+*.env.venvenv/venv/diff --git a/README.md b/README.mdindex 83cacd6d0..fa2b855c3 100644--- a/README.md+++ b/README.md@@ -100,13 +100,15 @@ own dependency requirements.[api_scripts]: openverse_catalog/dags/provider_api_scripts-### Development setup+### RequirementsYou'll need `docker` and `docker-compose` installed on your machine, withversions new enough to use version `3` of Docker Compose `.yml` files.You will also need the [`just`](https://github.com/casey/just#installation) command runner installed.+### Setup+To set up the local python environment along with the pre-commit hook, run:```shell@@ -115,15 +117,34 @@ source venv/bin/activatejust install```+The containers will be built when starting the stack up for the first time.+If you'd like to build them prior to that, run:++```shell+just build+```++### Environment+To set up environment variables run:```shelljust dotenv```-If needed, fill in API keys or other secrets and variables in `.env`. This is-not needed if you only want to run the tests. There is a-[`docker-compose.yml`][dockercompose] provided in the+This will generate a `.env` file which is used by the containers.++The `.env` file is split into four sections:+1. Airflow Settings - these can be used to tweak various Airflow properties+2. API Keys - set these if you intend to test one of the provider APIs referenced+3. Connection/Variable info - this will not likely need to be modified for local development, though the values will need to be changed in production+4. Other config - misc. configuration settings, some of which are useful for local dev++The `.env` file does not need to be modified if you only want to run the tests.++### Running & Testing++There is a [`docker-compose.yml`][dockercompose] provided in the[`openverse_catalog`][cc_airflow] directory, so from that directory, run```shelldiff --git a/docker-compose.yml b/docker-compose.ymlindex f6a3f3cd0..74af7c2c2 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -6,9 +6,6 @@ services:build:context: .dockerfile: docker/airflow/Dockerfile- args:- - OUTPUT_DIR=${OUTPUT_DIR}- - REQUIREMENTS_FILE=requirements_prod.txtenv_file: .envrestart: alwaysports:diff --git a/docker/airflow/Dockerfile b/docker/airflow/Dockerfileindex 44eaf5b89..754eabfd5 100644--- a/docker/airflow/Dockerfile+++ b/docker/airflow/Dockerfile@@ -1,17 +1,29 @@FROM python:3.9-slim-ARG OUTPUT_DIR=/var/workflow_output/+# Build-time arguments, with sensible defaultsARG REQUIREMENTS_FILE=requirements_prod.txt# https://airflow.apache.org/docs/apache-airflow/2.1.2/installation.html#constraints-filesARG CONSTRAINTS_FILE="https://raw.githubusercontent.com/apache/airflow/constraints-2.1.2/constraints-3.9.txt"++# Path configurationsENV AIRFLOW_HOME=/usr/local/airflow-ENV AIRFLOW__CORE__DAGS_FOLDER=${AIRFLOW_HOME}/openverse_catalog/dagsENV PATH=/usr/local/airflow/.local/bin:$PATH++# Container optimizationsENV PIPNOCACHEDIR=1ENV DEBIAN_FRONTEND=noninteractiveENV PYTHONUNBUFFERED=1ENV PIP_NO_COLOR=1+# Airflow/workflow configuration+ENV OUTPUT_DIR=/var/workflow_output/+ENV AIRFLOW__CORE__DAGS_FOLDER=${AIRFLOW_HOME}/openverse_catalog/dags+ENV AIRFLOW__CORE__LOAD_EXAMPLES=False+ENV AIRFLOW__CORE__LOAD_DEFAULT_CONNECTIONS=False+ENV AIRFLOW__LOGGING__REMOTE_LOGGING=True+ENV AIRFLOW__LOGGING__REMOTE_LOG_CONN_ID=aws_default+ENV AIRFLOW__LOGGING__REMOTE_BASE_LOG_FOLDER=s3://openverse-airflow-logs/logs+RUN apt-get update && apt-get -yqq upgrade && apt-get -yqq install \build-essential \diff --git a/docker/local_s3/Dockerfile b/docker/local_s3/Dockerfileindex 1c630d0a0..37531c691 100644--- a/docker/local_s3/Dockerfile+++ b/docker/local_s3/Dockerfile@@ -1,6 +1,6 @@FROM python:3.9-ARG CCCATALOG_STORAGE_BUCKET+ARG OPENVERSE_BUCKETENV SCRIPT_DIR=/appWORKDIR ${SCRIPT_DIR}diff --git a/docker/local_s3/create_bucket.py b/docker/local_s3/create_bucket.pyindex 5e7a8c108..c7f5d3def 100644--- a/docker/local_s3/create_bucket.py+++ b/docker/local_s3/create_bucket.py@@ -23,7 +23,7 @@ S3 = boto3.resource()BUCKET_LIST = ["openverse-airflow-logs",- "cccatalog-storage",+ "openverse-storage","commonsmapper-v2","commonsmapper",]diff --git a/env.template b/env.templateindex 7f65cea75..89bfaae0e 100644--- a/env.template+++ b/env.template@@ -1,6 +1,26 @@-# Change OUTPUT_DIR as appropriate for production-OUTPUT_DIR=/tmp/+# ,-. ;-. ,--. . . . , ,--. ,-. ,-. ,--. ,-. ,-. . . ,--. , ,-. #+# / \ | ) | |\ | | / | | ) ( ` | / / \ |\ | | | / #+# | | |-' |- | \| | / |- |-< `-. |- | | | | \| |- | | -. #+# \ / | | | | |/ | | \ . ) | \ \ / | | | | \ | #+# `-' ' `--' ' ' ' `--' ' ' `-' `--' `-' `-' ' ' ' ' `-' #++########################################################################################+# Airflow Settings+########################################################################################+# Some brand-based suggestions: #C52B9B (pink), #FFE033 (yellow)+AIRFLOW__WEBSERVER__NAVBAR_COLOR="#FFF"+# Disabled by default to make development easier+# (enabled on prod for security)+AIRFLOW__CORE__HIDE_SENSITIVE_VAR_CONN_FIELDS=False+# Use the following python code to generate a fernet key for production+# python -c "import base64, os; print(base64.urlsafe_b64encode(os.urandom(32)).decode())"+# AIRFLOW__CORE__FERNET_KEY=+# Executor to use+AIRFLOW__CORE__EXECUTOR=LocalExecutor+########################################################################################+# API Keys+########################################################################################WALTERS_ART_MUSEUEM_KEY=not_setBROOKLYN_MUSEUM_API_KEY=not_setDATA_GOV_API_KEY=not_set@@ -9,31 +29,16 @@ FLICKR_API_KEY=not_setJAMENDO_APP_KEY=not_setNYPL_API_KEY=not_setTHINGIVERSE_TOKEN=not_set--S3_BUCKET=not_set-AWS_ACCESS_KEY=test_key-AWS_SECRET_KEY=test_secret-TEST_ACCESS_KEY=test_key-TEST_SECRET_KEY=test_secret+########################################################################################+# Connection/Variable info+########################################################################################+# Airflow primary metadata database# Change the following line in prod to use the appropriate DBAIRFLOW__CORE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres:5432/airflow-AIRFLOW__CORE__EXECUTOR=LocalExecutor-AIRFLOW__CORE__LOAD_EXAMPLES=False--# Enable remote logging in airflow-# https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#config-logging-remote-logging+# Remote logging connection ID# Replace "access_key" and "secret+key" with the real values. Secret key must be URL-encodedAIRFLOW_CONN_AWS_DEFAULT=aws://test_key:test_secret@?region_name=us-east-1&host=http://s3:5000-AIRFLOW__LOGGING__REMOTE_LOGGING=True-AIRFLOW__LOGGING__REMOTE_LOG_CONN_ID=aws_default-AIRFLOW__LOGGING__REMOTE_BASE_LOG_FOLDER=s3://openverse-airflow-logs/logs--# Uncomment the following line and set a Fernet key in prod using the output of-# python -c "import base64, os; print(base64.urlsafe_b64encode(os.urandom(32)).decode())"-# AIRFLOW__CORE__FERNET_KEY=-# Change the following line in prod to use the appropriate DBAIRFLOW_CONN_POSTGRES_OPENLEDGER_UPSTREAM=postgres://deploy:deploy@postgres:5432/openledgerAIRFLOW_CONN_POSTGRES_OPENLEDGER_TESTING=postgres://deploy:deploy@postgres:5432/openledger@@ -54,7 +59,26 @@ AIRFLOW_CONN_EMR_TEST=emr://?host=http://s3:5000EMR_CONN_ID=emr_emptyEMR_TEST_CONN_ID=emr_test-CCCATALOG_STORAGE_BUCKET=cccatalog-storage+########################################################################################+# Other config+########################################################################################+# External port airflow will be mounted toAIRFLOW_PORT=9090+# Minutes to wait until processing a file that hasn't been modifiedLOADER_FILE_AGE=1+# Contact email for any APIs+# This is where intermediate TSVs and other files will be written+# In production, this should be a permanent (non-temp) location+OUTPUT_DIR=/tmp/+# AWS/S3 configuration - does not need to be changed for development+AWS_ACCESS_KEY=test_key+AWS_SECRET_KEY=test_secret+TEST_ACCESS_KEY=test_key+TEST_SECRET_KEY=test_secret+# General bucket used for TSV->DB ingestion and logging+# TODO: Might need a separate bucket for logging+OPENVERSE_BUCKET=openverse-storage+# Used only for commoncrawl parsing+S3_BUCKET=not_setdiff --git a/justfile b/justfileindex ef0260c34..c92687704 100644--- a/justfile+++ b/justfile@@ -35,8 +35,8 @@ recreate: dotenv@just up "--force-recreate --build"# Show logs of all, or named, Docker services-logs: up- docker-compose {{ DEV_DOCKER_FILES }} logs -f+logs service="": up+ docker-compose {{ DEV_DOCKER_FILES }} logs -f {{ service }}# Run pre-commit on all fileslint:diff --git a/openverse_catalog/dags/loader_workflow.py b/openverse_catalog/dags/loader_workflow.pyindex c1d1c7a38..ec4dda091 100644--- a/openverse_catalog/dags/loader_workflow.py+++ b/openverse_catalog/dags/loader_workflow.py@@ -65,7 +65,7 @@ DAG_ID = "tsv_to_postgres_loader"OVERWRITE_DAG_ID = "tsv_to_postgres_loader_overwrite"DB_CONN_ID = os.getenv("OPENLEDGER_CONN_ID", "postgres_openledger_testing")AWS_CONN_ID = os.getenv("AWS_CONN_ID", "no_aws_conn_id")-CCCATALOG_STORAGE_BUCKET = os.getenv("CCCATALOG_STORAGE_BUCKET")+OPENVERSE_BUCKET = os.getenv("OPENVERSE_BUCKET")MINIMUM_FILE_AGE_MINUTES = int(os.getenv("LOADER_FILE_AGE", 15))CONCURRENCY = 5SCHEDULE_CRON = "* * * * *"@@ -92,7 +92,7 @@ def create_dag(postgres_conn_id=DB_CONN_ID,aws_conn_id=AWS_CONN_ID,output_dir=OUTPUT_DIR_PATH,- storage_bucket=CCCATALOG_STORAGE_BUCKET,+ storage_bucket=OPENVERSE_BUCKET,minimum_file_age_minutes=MINIMUM_FILE_AGE_MINUTES,overwrite=False,):diff --git a/openverse_catalog/dags/provider_api_scripts/wikimedia_commons.py b/openverse_catalog/dags/provider_api_scripts/wikimedia_commons.pyindex f599585b3..fc44bfeb8 100644--- a/openverse_catalog/dags/provider_api_scripts/wikimedia_commons.py+++ b/openverse_catalog/dags/provider_api_scripts/wikimedia_commons.py@@ -43,7 +43,7 @@ HOST = "commons.wikimedia.org"ENDPOINT = f"https://{HOST}/w/api.php"PROVIDER = prov.WIKIMEDIA_DEFAULT_PROVIDERAUDIO_PROVIDER = prov.WIKIMEDIA_AUDIO_PROVIDER-CONTACT_EMAIL = os.getenv("WM_SCRIPT_CONTACT")+CONTACT_EMAIL = os.getenv("CONTACT_EMAIL")UA_STRING = f"CC-Catalog/0.1 (https://creativecommons.org; {CONTACT_EMAIL})"DEFAULT_REQUEST_HEADERS = {"User-Agent": UA_STRING}DEFAULT_QUERY_PARAMS = {diff --git a/tests/dags/util/loader/test_s3.py b/tests/dags/util/loader/test_s3.pyindex 06cbf31f8..76a6d7ac5 100644--- a/tests/dags/util/loader/test_s3.py+++ b/tests/dags/util/loader/test_s3.py@@ -68,7 +68,7 @@ def test_copy_file_to_s3_staging_uses_bucket_environ(monkeypatch):tsv_file_path = "/test/file/path/to/data.tsv"aws_conn_id = "test_conn_id"test_bucket_name = "test-bucket"- monkeypatch.setenv("CCCATALOG_STORAGE_BUCKET", test_bucket_name)+ monkeypatch.setenv("OPENVERSE_BUCKET", test_bucket_name)with patch.object(s3.S3Hook, "load_file") as mock_s3_load_file:s3.copy_file_to_s3_staging(