net_shell/models/
mod.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// 执行方式枚举
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub enum ExecutionMethod {
7    #[serde(rename = "ssh")]
8    SSH,
9    #[serde(rename = "websocket")]
10    WebSocket,
11}
12
13/// SSH连接配置
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct SshConfig {
16    pub host: String,
17    pub port: u16,
18    pub username: String,
19    pub password: Option<String>,
20    pub private_key_path: Option<String>,
21    pub session_timeout_seconds: Option<u64>,
22    pub timeout_seconds: Option<u64>,
23}
24
25/// WebSocket配置(预留,后续实现)
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct WebSocketConfig {
28    pub url: String,
29    pub timeout_seconds: Option<u64>,
30}
31
32/// 客户端配置
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct ClientConfig {
35    pub name: String,
36    pub execution_method: ExecutionMethod,
37    pub ssh_config: Option<SshConfig>,
38    pub websocket_config: Option<WebSocketConfig>,
39}
40
41/// 变量提取规则
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ExtractRule {
44    pub name: String,
45    pub patterns: Vec<String>, // 支持多个正则表达式,按顺序尝试直到匹配成功
46    pub source: String, // "stdout", "stderr", "exit_code"
47    #[serde(default = "default_cascade")]
48    pub cascade: bool, // 是否启用级联模式:前一个正则的匹配结果作为下一个正则的输入,默认为true
49}
50
51/// 默认级联模式为true
52fn default_cascade() -> bool {
53    true
54}
55
56/// 步骤配置
57#[derive(Debug, Clone, Serialize, Deserialize, Default)]
58pub struct Step {
59    pub title: Option<String>,
60    pub name: String,
61    pub script: String,
62    #[serde(default)]
63    pub servers: Vec<String>,
64    pub timeout_seconds: Option<u64>,
65    pub extract: Option<Vec<ExtractRule>>,
66    #[serde(default)]
67    pub variables: Option<HashMap<String, String>>,
68}
69
70/// 流水线配置
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct Pipeline {
73    pub name: String,
74    pub title: Option<String>,
75    pub steps: Vec<Step>,
76}
77
78/// 全局配置
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct RemoteExecutionConfig {
81    pub variables: Option<HashMap<String, String>>,
82    pub clients: HashMap<String, ClientConfig>,
83    pub pipelines: Vec<Pipeline>,
84    pub default_timeout: Option<u64>,
85    pub global_scripts:Vec<String>
86}
87
88/// 实时输出类型
89#[derive(Debug, Clone)]
90pub enum OutputType {
91    Stdout,
92    Stderr,
93    Log,
94    StepStarted,    // 步骤开始执行
95    StepCompleted,  // 步骤执行完成
96}
97
98/// 实时输出事件
99#[derive(Debug, Clone)]
100pub struct OutputEvent {
101    pub pipeline_name: String,
102    pub server_name: String,
103    pub step: Step, // 替换step_name为完整的Step对象,方便排错
104    pub output_type: OutputType,
105    pub content: String,
106    pub timestamp: std::time::Instant,
107    pub variables: HashMap<String, String>, // 添加当前变量上下文
108}
109
110/// 输出回调函数类型
111pub type OutputCallback = std::sync::Arc<dyn Fn(OutputEvent) + Send + Sync>;
112
113/// 执行结果
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct ExecutionResult {
116    pub success: bool,
117    pub stdout: String,
118    pub stderr: String,
119    pub script: String,
120    pub exit_code: i32,
121    pub execution_time_ms: u64,
122    pub error_message: Option<String>,
123}
124
125/// 步骤执行结果
126#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct StepExecutionResult {
128    pub title: String,
129    pub step_name: String,
130    pub server_name: String,
131    pub execution_result: ExecutionResult,
132    pub overall_success: bool,
133    pub execution_time_ms: u64,
134}
135
136/// 流水线执行结果
137#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct PipelineExecutionResult {
139    pub pipeline_name: String,
140    pub title: String,
141    pub step_results: Vec<StepExecutionResult>,
142    pub overall_success: bool,
143    pub total_execution_time_ms: u64,
144} 
145
146/// 流水线执行结果
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct ShellExecutionResult {
149    pub pipeline_results: Vec<PipelineExecutionResult>,
150    pub success: bool,
151    pub reason: String,
152}