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}
;
11 VerificationJobConfig
, VerificationJobStatus
, JOB_ID_SCHEMA
, Authid
,
12 PRIV_DATASTORE_AUDIT
, PRIV_DATASTORE_VERIFY
,
14 use pbs_config
::verify
;
25 compute_schedule_status
,
28 config
::cached_user_info
::CachedUserInfo
,
35 schema
: DATASTORE_SCHEMA
,
41 description
: "List configured jobs and their status (filtered by access)",
43 items
: { type: VerificationJobStatus }
,
46 permission
: &Permission
::Anybody
,
47 description
: "Requires Datastore.Audit or Datastore.Verify on datastore.",
50 /// List all verification jobs
51 pub fn list_verification_jobs(
52 store
: Option
<String
>,
54 mut rpcenv
: &mut dyn RpcEnvironment
,
55 ) -> Result
<Vec
<VerificationJobStatus
>, Error
> {
56 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
57 let user_info
= CachedUserInfo
::new()?
;
59 let required_privs
= PRIV_DATASTORE_AUDIT
| PRIV_DATASTORE_VERIFY
;
61 let (config
, digest
) = verify
::config()?
;
63 let job_config_iter
= config
64 .convert_to_typed_array("verification")?
66 .filter(|job
: &VerificationJobConfig
| {
67 let privs
= user_info
.lookup_privs(&auth_id
, &["datastore", &job
.store
]);
68 if privs
& required_privs
== 0 {
72 if let Some(store
) = &store
{
79 let mut list
= Vec
::new();
81 for job
in job_config_iter
{
82 let last_state
= JobState
::load("verificationjob", &job
.id
)
83 .map_err(|err
| format_err
!("could not open statefile for {}: {}", &job
.id
, err
))?
;
85 let status
= compute_schedule_status(&last_state
, job
.schedule
.as_deref())?
;
87 list
.push(VerificationJobStatus { config: job, status }
);
90 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
99 schema
: JOB_ID_SCHEMA
,
104 permission
: &Permission
::Anybody
,
105 description
: "Requires Datastore.Verify on job's datastore.",
108 /// Runs a verification job manually.
109 pub fn run_verification_job(
112 rpcenv
: &mut dyn RpcEnvironment
,
113 ) -> Result
<String
, Error
> {
114 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
115 let user_info
= CachedUserInfo
::new()?
;
117 let (config
, _digest
) = verify
::config()?
;
118 let verification_job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
120 user_info
.check_privs(&auth_id
, &["datastore", &verification_job
.store
], PRIV_DATASTORE_VERIFY
, true)?
;
122 let job
= Job
::new("verificationjob", &id
)?
;
124 let upid_str
= do_verification_job(job
, verification_job
, &auth_id
, None
)?
;
130 const VERIFICATION_INFO_SUBDIRS
: SubdirMap
= &[("run", &Router
::new().post(&API_METHOD_RUN_VERIFICATION_JOB
))];
132 const VERIFICATION_INFO_ROUTER
: Router
= Router
::new()
133 .get(&list_subdirs_api_method
!(VERIFICATION_INFO_SUBDIRS
))
134 .subdirs(VERIFICATION_INFO_SUBDIRS
);
136 pub const ROUTER
: Router
= Router
::new()
137 .get(&API_METHOD_LIST_VERIFICATION_JOBS
)
138 .match_all("id", &VERIFICATION_INFO_ROUTER
);