Compare commits

..

No commits in common. "main" and "panel-ui-improve" have entirely different histories.

15 changed files with 5937 additions and 447 deletions

View file

@ -1,209 +0,0 @@
# Backups
Nightly [restic](https://restic.net) backups of the VPS to a Scaleway Object
Storage bucket, configured in [`backups.nix`](backups.nix).
## What is backed up
One encrypted, deduplicated restic repository holds two kinds of snapshots:
| Tag | When (Europe/Amsterdam) | Paths | Notes |
|----------|------------------------|-------|-------|
| `panel` | daily 03:30 | `/var/lib/containers` (stacks, volumes, routes, panel state), `~reudy/.local/share/containers/storage/volumes` | The panel's running containers are **stopped for the duration** (usually under a minute) and started again afterwards, so databases inside them are copied consistently. |
| `system` | daily 04:00 | `/etc`, `/home`, `/root`, `/var/lib`, `/var/backup` | Forgejo, Nextcloud, Authelia, the panel's database, SSH host keys, `/var/lib/nixos` (uid/gid map), the `hello` container, and so on. |
Before the system snapshot, the SQLite databases (Forgejo, Nextcloud, Authelia,
panel) are dumped to `/var/backup/sqlite/<name>.sql`. These dumps are
consistent even though the services keep running. The raw `.db` files are
backed up as well, but a copy taken mid-write can be torn, so restore from the
dumps if a raw file looks broken.
**Not backed up** (it can all be rebuilt): `/nix/store` (rebuilt from this
flake), container images (pulled or built again on deploy), `~/.cache`,
Nextcloud previews, core dumps.
**Retention**, applied every Sunday at 05:30 by `restic-backups-maintenance`:
7 daily, 4 weekly and 6 monthly snapshots, per tag. The same run prunes
unreferenced data and checks the repository, reading a random 2 GB of it.
## Cost notes
Scaleway doesn't charge for API requests. It charges for stored GB-hours,
egress above 75 GB a month, and Glacier restores. So:
- objects are uploaded as **One Zone** (`ONEZONE_IA`), half the price of
Multi-AZ. The class is set per object at upload time (`extraOptions` in
`backups.nix`), so changing it only affects new uploads;
- the backup itself downloads almost nothing. All jobs share one local cache
in `/var/cache/restic`, so metadata is fetched once;
- pruning (which may download and re-pack partly-used data) runs weekly, not
after every backup;
- `check` reads a fixed 2 GB sample instead of the whole repository;
- Glacier isn't used. It stores more cheaply, but every restore and every
pruning repack would first need a paid, hours-long thaw.
`sudo restic-system stats --mode raw-data` shows how much is actually stored.
## One-time setup
The two secrets are agenix files. Create them **on a machine that holds one of
the user keys in [`secrets.nix`](secrets.nix)**, from a checkout of this repo:
```bash
# 1. The repository password. Generate a long random one (e.g. in your password
# manager) and keep it there: without it the backups cannot be read.
agenix -e secrets/restic-password.age
# 2. Where the repository is and how to reach it. Four lines, no quotes:
agenix -e secrets/restic-env.age
```
```ini
RESTIC_REPOSITORY=s3:https://s3.<region>.scw.cloud/<bucket>/vps
AWS_ACCESS_KEY_ID=SCW...
AWS_SECRET_ACCESS_KEY=...
AWS_DEFAULT_REGION=<region>
```
`<region>` is the bucket's region: `fr-par`, `nl-ams` or `pl-waw`. Use an API
key from a dedicated Scaleway IAM application whose policy only grants Object
Storage in the bucket's project, not your personal key.
Store the password, the access key pair, the bucket name and the region in your
password manager too. The `.age` files can be decrypted with your SSH keys, but
the canonical copy of this repo lives on the server you'd be recovering.
Then commit the two `.age` files, deploy, and run the first backups by hand:
```bash
sudo nixos-rebuild switch --flake .#vps
# Optional: seed the panel data without stopping anything. The real run below
# then uploads only what changed, so the apps are down for seconds, not for
# the whole first upload.
sudo restic-panel init # only if the repository doesn't exist yet
sudo restic-panel backup --tag seed /var/lib/containers
sudo systemctl start restic-backups-panel
sudo restic-panel forget --tag seed # its data stays, shared with the real snapshot
sudo systemctl start restic-backups-system # the first upload can take a while
journalctl -u restic-backups-system -f
```
## Day to day
```bash
systemctl list-timers 'restic-*' # next / last runs
systemctl status restic-backups-{panel,system,maintenance}
journalctl -u restic-backups-system -n 100
sudo restic-system snapshots # restic-<job> wrappers work
sudo restic-system snapshots --tag panel # all point at the same repo
```
A failed run leaves the unit in `failed` state. There is no alerting yet, so
`systemctl --failed` is the place to look.
## Restoring files
```bash
sudo restic-system snapshots --tag system
sudo restic-system ls latest --tag system /var/lib/forgejo
sudo restic-system restore latest --tag system \
--include /var/lib/forgejo/data/forgejo.db --target /root/restore
```
Restores keep owners and permissions. Stop the affected service before copying
files back into place.
### A panel app
```bash
panelctl stop <app>
sudo restic-panel restore latest --tag panel \
--include /var/lib/containers/volumes/<app> --target /
panelctl deploy <app>
```
### A SQLite database from its dump
```bash
sudo systemctl stop forgejo
sudo -u forgejo mv /var/lib/forgejo/data/forgejo.db{,.broken}
sudo -u forgejo sqlite3 /var/lib/forgejo/data/forgejo.db < /root/restore/var/backup/sqlite/forgejo.sql
sudo systemctl start forgejo
```
## Disaster recovery: rebuilding the server
On NixOS, "the whole server" is this flake plus the state in the backup.
1. **Get a fresh NixOS VPS** (vpsAdminOS NixOS template) and SSH in as root.
If the IP address changed, update the DNS records for `*.reudy.net`.
2. **Get restic and the credentials** into the shell. Don't paste them on the
command line:
```bash
nix --extra-experimental-features 'nix-command flakes' shell nixpkgs#restic nixpkgs#git nixpkgs#rsync
read -rs -p 'Scaleway access key: ' AWS_ACCESS_KEY_ID; echo
read -rs -p 'Scaleway secret key: ' AWS_SECRET_ACCESS_KEY; echo
read -rs -p 'Restic password: ' RESTIC_PASSWORD; echo
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY RESTIC_PASSWORD
export AWS_DEFAULT_REGION=<region>
export RESTIC_REPOSITORY=s3:https://s3.<region>.scw.cloud/<bucket>/vps
restic snapshots
```
3. **Restore both latest snapshots to a staging directory**:
```bash
restic restore latest --tag system --target /restore
restic restore latest --tag panel --target /restore
```
4. **Put identity back first**: the SSH host keys (agenix decrypts the secrets
with them) and the uid/gid map (so restored files keep the right owners):
```bash
cp -a /restore/etc/ssh/ssh_host_* /etc/ssh/
cp -a /restore/var/lib/nixos/. /var/lib/nixos/
```
5. **Get the flake.** Forgejo isn't running yet, so clone it from the restored
repository, and point the `panel` input at the restored panel repository:
```bash
git clone /restore/var/lib/forgejo/repositories/reudy-net/nixos.git /root/nixos
cd /root/nixos
nixos-rebuild boot --flake .#vps \
--override-input panel 'git+file:///restore/var/lib/forgejo/repositories/reudy-net/panel.git?ref=main'
```
6. **Copy the state into place and boot into the new system:**
```bash
rsync -aHAX /restore/var/lib/ /var/lib/
rsync -aHAX /restore/home/ /home/
rsync -aHAX /restore/root/ /root/
reboot
```
Caddy fetches new certificates by itself. If a service won't start because
its database is damaged, load the dump from `/var/backup/sqlite` (above).
Then run `passwd reudy` if sudo doesn't accept the old password.
7. **Bring the panel apps back**: their compose files, volumes and settings are
restored, but the images are not:
```bash
panelctl list
panelctl deploy <app> # for each app
```
8. Once Forgejo is up, rebuild once more without `--override-input`, check
`systemctl --failed`, then delete `/restore`.
Practise this once on a throwaway VPS. Until a restore has worked, the backup
hasn't been tested.

View file

@ -45,7 +45,7 @@
totp = {
disable = false;
issuer = "reudy.net";
issuer = "srazka.com";
period = 30;
};
@ -70,9 +70,9 @@
cookies = [
{
domain = "reudy.net";
authelia_url = "https://auth.reudy.net";
default_redirection_url = "https://hello.reudy.net";
domain = "srazka.com";
authelia_url = "https://auth.srazka.com";
default_redirection_url = "https://hello.srazka.com";
}
];
};
@ -82,7 +82,7 @@
default_policy = "two_factor";
# You can add per-domain rules here later, e.g.:
# rules = [
# { domain = "hello.reudy.net"; policy = "two_factor"; }
# { domain = "hello.srazka.com"; policy = "two_factor"; }
# ];
};

