Skip to content

Run apps in the mounted persistent storage directory - #50

Merged
jacob-curley-fnal merged 2 commits into
mainfrom
run_from_mounted_dir
Nov 10, 2026
Merged

jacob-curley-fnal merged 2 commits into
mainfrom
run_from_mounted_dir

Conversation

@jacob-curley-fnal

Copy link
Copy Markdown
Contributor

Summary

Apps built from this template now run from the launcher's persistent volume, /mnt/storage/ap-python-writable (the same path as AP_WRITABLE_STORAGE_DIR), instead of an arbitrary container directory. Local make targets mount a host directory at the same path so local runs behave like the launcher.

Changes

Dockerfile

  • Both runtime stages (runtime and xpra-runtime) create /mnt/storage/ap-python-writable, chown it to pyuser:pygroup, and set it as WORKDIR. This happens before USER pyuser, so the directory is writable when no volume is mounted.

docker/start.sh (GUI / Xpra image)

  • WRITABLE_DIR defaults from AP_WRITABLE_STORAGE_DIR, falling back to /mnt/storage/ap-python-writable.
  • app.log and xpra.log now default to ${WRITABLE_DIR}/… (previously /tmp). APP_LOG_FILE and XPRA_LOG_FILE still override them.
  • The script exits at startup with a clear error if the directory is missing or not writable. It then cds into it and exports AP_WRITABLE_STORAGE_DIR, so the app launched by Xpra starts there.
  • Xpra's runtime and socket directories intentionally stay in /tmp.

Makefile

  • run, run-gui, shell and shell-gui bind-mount STORAGE_HOST_DIR (default mount/<CONTAINER_NAME>/local) at /mnt/storage/ap-python-writable and set AP_WRITABLE_STORAGE_DIR.
  • New storage-dir target creates the host directory and makes it world-writable, because the container's UID 1000 may not match the host user.

