fix: 新增初始资产导入脚本,解决新管理端无模板无法部署的问题

- import-assets.py: Python 脚本,从 configs 目录导入模板/场景/覆盖层到 SQLite
- import-assets.sh: Bash 备选方案
- assets-import.tar.gz: 包含 edge 项目的 configs 和导入脚本的离线包

待办: 管理端启动时应自动检测空库并导入种子数据
This commit is contained in:
tian 2026-07-23 16:11:06 +08:00
parent 3d109b4b11
commit 97af8904ef
3 changed files with 144 additions and 0 deletions

BIN
assets-import.tar.gz Normal file

Binary file not shown.

View File

@ -0,0 +1,75 @@
#!/usr/bin/env python3
"""SafeSight 初始资产导入脚本
edge 项目 configs 目录下的模板/场景/覆盖层导入管理端数据库
用法: sudo python3 import-assets.py /tmp/configs
"""
import sys, os, json, sqlite3
from datetime import datetime, timezone
def main():
configs_dir = sys.argv[1] if len(sys.argv) > 1 else "/tmp/configs"
db_path = "/opt/safesightd/data/app.db"
if not os.path.isfile(db_path):
print(f"错误: 数据库文件不存在: {db_path}")
sys.exit(1)
conn = sqlite3.connect(db_path)
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
kinds = [
("templates", "templates", ["name", "description", "body_json", "created_at", "updated_at"]),
("profiles", "profiles", ["name", "primary_template_name", "business_name", "description", "body_json", "created_at", "updated_at"]),
("overlays", "overlays", ["name", "description", "body_json", "created_at", "updated_at"]),
]
print("========== SafeSight 资产导入 ==========")
print(f"数据库: {db_path}")
print(f"配置源: {configs_dir}\n")
for dir_name, table, cols in kinds:
src_dir = os.path.join(configs_dir, dir_name)
if not os.path.isdir(src_dir):
print(f" ⚠ 目录不存在: {src_dir}")
continue
count = 0
for fname in sorted(os.listdir(src_dir)):
if not fname.endswith(".json"):
continue
fpath = os.path.join(src_dir, fname)
name = fname[:-5] # remove .json
with open(fpath, "r", encoding="utf-8") as f:
body = f.read()
placeholders = ", ".join(["?" for _ in cols])
col_names = ", ".join(cols)
if table == "templates":
values = [name, "", body, now, now]
elif table == "overlays":
values = [name, "", body, now, now]
else: # profiles
values = [name, "", "", "", body, now, now]
try:
conn.execute(
f"INSERT OR IGNORE INTO {table} ({col_names}) VALUES ({placeholders})",
values,
)
count += 1
print(f"{dir_name.rstrip('s')}: {name}")
except Exception as e:
print(f"{name}: {e}")
conn.commit()
if count > 0:
print(f" → 导入 {count}\n")
conn.close()
print("========== 导入完成 ==========")
print("\n请执行: sudo systemctl restart safesightd")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,69 @@
#!/bin/bash
# SafeSight 初始资产导入脚本
# 将 edge 项目 configs 目录下的模板/场景/覆盖层导入管理端数据库
# 用法: sudo bash import-assets.sh
set -e
DB="/opt/safesightd/data/app.db"
CONFIGS_DIR="${1:-/tmp/configs}"
if [ ! -f "$DB" ]; then
echo "错误: 数据库文件不存在: $DB"
exit 1
fi
if [ ! -d "$CONFIGS_DIR/templates" ]; then
echo "错误: configs 目录不存在: $CONFIGS_DIR"
echo "请先把 edge 项目的 configs/templates configs/profiles configs/overlays 复制到 $CONFIGS_DIR"
exit 1
fi
NOW=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
T=0; P=0; O=0
import_json() {
local table="$1" name="$2" file="$3"
# 使用 base64 编码绕过 JSON 中的特殊字符问题
local b64=$(cat "$file" | base64 -w0)
sqlite3 "$DB" "INSERT OR IGNORE INTO ${table} (name, description, body_json, created_at, updated_at)
VALUES ('${name}', '', CAST(X'$(echo -n "$b64" | base64 -d | xxd -p -c0 | tr -d '\n')' AS TEXT), '${NOW}', '${NOW}');"
}
echo "========== SafeSight 资产导入 =========="
echo "数据库: $DB"
echo "配置源: $CONFIGS_DIR"
echo ""
# 导入模板
for f in "$CONFIGS_DIR/templates"/*.json; do
[ -f "$f" ] || continue
name=$(basename "$f" .json)
import_json "templates" "$name" "$f"
echo " ✓ 模板: $name"
((T++))
done
# 导入场景
for f in "$CONFIGS_DIR/profiles"/*.json; do
[ -f "$f" ] || continue
name=$(basename "$f" .json)
import_json "profiles" "$name" "$f"
echo " ✓ 场景: $name"
((P++))
done
# 导入覆盖层
for f in "$CONFIGS_DIR/overlays"/*.json; do
[ -f "$f" ] || continue
name=$(basename "$f" .json)
import_json "overlays" "$name" "$f"
echo " ✓ 覆盖层: $name"
((O++))
done
echo ""
echo "========== 导入完成 =========="
echo "模板: $T 场景: $P 覆盖层: $O"
echo ""
echo "请执行: sudo systemctl restart safesightd"