From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id CA2C21FF179 for ; Wed, 12 Nov 2025 10:42:29 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 16EAD1C725; Wed, 12 Nov 2025 10:43:17 +0100 (CET) From: Lukas Wagner To: pdm-devel@lists.proxmox.com Date: Wed, 12 Nov 2025 10:42:00 +0100 Message-ID: <20251112094203.112452-5-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251112094203.112452-1-l.wagner@proxmox.com> References: <20251112094203.112452-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1762940506198 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.029 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [PATCH datacenter-manager 4/7] api: pbs tasks: add PBS task API X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" Mostly based off of the PVE task API with the necessary changes for PBS. Signed-off-by: Lukas Wagner --- server/src/api/mod.rs | 22 ++++- server/src/api/pbs/mod.rs | 4 +- server/src/api/pbs/tasks.rs | 170 ++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 server/src/api/pbs/tasks.rs diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 6a7a65a2..119d57b7 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -1,6 +1,7 @@ //! Common API endpoints -use anyhow::Error; +use anyhow::{bail, Error}; +use pdm_api_types::{remotes::RemoteType, RemoteUpid}; use serde_json::{json, Value}; use proxmox_router::{list_subdirs_api_method, Permission, Router, SubdirMap}; @@ -67,3 +68,22 @@ fn version() -> Result { "repoid": pdm_buildcfg::PROXMOX_PKG_REPOID })) } + +/// Check a [`RemoteUpid`] matches the expected remote name and type. +pub(crate) fn verify_upid( + remote: &str, + remote_type: RemoteType, + upid: &RemoteUpid, +) -> Result<(), Error> { + if upid.remote() != remote { + bail!( + "remote '{remote}' does not match remote in upid ('{}')", + upid.remote() + ); + } + if upid.remote_type() != remote_type { + bail!("upid does not belong to a {remote_type} remote"); + } + + Ok(()) +} diff --git a/server/src/api/pbs/mod.rs b/server/src/api/pbs/mod.rs index a37fadb5..15e42727 100644 --- a/server/src/api/pbs/mod.rs +++ b/server/src/api/pbs/mod.rs @@ -22,6 +22,7 @@ use crate::remote_tasks; mod node; mod rrddata; +pub mod tasks; pub const ROUTER: Router = Router::new() .get(&list_subdirs_api_method!(SUBDIRS)) @@ -49,7 +50,8 @@ const REMOTE_SUBDIRS: SubdirMap = &sorted!([ ("nodes", &NODES_ROUTER), ("status", &Router::new().get(&API_METHOD_GET_STATUS)), ("rrddata", &rrddata::PBS_NODE_RRD_ROUTER), - ("datastore", &DATASTORE_ROUTER) + ("datastore", &DATASTORE_ROUTER), + ("tasks", &tasks::ROUTER), ]); const DATASTORE_ROUTER: Router = Router::new() diff --git a/server/src/api/pbs/tasks.rs b/server/src/api/pbs/tasks.rs new file mode 100644 index 00000000..5c86bff8 --- /dev/null +++ b/server/src/api/pbs/tasks.rs @@ -0,0 +1,170 @@ +//! Access to PBS tasks. + +use anyhow::Error; + +use proxmox_router::{list_subdirs_api_method, Permission, Router, RpcEnvironment, SubdirMap}; +use proxmox_schema::api; +use proxmox_sortable_macro::sortable; + +use pdm_api_types::remotes::{RemoteType, REMOTE_ID_SCHEMA}; +use pdm_api_types::{ + RemoteUpid, PRIV_RESOURCE_AUDIT, PRIV_RESOURCE_MANAGE, TASKLOG_DOWNLOAD_PARAM_SCHEMA, + TASKLOG_LIMIT_PARAM_SCHEMA, TASKLOG_START_PARAM_SCHEMA, +}; + +use crate::pbs_client; + +pub const ROUTER: Router = Router::new() + .get(&API_METHOD_LIST_TASKS) + .match_all("upid", &UPID_API_ROUTER); + +pub const UPID_API_ROUTER: Router = Router::new() + .get(&list_subdirs_api_method!(UPID_API_SUBDIRS)) + .delete(&API_METHOD_STOP_TASK) + .subdirs(UPID_API_SUBDIRS); + +#[sortable] +const UPID_API_SUBDIRS: SubdirMap = &sorted!([ + ("log", &Router::new().get(&API_METHOD_READ_TASK_LOG)), + ("status", &Router::new().get(&API_METHOD_GET_TASK_STATUS)), +]); + +#[api( + input: { + properties: { + remote: { schema: REMOTE_ID_SCHEMA }, + }, + }, + access: { + // FIXME: fine-grained task filtering? + permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_AUDIT, false), + }, + returns: { type: pve_api_types::TaskStatus }, +)] +/// Get the list of tasks either for a specific node, or query all at once. +async fn list_tasks(remote: String) -> Result, Error> { + let (remotes, _) = pdm_config::remotes::config()?; + let client = pbs_client::connect_to_remote(&remotes, &remote)?; + + Ok(client.get_task_list(Default::default()).await?) +} + +#[api( + input: { + properties: { + remote: { schema: REMOTE_ID_SCHEMA }, + upid: { type: RemoteUpid }, + }, + }, + access: { + // FIXME: fine-grained task filtering? + permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_MANAGE, false), + }, +)] +/// Get the status of a task from a Proxmox VE instance. +async fn stop_task(remote: String, upid: RemoteUpid) -> Result<(), Error> { + let (remotes, _) = pdm_config::remotes::config()?; + + crate::api::verify_upid(&remote, RemoteType::Pbs, &upid)?; + + let client = pbs_client::connect_to_remote(&remotes, upid.remote())?; + + Ok(client.stop_task(upid.upid()).await?) +} + +#[api( + input: { + properties: { + remote: { schema: REMOTE_ID_SCHEMA }, + upid: { type: RemoteUpid }, + wait: { + description: "wait for the task to finish before returning its result", + type: Boolean, + optional: true, + default: false, + }, + }, + }, + access: { + // FIXME: fine-grained task filtering? + permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_AUDIT, false), + }, + returns: { type: pve_api_types::TaskStatus }, +)] +/// Get the status of a task from a Proxmox VE instance. +pub async fn get_task_status( + remote: String, + upid: RemoteUpid, + wait: bool, +) -> Result { + let (remotes, _) = pdm_config::remotes::config()?; + + crate::api::verify_upid(&remote, RemoteType::Pbs, &upid)?; + + let client = pbs_client::connect_to_remote(&remotes, upid.remote())?; + + loop { + let status = client.get_task_status(upid.upid()).await?; + if !wait || !status.is_running() { + break Ok(status); + } + } +} + +// FIXME: make *actually* streaming with router support! +#[api( + input: { + properties: { + remote: { schema: REMOTE_ID_SCHEMA }, + upid: { type: RemoteUpid }, + start: { + schema: TASKLOG_START_PARAM_SCHEMA, + optional: true, + }, + limit: { + schema: TASKLOG_LIMIT_PARAM_SCHEMA, + optional: true, + }, + download: { + schema: TASKLOG_DOWNLOAD_PARAM_SCHEMA, + optional: true, + } + }, + }, + access: { + // FIXME: fine-grained task filtering? + permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_AUDIT, false), + }, + returns: { + type: Array, + items: { + type: pbs_client::TaskLogLine, + }, + description: "Array of task log lines", + }, +)] +/// Read a task log. +async fn read_task_log( + remote: String, + upid: RemoteUpid, + download: Option, + start: Option, + limit: Option, + rpcenv: &mut dyn RpcEnvironment, +) -> Result, Error> { + let (remotes, _) = pdm_config::remotes::config()?; + + crate::api::verify_upid(&remote, RemoteType::Pbs, &upid)?; + + let client = pbs_client::connect_to_remote(&remotes, &remote)?; + + let response = client + .get_task_log(upid.upid(), download, limit, start) + .await?; + + for (key, value) in response.attribs { + rpcenv[&key] = value; + } + + Ok(response.data) +} -- 2.47.3 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel