Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Split `justfile` to make it modular
Dhruv Bhanushalieebdf0ba4347
diff --git a/api/justfile b/api/justfilenew file mode 100644index 000000000..84f43022c--- /dev/null+++ b/api/justfile@@ -0,0 +1,109 @@+set dotenv-load := false++# Show all available recipes+@_default:+ echo "#######"+ echo "# API #"+ echo "#######"+ just --list --unsorted+++# Aliases _all-up from the parent directory+# https://github.com/casey/just/issues/1550+_all-up:+ just ../_all-up++# Install dependencies+install:+ pipenv install --dev++##########+# Health #+##########++# Check the health of the service+@health host="localhost:50280":+ -curl -s -o /dev/null -w '%{http_code}' 'http://{{ host }}/healthcheck/'++# Wait for the service to be healthy+@wait host="localhost:50280":+ # The just command on the second line is executed in the context of the+ # parent directory and so must be prefixed with `api/`.+ just ../_loop \+ '"$(just api/health {{ host }})" != "200"' \+ "Waiting for the API to be healthy..."++########+# cURL #+########++# Make a cURL GET request to service at the given path+_curl-get path host="localhost:50280":+ curl "http://{{ host }}/v1/{{ path }}"++# Make a test cURL GET request to the API+stats media="images" host="localhost:50280":+ just _curl-get "{{ media }}/stats/" {{ host }}++# Launch a `pgcli` shell in the web container (requires typing credentials)+pgcli db_host="db" db_name="openledger" db_user="deploy":+ just ../exec web pgcli -h {{ db_host }} openledger++#########################+# Django administration #+#########################++# Run Django administrative commands locally+dj-local +args="":+ pipenv run python manage.py {{ args }}++# Run Django administrative commands inside the Docker container+dj +args="": _all-up+ just ../exec web python manage.py {{ args }}++# Get IPython shell inside the Docker container+ipython:+ just dj shell++# Get DB shell inside the Docker container+dbshell:+ just dj dbshell++# Run `collectstatic` to prepare for building the `nginx` Dockerfile target.+collectstatic:+ # The STATIC_ROOT environment variable is relative to the Django container's+ # default working directory i.e. the `/api/` directory. The resulting output+ # will be `/api/static/`, which is ignored by Git for convenience.+ STATIC_ROOT="./static" just dj collectstatic --noinput++#########+# Tests #+#########++# Run API tests inside the Docker container+test *args: _all-up+ just ../exec web ./test/run_test.sh {{ args }}++# Run API tests locally+test-local *args:+ pipenv run ./test/run_test.sh {{ args }}++# Run smoke test for the API docs+doc-test: _all-up+ curl --fail 'http://localhost:50280/v1/?format=openapi'++##########+# Sphinx #+##########++# Compile Sphinx documentation into HTML output+sphinx-make: _all-up+ just ../exec web sphinx-build -M html docs/ build/++# Serve Sphinx documentation via a live-reload server+sphinx-live: _all-up+ just ../exec web sphinx-autobuild --host 0.0.0.0 --port 3000 docs/ build/html/++# Serve the Sphinx documentation from the HTML output directory+sphinx-serve dir="api" port="50231":+ cd {{ dir }}/build/html && pipenv run python -m http.server {{ port }}diff --git a/ingestion_server/justfile b/ingestion_server/justfilenew file mode 100644index 000000000..c5d19e5c1--- /dev/null+++ b/ingestion_server/justfile@@ -0,0 +1,66 @@+set dotenv-load := false++# Show all available recipes+@_default:+ echo "####################"+ echo "# Ingestion server #"+ echo "####################"+ just --list --unsorted+++# Install dependencies+install:+ pipenv install --dev++##########+# Health #+##########++# Check the health of the service+@health host="localhost:50281":+ -curl -s -o /dev/null -w '%{http_code}' 'http://{{ host }}/'++# Wait for the service to be healthy+@wait host="localhost:50281":+ # The just command on the second line is executed in the context of the+ # parent directory and so must be prefixed with `ingestion_server/`.+ just ../_loop \+ '"$(just ingestion_server/health {{ host }})" != "200"' \+ "Waiting for the ingestion-server to be healthy..."++########+# cURL #+########++# Make a cURL POST request to the service with the given data+_curl-post data host="localhost:50281":+ curl \+ -X POST \+ -H 'Content-Type: application/json' \+ -d '{{ data }}' \+ -w "\n" \+ 'http://{{ host }}/task'++# Load QA data into QA indices in Elasticsearch+load-test-data model="image":+ just _curl-post '{"model": "{{ model }}", "action": "LOAD_TEST_DATA"}'++# 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 }}"}'++# 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 }}"}'++# Delete an index in Elasticsearch+delete model="image" suffix="init" alias="image":+ just _curl-post '{"model": "{{ model }}", "action": "DELETE_INDEX", "index_suffix": "{{ suffix }}"}'++#########+# Tests #+#########++# Run ingestion-server tests locally+test-local *args:+ pipenv run ./test/run_test.sh {{ args }}diff --git a/justfile b/justfileindex 6e203f085..1a178f587 100644--- a/justfile+++ b/justfile@@ -1,13 +1,18 @@set dotenv-load := false+# Meaning of Just prefixes:+# @ - Quiet recipes (https://github.com/casey/just#quiet-recipes)+# _ - Private recipes (https://github.com/casey/just#private-recipes)+IS_PROD := env_var_or_default("PROD", "")IS_CI := env_var_or_default("CI", "")DC_USER := env_var_or_default("DC_USER", "opener")-# Show all available recipes-default:- @just --list --unsorted-+# Show all available recipes, also recurses inside nested justfiles+@_default:+ just --list --unsorted+ cd api && just+ cd ingestion_server && just############ Helpers #@@ -52,9 +57,9 @@ down flags="":# Recreate all volumes and containers from scratchrecreate:- @just down -v- @just up "--force-recreate --build"- @just init+ just down -v+ just up "--force-recreate --build"+ just init# Show logs of all, or named, Docker serviceslogs services="" args=(if IS_CI != "" { "" } else { "-f" }):@@ -68,7 +73,7 @@ EXEC_DEFAULTS := if IS_CI == "" { "" } else { "-T" }# Execute statement in service containers using Docker Composeexec +args:- docker-compose exec -u {{ DC_USER }} {{ EXEC_DEFAULTS }} {{ args }}+ just dc exec -u {{ DC_USER }} {{ EXEC_DEFAULTS }} {{ args }}######### Init #@@ -80,24 +85,23 @@ env:cp ingestion_server/env.template ingestion_server/.env# Ensure all services are up and running-@_api-up:+@_all-up:just up- just wait-for-ing- just wait-for-web+ just ingestion_server/wait+ just api/wait# Load sample data into the Docker Compose services-init: _api-up+init: _all-up./load_sample_data.sh-######## Dev ########-# Install Python dependencies in Pipenv environments+# Install all dependencies@install:- just _api-install- just _ing-install+ just api/install+ just ingestion_server/install# Setup pre-commit as a Git hookprecommit:@@ -120,14 +124,6 @@ precommit:lint:python3 pre-commit.pyz run --all-files-# Make locally trusted certificates-cert:- mkdir -p nginx/certs/- mkcert \- -cert-file nginx/certs/openverse.crt \- -key-file nginx/certs/openverse.key \- dev.openverse.test localhost 127.0.0.1 ::1-################## Elasticsearch ##################@@ -150,137 +146,3 @@ cert:just _loop \'"$(just check-index {{ index }})" != "1"' \"Waiting for index '{{ index }}' to be ready..."---####################-# Ingestion server #-####################--# Install dependencies for ingestion-server-_ing-install:- cd ingestion_server && pipenv install --dev--# Perform the given action on the given model by invoking the ingestion-server API-_ing-api data port="50281":- curl \- -X POST \- -H 'Content-Type: application/json' \- -d '{{ data }}' \- -w "\n" \- 'http://localhost:{{ port }}/task'--# Check the health of the ingestion-server-@ing-health ing_host:- -curl -s -o /dev/null -w '%{http_code}' 'http://{{ ing_host }}/'--# Wait for the ingestion-server to be healthy-@wait-for-ing ing_host="localhost:50281":- just _loop \- '"$(just ing-health {{ ing_host }})" != "200"' \- "Waiting for the ingestion-server to be healthy..."--# Load QA data into QA indices in Elasticsearch-@load-test-data model="image":- just _ing-api '{"model": "{{ model }}", "action": "LOAD_TEST_DATA"}'--# Load sample data into temp table in API and new index in Elasticsearch-@ingest-upstream model="image" suffix="init":- just _ing-api '{"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 _ing-api '{"model": "{{ model }}", "action": "PROMOTE", "index_suffix": "{{ suffix }}", "alias": "{{ alias }}"}'--# Delete an index in Elasticsearch-@delete model="image" suffix="init" alias="image":- just _ing-api '{"model": "{{ model }}", "action": "DELETE_INDEX", "index_suffix": "{{ suffix }}"}'--# Run ingestion-server tests locally-ing-testlocal *args:- cd ingestion_server && pipenv run ./test/run_test.sh {{ args }}---#######-# API #-#######--# Install dependencies for API-_api-install:- cd api && pipenv install --dev--# Check the health of the API-@web-health:- -curl -s -o /dev/null -w '%{http_code}' 'http://localhost:50280/healthcheck/'--# Wait for the API to be healthy-@wait-for-web:- just _loop \- '"$(just web-health)" != "200"' \- "Waiting for the API to be healthy..."--# Run smoke test for the API docs-api-doctest: _api-up- curl --fail 'http://localhost:50280/v1/?format=openapi'--# Run API tests inside Docker-api-test *args: _api-up- just exec web ./test/run_test.sh {{ args }}--# Run Django administrative commands locally-dj-local +args:- cd api && pipenv run python manage.py {{ args }}--# Run Django administrative commands in the docker container-@dj +args="": _api-up- just exec web python manage.py {{ args }}--# Make a test cURL request to the API-stats media="images":- curl "http://localhost:50280/v1/{{ media }}/stats/"--# Get Django shell with IPython-ipython:- just dj shell--# Run `collectstatic` to prepare for building the `nginx` Dockerfile target.-@collectstatic:- # The `STATIC_ROOT` setting is relative to the directory in which the Django- # container runs (i.e., the `api` directory at the root of the repository).- # The resulting output will be at `api/static` and is git ignored for convenience.- @STATIC_ROOT="./static" just dj collectstatic --noinput--# Run the nginx image locally-nginx upstream_url='api.openverse.engineering': collectstatic- # upstream_url can also be set to 172.17.0.1:50280 for local testing- cd api && docker build --target nginx . -t openverse-api-nginx:latest- @echo "--> NGINX server will be run at http://localhost:9090, upstream at {{ upstream_url }}"- @echo "--> Try a static URL like http://localhost:9090/static/admin/css/base.css to test"- docker run --rm -p 9090:8080 -it \- -e DJANGO_NGINX_UPSTREAM_URL="{{ upstream_url }}" \- -e DJANGO_NGINX_GIT_REVISION="$(git rev-parse HEAD)" \- openverse-api-nginx:latest--# Launch a psql shell in the web container-@dbshell:- just exec web python manage.py dbshell--# Launch a pgcli shell in the web container (require typing credentials)-@pgcli db_host="db":- just exec web pgcli -h {{ db_host }} openledger deploy---##########-# Sphinx #-##########--# Compile Sphinx documentation into HTML output-sphinx-make: _api-up- just exec web sphinx-build -M html docs/ build/--# Serve Sphinx documentation via a live-reload server-sphinx-live: _api-up- just exec web sphinx-autobuild --host 0.0.0.0 --port 3000 docs/ build/html/--# Serve the Sphinx documentation from the HTML output directory-sphinx-serve dir="api" port="50231":- cd {{ dir }}/build/html && pipenv run python -m http.server {{ port }}diff --git a/nginx/justfile b/nginx/justfilenew file mode 100644index 000000000..3fed12bea--- /dev/null+++ b/nginx/justfile@@ -0,0 +1,33 @@+set dotenv-load := false++# Show all available recipes+@_default:+ echo "#########"+ echo "# NGINX #"+ echo "#########"+ just --list --unsorted+++# Aliases collectstatic from the API directory+# https://github.com/casey/just/issues/1550+collectstatic:+ just ../api/collectstatic++# Run the NGINX image locally+build-run upstream_url='api.openverse.engineering': collectstatic+ # upstream_url can also be set to 172.17.0.1:50280 for local testing+ docker build --target nginx . -t openverse-api-nginx:latest+ @echo "--> NGINX server will be run at http://localhost:9090, upstream at {{ upstream_url }}"+ @echo "--> Try a static URL like http://localhost:9090/static/admin/css/base.css to test"+ docker run --rm -p 9090:8080 -it \+ -e DJANGO_NGINX_UPSTREAM_URL="{{ upstream_url }}" \+ -e DJANGO_NGINX_GIT_REVISION="$(git rev-parse HEAD)" \+ openverse-api-nginx:latest++# Make locally trusted certificates (requires mkcert installed)+cert:+ mkdir -p nginx/certs/+ mkcert \+ -cert-file nginx/certs/openverse.crt \+ -key-file nginx/certs/openverse.key \+ dev.openverse.test localhost 127.0.0.1 ::1