paygress-cli 0.1.9

Pay-per-use compute marketplace using Cashu ecash and Nostr — no accounts, no signups
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// Proxmox VE API Client
//
// Provides interface to Proxmox REST API for managing LXC containers and VMs.

use anyhow::{Context, Result};
use reqwest::{header, Client};
use serde::{Deserialize, Serialize};
use tracing::{error, info};

/// Proxmox API client for container and VM management
pub struct ProxmoxClient {
    client: Client,
    base_url: String,
    auth_header: String,
    node: String,
}

/// Configuration for creating an LXC container
#[derive(Debug, Clone, Serialize)]
pub struct LxcConfig {
    pub vmid: u32,
    pub hostname: String,
    pub ostemplate: String,
    pub storage: String,
    pub rootfs: String,
    pub memory: u32,
    pub cores: u32,
    pub net0: String,
    pub password: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ssh_public_keys: Option<String>,
    #[serde(default = "default_true")]
    pub start: bool,
    #[serde(default = "default_true")]
    pub unprivileged: bool,
}

fn default_true() -> bool {
    true
}

/// Configuration for creating a VM
#[derive(Debug, Clone, Serialize)]
pub struct VmConfig {
    pub vmid: u32,
    pub name: String,
    pub memory: u32,
    pub cores: u32,
    pub sockets: u32,
    pub ide2: String,  // ISO image
    pub scsi0: String, // Disk
    pub net0: String,
    pub ostype: String,
    #[serde(default = "default_true")]
    pub start: bool,
}

/// Node status information
#[derive(Debug, Clone, Deserialize)]
pub struct NodeStatus {
    pub cpu: f64,
    pub memory: MemoryInfo,
    pub uptime: u64,
    #[serde(default)]
    pub loadavg: Vec<f64>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MemoryInfo {
    pub total: u64,
    pub used: u64,
    pub free: u64,
}

/// Container/VM status
#[derive(Debug, Clone, Deserialize)]
pub struct WorkloadStatus {
    pub vmid: u32,
    pub status: String,
    pub name: String,
    #[serde(default)]
    pub uptime: u64,
    #[serde(default)]
    pub cpu: f64,
    #[serde(default)]
    pub mem: u64,
    #[serde(default)]
    pub maxmem: u64,
}

/// Proxmox API response wrapper
#[derive(Debug, Deserialize)]
struct ProxmoxResponse<T> {
    data: Option<T>,
    #[serde(default)]
    errors: Option<serde_json::Value>,
}

/// Task response from Proxmox (for async operations)
#[derive(Debug, Deserialize)]
struct TaskResponse {
    data: String, // UPID (task ID)
}

impl ProxmoxClient {
    /// Create a new Proxmox client
    ///
    /// # Arguments
    /// * `api_url` - Base URL of the Proxmox API (e.g., "https://192.168.1.100:8006/api2/json")
    /// * `token_id` - API token ID (e.g., "root@pam!paygress")
    /// * `token_secret` - API token secret
    /// * `node` - Proxmox node name (e.g., "pve")
    pub fn new(api_url: &str, token_id: &str, token_secret: &str, node: &str) -> Result<Self> {
        // Build client with self-signed cert support
        let client = Client::builder()
            .danger_accept_invalid_certs(true) // Proxmox often uses self-signed certs
            .build()
            .context("Failed to create HTTP client")?;

        let auth_header = format!("PVEAPIToken={}={}", token_id, token_secret);

        Ok(Self {
            client,
            base_url: api_url.trim_end_matches('/').to_string(),
            auth_header,
            node: node.to_string(),
        })
    }

    /// Get the node URL prefix
    fn node_url(&self) -> String {
        format!("{}/nodes/{}", self.base_url, self.node)
    }

    // ==================== LXC Container Operations ====================

