From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [RFC proxmox-backup 6/8] api: admin: add sanity check job api endpoints
Date: Wed, 13 Dec 2023 16:38:17 +0100 [thread overview]
Message-ID: <20231213153819.391392-7-c.ebner@proxmox.com> (raw)
In-Reply-To: <20231213153819.391392-1-c.ebner@proxmox.com>
Implements the api endpoints required to perform administration tasks
for sanity check jobs.
Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
src/api2/admin/mod.rs | 2 +
src/api2/admin/sanity_check.rs | 111 +++++++++++++++++++++++++++++++++
2 files changed, 113 insertions(+)
create mode 100644 src/api2/admin/sanity_check.rs
diff --git a/src/api2/admin/mod.rs b/src/api2/admin/mod.rs
index 168dc038..8577fc56 100644
--- a/src/api2/admin/mod.rs
+++ b/src/api2/admin/mod.rs
@@ -8,6 +8,7 @@ pub mod datastore;
pub mod metrics;
pub mod namespace;
pub mod prune;
+pub mod sanity_check;
pub mod sync;
pub mod traffic_control;
pub mod verify;
@@ -17,6 +18,7 @@ const SUBDIRS: SubdirMap = &sorted!([
("datastore", &datastore::ROUTER),
("metrics", &metrics::ROUTER),
("prune", &prune::ROUTER),
+ ("sanity-check", &sanity_check::ROUTER),
("sync", &sync::ROUTER),
("traffic-control", &traffic_control::ROUTER),
("verify", &verify::ROUTER),
diff --git a/src/api2/admin/sanity_check.rs b/src/api2/admin/sanity_check.rs
new file mode 100644
index 00000000..990ce6f2
--- /dev/null
+++ b/src/api2/admin/sanity_check.rs
@@ -0,0 +1,111 @@
+//! Sanity Check Job Management
+
+use anyhow::{format_err, Error};
+use serde_json::Value;
+
+use proxmox_router::{
+ list_subdirs_api_method, ApiMethod, Permission, Router, RpcEnvironment, SubdirMap,
+};
+use proxmox_schema::api;
+use proxmox_sortable_macro::sortable;
+
+use pbs_api_types::{
+ Authid, SanityCheckJobConfig, SanityCheckJobStatus, JOB_ID_SCHEMA, PRIV_SYS_AUDIT,
+ PRIV_SYS_MODIFY,
+};
+use pbs_config::{sanity_check, CachedUserInfo};
+
+use crate::server::{
+ do_sanity_check_job,
+ jobstate::{compute_schedule_status, Job, JobState},
+};
+
+#[api(
+ returns: {
+ description: "List configured jobs and their status",
+ type: Array,
+ items: { type: SanityCheckJobStatus },
+ },
+ access: {
+ permission: &Permission::Anybody,
+ description: "Requires Sys.Audit or Sys.Modify.",
+ },
+)]
+/// List all sanity check jobs
+pub fn list_sanity_check_jobs(
+ _param: Value,
+ rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Vec<SanityCheckJobStatus>, Error> {
+ let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+ let user_info = CachedUserInfo::new()?;
+ user_info.check_privs(&auth_id, &["/"], PRIV_SYS_AUDIT | PRIV_SYS_MODIFY, true)?;
+
+ let (config, digest) = sanity_check::config()?;
+ let sanity_check_job_config: Vec<SanityCheckJobConfig> =
+ config.convert_to_typed_array("sanity-check")?;
+
+ let mut list = Vec::new();
+ for job in sanity_check_job_config {
+ let last_state = JobState::load("sanitycheckjob", &job.id)
+ .map_err(|err| format_err!("could not open statefile for {}: {err}", &job.id))?;
+
+ let mut status = compute_schedule_status(&last_state, Some(&job.schedule))?;
+ if job.disable {
+ status.next_run = None;
+ }
+
+ list.push(SanityCheckJobStatus {
+ config: job,
+ status,
+ });
+ }
+
+ rpcenv["digest"] = hex::encode(digest).into();
+
+ Ok(list)
+}
+
+#[api(
+ input: {
+ properties: {
+ id: {
+ schema: JOB_ID_SCHEMA,
+ }
+ }
+ },
+ access: {
+ permission: &Permission::Anybody,
+ description: "Requires Sys.Modify.",
+ },
+)]
+/// Runs a sanity check job manually.
+pub fn run_sanity_check_job(
+ id: String,
+ _info: &ApiMethod,
+ rpcenv: &mut dyn RpcEnvironment,
+) -> Result<String, Error> {
+ let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+ let user_info = CachedUserInfo::new()?;
+ user_info.check_privs(&auth_id, &["/"], PRIV_SYS_MODIFY, true)?;
+
+ let (config, _digest) = sanity_check::config()?;
+ let sanity_check_job: SanityCheckJobConfig = config.lookup("sanity-check", &id)?;
+
+ let job = Job::new("sanitycheckjob", &id)?;
+
+ let upid_str = do_sanity_check_job(job, sanity_check_job.options, &auth_id, None)?;
+
+ Ok(upid_str)
+}
+
+#[sortable]
+const SANITY_CHECK_INFO_SUBDIRS: SubdirMap =
+ &[("run", &Router::new().post(&API_METHOD_RUN_SANITY_CHECK_JOB))];
+
+const SANITY_CHECK_INFO_ROUTER: Router = Router::new()
+ .get(&list_subdirs_api_method!(SANITY_CHECK_INFO_SUBDIRS))
+ .subdirs(SANITY_CHECK_INFO_SUBDIRS);
+
+pub const ROUTER: Router = Router::new()
+ .get(&API_METHOD_LIST_SANITY_CHECK_JOBS)
+ .match_all("id", &SANITY_CHECK_INFO_ROUTER);
--
2.39.2
next prev parent reply other threads:[~2023-12-13 15:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-13 15:38 [pbs-devel] [RFC proxmox-backup 0/8] implement sanity check jobs Christian Ebner
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 1/8] api-types: jobs: add sanity checks job types Christian Ebner
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 2/8] config: implement sanity check job configuration Christian Ebner
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 3/8] api: config: sanity check jobs api endpoints Christian Ebner
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 4/8] server: add sanity check job email notifications Christian Ebner
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 5/8] server: implement sanity check job Christian Ebner
2023-12-13 15:38 ` Christian Ebner [this message]
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 7/8] manager: add sanity check jobs management cli commands Christian Ebner
2023-12-13 15:38 ` [pbs-devel] [RFC proxmox-backup 8/8] proxy: add sanity check task to scheduler Christian Ebner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231213153819.391392-7-c.ebner@proxmox.com \
--to=c.ebner@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.