Slice 1a of the self-update story. Every HTML/CSS/shell-script/systemd-
unit payload that used to live as a triple-quoted string constant inside
webinstaller/app.py now lives as a real file under furtka/assets/:
furtka/assets/Caddyfile
furtka/assets/VERSION (new — matches pyproject.toml)
furtka/assets/www/{index.html, settings/index.html, style.css, status.json}
furtka/assets/bin/{furtka-status, furtka-welcome}
furtka/assets/systemd/furtka-{api,reconcile,status,welcome}.service
furtka/assets/systemd/furtka-status.timer
The installer now pulls each file from disk via _read_asset(). Byte-for-
byte identical output at install time — a fresh-ISO install should land
the same files in the same places with the same contents, verified by
tests/test_webinstaller_assets.py which reconstructs each base64 blob
and asserts equality against the on-disk asset.
iso/build.sh also copies furtka/assets/ next to the webinstaller source
at /opt/furtka/assets on the live ISO so _resolve_assets_dir() finds
them with a "next to me" lookup. In dev the same function walks two
levels up to the repo copy, so pytest works without any env vars.
furtka-status.sh drops the /etc/furtka/version TODO — it now reads
/opt/furtka/VERSION directly, which Slice 1b will upgrade to
/opt/furtka/current/VERSION once the symlink layout lands.
_FURTKA_WRAPPER_SH (the 5-line /usr/local/bin/furtka shim) stays inline;
it's tiny and not asset-shaped.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
523 lines
20 KiB
Python
523 lines
20 KiB
Python
# ruff: noqa: E501 — inline HTML/CSS/JS payloads (_INDEX_HTML, _STYLE_CSS,
|
|
# _CADDYFILE, _FURTKA_STATUS_SH, etc.) round-trip verbatim to the installed
|
|
# system; wrapping them hurts readability and the rendered output is what
|
|
# matters.
|
|
import base64
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from drives import list_scored_devices
|
|
from flask import Flask, jsonify, redirect, render_template, request, url_for
|
|
|
|
app = Flask(__name__)
|
|
|
|
LANGUAGES = {
|
|
"en": {"locale": "en_US.UTF-8", "label": "English", "keyboard": "us"},
|
|
"de": {"locale": "de_DE.UTF-8", "label": "Deutsch", "keyboard": "de"},
|
|
"pl": {"locale": "pl_PL.UTF-8", "label": "Polski", "keyboard": "pl"},
|
|
}
|
|
|
|
STATE_DIR = Path(os.environ.get("FURTKA_STATE_DIR", "/tmp/furtka"))
|
|
INSTALL_LOG = STATE_DIR / "install.log"
|
|
CONFIG_PATH = STATE_DIR / "user_configuration.json"
|
|
CREDS_PATH = STATE_DIR / "user_credentials.json"
|
|
|
|
# Pre-populated with sane defaults so the form has something useful on first
|
|
# render. POSTs validate and overwrite.
|
|
settings = {
|
|
"hostname": "furtka",
|
|
"username": "",
|
|
"password": "",
|
|
"language": "en",
|
|
"boot_drive": "",
|
|
}
|
|
|
|
HOSTNAME_RE = re.compile(r"^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$")
|
|
USERNAME_RE = re.compile(r"^[a-z_][a-z0-9_-]{0,31}$")
|
|
|
|
# Ordered phase markers for the install progress bar. Each tuple is
|
|
# (substring to search for in the archinstall log, progress percent when
|
|
# reached, user-facing label). Pick the furthest phase whose marker is
|
|
# present in the log. If archinstall changes its stdout wording the bar
|
|
# stalls on the last recognized phase — the install itself keeps going.
|
|
PROGRESS_PHASES = [
|
|
("Wiping partitions", 8, "Preparing your disk"),
|
|
("Creating partitions", 12, "Creating partitions"),
|
|
("Starting installation", 15, "Starting installation"),
|
|
("Waiting for", 18, "Syncing time and packages"),
|
|
("Installing packages: ['base'", 25, "Installing the base system (this takes a while)"),
|
|
("Adding bootloader", 65, "Setting up boot"),
|
|
("Installing packages: ['efibootmgr'", 70, "Setting up boot"),
|
|
("Installing packages: ['docker'", 80, "Installing your apps"),
|
|
("Enabling service", 90, "Turning on services"),
|
|
("Updating /mnt/etc/fstab", 95, "Almost done"),
|
|
("Installation completed without any errors", 100, "Done!"),
|
|
]
|
|
|
|
PROGRESS_ERROR_MARKERS = ("Traceback (most recent call last)", "archinstall: error:")
|
|
|
|
|
|
def parse_install_progress(log):
|
|
percent = 2
|
|
phase = "Starting up…"
|
|
for marker, pct, label in PROGRESS_PHASES:
|
|
if marker in log:
|
|
percent = pct
|
|
phase = label
|
|
|
|
if percent >= 100:
|
|
status = "done"
|
|
elif any(m in log for m in PROGRESS_ERROR_MARKERS):
|
|
status = "error"
|
|
phase = "Installation failed — open Show details below"
|
|
else:
|
|
status = "running"
|
|
|
|
return {"percent": percent, "phase": phase, "status": status}
|
|
|
|
|
|
def validate_step1(form):
|
|
errors = []
|
|
values = {
|
|
"hostname": form.get("hostname", "").strip(),
|
|
"username": form.get("username", "").strip(),
|
|
"password": form.get("password", ""),
|
|
"language": form.get("language", ""),
|
|
}
|
|
password2 = form.get("password2", "")
|
|
|
|
if not HOSTNAME_RE.match(values["hostname"]):
|
|
errors.append("Hostname must be lowercase letters, digits, hyphens (max 63 chars).")
|
|
if not USERNAME_RE.match(values["username"]):
|
|
errors.append("Username must start with a letter or underscore, lowercase only.")
|
|
if len(values["password"]) < 8:
|
|
errors.append("Password must be at least 8 characters.")
|
|
if values["password"] != password2:
|
|
errors.append("Passwords do not match.")
|
|
if values["language"] not in LANGUAGES:
|
|
errors.append("Pick a language.")
|
|
return errors, values
|
|
|
|
|
|
def build_disk_config(boot_drive):
|
|
# archinstall 4.x dropped the `use_entire_disk` shortcut — `default_layout`
|
|
# now requires fully-specified partitions. We call suggest_single_disk_layout
|
|
# with ext4 + no separate /home, which short-circuits its interactive prompts.
|
|
import asyncio
|
|
|
|
from archinstall.lib.disk.device_handler import device_handler
|
|
from archinstall.lib.disk.disk_menu import suggest_single_disk_layout
|
|
from archinstall.lib.models.device import (
|
|
DiskLayoutConfiguration,
|
|
DiskLayoutType,
|
|
FilesystemType,
|
|
)
|
|
|
|
device_handler.load_devices()
|
|
device = device_handler.get_device(Path(boot_drive))
|
|
if device is None:
|
|
raise RuntimeError(f"archinstall could not resolve device {boot_drive!r}")
|
|
|
|
device_mod = asyncio.run(
|
|
suggest_single_disk_layout(
|
|
device,
|
|
filesystem_type=FilesystemType.Ext4,
|
|
separate_home=False,
|
|
)
|
|
)
|
|
layout = DiskLayoutConfiguration(
|
|
config_type=DiskLayoutType.Default,
|
|
device_modifications=[device_mod],
|
|
)
|
|
return layout.json()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Post-install bootstrap payload
|
|
#
|
|
# Written into the target system via archinstall's `custom_commands` so that
|
|
# after reboot the user lands in "Furtka": Caddy serves a branded landing
|
|
# page + live status tiles on :80, avahi advertises proksi.local, and the
|
|
# console shows a welcome banner pointing at the URL.
|
|
#
|
|
# Asset files (HTML, CSS, shell scripts, systemd units, Caddyfile) live in
|
|
# furtka/assets/ in the repo — at ISO build time they end up on the live ISO
|
|
# as part of the webinstaller's source tree AND inside the resource-manager
|
|
# payload tarball. The installer reads them from the live-ISO copy, base64-
|
|
# encodes them, and hands them to archinstall so the chroot recreates each
|
|
# file bit-for-bit. Updates (Phase 2) refresh the tarball, which carries the
|
|
# same assets to the target's /opt/furtka/ tree.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# Tarball built by iso/build.sh containing the furtka/ Python package + the
|
|
# bundled apps/ tree (plus furtka/assets/). The webinstaller reads it from the
|
|
# live ISO at request-time and base64-encodes it into a custom_command for
|
|
# archinstall.
|
|
RESOURCE_MANAGER_PAYLOAD = Path("/opt/furtka-resource-manager.tar.gz")
|
|
|
|
|
|
# Asset root. Two layouts we have to handle:
|
|
# dev / tests — webinstaller/app.py sits at repo_root/webinstaller/ and
|
|
# assets live at repo_root/furtka/assets/.
|
|
# live ISO — iso/build.sh copies webinstaller/ to /opt/furtka/ AND
|
|
# copies furtka/assets/ to /opt/furtka/assets/ right next to
|
|
# app.py, so the same "assets next to me" lookup works.
|
|
# Probe the sibling path first (ISO case), fall back to the repo layout.
|
|
def _resolve_assets_dir() -> Path:
|
|
here = Path(__file__).resolve().parent
|
|
sibling = here / "assets"
|
|
if sibling.is_dir():
|
|
return sibling
|
|
repo_copy = here.parent / "furtka" / "assets"
|
|
if repo_copy.is_dir():
|
|
return repo_copy
|
|
raise FileNotFoundError(
|
|
f"furtka assets not found near {here} — looked in {sibling} and {repo_copy}"
|
|
)
|
|
|
|
|
|
_ASSETS_DIR = _resolve_assets_dir()
|
|
|
|
|
|
def _read_asset(relpath: str) -> str:
|
|
"""Return the UTF-8 contents of an on-disk asset shipped under furtka/assets/.
|
|
|
|
Raises FileNotFoundError if the asset is missing, which is loud by design:
|
|
an install that tries to write an asset that isn't there is broken before
|
|
the user ever boots the target, not after.
|
|
"""
|
|
path = _ASSETS_DIR / relpath
|
|
return path.read_text(encoding="utf-8")
|
|
|
|
|
|
_FURTKA_WRAPPER_SH = """\
|
|
#!/bin/sh
|
|
# Tiny launcher for the furtka resource-manager CLI. The Python source lives
|
|
# under /opt/furtka/furtka/ — added to PYTHONPATH so plain `python3 -m` finds
|
|
# it without needing pip on the target system.
|
|
PYTHONPATH=/opt/furtka exec python3 -m furtka.cli "$@"
|
|
"""
|
|
|
|
|
|
def _write_file_cmd(path, content, mode=None):
|
|
"""Shell command that recreates `path` with `content` inside the chroot.
|
|
|
|
Uses base64 so we don't have to worry about bash / JSON / archinstall
|
|
quoting the payload through three layers of shell. `base64` is part of
|
|
coreutils and always available in the target system.
|
|
"""
|
|
b64 = base64.b64encode(content.encode()).decode()
|
|
parent = path.rsplit("/", 1)[0]
|
|
cmd = f"mkdir -p {parent} && printf %s {b64} | base64 -d > {path}"
|
|
if mode is not None:
|
|
cmd += f" && chmod {mode} {path}"
|
|
return cmd
|
|
|
|
|
|
def _resource_manager_commands():
|
|
"""Commands to land /opt/furtka/ + the `furtka` CLI + reconcile.service.
|
|
|
|
Reads the payload tarball staged into the live ISO at build time. If the
|
|
file isn't present (dev box without an ISO build), returns [] so the rest
|
|
of the install still works — the resource manager just won't be installed.
|
|
"""
|
|
if not RESOURCE_MANAGER_PAYLOAD.exists():
|
|
print(
|
|
f"warning: {RESOURCE_MANAGER_PAYLOAD} missing, "
|
|
"resource manager will NOT be installed on target",
|
|
file=sys.stderr,
|
|
)
|
|
return []
|
|
payload_b64 = base64.b64encode(RESOURCE_MANAGER_PAYLOAD.read_bytes()).decode()
|
|
untar_cmd = (
|
|
f"mkdir -p /opt/furtka && printf %s {payload_b64} | base64 -d | tar -xzf - -C /opt/furtka"
|
|
)
|
|
return [
|
|
untar_cmd,
|
|
_write_file_cmd("/usr/local/bin/furtka", _FURTKA_WRAPPER_SH, mode="755"),
|
|
_write_file_cmd(
|
|
"/etc/systemd/system/furtka-reconcile.service",
|
|
_read_asset("systemd/furtka-reconcile.service"),
|
|
),
|
|
_write_file_cmd(
|
|
"/etc/systemd/system/furtka-api.service",
|
|
_read_asset("systemd/furtka-api.service"),
|
|
),
|
|
]
|
|
|
|
|
|
def _post_install_commands(hostname):
|
|
# nss-mdns: splice `mdns_minimal [NOTFOUND=return]` before `resolve` on
|
|
# the hosts line so `*.local` works from the installed system too. Guarded
|
|
# so a re-run (or a future Arch default that already ships mdns) is a
|
|
# no-op instead of double-injecting.
|
|
nss_sed = (
|
|
"grep -q 'mdns_minimal' /etc/nsswitch.conf || "
|
|
"sed -i '/^hosts:/ s/resolve/mdns_minimal [NOTFOUND=return] resolve/' "
|
|
"/etc/nsswitch.conf"
|
|
)
|
|
# Pin the chosen hostname into the static HTML at install-time so the
|
|
# landing page doesn't need a server-side template.
|
|
hostname_sed = f"sed -i 's/__HOSTNAME__/{hostname}/g' /srv/furtka/www/index.html"
|
|
return [
|
|
_write_file_cmd("/etc/caddy/Caddyfile", _read_asset("Caddyfile")),
|
|
_write_file_cmd("/srv/furtka/www/index.html", _read_asset("www/index.html")),
|
|
_write_file_cmd(
|
|
"/srv/furtka/www/settings/index.html",
|
|
_read_asset("www/settings/index.html"),
|
|
),
|
|
_write_file_cmd("/srv/furtka/www/style.css", _read_asset("www/style.css")),
|
|
_write_file_cmd("/srv/furtka/www/status.json", _read_asset("www/status.json")),
|
|
_write_file_cmd(
|
|
"/usr/local/bin/furtka-status",
|
|
_read_asset("bin/furtka-status"),
|
|
mode="755",
|
|
),
|
|
_write_file_cmd(
|
|
"/usr/local/bin/furtka-welcome",
|
|
_read_asset("bin/furtka-welcome"),
|
|
mode="755",
|
|
),
|
|
_write_file_cmd(
|
|
"/etc/systemd/system/furtka-status.service",
|
|
_read_asset("systemd/furtka-status.service"),
|
|
),
|
|
_write_file_cmd(
|
|
"/etc/systemd/system/furtka-status.timer",
|
|
_read_asset("systemd/furtka-status.timer"),
|
|
),
|
|
_write_file_cmd(
|
|
"/etc/systemd/system/furtka-welcome.service",
|
|
_read_asset("systemd/furtka-welcome.service"),
|
|
),
|
|
nss_sed,
|
|
hostname_sed,
|
|
*_resource_manager_commands(),
|
|
# archinstall calls `systemctl enable` on `services` *before*
|
|
# custom_commands runs, so our own unit files aren't on disk yet at
|
|
# that point. Enable them here, after they exist. caddy /
|
|
# avahi-daemon stay in the `services` list — those are packaged
|
|
# units, present right after pacstrap. furtka-reconcile +
|
|
# furtka-api are enabled only if the resource manager payload was
|
|
# actually installed above; the conditional keeps systemctl green
|
|
# on dev / payload-less builds.
|
|
"systemctl enable furtka-welcome.service furtka-status.timer "
|
|
"$([ -e /etc/systemd/system/furtka-reconcile.service ] "
|
|
"&& echo furtka-reconcile.service furtka-api.service)",
|
|
]
|
|
|
|
|
|
def _detect_bootloader():
|
|
# systemd-boot is UEFI-only; on BIOS/legacy it trips HardwareIncompatibilityError
|
|
# inside archinstall. /sys/firmware/efi exists iff we were booted via UEFI.
|
|
return "Systemd-boot" if Path("/sys/firmware/efi").exists() else "Grub"
|
|
|
|
|
|
def build_archinstall_config(s):
|
|
return {
|
|
"archinstall-language": "English",
|
|
"timezone": "Europe/Berlin",
|
|
"ntp": True,
|
|
"bootloader": _detect_bootloader(),
|
|
"disk_config": build_disk_config(s["boot_drive"]),
|
|
"hostname": s["hostname"],
|
|
"kernels": ["linux"],
|
|
"packages": [
|
|
"docker",
|
|
"docker-compose",
|
|
# Editors for console/SSH recovery — `nano` is the beginner-friendly
|
|
# one, `vim` stays because it's muscle-memory for the dev team.
|
|
"nano",
|
|
"vim",
|
|
"git",
|
|
"htop",
|
|
"curl",
|
|
# Remote access — archinstall 4.x's `ssh: True` flag is flaky about
|
|
# actually pulling in openssh, so list it explicitly and enable sshd
|
|
# via `services` below. Without this, the documented recovery path
|
|
# (SSH in → edit .env) doesn't work.
|
|
"openssh",
|
|
# Base OS post-install (landing page + mDNS on installed system).
|
|
"caddy",
|
|
"avahi",
|
|
"nss-mdns",
|
|
# Resource manager runtime — pure-stdlib Python, no pip needed
|
|
# because we expose the package via PYTHONPATH in /usr/local/bin/furtka.
|
|
"python",
|
|
],
|
|
"profile": {"type": "server"},
|
|
"services": [
|
|
"docker",
|
|
# Base OS post-install services. Only packaged units go here —
|
|
# archinstall runs `systemctl enable` on this list *before*
|
|
# custom_commands, so our own furtka-welcome + furtka-status.timer
|
|
# units (written in custom_commands) are enabled there instead.
|
|
"caddy",
|
|
"avahi-daemon",
|
|
"sshd",
|
|
],
|
|
# `gpasswd -a <user> docker` has to stay first — adds the user to
|
|
# the docker group once the group exists (archinstall creates users
|
|
# before pacstrapping extras). After that we drop the Furtka landing
|
|
# page, status timer, and welcome banner into place.
|
|
"custom_commands": [
|
|
f"gpasswd -a {s['username']} docker",
|
|
*_post_install_commands(s["hostname"]),
|
|
],
|
|
"network_config": {"type": "iso"},
|
|
"ssh": True,
|
|
"audio_config": None,
|
|
"locale_config": {
|
|
"locale": LANGUAGES[s["language"]]["locale"],
|
|
# Keyboard layout follows the chosen language so a German user
|
|
# doesn't get a US layout at the TTY console (where things like
|
|
# `/`, `-`, `=` land on surprising keys and make even `sudo vim`
|
|
# painful). `en` falls through to "us" which is what we want.
|
|
"keyboard_layout": LANGUAGES[s["language"]]["keyboard"],
|
|
},
|
|
}
|
|
|
|
|
|
def build_archinstall_creds(s):
|
|
# archinstall 4.x expects `!root-password` and `!password` (plaintext
|
|
# sentinels). Users with neither `!password` nor `enc_password` are
|
|
# silently dropped by User.parse_arguments — hence login failures.
|
|
return {
|
|
"!root-password": s["password"],
|
|
"users": [
|
|
{
|
|
"username": s["username"],
|
|
"!password": s["password"],
|
|
"sudo": True,
|
|
"groups": [],
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
def write_install_files(s, state_dir):
|
|
state_dir.mkdir(parents=True, exist_ok=True)
|
|
config_path = state_dir / "user_configuration.json"
|
|
creds_path = state_dir / "user_credentials.json"
|
|
config_path.write_text(json.dumps(build_archinstall_config(s), indent=2))
|
|
creds_path.write_text(json.dumps(build_archinstall_creds(s), indent=2))
|
|
creds_path.chmod(0o600)
|
|
return config_path, creds_path
|
|
|
|
|
|
def spawn_archinstall(config_path, creds_path, log_path):
|
|
log_fh = open(log_path, "wb")
|
|
return subprocess.Popen(
|
|
[
|
|
"archinstall",
|
|
"--config",
|
|
str(config_path),
|
|
"--creds",
|
|
str(creds_path),
|
|
"--silent",
|
|
],
|
|
stdout=log_fh,
|
|
stderr=subprocess.STDOUT,
|
|
start_new_session=True,
|
|
)
|
|
|
|
|
|
@app.route("/")
|
|
def home():
|
|
return redirect(url_for("install_step_1"))
|
|
|
|
|
|
@app.route("/install/step1", methods=["GET", "POST"])
|
|
def install_step_1():
|
|
errors = []
|
|
if request.method == "POST":
|
|
errors, values = validate_step1(request.form)
|
|
if not errors:
|
|
settings.update(values)
|
|
return redirect(url_for("install_step_2"))
|
|
form_values = values
|
|
else:
|
|
form_values = {k: settings[k] for k in ("hostname", "username", "language")}
|
|
return render_template(
|
|
"install/step1.html",
|
|
values=form_values,
|
|
languages=LANGUAGES,
|
|
errors=errors,
|
|
)
|
|
|
|
|
|
@app.route("/install/step2", methods=["GET", "POST"])
|
|
def install_step_2():
|
|
if request.method == "POST":
|
|
boot_drive = request.form.get("boot_drive", "").strip()
|
|
if boot_drive:
|
|
settings["boot_drive"] = boot_drive
|
|
return redirect(url_for("install_overview"))
|
|
return render_template(
|
|
"install/step2.html",
|
|
drives=list_scored_devices(),
|
|
selected=settings.get("boot_drive", ""),
|
|
)
|
|
|
|
|
|
@app.route("/install/overview")
|
|
def install_overview():
|
|
masked = {**settings, "password": "•" * 8 if settings["password"] else ""}
|
|
return render_template("install/overview.html", settings=masked)
|
|
|
|
|
|
@app.route("/install/run", methods=["POST"])
|
|
def install_run():
|
|
if not settings["boot_drive"] or not settings["username"] or not settings["password"]:
|
|
return redirect(url_for("install_step_1"))
|
|
config_path, creds_path = write_install_files(settings, STATE_DIR)
|
|
INSTALL_LOG.write_bytes(b"")
|
|
if os.environ.get("FURTKA_DRY_RUN") == "1":
|
|
INSTALL_LOG.write_text(
|
|
f"DRY RUN: would exec archinstall --config {config_path} "
|
|
f"--creds {creds_path} --silent\n"
|
|
)
|
|
else:
|
|
spawn_archinstall(config_path, creds_path, INSTALL_LOG)
|
|
return redirect(url_for("install_log_view"))
|
|
|
|
|
|
@app.route("/install/log")
|
|
def install_log_view():
|
|
log = INSTALL_LOG.read_text() if INSTALL_LOG.exists() else ""
|
|
return render_template(
|
|
"install/log.html",
|
|
log=log,
|
|
progress=parse_install_progress(log),
|
|
)
|
|
|
|
|
|
@app.route("/install/log.json")
|
|
def install_log_json():
|
|
log = INSTALL_LOG.read_text() if INSTALL_LOG.exists() else ""
|
|
return jsonify(log=log, progress=parse_install_progress(log))
|
|
|
|
|
|
@app.route("/install/reboot", methods=["POST"])
|
|
def install_reboot():
|
|
# Only allow rebooting once the install has actually finished — we don't
|
|
# want a panicked click during install to reboot mid-pacstrap.
|
|
log = INSTALL_LOG.read_text() if INSTALL_LOG.exists() else ""
|
|
if parse_install_progress(log)["status"] != "done":
|
|
return redirect(url_for("install_log_view"))
|
|
# Delay reboot a few seconds so the browser can finish fetching CSS / assets
|
|
# for the rebooting page before the Flask server (and network) go away.
|
|
# Without this, the reboot page renders unstyled (giant inline SVG icon).
|
|
subprocess.Popen(
|
|
["/bin/sh", "-c", "sleep 3 && /usr/bin/systemctl reboot"],
|
|
start_new_session=True,
|
|
)
|
|
return render_template("install/rebooting.html", hostname=settings["hostname"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, port=5000)
|