    /// Create an LXC container
    pub async fn create_lxc(&self, config: &LxcConfig) -> Result<String> {
        let url = format!("{}/lxc", self.node_url());

        info!(
            "Creating LXC container {} on node {}",
            config.vmid, self.node
        );

        let response = self
            .client
            .post(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .form(config)
            .send()
            .await
            .context("Failed to send create LXC request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            error!("Failed to create LXC: {} - {}", status, body);
            anyhow::bail!("Proxmox API error: {} - {}", status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse create LXC response")?;

        info!("LXC creation task started: {}", task.data);
        Ok(task.data)
    }

    /// Start an LXC container
    pub async fn start_lxc(&self, vmid: u32) -> Result<String> {
        let url = format!("{}/lxc/{}/status/start", self.node_url(), vmid);

        info!("Starting LXC container {}", vmid);

        let response = self
            .client
            .post(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to send start LXC request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to start LXC {}: {} - {}", vmid, status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse start LXC response")?;

        Ok(task.data)
    }

    /// Stop an LXC container
    pub async fn stop_lxc(&self, vmid: u32) -> Result<String> {
        let url = format!("{}/lxc/{}/status/stop", self.node_url(), vmid);

        info!("Stopping LXC container {}", vmid);

        let response = self
            .client
            .post(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to send stop LXC request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to stop LXC {}: {} - {}", vmid, status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse stop LXC response")?;

        Ok(task.data)
    }

    /// Delete an LXC container
    pub async fn delete_lxc(&self, vmid: u32) -> Result<String> {
        let url = format!("{}/lxc/{}", self.node_url(), vmid);

        info!("Deleting LXC container {}", vmid);

        let response = self
            .client
            .delete(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to send delete LXC request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to delete LXC {}: {} - {}", vmid, status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse delete LXC response")?;

        Ok(task.data)
    }

    /// Get LXC container status
    pub async fn get_lxc_status(&self, vmid: u32) -> Result<WorkloadStatus> {
        let url = format!("{}/lxc/{}/status/current", self.node_url(), vmid);

        let response = self
            .client
            .get(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to get LXC status")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get LXC {} status: {} - {}", vmid, status, body);
        }

        let resp: ProxmoxResponse<WorkloadStatus> = response
            .json()
            .await
            .context("Failed to parse LXC status response")?;

        resp.data.context("No status data returned")
    }

    /// List all LXC containers on the node
    pub async fn list_lxc(&self) -> Result<Vec<WorkloadStatus>> {
        let url = format!("{}/lxc", self.node_url());

        let response = self
            .client
            .get(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to list LXC containers")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to list LXC containers: {} - {}", status, body);
        }

        let resp: ProxmoxResponse<Vec<WorkloadStatus>> = response
            .json()
            .await
            .context("Failed to parse LXC list response")?;

        Ok(resp.data.unwrap_or_default())
    }

    // ==================== VM Operations ====================

    /// Create a VM
    pub async fn create_vm(&self, config: &VmConfig) -> Result<String> {
        let url = format!("{}/qemu", self.node_url());

        info!("Creating VM {} on node {}", config.vmid, self.node);

        let response = self
            .client
            .post(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .form(config)
            .send()
            .await
            .context("Failed to send create VM request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            error!("Failed to create VM: {} - {}", status, body);
            anyhow::bail!("Proxmox API error: {} - {}", status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse create VM response")?;

        info!("VM creation task started: {}", task.data);
        Ok(task.data)
    }

    /// Start a VM
    pub async fn start_vm(&self, vmid: u32) -> Result<String> {
        let url = format!("{}/qemu/{}/status/start", self.node_url(), vmid);

        info!("Starting VM {}", vmid);

        let response = self
            .client
            .post(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to send start VM request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to start VM {}: {} - {}", vmid, status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse start VM response")?;

        Ok(task.data)
    }

    /// Stop a VM
    pub async fn stop_vm(&self, vmid: u32) -> Result<String> {
        let url = format!("{}/qemu/{}/status/stop", self.node_url(), vmid);

        info!("Stopping VM {}", vmid);

        let response = self
            .client
            .post(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to send stop VM request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to stop VM {}: {} - {}", vmid, status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse stop VM response")?;

        Ok(task.data)
    }

    /// Delete a VM
    pub async fn delete_vm(&self, vmid: u32) -> Result<String> {
        let url = format!("{}/qemu/{}", self.node_url(), vmid);

        info!("Deleting VM {}", vmid);

        let response = self
            .client
            .delete(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to send delete VM request")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to delete VM {}: {} - {}", vmid, status, body);
        }

        let task: TaskResponse = response
            .json()
            .await
            .context("Failed to parse delete VM response")?;

        Ok(task.data)
    }

    /// Get VM status
    pub async fn get_vm_status(&self, vmid: u32) -> Result<WorkloadStatus> {
        let url = format!("{}/qemu/{}/status/current", self.node_url(), vmid);

        let response = self
            .client
            .get(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to get VM status")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get VM {} status: {} - {}", vmid, status, body);
        }

        let resp: ProxmoxResponse<WorkloadStatus> = response
            .json()
            .await
            .context("Failed to parse VM status response")?;

        resp.data.context("No status data returned")
    }

    /// List all VMs on the node
    pub async fn list_vm(&self) -> Result<Vec<WorkloadStatus>> {
        let url = format!("{}/qemu", self.node_url());

        let response = self
            .client
            .get(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to list VMs")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to list VMs: {} - {}", status, body);
        }

        let resp: ProxmoxResponse<Vec<WorkloadStatus>> = response
            .json()
            .await
            .context("Failed to parse VM list response")?;

        Ok(resp.data.unwrap_or_default())
    }

    // ==================== Node Operations ====================

    /// Get node status (CPU, memory, uptime)
    pub async fn get_node_status(&self) -> Result<NodeStatus> {
        let url = format!("{}/status", self.node_url());

        let response = self
            .client
            .get(&url)
            .header(header::AUTHORIZATION, &self.auth_header)
            .send()
            .await
            .context("Failed to get node status")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get node status: {} - {}", status, body);
        }

        let resp: ProxmoxResponse<NodeStatus> = response
            .json()
            .await
            .context("Failed to parse node status response")?;

        resp.data.context("No node status data returned")
    }

