]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/admin/sync.rs
typo fixes all over the place
[proxmox-backup.git] / src / api2 / admin / sync.rs
CommitLineData
d1d74c43 1//! Datastore Synchronization Job Management
bf78f708 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
70842b9e
DM
10use crate::{
11 api2::{
12 types::{
13 DATASTORE_SCHEMA,
14 JOB_ID_SCHEMA,
15 Authid,
16 },
17 pull::do_sync_job,
18 config::sync::{
19 check_sync_job_modify_access,
20 check_sync_job_read_access,
21 },
22 },
23 config::{
24 cached_user_info::CachedUserInfo,
25 sync::{
26 self,
27 SyncJobStatus,
28 SyncJobConfig,
29 },
30 },
31 server::{
32 jobstate::{
33 Job,
34 JobState,
35 compute_schedule_status,
36 },
37 },
38};
d43f86f3
DC
39
40#[api(
41 input: {
d58e6313
DC
42 properties: {
43 store: {
44 schema: DATASTORE_SCHEMA,
45 optional: true,
46 },
47 },
d43f86f3
DC
48 },
49 returns: {
50 description: "List configured jobs and their status.",
51 type: Array,
70842b9e 52 items: { type: SyncJobStatus },
d43f86f3 53 },
59af9ca9
FG
54 access: {
55 description: "Limited to sync jobs where user has Datastore.Audit on target datastore, and Remote.Audit on source remote.",
56 permission: &Permission::Anybody,
57 },
d43f86f3
DC
58)]
59/// List all sync jobs
60pub fn list_sync_jobs(
d58e6313 61 store: Option<String>,
d43f86f3
DC
62 _param: Value,
63 mut rpcenv: &mut dyn RpcEnvironment,
64) -> Result<Vec<SyncJobStatus>, Error> {
65
59af9ca9
FG
66 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
67 let user_info = CachedUserInfo::new()?;
68
d43f86f3
DC
69 let (config, digest) = sync::config()?;
70
70842b9e 71 let job_config_iter = config
d58e6313
DC
72 .convert_to_typed_array("sync")?
73 .into_iter()
70842b9e 74 .filter(|job: &SyncJobConfig| {
d58e6313
DC
75 if let Some(store) = &store {
76 &job.store == store
77 } else {
78 true
79 }
59af9ca9 80 })
70842b9e
DM
81 .filter(|job: &SyncJobConfig| {
82 check_sync_job_read_access(&user_info, &auth_id, &job)
83 });
84
85 let mut list = Vec::new();
d43f86f3 86
70842b9e 87 for job in job_config_iter {
664d8a27
DC
88 let last_state = JobState::load("syncjob", &job.id)
89 .map_err(|err| format_err!("could not open statefile for {}: {}", &job.id, err))?;
664d8a27 90
70842b9e 91 let status = compute_schedule_status(&last_state, job.schedule.as_deref())?;
8d785899 92
70842b9e 93 list.push(SyncJobStatus { config: job, status });
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);