]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/admin/verify.rs
replace Userid with Authid
[proxmox-backup.git] / src / api2 / admin / verify.rs
CommitLineData
8d1beca7
HL
1use anyhow::{format_err, Error};
2
3use proxmox::api::router::SubdirMap;
4use proxmox::{list_subdirs_api_method, sortable};
5use proxmox::api::{api, ApiMethod, Router, RpcEnvironment};
6
7use crate::api2::types::*;
1298618a
DM
8use crate::server::do_verification_job;
9use crate::server::jobstate::{Job, JobState};
8d1beca7
HL
10use crate::config::verify;
11use crate::config::verify::{VerificationJobConfig, VerificationJobStatus};
12use serde_json::Value;
13use crate::tools::systemd::time::{parse_calendar_event, compute_next_event};
14use crate::server::UPID;
15
16#[api(
17 input: {
d58e6313
DC
18 properties: {
19 store: {
20 schema: DATASTORE_SCHEMA,
21 optional: true,
22 },
23 },
8d1beca7
HL
24 },
25 returns: {
26 description: "List configured jobs and their status.",
27 type: Array,
28 items: { type: verify::VerificationJobStatus },
29 },
30)]
31/// List all verification jobs
32pub fn list_verification_jobs(
d58e6313 33 store: Option<String>,
8d1beca7
HL
34 _param: Value,
35 mut rpcenv: &mut dyn RpcEnvironment,
36) -> Result<Vec<VerificationJobStatus>, Error> {
37
38 let (config, digest) = verify::config()?;
39
d58e6313
DC
40 let mut list: Vec<VerificationJobStatus> = config
41 .convert_to_typed_array("verification")?
42 .into_iter()
43 .filter(|job: &VerificationJobStatus| {
44 if let Some(store) = &store {
45 &job.store == store
46 } else {
47 true
48 }
49 }).collect();
8d1beca7
HL
50
51 for job in &mut list {
52 let last_state = JobState::load("verificationjob", &job.id)
53 .map_err(|err| format_err!("could not open statefile for {}: {}", &job.id, err))?;
54
55 let (upid, endtime, state, starttime) = match last_state {
56 JobState::Created { time } => (None, None, None, time),
57 JobState::Started { upid } => {
58 let parsed_upid: UPID = upid.parse()?;
59 (Some(upid), None, None, parsed_upid.starttime)
60 },
61 JobState::Finished { upid, state } => {
62 let parsed_upid: UPID = upid.parse()?;
63 (Some(upid), Some(state.endtime()), Some(state.to_string()), parsed_upid.starttime)
64 },
65 };
66
67 job.last_run_upid = upid;
68 job.last_run_state = state;
69 job.last_run_endtime = endtime;
70
71 let last = job.last_run_endtime.unwrap_or_else(|| starttime);
72
73 job.next_run = (|| -> Option<i64> {
74 let schedule = job.schedule.as_ref()?;
75 let event = parse_calendar_event(&schedule).ok()?;
76 // ignore errors
77 compute_next_event(&event, last, false).unwrap_or_else(|_| None)
78 })();
79 }
80
81 rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
82
83 Ok(list)
84}
85
86#[api(
87 input: {
88 properties: {
89 id: {
90 schema: JOB_ID_SCHEMA,
91 }
92 }
93 }
94)]
95/// Runs a verification job manually.
96fn run_verification_job(
97 id: String,
98 _info: &ApiMethod,
99 rpcenv: &mut dyn RpcEnvironment,
100) -> Result<String, Error> {
101 let (config, _digest) = verify::config()?;
102 let verification_job: VerificationJobConfig = config.lookup("verification", &id)?;
103
e6dc35ac 104 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
8d1beca7
HL
105
106 let job = Job::new("verificationjob", &id)?;
107
e6dc35ac 108 let upid_str = do_verification_job(job, verification_job, &auth_id, None)?;
8d1beca7
HL
109
110 Ok(upid_str)
111}
112
113#[sortable]
114const VERIFICATION_INFO_SUBDIRS: SubdirMap = &[("run", &Router::new().post(&API_METHOD_RUN_VERIFICATION_JOB))];
115
116const VERIFICATION_INFO_ROUTER: Router = Router::new()
117 .get(&list_subdirs_api_method!(VERIFICATION_INFO_SUBDIRS))
118 .subdirs(VERIFICATION_INFO_SUBDIRS);
119
120pub const ROUTER: Router = Router::new()
121 .get(&API_METHOD_LIST_VERIFICATION_JOBS)
122 .match_all("id", &VERIFICATION_INFO_ROUTER);