diff --git a/PLAN.md b/PLAN.md index 88c806d..675eed8 100644 --- a/PLAN.md +++ b/PLAN.md @@ -48,12 +48,115 @@ What we learned provisioning (2026-12-02/03): `systemd-networkd-resolve-hook.socket` starts it at boot, it takes port 53 and pdns can't start after a reboot. coreapp-dev masks it through cloud-init; the installer should mask it too - - `upgrade-federated.timer` has `Requires=upgrade-federated.service`, so - the upgrade runs at every boot, not just at 5:00. It fails there - (exit 2), and on the first reboot it removed the pdns container that - couldn't start + - Every Core timer (upgrade-federated, free-up-space, backuptool, + dumpcerts, fix-nextcloud, nextcloud-maintenance) has + `Requires=` in `[Unit]`, so its job also runs at every + boot, about 5 s in, not just on its schedule. Evidence (2026-12-03): + boots at 18:01:20 and 18:14:08 UTC; all four checked jobs started at + 18:01:25 and 18:14:13. Consequences: + - upgrade-federated runs before the network is online (it has no + `After=network-online.target`): `git pull` fails with "Could not + resolve host: gitea.federated.computer", then `send_alert_upgrade` + emails ALERTS_EMAIL ("upgrade failed to run on - couldn't + pull from git") and posts a Matrix alert, and it exits 2 + (`/federated/logs/upgrade.log`). So every reboot sends a false + alert + - free-up-space runs `docker system prune -f` and + `docker image prune -a --filter until=24h` while containers are + still starting: at 18:01:25 it deleted the pdns container that + had failed to start (port 53) and its image + (`/federated/logs/freeupspace.log`). Any app container that is + stopped at boot time goes the same way - `/federated/bin/upgrade` has an API token in it, readable by anyone - with the image + with the image; `lib/functions.sh` has the monitoring registrar's + bearer token, and `uninstallapp`'s `set -x` prints it + - `bin/start` doesn't know apps outside its fixed lists, so a new + app in the generic format isn't started by `installapp` + +## Next: the SDK (build, deploy and test an app on the Core) + +### How the Core runs apps (studied 2026-12-03 on dev.fedcom.net) + +- `/federated` is a git checkout of Federated's `Core` repo, updated by + `/federated/bin/upgrade` (`git pull`). Untracked folders survive pulls. +- Installed apps live in `/federated/apps//`: `docker-compose.yml`, + `.env` (mode 600), secrets in files, data under `data/`. +- Every container is on the external Docker network `core` + (192.168.0.0/16) with a fixed `ipv4_address`. Federated's apps use + 192.168.0.7 to .121. +- traefik (`providers.docker`, `exposedByDefault=false`) routes by labels: + `traefik.enable=true`, `routers..rule=Host(`.`)`, + `entrypoints=websecure`, `tls.certresolver=letsencrypt` (DNS challenge + through the Core's own pdns, so any hostname gets a certificate), and + `services..loadbalancer.server.port` when the port isn't 80. +- DNS: the zone has `*.` CNAME `www.` (the public IP), so + every app hostname already resolves. Some apps also add their own A record. +- `/federated/bin/installapp [-n] ` installs an app from either + - `lib/.sh` with `config_`, `start_`, `uninstall_` and + optionally `configsso_` (Authelia OIDC) and `email_`; or + - the newer generic format, `services//`: + - `service`: `NAME`, `DESCRIPTION`, `DOCS`, `CONTAINER`, `VERSION`, + `INTERNAL_IP`, `DEPENDS`, `PORTS`, `MONITOR="https://.@DOMAIN@|http_2xx"` + - `docker-compose.yml.in` and `env.in`: templates. `@DOMAIN@` and other + variables from `/etc/federated`; `@IMAGE_VERSION@`; + `@NAME:RANDOM:LLLL...@` makes a random value (L = letter or digit) + - `icon.png` + - `install`: `. /federated/lib/helpers.sh` and `installGeneric ` + (fills in the templates into `/federated/apps//`); installapp + then runs `/federated/bin/start ` + - optional `uninstall`, `timers/`, `services/` (systemd units) + - installapp installs `DEPENDS` first; `uninstallapp ` reverses it. + - superpanel's catalog lists `services/*/service`, so an app installed this + way shows up in the Core's dashboard. + +### Design (proposed) + +An app is a folder in the generic format, kept anywhere on the workstation +(its own git repo). A `coreapp` command, run inside that folder, works +against the Core in the local VM (`coreapp-vm ip`): + +- `coreapp new `: scaffold the folder: `service`, + `docker-compose.yml.in` with the traefik labels, `env.in`, `install`, a + placeholder `icon.png`, a `Dockerfile` with a minimal web server, and a + README. Its `INTERNAL_IP` comes from 192.168.200.0/24, well away from + Federated's 192.168.0.x, and different for each app made on this machine. +- `coreapp deploy`: build the image locally (`docker build`), copy it to the + Core (`docker save | ssh docker load`, no registry needed), copy the folder + to `/federated/services//`, then `installapp -n ` the first + time. Later deploys re-render `docker-compose.yml` but keep `.env` (its + random secrets), and restart the app. +- `coreapp status`, `coreapp logs [-f]`, `coreapp shell`: on the Core. +- `coreapp check`: the app answers at `https://./` as its + `MONITOR` line says, with a valid certificate. +- `coreapp remove`: `uninstallapp ` and remove its services folder. + +Built and tested 2026-12-03 with a test app `hello` (busybox httpd): +new, deploy (first install and redeploy), check, status, logs, remove all +work against dev.fedcom.net; `https://hello.dev.fedcom.net/` answers through +the relay. + +What we learned: +- `/federated/bin/start ` only starts apps in the fixed `CORE_APPS` / + `EXTRA_APPS` lists in `lib/functions.sh`; for anything else it prints its + usage. So `installapp` installs a new app but doesn't start it, and + `start all` skips it. `coreapp deploy` starts it with the same + `docker compose ... up -d`; `restart: always` covers reboots +- `installapp` / `uninstallapp` call `update_monitors`, which registers or + removes the app's `MONITOR` URL with Federated's external monitoring + (blackboxregistrar.monitoreast.fedcom.net). Dev apps get registered too +- `uninstallapp` runs with `set -x`; its trace includes the monitoring + registrar's bearer token (in plain text in `lib/functions.sh`) and the + Core's email addresses. coreapp keeps the trace in + `~/.local/share/coreapp-dev/logs/` (mode 600) +- `uninstallapp` only stops a generic app if the app has an `uninstall` + script (sourced, so no `exit`); the template has one + +- Decided 2026-12-03: apps in development are not registered with + Federated's monitoring. `coreapp deploy` drops `MONITOR` from the copy of + `service` on the Core +- [ ] Document the app format fully in the README (done in each app's README) +- [ ] Later: a database (`DEPENDS=postgresql`), SSO through Authelia, apps + published for other Cores (registry, submitting to Federated) ## SDK packaging diff --git a/README.md b/README.md index 0426b3e..0f7dd8f 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ sibling add https://gitea.federated.computer/Federated_Computer/coreapp-dev.git This installs libvirt and QEMU/KVM, adds you to the `libvirt` group (log out and back in afterwards), sets up the `coreapp` VM network (10.123.0.0/24) and -installs the `coreapp-vm` command. CoreDesktop's `update` keeps them current. +installs Docker and the `coreapp-vm` and `coreapp` commands. CoreDesktop's `update` keeps them current. ## A Core in a VM @@ -41,7 +41,37 @@ installs the `coreapp-vm` command. CoreDesktop's `update` keeps them current. ([Provisioning a Core](https://documentation.federated.computer/self-hosted/cores/provisioning-a-core/)), with the public IP as `ip`. -## Commands +## Make an app + +``` +coreapp new my-app # makes ./my-app from the template +cd my-app +coreapp deploy # build, copy to the Core, install (or update) +coreapp check # https://my-app./ answers, valid certificate +``` + +An app is a folder in the format Federated Core installs apps from +(`/federated/services//` on the Core): `service`, +`docker-compose.yml.in`, `env.in`, `install`, `uninstall`, `icon.png`, plus +the app's own `Dockerfile`. The README in each new app explains the files. + +| Command | Does | +|---|---| +| `coreapp new ` | Make an app in `./`; it gets an address in 192.168.200.x on the Core | +| `coreapp deploy` | Build its image, copy it to the Core over SSH, install it, or update it keeping its `.env` | +| `coreapp check` | It answers at its `MONITOR` URL with a valid certificate | +| `coreapp status` / `logs [-f]` / `shell` | Its container, its log, a shell in it | +| `coreapp remove` | Uninstall it from the Core (asks first) | + +`COREAPP_VM` picks the VM (default `coreserver`). + +Installing and removing an app go through the Core's own `installapp` and +`uninstallapp`. Those would also add its `MONITOR` URL to Federated's +external monitoring, so `coreapp deploy` copies `service` to the Core without +the `MONITOR` line: apps in development aren't monitored. `coreapp check` +uses the line from your folder. + +## VM commands | Command | Does | |---|---| @@ -76,5 +106,7 @@ stay installed. | `install.sh`, `update.sh` | Run by CoreDesktop's `sibling add` and `update` | | `libvirt/network-hook.sh` | NAT and firewall rules for the `coreapp` network | | `bin/coreapp-vm` | Creates and manages CoreServer VMs (`coreapp-vm help`) | +| `bin/coreapp` | Makes, deploys and tests apps (`coreapp help`) | +| `templates/app/` | What `coreapp new` starts from | | `uninstall.sh` | Removes what coreapp-dev set up | | [PLAN.md](PLAN.md), [TEST.md](TEST.md) | What's next, and how to test it | diff --git a/TEST.md b/TEST.md index 2f6caa6..b9f8986 100644 --- a/TEST.md +++ b/TEST.md @@ -8,8 +8,8 @@ a temporary folder. Run these on a machine or VM you don't mind changing. ```bash cd ~/Projects/coreapp-dev -for f in install.sh update.sh uninstall.sh bin/coreapp-vm libvirt/network-hook.sh; do bash -n "$f"; done -shellcheck -x install.sh update.sh uninstall.sh bin/coreapp-vm libvirt/network-hook.sh +for f in install.sh update.sh uninstall.sh bin/coreapp-vm bin/coreapp libvirt/network-hook.sh; do bash -n "$f"; done +shellcheck -x install.sh update.sh uninstall.sh bin/coreapp-vm bin/coreapp libvirt/network-hook.sh ``` ## install.sh @@ -139,3 +139,33 @@ On a machine you can rebuild (it removes the network every VM uses): 3. Answering no to the data questions keeps `~/.local/share/coreapp-dev` and `~/.config/coreapp-dev`. 4. `bash install.sh` puts everything back. + +## Apps (coreapp) + +Against a Core in the VM (`coreapp-vm list` shows it running). These install +a test app on that Core. It must not be registered with Federated's +monitoring: `/federated/services/hello/service` on the Core has no +`MONITOR` line, and `~/.local/share/coreapp-dev/logs/hello-install.log` has +no `blackboxregistrar` request. + +1. `cd $(mktemp -d) && coreapp new hello`: `./hello` has `service`, + `docker-compose.yml.in`, `env.in`, `install`, `uninstall`, `icon.png`, + `Dockerfile`, `www/`, `README.md`, with no `{{...}}` left + (`grep -r '{{' hello` prints nothing). `coreapp new hello` again refuses. + `~/.local/share/coreapp-dev/app-addresses` has `hello 192.168.200.x`. +2. `cd hello && coreapp deploy`: builds, copies the image, installs, + prints `Deployed: https://hello./`. On the Core, + `/federated/apps/hello/.env` is root's, mode 600, with `IMAGE_VERSION` + equal to `VERSION` and a random `APP_SECRET`. +3. `coreapp check`: `OK: HTTP 200, valid certificate.` (the first + certificate can take a minute). +4. Change `www/index.html`, `coreapp deploy`: says "Updating it...", the + container is recreated, the page shows the change, `APP_SECRET` is + unchanged. `coreapp deploy` again: "The Core already has this image." +5. `coreapp status`, `coreapp logs`, `coreapp shell` (exit with `exit`). +6. `coreapp remove`, answering y: on the Core, `/federated/apps/hello`, + `/federated/services/hello`, the container and the `coreapp/hello` + image are gone. Nothing from `uninstallapp`'s trace is printed; it's in + `~/.local/share/coreapp-dev/logs/hello-remove.log` (mode 600). +7. Collisions: an app named like one of Federated's (`coreapp new nextcloud`, + deploy) is refused; so is an `INTERNAL_IP` another container uses. diff --git a/bin/coreapp b/bin/coreapp new file mode 100755 index 0000000..0cb891c --- /dev/null +++ b/bin/coreapp @@ -0,0 +1,326 @@ +#!/usr/bin/env bash +# +# Purpose : Make, deploy and test apps for Federated Core, against the Core +# in coreapp-dev's VM (coreapp-vm). +# +# coreapp new make a new app in ./ +# coreapp deploy build, copy to the Core, install or update +# coreapp check the app answers at https://./ +# coreapp status its container, address and URL +# coreapp logs [-f] its log +# coreapp shell a shell in its container +# coreapp remove uninstall it from the Core (asks first) +# +# Run everything but `new` in the app's folder. COREAPP_VM picks the VM +# (default coreserver). + +set -euo pipefail + +SELF_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" +TEMPLATE="$(dirname "$SELF_DIR")/templates/app" +DATA="$HOME/.local/share/coreapp-dev" +# The INTERNAL_IP given to each app made on this machine: "name ip" lines +ADDRESSES="$DATA/app-addresses" +# Apps get addresses here, away from Federated's apps in 192.168.0.x (both +# are in the Core's 192.168.0.0/16 network) +PREFIX=192.168.200 +SSH_KEY="$DATA/ssh/id_ed25519" +KNOWN_HOSTS="$DATA/ssh/known_hosts" +VM="${COREAPP_VM:-coreserver}" +# What the Core installs an app from; the rest of the folder stays here +CORE_FILES=(service docker-compose.yml.in env.in install uninstall icon.png timers services upgrade) + +die() { echo "$*" >&2; exit 1; } + +usage() { + sed -n '/^# coreapp /s/^# //p' "${BASH_SOURCE[0]}" +} + +# --- The Core ------------------------------------------------------------- + +CORE_IP="" +# Look up the VM's address once, in this shell (not in a $(...) subshell, +# which would forget it) +init_core() { + if [[ -z "$CORE_IP" ]]; then + CORE_IP="$("$SELF_DIR/coreapp-vm" ip "$VM" 2> /dev/null)" \ + || die "The Core's VM '$VM' isn't running: coreapp-vm start $VM" + fi +} +core_ip() { init_core; echo "$CORE_IP"; } + +# Run a shell command on the Core as root (stdin is passed through) +core() { + ssh -i "$SSH_KEY" -o UserKnownHostsFile="$KNOWN_HOSTS" \ + -o StrictHostKeyChecking=accept-new -o BatchMode=yes \ + "root@$(core_ip)" "$1" +} + +# The same, with a terminal (for shells and following logs) +core_tty() { + ssh -t -i "$SSH_KEY" -o UserKnownHostsFile="$KNOWN_HOSTS" \ + -o StrictHostKeyChecking=accept-new "root@$(core_ip)" "$1" +} + +DOMAIN="" +core_domain() { + if [[ -z "$DOMAIN" ]]; then + # shellcheck disable=SC2016 # expanded on the Core + DOMAIN="$(core '. /etc/federated; echo "$DOMAIN"')" + [[ -n "$DOMAIN" ]] || die "The VM has no Core installed yet (no DOMAIN in /etc/federated)." + fi + echo "$DOMAIN" +} + +# --- The app in the current folder --------------------------------------- + +# NAME is the app's id (its folder on the Core); TITLE is NAME in service +NAME="" TITLE="" CONTAINER="" VERSION="" INTERNAL_IP="" MONITOR="" HIDDEN="" +load_app() { + [[ -f service && -f install ]] \ + || die "No app here: run this in an app's folder (one with service and install), or make one with coreapp new ." + # service is shell variables, as the Core reads it + set +u + # shellcheck source=/dev/null + . ./service + set -u + TITLE="$NAME" + NAME="$(sed -n 's/^installGeneric[[:space:]]\{1,\}\([a-z0-9-]*\).*/\1/p' install | head -n 1)" + [[ -n "$NAME" ]] || die "install doesn't say which app it is (installGeneric )." + [[ -n "$CONTAINER" && -n "$VERSION" && -n "$INTERNAL_IP" ]] \ + || die "service needs CONTAINER, VERSION and INTERNAL_IP." + init_core +} + +image() { echo "$CONTAINER:$VERSION"; } + +# --- new -------------------------------------------------------------------- + +next_address() { + local n + for n in $(seq 10 250); do + if ! grep -qs " $PREFIX\.$n\$" "$ADDRESSES"; then + echo "$PREFIX.$n" + return + fi + done + die "All addresses in $PREFIX.10-250 are taken (see $ADDRESSES)." +} + +cmd_new() { + local name="${1:-}" title ip f + [[ -d "$TEMPLATE" ]] || die "The app template is missing: $TEMPLATE" + [[ -n "$name" ]] || die "Usage: coreapp new (lowercase letters, digits and -)" + [[ "$name" =~ ^[a-z][a-z0-9-]{1,30}$ ]] \ + || die "'$name': use 2-31 lowercase letters, digits and -, starting with a letter. It becomes https://$name.." + [[ ! -e "$name" ]] || die "./$name already exists." + + ip="$(grep -s "^$name " "$ADDRESSES" | cut -d' ' -f2 || true)" + if [[ -z "$ip" ]]; then + ip="$(next_address)" + mkdir -p "$DATA" + echo "$name $ip" >> "$ADDRESSES" + fi + # "my-app" -> "My App" + title="$(echo "$name" | sed -E 's/-/ /g; s/(^| )([a-z])/\1\u\2/g')" + + mkdir "$name" + tar -C "$TEMPLATE" --exclude=.claude -cf - . | tar -C "$name" -xf - + while IFS= read -r -d '' f; do + sed -i -e "s/{{NAME}}/$name/g" -e "s/{{TITLE}}/$title/g" -e "s/{{IP}}/$ip/g" "$f" + done < <(find "$name" -type f ! -name '*.png' -print0) + + echo "Made $title in ./$name (address on the Core: $ip)." + echo "Next: cd $name && coreapp deploy" +} + +# --- deploy ----------------------------------------------------------------- + +cmd_deploy() { + local domain info state theirs tier used local_id core_id f log files=() + load_app + domain="$(core_domain)" + + # What the Core has: is the app installed, is the name or address taken? + info="$(core " + . /etc/federated + echo \"tier \$TIER\" + [ -d /federated/apps/$NAME ] && echo installed || echo new + if [ -f /federated/services/$NAME/service ]; then + echo \"theirs \$(sed -n 's/^CONTAINER=//p' /federated/services/$NAME/service)\" + fi + docker network inspect core -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{println}}{{end}}' \ + | grep ' $INTERNAL_IP/' | sed 's/^/used /' || true + ")" + tier="$(sed -n 's/^tier //p' <<< "$info")" + state="$(grep -xE 'installed|new' <<< "$info")" + theirs="$(sed -n 's/^theirs //p' <<< "$info")" + used="$(sed -n 's/^used \([^ ]*\) .*/\1/p' <<< "$info" | grep -vx "$NAME" || true)" + + if [[ -n "$theirs" && "$theirs" != "$CONTAINER" ]]; then + die "The Core already has an app called '$NAME' ($theirs). Pick another name." + fi + if [[ -n "$used" ]]; then + die "$INTERNAL_IP is already used on the Core by: $used. Change INTERNAL_IP in service (and docker-compose.yml.in)." + fi + if [[ "$state" == new && "${tier,,}" != enterprise && "$HIDDEN" != true ]]; then + echo "This Core's tier is '$tier'. Installing records '$NAME' as an add-on app" + echo "(ADDON_APPS in /etc/federated), which Federated may bill for." + read -r -p "Install anyway? [y/N] " f + [[ "$f" =~ ^[Yy] ]] || die "Not deployed. HIDDEN=true in service keeps an app off that list." + fi + + echo "Building $(image)..." + docker build -q -t "$(image)" . > /dev/null + + local_id="$(docker image inspect -f '{{.Id}}' "$(image)")" + core_id="$(core "docker image inspect -f '{{.Id}}' $(image) 2> /dev/null || true")" + if [[ "$local_id" != "$core_id" ]]; then + echo "Copying the image to the Core ($(docker image inspect -f '{{.Size}}' "$(image)" | numfmt --to=iec))..." + docker save "$(image)" | core "docker load -q > /dev/null" + else + echo "The Core already has this image." + fi + + echo "Copying the app to /federated/services/$NAME..." + for f in "${CORE_FILES[@]}"; do [[ -e "$f" ]] && files+=("$f"); done + tar -cf - "${files[@]}" | core " + set -e + d=/federated/services/$NAME + rm -rf \$d.new && mkdir -p \$d.new && tar --no-same-owner -xf - -C \$d.new + # No MONITOR on the Core: installapp would register it with Federated's + # external monitoring, which apps in development shouldn't be in + sed -i '/^MONITOR=/d' \$d.new/service + rm -rf \$d && mv \$d.new \$d" + + if [[ "$state" == new ]]; then + echo "Installing it (the Core's installapp)..." + mkdir -p "$DATA/logs" + log="$DATA/logs/$NAME-install.log" + # installapp traces what it runs, tokens included: keep that private + : > "$log" && chmod 600 "$log" + if ! core "/federated/bin/installapp -n $NAME" > "$log" 2>&1; then + tail -n 20 "$log" >&2 + die "installapp failed (whole log: $log)." + fi + # installapp's start step only knows Federated's own apps (the list in + # /federated/lib/functions.sh), so start it the way the Core does + core "docker compose -f /federated/apps/$NAME/docker-compose.yml -p $NAME up -d 2>&1 | grep -v '^ *Container ' || true" + else + # Keep .env (its random secrets); redo the compose file, which may have + # changed, and the version + echo "Updating it..." + core " + set -e + cd /federated + . /etc/federated + . lib/helpers.sh + . services/$NAME/service + templatize services/$NAME/docker-compose.yml.in > apps/$NAME/docker-compose.yml + sed -i 's/^IMAGE_VERSION=.*/IMAGE_VERSION=$VERSION/' apps/$NAME/.env + docker compose -f apps/$NAME/docker-compose.yml -p $NAME up -d --remove-orphans 2>&1 | grep -v '^ *Container .* Running\$' || true" + fi + + if [[ "$(core "docker inspect -f '{{.State.Status}}' $NAME 2> /dev/null || true")" != running ]]; then + core "docker logs --tail 20 $NAME 2>&1" >&2 || true + die "$NAME isn't running on the Core (its log is above, if it has one)." + fi + echo "Deployed: https://$NAME.$domain/ (coreapp check tests it)" +} + +# --- check, status, logs, shell, remove ------------------------------------ + +cmd_check() { + local domain url expect result code verify tries=24 + load_app + domain="$(core_domain)" + url="https://$NAME.$domain/" + expect="http_2xx" + if [[ -n "$MONITOR" ]]; then + url="${MONITOR%%|*}" + url="${url//@DOMAIN@/$domain}" + [[ "$MONITOR" == *"|"* ]] && expect="${MONITOR#*|}" + fi + echo "Checking $url (expecting $expect)..." + # A new app's certificate can take a minute: traefik gets it from Let's + # Encrypt through the Core's DNS + while true; do + result="$(curl -sS -o /dev/null --max-time 15 -w '%{http_code} %{ssl_verify_result}' "$url" 2>&1 || true)" + code="${result%% *}" + verify="${result##* }" + if [[ "$code" =~ ^2 && "$verify" == 0 ]]; then + echo "OK: HTTP $code, valid certificate." + return 0 + fi + tries=$((tries - 1)) + if ((tries == 0)); then break; fi + sleep 5 + done + echo "Not OK: $result (HTTP code, then 0 if the certificate is valid)." >&2 + # Is it the app, or the way in? + result="$(curl -sSk -o /dev/null --max-time 15 -w '%{http_code}' \ + --resolve "$NAME.$domain:443:$(core_ip)" "$url" 2>&1 || true)" + echo "Straight to the VM, skipping DNS and the relay: HTTP $result." >&2 + exit 1 +} + +cmd_status() { + local domain + load_app + domain="$(core_domain)" + core "docker ps -a --filter name='^$NAME\$' --format 'Container: {{.Names}} {{.Status}} image {{.Image}}' + [ -n \"\$(docker ps -aq --filter name='^$NAME\$')\" ] || echo 'Not installed on the Core.'" + echo "App: $TITLE $VERSION ($CONTAINER)" + echo "Address on the Core: $INTERNAL_IP" + echo "URL: https://$NAME.$domain/" +} + +cmd_logs() { + load_app + if [[ "${1:-}" == -f ]]; then + core_tty "docker logs -f --tail 50 $NAME" + else + core "docker logs --tail 200 $NAME 2>&1" + fi +} + +cmd_shell() { + load_app + core_tty "docker exec -it $NAME sh -c 'command -v bash > /dev/null && exec bash || exec sh'" +} + +cmd_remove() { + local answer log + load_app + echo "This uninstalls $NAME from the Core in '$VM', with its settings and data" + echo "there. This folder stays as it is." + read -r -p "Remove it? [y/N] " answer + [[ "$answer" =~ ^[Yy] ]] || die "Not removed." + mkdir -p "$DATA/logs" + log="$DATA/logs/$NAME-remove.log" + # uninstallapp traces what it runs, tokens included: keep that private + : > "$log" && chmod 600 "$log" + if ! core " + set -e + if [ -d /federated/apps/$NAME ]; then /federated/bin/uninstallapp $NAME; fi + rm -rf /federated/services/$NAME + docker image ls -q '$CONTAINER' | xargs -r docker image rm -f" > "$log" 2>&1; then + tail -n 20 "$log" >&2 + die "Removing failed (whole log: $log)." + fi + echo "Removed $NAME from the Core." +} + +cmd="${1:-help}" +[[ $# -gt 0 ]] && shift +case "$cmd" in + new) cmd_new "$@" ;; + deploy) cmd_deploy ;; + check) cmd_check ;; + status) cmd_status ;; + logs) cmd_logs "$@" ;; + shell) cmd_shell ;; + remove | rm) cmd_remove ;; + help | -h | --help) usage ;; + *) usage >&2; exit 1 ;; +esac diff --git a/templates/app/.dockerignore b/templates/app/.dockerignore new file mode 100644 index 0000000..f82df0a --- /dev/null +++ b/templates/app/.dockerignore @@ -0,0 +1,7 @@ +.git +service +*.in +install +uninstall +icon.png +README.md diff --git a/templates/app/Dockerfile b/templates/app/Dockerfile new file mode 100644 index 0000000..f6b34ea --- /dev/null +++ b/templates/app/Dockerfile @@ -0,0 +1,7 @@ +# The app's image. coreapp deploy builds it as coreapp/{{NAME}}: +# and copies it to the Core. Replace this with your own app; keep it +# listening on the port in docker-compose.yml.in (8080). +FROM busybox:stable +COPY www /www +EXPOSE 8080 +CMD ["httpd", "-f", "-v", "-p", "8080", "-h", "/www"] diff --git a/templates/app/README.md b/templates/app/README.md new file mode 100644 index 0000000..1cbc74b --- /dev/null +++ b/templates/app/README.md @@ -0,0 +1,45 @@ +# {{TITLE}} + +An app for Federated Core, made with +[coreapp-dev](https://gitea.federated.computer/Federated_Computer/coreapp-dev). + +## Develop + +Change the app (`Dockerfile`, `www/`), then, in this folder: + +``` +coreapp deploy # build the image, copy it to the Core in your VM, install or update +coreapp check # https://{{NAME}}./ answers, with a valid certificate +coreapp logs -f # its log +coreapp status # its container, address and URL +coreapp shell # a shell in its container +coreapp remove # uninstall it from the Core +``` + +## Files + +The app is in the format Federated Core installs apps from +(`/federated/services/{{NAME}}/` on the Core): + +| File | What it is | +|---|---| +| `service` | Name, description, image (`CONTAINER`), `VERSION`, `INTERNAL_IP`, apps it needs (`DEPENDS`, e.g. `postgresql`), and how to check it's up (`MONITOR`) | +| `docker-compose.yml.in` | The container(s), on the Core's `core` network at `INTERNAL_IP`, with the traefik labels that give it `https://{{NAME}}.` | +| `env.in` | Its settings, as the `.env` file next to the compose file | +| `install` | Run by the Core's `installapp`; fills in the templates | +| `icon.png` | Its icon in the Core's dashboard | +| `Dockerfile`, `www/` | The app itself (not copied to the Core) | + +In `docker-compose.yml.in` and `env.in`, the Core replaces, once at install: + +- `@DOMAIN@`: the Core's domain, and any other variable in `/etc/federated` +- `@VERSION@`, `@CONTAINER@`, `@INTERNAL_IP@`: from `service` +- `@SECRET:RANDOM:LLLLLLLL@`: a random value (`L` a letter or digit, `a` + lowercase, `A` uppercase, `0` a digit, `x` hex), also available as + `@SECRET@` further down + +`${...}` is left for Docker Compose, which reads it from `.env`. + +Raise `VERSION` in `service` for each release. The Core's nightly upgrade +only redeploys an app when `VERSION` differs from `IMAGE_VERSION` in its +`.env`. diff --git a/templates/app/docker-compose.yml.in b/templates/app/docker-compose.yml.in new file mode 100644 index 0000000..aa76e3d --- /dev/null +++ b/templates/app/docker-compose.yml.in @@ -0,0 +1,23 @@ +services: + {{NAME}}: + image: coreapp/{{NAME}}:${IMAGE_VERSION} + container_name: {{NAME}} + hostname: {{NAME}}.@DOMAIN@ + restart: always + networks: + core: + ipv4_address: {{IP}} + env_file: + - ./.env + volumes: + - ./data:/data + labels: + - "traefik.enable=true" + - "traefik.http.routers.{{NAME}}.rule=Host(`{{NAME}}.@DOMAIN@`)" + - "traefik.http.routers.{{NAME}}.entrypoints=websecure" + - "traefik.http.routers.{{NAME}}.tls.certresolver=letsencrypt" + - "traefik.http.services.{{NAME}}.loadbalancer.server.port=8080" + +networks: + core: + external: true diff --git a/templates/app/env.in b/templates/app/env.in new file mode 100644 index 0000000..947b93b --- /dev/null +++ b/templates/app/env.in @@ -0,0 +1,5 @@ +# Filled in once, when the app is installed (see README.md). Redeploying +# keeps the result, so secrets don't change. +IMAGE_VERSION=@VERSION@ +APP_URL=https://{{NAME}}.@DOMAIN@ +APP_SECRET=@APP_SECRET:RANDOM:LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL@ diff --git a/templates/app/icon.png b/templates/app/icon.png new file mode 100644 index 0000000..9e76f46 Binary files /dev/null and b/templates/app/icon.png differ diff --git a/templates/app/install b/templates/app/install new file mode 100755 index 0000000..bfce6e3 --- /dev/null +++ b/templates/app/install @@ -0,0 +1,4 @@ +. /federated/lib/helpers.sh +installGeneric {{NAME}} +# .env holds secrets +chmod 600 /federated/apps/{{NAME}}/.env diff --git a/templates/app/service b/templates/app/service new file mode 100644 index 0000000..021c2b0 --- /dev/null +++ b/templates/app/service @@ -0,0 +1,9 @@ +NAME="{{TITLE}}" +DESCRIPTION="{{TITLE}}, made with coreapp-dev" +DOCS= +CONTAINER=coreapp/{{NAME}} +VERSION=0.1.0 +INTERNAL_IP={{IP}} +DEPENDS="" +PORTS=8080 +MONITOR="https://{{NAME}}.@DOMAIN@|http_2xx" diff --git a/templates/app/uninstall b/templates/app/uninstall new file mode 100755 index 0000000..e34d3ce --- /dev/null +++ b/templates/app/uninstall @@ -0,0 +1,4 @@ +# Sourced by the Core's uninstallapp (so no `exit` here): stop the app and +# delete its settings and data +docker compose -f /federated/apps/{{NAME}}/docker-compose.yml -p {{NAME}} down +rm -rf /federated/apps/{{NAME}} diff --git a/templates/app/www/index.html b/templates/app/www/index.html new file mode 100644 index 0000000..f796fbd --- /dev/null +++ b/templates/app/www/index.html @@ -0,0 +1,8 @@ + + +{{TITLE}} + +

