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>
This commit is contained in:
@@ -33,6 +33,7 @@ def _get_lights(db: Session, account_id: int) -> Optional[AccountStatusLight]:
|
||||
sso_result=result.sso_result,
|
||||
mailbox_result=result.mailbox_result,
|
||||
nc_result=result.nc_result,
|
||||
nc_mail_result=result.nc_mail_result,
|
||||
quota_usage=result.quota_usage,
|
||||
)
|
||||
|
||||
@@ -60,6 +61,7 @@ def list_accounts(
|
||||
|
||||
@router.post("", response_model=AccountResponse, status_code=201)
|
||||
def create_account(payload: AccountCreate, db: Session = Depends(get_db)):
|
||||
import secrets
|
||||
tenant = db.get(Tenant, payload.tenant_id)
|
||||
if not tenant:
|
||||
raise HTTPException(status_code=404, detail="Tenant not found")
|
||||
@@ -68,8 +70,12 @@ def create_account(payload: AccountCreate, db: Session = Depends(get_db)):
|
||||
account_code = _build_account_code(tenant.prefix, seq_no)
|
||||
email = f"{payload.sso_account}@{tenant.domain}"
|
||||
|
||||
data = payload.model_dump()
|
||||
if not data.get("default_password"):
|
||||
data["default_password"] = account_code # 預設密碼 = 帳號編碼,使用者首次登入後必須變更
|
||||
|
||||
account = Account(
|
||||
**payload.model_dump(),
|
||||
**data,
|
||||
seq_no=seq_no,
|
||||
account_code=account_code,
|
||||
email=email,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from fastapi import APIRouter
|
||||
from app.api.v1 import tenants, accounts, schedules, servers, status
|
||||
from app.api.v1 import tenants, accounts, schedules, servers, status, settings
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(tenants.router)
|
||||
@@ -7,3 +7,4 @@ api_router.include_router(accounts.router)
|
||||
api_router.include_router(schedules.router)
|
||||
api_router.include_router(servers.router)
|
||||
api_router.include_router(status.router)
|
||||
api_router.include_router(settings.router)
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
from typing import List
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
|
||||
from sqlalchemy.orm import Session
|
||||
from croniter import croniter
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.models.schedule import Schedule
|
||||
from app.schemas.schedule import ScheduleResponse, ScheduleUpdate, ScheduleLogResponse
|
||||
from app.schemas.schedule import (
|
||||
ScheduleResponse, ScheduleUpdate, ScheduleLogResponse,
|
||||
LogResultsResponse, TenantResultItem, AccountResultItem,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/schedules", tags=["schedules"])
|
||||
|
||||
@@ -29,9 +33,9 @@ def update_schedule_cron(schedule_id: int, payload: ScheduleUpdate, db: Session
|
||||
s = db.get(Schedule, schedule_id)
|
||||
if not s:
|
||||
raise HTTPException(status_code=404, detail="Schedule not found")
|
||||
# Validate cron expression
|
||||
# Validate cron expression (5-field: 分 時 日 月 週)
|
||||
try:
|
||||
cron = croniter(payload.cron_timer, datetime.utcnow())
|
||||
cron = croniter(payload.cron_timer, now_tw())
|
||||
next_run = cron.get_next(datetime)
|
||||
except Exception:
|
||||
raise HTTPException(status_code=422, detail="Invalid cron expression")
|
||||
@@ -67,3 +71,59 @@ def get_schedule_logs(schedule_id: int, limit: int = 20, db: Session = Depends(g
|
||||
.all()
|
||||
)
|
||||
return logs
|
||||
|
||||
|
||||
@router.get("/{schedule_id}/logs/{log_id}/results", response_model=LogResultsResponse)
|
||||
def get_log_results(schedule_id: int, log_id: int, db: Session = Depends(get_db)):
|
||||
"""取得某次排程執行的詳細逐項結果"""
|
||||
from app.models.result import TenantScheduleResult, AccountScheduleResult
|
||||
from app.models.tenant import Tenant
|
||||
from app.models.account import Account
|
||||
|
||||
tenant_results = []
|
||||
account_results = []
|
||||
|
||||
if schedule_id == 1:
|
||||
rows = (
|
||||
db.query(TenantScheduleResult)
|
||||
.filter(TenantScheduleResult.schedule_log_id == log_id)
|
||||
.all()
|
||||
)
|
||||
for r in rows:
|
||||
tenant = db.get(Tenant, r.tenant_id)
|
||||
tenant_results.append(TenantResultItem(
|
||||
tenant_id=r.tenant_id,
|
||||
tenant_name=tenant.name if tenant else None,
|
||||
traefik_status=r.traefik_status,
|
||||
sso_result=r.sso_result,
|
||||
mailbox_result=r.mailbox_result,
|
||||
nc_result=r.nc_result,
|
||||
office_result=r.office_result,
|
||||
quota_usage=r.quota_usage,
|
||||
fail_reason=r.fail_reason,
|
||||
))
|
||||
|
||||
elif schedule_id == 2:
|
||||
rows = (
|
||||
db.query(AccountScheduleResult)
|
||||
.filter(AccountScheduleResult.schedule_log_id == log_id)
|
||||
.all()
|
||||
)
|
||||
for r in rows:
|
||||
account_results.append(AccountResultItem(
|
||||
account_id=r.account_id,
|
||||
sso_account=r.sso_account,
|
||||
sso_result=r.sso_result,
|
||||
mailbox_result=r.mailbox_result,
|
||||
nc_result=r.nc_result,
|
||||
nc_mail_result=r.nc_mail_result,
|
||||
quota_usage=r.quota_usage,
|
||||
fail_reason=r.fail_reason,
|
||||
))
|
||||
|
||||
return LogResultsResponse(
|
||||
log_id=log_id,
|
||||
schedule_id=schedule_id,
|
||||
tenant_results=tenant_results,
|
||||
account_results=account_results,
|
||||
)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from typing import List, Optional
|
||||
from datetime import datetime, timedelta
|
||||
from app.core.utils import now_tw
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import func, case
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -12,7 +13,7 @@ router = APIRouter(prefix="/servers", tags=["servers"])
|
||||
|
||||
|
||||
def _calc_availability(db: Session, server_id: int, days: int) -> Optional[float]:
|
||||
since = datetime.utcnow() - timedelta(days=days)
|
||||
since = now_tw() - timedelta(days=days)
|
||||
row = (
|
||||
db.query(
|
||||
func.count().label("total"),
|
||||
|
||||
135
backend/app/api/v1/settings.py
Normal file
135
backend/app/api/v1/settings.py
Normal file
@@ -0,0 +1,135 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.utils import configure_timezone
|
||||
from app.models.settings import SystemSettings
|
||||
from app.models.tenant import Tenant
|
||||
from app.models.account import Account
|
||||
from app.schemas.settings import SettingsUpdate, SettingsResponse
|
||||
from app.services.keycloak_client import KeycloakClient
|
||||
|
||||
router = APIRouter(prefix="/settings", tags=["settings"])
|
||||
|
||||
|
||||
def _get_or_create(db: Session) -> SystemSettings:
|
||||
s = db.query(SystemSettings).first()
|
||||
if not s:
|
||||
s = SystemSettings(id=1)
|
||||
db.add(s)
|
||||
db.commit()
|
||||
db.refresh(s)
|
||||
return s
|
||||
|
||||
|
||||
@router.get("", response_model=SettingsResponse)
|
||||
def get_settings(db: Session = Depends(get_db)):
|
||||
return _get_or_create(db)
|
||||
|
||||
|
||||
@router.put("", response_model=SettingsResponse)
|
||||
def update_settings(payload: SettingsUpdate, db: Session = Depends(get_db)):
|
||||
s = _get_or_create(db)
|
||||
|
||||
# 啟用 SSO 前置條件檢查
|
||||
if payload.sso_enabled is True:
|
||||
manager = (
|
||||
db.query(Tenant)
|
||||
.filter(Tenant.is_manager == True, Tenant.is_active == True)
|
||||
.first()
|
||||
)
|
||||
if not manager:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail="啟用 SSO 前必須先建立 is_manager=true 的管理租戶"
|
||||
)
|
||||
has_account = (
|
||||
db.query(Account)
|
||||
.filter(Account.tenant_id == manager.id, Account.is_active == True)
|
||||
.first()
|
||||
)
|
||||
if not has_account:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail="管理租戶必須至少有一個有效帳號才能啟用 SSO"
|
||||
)
|
||||
|
||||
for field, value in payload.model_dump(exclude_none=True).items():
|
||||
setattr(s, field, value)
|
||||
db.commit()
|
||||
db.refresh(s)
|
||||
configure_timezone(s.timezone)
|
||||
return s
|
||||
|
||||
|
||||
@router.post("/test-keycloak")
|
||||
def test_keycloak(db: Session = Depends(get_db)):
|
||||
"""測試 Keycloak master realm 管理帳密是否正確"""
|
||||
s = _get_or_create(db)
|
||||
if not s.keycloak_url or not s.keycloak_admin_user or not s.keycloak_admin_pass:
|
||||
raise HTTPException(status_code=400, detail="請先設定 Keycloak URL 及管理帳密")
|
||||
|
||||
kc = KeycloakClient(s.keycloak_url, s.keycloak_admin_user, s.keycloak_admin_pass)
|
||||
try:
|
||||
token = kc._get_admin_token()
|
||||
if token:
|
||||
return {"ok": True, "message": f"連線成功:{s.keycloak_url}"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Keycloak 連線失敗:{str(e)}")
|
||||
|
||||
|
||||
@router.post("/init-sso-realm")
|
||||
def init_sso_realm(db: Session = Depends(get_db)):
|
||||
"""
|
||||
建立 Admin Portal SSO 環境:
|
||||
1. 以管理租戶的 keycloak_realm 為準(無管理租戶時 fallback 至 system settings)
|
||||
2. 確認該 realm 存在(不存在則建立)
|
||||
3. 在該 realm 建立 vmis-portal Public Client
|
||||
4. 同步回寫 system_settings.keycloak_realm(前端 JS Adapter 使用)
|
||||
"""
|
||||
s = _get_or_create(db)
|
||||
if not s.keycloak_url or not s.keycloak_admin_user or not s.keycloak_admin_pass:
|
||||
raise HTTPException(status_code=400, detail="請先設定並儲存 Keycloak 連線資訊")
|
||||
|
||||
# 以管理租戶的 keycloak_realm 為主要來源
|
||||
manager = (
|
||||
db.query(Tenant)
|
||||
.filter(Tenant.is_manager == True, Tenant.is_active == True)
|
||||
.first()
|
||||
)
|
||||
if manager and manager.keycloak_realm:
|
||||
realm = manager.keycloak_realm
|
||||
else:
|
||||
realm = s.keycloak_realm or "vmis-admin"
|
||||
|
||||
client_id = s.keycloak_client or "vmis-portal"
|
||||
|
||||
kc = KeycloakClient(s.keycloak_url, s.keycloak_admin_user, s.keycloak_admin_pass)
|
||||
results = []
|
||||
|
||||
# 確認/建立 realm
|
||||
if kc.realm_exists(realm):
|
||||
results.append(f"✓ Realm '{realm}' 已存在")
|
||||
else:
|
||||
ok = kc.create_realm(realm, manager.name if manager else "VMIS Admin Portal")
|
||||
if ok:
|
||||
results.append(f"✓ Realm '{realm}' 建立成功")
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail=f"Realm '{realm}' 建立失敗")
|
||||
|
||||
# 建立 vmis-portal Public Client
|
||||
status = kc.create_public_client(realm, client_id)
|
||||
if status == "exists":
|
||||
results.append(f"✓ Client '{client_id}' 已存在")
|
||||
elif status == "created":
|
||||
results.append(f"✓ Client '{client_id}' 建立成功")
|
||||
else:
|
||||
results.append(f"✗ Client '{client_id}' 建立失敗")
|
||||
|
||||
# 同步回寫 system_settings.keycloak_realm(前端 Keycloak JS Adapter 使用)
|
||||
if s.keycloak_realm != realm:
|
||||
s.keycloak_realm = realm
|
||||
db.commit()
|
||||
results.append(f"✓ 系統設定 keycloak_realm 同步為 '{realm}'")
|
||||
|
||||
return {"ok": True, "realm": realm, "details": results}
|
||||
@@ -17,6 +17,11 @@ class Settings(BaseSettings):
|
||||
DOCKER_SSH_USER: str = "porsche"
|
||||
TENANT_DEPLOY_BASE: str = "/home/porsche/tenants"
|
||||
|
||||
NC_ADMIN_USER: str = "admin"
|
||||
NC_ADMIN_PASSWORD: str = "NC1qaz2wsx"
|
||||
|
||||
OO_JWT_SECRET: str = "OnlyOffice2026Secret"
|
||||
|
||||
APP_ENV: str = "development"
|
||||
APP_PORT: int = 10281
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ engine = create_engine(
|
||||
pool_pre_ping=True,
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
connect_args={"options": "-c timezone=Asia/Taipei"},
|
||||
)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
18
backend/app/core/utils.py
Normal file
18
backend/app/core/utils.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
_tz = ZoneInfo("Asia/Taipei")
|
||||
|
||||
|
||||
def configure_timezone(tz_name: str) -> None:
|
||||
"""Update the application timezone. Called on startup and when settings change."""
|
||||
global _tz
|
||||
try:
|
||||
_tz = ZoneInfo(tz_name)
|
||||
except Exception:
|
||||
_tz = ZoneInfo("Asia/Taipei")
|
||||
|
||||
|
||||
def now_tw() -> datetime:
|
||||
"""Return current time in the configured timezone as a naive datetime."""
|
||||
return datetime.now(tz=_tz).replace(tzinfo=None)
|
||||
@@ -3,9 +3,11 @@ from app.models.account import Account
|
||||
from app.models.schedule import Schedule, ScheduleLog
|
||||
from app.models.result import TenantScheduleResult, AccountScheduleResult
|
||||
from app.models.server import Server, ServerStatusLog, SystemStatusLog
|
||||
from app.models.settings import SystemSettings
|
||||
|
||||
__all__ = [
|
||||
"Tenant", "Account", "Schedule", "ScheduleLog",
|
||||
"TenantScheduleResult", "AccountScheduleResult",
|
||||
"Server", "ServerStatusLog", "SystemStatusLog",
|
||||
"SystemSettings",
|
||||
]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy import Boolean, Column, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.core.database import Base
|
||||
@@ -21,8 +22,8 @@ class Account(Base):
|
||||
default_password = Column(String(200))
|
||||
seq_no = Column(Integer, nullable=False) # 同租戶內流水號
|
||||
|
||||
created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
created_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
updated_at = Column(DateTime, nullable=False, default=now_tw, onupdate=now_tw)
|
||||
|
||||
tenant = relationship("Tenant", back_populates="accounts")
|
||||
schedule_results = relationship("AccountScheduleResult", back_populates="account")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Text, DateTime, Float, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.core.database import Base
|
||||
@@ -28,7 +29,7 @@ class TenantScheduleResult(Base):
|
||||
|
||||
fail_reason = Column(Text)
|
||||
quota_usage = Column(Float) # GB
|
||||
recorded_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
recorded_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
|
||||
schedule_log = relationship("ScheduleLog", back_populates="tenant_results")
|
||||
tenant = relationship("Tenant", back_populates="schedule_results")
|
||||
@@ -52,9 +53,12 @@ class AccountScheduleResult(Base):
|
||||
nc_result = Column(Boolean)
|
||||
nc_done_at = Column(DateTime)
|
||||
|
||||
nc_mail_result = Column(Boolean)
|
||||
nc_mail_done_at = Column(DateTime)
|
||||
|
||||
fail_reason = Column(Text)
|
||||
quota_usage = Column(Float) # GB
|
||||
recorded_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
recorded_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
|
||||
schedule_log = relationship("ScheduleLog", back_populates="account_results")
|
||||
account = relationship("Account", back_populates="schedule_results")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.core.database import Base
|
||||
@@ -14,7 +15,7 @@ class Schedule(Base):
|
||||
last_run_at = Column(DateTime)
|
||||
next_run_at = Column(DateTime)
|
||||
last_status = Column(String(10)) # ok / error
|
||||
recorded_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
recorded_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
|
||||
logs = relationship("ScheduleLog", back_populates="schedule")
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Text, DateTime, Float, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.core.database import Base
|
||||
@@ -13,7 +14,7 @@ class Server(Base):
|
||||
description = Column(String(200))
|
||||
sort_order = Column(Integer, nullable=False, default=0)
|
||||
is_active = Column(Boolean, nullable=False, default=True)
|
||||
recorded_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
recorded_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
|
||||
status_logs = relationship("ServerStatusLog", back_populates="server")
|
||||
|
||||
@@ -27,7 +28,7 @@ class ServerStatusLog(Base):
|
||||
result = Column(Boolean, nullable=False)
|
||||
response_time = Column(Float) # ms
|
||||
fail_reason = Column(Text)
|
||||
recorded_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
recorded_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
|
||||
schedule_log = relationship("ScheduleLog", back_populates="server_status_logs")
|
||||
server = relationship("Server", back_populates="status_logs")
|
||||
@@ -43,6 +44,6 @@ class SystemStatusLog(Base):
|
||||
service_desc = Column(String(100))
|
||||
result = Column(Boolean, nullable=False)
|
||||
fail_reason = Column(Text)
|
||||
recorded_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
recorded_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
|
||||
schedule_log = relationship("ScheduleLog", back_populates="system_status_logs")
|
||||
|
||||
22
backend/app/models/settings.py
Normal file
22
backend/app/models/settings.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, Integer, String, Boolean, DateTime
|
||||
from app.core.database import Base
|
||||
from app.core.utils import now_tw
|
||||
|
||||
|
||||
class SystemSettings(Base):
|
||||
__tablename__ = "system_settings"
|
||||
|
||||
id = Column(Integer, primary_key=True, default=1)
|
||||
site_title = Column(String(200), nullable=False, default="VMIS Admin Portal")
|
||||
version = Column(String(50), nullable=False, default="2.0.0")
|
||||
timezone = Column(String(100), nullable=False, default="Asia/Taipei")
|
||||
sso_enabled = Column(Boolean, nullable=False, default=False)
|
||||
# Keycloak — master realm admin (for tenant realm management)
|
||||
keycloak_url = Column(String(200), nullable=False, default="https://auth.lab.taipei")
|
||||
keycloak_admin_user = Column(String(100), nullable=False, default="admin")
|
||||
keycloak_admin_pass = Column(String(200), nullable=False, default="")
|
||||
# Keycloak — Admin Portal SSO
|
||||
keycloak_realm = Column(String(100), nullable=False, default="vmis-admin")
|
||||
keycloak_client = Column(String(100), nullable=False, default="vmis-portal")
|
||||
updated_at = Column(DateTime, nullable=False, default=now_tw, onupdate=now_tw)
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Text, DateTime, Date
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.core.database import Base
|
||||
@@ -30,8 +31,8 @@ class Tenant(Base):
|
||||
is_active = Column(Boolean, nullable=False, default=True)
|
||||
status = Column(String(20), nullable=False, default="trial") # trial / active / inactive
|
||||
note = Column(Text)
|
||||
created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
created_at = Column(DateTime, nullable=False, default=now_tw)
|
||||
updated_at = Column(DateTime, nullable=False, default=now_tw, onupdate=now_tw)
|
||||
|
||||
accounts = relationship("Account", back_populates="tenant", cascade="all, delete-orphan")
|
||||
schedule_results = relationship("TenantScheduleResult", back_populates="tenant")
|
||||
|
||||
@@ -32,6 +32,7 @@ class AccountStatusLight(BaseModel):
|
||||
sso_result: Optional[bool] = None
|
||||
mailbox_result: Optional[bool] = None
|
||||
nc_result: Optional[bool] = None
|
||||
nc_mail_result: Optional[bool] = None
|
||||
quota_usage: Optional[float] = None
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
@@ -31,3 +31,39 @@ class ScheduleLogResponse(BaseModel):
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class TenantResultItem(BaseModel):
|
||||
tenant_id: int
|
||||
tenant_name: Optional[str] = None
|
||||
traefik_status: Optional[bool] = None
|
||||
sso_result: Optional[bool] = None
|
||||
mailbox_result: Optional[bool] = None
|
||||
nc_result: Optional[bool] = None
|
||||
office_result: Optional[bool] = None
|
||||
quota_usage: Optional[float] = None
|
||||
fail_reason: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class AccountResultItem(BaseModel):
|
||||
account_id: int
|
||||
sso_account: Optional[str] = None
|
||||
sso_result: Optional[bool] = None
|
||||
mailbox_result: Optional[bool] = None
|
||||
nc_result: Optional[bool] = None
|
||||
nc_mail_result: Optional[bool] = None
|
||||
quota_usage: Optional[float] = None
|
||||
fail_reason: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class LogResultsResponse(BaseModel):
|
||||
log_id: int
|
||||
schedule_id: int
|
||||
tenant_results: List[TenantResultItem] = []
|
||||
account_results: List[AccountResultItem] = []
|
||||
|
||||
31
backend/app/schemas/settings.py
Normal file
31
backend/app/schemas/settings.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class SettingsUpdate(BaseModel):
|
||||
site_title: Optional[str] = None
|
||||
version: Optional[str] = None
|
||||
timezone: Optional[str] = None
|
||||
sso_enabled: Optional[bool] = None
|
||||
keycloak_url: Optional[str] = None
|
||||
keycloak_admin_user: Optional[str] = None
|
||||
keycloak_admin_pass: Optional[str] = None
|
||||
keycloak_realm: Optional[str] = None
|
||||
keycloak_client: Optional[str] = None
|
||||
|
||||
|
||||
class SettingsResponse(BaseModel):
|
||||
id: int
|
||||
site_title: str
|
||||
version: str
|
||||
timezone: str
|
||||
sso_enabled: bool
|
||||
keycloak_url: str
|
||||
keycloak_admin_user: str
|
||||
keycloak_admin_pass: str
|
||||
keycloak_realm: str
|
||||
keycloak_client: str
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
DockerClient — docker-py (本機 Docker socket) + paramiko SSH (遠端 docker compose)
|
||||
管理租戶的 NC / OO 容器。
|
||||
DockerClient — paramiko SSH (遠端 docker / traefik 查詢)
|
||||
所有容器都在 10.1.0.254,透過 SSH 操作。
|
||||
3-state 回傳: None=未設定(灰), True=正常(綠), False=異常(紅)
|
||||
"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
@@ -12,70 +13,74 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DockerClient:
|
||||
def __init__(self):
|
||||
self._docker = None
|
||||
|
||||
def _get_docker(self):
|
||||
if self._docker is None:
|
||||
import docker
|
||||
self._docker = docker.from_env()
|
||||
return self._docker
|
||||
def _ssh(self):
|
||||
"""建立 SSH 連線到 10.1.0.254"""
|
||||
import paramiko
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(
|
||||
settings.DOCKER_SSH_HOST,
|
||||
username=settings.DOCKER_SSH_USER,
|
||||
timeout=15,
|
||||
)
|
||||
return client
|
||||
|
||||
def check_traefik_route(self, domain: str) -> bool:
|
||||
def check_traefik_domain(self, domain: str) -> Optional[bool]:
|
||||
"""
|
||||
Traefik API: GET http://localhost:8080/api/http/routers
|
||||
驗證 routers 中包含 domain,且 routers 數量 > 0
|
||||
None = domain 在 Traefik 沒有路由設定(灰)
|
||||
True = 路由存在且服務存活(綠)
|
||||
False = 路由存在但服務不通(紅)
|
||||
"""
|
||||
try:
|
||||
resp = httpx.get("http://localhost:8080/api/overview", timeout=5.0)
|
||||
resp = httpx.get(f"http://{settings.DOCKER_SSH_HOST}:8080/api/http/routers", timeout=5.0)
|
||||
if resp.status_code != 200:
|
||||
return False
|
||||
data = resp.json()
|
||||
# Verify actual routes exist (functional check)
|
||||
http_count = data.get("http", {}).get("routers", {}).get("total", 0)
|
||||
if http_count == 0:
|
||||
routers = resp.json()
|
||||
route_found = any(domain in str(r.get("rule", "")) for r in routers)
|
||||
if not route_found:
|
||||
return None
|
||||
# Route exists — probe the service
|
||||
try:
|
||||
probe = httpx.get(
|
||||
f"https://{domain}",
|
||||
timeout=5.0,
|
||||
follow_redirects=True,
|
||||
)
|
||||
return probe.status_code < 500
|
||||
except Exception:
|
||||
return False
|
||||
# Check domain-specific router
|
||||
routers_resp = httpx.get("http://localhost:8080/api/http/routers", timeout=5.0)
|
||||
if routers_resp.status_code != 200:
|
||||
return False
|
||||
routers = routers_resp.json()
|
||||
return any(domain in str(r.get("rule", "")) for r in routers)
|
||||
except Exception as e:
|
||||
logger.warning(f"Traefik check failed for {domain}: {e}")
|
||||
return False
|
||||
|
||||
def ensure_container_running(self, container_name: str, tenant_code: str, realm: str) -> bool:
|
||||
"""Check container status; start if exited; deploy via SSH if not found."""
|
||||
def check_container_ssh(self, container_name: str) -> Optional[bool]:
|
||||
"""
|
||||
SSH 到 10.1.0.254 查詢容器狀態。
|
||||
None = 容器不存在(未部署)
|
||||
True = 容器正在執行
|
||||
False = 容器存在但未執行(exited/paused)
|
||||
"""
|
||||
try:
|
||||
docker_client = self._get_docker()
|
||||
container = docker_client.containers.get(container_name)
|
||||
if container.status == "running":
|
||||
return True
|
||||
elif container.status == "exited":
|
||||
container.start()
|
||||
container.reload()
|
||||
return container.status == "running"
|
||||
except Exception as e:
|
||||
if "Not Found" in str(e) or "404" in str(e):
|
||||
return self._ssh_compose_up(tenant_code, realm)
|
||||
logger.error(f"Docker check failed for {container_name}: {e}")
|
||||
return False
|
||||
return False
|
||||
|
||||
def _ssh_compose_up(self, tenant_code: str, realm: str) -> bool:
|
||||
"""SSH into 10.1.0.254 and run docker compose up -d"""
|
||||
try:
|
||||
import paramiko
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(
|
||||
settings.DOCKER_SSH_HOST,
|
||||
username=settings.DOCKER_SSH_USER,
|
||||
timeout=15,
|
||||
client = self._ssh()
|
||||
_, stdout, _ = client.exec_command(
|
||||
f"docker inspect --format='{{{{.State.Status}}}}' {container_name} 2>/dev/null"
|
||||
)
|
||||
output = stdout.read().decode().strip()
|
||||
client.close()
|
||||
if not output:
|
||||
return None
|
||||
return output == "running"
|
||||
except Exception as e:
|
||||
logger.error(f"SSH container check failed for {container_name}: {e}")
|
||||
return False
|
||||
|
||||
def ssh_compose_up(self, tenant_code: str) -> bool:
|
||||
"""SSH 到 10.1.0.254 執行 docker compose up -d"""
|
||||
try:
|
||||
client = self._ssh()
|
||||
deploy_dir = f"{settings.TENANT_DEPLOY_BASE}/{tenant_code}"
|
||||
stdin, stdout, stderr = client.exec_command(
|
||||
_, stdout, _ = client.exec_command(
|
||||
f"cd {deploy_dir} && docker compose up -d 2>&1"
|
||||
)
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
@@ -84,3 +89,19 @@ class DockerClient:
|
||||
except Exception as e:
|
||||
logger.error(f"SSH compose up failed for {tenant_code}: {e}")
|
||||
return False
|
||||
|
||||
def get_oo_disk_usage_gb(self, container_name: str) -> Optional[float]:
|
||||
"""取得 OO 容器磁碟使用量(GB),容器不存在回傳 None"""
|
||||
try:
|
||||
client = self._ssh()
|
||||
_, stdout, _ = client.exec_command(
|
||||
f"docker exec {container_name} df -B1 / 2>/dev/null | awk 'NR==2 {{print $3}}'"
|
||||
)
|
||||
output = stdout.read().decode().strip()
|
||||
client.close()
|
||||
if output.isdigit():
|
||||
return round(int(output) / (1024 ** 3), 3)
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.warning(f"OO disk usage check failed for {container_name}: {e}")
|
||||
return None
|
||||
|
||||
@@ -1,34 +1,36 @@
|
||||
"""
|
||||
KeycloakClient — 直接呼叫 Keycloak REST API,不使用 python-keycloak 套件。
|
||||
管理租戶 realm 及帳號的建立/查詢。
|
||||
使用 master realm admin 帳密取得管理 token,管理租戶 realm 及帳號。
|
||||
"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
import httpx
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TIMEOUT = 10.0
|
||||
|
||||
|
||||
class KeycloakClient:
|
||||
def __init__(self):
|
||||
self._base = settings.KEYCLOAK_URL.rstrip("/")
|
||||
def __init__(self, base_url: str, admin_user: str, admin_pass: str):
|
||||
self._base = base_url.rstrip("/")
|
||||
self._admin_user = admin_user
|
||||
self._admin_pass = admin_pass
|
||||
self._admin_token: Optional[str] = None
|
||||
|
||||
def _get_admin_token(self) -> str:
|
||||
"""取得 vmis-admin realm 的 admin access token"""
|
||||
url = f"{self._base}/realms/{settings.KEYCLOAK_ADMIN_REALM}/protocol/openid-connect/token"
|
||||
"""取得 master realm 的 admin access token(Resource Owner Password)"""
|
||||
url = f"{self._base}/realms/master/protocol/openid-connect/token"
|
||||
resp = httpx.post(
|
||||
url,
|
||||
data={
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": settings.KEYCLOAK_ADMIN_CLIENT_ID,
|
||||
"client_secret": settings.KEYCLOAK_ADMIN_CLIENT_SECRET,
|
||||
"grant_type": "password",
|
||||
"client_id": "admin-cli",
|
||||
"username": self._admin_user,
|
||||
"password": self._admin_pass,
|
||||
},
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()["access_token"]
|
||||
@@ -43,7 +45,12 @@ class KeycloakClient:
|
||||
|
||||
def realm_exists(self, realm: str) -> bool:
|
||||
try:
|
||||
resp = httpx.get(self._admin_url(realm), headers=self._headers(), timeout=TIMEOUT)
|
||||
resp = httpx.get(
|
||||
self._admin_url(realm),
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
return resp.status_code == 200
|
||||
except Exception:
|
||||
return False
|
||||
@@ -60,21 +67,58 @@ class KeycloakClient:
|
||||
json=payload,
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
return resp.status_code in (201, 204)
|
||||
|
||||
def update_realm_token_settings(self, realm: str, access_code_lifespan: int = 600) -> bool:
|
||||
"""設定 realm 的授權碼有效期(秒),預設 10 分鐘"""
|
||||
resp = httpx.put(
|
||||
self._admin_url(realm),
|
||||
json={
|
||||
"accessCodeLifespan": access_code_lifespan,
|
||||
"actionTokenGeneratedByUserLifespan": access_code_lifespan,
|
||||
},
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
return resp.status_code in (200, 204)
|
||||
|
||||
def send_welcome_email(self, realm: str, user_id: str) -> bool:
|
||||
"""寄送歡迎信(含設定密碼連結)給新使用者"""
|
||||
try:
|
||||
resp = httpx.put(
|
||||
self._admin_url(f"{realm}/users/{user_id}/execute-actions-email"),
|
||||
json=["UPDATE_PASSWORD"],
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
return resp.status_code in (200, 204)
|
||||
except Exception as e:
|
||||
logger.error(f"KC send_welcome_email({realm}/{user_id}) failed: {e}")
|
||||
return False
|
||||
|
||||
def get_user_uuid(self, realm: str, username: str) -> Optional[str]:
|
||||
resp = httpx.get(
|
||||
self._admin_url(f"{realm}/users"),
|
||||
params={"username": username, "exact": "true"},
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
users = resp.json()
|
||||
return users[0]["id"] if users else None
|
||||
|
||||
def create_user(self, realm: str, username: str, email: str, password: Optional[str]) -> Optional[str]:
|
||||
def create_user(
|
||||
self,
|
||||
realm: str,
|
||||
username: str,
|
||||
email: str,
|
||||
password: Optional[str],
|
||||
) -> Optional[str]:
|
||||
payload = {
|
||||
"username": username,
|
||||
"email": email,
|
||||
@@ -82,14 +126,121 @@ class KeycloakClient:
|
||||
"emailVerified": True,
|
||||
}
|
||||
if password:
|
||||
payload["credentials"] = [{"type": "password", "value": password, "temporary": True}]
|
||||
payload["credentials"] = [
|
||||
{"type": "password", "value": password, "temporary": True}
|
||||
]
|
||||
resp = httpx.post(
|
||||
self._admin_url(f"{realm}/users"),
|
||||
json=payload,
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
if resp.status_code == 201:
|
||||
location = resp.headers.get("Location", "")
|
||||
return location.rstrip("/").split("/")[-1]
|
||||
return None
|
||||
|
||||
def create_public_client(self, realm: str, client_id: str) -> str:
|
||||
"""建立 Public Client。回傳 'exists' / 'created' / 'failed'"""
|
||||
resp = httpx.get(
|
||||
self._admin_url(f"{realm}/clients"),
|
||||
params={"clientId": client_id},
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
if resp.json():
|
||||
return "exists"
|
||||
payload = {
|
||||
"clientId": client_id,
|
||||
"name": client_id,
|
||||
"enabled": True,
|
||||
"publicClient": True,
|
||||
"standardFlowEnabled": True,
|
||||
"directAccessGrantsEnabled": False,
|
||||
"redirectUris": ["*"],
|
||||
"webOrigins": ["*"],
|
||||
}
|
||||
resp = httpx.post(
|
||||
self._admin_url(f"{realm}/clients"),
|
||||
json=payload,
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
return "created" if resp.status_code in (201, 204) else "failed"
|
||||
|
||||
def create_confidential_client(self, realm: str, client_id: str, redirect_uris: list[str]) -> str:
|
||||
"""建立 Confidential Client(用於 NC OIDC)。回傳 'exists' / 'created' / 'failed'"""
|
||||
resp = httpx.get(
|
||||
self._admin_url(f"{realm}/clients"),
|
||||
params={"clientId": client_id},
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
if resp.json():
|
||||
return "exists"
|
||||
origin = redirect_uris[0].rstrip("/*") if redirect_uris else "*"
|
||||
payload = {
|
||||
"clientId": client_id,
|
||||
"name": client_id,
|
||||
"enabled": True,
|
||||
"publicClient": False,
|
||||
"standardFlowEnabled": True,
|
||||
"directAccessGrantsEnabled": False,
|
||||
"redirectUris": redirect_uris,
|
||||
"webOrigins": [origin],
|
||||
"protocol": "openid-connect",
|
||||
}
|
||||
resp = httpx.post(
|
||||
self._admin_url(f"{realm}/clients"),
|
||||
json=payload,
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
return "created" if resp.status_code in (201, 204) else "failed"
|
||||
|
||||
def get_client_secret(self, realm: str, client_id: str) -> Optional[str]:
|
||||
"""取得 Confidential Client 的 Secret"""
|
||||
resp = httpx.get(
|
||||
self._admin_url(f"{realm}/clients"),
|
||||
params={"clientId": client_id},
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
clients = resp.json()
|
||||
if not clients:
|
||||
return None
|
||||
client_uuid = clients[0]["id"]
|
||||
resp = httpx.get(
|
||||
self._admin_url(f"{realm}/clients/{client_uuid}/client-secret"),
|
||||
headers=self._headers(),
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
return resp.json().get("value")
|
||||
return None
|
||||
|
||||
|
||||
def get_keycloak_client() -> KeycloakClient:
|
||||
"""Factory: reads credentials from system settings DB."""
|
||||
from app.core.database import SessionLocal
|
||||
from app.models.settings import SystemSettings
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
s = db.query(SystemSettings).first()
|
||||
if s:
|
||||
return KeycloakClient(s.keycloak_url, s.keycloak_admin_user, s.keycloak_admin_pass)
|
||||
finally:
|
||||
db.close()
|
||||
# Fallback to defaults
|
||||
return KeycloakClient("https://auth.lab.taipei", "admin", "")
|
||||
|
||||
@@ -1,24 +1,36 @@
|
||||
"""
|
||||
MailClient — 呼叫 Docker Mailserver Admin API (http://10.1.0.254:8080)
|
||||
管理 mail domain 和 mailbox 的建立/查詢。
|
||||
建立 domain 前必須驗證 MX DNS 設定(對 active 租戶)。
|
||||
MailClient — 透過 SSH + docker exec mailserver 管理 docker-mailserver。
|
||||
domain 在 docker-mailserver 中是隱式的(由 mailbox 決定),
|
||||
所以 domain_exists 檢查是否有任何 @domain 的 mailbox。
|
||||
"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
import httpx
|
||||
import dns.resolver
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TIMEOUT = 10.0
|
||||
MAILSERVER_CONTAINER = "mailserver"
|
||||
|
||||
|
||||
class MailClient:
|
||||
def __init__(self):
|
||||
self._base = settings.MAIL_ADMIN_API_URL.rstrip("/")
|
||||
self._headers = {"X-API-Key": settings.MAIL_ADMIN_API_KEY}
|
||||
|
||||
def _ssh_exec(self, cmd: str) -> tuple[int, str]:
|
||||
"""SSH 到 10.1.0.254 執行指令,回傳 (exit_code, stdout)"""
|
||||
import paramiko
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(
|
||||
settings.DOCKER_SSH_HOST,
|
||||
username=settings.DOCKER_SSH_USER,
|
||||
timeout=15,
|
||||
)
|
||||
_, stdout, _ = client.exec_command(cmd)
|
||||
output = stdout.read().decode().strip()
|
||||
exit_code = stdout.channel.recv_exit_status()
|
||||
client.close()
|
||||
return exit_code, output
|
||||
|
||||
def check_mx_dns(self, domain: str) -> bool:
|
||||
"""驗證 domain 的 MX record 是否指向正確的 mail server"""
|
||||
@@ -33,49 +45,65 @@ class MailClient:
|
||||
return False
|
||||
|
||||
def domain_exists(self, domain: str) -> bool:
|
||||
"""檢查 mailserver 是否有任何 @domain 的 mailbox(docker-mailserver 的 domain 由 mailbox 決定)"""
|
||||
try:
|
||||
resp = httpx.get(
|
||||
f"{self._base}/api/v1/domains/{domain}",
|
||||
headers=self._headers,
|
||||
timeout=TIMEOUT,
|
||||
code, output = self._ssh_exec(
|
||||
f"docker exec {MAILSERVER_CONTAINER} setup email list 2>/dev/null | grep -q '@{domain}' && echo yes || echo no"
|
||||
)
|
||||
return resp.status_code == 200
|
||||
except Exception:
|
||||
return output.strip() == "yes"
|
||||
except Exception as e:
|
||||
logger.warning(f"domain_exists({domain}) SSH failed: {e}")
|
||||
return False
|
||||
|
||||
def create_domain(self, domain: str) -> bool:
|
||||
"""
|
||||
docker-mailserver 不需要顯式建立 domain(mailbox 新增時自動處理)。
|
||||
新增一個 postmaster@ 系統帳號來確保 domain 被識別。
|
||||
"""
|
||||
try:
|
||||
resp = httpx.post(
|
||||
f"{self._base}/api/v1/domains",
|
||||
json={"domain": domain},
|
||||
headers=self._headers,
|
||||
timeout=TIMEOUT,
|
||||
import secrets
|
||||
passwd = secrets.token_urlsafe(16)
|
||||
code, output = self._ssh_exec(
|
||||
f"docker exec {MAILSERVER_CONTAINER} setup email add postmaster@{domain} {passwd} 2>&1"
|
||||
)
|
||||
return resp.status_code in (200, 201, 204)
|
||||
if code == 0:
|
||||
logger.info(f"create_domain({domain}): postmaster account created")
|
||||
return True
|
||||
# 若帳號已存在視為成功
|
||||
if "already exists" in output.lower():
|
||||
return True
|
||||
logger.error(f"create_domain({domain}) failed (exit {code}): {output}")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"create_domain({domain}) failed: {e}")
|
||||
logger.error(f"create_domain({domain}) SSH failed: {e}")
|
||||
return False
|
||||
|
||||
def mailbox_exists(self, email: str) -> bool:
|
||||
try:
|
||||
resp = httpx.get(
|
||||
f"{self._base}/api/v1/mailboxes/{email}",
|
||||
headers=self._headers,
|
||||
timeout=TIMEOUT,
|
||||
code, output = self._ssh_exec(
|
||||
f"docker exec {MAILSERVER_CONTAINER} setup email list 2>/dev/null | grep -q '{email}' && echo yes || echo no"
|
||||
)
|
||||
return resp.status_code == 200
|
||||
except Exception:
|
||||
return output.strip() == "yes"
|
||||
except Exception as e:
|
||||
logger.warning(f"mailbox_exists({email}) SSH failed: {e}")
|
||||
return False
|
||||
|
||||
def create_mailbox(self, email: str, password: Optional[str], quota_gb: int = 20) -> bool:
|
||||
try:
|
||||
resp = httpx.post(
|
||||
f"{self._base}/api/v1/mailboxes",
|
||||
json={"email": email, "password": password or "", "quota": quota_gb},
|
||||
headers=self._headers,
|
||||
timeout=TIMEOUT,
|
||||
import secrets
|
||||
passwd = password or secrets.token_urlsafe(16)
|
||||
quota_mb = quota_gb * 1024
|
||||
code, output = self._ssh_exec(
|
||||
f"docker exec {MAILSERVER_CONTAINER} setup email add {email} {passwd} 2>&1"
|
||||
)
|
||||
return resp.status_code in (200, 201, 204)
|
||||
if code != 0 and "already exists" not in output.lower():
|
||||
logger.error(f"create_mailbox({email}) failed (exit {code}): {output}")
|
||||
return False
|
||||
# 設定配額
|
||||
self._ssh_exec(
|
||||
f"docker exec {MAILSERVER_CONTAINER} setup quota set {email} {quota_mb}M 2>/dev/null"
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"create_mailbox({email}) failed: {e}")
|
||||
logger.error(f"create_mailbox({email}) SSH failed: {e}")
|
||||
return False
|
||||
|
||||
@@ -47,6 +47,20 @@ class NextcloudClient:
|
||||
logger.error(f"NC create_user({username}) failed: {e}")
|
||||
return False
|
||||
|
||||
def set_user_quota(self, username: str, quota_gb: int) -> bool:
|
||||
try:
|
||||
resp = httpx.put(
|
||||
f"{self._base}/ocs/v1.php/cloud/users/{username}",
|
||||
auth=self._auth,
|
||||
headers=OCS_HEADERS,
|
||||
data={"key": "quota", "value": f"{quota_gb}GB"},
|
||||
timeout=TIMEOUT,
|
||||
)
|
||||
return resp.status_code == 200
|
||||
except Exception as e:
|
||||
logger.error(f"NC set_user_quota({username}) failed: {e}")
|
||||
return False
|
||||
|
||||
def get_user_quota_used_gb(self, username: str) -> Optional[float]:
|
||||
try:
|
||||
resp = httpx.get(
|
||||
@@ -57,11 +71,57 @@ class NextcloudClient:
|
||||
)
|
||||
if resp.status_code != 200:
|
||||
return None
|
||||
used_bytes = resp.json().get("ocs", {}).get("data", {}).get("quota", {}).get("used", 0)
|
||||
quota = resp.json().get("ocs", {}).get("data", {}).get("quota", {})
|
||||
if not isinstance(quota, dict):
|
||||
return 0.0
|
||||
used_bytes = quota.get("used", 0) or 0
|
||||
return round(used_bytes / 1073741824, 4)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def subscribe_calendar(
|
||||
self,
|
||||
username: str,
|
||||
cal_slug: str,
|
||||
display_name: str,
|
||||
ics_url: str,
|
||||
color: str = "#e9322d",
|
||||
) -> bool:
|
||||
"""
|
||||
為使用者建立訂閱行事曆(CalDAV MKCALENDAR with calendarserver source)。
|
||||
已存在(405)視為成功;201=新建成功。
|
||||
"""
|
||||
try:
|
||||
body = (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
||||
'<x1:mkcalendar xmlns:x1="urn:ietf:params:xml:ns:caldav">\n'
|
||||
' <x0:set xmlns:x0="DAV:">\n'
|
||||
' <x0:prop>\n'
|
||||
f' <x0:displayname>{display_name}</x0:displayname>\n'
|
||||
f' <x2:calendar-color xmlns:x2="http://apple.com/ns/ical/">{color}</x2:calendar-color>\n'
|
||||
' <x3:source xmlns:x3="http://calendarserver.org/ns/">\n'
|
||||
f' <x0:href>{ics_url}</x0:href>\n'
|
||||
' </x3:source>\n'
|
||||
' </x0:prop>\n'
|
||||
' </x0:set>\n'
|
||||
'</x1:mkcalendar>'
|
||||
)
|
||||
url = f"{self._base}/remote.php/dav/calendars/{username}/{cal_slug}/"
|
||||
resp = httpx.request(
|
||||
"MKCALENDAR",
|
||||
url,
|
||||
auth=self._auth,
|
||||
content=body.encode("utf-8"),
|
||||
headers={"Content-Type": "application/xml; charset=utf-8"},
|
||||
timeout=TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
# 201=建立成功, 405=已存在(Method Not Allowed on existing resource)
|
||||
return resp.status_code in (201, 405)
|
||||
except Exception as e:
|
||||
logger.error(f"NC subscribe_calendar({username}, {cal_slug}) failed: {e}")
|
||||
return False
|
||||
|
||||
def get_total_quota_used_gb(self) -> Optional[float]:
|
||||
"""Sum all users' quota usage"""
|
||||
try:
|
||||
|
||||
@@ -17,28 +17,33 @@ def dispatch_schedule(schedule_id: int, log_id: int = None, db: Session = None):
|
||||
When called from manual API, creates its own session and log.
|
||||
"""
|
||||
own_db = db is None
|
||||
own_log = False
|
||||
log_obj = None
|
||||
|
||||
if own_db:
|
||||
db = SessionLocal()
|
||||
|
||||
if log_id is None:
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from app.models.schedule import ScheduleLog, Schedule
|
||||
schedule = db.get(Schedule, schedule_id)
|
||||
if not schedule:
|
||||
if own_db:
|
||||
db.close()
|
||||
return
|
||||
log = ScheduleLog(
|
||||
log_obj = ScheduleLog(
|
||||
schedule_id=schedule_id,
|
||||
schedule_name=schedule.name,
|
||||
started_at=datetime.utcnow(),
|
||||
started_at=now_tw(),
|
||||
status="running",
|
||||
)
|
||||
db.add(log)
|
||||
db.add(log_obj)
|
||||
db.commit()
|
||||
db.refresh(log)
|
||||
log_id = log.id
|
||||
db.refresh(log_obj)
|
||||
log_id = log_obj.id
|
||||
own_log = True
|
||||
|
||||
final_status = "error"
|
||||
try:
|
||||
if schedule_id == 1:
|
||||
from app.services.scheduler.schedule_tenant import run_tenant_check
|
||||
@@ -51,9 +56,16 @@ def dispatch_schedule(schedule_id: int, log_id: int = None, db: Session = None):
|
||||
run_system_status(log_id, db)
|
||||
else:
|
||||
logger.warning(f"Unknown schedule_id: {schedule_id}")
|
||||
final_status = "ok"
|
||||
except Exception as e:
|
||||
logger.exception(f"dispatch_schedule({schedule_id}) error: {e}")
|
||||
raise
|
||||
finally:
|
||||
# When called from manual trigger (own_log), finalize the log entry
|
||||
if own_log and log_obj is not None:
|
||||
from app.core.utils import now_tw
|
||||
log_obj.ended_at = now_tw()
|
||||
log_obj.status = final_status
|
||||
db.commit()
|
||||
if own_db:
|
||||
db.close()
|
||||
|
||||
@@ -4,6 +4,7 @@ Schedule 2 — 帳號檢查(每 3 分鐘)
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.account import Account
|
||||
@@ -13,16 +14,17 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run_account_check(schedule_log_id: int, db: Session):
|
||||
from app.services.keycloak_client import KeycloakClient
|
||||
from app.services.mail_client import MailClient
|
||||
from app.services.nextcloud_client import NextcloudClient
|
||||
|
||||
from app.services.keycloak_client import get_keycloak_client
|
||||
|
||||
accounts = (
|
||||
db.query(Account)
|
||||
.filter(Account.is_active == True)
|
||||
.all()
|
||||
)
|
||||
kc = KeycloakClient()
|
||||
kc = get_keycloak_client()
|
||||
mail = MailClient()
|
||||
|
||||
for account in accounts:
|
||||
@@ -32,7 +34,7 @@ def run_account_check(schedule_log_id: int, db: Session):
|
||||
schedule_log_id=schedule_log_id,
|
||||
account_id=account.id,
|
||||
sso_account=account.sso_account,
|
||||
recorded_at=datetime.utcnow(),
|
||||
recorded_at=now_tw(),
|
||||
)
|
||||
fail_reasons = []
|
||||
|
||||
@@ -45,15 +47,19 @@ def run_account_check(schedule_log_id: int, db: Session):
|
||||
if not account.sso_uuid:
|
||||
account.sso_uuid = sso_uuid
|
||||
else:
|
||||
sso_uuid = kc.create_user(realm, account.sso_account, account.email, account.default_password)
|
||||
kc_email = account.notification_email or account.email
|
||||
sso_uuid = kc.create_user(realm, account.sso_account, kc_email, account.default_password)
|
||||
result.sso_result = sso_uuid is not None
|
||||
result.sso_uuid = sso_uuid
|
||||
if sso_uuid and not account.sso_uuid:
|
||||
account.sso_uuid = sso_uuid
|
||||
result.sso_done_at = datetime.utcnow()
|
||||
# 新使用者:寄送歡迎信(含設定密碼連結)
|
||||
if account.notification_email:
|
||||
kc.send_welcome_email(realm, sso_uuid)
|
||||
result.sso_done_at = now_tw()
|
||||
except Exception as e:
|
||||
result.sso_result = False
|
||||
result.sso_done_at = datetime.utcnow()
|
||||
result.sso_done_at = now_tw()
|
||||
fail_reasons.append(f"sso: {e}")
|
||||
|
||||
# [2] Mailbox check (skip if mail domain not ready)
|
||||
@@ -65,30 +71,113 @@ def run_account_check(schedule_log_id: int, db: Session):
|
||||
else:
|
||||
created = mail.create_mailbox(email, account.default_password, account.quota_limit)
|
||||
result.mailbox_result = created
|
||||
result.mailbox_done_at = datetime.utcnow()
|
||||
result.mailbox_done_at = now_tw()
|
||||
except Exception as e:
|
||||
result.mailbox_result = False
|
||||
result.mailbox_done_at = datetime.utcnow()
|
||||
result.mailbox_done_at = now_tw()
|
||||
fail_reasons.append(f"mailbox: {e}")
|
||||
|
||||
# [3] NC user check
|
||||
try:
|
||||
nc = NextcloudClient(tenant.domain)
|
||||
from app.core.config import settings as _cfg
|
||||
nc = NextcloudClient(tenant.domain, _cfg.NC_ADMIN_USER, _cfg.NC_ADMIN_PASSWORD)
|
||||
nc_exists = nc.user_exists(account.sso_account)
|
||||
if nc_exists:
|
||||
result.nc_result = True
|
||||
else:
|
||||
created = nc.create_user(account.sso_account, account.default_password, account.quota_limit)
|
||||
result.nc_result = created
|
||||
result.nc_done_at = datetime.utcnow()
|
||||
# 確保 quota 設定正確(無論新建或已存在)
|
||||
if result.nc_result and account.quota_limit:
|
||||
nc.set_user_quota(account.sso_account, account.quota_limit)
|
||||
result.nc_done_at = now_tw()
|
||||
except Exception as e:
|
||||
result.nc_result = False
|
||||
result.nc_done_at = datetime.utcnow()
|
||||
result.nc_done_at = now_tw()
|
||||
fail_reasons.append(f"nc: {e}")
|
||||
|
||||
# [4] Quota
|
||||
# [3.5] NC 台灣國定假日行事曆訂閱
|
||||
# 前置條件:NC 帳號已存在;MKCALENDAR 為 idempotent(已存在回傳 405 視為成功)
|
||||
TW_HOLIDAYS_ICS_URL = "https://www.officeholidays.com/ics-clean/taiwan"
|
||||
if result.nc_result:
|
||||
try:
|
||||
cal_ok = nc.subscribe_calendar(
|
||||
account.sso_account,
|
||||
"taiwan-public-holidays",
|
||||
"台灣國定假日",
|
||||
TW_HOLIDAYS_ICS_URL,
|
||||
"#e9322d",
|
||||
)
|
||||
if cal_ok:
|
||||
logger.info(f"NC calendar subscribed [{account.sso_account}]: taiwan-public-holidays")
|
||||
else:
|
||||
logger.warning(f"NC calendar subscribe failed [{account.sso_account}]")
|
||||
except Exception as e:
|
||||
logger.warning(f"NC calendar subscribe error [{account.sso_account}]: {e}")
|
||||
|
||||
# [4] NC Mail 帳號設定
|
||||
# 前置條件:NC 帳號已建立 + mailbox 已建立
|
||||
try:
|
||||
nc = NextcloudClient(tenant.domain)
|
||||
if result.nc_result and result.mailbox_result:
|
||||
import paramiko
|
||||
from app.core.config import settings as _cfg
|
||||
is_active = tenant.status == "active"
|
||||
nc_container = f"nc-{tenant.code}" if is_active else f"nc-{tenant.code}-test"
|
||||
email = account.email or f"{account.sso_account}@{tenant.domain}"
|
||||
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(_cfg.DOCKER_SSH_HOST, username=_cfg.DOCKER_SSH_USER, timeout=15)
|
||||
|
||||
def _ssh_run(cmd, timeout=60):
|
||||
"""執行 SSH 指令並回傳輸出,超時返回空字串"""
|
||||
_, stdout, _ = ssh.exec_command(cmd)
|
||||
stdout.channel.settimeout(timeout)
|
||||
try:
|
||||
out = stdout.read().decode().strip()
|
||||
except Exception:
|
||||
out = ""
|
||||
return out
|
||||
|
||||
# 確認是否已設定(occ mail:account:export 回傳帳號列表)
|
||||
export_out = _ssh_run(
|
||||
f"docker exec -u www-data {nc_container} "
|
||||
f"php /var/www/html/occ mail:account:export {account.sso_account} 2>/dev/null"
|
||||
)
|
||||
already_set = len(export_out) > 10 and "imap" in export_out.lower()
|
||||
|
||||
if already_set:
|
||||
result.nc_mail_result = True
|
||||
else:
|
||||
display = account.legal_name or account.sso_account
|
||||
create_cmd = (
|
||||
f"docker exec -u www-data {nc_container} "
|
||||
f"php /var/www/html/occ mail:account:create "
|
||||
f"'{account.sso_account}' '{display}' '{email}' "
|
||||
f"10.1.0.254 143 none '{email}' '{account.default_password}' "
|
||||
f"10.1.0.254 587 none '{email}' '{account.default_password}' 2>&1"
|
||||
)
|
||||
out_text = _ssh_run(create_cmd)
|
||||
logger.info(f"NC mail:account:create [{account.sso_account}]: {out_text}")
|
||||
result.nc_mail_result = (
|
||||
"error" not in out_text.lower() and "exception" not in out_text.lower()
|
||||
)
|
||||
|
||||
ssh.close()
|
||||
else:
|
||||
result.nc_mail_result = False
|
||||
fail_reasons.append(
|
||||
f"nc_mail: skipped (nc={result.nc_result}, mailbox={result.mailbox_result})"
|
||||
)
|
||||
result.nc_mail_done_at = now_tw()
|
||||
except Exception as e:
|
||||
result.nc_mail_result = False
|
||||
result.nc_mail_done_at = now_tw()
|
||||
fail_reasons.append(f"nc_mail: {e}")
|
||||
|
||||
# [5] Quota
|
||||
try:
|
||||
nc = NextcloudClient(tenant.domain, _cfg.NC_ADMIN_USER, _cfg.NC_ADMIN_PASSWORD)
|
||||
result.quota_usage = nc.get_user_quota_used_gb(account.sso_account)
|
||||
except Exception as e:
|
||||
logger.warning(f"Quota check failed for {account.account_code}: {e}")
|
||||
|
||||
@@ -5,6 +5,7 @@ Part B: 伺服器 ping 檢查
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.server import SystemStatusLog, ServerStatusLog, Server
|
||||
@@ -64,7 +65,7 @@ def run_system_status(schedule_log_id: int, db: Session):
|
||||
service_desc=svc["service_desc"],
|
||||
result=result,
|
||||
fail_reason=fail_reason,
|
||||
recorded_at=datetime.utcnow(),
|
||||
recorded_at=now_tw(),
|
||||
))
|
||||
|
||||
# Part B: Server ping
|
||||
@@ -87,7 +88,7 @@ def run_system_status(schedule_log_id: int, db: Session):
|
||||
result=result,
|
||||
response_time=response_time,
|
||||
fail_reason=fail_reason,
|
||||
recorded_at=datetime.utcnow(),
|
||||
recorded_at=now_tw(),
|
||||
))
|
||||
|
||||
db.commit()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@ Watchdog: APScheduler BackgroundScheduler,每 3 分鐘掃描 schedules 表。
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from app.core.utils import now_tw
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from croniter import croniter
|
||||
from sqlalchemy import update
|
||||
@@ -24,7 +25,7 @@ def _watchdog_tick():
|
||||
db.query(Schedule)
|
||||
.filter(
|
||||
Schedule.status == "Waiting",
|
||||
Schedule.next_run_at <= datetime.utcnow(),
|
||||
Schedule.next_run_at <= now_tw(),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
@@ -44,7 +45,7 @@ def _watchdog_tick():
|
||||
log = ScheduleLog(
|
||||
schedule_id=schedule.id,
|
||||
schedule_name=schedule.name,
|
||||
started_at=datetime.utcnow(),
|
||||
started_at=now_tw(),
|
||||
status="running",
|
||||
)
|
||||
db.add(log)
|
||||
@@ -60,12 +61,12 @@ def _watchdog_tick():
|
||||
final_status = "error"
|
||||
|
||||
# Update log
|
||||
log.ended_at = datetime.utcnow()
|
||||
log.ended_at = now_tw()
|
||||
log.status = final_status
|
||||
|
||||
# Recalculate next_run_at
|
||||
# Recalculate next_run_at (5-field cron: 分 時 日 月 週)
|
||||
try:
|
||||
cron = croniter(schedule.cron_timer, datetime.utcnow())
|
||||
cron = croniter(schedule.cron_timer, now_tw())
|
||||
next_run = cron.get_next(datetime)
|
||||
except Exception:
|
||||
next_run = None
|
||||
@@ -76,7 +77,7 @@ def _watchdog_tick():
|
||||
.where(Schedule.id == schedule.id)
|
||||
.values(
|
||||
status="Waiting",
|
||||
last_run_at=datetime.utcnow(),
|
||||
last_run_at=now_tw(),
|
||||
next_run_at=next_run,
|
||||
last_status=final_status,
|
||||
)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
"""Initial data seed: schedules + servers"""
|
||||
"""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 = [
|
||||
@@ -26,7 +28,7 @@ INITIAL_SERVERS = [
|
||||
|
||||
def _calc_next_run(cron_timer: str) -> datetime:
|
||||
# croniter: six-field cron (sec min hour day month weekday)
|
||||
cron = croniter(cron_timer, datetime.utcnow())
|
||||
cron = croniter(cron_timer, now_tw())
|
||||
return cron.get_next(datetime)
|
||||
|
||||
|
||||
@@ -46,4 +48,13 @@ def seed_initial_data(db: Session) -> None:
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user