crosscode: --json reports, --config, env templates, frpc tunnels, restart/export/uninstall/purge
- Every command takes --json (crosscode.script-report/1 on stdout, the log on stderr) and --config (the .env values by layer, from the agent). - .env files come from the release's env.template.json (lib/envtpl.js): written whole on install; on deploy only new variables are appended, and a new required one with no value stops the deploy at the checks. - install --frp: Hub-given ports, a marked frpc.ini block (backup, frpc verify, reload; [common] admin lines added once), no nginx. A database on another server via DB_ADMIN_URL. Refuses an existing database or role. Everything the run made is removed if it fails. - restart, export (crosscode.church-export/1 ZIP), uninstall (final backup, uploads and .env kept 30 days) and purge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
# shellcheck shell=bash
|
||||
# frpc tunnels for church installs (docs/AGENT-1.0-PLAN.md §6). The script only ever appends
|
||||
# marked blocks to frpc.ini and removes its own blocks again; nothing outside a block is changed,
|
||||
# except two admin lines added once to [common] so `frpc reload` can pick up new tunnels without
|
||||
# dropping the others. Every change: a backup, a temporary file, `frpc verify`, then the move.
|
||||
|
||||
FRPC_INI="${FRPC_INI:-/etc/frp/frpc.ini}"
|
||||
FRPC_BACKUPS="${FRPC_BACKUPS:-/etc/frp/backups}"
|
||||
FRPC_BIN="${FRPC_BIN:-frpc}"
|
||||
|
||||
frpc_begin() { printf '# >>> crosscode:%s:%s' "$1" "$2"; }
|
||||
frpc_end() { printf '# <<< crosscode:%s:%s' "$1" "$2"; }
|
||||
|
||||
# frpc_block <church> <product> <web port> <api port>: the block (no leading blank line).
|
||||
frpc_block() {
|
||||
local church="$1" product="$2" web="$3" api="$4" name
|
||||
name="$church-$product"
|
||||
cat <<EOF
|
||||
$(frpc_begin "$church" "$product") (added by crosscode $(date +%F), do not edit by hand)
|
||||
[$name-web]
|
||||
type = tcp
|
||||
local_ip = 127.0.0.1
|
||||
local_port = $web
|
||||
remote_port = $web
|
||||
|
||||
[$name-api]
|
||||
type = tcp
|
||||
local_ip = 127.0.0.1
|
||||
local_port = $api
|
||||
remote_port = $api
|
||||
$(frpc_end "$church" "$product")
|
||||
EOF
|
||||
}
|
||||
|
||||
frpc_check_format() {
|
||||
[ -f "$FRPC_INI" ] || die "No $FRPC_INI: is frpc installed and set up on this server?"
|
||||
if [ -f "$(dirname "$FRPC_INI")/frpc.toml" ] && ! grep -q '^\[common\]' "$FRPC_INI"; then
|
||||
die "This server's frpc uses frpc.toml; crosscode only edits frpc.ini so far."
|
||||
fi
|
||||
}
|
||||
|
||||
frpc_has_block() { grep -qF "$(frpc_begin "$1" "$2")" "$FRPC_INI" 2>/dev/null; }
|
||||
|
||||
# frpc_ports <church> <product>: the web and api ports in its block ("web api"), "" if none.
|
||||
frpc_ports() {
|
||||
awk -v b="$(frpc_begin "$1" "$2")" -v e="$(frpc_end "$1" "$2")" '
|
||||
index($0, b) == 1 { inb = 1; next }
|
||||
index($0, e) == 1 { inb = 0 }
|
||||
inb && /^\[.*-web\]/ { w = 1; a = 0 }
|
||||
inb && /^\[.*-api\]/ { a = 1; w = 0 }
|
||||
inb && /^remote_port/ { split($0, p, "="); gsub(/ /, "", p[2]); if (w) web = p[2]; if (a) api = p[2] }
|
||||
END { if (web != "" || api != "") print web " " api }
|
||||
' "$FRPC_INI" 2>/dev/null
|
||||
}
|
||||
|
||||
# with_admin_lines <frpc.ini>: the file, with admin_addr / admin_port added to [common] if it has
|
||||
# no admin_addr there (an existing one is left as it is).
|
||||
with_admin_lines() {
|
||||
if awk '/^\[common\]/ { c = 1; next } /^\[/ { c = 0 } c && /^[[:space:]]*admin_addr[[:space:]]*=/ { f = 1 } END { exit !f }' "$1"; then
|
||||
cat "$1"
|
||||
else
|
||||
awk '{ print } /^\[common\][[:space:]]*$/ && !done { print "admin_addr = 127.0.0.1"; print "admin_port = 7400"; done = 1 }' "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
# frpc_replace <new file>: checks and moves a new frpc.ini into place (backup first).
|
||||
frpc_replace() {
|
||||
local new="$1" stamp
|
||||
if ! "$FRPC_BIN" verify -c "$new" >/dev/null 2>&1; then
|
||||
rm -f "$new"
|
||||
die "frpc verify refused the new frpc.ini, so it wasn't changed."
|
||||
fi
|
||||
stamp="$(date +%Y%m%d%H%M%S)"
|
||||
mkdir -p "$FRPC_BACKUPS"
|
||||
cp -p "$FRPC_INI" "$FRPC_BACKUPS/frpc.ini.$stamp"
|
||||
chmod --reference="$FRPC_INI" "$new" 2>/dev/null || chmod 600 "$new"
|
||||
mv -f "$new" "$FRPC_INI"
|
||||
}
|
||||
|
||||
# frpc_add <church> <product> <web> <api>: appends the block (no change if it's there already).
|
||||
frpc_add() {
|
||||
local church="$1" product="$2" web="$3" api="$4" new name
|
||||
frpc_check_format
|
||||
if frpc_has_block "$church" "$product"; then
|
||||
info "frpc: $church:$product is already in $FRPC_INI."
|
||||
return 0
|
||||
fi
|
||||
name="$church-$product"
|
||||
if grep -qE "^\[($name-web|$name-api)\]" "$FRPC_INI"; then
|
||||
die "frpc: a tunnel named $name-web or $name-api is already in $FRPC_INI (outside crosscode's blocks)."
|
||||
fi
|
||||
if grep -qE "^[[:space:]]*(local_port|remote_port)[[:space:]]*=[[:space:]]*($web|$api)[[:space:]]*$" "$FRPC_INI"; then
|
||||
die "frpc: port $web or $api is already used in $FRPC_INI."
|
||||
fi
|
||||
new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")"
|
||||
with_admin_lines "$FRPC_INI" > "$new"
|
||||
# Keep the file's last line intact: only add a newline if it had none.
|
||||
[ -z "$(tail -c 1 "$new")" ] || echo >> "$new"
|
||||
{ echo; frpc_block "$church" "$product" "$web" "$api"; } >> "$new"
|
||||
frpc_replace "$new"
|
||||
info "frpc: added $name-web ($web) and $name-api ($api)."
|
||||
}
|
||||
|
||||
# frpc_remove <church> <product>: removes its block (and the blank line added before it).
|
||||
frpc_remove() {
|
||||
local church="$1" product="$2" new
|
||||
frpc_check_format
|
||||
frpc_has_block "$church" "$product" || { info "frpc: no block for $church:$product."; return 0; }
|
||||
new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")"
|
||||
# Drops the block's lines and the one blank line straight before its begin marker.
|
||||
awk -v b="$(frpc_begin "$church" "$product")" -v e="$(frpc_end "$church" "$product")" '
|
||||
{ lines[NR] = $0 }
|
||||
END {
|
||||
start = 0; stop = 0
|
||||
for (i = 1; i <= NR; i++) if (index(lines[i], b) == 1) { start = i; break }
|
||||
for (i = start; i <= NR; i++) if (index(lines[i], e) == 1) { stop = i; break }
|
||||
if (start > 1 && lines[start - 1] == "") start--
|
||||
for (i = 1; i <= NR; i++) if (i < start || i > stop) print lines[i]
|
||||
}
|
||||
' "$FRPC_INI" > "$new"
|
||||
frpc_replace "$new"
|
||||
info "frpc: removed $church-$product."
|
||||
}
|
||||
|
||||
frpc_reload() {
|
||||
"$FRPC_BIN" reload -c "$FRPC_INI" || warn "frpc reload failed: restart frpc (systemctl restart frpc) to pick up the change."
|
||||
}
|
||||
Reference in New Issue
Block a user