View file

@ -1,176 +0,0 @@
{ config, lib, pkgs, ... }:
# Nightly restic backups to Scaleway Object Storage. BACKUPS.md covers what is
# kept, how to check on it and how to restore (including a full rebuild).
#
# Everything goes into one repository, as three jobs:
# system - host state: /etc, /home, /root, /var/lib (minus the panel)
# panel - panel apps: /var/lib/containers plus rootless named volumes,
# with the app containers stopped for a consistent copy
# maintenance - weekly forget/prune and a small check, no backup
#
# Scaleway doesn't charge for requests, only for stored GB-hours, egress over
# 75 GB a month and Glacier restores. So the settings below keep the stored size
# small and avoid downloads: no /nix/store (rebuilt from the flake), one shared
# local cache so metadata is fetched once, pruning only weekly, and checks that
# read a fixed 2 GB sample instead of the whole repository.
let
panel = config.services.reudy-panel;
# Rootless Podman keeps named volumes (and images) in the user's home.
panelPodmanStorage = "${config.users.users.${panel.user}.home}/.local/share/containers/storage";
# Consistent SQLite copies land here and are picked up by the system job.
sqliteDumpDir = "/var/backup/sqlite";
common = {
passwordFile = config.age.secrets."restic-password".path;
# RESTIC_REPOSITORY, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and
# AWS_DEFAULT_REGION; see BACKUPS.md for the format.
environmentFile = config.age.secrets."restic-env".path;
initialize = true;
extraOptions = [
# One Zone costs half as much as Multi-AZ and is enough for a copy that
# already lives away from the server. It only applies to newly uploaded
# objects, so changing it later doesn't move what's already stored.
"s3.storage-class=ONEZONE_IA"
];
};
# Copies each SQLite database with `.dump`, which reads inside a single
# transaction, so the copy is consistent even while the service writes. It
# runs as the database's owner so it can't leave root-owned -wal/-shm files
# behind that would lock the service out. A failed dump only warns: the live
# files are still backed up, and one bad database shouldn't stop the rest.
dumpSqlite = name: db: ''
if [ -f ${lib.escapeShellArg db} ]; then
owner=$(stat -c %U ${lib.escapeShellArg db})
if runuser -u "$owner" -- sqlite3 ${lib.escapeShellArg db} .dump > ${sqliteDumpDir}/${name}.sql.tmp; then
mv ${sqliteDumpDir}/${name}.sql.tmp ${sqliteDumpDir}/${name}.sql
else
echo "WARNING: dumping ${name} failed, keeping the previous dump" >&2
rm -f ${sqliteDumpDir}/${name}.sql.tmp
fi
else
echo "skipping ${name}: ${db} does not exist"
fi
'';
# Podman as the panel's user, the same way the panel itself runs it. The
# user has no fixed uid in this config, so look it up.
podmanAsPanelUser = ''
podman_panel() {
runuser -u ${panel.user} -- env XDG_RUNTIME_DIR="/run/user/$(id -u ${panel.user})" podman "$@"
}
'';
stoppedContainers = "/run/restic-backups-panel/stopped-containers";
in
{
age.secrets."restic-password".file = ./secrets/restic-password.age;
age.secrets."restic-env".file = ./secrets/restic-env.age;
systemd.tmpfiles.rules = [ "d ${sqliteDumpDir} 0700 root root -" ];
services.restic.backups = {
system = common // {
paths = [ "/etc" "/home" "/root" "/var/lib" "/var/backup" ];
exclude = [
# Backed up by the panel job, with the containers stopped.
"/var/lib/containers"
"${config.users.users.${panel.user}.home}/.local/share/containers"
# Regenerated on demand.
"/home/*/.cache"
"/root/.cache"
"/var/lib/systemd/coredump"
"${config.services.nextcloud.datadir}/data/appdata_*/preview"
];
extraBackupArgs = [ "--tag system" ];
backupPrepareCommand = ''
set -eu
${dumpSqlite "forgejo" config.services.forgejo.database.path}
${dumpSqlite "nextcloud" "${config.services.nextcloud.datadir}/data/${config.services.nextcloud.config.dbname}.db"}
${dumpSqlite "authelia" config.services.authelia.instances.main.settings.storage.local.path}
${dumpSqlite "panel" "${panel.baseDir}/state/panel/panel.db"}
'';
timerConfig = {
OnCalendar = "04:00";
RandomizedDelaySec = "10m";
Persistent = true;
};
};
panel = common // {
paths = [
panel.baseDir
"${panelPodmanStorage}/volumes"
];
exclude = [
# Rootful Podman's image store, if it is ever used. Images are pulled
# or rebuilt again on deploy, so they're not worth paying to store.
"${panel.baseDir}/storage"
"${panel.baseDir}/cache"
];
extraBackupArgs = [ "--tag panel" ];
# Stop the running app containers so databases inside them are copied
# in a consistent state. The cleanup command starts exactly these again,
# and runs even when the backup fails.
backupPrepareCommand = ''
set -eu
${podmanAsPanelUser}
podman_panel ps --quiet > ${stoppedContainers}
if [ -s ${stoppedContainers} ]; then
echo "stopping $(wc -l < ${stoppedContainers}) container(s)"
podman_panel stop --time 30 $(cat ${stoppedContainers})
fi
'';
backupCleanupCommand = ''
set -eu
${podmanAsPanelUser}
if [ -s ${stoppedContainers} ]; then
echo "starting $(wc -l < ${stoppedContainers}) container(s)"
podman_panel start $(cat ${stoppedContainers})
fi
'';
timerConfig = {
OnCalendar = "03:30";
RandomizedDelaySec = "10m";
Persistent = true;
};
};
# No paths, so this unit only forgets, prunes and checks.
maintenance = common // {
initialize = false;
pruneOpts = [
"--keep-daily 7"
"--keep-weekly 4"
"--keep-monthly 6"
];
# Without --with-cache, check re-downloads all metadata into a temp dir.
# The 2 GB sample spreads a real read of the data over the weeks while
# staying well inside the free egress.
checkOpts = [
"--with-cache"
"--read-data-subset=2G"
];
timerConfig = {
OnCalendar = "Sun 05:30";
Persistent = true;
};
};
};
systemd.services = lib.genAttrs
[ "restic-backups-system" "restic-backups-panel" "restic-backups-maintenance" ]
(_: {
# One cache for all jobs, so metadata is downloaded once instead of once
# per job. The restic-<job> wrappers pick this up from the unit too.
environment.RESTIC_CACHE_DIR = lib.mkForce "/var/cache/restic";
serviceConfig.CacheDirectory = lib.mkForce "restic";
path = [
"/run/wrappers" # newuidmap & co. for rootless Podman
config.virtualisation.podman.package
pkgs.sqlite
pkgs.util-linux
];
});
}

View file

@ -2,17 +2,20 @@
{
services.caddy = {
enable = true;
email = "admin@reudy.net";
email = "admin@srazka.com";
# App routes generated by the panel are imported by its module (panel.nix).
# Generated app routes from the panel backend.
extraConfig = ''
import /var/lib/containers/routes/routes.caddy
'';
# Authelia's own login portal
virtualHosts."auth.reudy.net".extraConfig = ''
virtualHosts."auth.srazka.com".extraConfig = ''
reverse_proxy 127.0.0.1:9091
'';
# Every protected site uses this snippet
virtualHosts."hello.reudy.net".extraConfig = ''
virtualHosts."hello.srazka.com".extraConfig = ''
forward_auth 127.0.0.1:9091 {
uri /api/authz/forward-auth
copy_headers Remote-User Remote-Groups Remote-Email Remote-Name
@ -23,7 +26,7 @@
# Nextcloud — served by local nginx
# (Authelia forward-auth disabled for now; re-enable by uncommenting the
# forward_auth block below.)
virtualHosts."nextcloud.reudy.net".extraConfig = ''
virtualHosts."nextcloud.srazka.com".extraConfig = ''
# forward_auth 127.0.0.1:9091 {
# uri /api/authz/forward-auth
# copy_headers Remote-User Remote-Groups Remote-Email Remote-Name
@ -33,7 +36,7 @@
# Forgejo — no Authelia forward-auth, since it would break git over HTTPS
# and the API. Forgejo handles its own logins.
virtualHosts."git.reudy.net".extraConfig = ''
virtualHosts."git.srazka.com".extraConfig = ''
reverse_proxy 127.0.0.1:14921
'';
};

22
flake.lock generated
View file

@ -149,32 +149,10 @@
"type": "github"
}
},
"panel": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1790531515,
"narHash": "sha256-EeanYOcMIrPGgJt3azJIFjdttHRU07jVY0Iw4awEdpg=",
"ref": "main",
"rev": "e11fc00840f2fe68a076ea56305ca67bd2ace332",
"revCount": 39,
"type": "git",
"url": "ssh://git@git.reudy.net:14922/reudy-net/panel"
},
"original": {
"ref": "main",
"type": "git",
"url": "ssh://git@git.reudy.net:14922/reudy-net/panel"
}
},
"root": {
"inputs": {
"agenix": "agenix",
"nixpkgs": "nixpkgs",
"panel": "panel",
"vpsadminos": "vpsadminos"
}
},

