diff --git a/.gitignore b/.gitignore index ee9ac8b..50e7e78 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,6 @@ Thumbs.db # Project-specific .uv/ + +# Local mounted directories +mount/ \ No newline at end of file diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..e4fba21 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..189d2b4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,160 @@ +# Changelog + +All notable changes to this template are documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html). The template version is the `version` field in [`pyproject.toml`](pyproject.toml). + +> **Created a project from this template?** This file describes changes to the *template*, not to your application. Use the migration guides below to bring template updates into your project, then replace this file with your own changelog if you like. Your project's `version` in `pyproject.toml` is yours to manage; it does not need to match the template version. + +## 0.2.0 + +This release starts the changelog and versioning of the template. Later template changes will be recorded here with migration notes for projects created from earlier versions. + +### Added + +- `CHANGELOG.md` (this file) and template versioning, starting at `0.2.0`. +- `.python-version` pinning the local Python version to `3.12` for uv and other tools. +- Local storage mount for the container Make targets. `make run`, `make run-gui`, `make shell`, and `make shell-gui` now bind-mount a host directory at `/mnt/storage/ap-python-writable` and set `AP_WRITABLE_STORAGE_DIR`, imitating the persistent volume the [AP Python Launcher](https://ad-apps-internal.fnal.gov/ap-python/) provides. + - The host directory defaults to `mount//local` in the repository. Override it with `STORAGE_HOST_DIR=...`. + - A new `make storage-dir` target creates the host directory and makes it world-writable, so the container user (UID 1000) can write to it whatever your host UID is. The run and shell targets depend on it. +- `mount/` added to `.gitignore`, so local storage directories are not committed. +- Docs: a new [Optional: local storage mount](docs/container.md#optional-local-storage-mount) section in `docs/container.md`, and more detail in [Persistent storage](docs/deployment.md#persistent-storage) about the working directory, log files, and file ownership. +- Docs: a new [View persisted files](docs/deployment.md#view-persisted-files) section explaining how to browse persisted data and logs outside a running launch at (the trailing slash is required). +- README: a note that the **Use this template** button appears only for signed-in members of the [Fermi-AD organization](https://github.com/fermi-ad). + +### Changed + +- **Both container images now run from the writable storage directory.** The CLI image (previously `/app`) and the GUI/Xpra image (previously `/home/pyuser`) now use `/mnt/storage/ap-python-writable` as their `WORKDIR`. The image creates this directory and gives ownership to `pyuser:pygroup`. In the launcher, the persistent volume is mounted there, so relative paths resolve inside persistent storage. +- **GUI image logs are now persistent.** `docker/start.sh` writes `app.log` and `xpra.log` to the writable directory (`$AP_WRITABLE_STORAGE_DIR`, default `/mnt/storage/ap-python-writable`) instead of `/tmp`. They are kept after the launch ends and are not cleaned up automatically. `APP_LOG_FILE` and `XPRA_LOG_FILE` still override the paths. +- `docker/start.sh` now: + - exits with an error if the writable directory is missing or not writable by the container user; + - changes into that directory before starting Xpra, so the application starts there; + - exports `AP_WRITABLE_STORAGE_DIR` so the application can find the directory the same way it does in the launcher. +- Xpra runtime directories (sockets and `chmod 700` directories) stay under `/tmp`; they do not belong on the shared volume. +- Docs (`docs/application.md`, `docs/deployment.md`) now recommend reading `AP_WRITABLE_STORAGE_DIR` explicitly rather than relying on the working directory. + +### Migration guide: 0.1.0 → 0.2.0 + +These steps apply to projects created from the 0.1.0 template. Your project has probably been renamed with `make rename-project`, so wherever the template uses `ap-python-starter-kit` or `ap_python_starter_kit`, use your project's names instead. The easiest way to see each change is to compare the files below with the template repository at 0.2.0. + +#### 1. Update the `Dockerfile` + +In **both** runtime stages (the CLI stage and the Xpra/GUI stage), replace the old `WORKDIR` with: + +```dockerfile +RUN mkdir -p /mnt/storage/ap-python-writable \ + && chown pyuser:pygroup /mnt/storage/ap-python-writable + +WORKDIR /mnt/storage/ap-python-writable +``` + +- CLI stage: this replaces `WORKDIR /app`. +- GUI stage: this replaces `WORKDIR /home/pyuser` and the comment above it. + +Keep the `RUN` before `USER pyuser:pygroup`, because it needs root to run `chown`. + +#### 2. Update `docker/start.sh` (GUI image only) + +1. Under "Application and logging settings," add the writable directory setting and change the log defaults: + + ```bash + # Persistent writable directory (mounted by the launcher). The application runs + # from here, and the logs below are written here by default. + WRITABLE_DIR="${AP_WRITABLE_STORAGE_DIR:-/mnt/storage/ap-python-writable}" + # Paths for Xpra and application logs. + XPRA_LOG_FILE="${XPRA_LOG_FILE:-${WRITABLE_DIR}/xpra.log}" + APP_LOG_FILE="${APP_LOG_FILE:-${WRITABLE_DIR}/app.log}" + ``` + +2. After the configuration block (just before the `cleanup()` function), add the directory check and `cd`: + + ```bash + 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 + + # Run the application (launched by Xpra) from the writable directory and let it + # discover the location through the same variable the launcher provides. + cd "${WRITABLE_DIR}" + export AP_WRITABLE_STORAGE_DIR="${WRITABLE_DIR}" + ``` + + Leave your `APP_CMD` default unchanged. + +#### 3. Update the `Makefile` (optional, recommended) + +To test with local persistent storage the same way the launcher provides it: + +1. After the `XPRA_BIND_HOST` setting, add: + + ```makefile + # Local stand-in for the launcher's persistent volume. This host directory is + # mounted at the same path the launcher uses; override STORAGE_HOST_DIR to change it. + 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)" + ``` + +2. Add `storage-dir` to `.PHONY`, and add the target: + + ```makefile + # Create the local storage directory. The container user (uid 1000) must be able + # to write to it regardless of the host user's uid, so it is made world-writable. + 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 + ``` + +3. Make `run`, `run-gui`, `shell`, and `shell-gui` depend on `storage-dir` (for example `run: storage-dir`), and add `$(DOCKER_STORAGE_ARGS)` to each of their `docker run` commands. + +4. Optionally, add the `STORAGE_HOST_DIR` note to the `help` output. + +Makefile recipe lines must start with a tab, not spaces. + +#### 4. Update `.gitignore` and add `.python-version` + +- Append to `.gitignore`: + + ```gitignore + # Local mounted directories + mount/ + ``` + +- Add a `.python-version` file containing `3.12`, unless your project already pins a different version. + +#### 5. Check your application code + +- **If your app writes files with relative paths**, they now go to `/mnt/storage/ap-python-writable` in the container. In the launcher, that means persistent storage instead of temporary storage. Check that this is what you want, and remember that persistent data is never cleaned up automatically. +- **If your app or users relied on `/home/pyuser` (GUI) or `/app` (CLI)** as the working directory, update those paths. For the GUI image, Xpra's file upload and download dialogs now open in the writable directory rather than the home directory. +- **If you read the GUI logs from `/tmp/app.log` or `/tmp/xpra.log`**, use `app.log` and `xpra.log` in the writable directory instead, or set `APP_LOG_FILE` / `XPRA_LOG_FILE` to keep the old paths. After a launch ends, you can read these logs in the launcher's [file browser](https://ad-apps-internal.fnal.gov/ap-python/files/) (keep the trailing slash). +- **For persistent data, read `AP_WRITABLE_STORAGE_DIR`** rather than relying on the working directory; see [Persistent storage](docs/deployment.md#persistent-storage). +- **If you run images with `docker run` yourself** and mount a directory at `/mnt/storage/ap-python-writable`, make sure UID 1000 can write to it. Otherwise the GUI image exits at startup with a "not writable" error. + +#### 6. Update docs (optional) + +If you kept the template's docs, update `docs/container.md`, `docs/deployment.md`, `docs/application.md`, and `README.md` to match the 0.2.0 template. + +#### 7. Rebuild and verify + +```bash +make build # or: make build-gui +make run # or: make run-gui +ls mount/*/local # files your app writes (and, for the GUI image, app.log and xpra.log) +``` + +## 0.1.0 + +First version of the AP Python template: the initial work to set up a template repository for Fermilab Accelerator Division Python applications. It was not versioned or tagged at the time; `0.1.0` refers to the template state before this changelog was added. + +### Added + +- A Python 3.12+ uv project (`ap-python-starter-kit`) with a `src/` layout, an installed CLI entry point, and a committed `uv.lock`. +- An example application that reads devices through [ACSys](https://github.com/fermi-ad/acsys-python) (`acsys_client.py`, `main.py`), and an optional PyQt6 desktop window (`--gui`, `gui-pyqt` extra). +- `scripts/rename_project.py` (`make rename-project`) to rename the starter package, CLI command, Docker image names, and docs references to your project. +- pytest tests for the example app and rename script, Ruff linting and formatting, and pre-commit hooks. +- A multi-stage `Dockerfile` with two runtime targets: a CLI/headless image and a GUI image served to the browser through Xpra HTML (`docker/start.sh`). +- A `Makefile` with targets to sync, test, lint, format, build, run, and open shells in the images. +- DevPod and dev container configuration, with Fermilab Kerberos defaults. +- GitHub workflows that hand checks, image builds, and delivery to Harbor off to externally maintained reusable workflows, for deployment through the AP Python Launcher. +- Documentation: quickstart, DevPod, customizing the application, everyday development, container usage, CI and deployment (including persistent storage through `AP_WRITABLE_STORAGE_DIR`), Kerberos, and migrating an existing Python project. diff --git a/Dockerfile b/Dockerfile index 47c9ceb..4dbb05a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -67,7 +67,10 @@ LABEL org.opencontainers.image.title="ap-python-template-cli" COPY --from=builder /usr/local /usr/local -WORKDIR /app +RUN mkdir -p /mnt/storage/ap-python-writable \ + && chown pyuser:pygroup /mnt/storage/ap-python-writable + +WORKDIR /mnt/storage/ap-python-writable USER pyuser:pygroup @@ -114,9 +117,11 @@ COPY --from=xpra-builder /usr/local /usr/local # Copy Xpra startup script with execute permissions COPY --chmod=755 docker/start.sh /usr/local/bin/start.sh -# Run from user's home dir so Xpra's file upload/download dialog opens to the -# same dir as the app's generated files -WORKDIR /home/pyuser +RUN mkdir -p /mnt/storage/ap-python-writable \ + && chown pyuser:pygroup /mnt/storage/ap-python-writable + +# Run from the mounted shared directory +WORKDIR /mnt/storage/ap-python-writable # Switch to non-root user for runtime security USER pyuser:pygroup diff --git a/Makefile b/Makefile index e10fd25..e194e0a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,13 @@ CONTAINER_NAME ?= ap-python-starter-kit XPRA_PORT ?= 14500 XPRA_BIND_HOST ?= 0.0.0.0 -.PHONY: help build build-gui build-no-cache docs-serve format lint rename-project run run-gui run-uv shell shell-gui test uv-sync clean +# Local stand-in for the launcher's persistent volume. This host directory is +# mounted at the same path the launcher uses; override STORAGE_HOST_DIR to change it. +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)" + +.PHONY: help build build-gui build-no-cache docs-serve format lint rename-project run run-gui run-uv shell shell-gui storage-dir test uv-sync clean help: @echo "Available targets:" @@ -19,6 +25,8 @@ help: @echo " make rename-project Rename project (update template names in files and src/ package, and rename Docker images in Makefile)" @echo " make run [APP_CMD=...] Run CLI container (defaults to template CLI launcher)" @echo " make run-gui [XPRA_PORT=14500] [APP_CMD=...] Run GUI + Xpra HTML container (integrated PyQt app)" + @echo " (run, run-gui, shell, shell-gui mount $(STORAGE_HOST_DIR)" + @echo " at $(STORAGE_CONTAINER_DIR); override with STORAGE_HOST_DIR=...)" @echo " make run-uv [APP_CMD=...] Run project locally using uv (defaults to template CLI launcher)" @echo " make shell Open an interactive shell in CLI container" @echo " make shell-gui Open an interactive shell in GUI + Xpra container" @@ -47,14 +55,22 @@ lint: rename-project: python3 scripts/rename_project.py -run: +# Create the local storage directory. The container user (uid 1000) must be able +# to write to it regardless of the host user's uid, so it is made world-writable. +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 + +run: storage-dir @docker run --rm --name $(CONTAINER_NAME) \ + $(DOCKER_STORAGE_ARGS) \ $(if $(APP_CMD),-e APP_CMD="$(APP_CMD)") \ $(IMAGE_NAME) -run-gui: +run-gui: storage-dir @docker run --rm --name $(CONTAINER_NAME)-xpra \ -p $(XPRA_PORT):14500 \ + $(DOCKER_STORAGE_ARGS) \ -e XPRA_BIND_HOST="$(XPRA_BIND_HOST)" \ $(if $(APP_CMD),-e APP_CMD="$(APP_CMD)") \ $(IMAGE_NAME_GUI) @@ -63,11 +79,11 @@ run-gui: run-uv: uv run $(APP_CMD) -shell: - docker run --rm -it --entrypoint /bin/bash $(IMAGE_NAME) +shell: storage-dir + docker run --rm -it $(DOCKER_STORAGE_ARGS) --entrypoint /bin/bash $(IMAGE_NAME) -shell-gui: - docker run --rm -it --entrypoint /bin/bash $(IMAGE_NAME_GUI) +shell-gui: storage-dir + docker run --rm -it $(DOCKER_STORAGE_ARGS) --entrypoint /bin/bash $(IMAGE_NAME_GUI) test: uv run pytest diff --git a/README.md b/README.md index 1e57404..a68def4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This Python 3.12+ repository is a **template for your own application**. Create ## Start here -1. Select **Use this template → Create a new repository** above, then develop in the repository you create. +1. Select **Use this template → Create a new repository** above, then develop in the repository you create. The **Use this template** button only appears if you are signed in to GitHub as a member of the [Fermi-AD organization](https://github.com/fermi-ad). If you do not see it, check that you are signed in with the correct account and are a member of the organization. 2. Open your repository in DevPod using the [workspace setup guide](docs/devpod.md#start-a-workspace). Its development image is maintained externally and supplies the common tools used to check your app. Keep the project's uv configuration working so the maintainer-run integration workflow can install and check it; you do not maintain the image or the workflow. 3. In the workspace terminal, check the example command without connecting to ACSys: @@ -27,3 +27,4 @@ This Python 3.12+ repository is a **template for your own application**. Create - [CI and deployment](docs/deployment.md) — maintained workflow path, image choice, Harbor GitHub App contacts, launcher resource limits, and persistent storage. - [Fermilab Kerberos defaults](docs/kerberos.md) — control-system authentication versus configuration and runtime tickets. - [Migrating an existing Python project](docs/migration.md) — adapt existing code and dependencies. +- [Changelog](CHANGELOG.md) — template versions and migration guides for projects created from earlier versions. diff --git a/docker/start.sh b/docker/start.sh index 551fcca..e63900a 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -22,9 +22,12 @@ XPRA_BIND_PORT="${XPRA_BIND_PORT:-14500}" ### Application and logging settings. # Command launched by Xpra. APP_CMD="${APP_CMD:-python -m ap_python_starter_kit.main --gui}" +# Persistent writable directory (mounted by the launcher). The application runs +# from here, and the logs below are written here by default. +WRITABLE_DIR="${AP_WRITABLE_STORAGE_DIR:-/mnt/storage/ap-python-writable}" # Paths for Xpra and application logs. -XPRA_LOG_FILE="${XPRA_LOG_FILE:-/tmp/xpra.log}" -APP_LOG_FILE="${APP_LOG_FILE:-/tmp/app.log}" +XPRA_LOG_FILE="${XPRA_LOG_FILE:-${WRITABLE_DIR}/xpra.log}" +APP_LOG_FILE="${APP_LOG_FILE:-${WRITABLE_DIR}/app.log}" ### Xpra lifecycle settings. # Stop Xpra when the launched application process (APP_CMD) exits. @@ -43,6 +46,16 @@ XPRA_RUN_DIR_DEFAULT="${XPRA_RUN_DIR:-${RUNTIME_BASE}/xpra}" USER_RUN_DIR_DEFAULT="${USER_RUN_DIR:-${RUNTIME_BASE}/user-1000}" ############################################################################### +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 + +# Run the application (launched by Xpra) from the writable directory and let it +# discover the location through the same variable the launcher provides. +cd "${WRITABLE_DIR}" +export AP_WRITABLE_STORAGE_DIR="${WRITABLE_DIR}" + cleanup() { echo "[start.sh] shutting down" xpra stop "${XPRA_DISPLAY}" >/dev/null 2>&1 || true diff --git a/docs/application.md b/docs/application.md index 6fff4c8..1b7307c 100644 --- a/docs/application.md +++ b/docs/application.md @@ -12,7 +12,7 @@ The rename script updates the example package paths, links, and installed comman 1. **Choose what your app does.** Decide what should happen when someone runs your installed command, what inputs it needs, and whether it needs a window. You can start with a simple command that prints a result and expand from there. 2. **Edit your package's entry point.** Open your package's [`main.py`](../src/ap_python_starter_kit/main.py). Change its argument parser and default behavior to run **your** application. Replace the example `--device` flag, ACSys request, five-reading loop, and `--gui` switch if they do not fit. Keep a callable entry point returning an exit status, or change the `[project.scripts]` mapping in [`pyproject.toml`](../pyproject.toml) to your new callable. Then `uv sync --dev` refreshes the installed command. Running your command with `--help` is a useful check if you retain an argument parser. -3. **Adapt the demo code and add what your app needs.** The example [`acsys_client.py`](../src/ap_python_starter_kit/acsys_client.py) wraps ACSys DPM calls for the sample request. You can adapt that wrapper or call ACSys from your own code. If you do not use the wrapper, update its imports and clean up unused demo code so the launcher starts your code. Add other libraries as needed, then run `uv sync` and review changes to [`uv.lock`](../uv.lock); see [Dependencies and environment](development.md#dependencies-and-environment). If your app needs to keep data after it exits, write it to the directory in `AP_WRITABLE_STORAGE_DIR`; see [Persistent storage](deployment.md#persistent-storage). +3. **Adapt the demo code and add what your app needs.** The example [`acsys_client.py`](../src/ap_python_starter_kit/acsys_client.py) wraps ACSys DPM calls for the sample request. You can adapt that wrapper or call ACSys from your own code. If you do not use the wrapper, update its imports and clean up unused demo code so the launcher starts your code. Add other libraries as needed, then run `uv sync` and review changes to [`uv.lock`](../uv.lock); see [Dependencies and environment](development.md#dependencies-and-environment). If your app needs to keep data after it exits, write it to the directory in `AP_WRITABLE_STORAGE_DIR`; see [Persistent storage](deployment.md#persistent-storage). Deployed apps start in that directory, but anything written outside it is lost when the launch ends, so use `AP_WRITABLE_STORAGE_DIR` explicitly rather than relying on the working directory. 4. **Decide whether you want a desktop window.** For a CLI or headless app, remove the unused GUI branch and module, and remove the `gui-pyqt` extra if you no longer need PyQt. For a desktop app, customize the example window and update thread in [`gui.py`](../src/ap_python_starter_kit/gui.py) for your own UI and data flow; you can still use ACSys. If you prefer another toolkit, declare its dependencies and arrange for its entry point to launch. To try the *unchanged* sample GUI, use `uv sync --extra gui-pyqt` and `uv run ap-python-starter-kit --gui`. The [DevPod desktop](devpod.md#desktop-access) lets you see a window during development. 5. **Choose how your app is delivered.** For a CLI/headless application, choose the `cli` image variant in the provided workflow; for a desktop app, `gui-xpra` provides browser access to its window. Most changes **inside the renamed entry point** need no image startup changes: the rename script updates the original module names in both startup paths. Only if you move or replace that entry point must you update the CLI image's fixed command in the [`Dockerfile`](../Dockerfile), or the GUI image's default `APP_CMD` in [`docker/start.sh`](../docker/start.sh), to launch your code. You do not need to build images locally to deliver your app. See [CI and deployment](deployment.md) for the normal release path and [Container usage](container.md) for optional local image tests and startup details. 6. **Make tests describe your app.** Replace the example behavior checks in [`tests/test_main.py`](../tests/test_main.py) with tests for your arguments, command behavior, and any window logic you retain. Existing tests mock the ACSys reader; they do not confirm real ACSys connectivity. Run `uv run pytest`, followed by the quality checks in [Everyday development](development.md#tests-and-code-quality), and keep the [uv project compatible with integration](development.md#stay-compatible-with-integration). Commit your changes on a branch and follow [CI and deployment](deployment.md) when ready to deliver. diff --git a/docs/container.md b/docs/container.md index 88ea5a2..34ac031 100644 --- a/docs/container.md +++ b/docs/container.md @@ -17,6 +17,16 @@ A **CLI image** runs a command-line/headless program and prints output to its co Choose **one** image for your app: run `make build` for the CLI target or `make build-gui` for the Xpra GUI target. The template's startup commands point to its example app; the rename script updates the original module names, but if you change the entry point, [update startup](application.md#replace-the-example-step-by-step) before building. The Xpra target uses an externally maintained base image. Local Make commands do **not** publish or deploy images; use [CI and deployment](deployment.md) for that. +## Optional: local storage mount + +Both images run as `pyuser` (UID 1000) with `/mnt/storage/ap-python-writable` as their working directory. In the [AP Python Launcher](https://ad-apps-internal.fnal.gov/ap-python/), a persistent volume is mounted there; see [Persistent storage](deployment.md#persistent-storage). The `make run`, `make run-gui`, `make shell`, and `make shell-gui` targets imitate this by bind-mounting a host directory at the same path and setting `AP_WRITABLE_STORAGE_DIR` to it. By default the host directory is `mount//local` in your repository (the `mount/` directory is ignored by Git). Override it with `STORAGE_HOST_DIR`: + +```bash +make run STORAGE_HOST_DIR=/tmp/my-app-storage +``` + +The Make targets create the host directory and make it world-writable, because the container's UID 1000 may not match your host user. If you run `docker run` yourself, mount a directory that UID 1000 can write to, or the GUI image will exit at startup with a "not writable" error. + ## Optional: run the CLI image ```bash @@ -47,7 +57,7 @@ docker run --rm --entrypoint python ap-python-starter-kit -m ap_python_starter_k To make `APP_CMD` control the CLI image, change the entrypoint in your project; setting the environment variable alone is insufficient. -The [`xpra-runtime` image](../Dockerfile) exposes port `14500`, has a health check against `http://localhost:14500/`, and runs as `pyuser` in `/home/pyuser`. Its [startup script](../docker/start.sh) defaults to `python -m ap_python_starter_kit.main --gui`. Xpra starts `openbox` and the configured application, writes the app and Xpra logs to `/tmp/app.log` and `/tmp/xpra.log` by default, and streams both logs to container stdout. +The [`xpra-runtime` image](../Dockerfile) exposes port `14500`, has a health check against `http://localhost:14500/`, and runs as `pyuser` in `/mnt/storage/ap-python-writable`. Its [startup script](../docker/start.sh) defaults to `python -m ap_python_starter_kit.main --gui`. The script exits with an error if the writable directory (`AP_WRITABLE_STORAGE_DIR`, default `/mnt/storage/ap-python-writable`) is missing or not writable. It changes into that directory, so the application starts there. Xpra starts `openbox` and the configured application, writes the app and Xpra logs to `app.log` and `xpra.log` in that directory by default (override with `APP_LOG_FILE` and `XPRA_LOG_FILE`), and streams both logs to container stdout. Because the directory is persistent storage in the launcher, these logs are kept after the launch ends and are not cleaned up automatically; see [View persisted files](deployment.md#view-persisted-files) to read them later. To select a different **host** port without changing the container's listening port: @@ -75,7 +85,7 @@ The [startup script](../docker/start.sh) supports these environment overrides (s | `XPRA_EXIT_WITH_WINDOWS` | `yes` | Request shutdown when no application windows remain. | | `XPRA_SERVER_IDLE_TIMEOUT` | `300` | Idle-server timeout in seconds. | -The startup script also accepts `APP_NAME` (session name), `XPRA_DISPLAY` (default `:100`), `XPRA_HTML` (default `on`), `XPRA_BIND_HOST` (default `0.0.0.0`), `XPRA_BIND_PORT` (default `14500`), and `XPRA_AUTH` (default `none`). It creates writable runtime directories under `/tmp` by default; see the [configuration block](../docker/start.sh) for log and directory path overrides. It waits for the HTML endpoint to become ready and stops Xpra on container shutdown. These settings are script/runtime options, not additional `make` arguments unless explicitly forwarded by the recipe. +The startup script also accepts `APP_NAME` (session name), `XPRA_DISPLAY` (default `:100`), `XPRA_HTML` (default `on`), `XPRA_BIND_HOST` (default `0.0.0.0`), `XPRA_BIND_PORT` (default `14500`), and `XPRA_AUTH` (default `none`). It creates Xpra's runtime directories under `/tmp` by default (sockets and `chmod 700` directories do not belong on the shared volume); see the [configuration block](../docker/start.sh) for log and directory path overrides. It waits for the HTML endpoint to become ready and stops Xpra on container shutdown. These settings are script/runtime options, not additional `make` arguments unless explicitly forwarded by the recipe. **Do not publish the default GUI port to an untrusted network.** Xpra defaults to `XPRA_AUTH=none` (no authentication), serves HTML, and listens on all container interfaces; the default [`make run-gui` port mapping](../Makefile) does not restrict the *host* interface. For local-only access, use an explicit loopback host-port binding instead of that `make` recipe, for example: diff --git a/docs/deployment.md b/docs/deployment.md index b638b70..0a1918d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -41,6 +41,12 @@ The [AP Python Launcher](https://ad-apps-internal.fnal.gov/ap-python/) gives eve The last column shows where each mounted directory lives in the PVC, which is useful when locating your app's data from outside the container. `` is your repository name and `` is the microsecond launch time. +### View persisted files + +To browse persisted files without a running launch, open the launcher's file browser at . Include the trailing slash; the URL currently does not work without it. The browser shows the shared storage. Find your app's data by repository name and launch timestamp, following the layout in the table's last column. This is also where you can find the GUI image's `app.log` and `xpra.log` from earlier launches. + +Both [container images](container.md) start with `/mnt/storage/ap-python-writable` as their working directory, so relative paths resolve inside the writable storage. The GUI (`gui-xpra`) image also writes its `app.log` and `xpra.log` files there. The mounted volume must be writable by the container user (`pyuser`, UID/GID 1000). + Example: ```python @@ -54,12 +60,12 @@ if not output_dir.is_dir(): (output_dir / "result.csv").write_text("value\n42\n") ``` -When running outside the launcher (for example, in your DevPod workspace), you can set `AP_WRITABLE_STORAGE_DIR` to a local directory for the code above to work. +When running outside the launcher (for example, in your DevPod workspace), you can set `AP_WRITABLE_STORAGE_DIR` to a local directory for the code above to work. The `make run` and `make run-gui` targets do this for you by mounting a local directory; see [local storage mount](container.md#optional-local-storage-mount). Keep these points in mind when using it: -- **Write persistent data to the writable directory only.** Anything written elsewhere in the container is lost when the launch ends. -- **Outputs outlive the launch.** Data stays available in the shared read-only view after the Job is deleted or expires. There is no automatic cleanup of old data. +- **Write persistent data to the writable directory only.** Anything written elsewhere in the container is lost when the launch ends. The working directory is the writable directory, but prefer `AP_WRITABLE_STORAGE_DIR` over relying on it, so your code does not depend on where it was started. +- **Outputs outlive the launch.** Data stays available in the shared read-only view and the [file browser](#view-persisted-files) after the Job is deleted or expires. There is no automatic cleanup of old data, including the GUI image's log files. - **Storage setup failures are reported by the launcher.** If the launcher cannot initialize your launch's directory, the launch fails and the failure is reported in its status instead of starting your app. ## Advanced: workflow settings and behavior diff --git a/pyproject.toml b/pyproject.toml index 976cec0..061d426 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ packages = ["src/ap_python_starter_kit"] [project] name = "ap-python-starter-kit" -version = "0.1.0" +version = "0.2.0" description = "Python starter kit" readme = "README.md" requires-python = ">=3.12" diff --git a/uv.lock b/uv.lock index 43f039e..5c8b232 100644 --- a/uv.lock +++ b/uv.lock @@ -17,7 +17,7 @@ settings = [ [[package]] name = "ap-python-starter-kit" -version = "0.1.0" +version = "0.2.0" source = { editable = "." } dependencies = [ { name = "acsys", extra = ["settings"] },