Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Collect automations and ingestion_server to Sphinx docs (#2330)

Roman Sosiakc94dee4a7bf6
diff --git a/automations/README.md b/automations/README.md
deleted file mode 100644
index df24fd7b6..000000000
--- a/automations/README.md
+++ /dev/null
@@ -1,16 +0,0 @@
-# Automations
-
-This directory contains automations for managing the Openverse repositories and
-Developer Experience.
-
-## Requirements
-
-- Node v14
- - This should be kept in sync between the GitHub Actions `node-version` and
- the `volta.node` key in the `package.json` file.
-- Python 3.10
-- [Just](https://github.com/casey/just) command runner
-
-## Installing Dependencies
-
-Run `just install` to set up Python and JavaScript dependencies.
diff --git a/automations/js/justfile b/automations/js/justfile
index d8e7ab6d7..e23171a23 100644
--- a/automations/js/justfile
+++ b/automations/js/justfile
@@ -9,6 +9,9 @@ NO_COLOR := "\\033[0m"
printf "==============================================={{ NO_COLOR }}\n"
just --list --unsorted
+# Run a script
+run script *args:
+ pnpm --filter automations exec node src/{{ script }} {{ args }}
#################
# Weekly report #
@@ -16,37 +19,27 @@ NO_COLOR := "\\033[0m"
# Generate and post the weekly report
report:
- node src/last_week_tonight.mjs
+ just run last_week_tonight.mjs
##########
# Render #
##########
-# Run `render-jinja.js` with given input file, output file and context
-render in_file out_file ctx="{}":
- node src/render-jinja.js {{ in_file }} {{ out_file }} '{{ ctx }}'
-
-# Render `.pre-commit-config.yaml`
-render-precommit:
- just render templates/.pre-commit-config.local.yaml.jinja .pre-commit-config.yaml
-
-# Render `prettier.config.js`
-render-prettier:
- just render templates/prettier.config.local.js.jinja prettier.config.js
-
-# Render GitHub issue & PR templates
-render-github:
- just render templates/pull_request_template.md.jinja .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
-
render-release-drafter:
- just render templates/release-drafter.yml.jinja .github/release-drafter-api.yml '{ "app": "API" }'
- just render templates/release-drafter.yml.jinja .github/release-drafter-ingestion_server.yml '{ "app": "Ingestion server" }'
- just render templates/release-drafter.yml.jinja .github/release-drafter-frontend.yml '{ "app": "Frontend" }'
- just render templates/release-drafter.yml.jinja .github/release-drafter-catalog.yml '{ "app": "Catalog" }'
+ #! /usr/bin/env sh
+ render_release_drafter() {
+ slug=$1
+ name=$2
+ just run render-jinja.js \
+ templates/release-drafter.yml.jinja \
+ .github/release-drafter-$slug.yml \
+ "'{ \"app\": \"$name\" }'"
+ }
+ render_release_drafter api API
+ render_release_drafter ingestion_server "Ingestion Server"
+ render_release_drafter frontend Frontend
+ render_release_drafter catalog Catalog
# Render all templates (shortcut for easy iteration)
render-templates:
- just render-precommit
- just render-prettier
- just render-github
just render-release-drafter
diff --git a/automations/python/justfile b/automations/python/justfile
index 20948b398..f09911025 100644
--- a/automations/python/justfile
+++ b/automations/python/justfile
@@ -13,3 +13,7 @@ NO_COLOR := "\\033[0m"
# Install dependencies
install:
pipenv install --dev
+
+# Run a script
+run script *args:
+ pipenv run {{ script }} {{ args }}
diff --git a/documentation/automations/guides/index.md b/documentation/automations/guides/index.md
new file mode 100644
index 000000000..80c9d8bbf
--- /dev/null
+++ b/documentation/automations/guides/index.md
@@ -0,0 +1,7 @@
+# Guides
+
+```{toctree}
+:titlesonly:
+
+quickstart
+```
diff --git a/documentation/automations/guides/quickstart.md b/documentation/automations/guides/quickstart.md
new file mode 100644
index 000000000..59d287910
--- /dev/null
+++ b/documentation/automations/guides/quickstart.md
@@ -0,0 +1,60 @@
+# Quickstart Guide
+
+This is a quickstart guide for locally running Openverse repository automation
+scripts written in Node.js or Python.
+
+## Prerequisites
+
+Refer to the [general setup guide](/general/general_setup.md) for setting up the
+prerequisites. Refer to the 'Management' column in the
+[requirement matrix](/general/general_setup.md#requirement-matrix) to know what
+you need to run this.
+
+## Installation
+
+`just install` in the repository root directory will install all Python and
+Node.js dependencies for automations as well as other parts of the repository.
+If you wish to install only dependencies for automations, run the following:
+
+- `pipenv install` in `automations/python`
+- `pnpm install --filter=automations`
+
+## Running a Script
+
+### Python
+
+Run Python automation scripts using the `automations/python/run` just recipe.
+Ex.:
+
+```
+$ just automations/python/run print_labels.py
+```
+
+The recipe is an alias for running `pipenv run <script>` inside the
+`automations/python` directory. This facilitates correct `PYTHONPATH`
+configuration as expected by many scripts.
+
+### Node.js
+
+Run Node.js automation scripts using the `automations/js/run` just recipe. Ex.:
+
+```
+$ just automations/js/run render-jinja.js
+```
+
+The recipe is an alias for running `pnpm exec <script>` inside the
+`automations/js` directory. As such, any `automations/js` dependencies that make
+executable scripts available can also be run using the same recipe.
+
+## Environment Variables
+
+Many Openverse automation scripts expect to run inside a GitHub actions context
+and require specific environment variables to operate. Refer to individual
+script documentation (or implementation when no module documentation exists) to
+know which environment variables are expected.
+[Pipenv automatically loads `.env` files](https://pipenv-fork.readthedocs.io/en/latest/advanced.html#automatic-loading-of-env)
+so for ease of use, you may place relevant environment variables in
+`automations/python/.env`. Unlike Pipenv, however, pnpm does _not_ automatically
+load environment variables. If a Node.js script requires environment variables,
+you must pass them yourself via the command line or use another method to make
+the variables available to the script.
diff --git a/documentation/automations/index.md b/documentation/automations/index.md
new file mode 100644
index 000000000..d0e776809
--- /dev/null
+++ b/documentation/automations/index.md
@@ -0,0 +1,12 @@
+# Automations
+
+This section documents automations used to manage the Openverse repositories and
+Developer Experience.
+
+```{toctree}
+:titlesonly:
+:maxdepth: 2
+:glob:
+
+*/index
+```
diff --git a/documentation/automations/reference/index.md b/documentation/automations/reference/index.md
new file mode 100644
index 000000000..ca6f9b62d
--- /dev/null
+++ b/documentation/automations/reference/index.md
@@ -0,0 +1,7 @@
+# Reference
+
+```{toctree}
+:titlesonly:
+
+python_scripts
+```
diff --git a/documentation/automations/reference/python_scripts.md b/documentation/automations/reference/python_scripts.md
new file mode 100644
index 000000000..521bf7244
--- /dev/null
+++ b/documentation/automations/reference/python_scripts.md
@@ -0,0 +1,6 @@
+# Python Scripts
+
+| Name | Description |
+| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
+| [`issues_with_prs.py`](https://github.com/WordPress/openverse/blob/main/automations/python/issues_with_prs.py) | Move issue cards with linked PRs from one project column to another |
+| [`sync_labels.py`](https://github.com/WordPress/openverse/blob/main/automations/python/sync_labels.py) | Ensure the presence of a consistent set of labels on all repos |
diff --git a/documentation/general/general_setup.md b/documentation/general/general_setup.md
index 878f77429..845de9dd4 100644
--- a/documentation/general/general_setup.md
+++ b/documentation/general/general_setup.md
@@ -282,14 +282,21 @@ following the
[official instruction](https://github.com/FiloSottile/mkcert#installation). You
can run `mkcert -install` to verify your installation.
-### OpenSSL
+### `psycopg2` build prerequisites
```{note}
-This is only need if the `psycopg2` install fails with the `ld: library not
-found for -lssl` error when running the project outside Docker.
+This is only needed if the `psycopg2` installation fails when running parts of the project outside of Docker.
```
-This `psycopg2` package can fail to install on Apple Silicon Macs with the
+Openverse uses `psycopg2` built from source on the client for compatibility
+reasons. You must ensure that
+[`psycopg2`'s build prerequisites are fulfilled for the library to install correctly](https://www.psycopg.org/docs/install.html#build-prerequisites).
+The linked documentation includes common troubleshooting instrustions for issues
+building the library.
+
+#### macOS
+
+The `psycopg2` package can fail to install on Apple Silicon Macs with the
`ld: library not found for -lssl` error. To rectify this, install the
[`openssl` formula](https://formulae.brew.sh/formula/openssl@3) using
[Homebrew](https://brew.sh/) and set `LDFLAGS` and `CPPFLAGS` as per the
diff --git a/documentation/ingestion_server/guides/config.md b/documentation/ingestion_server/guides/config.md
new file mode 100644
index 000000000..46022aaad
--- /dev/null
+++ b/documentation/ingestion_server/guides/config.md
@@ -0,0 +1,8 @@
+# Configuration
+
+All configuration is performed through environment variables. See the
+`env.template` file for a comprehensive list of all environment variables. The
+ones with reasonable defaults have been commented out.
+
+Pipenv will automatically load `.env` files when running commands with
+`pipenv run`.
diff --git a/documentation/ingestion_server/guides/index.md b/documentation/ingestion_server/guides/index.md
index 5042a4da7..2ffe6f2ba 100644
--- a/documentation/ingestion_server/guides/index.md
+++ b/documentation/ingestion_server/guides/index.md
@@ -4,5 +4,7 @@
:titlesonly:
quickstart
+config
+mapping
test
```
diff --git a/documentation/ingestion_server/guides/mapping.md b/documentation/ingestion_server/guides/mapping.md
new file mode 100644
index 000000000..cadbac1b4
--- /dev/null
+++ b/documentation/ingestion_server/guides/mapping.md
@@ -0,0 +1,15 @@
+# Mapping database tables to Elasticsearch
+
+In order to synchronize a given table to Elasticsearch, the following
+requirements must be met:
+
+- The database table must have an autoincrementing integer primary key named
+ `id`.
+- A SyncableDoctype must be defined in `ingestion_server/elasticsearch_models`.
+ Refer to `SyncableDocType`'s doc string for required subclass implementation
+ details.
+- The table name must be mapped to the corresponding Elasticsearch
+ SyncableDoctype in `media_type_to_elasticsearch_model` map.
+
+See existing media type definitions in `ingestion_server/elasticsearch_models`
+for examples.
diff --git a/documentation/ingestion_server/guides/quickstart.md b/documentation/ingestion_server/guides/quickstart.md
index 51f59fdd4..bcd16d75f 100644
--- a/documentation/ingestion_server/guides/quickstart.md
+++ b/documentation/ingestion_server/guides/quickstart.md
@@ -43,9 +43,11 @@ you need to run this.
Now you should be able to access the following endpoints:
- - the list of ingestion jobs on
+ - The list of ingestion jobs on
[http://localhost:50281/task](http://localhost:50281/task)
+ You can view logs for the service using `just logs ingestion_server`.
+
## Shutting down
Refer to the [common instructions](/general/quickstart.md#shutting-down).
diff --git a/documentation/ingestion_server/guides/test.md b/documentation/ingestion_server/guides/test.md
index d1c0ba97e..6be29b6c5 100644
--- a/documentation/ingestion_server/guides/test.md
+++ b/documentation/ingestion_server/guides/test.md
@@ -2,7 +2,7 @@
Once you've made some changes to the codebase, it is important to run tests.
-## Steps
+## Ingestion tests
1. Ensure you've gone through the
[quickstart guide](/ingestion_server/guides/quickstart.md). Ensure that the
@@ -15,12 +15,28 @@ Once you've made some changes to the codebase, it is important to run tests.
```
```{caution}
- If you experience error installing `psycopg2`, refer to the documentation
- about the [OpenSSL prerequisite](/general/general_setup.md#openssl).
+ If you experience error installing `psycopg2`, refer to [documentation
+ about the `psycopg2` build prerequisites](/general/general_setup.md#psycopg2-build-prerequisites).
```
-3. Run the tests.
+3. Run the integration tests.
```console
$ just ingestion_server/test-local
```
+
+ Note that if an `.env` file exists in the folder you're running `just` from,
+ it may interfere with the integration test variables and cause unexpected
+ failures.
+
+## Making general test requests
+
+To make cURL requests to the local server:
+
+```bash
+just ingestion_server/curl-post '{"model": <model>, "action": <action>}'
+```
+
+Replace `<model>` and `<action>` with the correct values. For example, to
+download and index all new images, `<model>` will be `"image"` and `<action>`
+will be `"INGEST_UPSTREAM"`.
diff --git a/documentation/ingestion_server/index.md b/documentation/ingestion_server/index.md
index 070406d79..c7e0dde51 100644
--- a/documentation/ingestion_server/index.md
+++ b/documentation/ingestion_server/index.md
@@ -1,5 +1,8 @@
# Ingestion server
+Ingestion Server is a small private API for copying data from an upstream source
+and loading it into the Openverse API.
+
```{toctree}
:titlesonly:
:maxdepth: 2
diff --git a/documentation/ingestion_server/reference/data_refresh.md b/documentation/ingestion_server/reference/data_refresh.md
new file mode 100644
index 000000000..c781f901c
--- /dev/null
+++ b/documentation/ingestion_server/reference/data_refresh.md
@@ -0,0 +1,6 @@
+# Data Refresh Limit
+
+The `DATA_REFRESH_LIMIT` variable can be used to define a limit to the number of
+rows pulled from the upstream catalog database. If the server is running in an
+`ENVIRONMENT` that is not `prod` or `production`, this is automatically set to
+100k records.
diff --git a/documentation/ingestion_server/reference/elasticsearch.md b/documentation/ingestion_server/reference/elasticsearch.md
new file mode 100644
index 000000000..9f618ffc0
--- /dev/null
+++ b/documentation/ingestion_server/reference/elasticsearch.md
@@ -0,0 +1,20 @@
+# Elasticsearch and Indexing
+
+There is a multi-step process for copying data from an upstream source and
+loading it into the Openverse API using ingestion server:
+
+1. The data is copied from the upstream catalog database and into the downstream
+ API database into new tables.
+2. Data from the downstream API database gets indexed in Elasticsearch in new
+ indexes.
+3. Aliases in the API database and Elasticsearch are updated to point to the new
+ tables and indexes. We refer to this as "promotion".
+4. The previous API tables and Elasticsearch indexes are deleted.
+
+Performance is dependent on the size of the target Elasticsearch cluster,
+database throughput, and bandwidth available to the ingestion server. The
+primary bottleneck is indexing to Elasticsearch.
+
+## How indexing works
+
+![How indexing works](./howitworks.png)
diff --git a/documentation/ingestion_server/reference/howitworks.png b/documentation/ingestion_server/reference/howitworks.png
new file mode 100644
index 000000000..bada67b45
Binary files /dev/null and b/documentation/ingestion_server/reference/howitworks.png differ
diff --git a/documentation/ingestion_server/reference/index.md b/documentation/ingestion_server/reference/index.md
new file mode 100644
index 000000000..8b5749210
--- /dev/null
+++ b/documentation/ingestion_server/reference/index.md
@@ -0,0 +1,10 @@
+# Reference
+
+```{toctree}
+:titlesonly:
+
+elasticsearch
+safety
+notifications
+data_refresh
+```
diff --git a/documentation/ingestion_server/reference/notifications.md b/documentation/ingestion_server/reference/notifications.md
new file mode 100644
index 000000000..9ab6a3c86
--- /dev/null
+++ b/documentation/ingestion_server/reference/notifications.md
@@ -0,0 +1,5 @@
+# Notifications
+
+If a `SLACK_WEBHOOK` variable is provided, the ingestion server will provide
+periodic updates on the progress of a data refresh, or relay any errors that may
+occur during the process.
diff --git a/documentation/ingestion_server/reference/safety.md b/documentation/ingestion_server/reference/safety.md
new file mode 100644
index 000000000..053bf1e0f
--- /dev/null
+++ b/documentation/ingestion_server/reference/safety.md
@@ -0,0 +1,8 @@
+# Safety and Security Considerations
+
+The server has been designed to fail gracefully in the event of network
+interruptions, full disks, etc. If a task fails to complete successfully, the
+whole process is rolled back with zero impact to production.
+
+The server is designed to be run in a private network only. You must not expose
+the private Ingestion Server API to the public internet.
diff --git a/ingestion_server/justfile b/ingestion_server/justfile
index a7d1f5ed8..fda1656fc 100644
--- a/ingestion_server/justfile
+++ b/ingestion_server/justfile
@@ -62,7 +62,7 @@ wait-up: up
########
# Make a cURL POST request to the service with the given data
-_curl-post data host="localhost:50281":
+curl-post data host="localhost:50281":
STATUS_CODE=$(curl \
-X POST \
-H 'Content-Type: application/json' \
@@ -77,21 +77,21 @@ _curl-post data host="localhost:50281":
# Load sample data into temp table in API and new index in Elasticsearch
ingest-upstream model="image" suffix="init":
- just _curl-post '{"model": "{{ model }}", "action": "INGEST_UPSTREAM", "index_suffix": "{{ suffix }}"}'
+ just curl-post '{"model": "{{ model }}", "action": "INGEST_UPSTREAM", "index_suffix": "{{ suffix }}"}'
# Promote temp table to prod in API and new index to primary in Elasticsearch
promote model="image" suffix="init" alias="image":
- just _curl-post '{"model": "{{ model }}", "action": "PROMOTE", "index_suffix": "{{ suffix }}", "alias": "{{ alias }}"}'
+ just curl-post '{"model": "{{ model }}", "action": "PROMOTE", "index_suffix": "{{ suffix }}", "alias": "{{ alias }}"}'
# Delete an index in Elasticsearch
delete model="image" suffix="init" alias="image":
- just _curl-post '{"model": "{{ model }}", "action": "DELETE_INDEX", "index_suffix": "{{ suffix }}"}'
+ just curl-post '{"model": "{{ model }}", "action": "DELETE_INDEX", "index_suffix": "{{ suffix }}"}'
point-alias model suffix alias:
- just _curl-post '{"model": "{{ model }}", "action": "POINT_ALIAS", "index_suffix": "{{ suffix }}", "alias": "{{ alias }}"}'
+ just curl-post '{"model": "{{ model }}", "action": "POINT_ALIAS", "index_suffix": "{{ suffix }}", "alias": "{{ alias }}"}'
create-and-populate-filtered-index model="image" destination_suffix="init":
- just _curl-post '{"model": "{{ model }}", "action": "CREATE_AND_POPULATE_FILTERED_INDEX", "destination_index_suffix": "{{ destination_suffix }}"}'
+ just curl-post '{"model": "{{ model }}", "action": "CREATE_AND_POPULATE_FILTERED_INDEX", "destination_index_suffix": "{{ destination_suffix }}"}'
#########
# Tests #