View file

@ -11,12 +11,6 @@
inputs.nixpkgs.follows = "nixpkgs";
inputs.darwin.follows = "";
};
# The deployment panel (panel.reudy.net), packaged in its own repository.
panel = {
url = "git+ssh://git@git.reudy.net:14922/reudy-net/panel?ref=main";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
@ -24,7 +18,6 @@
nixpkgs,
vpsadminos,
agenix,
panel,
...
}:
let
@ -38,13 +31,11 @@
{ nixpkgs.overlays = [ agenix.overlays.default ]; }
vpsadminos.nixosModules.container_25_11
./configuration.nix
panel.nixosModules.default
./panel.nix
./caddy.nix
./authelia.nix
./nextcloud.nix
./forgejo.nix
./backups.nix
./containers/hello.nix
agenix.nixosModules.default

View file

@ -1,6 +1,6 @@
{ config, ... }:
let
domain = "git.reudy.net";
domain = "git.srazka.com";
sshPort = 14922;
in
{
@ -24,7 +24,7 @@ in
# Forgejo's built-in SSH server, separate from the system sshd (which
# only allows 'reudy' on 14902). Clone URLs look like:
# ssh://git@git.reudy.net:14922/<user>/<repo>.git
# ssh://git@git.srazka.com:14922/<user>/<repo>.git
START_SSH_SERVER = true;
SSH_PORT = sshPort;
SSH_LISTEN_PORT = sshPort;

View file

@ -15,7 +15,7 @@
enable = true;
package = pkgs.nextcloud33;
hostName = "nextcloud.reudy.net";
hostName = "nextcloud.srazka.com";
https = true;
config = {
@ -28,7 +28,7 @@
settings = {
trusted_proxies = [ "127.0.0.1" ];
overwriteprotocol = "https";
overwritehost = "nextcloud.reudy.net";
overwritehost = "nextcloud.srazka.com";
default_phone_region = "NL";
};
};

View file

@ -1,14 +1,89 @@
{ ... }:
{ config, lib, pkgs, ... }:
# The panel itself lives in its own repository (reudy-net/panel) and is pulled
# in as the `panel` flake input. To deploy a new panel version:
# nix flake update panel && sudo nixos-rebuild switch --flake .#vps
let
forgejoServer = config.services.forgejo.settings.server;
in
{
services.reudy-panel = {
enable = true;
domain = "panel.reudy.net";
autheliaAddress = "127.0.0.1:9091";
# Runs as reudy with the shared panelroutes group, keeps everything in
# /var/lib/containers and picks up the local Forgejo automatically.
environment.systemPackages = [
(pkgs.writeShellScriptBin "panelctl" (builtins.readFile ./panel/panelctl.sh))
];
users.groups.panelroutes = { };
users.users.reudy.extraGroups = [ "panelroutes" ];
users.users.caddy.extraGroups = [ "panelroutes" ];
systemd.tmpfiles.rules = [
"d /var/lib/containers 0750 reudy panelroutes -"
"d /var/lib/containers/stacks 0750 reudy panelroutes -"
"d /var/lib/containers/volumes 0750 reudy panelroutes -"
"d /var/lib/containers/routes 0750 reudy panelroutes -"
"d /var/lib/containers/state 0750 reudy panelroutes -"
"d /var/lib/containers/state/apps 0750 reudy panelroutes -"
"d /var/lib/containers/backups 0750 reudy panelroutes -"
"f /var/lib/containers/routes/routes.caddy 0640 reudy panelroutes -"
];
systemd.services.panel-api = {
description = "Minimal panel API service";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = [
pkgs.podman
pkgs.podman-compose
pkgs.curl
pkgs.coreutils
pkgs.zip
pkgs.unzip
pkgs.git
pkgs.util-linux # flock, used by panelctl to serialise routes file writes
pkgs.openssh # cloning repositories over ssh with the panel's deploy key
];
serviceConfig = {
Type = "simple";
User = "reudy";
Group = "panelroutes";
Restart = "always";
RestartSec = 3;
WorkingDirectory = "/var/lib/containers";
ExecStart = "${pkgs.python3}/bin/python3 ${./panel/panel-api.py}";
};
environment = {
PANEL_API_BIND = "127.0.0.1";
PANEL_API_PORT = "9911";
PANEL_BASE_DIR = "/var/lib/containers";
PANELCTL_PATH = "/run/current-system/sw/bin/panelctl";
PANEL_FRONTEND_DIR = "${./panel/frontend}";
# Forgejo integration (repo picker, private clones, commit links).
# The API is reached on localhost; clones use the public URLs.
PANEL_FORGEJO_URL = lib.removeSuffix "/" forgejoServer.ROOT_URL;
PANEL_FORGEJO_API_URL = "http://${forgejoServer.HTTP_ADDR}:${toString forgejoServer.HTTP_PORT}";
PANEL_FORGEJO_SSH_URL = "ssh://${forgejoServer.BUILTIN_SSH_SERVER_USER}@${forgejoServer.DOMAIN}:${toString forgejoServer.SSH_PORT}";
};
};
}
services.caddy.virtualHosts."panel.srazka.com".extraConfig = ''
forward_auth 127.0.0.1:9091 {
uri /api/authz/forward-auth
copy_headers Remote-User Remote-Groups Remote-Email Remote-Name
}
reverse_proxy 127.0.0.1:9911
'';
systemd.paths."caddy-routes-reload" = {
wantedBy = [ "multi-user.target" ];
pathConfig = {
PathChanged = "/var/lib/containers/routes/routes.caddy";
};
};
systemd.services."caddy-routes-reload" = {
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.systemd}/bin/systemctl reload caddy.service";
};
};
}

287
panel/API.md Normal file
View file

