-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rs
More file actions
419 lines (374 loc) · 13.6 KB
/
server.rs
File metadata and controls
419 lines (374 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// utils/server.rs
//! # Server Utility Module
//!
//! This module provides utilities for starting, stopping, and managing StackQL server instances.
//! It supports detecting running servers, extracting process information, and managing server lifecycles
//! with functionalities to start, stop, and check server status across multiple platforms (Windows, Linux, macOS).
//!
//! ## Features
//! - Start a StackQL server on a specified host and port.
//! - Check if a server is running.
//! - Retrieve running servers by scanning processes.
//! - Stop a server by process ID (PID).
//! - Automatically detect and manage servers running on local or remote hosts.
//!
//! ## Example Usage
//! ```rust
//! use crate::utils::server::{check_and_start_server, start_server, stop_server, StartServerOptions};
//!
//! let options = StartServerOptions {
//! host: "localhost".to_string(),
//! port: 5444,
//! ..Default::default()
//! };
//!
//! match start_server(&options) {
//! Ok(pid) => println!("Server started with PID: {}", pid),
//! Err(e) => eprintln!("Failed to start server: {}", e),
//! }
//! ```
use std::fs::OpenOptions;
use std::path::Path;
use std::process;
use std::process::{Command as ProcessCommand, Stdio};
use std::thread;
use std::time::Duration;
use log::{debug, error, info, warn};
use crate::app::{DEFAULT_LOG_FILE, LOCAL_SERVER_ADDRESSES};
use crate::globals::{server_host, server_port};
use crate::utils::binary::get_binary_path;
/// Options for starting a StackQL server
pub struct StartServerOptions {
pub host: String,
pub port: u16,
pub registry: Option<String>,
pub mtls_config: Option<String>,
pub custom_auth_config: Option<String>,
pub log_level: Option<String>,
}
impl Default for StartServerOptions {
fn default() -> Self {
Self {
host: "localhost".to_string(),
port: crate::app::DEFAULT_SERVER_PORT,
registry: None,
mtls_config: None,
custom_auth_config: None,
log_level: None,
}
}
}
/// Represents a running StackQL server process
pub struct RunningServer {
pub pid: u32,
pub port: u16,
}
/// Check if the stackql server is running on a specific port
pub fn is_server_running(port: u16) -> bool {
let servers = find_all_running_servers();
debug!(
"is_server_running({}): found {} candidate server(s): {:?}",
port,
servers.len(),
servers
.iter()
.map(|s| format!("pid={} port={}", s.pid, s.port))
.collect::<Vec<_>>()
);
let result = servers.iter().any(|server| server.port == port);
debug!("is_server_running({}) -> {}", port, result);
result
}
/// Find all stackql servers that are running and their ports
pub fn find_all_running_servers() -> Vec<RunningServer> {
let mut running_servers = Vec::new();
if cfg!(target_os = "windows") {
// Use PowerShell Get-CimInstance to get stackql processes with command lines
let output = ProcessCommand::new("powershell")
.args([
"-NoProfile",
"-Command",
"Get-CimInstance Win32_Process -Filter \"Name='stackql.exe'\" | ForEach-Object { \"PID=$($_.ProcessId) CMD=$($_.CommandLine)\" }",
])
.output();
if let Ok(output) = output {
let output_str = String::from_utf8_lossy(&output.stdout);
for line in output_str.lines() {
let line = line.trim();
if let Some(rest) = line.strip_prefix("PID=") {
if let Some(space_pos) = rest.find(" CMD=") {
let pid_str = &rest[..space_pos];
let cmdline = &rest[space_pos + 5..];
if let Ok(pid) = pid_str.parse::<u32>() {
if let Some(port) = extract_port_from_cmdline(cmdline) {
debug!(
"find_all_running_servers (Windows): PID {} -> port {}",
pid, port
);
running_servers.push(RunningServer { pid, port });
}
}
}
}
}
} else {
debug!("find_all_running_servers: PowerShell command failed");
}
} else {
let output = ProcessCommand::new("pgrep")
.arg("-f")
.arg("stackql srv")
.output()
.unwrap_or_else(|_| panic!("Failed to execute pgrep"));
if !output.stdout.is_empty() {
let pids_str = String::from_utf8_lossy(&output.stdout).to_string();
let pids = pids_str.trim().split('\n').collect::<Vec<&str>>();
debug!(
"find_all_running_servers: pgrep found {} PID(s): {:?}",
pids.len(),
pids
);
for pid_str in pids {
if let Ok(pid) = pid_str.trim().parse::<u32>() {
// Log the full command line of this PID before attempting port extraction
if let Ok(ps_out) = ProcessCommand::new("ps")
.arg("-p")
.arg(pid.to_string())
.arg("-o")
.arg("args")
.output()
{
let cmdline = String::from_utf8_lossy(&ps_out.stdout);
debug!(
"find_all_running_servers: PID {} cmdline: {}",
pid,
cmdline.trim()
);
}
if let Some(port) = extract_port_from_ps(pid_str) {
debug!("find_all_running_servers: PID {} -> port {}", pid, port);
running_servers.push(RunningServer { pid, port });
} else {
debug!(
"find_all_running_servers: PID {} -> no --pgsrv.port found, skipping",
pid
);
}
}
}
} else {
debug!("find_all_running_servers: pgrep returned no matching PIDs");
}
}
running_servers
}
/// Extract port from process information on Unix-like systems using `ps`
fn extract_port_from_ps(pid: &str) -> Option<u16> {
let ps_output = ProcessCommand::new("ps")
.arg("-p")
.arg(pid)
.arg("-o")
.arg("args")
.output()
.ok()?;
let ps_str = String::from_utf8_lossy(&ps_output.stdout);
let patterns = [
"--pgsrv.port=",
"--pgsrv.port ",
"pgsrv.port=",
"pgsrv.port ",
];
for pattern in patterns.iter() {
if let Some(start_index) = ps_str.find(pattern) {
let port_start = start_index + pattern.len();
let port_end = ps_str[port_start..]
.split_whitespace()
.next()
.unwrap_or("")
.trim();
if let Ok(port) = port_end.parse::<u16>() {
return Some(port);
}
}
}
None
}
/// Extract port from a command line string by looking for --pgsrv.port argument
fn extract_port_from_cmdline(cmdline: &str) -> Option<u16> {
// Try --pgsrv.port=PORT format
if let Some(pos) = cmdline.find("--pgsrv.port=") {
let after = &cmdline[pos + "--pgsrv.port=".len()..];
let port_str: String = after.chars().take_while(|c| c.is_ascii_digit()).collect();
if let Ok(port) = port_str.parse::<u16>() {
return Some(port);
}
}
// Try --pgsrv.port PORT format
if let Some(pos) = cmdline.find("--pgsrv.port") {
let after = &cmdline[pos + "--pgsrv.port".len()..];
let trimmed = after.trim_start();
let port_str: String = trimmed.chars().take_while(|c| c.is_ascii_digit()).collect();
if let Ok(port) = port_str.parse::<u16>() {
return Some(port);
}
}
None
}
/// Get the PID of the running stackql server on a specific port
pub fn get_server_pid(port: u16) -> Option<u32> {
// Use find_all_running_servers which handles platform differences
let servers = find_all_running_servers();
servers.iter().find(|s| s.port == port).map(|s| s.pid)
}
/// Start the stackql server with the given options
pub fn start_server(options: &StartServerOptions) -> Result<u32, String> {
debug!(
"start_server called: host={}, port={}",
options.host, options.port
);
let binary_path = match get_binary_path() {
Some(path) => {
debug!("Using stackql binary at: {:?}", path);
path
}
_none => return Err("stackql binary not found".to_string()),
};
debug!(
"Checking if server is already running on port {}...",
options.port
);
if is_server_running(options.port) {
info!("Server is already running on port {}", options.port);
return Ok(get_server_pid(options.port).unwrap_or(0));
}
debug!(
"Server not running on port {}; proceeding to start.",
options.port
);
let mut cmd = ProcessCommand::new(&binary_path);
cmd.arg("srv");
cmd.arg("--pgsrv.address").arg(&options.host);
cmd.arg("--pgsrv.port").arg(options.port.to_string());
cmd.arg("--pgsrv.debug.enable=true");
cmd.arg("--pgsrv.loglevel=DEBUG");
if let Some(registry) = &options.registry {
cmd.arg("--registry").arg(registry);
}
if let Some(mtls_config) = &options.mtls_config {
cmd.arg("--mtls-config").arg(mtls_config);
}
if let Some(custom_auth) = &options.custom_auth_config {
cmd.arg("--custom-auth-config").arg(custom_auth);
}
if let Some(log_level) = &options.log_level {
cmd.arg("--log-level").arg(log_level);
}
let log_path = Path::new(DEFAULT_LOG_FILE);
let log_file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
// .append(true)
.open(log_path)
.map_err(|e| format!("Failed to open log file: {}", e))?;
debug!("Spawning stackql server process (log -> {:?})...", log_path);
let child = cmd
.stdout(Stdio::from(log_file.try_clone().unwrap()))
.stderr(Stdio::from(log_file))
.spawn()
.map_err(|e| format!("Failed to start server: {}", e))?;
let pid = child.id();
info!("Starting stackql server with PID: {}", pid);
debug!(
"Waiting 5 seconds for server on port {} to become ready...",
options.port
);
thread::sleep(Duration::from_secs(5));
debug!(
"Re-checking if server is running on port {}...",
options.port
);
if is_server_running(options.port) {
info!("Server started successfully on port {}", options.port);
Ok(pid)
} else {
Err("Server failed to start properly".to_string())
}
}
/// Stop the stackql server
pub fn stop_server(port: u16) -> Result<(), String> {
if !is_server_running(port) {
warn!("No server running on port {}", port);
return Ok(());
}
let pid = match get_server_pid(port) {
Some(pid) => pid,
_none => return Err("Could not determine server PID".to_string()),
};
info!("Stopping stackql server with PID: {}", pid);
if cfg!(target_os = "windows") {
ProcessCommand::new("taskkill")
.arg("/F")
.arg("/PID")
.arg(pid.to_string())
.output()
.map_err(|e| format!("Failed to stop server: {}", e))?;
} else {
ProcessCommand::new("kill")
.arg(pid.to_string())
.output()
.map_err(|e| format!("Failed to stop server: {}", e))?;
}
Ok(())
}
/// Checks if the server is running and starts it if necessary.
///
/// This function checks if the server is local and needs to be started. If the server is not running,
/// it attempts to start it with the specified host and port.
///
/// # Arguments
///
/// * `host` - A reference to the server host address.
/// * `port` - The port number to check.
///
/// # Behavior
///
/// * If the server is already running locally, it will display a message indicating this.
/// * If a remote server is specified, it will display a message indicating the remote connection.
/// * If the server needs to be started, it will attempt to do so and indicate success or failure.
pub fn check_and_start_server() {
let host = server_host();
let port = server_port();
debug!("check_and_start_server: host={}, port={}", host, port);
if LOCAL_SERVER_ADDRESSES.contains(&host) {
debug!(
"Host '{}' is local; checking if server is running on port {}...",
host, port
);
if is_server_running(port) {
info!("Local server is already running on port {}.", port);
} else {
debug!(
"Server not detected on port {}; will attempt to start it.",
port
);
info!("Server not running. Starting server...");
let options = StartServerOptions {
host: host.to_string(),
port,
..Default::default()
};
debug!(
"StartServerOptions: host={}, port={}",
options.host, options.port
);
if let Err(e) = start_server(&options) {
error!("Failed to start server: {}", e);
process::exit(1);
}
}
} else {
debug!("Host '{}' is remote; skipping local server start.", host);
info!("Using remote server {}:{}", host, port);
}
}