    /// Find the next available VMID in a given range
    pub async fn find_available_vmid(&self, range_start: u32, range_end: u32) -> Result<u32> {
        let lxc_list = self.list_lxc().await?;
        let vm_list = self.list_vm().await?;

        let used_ids: std::collections::HashSet<u32> = lxc_list
            .iter()
            .chain(vm_list.iter())
            .map(|w| w.vmid)
            .collect();

        for vmid in range_start..=range_end {
            if !used_ids.contains(&vmid) {
                return Ok(vmid);
            }
        }

        anyhow::bail!("No available VMID in range {}-{}", range_start, range_end)
    }

    /// Wait for a task to complete
    pub async fn wait_for_task(&self, upid: &str, timeout_secs: u64) -> Result<()> {
        use tokio::time::{sleep, Duration};

        let start = std::time::Instant::now();
        let timeout = Duration::from_secs(timeout_secs);

        loop {
            if start.elapsed() > timeout {
                anyhow::bail!("Task {} timed out after {} seconds", upid, timeout_secs);
            }

            let url = format!("{}/tasks/{}/status", self.node_url(), upid);

            let response = self
                .client
                .get(&url)
                .header(header::AUTHORIZATION, &self.auth_header)
                .send()
                .await?;

            if response.status().is_success() {
                #[derive(Deserialize)]
                struct TaskStatus {
                    status: String,
                    #[serde(default)]
                    exitstatus: Option<String>,
                }

                let resp: ProxmoxResponse<TaskStatus> = response.json().await?;

                if let Some(task) = resp.data {
                    if task.status == "stopped" {
                        if let Some(exit) = task.exitstatus {
                            if exit == "OK" {
                                return Ok(());
                            } else {
                                anyhow::bail!("Task failed with: {}", exit);
                            }
                        }
                        return Ok(());
                    }
                }
            }

            sleep(Duration::from_secs(2)).await;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lxc_config_serialization() {
        let config = LxcConfig {
            vmid: 100,
            hostname: "test-container".to_string(),
            ostemplate: "local:vztmpl/ubuntu-22.04-standard.tar.zst".to_string(),
            storage: "local-lvm".to_string(),
            rootfs: "local-lvm:8".to_string(),
            memory: 1024,
            cores: 1,
            net0: "name=eth0,bridge=vmbr0,ip=dhcp".to_string(),
            password: "testpass".to_string(),
            ssh_public_keys: None,
            start: true,
            unprivileged: true,
        };

        // Should serialize without errors
        let _serialized = serde_urlencoded::to_string(&config).unwrap();
    }
}

// ==================== ComputeBackend Implementation ====================

use crate::compute::{ComputeBackend, ContainerConfig, NodeStatus as ComputeNodeStatus};
use async_trait::async_trait;

/// Wrapper around ProxmoxClient to implement ComputeBackend trait
pub struct ProxmoxBackend {
    client: ProxmoxClient,
    storage: String,
    bridge: String,
    template: String,
}

impl ProxmoxBackend {
    pub fn new(client: ProxmoxClient, storage: &str, bridge: &str, template: &str) -> Self {
        Self {
            client,
            storage: storage.to_string(),
            bridge: bridge.to_string(),
            template: template.to_string(),
        }
    }
}

#[async_trait]
impl ComputeBackend for ProxmoxBackend {
    async fn find_available_id(&self, range_start: u32, range_end: u32) -> Result<u32> {
        self.client
            .find_available_vmid(range_start, range_end)
            .await
    }

    async fn create_container(&self, config: &ContainerConfig) -> Result<String> {
        // Use default template if config doesn't specify (config.image is usually "ubuntu:22.04" etc)
        // But Proxmox needs full template path "local:vztmpl/..."
        // For now, we ignore config.image and use self.template

        let lxc = LxcConfig {
            vmid: config.id,
            hostname: config.name.clone(),
            ostemplate: self.template.clone(),
            storage: self.storage.clone(),
            rootfs: format!("{}:8", self.storage),
            memory: config.memory_mb,
            cores: config.cpu_cores,
            net0: format!("name=eth0,bridge={},ip=dhcp", self.bridge),
            password: config.password.clone(),
            ssh_public_keys: config.ssh_key.clone(),
            start: true,
            unprivileged: true,
        };

        let task = self.client.create_lxc(&lxc).await?;
        self.client.wait_for_task(&task, 120).await?;
        Ok(config.id.to_string())
    }

    async fn start_container(&self, id: u32) -> Result<()> {
        let task = self.client.start_lxc(id).await?;
        self.client.wait_for_task(&task, 60).await?;
        Ok(())
    }

    async fn stop_container(&self, id: u32) -> Result<()> {
        let task = self.client.stop_lxc(id).await?;
        self.client.wait_for_task(&task, 60).await?;
        Ok(())
    }

    async fn delete_container(&self, id: u32) -> Result<()> {
        let task = self.client.delete_lxc(id).await?;
        self.client.wait_for_task(&task, 60).await?;
        Ok(())
    }

    async fn get_node_status(&self) -> Result<ComputeNodeStatus> {
        let status = self.client.get_node_status().await?;
        Ok(ComputeNodeStatus {
            cpu_usage: status.cpu,
            memory_used: status.memory.used,
            memory_total: status.memory.total,
            disk_used: 0,
            disk_total: 0,
        })
    }

    async fn get_container_ip(&self, _id: u32) -> Result<Option<String>> {
        // Proxmox API connection logic to get IP is complex without guest agent
        Ok(None)
    }
}