]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/admin/sync.rs
2d8810dca36a36b5f7a710d069740a060ba9f645
[proxmox-backup.git] / src / api2 / admin / sync.rs
1 use anyhow::{format_err, Error};
2 use serde_json::Value;
3
4 use proxmox::api::{api, ApiMethod, Router, RpcEnvironment};
5 use proxmox::api::router::SubdirMap;
6 use proxmox::{list_subdirs_api_method, sortable};
7
8 use crate::api2::types::*;
9 use crate::api2::pull::{get_pull_parameters};
10 use crate::config::sync::{self, SyncJobStatus, SyncJobConfig};
11 use crate::server::UPID;
12 use crate::config::jobstate::JobState;
13 use crate::tools::systemd::time::{
14 parse_calendar_event, compute_next_event};
15
16 #[api(
17 input: {
18 properties: {},
19 },
20 returns: {
21 description: "List configured jobs and their status.",
22 type: Array,
23 items: { type: sync::SyncJobStatus },
24 },
25 )]
26 /// List all sync jobs
27 pub fn list_sync_jobs(
28 _param: Value,
29 mut rpcenv: &mut dyn RpcEnvironment,
30 ) -> Result<Vec<SyncJobStatus>, Error> {
31
32 let (config, digest) = sync::config()?;
33
34 let mut list: Vec<SyncJobStatus> = config.convert_to_typed_array("sync")?;
35
36 for job in &mut list {
37 let last_state = JobState::load("syncjob", &job.id)
38 .map_err(|err| format_err!("could not open statefile for {}: {}", &job.id, err))?;
39 let (upid, endtime, state, starttime) = match last_state {
40 JobState::Created { time } => (None, None, None, time),
41 JobState::Started { upid } => {
42 let parsed_upid: UPID = upid.parse()?;
43 (Some(upid), None, None, parsed_upid.starttime)
44 },
45 JobState::Finished { upid, endtime, state } => {
46 let parsed_upid: UPID = upid.parse()?;
47 (Some(upid), Some(endtime), Some(state.to_string()), parsed_upid.starttime)
48 },
49 };
50
51 job.last_run_upid = upid;
52 job.last_run_state = state;
53 job.last_run_endtime = endtime;
54
55 let last = job.last_run_endtime.unwrap_or_else(|| starttime);
56
57 job.next_run = (|| -> Option<i64> {
58 let schedule = job.schedule.as_ref()?;
59 let event = parse_calendar_event(&schedule).ok()?;
60 compute_next_event(&event, last, false).ok()
61 })();
62 }
63
64 rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
65
66 Ok(list)
67 }
68
69 #[api(
70 input: {
71 properties: {
72 id: {
73 schema: JOB_ID_SCHEMA,
74 }
75 }
76 }
77 )]
78 /// Runs the sync jobs manually.
79 async fn run_sync_job(
80 id: String,
81 _info: &ApiMethod,
82 rpcenv: &mut dyn RpcEnvironment,
83 ) -> Result<String, Error> {
84
85 let (config, _digest) = sync::config()?;
86 let sync_job: SyncJobConfig = config.lookup("sync", &id)?;
87
88 let userid: Userid = rpcenv.get_user().unwrap().parse()?;
89
90 let delete = sync_job.remove_vanished.unwrap_or(true);
91 let (client, src_repo, tgt_store) = get_pull_parameters(&sync_job.store, &sync_job.remote, &sync_job.remote_store).await?;
92
93 let upid_str = WorkerTask::spawn("syncjob", Some(id.clone()), userid, false, move |worker| async move {
94
95 worker.log(format!("sync job '{}' start", &id));
96
97 crate::client::pull::pull_store(
98 &worker,
99 &client,
100 &src_repo,
101 tgt_store.clone(),
102 delete,
103 Userid::backup_userid().clone(),
104 ).await?;
105
106 worker.log(format!("sync job '{}' end", &id));
107
108 Ok(())
109 })?;
110
111 Ok(upid_str)
112 }
113
114 #[sortable]
115 const SYNC_INFO_SUBDIRS: SubdirMap = &[
116 (
117 "run",
118 &Router::new()
119 .post(&API_METHOD_RUN_SYNC_JOB)
120 ),
121 ];
122
123 const SYNC_INFO_ROUTER: Router = Router::new()
124 .get(&list_subdirs_api_method!(SYNC_INFO_SUBDIRS))
125 .subdirs(SYNC_INFO_SUBDIRS);
126
127
128 pub const ROUTER: Router = Router::new()
129 .get(&API_METHOD_LIST_SYNC_JOBS)
130 .match_all("id", &SYNC_INFO_ROUTER);