Files
vmis/backend/app/services/seed.py
VMIS Developer 62baadb06f feat(vmis): 租戶自動開通完整流程 + Admin Portal SSO + NC 行事曆訂閱
Backend:
- schedule_tenant: NC 新容器自動 pgsql 安裝 (_nc_db_check 全新容器處理)
- schedule_tenant: NC 初始化加入 Redis + APCu memcache 設定 (修正 OIDC invalid_state)
- schedule_tenant: 新租戶 KC realm 自動設定 accessCodeLifespan=600s (修正 authentication_expired)
- schedule_account: NC Mail 帳號自動設定 (nc_mail_result/nc_mail_done_at)
- schedule_account: NC 台灣國定假日行事曆自動訂閱 (CalDAV MKCALENDAR)
- nextcloud_client: 新增 subscribe_calendar() CalDAV 訂閱方法
- settings: 新增系統設定 API (site_title/version/timezone/SSO/Keycloak)
- models/result: 新增 nc_mail_result, nc_mail_done_at 欄位
- alembic: 遷移 002(system_settings) 003(keycloak_admin) 004(nc_mail_result)

Frontend (Admin Portal):
- 新增完整管理後台 (index/tenants/accounts/servers/schedules/logs/settings/system-status)
- api.js: Keycloak JS Adapter SSO 整合 (PKCE/S256, fallback KC JS 來源, 自動 token 更新)
- index.html: Promise.allSettled 取代 Promise.all,防止單一 API 失敗影響整頁
- 所有頁面加入 try/catch + toast 錯誤處理
- 新增品牌 LOGO 與 favicon

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 15:31:37 +08:00

61 lines
2.1 KiB
Python

"""Initial data seed: schedules + servers + system settings"""
from datetime import datetime
from app.core.utils import now_tw, configure_timezone
from croniter import croniter
from sqlalchemy.orm import Session
from app.models.schedule import Schedule
from app.models.server import Server
from app.models.settings import SystemSettings
INITIAL_SCHEDULES = [
{"id": 1, "name": "租戶檢查", "cron_timer": "0 */3 * * * *"},
{"id": 2, "name": "帳號檢查", "cron_timer": "0 */3 * * * *"},
{"id": 3, "name": "系統狀態", "cron_timer": "0 0 8 * * *"},
]
INITIAL_SERVERS = [
{"id": 1, "name": "home", "ip_address": "10.1.0.254", "sort_order": 1,
"description": "核心服務主機 (Ubuntu 24.04 / Dell Inspiron 3910)"},
{"id": 2, "name": "小的NAS", "ip_address": "10.1.0.20", "sort_order": 2,
"description": "資料庫主機 (Synology DS716+II / DSM 6.2.4)"},
{"id": 3, "name": "大的NAS", "ip_address": "10.1.0.30", "sort_order": 3,
"description": "儲存主機 (Synology DS920+ / DSM 7.3.2)"},
{"id": 4, "name": "Porsche_KLI", "ip_address": "10.1.0.245", "sort_order": 4,
"description": "開發環境 (ASUS MINIPC PN62 / Windows 11)"},
]
def _calc_next_run(cron_timer: str) -> datetime:
# croniter: six-field cron (sec min hour day month weekday)
cron = croniter(cron_timer, now_tw())
return cron.get_next(datetime)
def seed_initial_data(db: Session) -> None:
"""Insert initial schedules and servers if not present."""
for s in INITIAL_SCHEDULES:
if not db.get(Schedule, s["id"]):
db.add(Schedule(
id=s["id"],
name=s["name"],
cron_timer=s["cron_timer"],
status="Waiting",
next_run_at=_calc_next_run(s["cron_timer"]),
))
for sv in INITIAL_SERVERS:
if not db.get(Server, sv["id"]):
db.add(Server(**sv))
# Seed default system settings (id=1)
if not db.get(SystemSettings, 1):
db.add(SystemSettings(id=1))
db.commit()
# Apply timezone from settings
s = db.get(SystemSettings, 1)
if s:
configure_timezone(s.timezone)