Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Make `ov` base image updates overall more convenient and take immediate effect (#4560)
sarayourfriend38709674a0e0
diff --git a/docker/dev_env/.gitignore b/docker/dev_env/.gitignorenew file mode 100644index 000000000..16d3c4dbb--- /dev/null+++ b/docker/dev_env/.gitignore@@ -0,0 +1 @@+.cachediff --git a/docker/dev_env/Dockerfile b/docker/dev_env/Dockerfileindex f324d13c7..0c07ebd6f 100644--- a/docker/dev_env/Dockerfile+++ b/docker/dev_env/Dockerfile@@ -17,8 +17,12 @@ ENV PDM_PYTHONS="${HOME}/pdm/bin"# location where `n` installs Node.js versionsENV N_PREFIX="${HOME}/n"+ENV PIPX_GLOBAL_HOME=/pipx++ENV PNPM_BIN="${HOME}/pnpm"+# Add tooling installed in custom locations to `PATH`.-ENV PATH="${N_PREFIX}/bin:${PDM_PYTHONS}:${HOME}/.local/bin:${PATH}"+ENV PATH="${PNPM_BIN}:${N_PREFIX}/bin:${PDM_PYTHONS}:${HOME}/.local/bin:${PATH}"# Dependency explanations:# - git: required by some python package; contributors should use git on their host@@ -39,7 +43,8 @@ ENV PATH="${N_PREFIX}/bin:${PDM_PYTHONS}:${HOME}/.local/bin:${PATH}"# Node dependencies:# - n: Node.js distribution manager# - corepack: Node.js package-manager-manager-RUN dnf -y install dnf-plugins-core \+RUN mkdir /pipx \+ && dnf -y install dnf-plugins-core \&& dnf -y config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo \&& dnf -y install \git \@@ -51,17 +56,16 @@ RUN dnf -y install dnf-plugins-core \python3.12 pipx \docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \&& dnf clean all \- && pipx install \+ && pipx install --global \httpie \pdm pipenv \+ pgcli \pre-commit \&& npm install -g \n \corepack \&& corepack enable-RUN bash -c 'chmod -Rv 0777 /opt'-# Avoid overwriting `.venv`'s from the hostENV PDM_VENV_IN_PROJECT="False"diff --git a/docker/dev_env/run.sh b/docker/dev_env/run.shindex 71c9ba1e0..8ca3a8f49 100755--- a/docker/dev_env/run.sh+++ b/docker/dev_env/run.sh@@ -3,9 +3,10 @@set -econtainer_name="openverse-dev_env"+image_name="$container_name"volume_name="openverse_dev-env"-if ! docker volume inspect openverse-dev_env &>/dev/null; then+if ! docker volume inspect "$container_name" &>/dev/null; thendocker volume create "$volume_name" 1>/dev/nullfi@@ -13,6 +14,12 @@ shared_args=(-i--env "OPENVERSE_PROJECT=$OPENVERSE_PROJECT"--env "TERM=xterm-256color"+ # Avoid overwriting `.venv`'s from the host+ --env "PDM_VENV_IN_PROJECT=False"+ # Install global pipx packages separate from user packages+ # Prevents permissions issue with packages installed as root+ # when running packages.sh+ --env "PIPX_GLOBAL_HOME=/pipx"--workdir "$(pwd)")@@ -72,11 +79,20 @@ darwin*)esac+# Ensure cache directory uses, `ov clean` destroys it+mkdir -p "$DEV_ENV"/.cache+if command -v pnpm &>/dev/null; then- host_pnpm_store="$(pnpm store path)"+ if [ ! -f "$DEV_ENV"/.cache/pnpm-store-path ]; then+ # This command can be slow, so cache the response+ # Should be safe, it's rare to change this, ov clean removes it otherwise+ pnpm store path >"$DEV_ENV"/.cache/pnpm-store-path+ fi++ host_pnpm_store=$(cat "$DEV_ENV"/.cache/pnpm-store-path)# Share the pnpm cache with the container, if it's available locally- if [ "$host_pnpm_store" != "" ]; then+ if [ -n "$host_pnpm_store" ]; thenpnpm_home="$(dirname "$host_pnpm_store")"shared_args+=(--env PNPM_HOME="$pnpm_home")run_args+=(-v "$pnpm_home:$pnpm_home:rw,z")@@ -84,20 +100,44 @@ if command -v pnpm &>/dev/null; thenfiif command -v pdm &>/dev/null; then+ if [ ! -f "$DEV_ENV"/.cache/pdm-cache-path ]; then+ # These commands can be slow, so cache the response+ # Should be safe, it's rare to change this+ if [ "$(pdm config --quiet install.cache)" == "True" ]; then+ pdm config --quiet cache_dir >"$DEV_ENV"/.cache/pdm-cache-path+ else+ echo "" >"$DEV_ENV"/.cache/pdm-cache-path+ fi+ fi++ host_pdm_cache=$(cat "$DEV_ENV"/.cache/pdm-cache-path)+# Share the PDM cache with the container, if it's available locally# --quiet so PDM doesn't repeatedly fill the console with update messages# if they're enabled- if [ "$(pdm config --quiet install.cache)" == "True" ]; then- host_pdm_cache="$(pdm config --quiet cache_dir)"+ if [ -n "$host_pdm_cache" ]; thenshared_args+=(--env "PDM_CACHE_DIR=$host_pdm_cache")run_args+=(-v "$host_pdm_cache:$host_pdm_cache:rw,z")fifiexisting_container_id=$(docker ps -a --filter name="$container_name" -q)+existing_container_image_id=$(docker ps -a --format="{{ .Image }}" -f name="$container_name")++run_container() {+ docker run "${shared_args[@]}" "${run_args[@]}" "$image_name":latest 1>/dev/null+}if [ -z "$existing_container_id" ]; then- docker run "${shared_args[@]}" "${run_args[@]}" openverse-dev_env:latest+ run_container+elif [ "$existing_container_image_id" != "$image_name":latest ]; then+ # If the existing container is running the latest image, it will be referred to as the ":latest" tag+ # Otherwise, $existing_container_image_id will be the actual image ID hash, in which case we aren't running the latest available image+ # Redirect output to stderr so that it isn't hidden but still won't affect piping or otherwise capturing stdout when running `ov`.+ printf "Detected a new version of the base image. Stopping the existing container and starting a new one with the latest base image.\n" >/dev/stderr+ docker stop "$existing_container_id" 1>/dev/stderr+ docker rm "$existing_container_id" 1>/dev/stderr+ run_containerelse# Do not need to bother checking if the container is already running, docker start# is a noop in that case with no adverse effectsdiff --git a/docker/dev_env/setup_env.sh b/docker/dev_env/setup_env.shindex 73ebcbb4b..89256fbbc 100755--- a/docker/dev_env/setup_env.sh+++ b/docker/dev_env/setup_env.sh@@ -2,6 +2,8 @@set -e+chmod -R 0777 /opt /pipx+if [ ! -d "$OPENVERSE_PROJECT"/.git ]; thenprintf "Repository not mounted to container!\n"exit 1@@ -16,8 +18,8 @@ if [ -z "$(n ls 2>/dev/null)" ]; thenn install autofi-if [ -n "$PNPM_HOME" ]; then- export PATH="$PNPM_HOME:$PATH"+if [[ -n $PNPM_HOME && ! -L $PNPM_BIN ]]; then+ ln -s "$PNPM_HOME" "$PNPM_BIN"fipdm config python.install_root "/opt/pdm/python"@@ -34,10 +36,8 @@ fiPYTHON_311=${_python3s[0]}export PYTHON_311-mkdir -p "$PDM_PYTHONS"--if [ ! -x "$PDM_PYTHONS"/python3.11 ]; then- ln -s "$PYTHON_311" "$PDM_PYTHONS"/python3.11+if [ ! -x /usr/local/bin/python3.11 ]; then+ ln -s "$PYTHON_311" /usr/local/bin/python3.11fiif [ -z "$(command -v pipenv)" ]; thendiff --git a/ov b/ovindex b1e12a3cf..d792e718f 100755--- a/ov+++ b/ov@@ -11,6 +11,7 @@ _self="$OPENVERSE_PROJECT/ov"_cmd="$1"dev_env="$OPENVERSE_PROJECT"/docker/dev_env+export DEV_ENV="$dev_env"if [[ $_cmd == "help" || -z $_cmd ]]; thencat <<-'EOF'@@ -81,7 +82,7 @@ build);;setup-env)- "$_self" bash "$dev_env"/setup_env.sh+ "$_self" sudo bash "$dev_env"/setup_env.sh;;stop)@@ -95,6 +96,7 @@ clean)docker container rm openverse-dev_env || truedocker image rm openverse-dev_env || truedocker volume rm openverse_dev-env || true+ rm -rf "$dev_env"/.cache;;hook)