Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Add justfile with scripts and update README #153

Merged
merged 9 commits into from
Sep 1, 2021
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,20 @@ own dependency 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.

To set up the local python environment along with the pre-commit hook, run:

```shell
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pre-commit install
```

Optionally, to install dependencies for your editor to introspect stuff about them:

```shell
pip install -r openverse_catalog/requirements_dev.txt
just install
```

To set up environment variables, navigate to the
[`openverse_catalog`][cc_airflow] directory, and run
To set up environment variables run:

```shell
cp env.template .env
just dotenv
```

If needed, fill in API keys or other secrets and variables in `.env`. This is
Expand All @@ -134,13 +128,14 @@ not needed if you only want to run the tests. There is a
[`openverse_catalog`][cc_airflow] directory, so from that directory, run

```shell
docker-compose up -d
just up
```

This results, among other things, in the following running containers:
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved

- `openverse_catalog_webserver_1`
- `openverse_catalog_postgres_1`
- `openverse_catalog_s3_1`

and some networking setup so that they can communicate. Note:

Expand All @@ -156,47 +151,47 @@ and some networking setup so that they can communicate. Note:
At this stage, you can run the tests via:

```shell
docker exec openverse_catalog_webserver_1 /usr/local/airflow/.local/bin/pytest
just test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this change!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, if you run the tests too early, before the airflow db loads, some tests fail:

AILED dags/util/test_operator_util.py::test_get_dated_main_runner_handles_zero_shift
FAILED dags/util/test_operator_util.py::test_get_dated_main_runner_handles_day_shift
FAILED dags/util/loader/test_paths.py::test_stage_oldest_tsv_file_finds_tsv_file
FAILED dags/util/loader/test_paths.py::test_stage_oldest_tsv_file_stages_tsv_file
FAILED dags/util/loader/test_paths.py::test_stage_oldest_tsv_file_removes_staged_file_from_output_dir
FAILED dags/util/loader/test_paths.py::test_stage_oldest_tsv_file_stages_older_file
FAILED dags/util/loader/test_paths.py::test_stage_oldest_tsv_file_ignores_newer_file
FAILED dags/util/loader/test_sql.py::test_create_loading_table_creates_table
FAILED dags/util/loader/test_sql.py::test_create_loading_table_errors_if_run_twice_with_same_id
FAILED dags/util/loader/test_sql.py::test_drop_load_table_drops_table - sqlal..

They pass after the db loads

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱 Does that mean we need to add a "wait for Postgres" script to the webserver entrypoint?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, or maybe just write something like 'wait a minute for db to load '?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to reproduce this. Can you share what steps you took for this to happen?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect it only happens when you first build the containers, because I tried to reproduce it now, and couldn't .

```

Edits to the source files or tests can be made on your local machine, then tests
can be run in the container via the above command to see the effects.

If you'd like, it's possible to login to the webserver container via
If you'd like, it's possible to login to the webserver container via:

```shell
docker exec -it openverse_catalog_webserver_1 /bin/bash
just shell
obulat marked this conversation as resolved.
Show resolved Hide resolved
```

It's also possible to attach to the running command process of the webserver
container via
If you just need to run an airflow command, you can use the `airflow` recipe. Arguments passed to airflow must be quoted:
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved

```shell
docker attach --sig-proxy=false openverse_catalog_webserver_1
just airflow "config list"
```

Attaching in this manner lets you see the output from both the Airflow webserver
and scheduler, which can be useful for debugging purposes. To leave the
container, (but keep it running), press `Ctrl-C` on \*nix platforms
To follow the logs of the running container:

```shell
just logs
```

To see the Airflow web UI, point your browser to `localhost:9090`.

If you'd like to bring down the containers, run

```shell
docker-compose down
just down
```

from the [`openverse_catalog`][cc_airflow] directory.

To reset the test DB (wiping out all databases, schemata, and tables), run

```shell
docker-compose down -v
just down -v
```

`docker volume prune` can also be useful if you've already stopped the running containers, but be warned that it will remove all volumes associated with stopped containers, not just openverse-catalog ones.

[justfile]: justfile
[dockercompose]: openverse_catalog/docker-compose.yml
[cc_airflow]: openverse_catalog/

Expand Down
37 changes: 37 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
set dotenv-load := false

DEV_DOCKER_FILES := "--file=openverse_catalog/docker-compose.yml --file=openverse_catalog/docker-compose.override.yml"
SERVICE := "webserver"


install:
pip install -r requirements.txt -r openverse_catalog/requirements_dev.txt
pre-commit install


dotenv:
@([ ! -f openverse_catalog/.env ] && cp openverse_catalog/env.template openverse_catalog/.env) || true
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved


up: dotenv
docker-compose {{ DEV_DOCKER_FILES }} up -d


down flags="":
docker-compose {{ DEV_DOCKER_FILES }} down {{ flags }}


logs: dotenv up
docker-compose {{ DEV_DOCKER_FILES }} logs -f


test: dotenv up
docker-compose {{ DEV_DOCKER_FILES }} exec {{ SERVICE }} /usr/local/airflow/.local/bin/pytest


shell: dotenv up
docker-compose {{ DEV_DOCKER_FILES }} exec {{ SERVICE }} /bin/bash


airflow command="": dotenv up
docker-compose {{ DEV_DOCKER_FILES }} exec {{ SERVICE }} airflow {{ command }}