fix(fnos): reuse content-addressed core runtime

This commit is contained in:
2026-08-12 17:33:05 +08:00
parent eec1227179
commit 1babec1b85
5 changed files with 63 additions and 12 deletions
+30 -8
View File
@@ -18,11 +18,15 @@ fail() {
[ -d "$WHEEL_ROOT" ] || fail "ImageFind 运行环境初始化失败:安装包缺少核心 wheelhouse。"
[ -f "$CORE_REQUIREMENTS" ] || fail "ImageFind 运行环境初始化失败:安装包缺少核心依赖锁文件。"
VERSION=$(sed -n '1p' "$VERSION_FILE")
case "$VERSION" in
PACKAGE_VERSION=$(sed -n '1p' "$VERSION_FILE")
case "$PACKAGE_VERSION" in
''|*[!0-9.]* ) fail "ImageFind 运行环境初始化失败:版本信息无效。" ;;
esac
PACKAGE_ID=$(sed -n '2p' "$VERSION_FILE")
APP_VERSION=$(sed -n '2p' "$VERSION_FILE")
case "$APP_VERSION" in
''|*[!0-9.]* ) fail "ImageFind 运行环境初始化失败:应用版本信息无效。" ;;
esac
PACKAGE_ID=$(sed -n '3p' "$VERSION_FILE")
case "$PACKAGE_ID" in
''|*[!0-9a-f]* ) fail "ImageFind 运行环境初始化失败:安装包指纹无效。" ;;
esac
@@ -39,8 +43,9 @@ PYTHON_VERSION=$($SYSTEM_PYTHON -c 'import sys; print(f"{sys.version_info.major}
[ "$PYTHON_VERSION" = "3.12" ] || fail "ImageFind 需要 Python 3.12,当前为 $PYTHON_VERSION。"
mkdir -p "$RUNTIME_ROOT"
TARGET="$RUNTIME_ROOT/core-$VERSION"
STAGE="$RUNTIME_ROOT/.core-$VERSION.tmp.$$"
RUNTIME_ID=$(printf '%s' "$PACKAGE_ID" | cut -c1-16)
TARGET="$RUNTIME_ROOT/core-$APP_VERSION-$RUNTIME_ID"
STAGE="$RUNTIME_ROOT/.core-$APP_VERSION-$RUNTIME_ID.tmp.$$"
CURRENT="$RUNTIME_ROOT/current"
runtime_valid() {
@@ -51,7 +56,7 @@ runtime_valid() {
IMAGEFIND_DATA_DIR="$DATA_ROOT/data" "$1/bin/python" -c \
'import imagefind; print(imagefind.__version__)' 2>"$RUNTIME_ROOT/version-check.err"
) || return 1
[ "$RUNTIME_VERSION" = "$VERSION" ]
[ "$RUNTIME_VERSION" = "$APP_VERSION" ]
}
if ! runtime_valid "$TARGET"; then
@@ -83,7 +88,7 @@ if ! runtime_valid "$TARGET"; then
fail "ImageFind 核心运行环境校验失败。"
}
if [ -e "$TARGET" ]; then
BACKUP="$RUNTIME_ROOT/core-$VERSION.invalid.$(date +%s)"
BACKUP="$RUNTIME_ROOT/core-$APP_VERSION-$RUNTIME_ID.invalid.$(date +%s)"
mv "$TARGET" "$BACKUP"
fi
mv "$STAGE" "$TARGET"
@@ -122,5 +127,22 @@ if ! runtime_valid "$CURRENT"; then
else
ACTUAL_VERSION='Python 不可执行'
fi
fail "ImageFind 核心运行环境切换失败:期望版本 $VERSIONcurrent=$CURRENT_LINK,实际版本=$ACTUAL_VERSION。"
fail "ImageFind 核心运行环境切换失败:期望版本 $APP_VERSIONcurrent=$CURRENT_LINK,实际版本=$ACTUAL_VERSION。"
fi
# Keep the active runtime plus one most-recent rollback candidate. fnOS package
# build numbers change on every release, so retaining every historical venv
# would otherwise grow the application data directory without bound.
ROLLBACK_KEPT=0
for OLD_RUNTIME in $(ls -dt "$RUNTIME_ROOT"/core-* 2>/dev/null || true); do
[ -d "$OLD_RUNTIME" ] || continue
[ "$OLD_RUNTIME" = "$TARGET" ] && continue
case "$OLD_RUNTIME" in
*.invalid.*) rm -rf -- "$OLD_RUNTIME"; continue ;;
esac
if [ "$ROLLBACK_KEPT" -eq 0 ]; then
ROLLBACK_KEPT=1
else
rm -rf -- "$OLD_RUNTIME"
fi
done