1 //! Datastore Verify Job Management
3 use anyhow
::{format_err, Error}
;
6 use proxmox
::api
::router
::SubdirMap
;
7 use proxmox
::{list_subdirs_api_method, sortable}
;
8 use proxmox
::api
::{api, ApiMethod, Permission, Router, RpcEnvironment}
;
10 use pbs_api_types
::{VerificationJobConfig, VerificationJobStatus, JOB_ID_SCHEMA, Authid}
;
21 compute_schedule_status
,
28 PRIV_DATASTORE_VERIFY
,
30 cached_user_info
::CachedUserInfo
,
39 schema
: DATASTORE_SCHEMA
,
45 description
: "List configured jobs and their status (filtered by access)",
47 items
: { type: VerificationJobStatus }
,
50 permission
: &Permission
::Anybody
,
51 description
: "Requires Datastore.Audit or Datastore.Verify on datastore.",
54 /// List all verification jobs
55 pub fn list_verification_jobs(
56 store
: Option
<String
>,
58 mut rpcenv
: &mut dyn RpcEnvironment
,
59 ) -> Result
<Vec
<VerificationJobStatus
>, Error
> {
60 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
61 let user_info
= CachedUserInfo
::new()?
;
63 let required_privs
= PRIV_DATASTORE_AUDIT
| PRIV_DATASTORE_VERIFY
;
65 let (config
, digest
) = verify
::config()?
;
67 let job_config_iter
= config
68 .convert_to_typed_array("verification")?
70 .filter(|job
: &VerificationJobConfig
| {
71 let privs
= user_info
.lookup_privs(&auth_id
, &["datastore", &job
.store
]);
72 if privs
& required_privs
== 0 {
76 if let Some(store
) = &store
{
83 let mut list
= Vec
::new();
85 for job
in job_config_iter
{
86 let last_state
= JobState
::load("verificationjob", &job
.id
)
87 .map_err(|err
| format_err
!("could not open statefile for {}: {}", &job
.id
, err
))?
;
89 let status
= compute_schedule_status(&last_state
, job
.schedule
.as_deref())?
;
91 list
.push(VerificationJobStatus { config: job, status }
);
94 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
103 schema
: JOB_ID_SCHEMA
,
108 permission
: &Permission
::Anybody
,
109 description
: "Requires Datastore.Verify on job's datastore.",
112 /// Runs a verification job manually.
113 pub fn run_verification_job(
116 rpcenv
: &mut dyn RpcEnvironment
,
117 ) -> Result
<String
, Error
> {
118 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
119 let user_info
= CachedUserInfo
::new()?
;
121 let (config
, _digest
) = verify
::config()?
;
122 let verification_job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
124 user_info
.check_privs(&auth_id
, &["datastore", &verification_job
.store
], PRIV_DATASTORE_VERIFY
, true)?
;
126 let job
= Job
::new("verificationjob", &id
)?
;
128 let upid_str
= do_verification_job(job
, verification_job
, &auth_id
, None
)?
;
134 const VERIFICATION_INFO_SUBDIRS
: SubdirMap
= &[("run", &Router
::new().post(&API_METHOD_RUN_VERIFICATION_JOB
))];
136 const VERIFICATION_INFO_ROUTER
: Router
= Router
::new()
137 .get(&list_subdirs_api_method
!(VERIFICATION_INFO_SUBDIRS
))
138 .subdirs(VERIFICATION_INFO_SUBDIRS
);
140 pub const ROUTER
: Router
= Router
::new()
141 .get(&API_METHOD_LIST_VERIFICATION_JOBS
)
142 .match_all("id", &VERIFICATION_INFO_ROUTER
);