实现 LightOps 运维面板基础功能

This commit is contained in:
2026-05-25 01:13:03 +08:00
commit d3bb9f45a6
84 changed files with 23505 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize)]
pub struct ApiResponse<T>
where
T: Serialize,
{
pub success: bool,
pub data: Option<T>,
pub error: Option<String>,
}
impl<T> ApiResponse<T>
where
T: Serialize,
{
pub fn ok(data: T) -> Self {
Self {
success: true,
data: Some(data),
error: None,
}
}
}
impl ApiResponse<()> {
pub fn empty() -> Self {
Self {
success: true,
data: Some(()),
error: None,
}
}
pub fn err(message: impl Into<String>) -> Self {
Self {
success: false,
data: None,
error: Some(message.into()),
}
}
}
#[derive(Debug, Deserialize)]
pub struct PageQuery {
pub limit: Option<i64>,
pub offset: Option<i64>,
}

View File

@@ -0,0 +1,2 @@
pub mod api;
pub mod protocol;

View File

@@ -0,0 +1,280 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentCapabilities {
pub file: bool,
pub terminal: bool,
pub systemd: bool,
pub nginx: bool,
pub docker: bool,
pub logs: bool,
}
impl Default for AgentCapabilities {
fn default() -> Self {
Self {
file: true,
terminal: true,
systemd: true,
nginx: true,
docker: true,
logs: true,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemMetrics {
pub cpu_usage: f64,
pub memory_total: u64,
pub memory_used: u64,
pub disk_total: u64,
pub disk_used: u64,
pub load_avg: f64,
pub uptime: u64,
pub networks: HashMap<String, NetworkInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkInfo {
pub received: u64,
pub transmitted: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AgentMessage {
#[serde(rename = "agent.hello")]
AgentHello {
agent_id: Option<String>,
token: Option<String>,
secret: Option<String>,
hostname: String,
os: String,
arch: String,
version: String,
capabilities: AgentCapabilities,
},
#[serde(rename = "agent.heartbeat")]
AgentHeartbeat {
agent_id: String,
metrics: Option<SystemMetrics>,
},
#[serde(rename = "agent.pong")]
AgentPong { agent_id: String, timestamp: i64 },
#[serde(rename = "task.response")]
TaskResponse {
task_id: String,
success: bool,
data: Value,
error: Option<String>,
},
#[serde(rename = "task.event")]
TaskEvent {
task_id: String,
level: String,
message: String,
data: Value,
},
#[serde(rename = "stream.open")]
StreamOpen {
stream_id: String,
kind: String,
meta: Value,
},
#[serde(rename = "stream.data")]
StreamData {
stream_id: String,
data: String,
binary: bool,
},
#[serde(rename = "stream.close")]
StreamClose {
stream_id: String,
reason: Option<String>,
},
#[serde(rename = "error")]
ErrorMessage { code: String, message: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ServerMessage {
#[serde(rename = "agent.accepted")]
AgentAccepted {
agent_id: String,
secret: Option<String>,
},
#[serde(rename = "task.request")]
TaskRequest {
task_id: String,
action: String,
params: Value,
},
#[serde(rename = "server.ping")]
ServerPing { timestamp: i64 },
#[serde(rename = "stream.open")]
StreamOpen {
stream_id: String,
kind: String,
meta: Value,
},
#[serde(rename = "stream.data")]
StreamData {
stream_id: String,
data: String,
binary: bool,
},
#[serde(rename = "stream.close")]
StreamClose {
stream_id: String,
reason: Option<String>,
},
#[serde(rename = "error")]
ErrorMessage { code: String, message: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileEntry {
pub name: String,
pub path: String,
pub is_dir: bool,
pub size: u64,
pub modified: Option<String>,
pub readonly: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceInfo {
pub name: String,
pub load: String,
pub active: String,
pub sub: String,
pub description: String,
pub enabled: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DockerContainer {
pub id: String,
pub image: String,
pub command: String,
pub status: String,
pub names: String,
pub ports: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DockerImage {
pub repository: String,
pub tag: String,
pub id: String,
pub size: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NginxSite {
pub name: String,
pub enabled: bool,
pub path: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ApplicationType {
WebApp,
Service,
Database,
Runtime,
Tool,
Container,
ComposeProject,
StaticSite,
ReverseProxy,
Custom,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ApplicationProviderType {
Systemd,
Docker,
DockerCompose,
Apt,
Dnf,
Pacman,
Snap,
Flatpak,
Binary,
PM2,
Supervisor,
NginxSite,
LightOpsManaged,
Custom,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ApplicationStatus {
Running,
Stopped,
Failed,
Enabled,
Disabled,
Installing,
Updating,
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Application {
pub id: String,
pub agent_id: String,
pub name: String,
pub display_name: String,
pub description: Option<String>,
pub app_type: ApplicationType,
pub provider: ApplicationProviderType,
pub status: ApplicationStatus,
pub version: Option<String>,
pub install_path: Option<String>,
pub work_dir: Option<String>,
pub config_paths: Vec<String>,
pub log_paths: Vec<String>,
pub data_paths: Vec<String>,
pub ports: Vec<u16>,
pub domains: Vec<String>,
pub service_name: Option<String>,
pub container_id: Option<String>,
pub compose_project: Option<String>,
pub package_name: Option<String>,
pub nginx_site: Option<String>,
pub run_user: Option<String>,
pub is_system: bool,
pub is_managed: bool,
pub is_lightops_managed: bool,
pub metadata: Value,
pub created_at: Option<String>,
pub updated_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationRelation {
pub id: String,
pub agent_id: String,
pub app_id: String,
pub relation_type: String,
pub target_id: Option<String>,
pub target_name: Option<String>,
pub metadata: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationDetail {
pub application: Application,
pub relations: Vec<ApplicationRelation>,
pub recent_actions: Vec<Value>,
pub runtime_info: Value,
pub available_actions: Vec<String>,
pub risk_level: String,
pub provider_specific_info: Value,
}