coreapp new --postgres: apps with their own database

A template overlay (templates/postgres) adds DEPENDS="postgresql", DB_*
settings with a random password, an install that creates the user and
database on the Core's PostgreSQL (safe to re-run), an uninstall that
drops them, and a Python example that counts visits. Tested with a
throwaway app: deploy, redeploy and a reboot of the Core keep the data,
remove drops the database and user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 12:06:55 -07:00
co-authored by Claude Opus 5.5
parent 1108060432
commit 74bdc13f0f
12 changed files with 211 additions and 9 deletions
+31 -7
View File
@@ -3,7 +3,8 @@
# Purpose : Make, deploy and test apps for Federated Core, against the Core
# in coreapp-dev's VM (coreapp-vm).
#
# coreapp new <name> make a new app in ./<name>
# coreapp new [--postgres] <name>
# make a new app in ./<name> (with its own database)
# coreapp deploy build, copy to the Core, install or update
# coreapp check the app answers at https://<name>.<domain>/
# coreapp status its container, address and URL
@@ -17,7 +18,8 @@
set -euo pipefail
SELF_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
TEMPLATE="$(dirname "$SELF_DIR")/templates/app"
TEMPLATES="$(dirname "$SELF_DIR")/templates"
TEMPLATE="$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"
@@ -108,9 +110,17 @@ next_address() {
}
cmd_new() {
local name="${1:-}" title ip f
local name="" overlay="" title ip dbname f
while [[ $# -gt 0 ]]; do
case "$1" in
--postgres) overlay="$TEMPLATES/postgres" ;;
-*) die "Unknown option $1. Usage: coreapp new [--postgres] <name>" ;;
*) name="$1" ;;
esac
shift
done
[[ -d "$TEMPLATE" ]] || die "The app template is missing: $TEMPLATE"
[[ -n "$name" ]] || die "Usage: coreapp new <name> (lowercase letters, digits and -)"
[[ -n "$name" ]] || die "Usage: coreapp new [--postgres] <name> (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.<domain>."
[[ ! -e "$name" ]] || die "./$name already exists."
@@ -121,16 +131,30 @@ cmd_new() {
mkdir -p "$DATA"
echo "$name $ip" >> "$ADDRESSES"
fi
# "my-app" -> "My App"
# "my-app" -> "My App"; database names can't have "-"
title="$(echo "$name" | sed -E 's/-/ /g; s/(^| )([a-z])/\1\u\2/g')"
dbname="${name//-/_}"
mkdir "$name"
tar -C "$TEMPLATE" --exclude=.claude -cf - . | tar -C "$name" -xf -
if [[ -n "$overlay" ]]; then
# The overlay's files replace the base's; *.append ones are added to the
# end of the base file; .remove lists base files it doesn't use
tar -C "$overlay" --exclude=.claude --exclude=.remove --exclude='*.append' -cf - . \
| tar -C "$name" -xf -
for f in "$overlay"/*.append; do
[[ -e "$f" ]] && cat "$f" >> "$name/$(basename "$f" .append)"
done
if [[ -f "$overlay/.remove" ]]; then
while read -r f; do [[ -n "$f" ]] && rm -rf "${name:?}/$f"; done < "$overlay/.remove"
fi
fi
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"
sed -i -e "s/{{NAME}}/$name/g" -e "s/{{TITLE}}/$title/g" -e "s/{{IP}}/$ip/g" \
-e "s/{{DBNAME}}/$dbname/g" "$f"
done < <(find "$name" -type f ! -name '*.png' -print0)
echo "Made $title in ./$name (address on the Core: $ip)."
echo "Made $title in ./$name (address on the Core: $ip${overlay:+, database $dbname})."
echo "Next: cd $name && coreapp deploy"
}