{{TITLE}}

+

Running on Federated Core, deployed with coreapp-dev.

+ + diff --git a/uninstall.sh b/uninstall.sh index 057654a..3fe11e0 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -54,7 +54,7 @@ if [ ${#vms[@]} -gt 0 ]; then fi fi -rm -f "$HOME/.local/bin/coreapp-vm" +rm -f "$HOME/.local/bin/coreapp-vm" "$HOME/.local/bin/coreapp" if command -v virsh > /dev/null && v net-info "$NETWORK" &> /dev/null; then echo "Removing coreapp-dev's VM network..." diff --git a/update.sh b/update.sh index ffa3324..9a18ff4 100644 --- a/update.sh +++ b/update.sh @@ -22,12 +22,14 @@ PACKAGES=( dnsmasq # DHCP/DNS for libvirt's default NAT network edk2-ovmf # UEFI firmware for guests dmidecode # host hardware info for libvirt + docker # coreapp builds apps' images here + docker-buildx ) echo "Checking coreapp-dev's packages..." install_packages "${PACKAGES[@]}" -enable_services --now libvirtd.service +enable_services --now libvirtd.service docker.service if ! systemctl is-active --quiet libvirtd.service; then echo "Starting libvirtd.service..." sudo systemctl start libvirtd.service @@ -173,8 +175,11 @@ if command -v ufw > /dev/null && grep -qs '^ENABLED=yes' /etc/ufw/ufw.conf; then done fi -# The coreapp-vm command: a symlink into this clone, so pulling updates it +# The coreapp-vm and coreapp commands: symlinks into this clone, so pulling +# updates them mkdir -p "$HOME/.local/bin" -ln -sfn "$SELF/bin/coreapp-vm" "$HOME/.local/bin/coreapp-vm" +for cmd in coreapp-vm coreapp; do + ln -sfn "$SELF/bin/$cmd" "$HOME/.local/bin/$cmd" +done echo "coreapp-dev's host software is up to date."