Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Use a persistent container for `ov` (#4491)
sarayourfriendb4dd8a5280bc
diff --git a/docker/dev_env/Dockerfile b/docker/dev_env/Dockerfileindex ea6e18ab7..4336c266c 100644--- a/docker/dev_env/Dockerfile+++ b/docker/dev_env/Dockerfile@@ -60,8 +60,6 @@ RUN bash -c 'chmod -Rv 0777 /opt'# Avoid overwriting `.venv`'s from the hostENV PDM_VENV_IN_PROJECT="False"-COPY entrypoint.sh /entrypoint.sh--ENTRYPOINT ["/entrypoint.sh"]--CMD ["/bin/sh"]+# The fedora container's default entrypoint + command behaviour is to keep running+# so we do not need to modify entrypoint and command to use the container as a+# persistent environment.diff --git a/docker/dev_env/expand_aliases.py b/docker/dev_env/exec.pysimilarity index 71%rename from docker/dev_env/expand_aliases.pyrename to docker/dev_env/exec.pyindex 7d244f16e..a9d24f012 100644--- a/docker/dev_env/expand_aliases.py+++ b/docker/dev_env/exec.py@@ -21,8 +21,12 @@ def expand_aliases(args: list[str]):while args[0] in aliases:args = aliases.pop(args[0]) + args[1:]- print(*args)+ return argsif __name__ == "__main__":- expand_aliases(sys.argv[1:])+ args = expand_aliases(sys.argv[1:])+ try:+ os.execvp(args[0], args)+ except FileNotFoundError:+ sys.stdout.buffer.write(args[0].encode() + b": command not found\n")diff --git a/docker/dev_env/run.sh b/docker/dev_env/run.shindex 666d7bfe1..0aefa4e76 100755--- a/docker/dev_env/run.sh+++ b/docker/dev_env/run.sh@@ -2,22 +2,27 @@set -e-volume_name="openverse-dev-env"+container_name="openverse-dev-env"+volume_name="$container_name"if ! docker volume inspect openverse-dev-env &>/dev/null; thendocker volume create "$volume_name" 1>/dev/nullfi-run_args=(+shared_args=(-i- --rm--env "OPENVERSE_PROJECT=$OPENVERSE_PROJECT"--env "TERM=xterm-256color"+ --workdir "$OPENVERSE_PROJECT"+)++run_args=(+ -d+ --name "$container_name"--network host# Bind the repo to the same exact location inside the container so that pre-commit# and others don't get confused about where files are supposed to be-v "$OPENVERSE_PROJECT:$OPENVERSE_PROJECT:rw,z"- --workdir "$OPENVERSE_PROJECT"# Save the /opt directory of the container so we can reuse it each time--mount "type=volume,src=$volume_name,target=/opt"# Expose the host's docker socket to the container so the container can run docker/compose etc@@ -33,20 +38,38 @@ run_args=(# In other words, only tell Docker to attach a TTY to the container when# there's one to attach in the first place.if [ -t 0 ]; then- run_args+=(-t)+ shared_args+=(-t)+fi++_cmd=("$@")++if [ "$1" == "sudo" ]; then+ # Remove sudo+ # Linux hosts will run the command as root instead in the user check below.+ # macOS hosts don't need to bother, they're always running as root in the container anyway+ _cmd=("${@:2}")ficase "$OSTYPE" inlinux*)- run_args+=(--user "$UID:$(getent group docker | cut -d: -f3)")+ docker_group=$(getent group docker | cut -d: -f3)+ if [ "$1" == "sudo" ]; then+ user_id="0"+ else+ user_id="$UID"+ fi+ shared_args+=(--user "$user_id:$docker_group");;+darwin*)# noop, just catching them to avoid the fall-through error case;;+*)printf "Openverse development is only supported on Linux and macOS hosts. Please use WSL to run the Openverse development environment under Linux on Windows computers." >/dev/stderrexit 1;;+esacif command -v pnpm &>/dev/null; then@@ -55,10 +78,8 @@ if command -v pnpm &>/dev/null; then# Share the pnpm cache with the container, if it's available locallyif [ "$host_pnpm_store" != "" ]; thenpnpm_home="$(dirname "$host_pnpm_store")"- run_args+=(- --env PNPM_HOME="$pnpm_home"- -v "$pnpm_home:$pnpm_home:rw,z"- )+ shared_args+=(--env PNPM_HOME="$pnpm_home")+ run_args+=(-v "$pnpm_home:$pnpm_home:rw,z")fifi@@ -68,11 +89,19 @@ if command -v pdm &>/dev/null; then# if they're enabledif [ "$(pdm config --quiet install.cache)" == "True" ]; thenhost_pdm_cache="$(pdm config --quiet cache_dir)"- run_args+=(- --env "PDM_CACHE_DIR=$host_pdm_cache"- -v "$host_pdm_cache:$host_pdm_cache:rw,z"- )+ shared_args+=(--env "PDM_CACHE_DIR=$host_pdm_cache")+ run_args+=(-v "$host_pdm_cache:$host_pdm_cache:rw,z")fifi-docker run "${run_args[@]}" openverse-dev-env:latest "$@"+existing_container_id=$(docker ps -a --filter name="$container_name" -q)++if [ -z "$existing_container_id" ]; then+ docker run "${shared_args[@]}" "${run_args[@]}" openverse-dev-env:latest+else+ # Do not need to bother checking if the container is already running, docker start+ # is a noop in that case with no adverse effects+ docker start "$existing_container_id" 1>/dev/null+fi++docker exec "${shared_args[@]}" "$container_name" python3 docker/dev_env/exec.py "${_cmd[@]}"diff --git a/docker/dev_env/entrypoint.sh b/docker/dev_env/setup_env.shsimilarity index 80%rename from docker/dev_env/entrypoint.shrename to docker/dev_env/setup_env.shindex d8156b570..73ebcbb4b 100755--- a/docker/dev_env/entrypoint.sh+++ b/docker/dev_env/setup_env.sh@@ -9,10 +9,10 @@ ficd "$OPENVERSE_PROJECT" || exit 1-corepack install 1>/dev/null+corepack installif [ -z "$(n ls 2>/dev/null)" ]; then- printf "Installing the specific Node JS version required by Openverse frontend; this is only necessary the first time the toolkit runs\n"+ printf "Installing the specific Node JS version required by Openverse frontend\n"n install autofi@@ -26,7 +26,7 @@ pdm config venv.location "/opt/pdm/venvs"_python3s=(/opt/pdm/python/[email protected].*/bin/python3)if [ ! -x "${_python3s[0]}" ]; then- printf "Installing the specific Python version required for pipenv environments; this is only necessary the first time the toolkit runs\n"+ printf "Installing the specific Python version required for pipenv environments\n"pdm python install 3.11_python3s=(/opt/pdm/python/[email protected].*/bin/python3)fi@@ -49,6 +49,3 @@ fiif [ -n "$PDM_CACHE_DIR" ]; thenpdm config install.cache onfi--expanded_args=$(python3 "$OPENVERSE_PROJECT"/docker/dev_env/expand_aliases.py "$@")-bash -c "$expanded_args"diff --git a/ov b/ovindex 368ab831b..376432d94 100755--- a/ov+++ b/ov@@ -17,29 +17,43 @@ if [[ $_cmd == "help" || -z $_cmd ]]; thenOpenverse development toolkitUSAGE- ov <subcommand> [args]+ ov <subcommand> [...ARGS]COMMANDSov initInitialise the Openverse development toolkit for the first timeAlias for:- ov build && ov just install-hooks+ ov build && ov setup-env && ov just install-hooks && ov just init-ov-aliases- ov build- Build the Openverse development toolkit Docker image+ ov build [...ARGS]+ Build the Openverse development toolkit Docker image.+ ARGS are passed to `docker build`. See `docker build --help` for options.++ ov setup-env+ Setup the environment inside the container. `ov init` automatically runs this,+ and in most cases you should not need to run this manually. However, you may+ occasionally need to run it manually if tool requirements inside the container+ change. This can also be a helpful debugging step if tools in the container are+ not working as expected.++ ov stop+ Stop the persistent `ov` Docker container.ov clean- Remove the Openverse development toolkit Docker image and volume+ Remove the Openverse development toolkit Docker container, image and volume.Use in conjunction with `ov init` to recreate the environment fromscratch:ov clean && ov init+ Warning: running `ov clean` will result in needing to fully rebuild the environment+ including downloading tools handled by `ov setup-env` and pre-commit environments.+ov hook HOOKRun Git hooks through pre-commit inside the development toolkit container- ov COMMAND+ ov COMMAND [...ARGS]Run COMMAND inside the development toolkit containerHints: The toolkit comes loaded with many tools for working with Openverse! Trysome of the following:@@ -57,14 +71,27 @@ ficase "$_cmd" ininit)"$_self" build- "$_self" 'just install-hooks && just init-ov-aliases'+ "$_self" setup-env+ "$_self" just install-hooks+ "$_self" just init-ov-aliases;;build)- docker build -t openverse-dev-env:latest "$dev_env"+ docker build "${@:2}" -t openverse-dev-env:latest "$dev_env"+ ;;++setup-env)+ "$_self" bash "$dev_env"/setup_env.sh+ ;;++stop)+ docker stop openverse-dev-env;;clean)+ docker kill openverse-dev-env+ docker rm openverse-dev-env+ docker rmi openverse-dev-envdocker volume rm openverse-dev-env;;