Other

  • .gitignore ignores mount/.
  • Docs: docs/container.md (new local storage mount section; removed stale /home/pyuser and /tmp/*.log statements), docs/deployment.md, docs/application.md.
  • README.md: notes that the Use this template button only appears when signed in to GitHub as a member of the Fermi-AD organization.

Behavior changes to be aware of

  • Working directory: relative paths in your app now resolve inside persistent storage. Anything written there is kept after the launch ends, and there is no automatic cleanup.
  • Log location (GUI image): logs moved from /tmp/app.log and /tmp/xpra.log to the writable directory. They now persist.
  • Fail-fast: the GUI image exits at startup if the writable directory isn't writable by UID/GID 1000.
  • Launcher requirements: the mounted volume must be writable by UID/GID 1000 (for example via fsGroup), and the pod spec must not override workingDir.
  • No changes to the CI/CD workflow or to the app source.

Testing

  • make -n run run-gui shell shell-gui shows the expected docker run commands.
  • bash -n docker/start.sh passes.
  • Not yet verified: image builds, make run, make run-gui, and a launch on the cluster.

Migration for repositories already created from this template

Your repository has its own copy of these files, with names changed by the rename script, so don't overwrite them with the template versions. Apply the edits below by hand (or cherry-pick and resolve conflicts). Replace <project> with your project name.

  1. Dockerfile: in both the runtime and xpra-runtime stages, add the following immediately before USER pyuser:pygroup:

    RUN mkdir -p /mnt/storage/ap-python-writable \
     && chown pyuser:pygroup /mnt/storage/ap-python-writable
    
    WORKDIR /mnt/storage/ap-python-writable

    If you removed one of the stages, update only the one you kept.

  2. docker/start.sh (only if you use the gui-xpra image):

    • In the "Application and logging settings" block, replace the two hardcoded log paths:

      WRITABLE_DIR="${AP_WRITABLE_STORAGE_DIR:-/mnt/storage/ap-python-writable}"
      XPRA_LOG_FILE="${XPRA_LOG_FILE:-${WRITABLE_DIR}/xpra.log}"
      APP_LOG_FILE="${APP_LOG_FILE:-${WRITABLE_DIR}/app.log}"
    • After the configuration block and before cleanup(), add:

      if [ ! -d "${WRITABLE_DIR}" ] || [ ! -w "${WRITABLE_DIR}" ]; then
        echo "[start.sh] error: writable storage directory ${WRITABLE_DIR} is missing or not writable by $(id -un) (uid $(id -u))" >&2
        exit 1
      fi
      cd "${WRITABLE_DIR}"
      export AP_WRITABLE_STORAGE_DIR="${WRITABLE_DIR}"
    • Keep your own APP_CMD and APP_NAME values. Don't copy those lines from the template.

  3. Makefile:

    • Add these variables and add storage-dir to .PHONY:

      STORAGE_HOST_DIR ?= $(CURDIR)/mount/$(CONTAINER_NAME)/local
      STORAGE_CONTAINER_DIR := /mnt/storage/ap-python-writable
      DOCKER_STORAGE_ARGS = -v "$(STORAGE_HOST_DIR):$(STORAGE_CONTAINER_DIR)" -e AP_WRITABLE_STORAGE_DIR="$(STORAGE_CONTAINER_DIR)"
    • Add the new target:

      storage-dir:
      	@mkdir -p "$(STORAGE_HOST_DIR)"
      	@chmod a+rwx "$(STORAGE_HOST_DIR)" || echo "warning: could not chmod $(STORAGE_HOST_DIR); the container user (uid 1000) may be unable to write to it" >&2
    • Make run, run-gui, shell and shell-gui depend on storage-dir, and add $(DOCKER_STORAGE_ARGS) to each docker run command.

  4. .gitignore: add mount/ if it isn't already ignored.

  5. Your application code:

    • Search for places that write to the current directory or to hardcoded paths (/tmp, /home/pyuser, /app).
    • Persistent output should go to Path(os.environ["AP_WRITABLE_STORAGE_DIR"]). Anything written elsewhere is lost when the launch ends.
    • Output written to relative paths now lands in persistent storage. Decide whether to keep that or move scratch files to /tmp.
    • Anything that reads /tmp/app.log or /tmp/xpra.log (scripts, docs, support runbooks) must use the new location.
  6. Docs (optional): copy the relevant changes from docs/container.md, docs/deployment.md and docs/application.md if your repository still carries the template docs.

  7. Verify, with Docker available:

    make build && make run
    make build-gui && make run-gui   # GUI image only; confirm app.log and xpra.log appear in mount/<container-name>/local/

    No changes are needed to .github/workflows/ci-cd.yaml. Merge to main as usual to deliver. After delivery, confirm in the launcher that the app starts, can write to AP_WRITABLE_STORAGE_DIR, and (for the GUI) that logs appear in the launch's storage directory.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Code Coverage Report - 46 of 115 lines covered ( ⛔ 40.00%)

init.py - 0 of 0 lines covered ( ⛔ 0.00%)

  ⛔ This file is missing coverage.

acsys_client.py - 9 of 28 lines covered ( ⛔ 32.14%)

  Uncovered lines: ⚠️ 18-19, 21-24, 26-28, 30, 34, 36-38, 40-41, 45-46, 48

gui.py - 16 of 65 lines covered ( ⛔ 24.62%)

  Uncovered lines: ⚠️ 12-13, 20-23, 25-26, 28-31, 33-34, 36, 39, 42, 45, 51, 58-59, 61-65, 67-68, 70-73, 75, 77-82, 84-85, 87-88, 90-91, 95-98

main.py - 21 of 22 lines covered ( ✅ 95.45%)

  Uncovered lines: ⚠️ 47

@chowington chowington left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My comments:

Good stuff! A couple of requests (that aren't exactly related to this work!):

  1. Out of scope based on the task, but do you mind adding quick documentation about the new way to access the PVC data externally at https://ad-apps-internal.fnal.gov/ap-python/files/ ? Note that the trailing slash is currently required

  2. Could you please add a CHANGELOG? Based on conversation from other template repos, we've determined it's highly valuable for a template to have a changelog so that repos created from the template have a good way to know both (1) what template version their repo was created from (since they'll have a snapshot of the changelog at the time they made their repo) and (2) what updates they can bring in from the template as it improves


AI-assisted review:

Findings

1. major — Preserve permissions on existing host storage directories

Reference: Makefile:58–62, particularly chmod a+rwx.

The storage prerequisite unconditionally adds read, write, and search permissions for every host user to the selected directory, including existing directories supplied through STORAGE_HOST_DIR. Running a container or opening an image shell should not silently remove the privacy boundary of an existing host directory. Without a sticky bit, other local users who can traverse the parent directory can also remove or replace entries, even when the files themselves have restrictive modes.

Suggested fix: Preserve modes for existing directories and report incompatible access instead of broadening it. For newly created local storage, use ownership/group access or an explicitly opted-in permission policy; do not apply world-writable permissions indiscriminately to overrides.

Connor's comment: I'd change this just in case someone does run this locally rather than in a disposable dev container Update: not blocking based on in-person conversation with Jacob

2. minor — Resolve a relative writable-directory override before changing directory

Reference: docker/start.sh:27–30 and docker/start.sh:49–57.

The script accepts an existing relative writable-directory override and computes relative default log paths from it, then changes into that directory. Later log creation interprets those paths relative to the new working directory, effectively repeating the directory prefix. Startup exits before Xpra starts, even though the configured storage passed the initial existence and writability checks. The exported directory also remains relative to the changed working directory rather than identifying the validated directory.

Realistic trigger: Suppose the container starts in /mnt/storage/ap-python-writable, which contains a writable child directory named output, and the user sets AP_WRITABLE_STORAGE_DIR=output. The initial checks succeed. The script computes default log paths output/app.log and output/xpra.log, then changes into /mnt/storage/ap-python-writable/output. The later touch now targets /mnt/storage/ap-python-writable/output/output/app.log and /mnt/storage/ap-python-writable/output/output/xpra.log. Since the nested output directory does not exist, startup exits with status 1 before Xpra starts. The exported storage value also still says output, so application code resolving it from the new working directory would target that same unintended nested directory.

Suggested fix: After validating and entering the directory, replace the internal writable-directory value with the absolute current directory, then derive default log paths and export that resolved value. Alternatively, explicitly require and reject non-absolute values with a clear diagnostic.

Connor's comment: Not a blocker, but would be nice to fix

@chowington

Copy link
Copy Markdown
Contributor

Update after in-person conversation with Jacob: we'll pass on AI finding #1 given how OS's handle subdirectory permissions

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Code Coverage Report - 46 of 115 lines covered ( ⛔ 40.00%)

init.py - 0 of 0 lines covered ( ⛔ 0.00%)

  ⛔ This file is missing coverage.

acsys_client.py - 9 of 28 lines covered ( ⛔ 32.14%)

  Uncovered lines: ⚠️ 18-19, 21-24, 26-28, 30, 34, 36-38, 40-41, 45-46, 48

gui.py - 16 of 65 lines covered ( ⛔ 24.62%)

  Uncovered lines: ⚠️ 12-13, 20-23, 25-26, 28-31, 33-34, 36, 39, 42, 45, 51, 58-59, 61-65, 67-68, 70-73, 75, 77-82, 84-85, 87-88, 90-91, 95-98

main.py - 21 of 22 lines covered ( ✅ 95.45%)

  Uncovered lines: ⚠️ 47

@jacob-curley-fnal

Copy link
Copy Markdown
Contributor Author

Thanks for the review, Connor! I've added the changelog and updated the template version to 0.2.0. Also went ahead and added the note about the file read URL, so the docs are more complete

@chowington chowington left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, Jacob! Looks good 👍

@jacob-curley-fnal
jacob-curley-fnal merged commit 7ee9e68 into main Nov 10, 2026
4 checks passed
@jacob-curley-fnal
jacob-curley-fnal deleted the run_from_mounted_dir branch November 10, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants