Apps are a coreapp.toml; a trusted installer makes the Core side
An app's repository now has only coreapp.toml, its Dockerfile, its code and an icon: no install scripts or compose file, so installing an app from someone else's repository can't run their code as root on a Core. core/coreapps runs on the Core (coreapp deploy puts it in /opt/coreapps). It checks the manifest, refuses Federated's app names and the Core's hostnames, picks a free address on that Core, writes the generic app format from its own templates, sets up the needs (postgres, sso) and installs through installapp. Updates keep the address, secrets and data, add settings for new needs and delete old images; dropping a need is refused. remove rebuilds Superpanel's catalog afterwards. PLAN.md: the Coreapps direction and steps, and what the Core offers apps (Superpanel's AI features, mcpserver, hermes, email, PowerDNS). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+93
-170
@@ -12,26 +12,23 @@
|
||||
# coreapp logs [-f] its log
|
||||
# coreapp shell a shell in its container
|
||||
# coreapp remove uninstall it from the Core (asks first)
|
||||
# coreapp list the apps on the Core installed this way
|
||||
#
|
||||
# Run everything but `new` in the app's folder. COREAPP_VM picks the VM
|
||||
# (default coreserver).
|
||||
# Run everything but `new` and `list` in the app's folder (the one with
|
||||
# coreapp.toml). COREAPP_VM picks the VM (default coreserver).
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SELF_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
|
||||
TEMPLATES="$(dirname "$SELF_DIR")/templates"
|
||||
TEMPLATE="$TEMPLATES/app"
|
||||
# The installer that runs on the Core (coreapps), and where it goes there
|
||||
INSTALLER="$(dirname "$SELF_DIR")/core"
|
||||
REMOTE_INSTALLER=/opt/coreapps
|
||||
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; }
|
||||
|
||||
@@ -77,45 +74,57 @@ core_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="" PORTS=""
|
||||
# From coreapp.toml, as checked by the installer
|
||||
NAME="" TITLE="" VERSION="" IMAGE="" PORT="" HEALTH="" NEEDS=""
|
||||
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 <name>."
|
||||
# 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 <name>)."
|
||||
[[ -n "$CONTAINER" && -n "$VERSION" && -n "$INTERNAL_IP" ]] \
|
||||
|| die "service needs CONTAINER, VERSION and INTERNAL_IP."
|
||||
local values key value
|
||||
[[ -f coreapp.toml ]] \
|
||||
|| die "No app here: run this in an app's folder (one with coreapp.toml), or make one with coreapp new <name>."
|
||||
values="$(python3 "$INSTALLER/coreapps" manifest .)" || exit 1
|
||||
while IFS='=' read -r key value; do
|
||||
case "$key" in
|
||||
name) NAME="$value" ;;
|
||||
title) TITLE="$value" ;;
|
||||
version) VERSION="$value" ;;
|
||||
image) IMAGE="$value" ;;
|
||||
port) PORT="$value" ;;
|
||||
health) HEALTH="$value" ;;
|
||||
needs) NEEDS="$value" ;;
|
||||
esac
|
||||
done <<< "$values"
|
||||
init_core
|
||||
}
|
||||
|
||||
image() { echo "$CONTAINER:$VERSION"; }
|
||||
image() { echo "$IMAGE:$VERSION"; }
|
||||
|
||||
# Put the installer on the Core, as this coreapp-dev has it
|
||||
sync_installer() {
|
||||
tar -C "$INSTALLER" --exclude=__pycache__ --exclude=.claude -cf - . | core "
|
||||
set -e
|
||||
rm -rf $REMOTE_INSTALLER.new && mkdir -p $REMOTE_INSTALLER.new
|
||||
tar --no-same-owner -xf - -C $REMOTE_INSTALLER.new
|
||||
rm -rf $REMOTE_INSTALLER && mv $REMOTE_INSTALLER.new $REMOTE_INSTALLER"
|
||||
}
|
||||
|
||||
# Run the installer on the Core with this app's coreapp.toml and icon.png,
|
||||
# copied to a temporary folder there: coreapps <command> [options] <folder>
|
||||
installer() {
|
||||
local files=(coreapp.toml)
|
||||
[[ -f icon.png ]] && files+=(icon.png)
|
||||
tar -cf - "${files[@]}" | core "
|
||||
d=\$(mktemp -d) && tar --no-same-owner -xf - -C \$d || exit 1
|
||||
$REMOTE_INSTALLER/coreapps $* \$d; status=\$?
|
||||
rm -rf \$d; exit \$status"
|
||||
}
|
||||
|
||||
# --- 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)."
|
||||
}
|
||||
|
||||
# Lay an overlay over the app in $1: its files replace the app's, except
|
||||
# *.append (added to the end of the app's file), .remove (app files it
|
||||
# doesn't use) and .depends (apps it needs, for DEPENDS in service)
|
||||
# doesn't use) and .needs (what it needs from the Core, for coreapp.toml)
|
||||
apply_overlay() {
|
||||
local app="$1" overlay="$2" f
|
||||
tar -C "$overlay" --exclude=.claude --exclude=.remove --exclude=.depends \
|
||||
tar -C "$overlay" --exclude=.claude --exclude=.remove --exclude=.needs \
|
||||
--exclude='*.append' -cf - . | tar -C "$app" -xf -
|
||||
for f in "$overlay"/*.append; do
|
||||
[[ -e "$f" ]] && cat "$f" >> "$app/$(basename "$f" .append)"
|
||||
@@ -126,7 +135,7 @@ apply_overlay() {
|
||||
}
|
||||
|
||||
cmd_new() {
|
||||
local name="" postgres="" sso="" title ip dbname f depends="" what=""
|
||||
local name="" postgres="" sso="" title dbname f needs="" what=""
|
||||
local overlays=()
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
@@ -139,8 +148,8 @@ cmd_new() {
|
||||
done
|
||||
[[ -d "$TEMPLATE" ]] || die "The app template is missing: $TEMPLATE"
|
||||
[[ -n "$name" ]] || die "Usage: coreapp new [--postgres] [--sso] <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>."
|
||||
[[ "$name" =~ ^[a-z]([a-z0-9-]{0,29}[a-z0-9])$ ]] \
|
||||
|| die "'$name': use 2-31 lowercase letters, digits and -, starting with a letter and not ending with -. It becomes https://$name.<domain>."
|
||||
[[ ! -e "$name" ]] || die "./$name already exists."
|
||||
|
||||
# In this order; with both, the combined example app goes on top
|
||||
@@ -151,15 +160,7 @@ cmd_new() {
|
||||
[[ -d "$f" ]] || die "The template $f is missing."
|
||||
done
|
||||
|
||||
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". Database names: no "-", and a prefix, because
|
||||
# PostgreSQL reserves names starting with pg_ and Federated's apps have
|
||||
# their own databases (nextcloud, superpanel, ...)
|
||||
# "my-app" -> "My App". The database name is the installer's (coreapps)
|
||||
title="$(echo "$name" | sed -E 's/-/ /g; s/(^| )([a-z])/\1\u\2/g')"
|
||||
dbname="coreapp_${name//-/_}"
|
||||
|
||||
@@ -167,61 +168,47 @@ cmd_new() {
|
||||
tar -C "$TEMPLATE" --exclude=.claude -cf - . | tar -C "$name" -xf -
|
||||
for f in "${overlays[@]}"; do
|
||||
apply_overlay "$name" "$f"
|
||||
[[ -f "$f/.depends" ]] && depends="$depends $(cat "$f/.depends")"
|
||||
[[ -f "$f/.needs" ]] && needs="$needs, \"$(cat "$f/.needs")\""
|
||||
done
|
||||
depends="${depends# }"
|
||||
sed -i "s/^DEPENDS=.*/DEPENDS=\"$depends\"/" "$name/service"
|
||||
sed -i "s/^needs = .*/needs = [${needs#, }]/" "$name/coreapp.toml"
|
||||
# The example apps with a database or sign-in have a /health path
|
||||
[[ ${#overlays[@]} -gt 0 ]] && sed -i 's|^health = .*|health = "/health"|' "$name/coreapp.toml"
|
||||
|
||||
while IFS= read -r -d '' f; do
|
||||
sed -i -e "s/{{NAME}}/$name/g" -e "s/{{TITLE}}/$title/g" -e "s/{{IP}}/$ip/g" \
|
||||
-e "s/{{DBNAME}}/$dbname/g" "$f"
|
||||
sed -i -e "s/{{NAME}}/$name/g" -e "s/{{TITLE}}/$title/g" -e "s/{{DBNAME}}/$dbname/g" "$f"
|
||||
done < <(find "$name" -type f ! -name '*.png' -print0)
|
||||
|
||||
[[ -n "$postgres" ]] && what=", database $dbname"
|
||||
[[ -n "$sso" ]] && what="$what, sign-in through Authelia"
|
||||
echo "Made $title in ./$name (address on the Core: $ip$what)."
|
||||
[[ -n "$postgres" ]] && what=", with a database"
|
||||
[[ -n "$sso" ]] && what="$what, with sign-in through Authelia"
|
||||
echo "Made $title in ./$name$what."
|
||||
echo "Next: cd $name && coreapp deploy"
|
||||
}
|
||||
|
||||
# --- deploy -----------------------------------------------------------------
|
||||
|
||||
cmd_deploy() {
|
||||
local domain info state theirs tier used local_id core_id f log files=()
|
||||
local plan state tier answer local_id core_id addon=""
|
||||
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 "Updating the installer on the Core ($REMOTE_INSTALLER)..."
|
||||
sync_installer
|
||||
# What installing would do (and whether the Core refuses it: name taken...)
|
||||
plan="$(installer plan)" || exit 1
|
||||
state="$(sed -n 's/^state=//p' <<< "$plan")"
|
||||
tier="$(sed -n 's/^tier=//p' <<< "$plan")"
|
||||
if grep -qx 'addon=yes' <<< "$plan"; 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."
|
||||
read -r -p "Install anyway? [y/N] " answer
|
||||
[[ "$answer" =~ ^[Yy] ]] || die "Not deployed."
|
||||
addon=--addon-ok
|
||||
fi
|
||||
|
||||
echo "Building $(image)..."
|
||||
docker build -q -t "$(image)" . > /dev/null
|
||||
|
||||
# localhost/ images exist only where they're copied; others the Core can
|
||||
# pull, but while developing, the one just built is the one to test
|
||||
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
|
||||
@@ -231,82 +218,21 @@ cmd_deploy() {
|
||||
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
|
||||
# The same extra values the template's install provides
|
||||
TRAEFIK_IP=\"\$(sed -n 's/^INTERNAL_IP=//p' services/traefik/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
|
||||
# Running isn't answering: wait until it answers on its own address and
|
||||
# port (PORTS in service), from the Core, as traefik reaches it
|
||||
if [[ -n "$PORTS" ]]; then
|
||||
echo -n "Waiting for it to answer on $INTERNAL_IP:${PORTS%% *}..."
|
||||
if ! core "for i in \$(seq 1 30); do
|
||||
curl -s -o /dev/null --max-time 2 http://$INTERNAL_IP:${PORTS%% *}/ && exit 0
|
||||
sleep 2
|
||||
done; exit 1"; then
|
||||
echo
|
||||
core "docker logs --tail 20 $NAME 2>&1" >&2 || true
|
||||
die "$NAME runs but doesn't answer on port ${PORTS%% *} after a minute (its log is above)."
|
||||
fi
|
||||
echo " it does."
|
||||
fi
|
||||
echo "Deployed: https://$NAME.$domain/ (coreapp check tests it)"
|
||||
# Install, or update (which recreates the container if the image changed,
|
||||
# even under the same version)
|
||||
installer install $addon || exit 1
|
||||
[[ "$state" == new ]] && echo "(coreapp check tests it from the internet.)"
|
||||
return 0
|
||||
}
|
||||
|
||||
# --- check, status, logs, shell, remove ------------------------------------
|
||||
|
||||
cmd_check() {
|
||||
local domain url expect result code verify tries=24
|
||||
local domain url 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)..."
|
||||
url="https://$NAME.$domain$HEALTH"
|
||||
echo "Checking $url (expecting HTTP 2xx and a valid certificate)..."
|
||||
# A new app's certificate can take a minute: traefik gets it from Let's
|
||||
# Encrypt through the Core's DNS
|
||||
while true; do
|
||||
@@ -335,8 +261,9 @@ cmd_status() {
|
||||
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 "App: $TITLE $(image)"
|
||||
echo "Address on the Core: $(core "sed -n 's/^INTERNAL_IP=//p' /federated/services/$NAME/service 2> /dev/null"), port $PORT"
|
||||
echo "Needs: ${NEEDS:-nothing}"
|
||||
echo "URL: https://$NAME.$domain/"
|
||||
}
|
||||
|
||||
@@ -355,25 +282,20 @@ cmd_shell() {
|
||||
}
|
||||
|
||||
cmd_remove() {
|
||||
local answer log
|
||||
local answer
|
||||
load_app
|
||||
echo "This uninstalls $NAME from the Core in '$VM', with its settings and data"
|
||||
echo "there. This folder stays as it is."
|
||||
echo "there${NEEDS:+ (and what it has in: $NEEDS)}. 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."
|
||||
sync_installer
|
||||
core "$REMOTE_INSTALLER/coreapps remove $NAME"
|
||||
}
|
||||
|
||||
cmd_list() {
|
||||
init_core
|
||||
sync_installer
|
||||
core "$REMOTE_INSTALLER/coreapps list"
|
||||
}
|
||||
|
||||
cmd="${1:-help}"
|
||||
@@ -386,6 +308,7 @@ case "$cmd" in
|
||||
logs) cmd_logs "$@" ;;
|
||||
shell) cmd_shell ;;
|
||||
remove | rm) cmd_remove ;;
|
||||
list | ls) cmd_list ;;
|
||||
help | -h | --help) usage ;;
|
||||
*) usage >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user