@ -0,0 +1,287 @@
# panel-api
HTTP API wrapper around panelctl with a web UI.
Default bind: `127.0.0.1:9911`
## Endpoints
### Health & UI
| Method | Path | Description |
|--------|------|-------------|
| GET | `/` | Web UI (served from `frontend/index.html`) |
| GET | `/health` | Health check |
| GET | `/status` | All apps with routes, container status and running operation (what the UI polls) |
| GET | `/integrations` | Forgejo connection (`configured`, `url`, `has_token`, `user`) and the SSH deploy public key |
| POST | `/integrations/forgejo` | `{"token": "..."}` — verify against Forgejo and store; `""` disconnects |
| GET | `/forgejo/repos?q=` | Search repositories visible to the stored token (public ones without) |
| GET | `/forgejo/branches?repo=owner/name` | Branch names of a Forgejo repository |
### Apps — Read
| Method | Path | Description |
|--------|------|-------------|
| GET | `/apps` | List all apps |
| GET | `/apps/<name>` | Show single app manifest |
| GET | `/apps/<name>/routes` | Get parsed route entries |
| GET | `/apps/<name>/status` | Container status (running/stopped) |
| GET | `/apps/<name>/compose` | Read compose.yaml content |
| GET | `/apps/<name>/logs?tail=N` | Fetch last N log lines (default 100) |
| GET | `/apps/<name>/backups` | List available backups |
| GET | `/apps/<name>/backups/<file>` | Download backup zip |
| GET | `/apps/<name>/env` | Environment variables: `{"vars": [{"key", "value"}], "inject": true}` |
| GET | `/apps/<name>/repo` | Git source info (URL, web URL, provider, branch, deployed commit, local changes, deploy key for ssh) |
| GET | `/apps/<name>/repo?fetch=1` | Same, plus fetches the remote and reports `behind` / `remote` |
| GET | `/apps/<name>/volumes` | Volumes the file browser can open |
| GET | `/apps/<name>/volume/files?vol=&path=` | List a folder in a volume |
| GET | `/apps/<name>/volume/download?vol=&path=` | Download a file from a volume |
| PUT | `/apps/<name>/volume/files?vol=&path=` | Upload a file (raw body) |
| DELETE | `/apps/<name>/volume/files?vol=&path=` | Delete a file or folder |
### Apps — Write
| Method | Path | Description |
|--------|------|-------------|
| POST | `/apps/init` | Create a new app |
| POST | `/apps/<name>/routes` | Update routes (hot — Caddy reloads automatically) |
| POST | `/apps/<name>/deploy` | Deploy (compose up + caddy reload) |
| POST | `/apps/<name>/restart` | Restart (compose down + up) |
| POST | `/apps/<name>/stop` | Stop (compose down) |
| POST | `/apps/<name>/render-route` | Re-render Caddy route |
| POST | `/apps/<name>/compose` | Save compose.yaml content |
| POST | `/apps/<name>/validate-compose` | Validate compose file |
| POST | `/apps/<name>/backup` | Create volume backup (zip) |
| POST | `/apps/<name>/restore` | Restore from backup |
| POST | `/apps/<name>/remove` | Remove app |
| POST | `/apps/<name>/repo-pull` | Git apps: fetch branch, hard-reset checkout to it, redeploy |
| POST | `/apps/<name>/env` | Replace environment variables: `{"vars": [...], "inject": true, "deploy": false}` |
| POST | `/apps/<name>/volume-clear` | Stop the app and empty its default data folder |
Write operations are serialised per app. While one runs, another write to the
same app returns `409` with `{"ok": false, "error": "...", "busy": "deploy"}`.
`deploy` returns the compose output in `stdout` (or `stderr` on failure).
### Create app (git repository, with environment variables)
```json
{
"name": "blog",
"routes": [{"domain": "blog.srazka.com", "upstream": "127.0.0.1:18090"}],
"auth": true,
"source_type": "git",
"repo_url": "https://git.srazka.com/reudy-net/blog.git",
"repo_branch": "",
"use_forgejo_token": true,
"env": [{"key": "DATABASE_URL", "value": "postgres://..."}],
"env_inject": true
}
```
- `repo_url` may be `https://…`, `ssh://git@host:port/owner/repo.git` or
`git@host:owner/repo.git`. ssh URLs use the panel's deploy key.
- `repo_token` sets an https token explicitly; `use_forgejo_token` uses the
token stored in Settings (only for URLs on the configured Forgejo host).
- An empty branch uses the repository's default branch. The compose file must
be at the repository root.
- The older `source_type: "github"` with `github_url` / `github_branch` /
`github_pat` is still accepted.
### Sync response (`repo-pull`)
```json
{
"ok": true,
"stdout": "HEAD is now at d7df557 Bump image tag\n...compose output...",
"before": {"sha": "4fb7976...", "short": "4fb7976", "subject": "Initial compose", "author": "reudy", "time": 1790460618},
"after": {"sha": "d7df557...", "short": "d7df557", "subject": "Bump image tag", "author": "reudy", "time": 1790460643},
"changed": true
}
```
### Status response (`/status`)
```json
{
"ok": true,
"time": 1790460650,
"apps": [
{
"name": "whoami",
"routes": [{"domain": "whoami.srazka.com", "upstream": "127.0.0.1:18080"}],
"auth": true,
"compose_file": "/var/lib/containers/stacks/whoami/compose.yaml",
"repo_url": "",
"repo_branch": "",
"busy": null,
"status": {
"state": "running",
"running": true,
"running_count": 1,
"total": 1,
"containers": [{"name": "whoami-app-1", "state": "running", "status": "Up 3 minutes", "image": "docker.io/traefik/whoami:latest", "running": true}]
}
}
]
}
```
`state` is one of `running`, `partial` (some containers down), `stopped` or `unknown`.
## Example payloads
### Create app (single route)
```json
{
"name": "whoami",
"routes": [
{"domain": "whoami.srazka.com", "upstream": "127.0.0.1:18080"}
],
"auth": true
}
```
### Create app (multiple routes, different ports)
```json
{
"name": "myapp",
"routes": [
{"domain": "app.srazka.com", "upstream": "127.0.0.1:18080"},
{"domain": "api.app.srazka.com", "upstream": "127.0.0.1:18081"}
],
"auth": true
}
```
### Create app (multiple routes, different ports, with paths)
```json
{
"name": "pocketbase",
"routes": [
{"domain": "pb.srazka.com", "upstream": "127.0.0.1:8090", "path": "/_/*"}
],
"auth": true
}
```
The `path` field is optional. When present, it generates a Caddy `reverse_proxy /_/* 127.0.0.1:8090` rule, letting you route requests to a specific path prefix within a domain.
### Create app (wildcard domain)
```json
{
"name": "wildcard",
"routes": [
{"domain": "*.srazka.com", "upstream": "127.0.0.1:18082"}
],
"auth": false
}
```
Note: Wildcard domains require DNS challenge configuration in Caddy.
### Update routes (hot)
```json
{
"routes": [
{"domain": "app.srazka.com", "upstream": "127.0.0.1:18080"},
{"domain": "api.srazka.com", "upstream": "127.0.0.1:18081"},
{"domain": "pb.srazka.com", "upstream": "127.0.0.1:8090", "path": "/_/*"}
]
}
```
The optional `path` field generates a Caddy `reverse_proxy <path> <upstream>` rule for sub-path routing.
Caddy reloads automatically via the systemd path watcher. Containers stay running.
### Save compose
```json
{
"content": "services:\n app:\n image: nginx:latest\n ports:\n - '127.0.0.1:18080:80'\n"
}
```
### Remove and keep volumes
```json
{
"keepVolumes": true
}
```
### Restore from backup
```json
{
"file": "whoami-20260101-120000.zip"
}
```
## Routes response
```json
{
"ok": true,
"name": "myapp",
"routes": [
{"domain": "app.srazka.com", "upstream": "127.0.0.1:18080"},
{"domain": "api.srazka.com", "upstream": "127.0.0.1:18081", "path": "/api/*"}
]
}
```
The `path` field is only present when a route has a path configured.
## Response format
All JSON responses include an `ok` boolean:
```json
{
"ok": true,
"apps": [...]
}
```
Error responses:
```json
{
"ok": false,
"error": "description",
"stderr": "panelctl error output"
}
```
## Status response
```json
{
"ok": true,
"name": "whoami",
"running": true,
"containers": [
{
"name": "whoami-app-1",
"state": "running",
"image": "docker.io/traefik/whoami:latest"
}
]
}
```
## Local test
```bash
curl -s http://127.0.0.1:9911/health | jq .
curl -s http://127.0.0.1:9911/apps | jq .
curl -s http://127.0.0.1:9911/apps/whoami/status | jq .
curl -s http://127.0.0.1:9911/apps/whoami/logs?tail=50 | jq .
curl -s http://127.0.0.1:9911/apps/whoami/backups | jq .
```

165
panel/README.md Normal file
View file

