]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/admin/sync.rs
improve code docs in api2
[proxmox-backup.git] / src / api2 / admin / sync.rs
CommitLineData
bf78f708
DM
1//! Datastore Syncronization Job Management
2
59af9ca9 3use anyhow::{bail, format_err, Error};
d43f86f3 4use serde_json::Value;
d43f86f3 5
59af9ca9 6use proxmox::api::{api, ApiMethod, Permission, Router, RpcEnvironment};
d43f86f3
DC
7use proxmox::api::router::SubdirMap;
8use proxmox::{list_subdirs_api_method, sortable};
9
10use crate::api2::types::*;
42b68f72 11use crate::api2::pull::do_sync_job;
59af9ca9
FG
12use crate::api2::config::sync::{check_sync_job_modify_access, check_sync_job_read_access};
13
14use crate::config::cached_user_info::CachedUserInfo;
d43f86f3 15use crate::config::sync::{self, SyncJobStatus, SyncJobConfig};
664d8a27 16use crate::server::UPID;
1298618a 17use crate::server::jobstate::{Job, JobState};
d43f86f3
DC
18use crate::tools::systemd::time::{
19 parse_calendar_event, compute_next_event};
20
21#[api(
22 input: {
d58e6313
DC
23 properties: {
24 store: {
25 schema: DATASTORE_SCHEMA,
26 optional: true,
27 },
28 },
d43f86f3
DC
29 },
30 returns: {
31 description: "List configured jobs and their status.",
32 type: Array,
33 items: { type: sync::SyncJobStatus },
34 },
59af9ca9
FG
35 access: {
36 description: "Limited to sync jobs where user has Datastore.Audit on target datastore, and Remote.Audit on source remote.",
37 permission: &Permission::Anybody,
38 },
d43f86f3
DC
39)]
40/// List all sync jobs
41pub fn list_sync_jobs(
d58e6313 42 store: Option<String>,
d43f86f3
DC
43 _param: Value,
44 mut rpcenv: &mut dyn RpcEnvironment,
45) -> Result<Vec<SyncJobStatus>, Error> {
46
59af9ca9
FG
47 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
48 let user_info = CachedUserInfo::new()?;
49
d43f86f3
DC
50 let (config, digest) = sync::config()?;
51
d58e6313
DC
52 let mut list: Vec<SyncJobStatus> = config
53 .convert_to_typed_array("sync")?
54 .into_iter()
55 .filter(|job: &SyncJobStatus| {
56 if let Some(store) = &store {
57 &job.store == store
58 } else {
59 true
60 }
59af9ca9
FG
61 })
62 .filter(|job: &SyncJobStatus| {
44288184 63 let as_config: SyncJobConfig = job.into();
59af9ca9 64 check_sync_job_read_access(&user_info, &auth_id, &as_config)
d58e6313 65 }).collect();
d43f86f3 66
664d8a27
DC
67 for job in &mut list {
68 let last_state = JobState::load("syncjob", &job.id)
69 .map_err(|err| format_err!("could not open statefile for {}: {}", &job.id, err))?;
70 let (upid, endtime, state, starttime) = match last_state {
71 JobState::Created { time } => (None, None, None, time),
72 JobState::Started { upid } => {
73 let parsed_upid: UPID = upid.parse()?;
74 (Some(upid), None, None, parsed_upid.starttime)
75 },
77bd2a46 76 JobState::Finished { upid, state } => {
664d8a27 77 let parsed_upid: UPID = upid.parse()?;
77bd2a46 78 (Some(upid), Some(state.endtime()), Some(state.to_string()), parsed_upid.starttime)
664d8a27 79 },
d43f86f3 80 };
d43f86f3 81
664d8a27
DC
82 job.last_run_upid = upid;
83 job.last_run_state = state;
84 job.last_run_endtime = endtime;
85
22a9189e 86 let last = job.last_run_endtime.unwrap_or(starttime);
8d785899
DC
87
88 job.next_run = (|| -> Option<i64> {
89 let schedule = job.schedule.as_ref()?;
90 let event = parse_calendar_event(&schedule).ok()?;
15ec790a 91 // ignore errors
22a9189e 92 compute_next_event(&event, last, false).unwrap_or(None)
8d785899 93 })();
d43f86f3
DC
94 }
95
96 rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
97
98 Ok(list)
99}
100
101#[api(
102 input: {
103 properties: {
104 id: {
105 schema: JOB_ID_SCHEMA,
106 }
107 }
59af9ca9
FG
108 },
109 access: {
110 description: "User needs Datastore.Backup on target datastore, and Remote.Read on source remote. Additionally, remove_vanished requires Datastore.Prune, and any owner other than the user themselves requires Datastore.Modify",
111 permission: &Permission::Anybody,
112 },
d43f86f3
DC
113)]
114/// Runs the sync jobs manually.
bf78f708 115pub fn run_sync_job(
d43f86f3
DC
116 id: String,
117 _info: &ApiMethod,
118 rpcenv: &mut dyn RpcEnvironment,
119) -> Result<String, Error> {
59af9ca9
FG
120 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
121 let user_info = CachedUserInfo::new()?;
d43f86f3
DC
122
123 let (config, _digest) = sync::config()?;
124 let sync_job: SyncJobConfig = config.lookup("sync", &id)?;
125
59af9ca9
FG
126 if !check_sync_job_modify_access(&user_info, &auth_id, &sync_job) {
127 bail!("permission check failed");
128 }
d43f86f3 129
93bb51fe 130 let job = Job::new("syncjob", &id)?;
02543a5c 131
e6dc35ac 132 let upid_str = do_sync_job(job, sync_job, &auth_id, None)?;
d43f86f3
DC
133
134 Ok(upid_str)
135}
136
137#[sortable]
138const SYNC_INFO_SUBDIRS: SubdirMap = &[
139 (
140 "run",
141 &Router::new()
142 .post(&API_METHOD_RUN_SYNC_JOB)
143 ),
144];
145
146const SYNC_INFO_ROUTER: Router = Router::new()
147 .get(&list_subdirs_api_method!(SYNC_INFO_SUBDIRS))
148 .subdirs(SYNC_INFO_SUBDIRS);
149
150
151pub const ROUTER: Router = Router::new()
152 .get(&API_METHOD_LIST_SYNC_JOBS)
153 .match_all("id", &SYNC_INFO_ROUTER);