tftsr-devops_investigation/src-tauri/src/proxmox/migration.rs
Shaun Arman 76d923a570
Some checks failed
Test / frontend-tests (pull_request) Successful in 1m40s
Test / frontend-typecheck (pull_request) Successful in 1m49s
PR Review Automation / review (pull_request) Successful in 6m0s
Test / rust-clippy (pull_request) Has been cancelled
Test / rust-tests (pull_request) Has been cancelled
Test / rust-fmt-check (pull_request) Has been cancelled
feat(proxmox): ISO upload, full CRUD validation, and security hardening
- Add ISO upload via OS file picker: multipart POST to nodes/{node}/storage/{storage}/upload,
  returns task UPID; Upload ISO button in CreateVmDialog triggers dialog filtered to .iso files
- Add cluster/datacenter selector to CreateVmDialog (shown when >1 cluster configured)
- Replace ISO text input with dropdown populated from listIsoImages; falls back to text input
  when storage has no ISOs
- Rewrite NetworkPage with full CRUD: add/edit/delete interfaces via dialog, Checkbox toggles
  for active/autostart, per-row Edit/Delete buttons
- Fix serde_bool_as_int deserializer to accept both bool and integer using visitor pattern
- Fix Content-Type conflict: remove pre-set header from build_headers(), let .json()/.form()
  manage it (root cause of 400 Bad Request on VM start/migrate)
- Fix migration: remove invalid targetcluster/targetstorage params, switch to JSON body
- Security: wire validate_pve_identifier() into all 9 path-interpolating commands
  (list/create/update/delete network interfaces, all 4 snapshot commands, list/upload ISO)
  — previously only create_proxmox_vm was guarded
- Add post_multipart() method to ProxmoxClient for multipart form-data requests
- Add uploadIsoImage TypeScript wrapper and update proxmoxClient exports
- Update IPC-Commands wiki with all new and previously undocumented commands
2026-06-21 21:41:57 -05:00

195 lines
5.9 KiB
Rust

// Remote Migration module
// Provides operations for cross-cluster VM migration
use serde::{Deserialize, Serialize};
/// Migration task information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationTask {
pub task_id: String,
pub vm_id: u32,
pub source_node: String,
pub target_node: String,
pub source_cluster: String,
pub target_cluster: String,
pub status: String,
pub progress: u32,
pub start_time: String,
pub end_time: Option<String>,
pub error: Option<String>,
}
/// Migration status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationStatus {
pub task_id: String,
pub status: String,
pub progress: u32,
pub bytes_transferred: u64,
pub bytes_remaining: u64,
pub downtime: u64,
}
/// Migrate VM to remote cluster
pub async fn migrate_vm(
client: &crate::proxmox::client::ProxmoxClient,
node: &str,
vm_id: u32,
target_node: &str,
target_cluster: &str,
ticket: &str,
) -> Result<MigrationTask, String> {
let path = format!("nodes/{}/qemu/{}/migrate", node, vm_id);
let body = serde_json::json!({
"target": target_node,
"online": 1,
"force": 0,
});
let response: serde_json::Value = client
.post::<serde_json::Value, _>(&path, &body, Some(ticket))
.await
.map_err(|e| format!("Failed to migrate VM {}: {}", vm_id, e))?;
// handle_response unwraps the "data" envelope; migrate returns the task UPID as a string.
let task_id = response.as_str().unwrap_or("").to_string();
let start_time = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
Ok(MigrationTask {
task_id,
vm_id,
source_node: node.to_string(),
target_node: target_node.to_string(),
source_cluster: client.base_url().to_string(),
target_cluster: target_cluster.to_string(),
status: "running".to_string(),
progress: 0,
start_time,
end_time: None,
error: None,
})
}
/// List migration tasks
pub async fn list_migration_status(
client: &crate::proxmox::client::ProxmoxClient,
node: &str,
ticket: &str,
) -> Result<Vec<MigrationTask>, String> {
let path = format!("nodes/{}/tasks", node);
let response: serde_json::Value = client
.get(&path, Some(ticket))
.await
.map_err(|e| format!("Failed to list migration tasks for node {}: {}", node, e))?;
if let Some(tasks) = response.as_array() {
let task_list: Vec<MigrationTask> = tasks
.iter()
.filter_map(|task| {
let id = task.get("id")?.as_str()?.to_string();
let vm_id = task
.get("vmid")
.and_then(|v| v.as_u64())
.map(|v| v as u32)?;
let status = task
.get("status")
.and_then(|s| s.as_str())
.unwrap_or("unknown")
.to_string();
let progress = task.get("progress").and_then(|p| p.as_u64()).unwrap_or(0) as u32;
let start_time = task
.get("starttime")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
let end_time = task
.get("endtime")
.and_then(|e| e.as_str())
.map(|e| e.to_string());
let error = task
.get("exitstatus")
.and_then(|e| e.as_str())
.filter(|e| !e.is_empty())
.map(|e| e.to_string());
Some(MigrationTask {
task_id: id,
vm_id,
source_node: node.to_string(),
target_node: "".to_string(),
source_cluster: "".to_string(),
target_cluster: "".to_string(),
status,
progress,
start_time,
end_time,
error,
})
})
.collect();
Ok(task_list)
} else {
Ok(vec![])
}
}
/// Get migration task status
pub async fn get_migration_task_status(
client: &crate::proxmox::client::ProxmoxClient,
node: &str,
task_id: &str,
ticket: &str,
) -> Result<MigrationStatus, String> {
let path = format!("nodes/{}/tasks/{}", node, task_id);
let response: serde_json::Value = client
.get(&path, Some(ticket))
.await
.map_err(|e| format!("Failed to get migration task {}: {}", task_id, e))?;
{
let data = &response;
let status = data
.get("status")
.and_then(|s| s.as_str())
.unwrap_or("unknown")
.to_string();
let progress = data.get("progress").and_then(|p| p.as_u64()).unwrap_or(0) as u32;
let bytes_transferred = data
.get("bytes_transferred")
.and_then(|b| b.as_u64())
.unwrap_or(0);
let bytes_remaining = data
.get("bytes_remaining")
.and_then(|b| b.as_u64())
.unwrap_or(0);
let downtime = data.get("downtime").and_then(|d| d.as_u64()).unwrap_or(0);
Ok(MigrationStatus {
task_id: task_id.to_string(),
status,
progress,
bytes_transferred,
bytes_remaining,
downtime,
})
}
}
/// Cancel migration task
pub async fn cancel_migration(
client: &crate::proxmox::client::ProxmoxClient,
node: &str,
vm_id: u32,
ticket: &str,
) -> Result<(), String> {
let path = format!("nodes/{}/qemu/{}/migrate", node, vm_id);
let params = vec![("cancel", "1")];
let _response: serde_json::Value = client
.post_form(&path, &params, Some(ticket))
.await
.map_err(|e| format!("Failed to cancel migration for VM {}: {}", vm_id, e))?;
Ok(())
}