2026-05-21 23:18:49 +08:00
|
|
|
"""Nexus — Pydantic Request/Response Models
|
|
|
|
|
Shared schemas for API validation, replacing raw `dict` parameters.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from typing import Optional, List
|
2026-05-24 16:26:40 +08:00
|
|
|
from pydantic import BaseModel, Field, model_validator
|
2026-05-21 23:18:49 +08:00
|
|
|
|
|
|
|
|
|
2026-05-21 23:38:01 +08:00
|
|
|
# ── Setting ──
|
|
|
|
|
|
|
|
|
|
class SettingUpdatePayload(BaseModel):
|
|
|
|
|
value: str = Field(..., min_length=0)
|
|
|
|
|
|
|
|
|
|
|
2026-05-25 22:49:57 +08:00
|
|
|
class ApiKeyRevealRequest(BaseModel):
|
|
|
|
|
"""Shared re-auth payload for revealing sensitive values (API key, install cmd, etc.)."""
|
|
|
|
|
current_password: str = Field(..., min_length=1, max_length=255)
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:38:01 +08:00
|
|
|
# ── Agent ──
|
|
|
|
|
|
|
|
|
|
class AgentHeartbeat(BaseModel):
|
2026-05-30 18:20:14 +08:00
|
|
|
server_id: Optional[int] = None # None = unregistered agent, silently discard
|
2026-05-21 23:38:01 +08:00
|
|
|
is_online: bool = True
|
|
|
|
|
system_info: Optional[dict] = None
|
|
|
|
|
agent_version: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
# ── Server ──
|
|
|
|
|
|
|
|
|
|
class ServerCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
domain: str = Field(..., min_length=1, max_length=255)
|
|
|
|
|
port: int = Field(22, ge=1, le=65535)
|
|
|
|
|
username: str = Field("root", max_length=100)
|
|
|
|
|
auth_method: str = Field("key", pattern="^(key|password)$")
|
|
|
|
|
password: Optional[str] = None
|
2026-05-25 22:49:57 +08:00
|
|
|
preset_id: Optional[int] = None
|
2026-05-21 23:18:49 +08:00
|
|
|
ssh_key_path: Optional[str] = None
|
|
|
|
|
ssh_key_private: Optional[str] = None
|
|
|
|
|
ssh_key_public: Optional[str] = None
|
2026-05-25 22:49:57 +08:00
|
|
|
ssh_key_preset_id: Optional[int] = None
|
2026-05-21 23:18:49 +08:00
|
|
|
agent_port: int = Field(8601, ge=1, le=65535)
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
category: Optional[str] = None
|
|
|
|
|
platform_id: Optional[int] = None
|
|
|
|
|
node_id: Optional[int] = None
|
|
|
|
|
protocols: Optional[list] = None
|
|
|
|
|
extra_attrs: Optional[dict] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServerUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
domain: Optional[str] = Field(None, min_length=1, max_length=255)
|
|
|
|
|
port: Optional[int] = Field(None, ge=1, le=65535)
|
|
|
|
|
username: Optional[str] = None
|
|
|
|
|
auth_method: Optional[str] = Field(None, pattern="^(key|password)$")
|
|
|
|
|
password: Optional[str] = None
|
2026-05-25 22:49:57 +08:00
|
|
|
preset_id: Optional[int] = None
|
2026-05-21 23:18:49 +08:00
|
|
|
ssh_key_path: Optional[str] = None
|
|
|
|
|
ssh_key_private: Optional[str] = None
|
|
|
|
|
ssh_key_public: Optional[str] = None
|
2026-05-25 22:49:57 +08:00
|
|
|
ssh_key_preset_id: Optional[int] = None
|
2026-05-21 23:18:49 +08:00
|
|
|
agent_port: Optional[int] = Field(None, ge=1, le=65535)
|
|
|
|
|
agent_api_key: Optional[str] = None
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
category: Optional[str] = None
|
|
|
|
|
platform_id: Optional[int] = None
|
|
|
|
|
node_id: Optional[int] = None
|
|
|
|
|
protocols: Optional[list] = None
|
|
|
|
|
extra_attrs: Optional[dict] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServerCheck(BaseModel):
|
|
|
|
|
server_ids: List[int] = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
2026-05-26 00:17:43 +08:00
|
|
|
class ServerImportResult(BaseModel):
|
|
|
|
|
"""Result of CSV batch import."""
|
|
|
|
|
total: int = 0
|
|
|
|
|
created: int = 0
|
|
|
|
|
skipped: int = 0
|
|
|
|
|
failed: int = 0
|
|
|
|
|
errors: List[dict] = [] # [{row, name, domain, error}]
|
|
|
|
|
created_ids: List[int] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BatchAgentAction(BaseModel):
|
|
|
|
|
"""Batch install/upgrade Agent on multiple servers."""
|
|
|
|
|
server_ids: List[int] = Field(..., min_length=1, max_length=50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BatchAgentResultItem(BaseModel):
|
|
|
|
|
server_id: int
|
|
|
|
|
server_name: str
|
|
|
|
|
success: bool
|
|
|
|
|
stdout: str = ""
|
|
|
|
|
error: str = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BatchAgentResult(BaseModel):
|
|
|
|
|
results: List[BatchAgentResultItem] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileUpload(BaseModel):
|
|
|
|
|
"""Upload file to remote server via SFTP."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
|
|
|
|
remote_path: str = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
# ── Sync ──
|
|
|
|
|
|
|
|
|
|
class SyncFiles(BaseModel):
|
|
|
|
|
server_ids: List[int] = Field(..., min_length=1)
|
|
|
|
|
source_path: str = Field(..., min_length=1)
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
sync_mode: str = Field("incremental", pattern="^(incremental|full|overwrite|checksum)$")
|
|
|
|
|
batch_size: int = Field(50, ge=1, le=200)
|
|
|
|
|
concurrency: int = Field(10, ge=1, le=50)
|
|
|
|
|
|
|
|
|
|
|
2026-05-23 16:41:47 +08:00
|
|
|
class SyncPreview(BaseModel):
|
|
|
|
|
"""Dry-run: rsync --dry-run --stats against one representative server."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
|
|
|
|
source_path: str = Field(..., min_length=1)
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
sync_mode: str = Field("incremental", pattern="^(incremental|full|overwrite|checksum)$")
|
|
|
|
|
verbose: bool = False # If True, include per-file list (truncated to 200 lines)
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
class SyncBrowse(BaseModel):
|
|
|
|
|
server_id: int
|
|
|
|
|
path: str = Field("/", min_length=1)
|
|
|
|
|
|
|
|
|
|
|
2026-05-28 21:01:27 +08:00
|
|
|
class SyncBrowseLocal(BaseModel):
|
|
|
|
|
"""Browse a local directory on the Nexus server (for upload file manager)."""
|
|
|
|
|
path: str = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocalFileOperation(BaseModel):
|
|
|
|
|
"""Delete or rename a file/directory in the local upload staging area."""
|
|
|
|
|
operation: str = Field(..., pattern="^(delete|rename)$")
|
|
|
|
|
path: str = Field(..., min_length=1)
|
|
|
|
|
new_name: Optional[str] = None # required for rename
|
|
|
|
|
|
|
|
|
|
|
2026-05-29 15:59:30 +08:00
|
|
|
class LocalFilePreview(BaseModel):
|
|
|
|
|
"""Preview file content in the local upload staging area."""
|
|
|
|
|
path: str = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
2026-05-29 21:29:41 +08:00
|
|
|
class ValidateSourcePath(BaseModel):
|
|
|
|
|
"""Validate a local directory path on the Nexus server as a push source."""
|
|
|
|
|
path: str = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SyncDiagnose(BaseModel):
|
|
|
|
|
"""Diagnose why a push failed for a specific server."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileSyncDiff(BaseModel):
|
|
|
|
|
"""Compare a local file with its remote counterpart to show push diff."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
|
|
|
|
source_path: str = Field(..., min_length=1)
|
|
|
|
|
relative_path: str = Field(..., min_length=1)
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SyncCancel(BaseModel):
|
|
|
|
|
"""Cancel an in-progress push by batch_id."""
|
|
|
|
|
batch_id: str = Field(..., min_length=1, max_length=32)
|
|
|
|
|
|
|
|
|
|
|
2026-05-28 21:01:27 +08:00
|
|
|
class SyncVerify(BaseModel):
|
|
|
|
|
"""Post-push verification: compare local source with remote target via md5sum."""
|
|
|
|
|
server_ids: List[int] = Field(..., min_length=1)
|
|
|
|
|
source_path: str = Field(..., min_length=1)
|
|
|
|
|
target_path: Optional[str] = None
|
|
|
|
|
max_files: int = Field(200, ge=1, le=500)
|
|
|
|
|
|
|
|
|
|
|
2026-05-23 16:46:00 +08:00
|
|
|
class FileOperation(BaseModel):
|
|
|
|
|
"""Remote file operation via SSH exec (delete / rename / mkdir)."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
|
|
|
|
operation: str = Field(..., pattern="^(delete|rename|mkdir)$")
|
|
|
|
|
path: str = Field(..., min_length=1) # source path (or new dir path for mkdir)
|
|
|
|
|
new_path: Optional[str] = None # required for rename
|
|
|
|
|
|
|
|
|
|
|
2026-05-30 23:34:24 +08:00
|
|
|
class FileDownload(BaseModel):
|
|
|
|
|
"""Download a file from a remote server."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
2026-05-30 23:44:35 +08:00
|
|
|
path: str = Field(..., min_length=1, max_length=4096)
|
2026-05-30 23:34:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileRead(BaseModel):
|
|
|
|
|
"""Read text file content from a remote server."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
2026-05-30 23:44:35 +08:00
|
|
|
path: str = Field(..., min_length=1, max_length=4096)
|
2026-05-30 23:34:24 +08:00
|
|
|
max_size: int = Field(1_000_000, ge=1, le=5_000_000) # 1MB default, 5MB max
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileWrite(BaseModel):
|
|
|
|
|
"""Write text content to a file on a remote server."""
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
2026-05-30 23:44:35 +08:00
|
|
|
path: str = Field(..., min_length=1, max_length=4096)
|
2026-05-30 23:34:24 +08:00
|
|
|
content: str = Field(..., max_length=5_000_000)
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
# ── Script ──
|
|
|
|
|
|
|
|
|
|
class ScriptCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
category: str = Field("ops", pattern="^(ops|deploy|check|cleanup)$")
|
|
|
|
|
content: str = Field(..., min_length=1)
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
created_by: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
category: Optional[str] = Field(None, pattern="^(ops|deploy|check|cleanup)$")
|
|
|
|
|
content: Optional[str] = None
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptExecute(BaseModel):
|
|
|
|
|
script_id: Optional[int] = None
|
|
|
|
|
command: str = Field(..., min_length=1)
|
|
|
|
|
server_ids: List[int] = Field(..., min_length=1)
|
2026-05-23 15:26:56 +08:00
|
|
|
credential_id: Optional[int] = None
|
2026-05-21 23:18:49 +08:00
|
|
|
timeout: int = Field(60, ge=1, le=600)
|
2026-05-23 15:26:56 +08:00
|
|
|
long_task: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptExecutionRetry(BaseModel):
|
|
|
|
|
server_ids: Optional[List[int]] = None
|
|
|
|
|
credential_id: Optional[int] = None
|
|
|
|
|
long_task: Optional[bool] = None
|
|
|
|
|
timeout: int = Field(120, ge=1, le=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptExecutionMarkStuck(BaseModel):
|
|
|
|
|
detail: str = Field("", max_length=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AgentScriptCallback(BaseModel):
|
|
|
|
|
"""Child host reports long-running script completion (curl from nohup wrapper)."""
|
|
|
|
|
job_id: str = Field(..., min_length=8, max_length=64)
|
|
|
|
|
server_id: int = Field(..., ge=1)
|
|
|
|
|
secret: str = Field(..., min_length=16, max_length=128)
|
|
|
|
|
exit_code: int = 0
|
|
|
|
|
message: str = Field("", max_length=500)
|
|
|
|
|
log_tail: Optional[str] = Field(None, max_length=2000)
|
2026-05-21 23:18:49 +08:00
|
|
|
|
|
|
|
|
|
2026-05-21 23:38:01 +08:00
|
|
|
# ── DB Credential ──
|
|
|
|
|
|
|
|
|
|
class DbCredentialCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
db_type: str = Field("mysql", pattern="^(mysql|postgresql|mariadb|mongodb|redis)$")
|
|
|
|
|
host: str = Field(..., min_length=1)
|
|
|
|
|
port: int = Field(3306, ge=1, le=65535)
|
|
|
|
|
username: str = Field(..., min_length=1)
|
|
|
|
|
password: str = Field(..., min_length=1)
|
|
|
|
|
database: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
2026-05-25 22:49:57 +08:00
|
|
|
class DbCredentialUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
db_type: Optional[str] = Field(None, pattern="^(mysql|postgresql|mariadb|mongodb|redis)$")
|
|
|
|
|
host: Optional[str] = Field(None, min_length=1)
|
|
|
|
|
port: Optional[int] = Field(None, ge=1, le=65535)
|
|
|
|
|
username: Optional[str] = Field(None, min_length=1)
|
|
|
|
|
password: Optional[str] = Field(None, min_length=1, description="留空则不修改密码")
|
|
|
|
|
database: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
# ── Schedule ──
|
|
|
|
|
|
|
|
|
|
class ScheduleCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
2026-05-23 17:29:16 +08:00
|
|
|
server_ids: str = Field(..., min_length=3) # JSON array string
|
2026-05-24 16:26:40 +08:00
|
|
|
run_mode: str = Field("once", pattern="^(once|cron)$")
|
|
|
|
|
cron_expr: Optional[str] = None # required when run_mode=cron
|
|
|
|
|
fire_at: Optional[str] = None # ISO datetime, required when run_mode=once
|
2026-05-21 23:18:49 +08:00
|
|
|
enabled: bool = True
|
2026-05-23 17:29:16 +08:00
|
|
|
# Schedule type
|
|
|
|
|
schedule_type: str = Field("push", pattern="^(push|script)$")
|
|
|
|
|
# Push-specific (required when schedule_type == 'push')
|
|
|
|
|
source_path: Optional[str] = None
|
|
|
|
|
sync_mode: str = Field("incremental", pattern="^(incremental|full|overwrite|checksum)$")
|
|
|
|
|
# Script-specific (required when schedule_type == 'script')
|
|
|
|
|
script_id: Optional[int] = None
|
|
|
|
|
script_content: Optional[str] = None
|
|
|
|
|
exec_timeout: int = Field(60, ge=10, le=600)
|
|
|
|
|
long_task: bool = False
|
2026-05-21 23:18:49 +08:00
|
|
|
|
2026-05-24 16:26:40 +08:00
|
|
|
@model_validator(mode="after")
|
|
|
|
|
def _validate_schedule_fields(self):
|
|
|
|
|
if self.run_mode == "cron" and not self.cron_expr:
|
|
|
|
|
raise ValueError("cron 模式必须提供 cron_expr")
|
|
|
|
|
if self.run_mode == "once" and not self.fire_at:
|
|
|
|
|
raise ValueError("一次性模式必须提供 fire_at 执行时间")
|
|
|
|
|
if self.schedule_type == "script" and not self.script_id and not self.script_content:
|
|
|
|
|
raise ValueError("script 类型调度必须提供 script_id 或 script_content")
|
|
|
|
|
if self.schedule_type == "push" and not self.source_path:
|
|
|
|
|
raise ValueError("push 类型调度必须提供 source_path")
|
|
|
|
|
return self
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
|
|
|
|
|
class ScheduleUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
source_path: Optional[str] = None
|
|
|
|
|
server_ids: Optional[str] = None
|
2026-05-24 16:26:40 +08:00
|
|
|
run_mode: Optional[str] = Field(None, pattern="^(once|cron)$")
|
2026-05-21 23:18:49 +08:00
|
|
|
cron_expr: Optional[str] = None
|
2026-05-24 16:26:40 +08:00
|
|
|
fire_at: Optional[str] = None
|
2026-05-22 11:01:17 +08:00
|
|
|
sync_mode: Optional[str] = Field(None, pattern="^(incremental|full|overwrite|checksum)$")
|
2026-05-21 23:18:49 +08:00
|
|
|
enabled: Optional[bool] = None
|
2026-05-23 17:29:16 +08:00
|
|
|
schedule_type: Optional[str] = Field(None, pattern="^(push|script)$")
|
|
|
|
|
script_id: Optional[int] = None
|
|
|
|
|
script_content: Optional[str] = None
|
|
|
|
|
exec_timeout: Optional[int] = Field(None, ge=10, le=600)
|
|
|
|
|
long_task: Optional[bool] = None
|
2026-05-21 23:18:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── Preset ──
|
|
|
|
|
|
|
|
|
|
class PresetCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
encrypted_pw: str = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
2026-05-25 22:49:57 +08:00
|
|
|
class PresetUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
encrypted_pw: Optional[str] = Field(None, min_length=1, description="留空则不修改密码")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── SSH Key Preset ──
|
|
|
|
|
|
|
|
|
|
class SshKeyPresetCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
private_key: str = Field(..., min_length=1)
|
|
|
|
|
public_key: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SshKeyPresetUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
private_key: Optional[str] = Field(None, min_length=1, description="留空则不修改私钥")
|
|
|
|
|
public_key: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 23:18:49 +08:00
|
|
|
# ── Platform / Node ──
|
|
|
|
|
|
|
|
|
|
class PlatformCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
category: str = Field(..., min_length=1, max_length=50)
|
|
|
|
|
type: str = Field(..., min_length=1, max_length=50)
|
|
|
|
|
default_protocols: Optional[list] = None
|
|
|
|
|
charset: str = "utf-8"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlatformUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
category: Optional[str] = None
|
|
|
|
|
type: Optional[str] = None
|
|
|
|
|
default_protocols: Optional[list] = None
|
|
|
|
|
charset: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeCreate(BaseModel):
|
|
|
|
|
name: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
parent_id: Optional[int] = None
|
|
|
|
|
sort_order: int = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeUpdate(BaseModel):
|
|
|
|
|
name: Optional[str] = Field(None, min_length=1, max_length=100)
|
|
|
|
|
parent_id: Optional[int] = None
|
|
|
|
|
sort_order: Optional[int] = None
|