Let an installed app drop a need
Taking sso or email out of needs removes the client or mail account once the new version answers; postgres or mysql need --drop-database (coreapp deploy asks for the app's name). Their settings leave .env first, so a postgres -> mysql switch gets mysql's own values; a failed update rolls back with them intact. The route wait now compares traefik's answer with the app's own, so an app's 404 isn't taken for a missing route. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+78
-15
@@ -10,11 +10,14 @@ whoever wrote the app.
|
||||
|
||||
coreapps manifest <dir> check <dir>/coreapp.toml, print its values
|
||||
coreapps plan [--downgrade] <source> what install would do on this Core
|
||||
coreapps install [--addon-ok] [--recreate] [--downgrade] <source>
|
||||
coreapps install [--addon-ok] [--recreate] [--downgrade] [--drop-database] <source>
|
||||
install the app, or update it
|
||||
(--recreate: a new container even if
|
||||
nothing changed; --downgrade: allow an
|
||||
older release than the installed one)
|
||||
older release than the installed one;
|
||||
--drop-database: allow dropping
|
||||
"postgres" or "mysql" from needs,
|
||||
deleting its database)
|
||||
coreapps update [--addon-ok] <name> update an app installed from a
|
||||
repository to its newest release
|
||||
coreapps remove <name> uninstall an app installed by coreapps
|
||||
@@ -537,7 +540,7 @@ def marker_value(name, key):
|
||||
return ""
|
||||
|
||||
|
||||
def plan(m, origin=None, downgrade=False):
|
||||
def plan(m, origin=None, downgrade=False, drop_database=False):
|
||||
"""What installing m would do here, or Refused."""
|
||||
name = m["name"]
|
||||
domain, tier = federated_settings()
|
||||
@@ -563,10 +566,14 @@ def plan(m, origin=None, downgrade=False):
|
||||
if installed:
|
||||
before = marker_needs(name)
|
||||
dropped = [n for n in before if n not in m["needs"]]
|
||||
if dropped:
|
||||
raise Refused(f"The new coreapp.toml drops needs {', '.join(dropped)}; "
|
||||
"removing them would delete what the app keeps there. "
|
||||
"Remove the app and install it again to do that.")
|
||||
# A database is the app's data; a sign-in client or mail account
|
||||
# can be made again
|
||||
data = [n for n in dropped if n in DUMP]
|
||||
if data and not drop_database:
|
||||
raise Refused(f"The new coreapp.toml drops {', '.join(data)}: that deletes "
|
||||
f"{name}'s database, with everything in it. To do that, add "
|
||||
"--drop-database (it's dropped once the new version works).")
|
||||
p["dropped_needs"] = dropped
|
||||
p["new_needs"] = [n for n in m["needs"] if n not in before]
|
||||
# Another repository's app of the same name would take over this
|
||||
# one's data, database and sign-in client
|
||||
@@ -588,6 +595,7 @@ def plan(m, origin=None, downgrade=False):
|
||||
p["downgrade_from"] = installed_tag
|
||||
else:
|
||||
p["new_needs"] = list(m["needs"])
|
||||
p["dropped_needs"] = []
|
||||
for key in ("source", "tag", "commit"):
|
||||
if origin:
|
||||
p[key] = origin[key]
|
||||
@@ -843,12 +851,12 @@ done
|
||||
'''
|
||||
|
||||
|
||||
def install(arg, addon_ok, recreate=False, downgrade=False):
|
||||
def install(arg, addon_ok, recreate=False, downgrade=False, drop_database=False):
|
||||
with tempfile.TemporaryDirectory(prefix="coreapps-") as staging:
|
||||
folder, origin = resolve(arg, staging)
|
||||
m = read_manifest(folder)
|
||||
check_release(m, origin)
|
||||
p = plan(m, origin, downgrade)
|
||||
p = plan(m, origin, downgrade, drop_database)
|
||||
return install_app(m, p, folder, origin, addon_ok, recreate)
|
||||
|
||||
|
||||
@@ -893,6 +901,9 @@ def install_app(m, p, folder, origin, addon_ok, recreate):
|
||||
keep_for_rollback(m, p, backup, log)
|
||||
try:
|
||||
make_service_folder(m, p, folder, origin)
|
||||
# Settings of needs it drops go, so a need that gives the
|
||||
# same names (postgres, mysql: DB_*) gets its own values
|
||||
drop_env(name, p["dropped_needs"])
|
||||
env = dict(os.environ, NAME_ID=name)
|
||||
with open(log, "a") as f:
|
||||
result = subprocess.run(["bash", "-c", UPDATE_SCRIPT, "update", name,
|
||||
@@ -906,6 +917,10 @@ def install_app(m, p, folder, origin, addon_ok, recreate):
|
||||
except (Exception, KeyboardInterrupt) as e:
|
||||
say(f"{e}" if isinstance(e, Refused) else f"Updating failed: {e!r}")
|
||||
raise Refused(roll_back(m, p, backup, log)) from e
|
||||
# The new version works: undo what the dropped needs set up,
|
||||
# with their teardown from before the update
|
||||
if p["dropped_needs"]:
|
||||
drop_needs(name, p["dropped_needs"], backup, log)
|
||||
|
||||
check_needs(m)
|
||||
if p["state"] == "update":
|
||||
@@ -1077,6 +1092,46 @@ for need in "$@"; do
|
||||
done
|
||||
'''
|
||||
|
||||
# Teardown of needs an update dropped, from the service folder kept before
|
||||
# it. Arguments: the kept folder, needs.
|
||||
DROP_SCRIPT = r'''
|
||||
cd /federated
|
||||
. /etc/federated
|
||||
. lib/functions.sh
|
||||
. lib/helpers.sh
|
||||
kept=$1
|
||||
shift
|
||||
for need in "$@"; do
|
||||
if [ -e "$kept/needs/$need/teardown" ]; then . "$kept/needs/$need/teardown"; fi
|
||||
done
|
||||
'''
|
||||
|
||||
|
||||
def drop_env(name, needs):
|
||||
"""Remove the settings these needs gave from the app's .env."""
|
||||
keys = {line.split("=", 1)[0] for n in needs
|
||||
for line in read_text(NEEDS_DIR, n, "env.in").splitlines()
|
||||
if re.match(r"[A-Z_]+=", line)}
|
||||
if not keys:
|
||||
return
|
||||
path = f"{APPS}/{name}/.env"
|
||||
with open(path) as f:
|
||||
lines = [line for line in f.read().splitlines()
|
||||
if line.split("=", 1)[0] not in keys]
|
||||
write(path, "\n".join(lines) + "\n", 0o600)
|
||||
|
||||
|
||||
def drop_needs(name, needs, backup, log):
|
||||
say(f"Removing what it no longer needs from the Core: {', '.join(needs)}...")
|
||||
with open(log, "a") as f:
|
||||
result = subprocess.run(["bash", "-c", DROP_SCRIPT, "drop",
|
||||
os.path.join(backup, "service"), *needs],
|
||||
stdout=f, stderr=subprocess.STDOUT)
|
||||
if result.returncode != 0:
|
||||
say(f"Warning: removing {', '.join(needs)} didn't finish (log on the Core: {log}); "
|
||||
"the app runs without them.")
|
||||
|
||||
|
||||
# After the old service folder is back: its needs' update steps, so what
|
||||
# they set up matches it again (e.g. an Authelia client the update changed)
|
||||
RESYNC_SCRIPT = r'''
|
||||
@@ -1229,10 +1284,17 @@ def wait_for_route(m, p):
|
||||
traefik = service_value(os.path.join(SERVICES, "traefik", "service"), "INTERNAL_IP")
|
||||
if not traefik:
|
||||
return
|
||||
|
||||
def status(*args):
|
||||
return run(["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", "3",
|
||||
*args]).stdout
|
||||
# What the app itself answers there (a 404 can be the app's own, so
|
||||
# wait for traefik to give the same, not for "not 404")
|
||||
direct = status("-H", f"Host: {host}", "-H", "X-Forwarded-Proto: https",
|
||||
f"http://{p['address']}:{m['port']}{m['health']}")
|
||||
for _ in range(30):
|
||||
code = run(["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", "3",
|
||||
"--resolve", f"{host}:443:{traefik}", f"https://{host}{m['health']}"]).stdout
|
||||
if code not in ("", "000", "404", "502", "503"):
|
||||
code = status("--resolve", f"{host}:443:{traefik}", f"https://{host}{m['health']}")
|
||||
if code == direct or code not in ("", "000", "404", "502", "503"):
|
||||
return
|
||||
time.sleep(1)
|
||||
say(f"Note: traefik doesn't route https://{host}/ to it yet; it may take a moment.")
|
||||
@@ -1498,13 +1560,14 @@ def main(args):
|
||||
if os.geteuid() != 0:
|
||||
raise Refused("Run coreapps as root on the Core.")
|
||||
downgrade = "--downgrade" in rest
|
||||
rest = [a for a in rest if a != "--downgrade"]
|
||||
drop_database = "--drop-database" in rest
|
||||
rest = [a for a in rest if a not in ("--downgrade", "--drop-database")]
|
||||
if cmd == "plan" and len(rest) == 1:
|
||||
with tempfile.TemporaryDirectory(prefix="coreapps-") as staging:
|
||||
folder, origin = resolve(rest[0], staging)
|
||||
m = read_manifest(folder)
|
||||
check_release(m, origin)
|
||||
p = plan(m, origin, downgrade)
|
||||
p = plan(m, origin, downgrade, drop_database)
|
||||
for key in ("name", "title", "description", "version", "image"):
|
||||
print(f"{key}={m[key]}")
|
||||
print(f"needs={' '.join(m['needs'])}")
|
||||
@@ -1516,7 +1579,7 @@ def main(args):
|
||||
recreate = "--recreate" in rest
|
||||
folders = [a for a in rest if a not in ("--addon-ok", "--recreate")]
|
||||
if len(folders) == 1:
|
||||
return install(folders[0], addon_ok, recreate, downgrade)
|
||||
return install(folders[0], addon_ok, recreate, downgrade, drop_database)
|
||||
if cmd == "update" and rest:
|
||||
names = [a for a in rest if a != "--addon-ok"]
|
||||
if len(names) == 1:
|
||||
|
||||
Reference in New Issue
Block a user