installer: harden dashboard handoff
This commit is contained in:
4 files changed
+235
-93
No files matched your search
@@ -67,4 +67,8 @@ def run(ctx: InstallContext, phases: list[tuple[str, PhaseFn]]) -> None:
|
||||
|
||||
|
||||
def _write_state(path: Path, state: dict) -> None:
|
||||
path.write_text(json.dumps(state, indent=2, default=str))
|
||||
# Dashboard polls this file while phases update it. Write atomically so the
|
||||
# reader never observes a truncated/partial JSON document and resets UI.
|
||||
tmp = path.with_name(f".{path.name}.tmp")
|
||||
tmp.write_text(json.dumps(state, indent=2, default=str))
|
||||
tmp.replace(path)
|
||||
@@ -820,7 +820,7 @@ def run_chroot_finalizer(ctx: InstallContext) -> None:
|
||||
subprocess.run(["mount", "--bind", src, str(target_dst)], check=True)
|
||||
ctx.state["bind_mounts"].append(str(target_dst))
|
||||
|
||||
# 3: sudoers shim. Cleaned up by omarchy's first-run flow.
|
||||
# 3: sudoers shim. Removed after finalize.sh returns.
|
||||
sudoers = ctx.target / "etc" / "sudoers.d" / "99-omarchy-installer"
|
||||
sudoers.parent.mkdir(parents=True, exist_ok=True)
|
||||
sudoers.write_text(
|
||||
@@ -871,6 +871,7 @@ def run_chroot_finalizer(ctx: InstallContext) -> None:
|
||||
mirror_channel = _read_omarchy_mirror()
|
||||
env_extras = [
|
||||
"OMARCHY_INSTALL_MODE=offline",
|
||||
"OMARCHY_CHROOT_FINALIZER=1",
|
||||
"OMARCHY_PATH=/usr/share/omarchy",
|
||||
f"OMARCHY_INSTALL={tooling_path / 'install'}",
|
||||
f"OMARCHY_START_TIME={omarchy_start_time}",
|
||||
@@ -890,7 +891,10 @@ def run_chroot_finalizer(ctx: InstallContext) -> None:
|
||||
*env_extras,
|
||||
"/bin/bash", str(tooling_path / "finalize.sh"),
|
||||
]
|
||||
subprocess.run(cmd, check=True)
|
||||
try:
|
||||
subprocess.run(cmd, check=True)
|
||||
finally:
|
||||
sudoers.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def _read_omarchy_mirror() -> str:
|
||||
@@ -1047,16 +1051,77 @@ def cleanup_protected_state(ctx: InstallContext) -> None:
|
||||
# finish: prompt for reboot. Bind mounts are unwound in main()'s finally.
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
def _stop_install_dashboard() -> None:
|
||||
pid = os.environ.get("OMARCHY_INSTALL_DASHBOARD_PID")
|
||||
if not pid:
|
||||
return
|
||||
def _dashboard_stopped(pid: int) -> bool:
|
||||
stat = Path(f"/proc/{pid}/stat")
|
||||
if not stat.exists():
|
||||
return True
|
||||
try:
|
||||
os.kill(int(pid), 15)
|
||||
except (OSError, ValueError):
|
||||
# /proc/<pid>/stat field 3 is process state. A zombie cannot write to
|
||||
# the tty anymore; the shell parent will reap it when control returns.
|
||||
return stat.read_text().split()[2] == "Z"
|
||||
except OSError:
|
||||
return True
|
||||
|
||||
|
||||
def _pid_is_dashboard(pid: int) -> bool:
|
||||
try:
|
||||
cmdline = Path(f"/proc/{pid}/cmdline").read_bytes().replace(b"\0", b" ")
|
||||
except OSError:
|
||||
return False
|
||||
return b"omarchy-install-dashboard" in cmdline
|
||||
|
||||
|
||||
def _dashboard_pids() -> list[int]:
|
||||
pids: list[int] = []
|
||||
pid_text = os.environ.get("OMARCHY_INSTALL_DASHBOARD_PID")
|
||||
if pid_text:
|
||||
try:
|
||||
pid = int(pid_text)
|
||||
if _pid_is_dashboard(pid):
|
||||
pids.append(pid)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# Belt-and-suspenders fallback in case the env var was lost before finish.
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
["pgrep", "-f", "omarchy-install-dashboard"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
for line in proc.stdout.splitlines():
|
||||
try:
|
||||
pid = int(line)
|
||||
except ValueError:
|
||||
continue
|
||||
if pid not in pids and _pid_is_dashboard(pid):
|
||||
pids.append(pid)
|
||||
except OSError:
|
||||
pass
|
||||
# Give the shell dashboard a moment to restore the cursor before gum draws.
|
||||
time.sleep(0.1)
|
||||
return pids
|
||||
|
||||
|
||||
def _stop_install_dashboard() -> None:
|
||||
pids = _dashboard_pids()
|
||||
for signal_number in (15, 9):
|
||||
for pid in pids:
|
||||
if _dashboard_stopped(pid):
|
||||
continue
|
||||
if not _pid_is_dashboard(pid):
|
||||
continue
|
||||
try:
|
||||
os.kill(pid, signal_number)
|
||||
except OSError:
|
||||
pass
|
||||
deadline = time.time() + (1.0 if signal_number == 15 else 0.5)
|
||||
while time.time() < deadline:
|
||||
if all(_dashboard_stopped(pid) for pid in pids):
|
||||
break
|
||||
time.sleep(0.05)
|
||||
if all(_dashboard_stopped(pid) for pid in pids):
|
||||
break
|
||||
|
||||
try:
|
||||
with open("/dev/tty", "w", encoding="utf-8") as tty:
|
||||
tty.write("\033[?25h\033[H\033[2J")
|
||||
@@ -1065,8 +1130,76 @@ def _stop_install_dashboard() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def _tty_size() -> tuple[int, int]:
|
||||
try:
|
||||
with open("/dev/tty", "rb") as tty:
|
||||
res = subprocess.run(
|
||||
["stty", "size"],
|
||||
stdin=tty,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
rows, cols = res.stdout.strip().split()
|
||||
return int(rows), int(cols)
|
||||
except Exception: # noqa: BLE001
|
||||
return 24, 80
|
||||
|
||||
|
||||
def _center_text(text: str, width: int) -> str:
|
||||
pad = max((width - len(text)) // 2, 0)
|
||||
return " " * pad + text
|
||||
|
||||
|
||||
def _install_duration(ctx: InstallContext) -> str | None:
|
||||
candidates = [
|
||||
ctx.target / "var" / "log" / "omarchy-install.log",
|
||||
Path("/var/log/omarchy-install.log"),
|
||||
]
|
||||
patterns = ("Total:", "Omarchy:")
|
||||
for path in candidates:
|
||||
if not path.exists():
|
||||
continue
|
||||
try:
|
||||
lines = path.read_text(errors="ignore").splitlines()
|
||||
except OSError:
|
||||
continue
|
||||
for prefix in patterns:
|
||||
for line in reversed(lines[-80:]):
|
||||
if line.startswith(prefix):
|
||||
duration = line.split(":", 1)[1].strip()
|
||||
if duration:
|
||||
return duration
|
||||
return None
|
||||
|
||||
|
||||
def _render_finish_screen(ctx: InstallContext) -> None:
|
||||
_, cols = _tty_size()
|
||||
logo_path = Path("/usr/share/omarchy/logo.txt")
|
||||
green = "\033[32m"
|
||||
reset = "\033[0m"
|
||||
try:
|
||||
with open("/dev/tty", "w", encoding="utf-8") as tty:
|
||||
tty.write("\033[?25h\033[H\033[2J\n")
|
||||
if logo_path.exists():
|
||||
logo_lines = logo_path.read_text(errors="ignore").splitlines()
|
||||
logo_width = max((len(line) for line in logo_lines), default=0)
|
||||
left = max((cols - logo_width) // 2, 0)
|
||||
for line in logo_lines:
|
||||
tty.write(" " * left + green + line + reset + "\n")
|
||||
else:
|
||||
tty.write(_center_text(green + "Omarchy" + reset, cols) + "\n")
|
||||
tty.write("\n")
|
||||
duration = _install_duration(ctx)
|
||||
message = f"Installed in {duration}" if duration else "Finished installing"
|
||||
tty.write(_center_text(message, cols) + "\n\n")
|
||||
tty.flush()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def finish(ctx: InstallContext) -> None:
|
||||
_stop_install_dashboard()
|
||||
info("Installation finished. Reboot when ready.")
|
||||
_render_finish_screen(ctx)
|
||||
if confirm("Reboot now?", default=True):
|
||||
os.system("reboot")
|
||||
Reference in new issue
Block a user