Let an app name the oldest database server it works with
coreapp.toml's [postgres] or [mysql] min_version: the installer compares it with the Core's PostgreSQL or MariaDB on install and update, and refuses an older Core with both versions, instead of the app failing later. coreapp core reads the versions the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+64
-21
@@ -99,6 +99,7 @@ FIELDS = {
|
||||
"needs": None,
|
||||
"sso": None,
|
||||
"postgres": None,
|
||||
"mysql": None,
|
||||
}
|
||||
REQUIRED = ["name", "title", "version", "image", "port"]
|
||||
|
||||
@@ -109,9 +110,13 @@ SSO_DEFAULTS = {"callback": "/auth/callback", "pkce": True,
|
||||
SSO_CALLBACK = r"/[A-Za-z0-9._~/-]{0,200}"
|
||||
SSO_TOKEN_AUTH = ["client_secret_basic", "client_secret_post"]
|
||||
|
||||
# [postgres]: extensions the Core's superuser enables in the app's database
|
||||
# (ones the app's own user can't, like vector)
|
||||
POSTGRES_SETTINGS = ["extensions"]
|
||||
# [postgres] and [mysql]: the oldest server version the app works with
|
||||
# (the installer refuses an older Core), and for postgres the extensions
|
||||
# the Core's superuser makes in the app's database (ones the app's own user
|
||||
# can't, like vector)
|
||||
DB_SETTINGS = {"postgres": ["min_version", "extensions"], "mysql": ["min_version"]}
|
||||
DB_SERVER = {"postgres": "PostgreSQL", "mysql": "MariaDB"}
|
||||
MIN_VERSION = r"[0-9]{1,4}(?:\.[0-9]{1,4}){0,2}"
|
||||
EXTENSION = r"[a-z0-9][a-z0-9_-]{0,62}"
|
||||
|
||||
# Repositories apps are installed from: https only, no user or password in
|
||||
@@ -162,20 +167,31 @@ def read_sso(value):
|
||||
return sso
|
||||
|
||||
|
||||
def read_postgres(value):
|
||||
"""coreapp.toml's [postgres] table, checked."""
|
||||
def read_database(need, value):
|
||||
"""coreapp.toml's [postgres] or [mysql] table, checked."""
|
||||
known = DB_SETTINGS[need]
|
||||
if not isinstance(value, dict):
|
||||
raise Refused('coreapp.toml: postgres must be a table: [postgres] with extensions = ["vector"].')
|
||||
unknown = sorted(set(value) - set(POSTGRES_SETTINGS))
|
||||
raise Refused(f"coreapp.toml: {need} must be a table: [{need}] with {', '.join(known)}.")
|
||||
unknown = sorted(set(value) - set(known))
|
||||
if unknown:
|
||||
raise Refused(f"coreapp.toml: [postgres]: unknown setting(s) {', '.join(unknown)} "
|
||||
f"(it knows {', '.join(POSTGRES_SETTINGS)}).")
|
||||
raise Refused(f"coreapp.toml: [{need}]: unknown setting(s) {', '.join(unknown)} "
|
||||
f"(it knows {', '.join(known)}).")
|
||||
db = {"min_version": "", "extensions": []}
|
||||
if "min_version" in value:
|
||||
version = value["min_version"]
|
||||
if isinstance(version, int) and not isinstance(version, bool):
|
||||
version = str(version)
|
||||
if not isinstance(version, str) or not re.fullmatch(MIN_VERSION, version):
|
||||
raise Refused(f'coreapp.toml: [{need}] min_version must be a {DB_SERVER[need]} '
|
||||
'version, like min_version = "18" or "17.4".')
|
||||
db["min_version"] = version
|
||||
extensions = value.get("extensions", [])
|
||||
if not isinstance(extensions, list) or not all(
|
||||
isinstance(e, str) and re.fullmatch(EXTENSION, e) for e in extensions):
|
||||
raise Refused('coreapp.toml: [postgres] extensions must be a list of extension names, '
|
||||
'like extensions = ["vector"].')
|
||||
return {"extensions": sorted(set(extensions))}
|
||||
db["extensions"] = sorted(set(extensions))
|
||||
return db
|
||||
|
||||
|
||||
def read_manifest(folder):
|
||||
@@ -200,7 +216,8 @@ def read_manifest(folder):
|
||||
raise Refused(f"coreapp.toml needs {', '.join(missing)}.")
|
||||
|
||||
m = {"description": "", "health": "/", "docs": "", "needs": [], "sso": dict(SSO_DEFAULTS),
|
||||
"postgres": {"extensions": []}}
|
||||
"postgres": {"min_version": "", "extensions": []},
|
||||
"mysql": {"min_version": "", "extensions": []}}
|
||||
for key, value in data.items():
|
||||
if key == "port":
|
||||
if not isinstance(value, int) or isinstance(value, bool) or not 1 <= value <= 65535:
|
||||
@@ -218,8 +235,8 @@ def read_manifest(folder):
|
||||
"(both give the app DB_* settings).")
|
||||
elif key == "sso":
|
||||
value = read_sso(value)
|
||||
elif key == "postgres":
|
||||
value = read_postgres(value)
|
||||
elif key in DB_SETTINGS:
|
||||
value = read_database(key, value)
|
||||
else:
|
||||
if not isinstance(value, str) or not re.fullmatch(FIELDS[key], value):
|
||||
raise Refused(f"coreapp.toml: {key} = {value!r} isn't allowed. "
|
||||
@@ -227,8 +244,9 @@ def read_manifest(folder):
|
||||
m[key] = value
|
||||
if "sso" in data and "sso" not in m["needs"]:
|
||||
raise Refused('coreapp.toml: [sso] is for apps with "sso" in needs.')
|
||||
if "postgres" in data and "postgres" not in m["needs"]:
|
||||
raise Refused('coreapp.toml: [postgres] is for apps with "postgres" in needs.')
|
||||
for need in DB_SETTINGS:
|
||||
if need in data and need not in m["needs"]:
|
||||
raise Refused(f'coreapp.toml: [{need}] is for apps with "{need}" in needs.')
|
||||
if m["name"] in RESERVED:
|
||||
raise Refused(f"'{m['name']}' is one of the Core's own hostnames. Pick another name.")
|
||||
m["dbname"] = "coreapp_" + m["name"].replace("-", "_")
|
||||
@@ -623,6 +641,7 @@ def plan(m, origin=None, downgrade=False, drop_database=False):
|
||||
else:
|
||||
p["new_needs"] = list(m["needs"])
|
||||
p["dropped_needs"] = []
|
||||
check_db_version(m)
|
||||
p["new_extensions"] = extensions_to_enable(m, p)
|
||||
for key in ("source", "tag", "commit"):
|
||||
if origin:
|
||||
@@ -630,6 +649,34 @@ def plan(m, origin=None, downgrade=False, drop_database=False):
|
||||
return p
|
||||
|
||||
|
||||
def db_version(need):
|
||||
"""The Core's PostgreSQL or MariaDB version, like "17.6", or ""."""
|
||||
if need == "postgres":
|
||||
out = run(["docker", "exec", "postgresql", "postgres", "--version"]).stdout
|
||||
found = re.search(r"\) ([0-9][0-9.]*[0-9])", out)
|
||||
else:
|
||||
out = run(["docker", "exec", "pdnsmysql", "mariadb", "--version"]).stdout
|
||||
found = re.search(r"([0-9][0-9.]*[0-9])-MariaDB", out)
|
||||
return found.group(1) if found else ""
|
||||
|
||||
|
||||
def check_db_version(m):
|
||||
"""Refused if the app's database server here is older than its
|
||||
[postgres] or [mysql] min_version."""
|
||||
for need in DB_SETTINGS:
|
||||
wanted = m[need]["min_version"]
|
||||
if need not in m["needs"] or not wanted:
|
||||
continue
|
||||
have = db_version(need)
|
||||
if not have:
|
||||
raise Refused(f"Couldn't find this Core's {DB_SERVER[need]} version to check "
|
||||
f"[{need}] min_version = \"{wanted}\". Is it running?")
|
||||
key = lambda v: [int(x) for x in v.split(".")]
|
||||
if key(have) < key(wanted):
|
||||
raise Refused(f"{m['title']} needs {DB_SERVER[need]} {wanted} or newer "
|
||||
f"([{need}] min_version in coreapp.toml); this Core has {have}.")
|
||||
|
||||
|
||||
def extensions_to_enable(m, p):
|
||||
"""[postgres] extensions the app's database doesn't have yet, or Refused
|
||||
if this Core's PostgreSQL doesn't have one."""
|
||||
@@ -1433,9 +1480,7 @@ def core_info():
|
||||
"""The Core's services, versions and machine, as apps see them."""
|
||||
domain, _ = federated_settings()
|
||||
rows = [("Domain", domain)]
|
||||
pg = run(["docker", "exec", "postgresql", "postgres", "--version"]).stdout
|
||||
found = re.search(r"\) ([0-9][0-9.]*)", pg)
|
||||
rows.append(("PostgreSQL", (found.group(1) if found else "?") + ' (needs = ["postgres"])'))
|
||||
rows.append(("PostgreSQL", (db_version("postgres") or "?") + ' (needs = ["postgres"])'))
|
||||
ext = run(["docker", "exec", "postgresql", "psql", "-U", "postgres", "-Atc",
|
||||
"SELECT v.trusted, string_agg(e.name || ' ' || e.default_version, ', ' ORDER BY e.name) "
|
||||
"FROM pg_available_extensions e JOIN pg_available_extension_versions v "
|
||||
@@ -1445,9 +1490,7 @@ def core_info():
|
||||
trusted, _, names = line.partition("|")
|
||||
rows.append((" extensions the app can enable" if trusted == "t" else
|
||||
" extensions only the Core can enable", names))
|
||||
maria = run(["docker", "exec", "pdnsmysql", "mariadb", "--version"]).stdout
|
||||
found = re.search(r"([0-9][0-9.]*)-MariaDB", maria)
|
||||
rows.append(("MariaDB", (found.group(1) if found else "?") + ' (needs = ["mysql"])'))
|
||||
rows.append(("MariaDB", (db_version("mysql") or "?") + ' (needs = ["mysql"])'))
|
||||
rows.append(("Authelia", image_tag("authelia") + ' (needs = ["sso"], OpenID Connect)'))
|
||||
rows.append(("Mail server", "docker-mailserver " + image_tag("mail")
|
||||
+ ' (needs = ["email"], sending with SMTP and STARTTLS)'))
|
||||
|
||||
Reference in New Issue
Block a user