xbp 0.9.3

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
Documentation
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
use actix_web::{web, HttpResponse, Responder};
use tokio::process::Command;
use tracing::{error, info};
use reqwest;
use crate::api::models::*;
use crate::commands::{
    pm2_stop, pm2_delete, pm2_save, run_service_command as run_service_cmd,
    run_config, install_package as install_package_cmd, run_setup, run_redeploy_service,
    run_redeploy, run_ports,
};
use crate::strategies::get_all_services;
use crate::commands::service::load_xbp_config;
use std::fs;

pub async fn health() -> impl Responder {
    HttpResponse::Ok().json(HealthResponse {
        status: "ok".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
    })
}

pub async fn list_ports() -> impl Responder {
    match get_ports_data(None).await {
        Ok(ports) => HttpResponse::Ok().json(PortsResponse { ports }),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn get_port(path: web::Path<u16>) -> impl Responder {
    let port = path.into_inner();
    match get_ports_data(Some(port)).await {
        Ok(ports) => HttpResponse::Ok().json(PortsResponse { ports }),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn kill_port(path: web::Path<u16>) -> impl Responder {
    let port = path.into_inner();
    let args = vec!["-p".to_string(), port.to_string(), "--kill".to_string()];
    match run_ports(&args, false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Killed processes on port {}", port)})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

async fn get_ports_data(port_filter: Option<u16>) -> Result<Vec<PortInfo>, String> {
    use netstat2::{get_sockets_info, AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo};
    use std::collections::BTreeMap;

    let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
    let proto_flags = ProtocolFlags::TCP;

    let sockets = get_sockets_info(af_flags, proto_flags)
        .map_err(|e| format!("Failed to get sockets info: {}", e))?;

    let mut port_map: BTreeMap<u16, Vec<PortInfo>> = BTreeMap::new();

    for socket in sockets {
        if let ProtocolSocketInfo::Tcp(ref tcp_info) = socket.protocol_socket_info {
            if let Some(filter) = port_filter {
                if tcp_info.local_port != filter {
                    continue;
                }
            }

            let pid = if !socket.associated_pids.is_empty() {
                Some(socket.associated_pids[0].to_string())
            } else {
                None
            };

            let port_info = PortInfo {
                port: tcp_info.local_port,
                pid,
                local_addr: tcp_info.local_addr.to_string(),
                remote_addr: tcp_info.remote_addr.to_string(),
                state: format!("{:?}", tcp_info.state),
                process: "-".to_string(),
            };

            port_map.entry(tcp_info.local_port).or_default().push(port_info);
        }
    }

    let mut result = Vec::new();
    for ports in port_map.values() {
        result.extend(ports.iter().cloned());
    }

    Ok(result)
}

pub async fn list_systemctl() -> impl Responder {
    match get_systemctl_data(None).await {
        Ok(services) => HttpResponse::Ok().json(SystemctlResponse { services }),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn get_systemctl_service(path: web::Path<String>) -> impl Responder {
    let service_name = path.into_inner();
    match get_systemctl_data(Some(&service_name)).await {
        Ok(services) => HttpResponse::Ok().json(SystemctlResponse { services }),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

async fn get_systemctl_data(filter: Option<&str>) -> Result<Vec<SystemctlService>, String> {
    let mut cmd = Command::new("systemctl");
    cmd.arg("list-units");
    cmd.arg("--type=service");
    cmd.arg("--no-pager");
    cmd.arg("--no-legend");

    let output = cmd.output().await.map_err(|e| format!("Failed to run systemctl: {}", e))?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).to_string());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut services = Vec::new();

    for line in stdout.lines() {
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() < 4 {
            continue;
        }

        let name = parts[0].to_string();
        if let Some(filter_name) = filter {
            if !name.contains(filter_name) {
                continue;
            }
        }

        let status = parts[2].to_string();
        let active = status == "active";
        let enabled = parts[3] == "enabled";

        services.push(SystemctlService {
            name,
            status,
            active,
            enabled,
        });
    }

    Ok(services)
}

pub async fn list_pm2() -> impl Responder {
    match get_pm2_data().await {
        Ok(processes) => HttpResponse::Ok().json(Pm2Response { processes }),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

async fn get_pm2_data() -> Result<Vec<Pm2Process>, String> {
    let mut cmd = Command::new("pm2");
    cmd.arg("jlist");

    let output = cmd.output().await.map_err(|e| format!("Failed to run pm2 jlist: {}", e))?;

    if !output.status.success() {
        return Err(String::from_utf8_lossy(&output.stderr).to_string());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let processes: Vec<serde_json::Value> = serde_json::from_str(&stdout)
        .map_err(|e| format!("Failed to parse pm2 output: {}", e))?;

    let mut result = Vec::new();
    for proc in processes {
        let name = proc["name"].as_str().unwrap_or("unknown").to_string();
        let pid = proc["pid"].as_u64().map(|p| p as u32);
        let status = proc["pm2_env"]["status"].as_str().unwrap_or("unknown").to_string();
        let cpu = proc["monit"]["cpu"].as_f64();
        let memory = proc["monit"]["memory"].as_f64().map(|m| m / 1024.0 / 1024.0);
        let uptime = proc["pm2_env"]["pm_uptime"].as_u64().map(|u| {
            let seconds = u / 1000;
            format!("{}s", seconds)
        });

        result.push(Pm2Process {
            name,
            pid,
            status,
            cpu,
            memory,
            uptime,
        });
    }

    Ok(result)
}

pub async fn delete_pm2(path: web::Path<String>) -> impl Responder {
    let name = path.into_inner();
    match pm2_delete(&name, false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Deleted PM2 process: {}", name)})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn start_pm2(path: web::Path<String>) -> impl Responder {
    let name = path.into_inner();
    let mut cmd = Command::new("pm2");
    cmd.arg("start").arg(&name);
    match cmd.output().await {
        Ok(output) => {
            if output.status.success() {
                HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Started PM2 process: {}", name)}))
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                HttpResponse::InternalServerError().json(ErrorResponse { error: format!("PM2 start failed: {}", stderr) })
            }
        }
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: format!("Failed to start PM2 process: {}", e) }),
    }
}

pub async fn stop_pm2(path: web::Path<String>) -> impl Responder {
    let name = path.into_inner();
    match pm2_stop(&name, false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Stopped PM2 process: {}", name)})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn restart_pm2(path: web::Path<String>) -> impl Responder {
    let name = path.into_inner();
    let mut cmd = Command::new("pm2");
    cmd.arg("restart").arg(&name);
    match cmd.output().await {
        Ok(output) => {
            if output.status.success() {
                HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Restarted PM2 process: {}", name)}))
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                HttpResponse::InternalServerError().json(ErrorResponse { error: format!("PM2 restart failed: {}", stderr) })
            }
        }
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: format!("Failed to restart PM2 process: {}", e) }),
    }
}

pub async fn list_services() -> impl Responder {
    match load_xbp_config().await {
        Ok(config) => {
            let services = get_all_services(&config);
            let service_infos: Vec<ServiceInfo> = services.iter().map(|s| {
                ServiceInfo {
                    name: s.name.clone(),
                    target: s.target.clone(),
                    port: s.port,
                    branch: s.branch.clone(),
                    url: s.url.clone(),
                }
            }).collect();
            HttpResponse::Ok().json(ServicesResponse { services: service_infos })
        }
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn run_service_command(path: web::Path<(String, String)>) -> impl Responder {
    let (name, command) = path.into_inner();
    match run_service_cmd(&command, &name, false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Executed {} on service {}", command, name)})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn get_config() -> impl Responder {
    match run_config(false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn get_logs() -> impl Responder {
    HttpResponse::Ok().json(serde_json::json!({"message": "Logs endpoint - use /logs?command=<command> for specific logs"}))
}

pub async fn install_package(path: web::Path<String>) -> impl Responder {
    let package = path.into_inner();
    match install_package_cmd(&package, false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Installed package: {}", package)})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn setup() -> impl Responder {
    match run_setup(false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": "Setup completed"})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn redeploy() -> impl Responder {
    match run_redeploy().await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": "Redeploy completed"})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn redeploy_service(path: web::Path<String>) -> impl Responder {
    let service_name = path.into_inner();
    match run_redeploy_service(&service_name, false).await {
        Ok(_) => HttpResponse::Ok().json(serde_json::json!({"success": true, "message": format!("Redeployed service: {}", service_name)})),
        Err(e) => HttpResponse::InternalServerError().json(ErrorResponse { error: e }),
    }
}

pub async fn download_and_run_binary(req: web::Json<BinaryDownloadRequest>) -> impl Responder {
    let download_req = req.into_inner();
    
    info!("Downloading binary from: {}", download_req.url);
    
    let client = reqwest::Client::new();
    let response = match client.get(&download_req.url).send().await {
        Ok(resp) => resp,
        Err(e) => {
            error!("Failed to download binary: {}", e);
            return HttpResponse::BadRequest().json(ErrorResponse {
                error: format!("Failed to download binary: {}", e),
            });
        }
    };

    let bytes = match response.bytes().await {
        Ok(b) => b,
        Err(e) => {
            error!("Failed to read binary data: {}", e);
            return HttpResponse::BadRequest().json(ErrorResponse {
                error: format!("Failed to read binary data: {}", e),
            });
        }
    };

    let binary_path = format!("/tmp/{}", download_req.name);
    match fs::write(&binary_path, &bytes) {
        Ok(_) => {
            info!("Binary saved to: {}", binary_path);
        }
        Err(e) => {
            error!("Failed to save binary: {}", e);
            return HttpResponse::InternalServerError().json(ErrorResponse {
                error: format!("Failed to save binary: {}", e),
            });
        }
    }

    let chmod_output = Command::new("chmod")
        .arg("+x")
        .arg(&binary_path)
        .output()
        .await;

    if let Err(e) = chmod_output {
        error!("Failed to make binary executable: {}", e);
        return HttpResponse::InternalServerError().json(ErrorResponse {
            error: format!("Failed to make binary executable: {}", e),
        });
    }

    let mut pm2_cmd = Command::new("pm2");
    pm2_cmd.arg("start");
    pm2_cmd.arg(&binary_path);
    pm2_cmd.arg("--name");
    pm2_cmd.arg(&download_req.name);

    if let Some(ref args) = download_req.args {
        for arg in args {
            pm2_cmd.arg(arg);
        }
    }

    match pm2_cmd.output().await {
        Ok(output) => {
            if output.status.success() {
                let _ = pm2_save(false).await;
                HttpResponse::Ok().json(BinaryDownloadResponse {
                    success: true,
                    message: format!("Binary downloaded and started as PM2 process: {}", download_req.name),
                    pm2_name: Some(download_req.name),
                })
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                error!("PM2 start failed: {}", stderr);
                HttpResponse::InternalServerError().json(ErrorResponse {
                    error: format!("PM2 start failed: {}", stderr),
                })
            }
        }
        Err(e) => {
            error!("Failed to start PM2 process: {}", e);
            HttpResponse::InternalServerError().json(ErrorResponse {
                error: format!("Failed to start PM2 process: {}", e),
            })
        }
    }
}