123 lines
11 KiB
Markdown
123 lines
11 KiB
Markdown
---
|
|
name: web-project-scaffold-containerized-python
|
|
description: Scaffold a new web project with a Python (FastAPI) backend and a no-build HTML/CSS/JS frontend, packaged in a Docker container that deploys by pulling source from a remote git repo — `docker exec <name> update` fetches the latest commit, syncs requirements, restarts the app in place and rolls back on failure, with no image rebuild. Servers are set up by one copy-paste install command (curl … | bash) that installs Docker, clones the repo, writes the .env with repo URL and credentials, and starts the container. Requires an existing remote git repo URL up front. Use when the user wants to start/create/bootstrap a new web app, site, API or internal tool that should be Python-based, containerized, or easy to deploy and update on a server.
|
|
---
|
|
|
|
# Scaffold a containerized Python web project
|
|
|
|
Goal: hand back a project where **`scripts/verify.sh` prints `VERIFY: PASS`** before any feature work. The run proves the image builds, the container clones from the repo named in `.env`, the tests pass, and `update` deploys, refuses broken code and rolls back without a rebuild. A passing run is the contract. Don't build features on a skeleton that hasn't passed.
|
|
|
|
## Principles (why the skeleton looks the way it does)
|
|
|
|
1. **Nothing is compiled.** The backend is Python (FastAPI + uvicorn). The frontend is plain HTML, CSS and ES modules served from `app/static/`, with no bundler, TypeScript, Sass, JSX or npm. Third-party browser libraries are vendored into `app/static/vendor/` as files with the pinned version in a comment. A deploy is therefore just new files.
|
|
2. **Containerized.** The host needs only Docker and git. Python, the venv and every tool run in the container, for dev and prod alike.
|
|
3. **The image is the runtime, not the app.** The image holds Python, git, tini and three scripts in `container/`. The source is a git checkout in the `/srv` volume, cloned from `REPO_URL` on first start. A rebuild is needed only when `container/` or the Python version changes.
|
|
4. **Update is one command.** `/usr/local/bin/update` is on PATH, so `docker exec <name> update` works. It reads `.env`, fetches `REPO_REF` from `REPO_URL`, checks out, reinstalls requirements if they changed, checks that `app.main` imports, sends SIGHUP to the in-container supervisor (which restarts uvicorn), and waits for `/healthz` to report the new commit. Any failure restores the previous commit. `--check`, `--rollback` and `<ref>` cover the rest.
|
|
5. **Remote and credentials live in `.env`.** It is gitignored and mounted read-only at `/run/config/app.env`. The container parses it (it doesn't execute it) on every start and every update, so a new URL or rotated token needs no rebuild or recreate.
|
|
- **Variables:** `REPO_URL`, `REPO_REF`, `GIT_TOKEN`/`GIT_USERNAME` (https) or `GIT_SSH_KEY` (base64 deploy key), plus any app settings.
|
|
- **Token:** a git credential helper supplies it from the environment. It never goes into `.git/config` or the URL.
|
|
- **Isolation:** the uvicorn process gets the app settings but not the git credentials.
|
|
|
|
6. **Install is one pasted line.** The project ships `install.sh`. On a fresh Linux server, the user pastes `curl -fsSL <raw install.sh URL> | bash`, Homebrew-style, and the installer does the rest:
|
|
- installs git and Docker (Engine + Compose) if they're missing
|
|
- checks it can read the repo, asking for a token on the terminal if the https repo is private
|
|
- clones into `/opt/<name>`
|
|
- writes `.env` from `.env.example` with the repo URL, branch, credentials and port, owned by uid 1000 with mode 640
|
|
- builds the image, starts the container and waits for `/healthz`
|
|
|
|
Re-running it is safe, and is also how the image gets rebuilt: it keeps `.env` and replaces only the keys passed in. Everything is inside `main()`, so a partial download runs nothing. It uses sudo only for steps that need it.
|
|
|
|
## Step 0 — Get the remote repo URL first (required)
|
|
|
|
Servers install from and update from the remote. The install command is that repo's raw `install.sh` URL, and the URL is the default `REPO_URL` baked into `install.sh` and `.env.example`. So **don't scaffold until the user has given the remote git repo URL.** If they haven't, ask for it before anything else. Ask whether it is private: over https it needs a read-only token, and over SSH a deploy key.
|
|
- Have them create an empty repo first if it doesn't exist (GitHub, GitLab, Gitea, …).
|
|
- GitHub and GitLab raw URLs are derived. For any other host, ask for the raw URL of `install.sh` on the default branch and pass `--install-url`.
|
|
- The URL must not contain credentials. Those go only in the server's `.env`, through the installer.
|
|
|
|
## Step 1 — Check the environment (never install silently)
|
|
|
|
```bash
|
|
bash "$SKILL_DIR/scripts/doctor.sh"
|
|
```
|
|
|
|
It checks git, docker, compose v2, buildx and daemon access. If it exits non-zero, show the user its fix lines and stop. Let them run the installs, suggesting the `! <command>` prefix. Don't scaffold blind.
|
|
|
|
## Step 2 — Decide the few inputs
|
|
|
|
- `--name`: a lowercase slug. It becomes the container name (so `docker exec <name> update`), the image name and the compose project.
|
|
- `--title`: the display name.
|
|
- `--python`: default 3.14. Use the user's version if they give one.
|
|
- `--port`: the host port on 127.0.0.1, default 8000.
|
|
- `--branch`: default `main`.
|
|
- `--repo-url` (**required**, from Step 0): the remote the servers clone from. It goes into `install.sh`, `.env.example` and the local `origin`.
|
|
- `--install-url`: only for hosts other than GitHub and GitLab.
|
|
|
|
Ask only for what you can't default. The name is usually obvious from the request.
|
|
|
|
## Step 3 — Generate
|
|
|
|
```bash
|
|
bash "$SKILL_DIR/scripts/scaffold.sh" --dir <project-dir> --name <slug> --repo-url <url> [--title "…"] [--python 3.14] [--port 8000] [--branch main] [--install-url <url>]
|
|
```
|
|
|
|
- **Pins:** it resolves current `fastapi`, `uvicorn[standard]`, `pytest` and `httpx2` versions in a throwaway `python:<ver>-slim` container and pins them `==X.Y.*`. Use `--no-resolve` when offline.
|
|
- **Templates and tokens:** it copies `templates/project`. If the `web-frontend` skill is installed, it copies that skill's `tokens.css` into `app/static/css/`.
|
|
- **`.env` and git:** it writes a local `.env` (mode 600) for dev, runs `git init` and adds `origin`, then prints the install command. It doesn't commit, push, build or start anything.
|
|
|
|
Resulting layout (details in the generated `README.md`):
|
|
|
|
```
|
|
app/main.py, app/api.py FastAPI: /healthz {status, version}, /api/*, /static (no-cache), /
|
|
app/static/ index.html, css/tokens.css + app.css, js/main.js + api.js, vendor/
|
|
tests/test_app.py pytest + TestClient
|
|
install.sh server installer: curl … | bash (Docker, clone, .env, build, start, health)
|
|
container/ Dockerfile, lib.sh, entrypoint.sh (setup + supervisor), update.sh
|
|
compose.yml prod: volumes runtime:/srv + data:/data, ./.env mounted, loopback port, read-only rootfs
|
|
compose.dev.yml dev overlay: source bind-mounted, DEV=1 → dev deps + --reload, update disabled
|
|
.env.example / .env REPO_URL, REPO_REF, GIT_TOKEN/GIT_USERNAME, GIT_SSH_KEY, HOST_PORT, app settings
|
|
```
|
|
|
|
## Step 4 — Verify the contract
|
|
|
|
```bash
|
|
bash "$SKILL_DIR/scripts/verify.sh" <project-dir> # add --keep to leave containers for debugging
|
|
```
|
|
|
|
It works on a throwaway copy with a local bare repo as the "remote" and a dummy token in its `.env`. Your project, its `.env` and its git history are untouched. It checks:
|
|
- the build and a healthy start
|
|
- the app sees `.env` settings but not `GIT_TOKEN`, and the token is never written to disk
|
|
- pytest passes
|
|
- `update` deploys a commit that changes requirements, with the same image and the same container start time
|
|
- an import-breaking commit is refused and a startup-crashing commit is rolled back, with the old commit still serving
|
|
- `update --rollback`
|
|
- `install.sh` piped into bash, as on a fresh server, against a `git://` remote: it brings the app up healthy and reachable on the host port, writes `.env` (uid 1000, mode 640) and never calls sudo when it isn't needed; a re-run is safe and replaces only the keys passed in
|
|
|
|
It doesn't exercise the Docker-install branch, because Docker is already present. That branch was tested on a bare Debian image: git, ssh, Docker and Compose all installed.
|
|
|
|
It must end with `VERIFY: PASS`. If it fails, fix the template or the project until it passes, then report the versions used.
|
|
|
|
Then tell the user the flow:
|
|
1. **Push** the project to the remote. Offer to make the first commit and push; don't do either unless they ask. The install command only works once `install.sh` is on the remote branch.
|
|
2. **Install:** paste the printed command on the server. For a private https repo, use the `read -rsp … && curl -H …` form from the README, so the token is never echoed or saved in shell history.
|
|
3. **Deploy:** `git push`, then `docker exec <name> update` on the server.
|
|
4. **Rebuild** (only when `container/` changes): run the install command again.
|
|
|
|
## Building on the skeleton
|
|
|
|
- **UI:** follow the `web-frontend` skill for the look (tokens, layout, components), within the no-build rule: plain CSS on the tokens, ES modules, vendored libraries, no CDN. Web fonts are vendored into `static/vendor/fonts/` or left on the system stack.
|
|
- **API:** routes go in `app/api.py`, or an `app/api/` package as it grows, under `/api`. Use Pydantic models for request and response bodies.
|
|
- **Persistence:** keep it under `$DATA_DIR` (`/data`, its own volume). SQLite is the default for small apps. Create the schema and run migrations idempotently in `lifespan`, never at import time, because `update` imports `app.main` as its pre-flight check.
|
|
- **Dependencies:** add each one to `requirements.txt` with a `==X.Y.*` pin. `update` installs it on the next deploy. Test-only dependencies go in `requirements-dev.txt`.
|
|
- **Settings and secrets:** new ones go in `.env` and `.env.example`, which holds names and placeholders only. Read them with `os.environ` in the app.
|
|
- **System packages** (e.g. `ffmpeg`, `libpq`) mean a `container/Dockerfile` change, and so a rebuild. Prefer pure-Python wheels. When a system package is needed, say a rebuild is required.
|
|
|
|
## Guardrails
|
|
|
|
- **Every "it works" claim comes from a `verify.sh` run that printed `VERIFY: PASS`,** never from reading the files.
|
|
- **No build step, ever.** Don't add `package.json`, a bundler, TypeScript or a CSS preprocessor. If the user needs one, they are asking for a different project shape; say so.
|
|
- **Never bake app source into the image.** `.dockerignore` lets in only `container/`. Copying `app/` into the image would make `update` and the image disagree.
|
|
- **No remote, no scaffold.** Get the repo URL first (Step 0). Don't invent a placeholder URL to fill in later.
|
|
- **The server is set up only through `install.sh`.** Keep it idempotent and inside `main()`. When a project needs more server setup (a system package, a firewall rule, a volume path), add it to the installer rather than to manual README steps.
|
|
- **Never commit `.env`, tokens or keys.** Don't put credentials in `REPO_URL`. Don't print `.env` values. Name variables, never their values.
|
|
- **Keep the scripts in `container/` generic.** Project-specific logic belongs in the app (`lifespan`), not in `update.sh`, which can only change with a rebuild.
|
|
- **Used ⇒ declared:** every import maps to a pinned line in `requirements*.txt`, and every vendored file has its version noted.
|