From 163e9bbe91715a91d77e38a8f288857d1602fdd5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 7 Dec 2019 16:11:26 +0100 Subject: [PATCH] src/bin/proxmox-backup-client.rs: new display task log helper New helper to display worker task logs. Use it for prune (experimental). --- src/api2/admin/datastore.rs | 3 +- src/bin/proxmox-backup-client.rs | 83 +++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 38351494..b387fb93 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -383,6 +383,7 @@ fn prune( keep_yearly: param["keep-yearly"].as_u64(), }; + // We use a WorkerTask just to have a task log, but run synchrounously let worker = WorkerTask::new("prune", Some(store.to_owned()), "root@pam", true)?; let result = try_block! { if !prune_options.keeps_something() { @@ -416,7 +417,7 @@ fn prune( bail!("prune failed - {}", err); } - Ok(json!(null)) + Ok(json!(worker.to_string())) // return the UPID } #[sortable] diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index b607fbcf..37921e38 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -1124,6 +1124,49 @@ fn upload_log( }) } +fn display_task_log( + client: HttpClient, + upid_str: &str, +) -> Result<(), Error> { + println!("TESTLOG {}", upid_str); + + let path = format!("api2/json/nodes/localhost/tasks/{}/log", upid_str); + + let mut start = 1; + let limit = 500; + + loop { + let param = json!({ "start": start, "limit": limit, "test-status": true }); + let result = async_main(async { client.get(&path, Some(param)).await })?; + + let active = result["active"].as_bool().unwrap(); + let total = result["total"].as_u64().unwrap(); + let data = result["data"].as_array().unwrap(); + + let lines = data.len(); + + for item in data { + let n = item["n"].as_u64().unwrap(); + let t = item["t"].as_str().unwrap(); + if n != start { bail!("got wrong line number in response data ({} != {}", n, start); } + start += 1; + println!("{}", t); + } + + if start > total { + if active { + std::thread::sleep(std::time::Duration::from_millis(1000)); + } else { + break; + } + } else { + if lines != limit { bail!("got wrong number of lines from server ({} != {})", lines, limit); } + } + } + + Ok(()) +} + fn prune( mut param: Value, _info: &ApiMethod, @@ -1138,33 +1181,48 @@ fn prune( let group = tools::required_string_param(¶m, "group")?; let group = BackupGroup::parse(group)?; + let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); let dry_run = param["dry-run"].as_bool().unwrap_or(false); param.as_object_mut().unwrap().remove("repository"); param.as_object_mut().unwrap().remove("group"); param.as_object_mut().unwrap().remove("dry-run"); + param.as_object_mut().unwrap().remove("output-format"); param["backup-type"] = group.backup_type().into(); param["backup-id"] = group.backup_id().into(); if dry_run { - let result = async_main(async move { client.get(&path, Some(param)).await })?; + let result = async_main(async { client.get(&path, Some(param)).await })?; let data = &result["data"]; - for item in data.as_array().unwrap() { - let timestamp = item["backup-time"].as_i64().unwrap(); - let timestamp = BackupDir::backup_time_to_string(Utc.timestamp(timestamp, 0)); - let keep = item["keep"].as_bool().unwrap(); - println!("{}/{}/{} {}", - group.backup_type(), - group.backup_id(), - timestamp, - if keep { "keep" } else { "remove" }, - ); + if output_format == "text" { + for item in data.as_array().unwrap() { + let timestamp = item["backup-time"].as_i64().unwrap(); + let timestamp = BackupDir::backup_time_to_string(Utc.timestamp(timestamp, 0)); + let keep = item["keep"].as_bool().unwrap(); + println!("{}/{}/{} {}", + group.backup_type(), + group.backup_id(), + timestamp, + if keep { "keep" } else { "remove" }, + ); + } + } else { + format_and_print_result(&data, &output_format); } } else { - let _result = async_main(async move { client.post(&path, Some(param)).await })?; + let result = async_main(async { client.post(&path, Some(param)).await })?; + let data = &result["data"]; + if output_format == "text" { + if let Some(upid) = data.as_str() { + println!("UPID {:?}", data); + display_task_log(client, upid)?; + } + } else { + format_and_print_result(&data, &output_format); + } } record_repository(&repo); @@ -2157,6 +2215,7 @@ We do not extraxt '.pxar' archives when writing to stdandard output. .schema()), ("group", false, &StringSchema::new("Backup group.").schema()), ], [ + ("output-format", true, &OUTPUT_FORMAT), ("repository", true, &REPO_URL_SCHEMA), ]) )