@ -0,0 +1,165 @@
# panelctl quickstart
Minimal container management panel for rootless Podman + Caddy.
## Base directory
`/var/lib/containers`
## Generated structure
```
/var/lib/containers/
├── stacks/<app>/compose.yaml # Compose file per app
├── volumes/<app>/data # Persistent volumes
├── routes/routes.caddy # Single aggregate Caddy routes file
├── backups/<app>-<timestamp>.zip # Volume backups
└── state/apps/<app>.env # App manifest
```
All app routes are written to a single `routes/routes.caddy` file that Caddy imports.
## Quick workflow
```bash
# Routes are "domain|upstream[|path]" entries, comma-separated.
# Create a new app (single route, protected by Authelia)
panelctl init whoami "whoami.srazka.com|127.0.0.1:18080" true
# Create with multiple routes (different ports, optional path)
panelctl init myapp "app.srazka.com|127.0.0.1:18081,api.srazka.com|127.0.0.1:18082|/api/*" true
# Create with wildcard domain (requires DNS challenge in Caddy)
panelctl init wild "*.srazka.com|127.0.0.1:18083" false
# Change routes later (Caddy reloads automatically)
panelctl set-routes whoami "whoami.srazka.com|127.0.0.1:18080,who.srazka.com|127.0.0.1:18080"
# Deploy (compose up + caddy reload)
panelctl deploy whoami
# Check container status
panelctl status whoami
# View logs
panelctl logs whoami --tail 50
# Restart containers
panelctl restart whoami
# Stop containers
panelctl stop whoami
# Validate compose file
panelctl validate-compose whoami
# Backup volumes to zip
panelctl backup whoami
# List backups
panelctl list-backups whoami
# Restore from backup
panelctl restore whoami whoami-20260101-120000.zip
# List all apps
panelctl list
# Show app manifest
panelctl show whoami
# Remove app (keeps volumes)
panelctl remove whoami --keep-volumes
# Remove app and all data
panelctl remove whoami
```
## Notes
- The default compose file uses `traefik/whoami` for smoke testing — edit before production use.
- App names must be lowercase slugs (`[a-z0-9-]`).
- Wildcard domains (`*.example.com`) require DNS challenge in Caddy (provider-specific).
- Backups stop containers for consistency, then restart if they were running.
- If deploy reports `XDG_RUNTIME_DIR` missing, enable lingering:
```
sudo loginctl enable-linger reudy
```
## Web UI & API
- Nix runs `panel-api` as a systemd service on `127.0.0.1:9911`.
- Caddy proxies `https://panel.srazka.com` → panel-api with Authelia forward_auth.
- Open `https://panel.srazka.com` for the web UI.
- API docs: [API.md](API.md)
### Web UI features
- Live status: one `/status` poll every few seconds (faster while something is
running, paused when the tab is hidden) updates cards in place, so open tabs,
unsaved edits and scroll positions are never lost. The header shows when the
panel last synced and warns when the Authelia session has expired.
- Per-app status (running / partial / stopped), container list, and a busy
indicator that is shared between browsers while an operation runs.
- New-app dialog: starter container, pasted compose file or git repository;
suggests the next free port and a domain based on the app name.
- Compose editor with unsaved-changes tracking, Ctrl+S, save & deploy, validate.
- Logs with follow mode, routes editor with validation, file browser with
drag-and-drop upload, backups with restore (and optional redeploy).
- Git source tab: deployed commit, "check for updates", and sync & deploy.
- Activity drawer with the output of every operation (e.g. why a deploy failed).
- Keyboard: `/` search, `N` new app, `Esc` closes menus. Deep links like
`#/whoami/logs` open an app on a specific tab.
### Git-backed apps
Apps created from a repository (Forgejo, GitHub or any git host, over https or
ssh) are cloned to `stacks/<app>/repo`.
**Forgejo.** `panel.nix` points the panel at the local Forgejo
(`PANEL_FORGEJO_URL`, `PANEL_FORGEJO_API_URL`, `PANEL_FORGEJO_SSH_URL`, taken
from `forgejo.nix`). In the panel's **Settings** you can connect a Forgejo
access token (read access to repositories and user). With it, the new-app
dialog lists your repositories and branches, and private ones are cloned over
https with the token. Without it, public repositories are listed and private
ones are cloned over ssh with the deploy key. Commit and compare links point at
Forgejo. The token is stored in `state/panel/forgejo-token` (mode 0600).
**SSH / deploy key.** The panel generates an ed25519 key pair in
`state/panel/ssh/` the first time it is needed. Its public half is shown in
Settings (and next to ssh URLs); add it as a read-only deploy key to a
repository — or to your Forgejo account for access to all repositories — to
clone `ssh://git@git.srazka.com:14922/owner/repo.git` style URLs. **Sync** fetches the configured branch and
hard-resets the checkout to it before redeploying, so the repository is the
source of truth: compose edits made in the panel are discarded on the next sync
(the UI warns about this). An access token for a private repository is stored in
the clone's `.git/config`; use a read-only token.
### Environment variables
Each app can have environment variables (the **Environment** tab, or when
creating the app; `.env` text can be pasted in). They are stored in
`state/env/<app>.env` as `KEY=VALUE` lines (mode 0600) — outside the repository
and stack directory, so git syncs never touch them — and `panelctl` passes them
to every compose command:
- They are always available for `${VAR}` interpolation in the compose file.
The UI points out variables the compose file uses without a default that
aren't set.
- With **Pass to every container** (the default), `deploy`/`restart` also
generate `stacks/<app>/.panel-env.yaml`, a compose override that lists the
keys under every service's `environment:`. Compose reads the values from its
own environment, so they are never quoted into YAML, and they take
precedence over values set in the compose file.
Values must be single-line. Names that would change how podman/compose run
(`PATH`, `HOME`, `XDG_*`, `DOCKER_*`, `COMPOSE_*`, `PODMAN_*`, …) are rejected.
Changes apply on the next deploy. Backups do not include variables.
### Concurrency
`panel-api` handles requests concurrently, so a long deploy never blocks status
or logs. Mutating operations are serialised per app — a second operation on a
busy app gets HTTP 409 — and `panelctl` takes a `flock` on the shared routes
file while rewriting it.

2877
panel/frontend/index.html Normal file

File diff suppressed because it is too large Load diff

1591
panel/panel-api.py Normal file

File diff suppressed because it is too large Load diff

913
panel/panelctl.sh Normal file
View file

