Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat: 托盘首启起引导服务,首次运行页升级成表单

longmeidaod1a9ab8697fb
diff --git a/src/peach/onboarding.py b/src/peach/onboarding.py
index 44f43684c..100b6e7ca 100644
--- a/src/peach/onboarding.py
+++ b/src/peach/onboarding.py
@@ -48,6 +48,12 @@ MIGRATIONS_DIR = settings_file.PROJECT_ROOT / "migrations"
#: 设置页所在的引导服务在设置完成的那一刻就会被托盘停掉,扫描不能跑在它的进程里。
SCAN_REQUEST_NAME = "first-scan.request"
+#: 监听范围的两个选项:(提交值, 说明)。题目文本和设置页的下拉都由它生成,
+#: 所以两处永远是同一批选项,改一个不会漏掉另一个。
+HOST_OPTIONS = (("1", "仅本机(127.0.0.1)"), ("2", "局域网(0.0.0.0)"))
+#: 首扫题的题面。CLI 在后面接 `(Y/n)`,设置页把它当勾选框的标签。
+SCAN_PROMPT = "现在扫描 {target}?"
+
_DNS_LABEL = re.compile(r"^[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?$")
_HOST_CHOICES = {
"1": LOOPBACK_HOST, "本机": LOOPBACK_HOST, LOOPBACK_HOST: LOOPBACK_HOST,
@@ -157,7 +163,8 @@ def questions(
Question("media_dir", "本地媒体目录(来源 local,必须已存在)",
str(media_default) if media_default else "",
media_dir_validator(windows=windows)),
- Question("host", "监听范围:1 = 仅本机(127.0.0.1),2 = 局域网(0.0.0.0)",
+ Question("host",
+ "监听范围:" + ",".join(f"{value} = {label}" for value, label in HOST_OPTIONS),
"1", validate_host),
Question("port", "服务端口", str(config.server.port), validate_port),
Question("mdns_name", "局域网名字(<名字>.local,只在监听局域网时发布)",
@@ -165,8 +172,9 @@ def questions(
)
-def scan_question(media_dir: Path) -> Question:
- return Question("scan_now", f"现在扫描 {media_dir}?(Y/n)", "Y", validate_yes_no)
+def scan_question(media_dir: Path | str) -> Question:
+ return Question("scan_now", SCAN_PROMPT.format(target=media_dir) + "(Y/n)",
+ "Y", validate_yes_no)
def ask_until_valid(
@@ -366,6 +374,7 @@ def is_interactive(stdin) -> bool:
__all__ = [
+ "HOST_OPTIONS", "SCAN_PROMPT",
"Answers", "Applied", "Ask", "DataTree", "LOCAL_LOCATION", "MAX_ATTEMPTS",
"OnboardingAborted", "POSIX_LOCAL_DECLARED_ROOT", "Question", "apply",
"ask_until_valid", "configure", "console_ask", "create_data_tree",
diff --git a/src/peach/routes_pages.py b/src/peach/routes_pages.py
index 9f67f0004..234558742 100644
--- a/src/peach/routes_pages.py
+++ b/src/peach/routes_pages.py
@@ -16,11 +16,14 @@
from __future__ import annotations
import hashlib
+import os
import re
+from html import escape
from pathlib import Path
+from urllib.parse import parse_qs
-from fastapi import APIRouter, Depends, Request
+from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import (
FileResponse,
HTMLResponse,
@@ -29,40 +32,151 @@ from fastapi.responses import (
Response,
)
-from .config import DATA_ROOT, PROJECT_ROOT, SETTINGS_PATH
+from . import onboarding, settings_file
+from .config import PROJECT_ROOT
from .routes_auth import require_asset_auth, require_page_auth, set_auth_cookie
from .web_state import FAVICON
router = APIRouter()
-#: 未配置时的首次运行页。刻意不引用 `web/` 里的任何资产:那一套一上来就会去打
-#: `/api/items`,而未配置的机器还没有数据库,页面只会是一屏红色报错。
-_SETUP_PAGE = """<!doctype html>
-<html lang="zh-CN"><head><meta charset="utf-8">
-<meta name="viewport" content="width=device-width,initial-scale=1">
-<title>Peach · 首次运行</title>
-<style>body{{margin:0;padding:3rem 1.5rem;background:#141216;color:#f2eef5;
-font:16px/1.7 system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif}}
-main{{max-width:36rem;margin:0 auto}}h1{{font-size:1.5rem;margin:0 0 1rem}}
-code{{background:#231f27;border-radius:.3rem;padding:.15rem .4rem}}
-pre{{background:#231f27;border-radius:.5rem;padding:.9rem 1rem;overflow-x:auto}}
-dt{{color:#b9aec4;font-size:.9rem}}dd{{margin:0 0 .6rem;word-break:break-all}}</style>
-</head><body><main>
-<h1>Peach 还没初始化</h1>
-<p>这台机器上还没有数据根和设置文件,所以没有账本可读。服务本身是正常的。</p>
-<pre>peach init</pre>
-<p>它会建好数据目录、把数据库迁到最新、生成设置文件和本机 CA,然后告诉你下一步。
-已经在跑的旧部署改用 <code>peach init --from-existing</code>:只生成设置文件,不动现有数据。</p>
-<dl><dt>数据根(预定落点)</dt><dd>{data_root}</dd>
-<dt>设置文件</dt><dd>{settings_path}</dd></dl>
-<p><code>/healthz</code> 会报 <code>configured: false</code>,初始化完刷新本页即可。</p>
-</main></body></html>
-"""
+#: 回环地址的三种写法。既用来判提交端点的调用方,也用来判「仅本机」那个监听选择。
+_LOOPBACK = frozenset({"127.0.0.1", "::1", "localhost"})
+
+#: 首次运行页的样式。刻意不引用 `web/` 里的任何资产:那一套一上来就会去打
+#: `/api/items`,而未配置的机器还没有数据库,页面只会是一屏红色报错。这一页因此
+#: 落在 SPA 外壳之外,不是一个 `frontend/` island(ADR-0022、docs/FRONTEND.md)。
+_SETUP_STYLE = """<style>
+body{margin:0;padding:3rem 1.5rem;background:#141216;color:#f2eef5;
+font:16px/1.7 system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif}
+main{max-width:36rem;margin:0 auto}h1{font-size:1.5rem;margin:0 0 1rem}
+code{background:#231f27;border-radius:.3rem;padding:.15rem .4rem}
+dt{color:#b9aec4;font-size:.9rem}dd{margin:0 0 .6rem;word-break:break-all}
+label{display:block;margin:1.4rem 0 .35rem;color:#e7dff0}
+input[type=text],input[type=number],select{width:100%;box-sizing:border-box;height:2.6rem;
+padding:0 .7rem;border:1px solid #3a3340;border-radius:.5rem;background:#0f0d12;
+color:#f2eef5;font:inherit}
+input:focus,select:focus{outline:none;border-color:#ff8b70}
+.note{margin:.35rem 0 0;color:#b9aec4;font-size:.88rem}
+.bad{margin:.35rem 0 0;color:#ff9a9a;font-size:.9rem}
+.check{display:flex;align-items:center;gap:.6rem;margin:1.6rem 0 0;color:#e7dff0}
+.check input{width:1.1rem;height:1.1rem;margin:0}
+button{margin-top:1.8rem;width:100%;height:2.8rem;border:0;border-radius:.6rem;
+cursor:pointer;background:linear-gradient(135deg,#ff9a76,#f2557b);color:#130609;
+font:700 15px system-ui,sans-serif}
+button:hover{filter:brightness(1.06)}
+</style>"""
+
+_SETUP_HEAD = ('<!doctype html>\n<html lang="zh-CN"><head><meta charset="utf-8">'
+ '<meta name="viewport" content="width=device-width,initial-scale=1">'
+ '<meta name="color-scheme" content="dark">')
+
+#: 键 -> 输入控件类型。题目、默认值与顺序全部来自 `onboarding.questions()`,
+#: 这里只决定同一道题在浏览器里长什么样,不另抄一份字段清单。
+_SETUP_WIDGETS = {"host": "select", "port": "number"}
+
+
+def _document(title: str, body: str) -> str:
+ return (f"{_SETUP_HEAD}<title>{title}</title>{_SETUP_STYLE}"
+ f"</head><body><main>{body}</main></body></html>\n")
+
+
+def _field_html(
+ question, value: str, error: str, note: str,
+) -> str:
+ key = escape(question.key, quote=True)
+ label = f'<label for="f-{key}">{escape(question.prompt)}</label>'
+ if _SETUP_WIDGETS.get(question.key) == "select":
+ options = "".join(
+ f'<option value="{escape(choice, quote=True)}"'
+ f'{" selected" if choice == value else ""}>{escape(text)}</option>'
+ for choice, text in onboarding.HOST_OPTIONS
+ )
+ control = f'<select id="f-{key}" name="{key}">{options}</select>'
+ else:
+ kind = _SETUP_WIDGETS.get(question.key, "text")
+ control = (f'<input id="f-{key}" name="{key}" type="{kind}" required '
+ f'value="{escape(value, quote=True)}">')
+ tail = f'<p class="note">{escape(note)}</p>' if note else ""
+ tail += f'<p class="bad" role="alert">{escape(error)}</p>' if error else ""
+ return label + control + tail
+
+
+def setup_page(
+ config, *, windows: bool, values: dict[str, str] | None = None,
+ errors: dict[str, str] | None = None, scan_now: bool = True,
+) -> str:
+ """首次运行表单。题目、默认值与顺序全部来自 `onboarding.questions()`。
+
+ 校验失败时带着 `values` 和 `errors` 重新渲染:已经填对的几项不能让人再填一遍,
+ 错在哪一项也要写在那一项底下,而不是页首一句「有字段不合法」。
+ """
+ values = values or {}
+ errors = errors or {}
+ asked = onboarding.questions(config, windows=windows)
+ fields = []
+ for question in asked:
+ value = values.get(question.key, question.default)
+ note = ""
+ if question.key == "media_dir" and not windows:
+ note = onboarding.mounts_explanation(value or "你在上面填的目录")
+ fields.append(_field_html(question, value, errors.get(question.key, ""), note))
+ media_value = values.get("media_dir") or next(
+ (q.default for q in asked if q.key == "media_dir"), "") or "这个目录"
+ scan_label = onboarding.SCAN_PROMPT.format(target=media_value)
+ body = (
+ "<h1>欢迎使用 Peach</h1>"
+ "<p>这台机器还没有数据根和设置文件。填完下面几项就能开始用;"
+ "每一项都已经填好默认值,不确定就直接提交。</p>"
+ '<form method="post" action="/setup">'
+ + "".join(fields)
+ + f'<label class="check"><input type="checkbox" name="scan_now" value="y"'
+ + (" checked" if scan_now else "")
+ + f'>{escape(scan_label)}</label>'
+ '<p class="note">扫描只读取文件名、大小和修改时间,不改动任何媒体文件。</p>'
+ '<button type="submit">完成设置</button></form>'
+ )
+ return _document("Peach · 首次运行", body)
+
+
+def setup_done_page(applied, *, windows: bool, scan_requested: bool) -> str:
+ """成功页:接下来会自动发生什么,以及口令在哪。口令本身不显示在页面上。"""
+ tree = applied.tree
+ config = applied.config
+ ledger = (f"账本已存在,没有动它:{tree.database}" if tree.ledger_existed
+ else f"账本:{tree.database}(已应用 {tree.migrations} 个迁移)")
+ ca = (f"本机 CA:{tree.ca_cert}" if tree.ca_cert is not None
+ else f"未生成本机 CA({tree.ca_error});装好 openssl 后跑 "
+ "<code>peach init --force</code> 补上。局域网设备要装这份 CA 才不报证书错。")
+ scan = ("<li>首次扫描已排队,托盘会在服务起来之后在后台跑,期间页面照常能用。</li>"
+ if scan_requested else
+ "<li>没有请求首次扫描;要扫就跑 <code>peach scan local</code>。</li>")
+ mounts = ("" if windows else
+ f"<p>{escape(onboarding.mounts_explanation(config.mounts.get('local', '')))}</p>")
+ body = (
+ "<h1>设置完成</h1>"
+ "<p>托盘正在停掉这条引导服务,改用正常的 Peach 服务;这个页面几秒后就会连不上,"
+ f"届时打开 <code>{escape(_normal_url(config))}</code> 即可。</p>"
+ "<ul>"
+ f"<li>{escape(ledger)}</li>"
+ f"<li>{ca}</li>"
+ f"<li>访问口令文件:<code>{escape(str(tree.token_path))}</code>;"
+ "口令内容用 <code>peach token</code> 看,别的设备第一次访问时贴进登录页。</li>"
+ f"{scan}"
+ "</ul>"
+ f"{mounts}"
+ "<dl>"
+ f"<dt>数据根</dt><dd>{escape(str(config.data_root))}</dd>"
+ f"<dt>设置文件</dt><dd>{escape(str(applied.settings_path))}</dd>"
+ "</dl>"
+ )
+ return _document("Peach · 设置完成", body)
-def setup_page() -> str:
- """未配置提示页。路径是这一页唯一的动态内容,别让人自己去猜文件在哪。"""
- return _SETUP_PAGE.format(data_root=DATA_ROOT, settings_path=SETTINGS_PATH)
+def _normal_url(config) -> str:
+ """设置完成之后正常服务的地址。仅本机就给回环,局域网给 `<名字>.local`。"""
+ if config.server.host in _LOOPBACK:
+ return f"http://127.0.0.1:{config.server.port}/"
+ return f"https://{config.server.mdns_name}.local/"
def asset_response(request: Request, path: Path, media: str) -> Response:
@@ -135,8 +249,8 @@ def index(request: Request, args: dict[str, str] = Depends(require_page_auth)):
set_auth_cookie(response, request)
return response
if not settings.configured:
- # 未配置不是错误状态:服务照常起,页面告诉人去跑 `peach init`。
- return HTMLResponse(setup_page())
+ # 未配置不是错误状态:服务照常起,首页变成首次运行表单。
+ return HTMLResponse(setup_page(settings_file.active(), windows=os.name == "nt"))
if not settings.page_path.is_file():
return PlainTextResponse("Peach page missing", status_code=500)
response = FileResponse(settings.page_path, media_type="text/html")
@@ -145,6 +259,67 @@ def index(request: Request, args: dict[str, str] = Depends(require_page_auth)):
return response
+async def setup_submit(request: Request):
+ """首次运行表单的提交端点。落盘逻辑全在 `peach.onboarding`,这里只做守卫和渲染。
+
+ 三道守卫,形态各不相同因为原因各不相同:已经配置过的机器上这个端点根本不存在
+ (404,不是「禁止」——把它做成一条可探测的 403 等于对外宣告这里有个初始化入口);
+ 非回环调用方是 403(引导服务只绑 127.0.0.1,能走到这里说明有人转发了它);
+ 设置文件已经在了是 409(并发提交或刷新重发,不能覆盖别人刚写好的那份)。
+
+ 扫描不在这里跑:这条引导服务在设置完成的那一刻就会被托盘停掉,跑在它进程里的
+ 扫描会跟着一起死。这里只写一个标记,由托盘切到正常服务之后消费。
+ """
+ settings = request.app.state.settings
+ if settings.configured:
+ raise HTTPException(status_code=404, detail="not found")
+ host = request.client.host if request.client else ""
+ if host not in _LOOPBACK:
+ raise HTTPException(status_code=403, detail="setup is loopback-only")
+
+ windows = os.name == "nt"
+ form = parse_qs((await request.body()).decode("utf-8", "replace"), keep_blank_values=True)
+ submitted = {key: (value or [""])[0] for key, value in form.items()}
+ scan_now = "scan_now" in form
+
+ config = settings_file.active()
+ answers, errors = _read_answers(config, submitted, windows=windows)
+ if errors:
+ return HTMLResponse(
+ setup_page(config, windows=windows, values=submitted, errors=errors,
+ scan_now=scan_now),
+ status_code=400,
+ )
+ # 数据根决定设置文件在哪,所以拿到它之后要按它重新解析一次,不能沿用进程启动
+ # 那一刻按发现顺序算出来的这份。
+ resolved, _broken = onboarding.resolve_config(answers.data_root)
+ if resolved.path.exists():
+ raise HTTPException(status_code=409, detail="settings file already exists")
+ applied = onboarding.apply(resolved, answers, windows=windows)
+ if scan_now:
+ onboarding.request_first_scan(applied.config)
+ return HTMLResponse(
+ setup_done_page(applied, windows=windows, scan_requested=scan_now))
+
+
+def _read_answers(
+ config, submitted: dict[str, str], *, windows: bool,
+) -> tuple[object, dict[str, str]]:
+ """逐字段校验,错误按字段收集。校验器和 CLI 问答用的是同一批。"""
+ values: dict[str, object] = {}
+ errors: dict[str, str] = {}
+ for question in onboarding.questions(config, windows=windows):
+ raw = submitted.get(question.key, "")
+ try:
+ values[question.key] = question.validate(raw if raw.strip() else question.default)
+ except ValueError as exc:
+ errors[question.key] = str(exc)
+ if errors:
+ return None, errors
+ return onboarding.Answers(**values), {} # type: ignore[arg-type]
+
+
@router.api_route("/app.css", methods=["GET", "HEAD"])
@router.api_route("/app.js", methods=["GET", "HEAD"])
def app_asset(request: Request, args: dict[str, str] = Depends(require_asset_auth)):
diff --git a/src/peach/tray.py b/src/peach/tray.py
index 5008182b0..c36f2184a 100644
--- a/src/peach/tray.py
+++ b/src/peach/tray.py
@@ -18,10 +18,11 @@ import httpx
import pystray
from PIL import Image
+from . import onboarding, settings_file
from .appid import MACOS_LAUNCH_AGENT_LABEL
from .certs import ensure_certificate
from .config import (
- DATABASE_PATH, LOG_DIR, MDNS_HOSTNAME, PROJECT_ROOT, REPLICATION_ENABLED,
+ DATABASE_PATH, LOG_DIR, PROJECT_ROOT, REPLICATION_ENABLED,
SECRETS_DIR, SHARED_DATABASE_PATH, SHARED_SMB_HOST, SHARED_SMB_SHARE,
SHARED_SMB_USER, STATE_DIR,
)
@@ -41,11 +42,6 @@ LOGGER = logging.getLogger(__name__)
MACOS_PORT = 8900
#: HTTPS 同理,443 也要 root,所以走 8443。
MACOS_TLS_PORT = 8443
-#: 单击菜单栏/托盘图标打开本机的固定地址 `<mdns_name>.local`;名字来自设置文件的
-#: `[server].mdns_name`,同一局域网里两台机器必须不同名。macOS 上 80/443 由 pf 转到
-#: 高位端口(scripts/setup_macos_port80.sh)。
-OPEN_URL = f"https://{MDNS_HOSTNAME}/"
-
#: LaunchAgent 的标签。托盘、安装脚本和 `.app` 外壳共用 `peach.appid` 这一处定义。
LAUNCH_AGENT_LABEL = MACOS_LAUNCH_AGENT_LABEL
@@ -194,8 +190,15 @@ class ServiceManager:
run: Callable[..., subprocess.CompletedProcess] = subprocess.run,
ledger_plan: Callable[[], SyncPlan] | None = None,
mount_share: Callable[[], bool] | None = None,
+ log_dir: Path | None = None,
+ extra_env: dict[str, str] | None = None,
) -> None:
self.specs = specs
+ # 日志目录与子进程环境可替换:首次设置完成后数据根可能换了地方,那之后拉起的
+ # 服务必须把日志写进新数据根,也必须显式看到新的 PEACH_DATA_ROOT——托盘进程
+ # 自己的模块常量还停在启动那一刻。
+ self._log_dir = Path(log_dir) if log_dir is not None else LOG_DIR
+ self._extra_env = dict(extra_env or {})
self._popen = popen
self._health_get = health_get
self._run = run
@@ -225,6 +228,10 @@ class ServiceManager:
self._last_health[spec.name] = (ok, detail)
return ok
+ @property
+ def log_dir(self) -> Path:
+ return self._log_dir
+
def status(self) -> str:
"""菜单里那一行状态:每个服务逐个点名,正常的和异常的都写出来。
@@ -239,12 +246,34 @@ class ServiceManager:
parts.append(f"{spec.name.upper()} {state}")
return " · ".join(parts)
- def start_missing(self) -> None:
- LOG_DIR.mkdir(parents=True, exist_ok=True)
+ def replace_specs(
+ self, specs: tuple[ServiceSpec, ...], *, log_dir: Path | None = None,
+ extra_env: dict[str, str] | None = None,
+ ) -> None:
+ """换掉这一组服务规格。先停掉旧的那组,调用方随后 `start_missing()`。
+
+ 首次设置完成时走这条路:引导服务停下,正常的 HTTP/HTTPS 顶上,托盘进程不重启。
+ """
+ self.stop_owned()
+ with self._lock:
+ self.specs = tuple(specs)
+ if log_dir is not None:
+ self._log_dir = Path(log_dir)
+ if extra_env is not None:
+ self._extra_env = dict(extra_env)
+ self._last_health = {spec.name: (False, "未检测") for spec in self.specs}
+
+ def child_environment(self) -> dict[str, str]:
environment = os.environ.copy()
if getattr(sys, "frozen", False):
# A frozen tray must not pass its one-file bootloader state to Peach.exe.
environment["PYINSTALLER_RESET_ENVIRONMENT"] = "1"
+ environment.update(self._extra_env)
+ return environment
+
+ def start_missing(self) -> None:
+ self._log_dir.mkdir(parents=True, exist_ok=True)
+ environment = self.child_environment()
with self._lock:
for spec in self.specs:
owned = self._owned.get(spec.name)
@@ -252,8 +281,8 @@ class ServiceManager:
continue
if self.healthy(spec):
continue
- stdout = (LOG_DIR / f"tray-{spec.name}.out.log").open("ab")
- stderr = (LOG_DIR / f"tray-{spec.name}.err.log").open("ab")
+ stdout = (self._log_dir / f"tray-{spec.name}.out.log").open("ab")
+ stderr = (self._log_dir / f"tray-{spec.name}.err.log").open("ab")
self._logs.extend((stdout, stderr))
creationflags = subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0
self._owned[spec.name] = self._popen(
@@ -392,7 +421,7 @@ class ServiceManager:
]
if take_ownership:
command.append("--take-ownership")
- sync_environment = os.environ.copy()
+ sync_environment = self.child_environment()
# Windows 上 Python 的 stdout 被管道捕获时默认使用 ANSI code page
#(简中系统是 GBK),而父进程按 UTF-8 解码。明确约定子进程输出 UTF-8,
# 不能只锁定父进程的 decoding,否则中文通知会全部变成替换字符。
@@ -439,7 +468,7 @@ def _peach_executable() -> Path:
raise FileNotFoundError(f"{_EXECUTABLE} is missing; reinstall the editable project")
-def build_macos_service_specs() -> tuple[ServiceSpec, ...]:
+def build_macos_service_specs(*, tls_dir: Path | None = None) -> tuple[ServiceSpec, ...]:
"""macOS 用非特权端口起 HTTP,有 TLS 材料时再加一个 HTTPS。
80/443 在 macOS 上要 root,所以服务本身跑在 8900/8443,由 pf 把 80/443 转过去
@@ -461,7 +490,7 @@ def build_macos_service_specs() -> tuple[ServiceSpec, ...]:
True,
),
]
- cert_dir = SECRETS_DIR / "tls"
+ cert_dir = Path(tls_dir) if tls_dir is not None else SECRETS_DIR / "tls"
ca, cert, key = (cert_dir / n for n in
("peach-local-ca.crt", "peach.crt", "peach.key"))
if all(path.is_file() for path in (ca, cert, key)):
@@ -488,7 +517,7 @@ def build_service_specs(
tls_dir: Path | None = None,
) -> tuple[ServiceSpec, ...]:
if sys.platform == "darwin":
- return build_macos_service_specs()
+ return build_macos_service_specs(tls_dir=tls_dir)
address = lan_address or os.environ.get("PEACH_LAN_ADDRESS") or lan_ipv4()
peach = str(_peach_executable())
cert_dir = Path(tls_dir) if tls_dir is not None else SECRETS_DIR / "tls"
@@ -521,6 +550,165 @@ def build_service_specs(
)
+#: 引导服务这条规格的名字。它和 `http`/`https` 互斥:托盘要么在等人填表单,
+#: 要么在跑正常服务,不会两者同时。
+SETUP_SPEC_NAME = "setup"
+
+
+def needs_setup(config) -> bool:
+ """这台机器还得先走一遍首次设置吗。
+
+ 判据不是 `config.configured`,而是「没有设置文件,也没有账本」。数据根目录本身
+ 不算数:托盘的单实例锁一启动就会在数据根下建出 `state/`,而 `discover_data_root`
+ 只看目录在不在——只用 `configured` 的话,一次失败的托盘启动就足以让下一次启动
+ 认为这台机器已经配置好,然后照旧倒在缺 TLS 材料上。反过来,还没生成过
+ `config.toml` 的老部署账本是在的,不能被拖进首次设置。
+ """
+ if config.present:
+ return False
+ return not (config.directory("database") / "ledger.db").is_file()
+
+
+def build_setup_service_specs(config) -> tuple[ServiceSpec, ...]:
+ """首次设置的引导服务:只绑回环、无 TLS、无口令、不发布 mDNS。
+
+ 不能用正常规格:全新机器上 `build_service_specs()` 会因为缺 TLS 材料直接抛错,
+ 而 macOS 那条即使起得来,`peach serve --host 0.0.0.0` 也会被 `_serve_token`
+ 以「没有口令」拒掉。这条服务的安全边界就是那个绑定地址——回环之外够不着它。
+ """
+ peach = str(_peach_executable())
+ port = config.server.port
+ return (
+ ServiceSpec(
+ SETUP_SPEC_NAME,
+ f"http://127.0.0.1:{port}/healthz",
+ (
+ peach, "serve", "--setup", "--host", "127.0.0.1", "--port", str(port),
+ "--no-mdns", "--no-ledger-sync",
+ ),
+ True,
+ ),
+ )
+
+
+def setup_url(config) -> str:
+ return f"http://127.0.0.1:{config.server.port}/"
+
+
+def normal_url(config) -> str:
+ """正常服务的固定地址 `<mdns_name>.local`,同一局域网里两台机器必须不同名。
+
+ 名字取新鲜读到的设置,不用 import 期就定型的 `MDNS_HOSTNAME`:首次设置里那一题
+ 正是它。macOS 上 80/443 由 pf 转到高位端口(scripts/setup_macos_port80.sh)。
+ """
+ return f"https://{config.server.mdns_name}.local/"
+
+
+class SetupGate:
+ """首次设置期间的服务切换。Windows 托盘与 macOS 菜单栏共用这一层。
+
+ 托盘不重启自己就能完成切换,因为真正读数据根的是**子进程**:`peach serve` 每次
+ 都自己读一遍设置文件。托盘进程里剩下的几处依赖都已经改成可传入——服务规格的
+ TLS 目录、日志目录、子进程环境里的 `PEACH_DATA_ROOT`、菜单里的地址。账本复制那
+ 几个模块常量不在此列,但首次设置写出的 `replication.enabled` 恒为 false,
+ 对应菜单项本来就不装配。
+ """
+
+ def __init__(
+ self,
+ manager: ServiceManager,
+ config,
+ *,
+ waiting: bool,
+ load: Callable[[], object] = settings_file.load_config,
+ popen: Callable[..., subprocess.Popen] = subprocess.Popen,
+ open_browser: Callable[[str], object] = webbrowser.open,
+ ) -> None:
+ self.manager = manager
+ self.config = config
+ self._waiting = waiting
+ self._load = load
+ self._popen = popen
+ self._open_browser = open_browser
+ #: 由各平台的托盘装上;默认丢掉,`SetupGate` 本身不认识通知机制。
+ self.notify: Callable[[str, str], None] = lambda _message, _title: None
+
+ @property
+ def waiting(self) -> bool:
+ return self._waiting
+
+ def open_label(self) -> str:
+ return "重新打开设置页" if self._waiting else "打开 Peach"
+
+ def open_url(self) -> str:
+ return setup_url(self.config) if self._waiting else normal_url(self.config)
+
+ def status_line(self) -> str:
+ if self._waiting:
+ return f"等待完成首次设置 · {self.manager.status()}"
+ return self.manager.status()
+
+ def open(self) -> None:
+ self._open_browser(self.open_url())
+
+ def poll(self) -> bool:
+ """设置完成了就切到正常服务。返回这一轮有没有切。
+
+ 由健康轮询驱动,所以每几秒问一次:设置文件是新鲜读的,不是模块常量;TLS 材料
+ 还没齐就原地等下一轮,而不是拿一组缺文件的规格去启动。
+ """
+ if not self._waiting:
+ return False
+ config = self._load()
+ if needs_setup(config):
+ return False
+ try:
+ specs = build_service_specs(tls_dir=config.directory("secrets") / "tls")
+ except FileNotFoundError:
+ return False # CA 还没生成完(或没有 openssl),下一轮再看
+ self._waiting = False
+ self.config = config
+ self.manager.replace_specs(
+ specs, log_dir=config.directory("logs"),
+ extra_env={"PEACH_DATA_ROOT": str(config.data_root)},
+ )
+ self.manager.start_missing()
+ self.notify(f"首次设置完成,正在启动服务:{normal_url(config)}", "Peach")
+ self.start_first_scan(config)
+ return True
+
+ def start_first_scan(self, config) -> subprocess.Popen | None:
+ """消费首扫标记,用子进程跑一遍 `peach scan <来源>`。
+
+ 扫描不能跑在引导服务里——那个进程刚被上面停掉;也不能跑在托盘线程里——
+ 几万个文件要走十几分钟,会把健康轮询和菜单一起卡住。
+ """
+ location = onboarding.take_first_scan_request(config)
+ if location is None:
+ return None
+ log_dir = config.directory("logs")
+ log_dir.mkdir(parents=True, exist_ok=True)
+ environment = self.manager.child_environment()
+ environment["PEACH_DATA_ROOT"] = str(config.data_root)
+ # Windows 上子进程的 stdout 被重定向时默认走 ANSI code page,扫描进度里的
+ # 中文会变成替换字符;这里和账本同步用同一条约定。
+ environment["PYTHONIOENCODING"] = "utf-8"
+ try:
+ handle = (log_dir / "tray-scan.out.log").open("ab")
+ process = self._popen(
+ [str(_peach_executable()), "scan", location],
+ cwd=str(PROJECT_ROOT), stdin=subprocess.DEVNULL,
+ stdout=handle, stderr=subprocess.STDOUT, shell=False,
+ creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0,
+ env=environment,
+ )
+ except (OSError, subprocess.SubprocessError) as exc:
+ self.notify(f"首次扫描没能启动:{exc}", "Peach")
+ return None
+ self.notify(f"首次扫描已在后台开始,进度写在 {log_dir / 'tray-scan.out.log'}", "Peach")
+ return process
+
+
def create_icon(size: int = 64, *, template: bool = False) -> Image.Image:
"""Load the shared square Peach brand asset at tray resolution.
@@ -572,8 +760,10 @@ class PeachTray:
manager: ServiceManager,
versions: VersionManager | None = None,
windows_updates: WindowsUpdateInstaller | None = None,
+ gate: "SetupGate | None" = None,
) -> None:
self.manager = manager
+ self.gate = gate or SetupGate(manager, settings_file.active(), waiting=False)
self.versions = versions or VersionManager()
self.windows_updates = windows_updates or WindowsUpdateInstaller(
getattr(self.versions, "root", PROJECT_ROOT),
@@ -596,8 +786,8 @@ class PeachTray:
create_icon(template=sys.platform == "darwin"),
"Peach · 蜜桃",
pystray.Menu(
- pystray.MenuItem("打开 Peach", self.open, default=True),
- pystray.MenuItem(lambda _item: f"状态:{self.manager.status()}", None, enabled=False),
+ pystray.MenuItem(lambda _item: self.gate.open_label(), self.open, default=True),
+ pystray.MenuItem(lambda _item: f"状态:{self.gate.status_line()}", None, enabled=False),
pystray.Menu.SEPARATOR,
pystray.MenuItem("同步开发进度", self.sync_source),
*ledger_menu_items(
@@ -611,7 +801,8 @@ class PeachTray:
)
def open(self, _icon=None, _item=None) -> None:
- webbrowser.open(OPEN_URL)
+ # 等待首次设置时打开的是引导服务的表单,不是那个还没有人在监听的 `.local`。
+ self.gate.open()
def restart(self, icon=None, _item=None) -> None:
if not self._action_lock.acquire(blocking=False):
@@ -735,6 +926,8 @@ class PeachTray:
def _monitor(self) -> None:
while not self._stop_event.wait(10):
+ # 先看首次设置有没有完成:切换会换掉 `manager.specs`,采样必须落在换完之后。
+ self.gate.poll()
for spec in self.manager.specs:
self.manager.healthy(spec)
self.icon.update_menu()
@@ -743,13 +936,19 @@ class PeachTray:
icon.visible = True
# 必须等图标真的创建出来才拿得到底层 NSImage,所以放在 setup 里而不是构造时。
apply_macos_template(icon)
+ self.gate.notify = lambda message, title: icon.notify(message, title)
if self._startup_warning:
icon.notify(self._startup_warning, "Peach")
def run(self) -> None:
self.manager.start_missing()
self._startup_warning = None
- if not self.manager.wait_until_ready():
+ if self.gate.waiting:
+ if self.manager.wait_until_ready():
+ self.gate.open()
+ else:
+ self._startup_warning = "首次设置服务没能启动,请查看日志。"
+ elif not self.manager.wait_until_ready():
self._startup_warning = "Peach 只启动了部分服务,请查看托盘状态和日志。"
threading.Thread(target=self._monitor, name="PeachHealth", daemon=True).start()
try:
@@ -804,7 +1003,7 @@ def restart_tray_process(
)
-def run_macos_menu_bar(manager: "ServiceManager") -> None:
+def run_macos_menu_bar(manager: "ServiceManager", gate: "SetupGate | None" = None) -> None:
"""macOS 走原生菜单栏项。
pystray 的 darwin 后端漏了 activation policy 和图标尺寸两件必需的事,补齐等于
@@ -812,6 +1011,7 @@ def run_macos_menu_bar(manager: "ServiceManager") -> None:
"""
from .menubar import MenuBarApp
+ gate = gate or SetupGate(manager, settings_file.active(), waiting=False)
versions = VersionManager()
# 「同步开发进度」会把版本行写旧,所以标题读这个可变快照而不是闭包里那一份。
# 不在标题里直接调 `inspect()`:它要开四次 git,而标题每 5 秒刷新一次。
@@ -904,9 +1104,9 @@ def run_macos_menu_bar(manager: "ServiceManager") -> None:
create_icon(template=True),
"Peach · 蜜桃",
[
- ("打开 Peach", lambda: webbrowser.open(OPEN_URL)),
- (lambda: f"状态:{manager.status()}", None),
- (f"地址:{OPEN_URL}", None),
+ (gate.open_label, gate.open),
+ (lambda: f"状态:{gate.status_line()}", None),
+ (lambda: f"地址:{gate.open_url()}", None),
(None, None),
("同步开发进度", sync_source),
*ledger_menu_items(
@@ -920,12 +1120,16 @@ def run_macos_menu_bar(manager: "ServiceManager") -> None:
],
)
app["app"] = menu
+ gate.notify = lambda message, title: notify(message, title)
+ if gate.waiting:
+ gate.open()
# `manager.status()` 读的是 `healthy()` 写下的缓存。pystray 那条路径有 `_monitor`
# 线程定时重采样,这条路径漏了,于是缓存永远停在启动那一刻——服务还没起来时测的
# 那一次 False,菜单里就一直显示「未运行」。
def poll_health() -> None:
while not stopped.wait(5.0):
+ gate.poll()
for spec in manager.specs:
manager.healthy(spec)
@@ -936,7 +1140,11 @@ def run_macos_menu_bar(manager: "ServiceManager") -> None:
报证书无效。由系统的网络变化事件驱动,不轮询——换 Wi-Fi 的那一刻就跑。
"""
try:
- reason = ensure_certificate(SECRETS_DIR / "tls", MDNS_HOSTNAME, {lan_ipv4()})
+ # 目录和主机名取 gate 手上那份配置:首次设置可能刚把数据根和 mDNS 名换掉,
+ # 模块常量还停在托盘启动那一刻。
+ reason = ensure_certificate(
+ gate.config.directory("secrets") / "tls",
+ f"{gate.config.server.mdns_name}.local", {lan_ipv4()})
except Exception:
LOGGER.exception("证书自检失败")
return False
@@ -1002,20 +1210,26 @@ def main(argv: list[str] | None = None) -> int:
build_parser().parse_args(argv)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
enable_hidpi()
- instance = SingleInstance(STATE_DIR / "peach-tray.lock")
+ # 设置文件新鲜读一次。`config.py` 的常量是 import 期算的,而首次设置正要改变它们
+ # 指向的数据根;这里只用它判断该起哪一组服务,之后由 `SetupGate` 接手。
+ config = settings_file.load_config()
+ waiting = needs_setup(config)
+ instance = SingleInstance(config.directory("state") / "peach-tray.lock")
manager = None
try:
instance.acquire()
except AlreadyRunning:
- webbrowser.open(OPEN_URL)
+ webbrowser.open(setup_url(config) if waiting else normal_url(config))
return 0
try:
- manager = ServiceManager(build_service_specs())
- manager.start_missing()
+ specs = build_setup_service_specs(config) if waiting else build_service_specs()
+ manager = ServiceManager(specs, log_dir=config.directory("logs"))
+ gate = SetupGate(manager, config, waiting=waiting)
if sys.platform == "darwin":
- run_macos_menu_bar(manager)
+ manager.start_missing()
+ run_macos_menu_bar(manager, gate)
else:
- PeachTray(manager).run()
+ PeachTray(manager, gate=gate).run()
except Exception as exc:
show_message("Peach 启动失败", str(exc), error=True)
return 1