Skip to content

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/.gitignore
index 28aec127b..6afe4b7d4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -98,7 +98,7 @@ celerybeat-schedule
*.sage.py
# Environments
-.env
+*.env
.venv
env/
venv/
diff --git a/README.md b/README.md
index 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
+### Requirements
You'll need `docker` and `docker-compose` installed on your machine, with
versions 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/activate
just 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:
```shell
just 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
```shell
diff --git a/docker-compose.yml b/docker-compose.yml
index 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.txt
env_file: .env
restart: always
ports:
diff --git a/docker/airflow/Dockerfile b/docker/airflow/Dockerfile
index 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 defaults
ARG REQUIREMENTS_FILE=requirements_prod.txt
# https://airflow.apache.org/docs/apache-airflow/2.1.2/installation.html#constraints-files
ARG CONSTRAINTS_FILE="https://raw.githubusercontent.com/apache/airflow/constraints-2.1.2/constraints-3.9.txt"
+
+# Path configurations
ENV AIRFLOW_HOME=/usr/local/airflow
-ENV AIRFLOW__CORE__DAGS_FOLDER=${AIRFLOW_HOME}/openverse_catalog/dags
ENV PATH=/usr/local/airflow/.local/bin:$PATH
+
+# Container optimizations
ENV PIPNOCACHEDIR=1
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV 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/Dockerfile
index 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_BUCKET
ENV SCRIPT_DIR=/app
WORKDIR ${SCRIPT_DIR}
diff --git a/docker/local_s3/create_bucket.py b/docker/local_s3/create_bucket.py
index 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.template
index 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_set
BROOKLYN_MUSEUM_API_KEY=not_set
DATA_GOV_API_KEY=not_set
@@ -9,31 +29,16 @@ FLICKR_API_KEY=not_set
JAMENDO_APP_KEY=not_set
NYPL_API_KEY=not_set
THINGIVERSE_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 DB
AIRFLOW__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-encoded
AIRFLOW_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 DB
AIRFLOW_CONN_POSTGRES_OPENLEDGER_UPSTREAM=postgres://deploy:deploy@postgres:5432/openledger
AIRFLOW_CONN_POSTGRES_OPENLEDGER_TESTING=postgres://deploy:deploy@postgres:5432/openledger
@@ -54,7 +59,26 @@ AIRFLOW_CONN_EMR_TEST=emr://?host=http://s3:5000
EMR_CONN_ID=emr_empty
EMR_TEST_CONN_ID=emr_test
-CCCATALOG_STORAGE_BUCKET=cccatalog-storage
+########################################################################################
+# Other config
+########################################################################################
+# External port airflow will be mounted to
AIRFLOW_PORT=9090
+# Minutes to wait until processing a file that hasn't been modified
LOADER_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_set
diff --git a/justfile b/justfile
index 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 files
lint:
diff --git a/openverse_catalog/dags/loader_workflow.py b/openverse_catalog/dags/loader_workflow.py
index 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 = 5
SCHEDULE_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.py
index 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_PROVIDER
AUDIO_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.py
index 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(