@ -0,0 +1,913 @@
#!/usr/bin/env bash
set -euo pipefail
BASE_DIR="${PANEL_BASE_DIR:-/var/lib/containers}"
STACKS_DIR="${BASE_DIR}/stacks"
VOLUMES_DIR="${BASE_DIR}/volumes"
ROUTES_DIR="${BASE_DIR}/routes"
STATE_DIR="${BASE_DIR}/state"
APPS_DIR="${STATE_DIR}/apps"
ENV_DIR="${STATE_DIR}/env"
BACKUPS_DIR="${BASE_DIR}/backups"
# Set by load_app: compose file arguments, and the app's environment variables
# as KEY=VALUE words (passed to compose via env(1), never sourced).
COMPOSE_ARGS=()
APP_ENV_ARGS=()
APP_ENV_KEYS=()
FORWARD_AUTH_BLOCK=' forward_auth 127.0.0.1:9091 {
uri /api/authz/forward-auth
copy_headers Remote-User Remote-Groups Remote-Email Remote-Name
}
'
validate_route_entry() {
local entry="$1"
# Format: domain|upstream[/path] or domain|upstream (path is optional)
IFS='|' read -r domain upstream path <<< "${entry}"
[[ -n "${domain}" ]] || fail "empty domain in route entry '${entry}'"
[[ -n "${upstream}" ]] || fail "empty upstream in route entry '${entry}'"
validate_single_domain "${domain}"
# Validate upstream has a port
local upstream_port="${upstream##*:}"
[[ "${upstream_port}" =~ ^[0-9]+$ ]] || fail "upstream '${upstream}' missing numeric port in route entry '${entry}'"
validate_port "${upstream_port}"
if [[ -n "${path}" ]]; then
[[ "${path}" == /* ]] || fail "path '${path}' must start with / in route entry '${entry}'"
fi
}
validate_routes() {
local routes_str="$1"
IFS=',' read -ra entries <<< "${routes_str}"
[[ ${#entries[@]} -ge 1 ]] || fail "at least one route is required"
for entry in "${entries[@]}"; do
entry="$(echo "${entry}" | xargs)"
validate_route_entry "${entry}"
done
}
usage() {
cat <<'EOF'
panelctl - minimal app panel helper
Usage:
panelctl init <name> "<domain>|<upstream>[,...]" [auth]
panelctl set-routes <name> "<domain>|<upstream>[,...]"
panelctl render-route <name>
panelctl deploy <name>
panelctl restart <name>
panelctl stop <name>
panelctl status <name>
panelctl logs <name> [--tail N]
panelctl remove <name> [--keep-volumes]
panelctl backup <name>
panelctl list-backups <name>
panelctl restore <name> <backup-file>
panelctl volume-clear <name>
panelctl validate-compose <name>
panelctl list
panelctl show <name>
Each route is a domain|upstream pair. Upstream is host:port.
Multiple routes are comma-separated:
panelctl init myapp "app.example.com|127.0.0.1:18080,api.example.com|127.0.0.1:18081" true
Wildcard domains are supported (requires DNS challenge in Caddy):
panelctl init myapp "*.example.com|127.0.0.1:18080" true
Examples:
panelctl init whoami "whoami.srazka.com|127.0.0.1:18080" true
panelctl deploy whoami
panelctl restart whoami
panelctl status whoami
panelctl logs whoami --tail 50
panelctl backup whoami
panelctl list-backups whoami
panelctl restore whoami whoami-20260101-120000.zip
EOF
}
fail() {
echo "error: $*" >&2
exit 1
}
log() {
local level="${1:-info}"
local msg="${2:-}"
echo "${msg}" | systemd-cat -t panelctl -p "${level}" 2>/dev/null || true
}
ensure_base_dirs() {
mkdir -p "${STACKS_DIR}" "${VOLUMES_DIR}" "${ROUTES_DIR}" "${APPS_DIR}" "${BACKUPS_DIR}"
}
validate_name() {
local name="$1"
[[ "${name}" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]] || fail "invalid name '${name}' (use lowercase slug)"
}
validate_single_domain() {
local domain="$1"
# Allow wildcard prefix *.
local check="${domain}"
if [[ "${check}" == \*.* ]]; then
check="${check#\*.}"
fi
[[ "${check}" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]] || fail "invalid domain '${domain}'"
[[ "${domain}" == *.* ]] || fail "domain '${domain}' must include a dot"
}
validate_domains() {
local domains_str="$1"
IFS=',' read -ra domains <<< "${domains_str}"
[[ ${#domains[@]} -ge 1 ]] || fail "at least one domain is required"
for d in "${domains[@]}"; do
d="$(echo "${d}" | xargs)" # trim whitespace
validate_single_domain "${d}"
done
}
validate_port() {
local port="$1"
[[ "${port}" =~ ^[0-9]+$ ]] || fail "port must be numeric"
(( port >= 1024 && port <= 65535 )) || fail "port must be in range 1024-65535"
}
app_manifest() {
local name="$1"
echo "${APPS_DIR}/${name}.env"
}
app_stack_dir() {
local name="$1"
echo "${STACKS_DIR}/${name}"
}
app_volume_dir() {
local name="$1"
echo "${VOLUMES_DIR}/${name}"
}
# All routes go into a single aggregate file that Caddy imports.
app_route_file() {
echo "${ROUTES_DIR}/routes.caddy"
}
# The routes file is shared by all apps and rewritten read-modify-write, so
# concurrent panelctl runs (the API handles requests in parallel) must take turns.
routes_lock() {
exec 9>"${ROUTES_DIR}/.routes.lock"
if command -v flock >/dev/null 2>&1; then
flock -w 30 9 || fail "timed out waiting for the routes file lock"
fi
}
routes_unlock() {
exec 9>&-
}
load_app() {
local name="$1"
local manifest
manifest="$(app_manifest "${name}")"
[[ -f "${manifest}" ]] || fail "app '${name}' does not exist"
# shellcheck disable=SC1090
source "${manifest}"
# Backward compat: migrate old APP_DOMAIN/APP_PORT/APP_UPSTREAM to APP_ROUTES
if [[ -z "${APP_ROUTES:-}" && -n "${APP_DOMAIN:-}" ]]; then
local upstream="${APP_UPSTREAM:-127.0.0.1:${APP_PORT:-18080}}"
local routes=""
local domains_str="${APP_DOMAINS:-${APP_DOMAIN}}"
IFS=',' read -ra domain_arr <<< "${domains_str}"
for d in "${domain_arr[@]}"; do
d="$(echo "${d}" | xargs)"
if [[ -n "${routes}" ]]; then
routes="${routes},${d}|${upstream}"
else
routes="${d}|${upstream}"
fi
done
APP_ROUTES="${routes}"
fi
load_app_env "${name}"
}
app_env_file() {
echo "${ENV_DIR}/$1.env"
}
# Generated compose override that passes the app's variables into every service.
app_env_override() {
echo "${STACKS_DIR}/$1/.panel-env.yaml"
}
load_app_env() {
local name="$1"
local file line key override
file="$(app_env_file "${name}")"
APP_ENV_ARGS=()
APP_ENV_KEYS=()
if [[ -f "${file}" ]]; then
while IFS= read -r line || [[ -n "${line}" ]]; do
[[ -z "${line}" || "${line}" == \#* || "${line}" != *=* ]] && continue
key="${line%%=*}"
[[ "${key}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue
APP_ENV_ARGS+=("${line}")
APP_ENV_KEYS+=("${key}")
done <"${file}"
fi
COMPOSE_ARGS=(-f "${APP_COMPOSE_FILE}")
override="$(app_env_override "${name}")"
if [[ -f "${override}" ]]; then
COMPOSE_ARGS+=(-f "${override}")
fi
}
# (Re)generate the env override before containers are created. Variables are
# always available for ${VAR} interpolation; with APP_ENV_INJECT (default true)
# every service also receives them. Bare keys make compose read the values from
# its own environment, so values never have to be quoted into YAML.
prepare_env_override() {
local name="$1"
local override services svc key tmp
override="$(app_env_override "${name}")"
if [[ ${#APP_ENV_KEYS[@]} -eq 0 || "${APP_ENV_INJECT:-true}" != "true" ]]; then
rm -f "${override}"
COMPOSE_ARGS=(-f "${APP_COMPOSE_FILE}")
return
fi
services="$(run_compose -f "${APP_COMPOSE_FILE}" config --services 2>/dev/null)" \
|| fail "could not list compose services to pass environment variables (is the compose file valid?)"
tmp="$(mktemp)"
{
echo "# Generated by panelctl from the app's environment variables. Do not edit."
echo "services:"
while IFS= read -r svc; do
[[ "${svc}" =~ ^[A-Za-z0-9._-]+$ ]] || continue
printf ' "%s":\n environment:\n' "${svc}"
for key in "${APP_ENV_KEYS[@]}"; do
printf ' - %s\n' "${key}"
done
done <<<"${services}"
} >"${tmp}"
install -m 0640 "${tmp}" "${override}"
rm -f "${tmp}"
COMPOSE_ARGS=(-f "${APP_COMPOSE_FILE}" -f "${override}")
}
compose_command() {
local podman_bin=""
local podman_compose_bin=""
if command -v podman >/dev/null 2>&1; then
podman_bin="$(command -v podman)"
elif [[ -x /run/current-system/sw/bin/podman ]]; then
podman_bin="/run/current-system/sw/bin/podman"
fi
if command -v podman-compose >/dev/null 2>&1; then
podman_compose_bin="$(command -v podman-compose)"
elif [[ -x /run/current-system/sw/bin/podman-compose ]]; then
podman_compose_bin="/run/current-system/sw/bin/podman-compose"
fi
if [[ -n "${podman_bin}" ]] && "${podman_bin}" compose version >/dev/null 2>&1; then
echo "${podman_bin} compose"
return
fi
if [[ -n "${podman_compose_bin}" ]]; then
echo "${podman_compose_bin}"
return
fi
fail "no compose command available (need 'podman compose' or 'podman-compose')"
}
ensure_podman_runtime_env() {
local uid
uid="$(id -u)"
if [[ -z "${HOME:-}" ]]; then
HOME="$(getent passwd "${uid}" | cut -d: -f6 || true)"
export HOME
fi
if [[ -z "${XDG_RUNTIME_DIR:-}" ]]; then
XDG_RUNTIME_DIR="/run/user/${uid}"
export XDG_RUNTIME_DIR
fi
if [[ ! -d "${XDG_RUNTIME_DIR}" ]]; then
fail "XDG_RUNTIME_DIR '${XDG_RUNTIME_DIR}' does not exist for uid ${uid}. Ensure user runtime is available (e.g. loginctl enable-linger $(id -un))."
fi
if [[ -z "${DBUS_SESSION_BUS_ADDRESS:-}" && -S "${XDG_RUNTIME_DIR}/bus" ]]; then
DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"
export DBUS_SESSION_BUS_ADDRESS
fi
unset DOCKER_HOST
unset CONTAINER_HOST
}
run_compose() {
local compose
compose="$(compose_command)"
ensure_podman_runtime_env
if [[ "${compose}" == *" compose" ]]; then
local podman_bin="${compose% compose}"
env "${APP_ENV_ARGS[@]}" "${podman_bin}" compose "$@"
return
fi
env "${APP_ENV_ARGS[@]}" "${compose}" "$@"
}
write_default_compose() {
local name="$1"
local routes="$2"
local stack_dir
local volume_dir
stack_dir="$(app_stack_dir "${name}")"
volume_dir="$(app_volume_dir "${name}")"
# Use first route's upstream port for the default compose mapping
local first_route="${routes%%,*}"
local first_upstream="${first_route#*|}"
local container_port="${first_upstream##*:}"
cat >"${stack_dir}/compose.yaml" <<EOF
services:
app:
image: docker.io/traefik/whoami:latest
restart: unless-stopped
ports:
- "127.0.0.1:${container_port}:80"
volumes:
- ${volume_dir}/data:/data
EOF
}
write_manifest() {
local name="$1"
local routes="$2"
local auth="$3"
local manifest
local stack_dir
local volume_dir
local route_file
manifest="$(app_manifest "${name}")"
stack_dir="$(app_stack_dir "${name}")"
volume_dir="$(app_volume_dir "${name}")"
route_file="$(app_route_file)"
cat >"${manifest}" <<EOF
APP_NAME="${name}"
APP_ROUTES="${routes}"
APP_AUTH_PROTECTED="${auth}"
APP_STACK_DIR="${stack_dir}"
APP_COMPOSE_FILE="${stack_dir}/compose.yaml"
APP_VOLUME_DIR="${volume_dir}"
APP_ROUTE_FILE="${route_file}"
EOF
}
cmd_init() {
local name="$1"
local routes="$2"
local auth="${3:-true}"
validate_name "${name}"
validate_routes "${routes}"
[[ "${auth}" == "true" || "${auth}" == "false" ]] || fail "auth must be true or false"
ensure_base_dirs
local manifest
local stack_dir
local volume_dir
manifest="$(app_manifest "${name}")"
stack_dir="$(app_stack_dir "${name}")"
volume_dir="$(app_volume_dir "${name}")"
[[ ! -f "${manifest}" ]] || fail "app '${name}' already exists"
mkdir -p "${stack_dir}" "${volume_dir}/data"
write_default_compose "${name}" "${routes}"
write_manifest "${name}" "${routes}" "${auth}"
cmd_render_route "${name}"
log info "initialized app '${name}'"
}
cmd_render_route() {
local name="$1"
validate_name "${name}"
load_app "${name}"
local auth_block=""
if [[ "${APP_AUTH_PROTECTED}" == "true" ]]; then
auth_block="${FORWARD_AUTH_BLOCK}"
fi
local route_file
route_file="$(app_route_file)"
routes_lock
# Strip any existing block for this app from the aggregate file.
local tmp
tmp="$(mktemp)"
if [[ -f "${route_file}" ]]; then
sed "/^# route:${name}:start$/,/^# route:${name}:end$/d" "${route_file}" >"${tmp}" || true
else
printf "" >"${tmp}"
fi
{
printf "# route:%s:start\n" "${name}"
IFS=',' read -ra route_entries <<< "${APP_ROUTES}"
for entry in "${route_entries[@]}"; do
entry="$(echo "${entry}" | xargs)"
IFS='|' read -r domain upstream path <<< "${entry}"
# If upstream is empty (no second pipe), this is the old format
if [[ -z "${upstream}" ]]; then
upstream="${path}"
path=""
fi
if [[ -n "${path}" ]]; then
if [[ -n "${auth_block}" ]]; then
printf "%s {\n%s reverse_proxy %s %s\n}\n" "${domain}" "${auth_block}" "${path}" "${upstream}"
else
printf "%s {\n reverse_proxy %s %s\n}\n" "${domain}" "${path}" "${upstream}"
fi
else
if [[ -n "${auth_block}" ]]; then
printf "%s {\n%s reverse_proxy %s\n}\n" "${domain}" "${auth_block}" "${upstream}"
else
printf "%s {\n reverse_proxy %s\n}\n" "${domain}" "${upstream}"
fi
fi
done
printf "# route:%s:end\n" "${name}"
} >>"${tmp}"
install -m 0664 -o reudy -g panelroutes "${tmp}" "${route_file}"
rm -f "${tmp}"
routes_unlock
log info "rendered route ${route_file}"
}
cmd_deploy() {
local name="$1"
validate_name "${name}"
load_app "${name}"
log info "Starting deployment for app '${name}'"
cmd_render_route "${name}"
prepare_env_override "${name}"
# Capture compose output so callers (the web UI) can show why a deploy failed,
# and still forward it to the journal.
local output
if ! output="$(run_compose "${COMPOSE_ARGS[@]}" up -d --build --remove-orphans 2>&1)"; then
printf '%s\n' "${output}" | systemd-cat -t panelctl -p err 2>/dev/null || true
printf '%s\n' "${output}" >&2
log err "Deployment failed for app '${name}'"
fail "compose up failed"
fi
printf '%s\n' "${output}" | systemd-cat -t panelctl -p info 2>/dev/null || true
printf '%s\n' "${output}"
log info "Successfully deployed app '${name}'"
}
cmd_restart() {
local name="$1"
validate_name "${name}"
load_app "${name}"
log info "Restarting app '${name}'"
run_compose "${COMPOSE_ARGS[@]}" down --remove-orphans || fail "compose down failed"
prepare_env_override "${name}"
if ! run_compose "${COMPOSE_ARGS[@]}" up -d --build --remove-orphans 2>&1; then
fail "compose up failed during restart"
fi
log info "restarted app '${name}'"
}
cmd_stop() {
local name="$1"
validate_name "${name}"
load_app "${name}"
run_compose "${COMPOSE_ARGS[@]}" down --remove-orphans || fail "compose down failed"
log info "stopped app '${name}'"
}
cmd_status() {
local name="$1"
validate_name "${name}"
load_app "${name}"
run_compose "${COMPOSE_ARGS[@]}" ps --format json 2>/dev/null || \
run_compose "${COMPOSE_ARGS[@]}" ps 2>/dev/null || \
log info "no containers running"
}
cmd_logs() {
local name="$1"
shift
validate_name "${name}"
load_app "${name}"
local tail_lines="100"
while [[ $# -gt 0 ]]; do
case "$1" in
--tail)
tail_lines="$2"
shift 2
;;
*)
shift
;;
esac
done
run_compose "${COMPOSE_ARGS[@]}" logs --tail "${tail_lines}" 2>&1 || log info "no logs available"
}
cmd_validate_compose() {
local name="$1"
validate_name "${name}"
load_app "${name}"
if run_compose "${COMPOSE_ARGS[@]}" config >/dev/null 2>&1; then
log info "compose file is valid"
else
local output
output="$(run_compose "${COMPOSE_ARGS[@]}" config 2>&1 || true)"
fail "compose validation failed: ${output}"
fi
}
cmd_remove() {
local name="$1"
local keep_volumes="${2:-}"
validate_name "${name}"
load_app "${name}"
run_compose "${COMPOSE_ARGS[@]}" down --remove-orphans 2>/dev/null || true
# Remove this app's block from the aggregate routes file.
local route_file
route_file="$(app_route_file)"
if [[ -f "${route_file}" ]]; then
routes_lock
local tmp
tmp="$(mktemp)"
sed "/^# route:${name}:start$/,/^# route:${name}:end$/d" "${route_file}" >"${tmp}" || true
install -m 0664 -o reudy -g panelroutes "${tmp}" "${route_file}"
rm -f "${tmp}"
routes_unlock
fi
rm -f "$(app_manifest "${name}")" "$(app_env_file "${name}")"
rm -rf "${APP_STACK_DIR}"
if [[ "${keep_volumes}" != "--keep-volumes" ]]; then
rm -rf "${APP_VOLUME_DIR}"
fi
log info "removed app '${name}'"
}
cmd_set_routes() {
local name="$1"
local routes="$2"
local manifest
validate_name "${name}"
validate_routes "${routes}"
manifest="$(app_manifest "${name}")"
[[ -f "${manifest}" ]] || fail "app '${name}' does not exist"
# Update APP_ROUTES in the manifest file, strip old fields, preserve others
local tmp
tmp="$(mktemp)"
local found_routes=false
while IFS= read -r line; do
case "${line}" in
APP_ROUTES=*)
printf 'APP_ROUTES="%s"\n' "${routes}" >> "${tmp}"
found_routes=true
;;
APP_DOMAIN=*|APP_DOMAINS=*|APP_PORT=*|APP_UPSTREAM=*)
# Strip old format fields
;;
*)
printf '%s\n' "${line}" >> "${tmp}"
;;
esac
done < "${manifest}"
if ! "${found_routes}"; then
printf 'APP_ROUTES="%s"\n' "${routes}" >> "${tmp}"
fi
install -m 0664 "${tmp}" "${manifest}"
# Re-render Caddy routes (auto-reloads via systemd.path watcher)
cmd_render_route "${name}"
log info "updated routes for app '${name}'. Edit compose file if new ports need exposing."
}
cmd_backup() {
local name="$1"
validate_name "${name}"
load_app "${name}"
ensure_base_dirs
local volume_dir
volume_dir="$(app_volume_dir "${name}")"
[[ -d "${volume_dir}" ]] || fail "volume directory '${volume_dir}' does not exist"
local timestamp
timestamp="$(date +%Y%m%d-%H%M%S)"
local backup_file="${BACKUPS_DIR}/${name}-${timestamp}.zip"
# Stop containers before backup for consistency
local was_running=false
if run_compose "${COMPOSE_ARGS[@]}" ps --format json 2>/dev/null | grep -q '"running"' 2>/dev/null; then
was_running=true
log info "stopping containers for consistent backup..."
run_compose "${COMPOSE_ARGS[@]}" down 2>/dev/null || true
fi
(cd "${volume_dir}" && zip -r "${backup_file}" .) || fail "zip failed"
# Also include the compose file in the backup
local stack_dir
stack_dir="$(app_stack_dir "${name}")"
if [[ -f "${stack_dir}/compose.yaml" ]]; then
(cd "${stack_dir}" && zip -j "${backup_file}" compose.yaml) || true
fi
# Restart if it was running
if [[ "${was_running}" == "true" ]]; then
log info "restarting containers after backup..."
run_compose "${COMPOSE_ARGS[@]}" up -d 2>/dev/null || true
fi
local size
size="$(du -h "${backup_file}" | cut -f1)"
log info "backup created: ${backup_file} (${size})"
}
cmd_list_backups() {
local name="$1"
validate_name "${name}"
load_app "${name}"
ensure_base_dirs
local found=0
for bf in "${BACKUPS_DIR}/${name}"-*.zip; do
[[ -e "${bf}" ]] || continue
found=1
local fname size mtime
fname="$(basename "${bf}")"
size="$(du -h "${bf}" | cut -f1)"
mtime="$(stat -c '%Y' "${bf}" 2>/dev/null || stat -f '%m' "${bf}" 2>/dev/null || echo "0")"
echo "${fname} ${size} ${mtime}"
done
if [[ "${found}" -eq 0 ]]; then
log info "no backups found for '${name}'"
fi
}
cmd_volume_clear() {
local name="$1"
validate_name "${name}"
load_app "${name}"
log info "clearing volume data for app '${name}'"
run_compose "${COMPOSE_ARGS[@]}" down --remove-orphans 2>/dev/null || true
local data_dir="${APP_VOLUME_DIR}/data"
if [[ -d "${data_dir}" ]]; then
rm -rf "${data_dir:?}"/*
rm -rf "${data_dir:?}"/.[!.]* 2>/dev/null || true
fi
mkdir -p "${APP_VOLUME_DIR}/data"
log info "volume data cleared for app '${name}'"
}
cmd_restore() {
local name="$1"
local backup_file="$2"
validate_name "${name}"
load_app "${name}"
# Resolve backup file path
local full_path="${backup_file}"
if [[ ! -f "${full_path}" ]]; then
full_path="${BACKUPS_DIR}/${backup_file}"
fi
[[ -f "${full_path}" ]] || fail "backup file '${backup_file}' not found"
# Ensure it's a zip file within the backups directory
local norm_path
norm_path="$(realpath "${full_path}")"
local norm_backups
norm_backups="$(realpath "${BACKUPS_DIR}")"
[[ "${norm_path}" == "${norm_backups}"/* ]] || fail "backup file must be in the backups directory"
local volume_dir
volume_dir="$(app_volume_dir "${name}")"
# Stop containers before restore
log info "stopping containers for restore..."
run_compose "${COMPOSE_ARGS[@]}" down 2>/dev/null || true
# Clear existing volume data and extract backup
rm -rf "${volume_dir:?}"/*
mkdir -p "${volume_dir}"
(cd "${volume_dir}" && unzip -o "${norm_path}") || fail "unzip failed"
log info "restored '${name}' from $(basename "${norm_path}")"
log info "run 'panelctl deploy ${name}' to start the app'"
}
cmd_inspect_volumes() {
local name="$1"
validate_name "${name}"
load_app "${name}"
echo "default|${APP_VOLUME_DIR}/data"
local podman_bin=""
if command -v podman >/dev/null 2>&1; then
podman_bin="$(command -v podman)"
elif [[ -x /run/current-system/sw/bin/podman ]]; then
podman_bin="/run/current-system/sw/bin/podman"
fi
if [[ -n "${podman_bin}" ]]; then
"${podman_bin}" volume ls --filter label=com.docker.compose.project="${name}" --format '{{.Name}}|{{.Mountpoint}}' 2>/dev/null || true
"${podman_bin}" volume ls --filter label=io.podman.compose.project="${name}" --format '{{.Name}}|{{.Mountpoint}}' 2>/dev/null || true
# grep exits 1 when there are no named volumes; that is not an error.
fi | sort -u | grep -v '^$' || true
}
cmd_list() {
ensure_base_dirs
local found=0
for mf in "${APPS_DIR}"/*.env; do
[[ -e "${mf}" ]] || continue
found=1
# shellcheck disable=SC1090
source /dev/null # reset any leftover variables
unset APP_REPO_URL APP_REPO_BRANCH APP_REPO_DIR 2>/dev/null || true
source "${mf}"
# Backward compat: build APP_ROUTES from old format
local routes="${APP_ROUTES:-}"
if [[ -z "${routes}" && -n "${APP_DOMAIN:-}" ]]; then
local upstream="${APP_UPSTREAM:-127.0.0.1:${APP_PORT:-18080}}"
local domains_str="${APP_DOMAINS:-${APP_DOMAIN}}"
IFS=',' read -ra domain_arr <<< "${domains_str}"
for d in "${domain_arr[@]}"; do
d="$(echo "${d}" | xargs)"
if [[ -n "${routes}" ]]; then
routes="${routes},${d}|${upstream}"
else
routes="${d}|${upstream}"
fi
done
fi
# Show abbreviated: first route's domain + upstream, and count
local first_route="${routes%%,*}"
local route_count=1
if [[ "${routes}" == *","* ]]; then
route_count="$(( $(grep -o ',' <<< "${routes}" | wc -l) + 1 ))"
fi
local repo_info="${APP_REPO_URL:-}"
echo "${APP_NAME} ${first_route} routes=${route_count} auth=${APP_AUTH_PROTECTED} ${repo_info}"
done
if [[ "${found}" -eq 0 ]]; then
log info "no apps found"
fi
}
cmd_show() {
local name="$1"
validate_name "${name}"
local mf
mf="$(app_manifest "${name}")"
[[ -f "${mf}" ]] || fail "app '${name}' does not exist"
cat "${mf}"
}
main() {
local cmd="${1:-}"
case "${cmd}" in
init)
[[ $# -ge 3 ]] || fail "usage: panelctl init <name> <routes> [auth]"
cmd_init "$2" "$3" "${4:-true}"
;;
set-routes)
[[ $# -eq 3 ]] || fail "usage: panelctl set-routes <name> <routes>"
cmd_set_routes "$2" "$3"
;;
render-route)
[[ $# -eq 2 ]] || fail "usage: panelctl render-route <name>"
cmd_render_route "$2"
;;
deploy)
[[ $# -eq 2 ]] || fail "usage: panelctl deploy <name>"
cmd_deploy "$2"
;;
restart)
[[ $# -eq 2 ]] || fail "usage: panelctl restart <name>"
cmd_restart "$2"
;;
stop)
[[ $# -eq 2 ]] || fail "usage: panelctl stop <name>"
cmd_stop "$2"
;;
status)
[[ $# -eq 2 ]] || fail "usage: panelctl status <name>"
cmd_status "$2"
;;
logs)
[[ $# -ge 2 ]] || fail "usage: panelctl logs <name> [--tail N]"
cmd_logs "$2" "${@:3}"
;;
validate-compose)
[[ $# -eq 2 ]] || fail "usage: panelctl validate-compose <name>"
cmd_validate_compose "$2"
;;
remove)
[[ $# -ge 2 ]] || fail "usage: panelctl remove <name> [--keep-volumes]"
cmd_remove "$2" "${3:-}"
;;
backup)
[[ $# -eq 2 ]] || fail "usage: panelctl backup <name>"
cmd_backup "$2"
;;
list-backups)
[[ $# -eq 2 ]] || fail "usage: panelctl list-backups <name>"
cmd_list_backups "$2"
;;
restore)
[[ $# -eq 3 ]] || fail "usage: panelctl restore <name> <backup-file>"
cmd_restore "$2" "$3"
;;
volume-clear)
[[ $# -eq 2 ]] || fail "usage: panelctl volume-clear <name>"
cmd_volume_clear "$2"
;;
inspect-volumes)
[[ $# -eq 2 ]] || fail "usage: panelctl inspect-volumes <name>"
cmd_inspect_volumes "$2"
;;
list)
[[ $# -eq 1 ]] || fail "usage: panelctl list"
cmd_list
;;
show)
[[ $# -eq 2 ]] || fail "usage: panelctl show <name>"
cmd_show "$2"
;;
""|-h|--help|help)
usage
;;
*)
fail "unknown command '${cmd}'"
;;
esac
}
main "$@"

View file

@ -20,9 +20,4 @@ in
# Nextcloud admin password (plaintext, read by the nextcloud service)
"secrets/nextcloud-admin-pass.age".publicKeys = users ++ systems;
# Restic backups (backups.nix): the repository password, and an env file with
# the repository URL and Scaleway S3 keys. See BACKUPS.md.
"secrets/restic-password.age".publicKeys = users ++ systems;
"secrets/restic-env.age".publicKeys = users ++ systems;
}