]>
Commit | Line | Data |
---|---|---|
a2479cfa | 1 | use std::path::PathBuf; |
6ce50400 | 2 | |
f7d4e4b5 | 3 | use anyhow::{bail, Error}; |
5e62d19c | 4 | use serde_json::Value; |
0a00f6e0 | 5 | use ::serde::{Deserialize, Serialize}; |
6ce50400 | 6 | |
67f7ffd0 | 7 | use proxmox::api::{api, Router, RpcEnvironment, Permission}; |
98c259b4 | 8 | use proxmox::tools::fs::open_file_locked; |
a2479cfa | 9 | |
66c49c21 | 10 | use crate::api2::types::*; |
a2479cfa | 11 | use crate::backup::*; |
b93bbab4 | 12 | use crate::config::cached_user_info::CachedUserInfo; |
67f7ffd0 | 13 | use crate::config::datastore::{self, DataStoreConfig, DIR_NAME_SCHEMA}; |
41bfd249 | 14 | use crate::config::acl::{PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY}; |
1298618a | 15 | use crate::server::jobstate; |
567713b4 | 16 | |
688fbe07 DM |
17 | #[api( |
18 | input: { | |
19 | properties: {}, | |
20 | }, | |
21 | returns: { | |
f3ec5dae | 22 | description: "List the configured datastores (with config digest).", |
688fbe07 | 23 | type: Array, |
67f7ffd0 | 24 | items: { type: datastore::DataStoreConfig }, |
688fbe07 | 25 | }, |
c0ef209a | 26 | access: { |
b93bbab4 | 27 | permission: &Permission::Anybody, |
c0ef209a | 28 | }, |
688fbe07 DM |
29 | )] |
30 | /// List all datastores | |
31 | pub fn list_datastores( | |
6049b71f | 32 | _param: Value, |
67f7ffd0 DM |
33 | mut rpcenv: &mut dyn RpcEnvironment, |
34 | ) -> Result<Vec<DataStoreConfig>, Error> { | |
567713b4 | 35 | |
d0187a51 | 36 | let (config, digest) = datastore::config()?; |
b65eaac6 | 37 | |
e6dc35ac | 38 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
b93bbab4 | 39 | let user_info = CachedUserInfo::new()?; |
67f7ffd0 DM |
40 | |
41 | rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); | |
42 | ||
b93bbab4 FG |
43 | let list:Vec<DataStoreConfig> = config.convert_to_typed_array("datastore")?; |
44 | let filter_by_privs = |store: &DataStoreConfig| { | |
e6dc35ac | 45 | let user_privs = user_info.lookup_privs(&auth_id, &["datastore", &store.name]); |
b93bbab4 FG |
46 | (user_privs & PRIV_DATASTORE_AUDIT) != 0 |
47 | }; | |
48 | ||
49 | Ok(list.into_iter().filter(filter_by_privs).collect()) | |
ea0b8b6e DM |
50 | } |
51 | ||
67f7ffd0 DM |
52 | |
53 | // fixme: impl. const fn get_object_schema(datastore::DataStoreConfig::API_SCHEMA), | |
54 | // but this need support for match inside const fn | |
55 | // see: https://github.com/rust-lang/rust/issues/49146 | |
56 | ||
688fbe07 DM |
57 | #[api( |
58 | protected: true, | |
59 | input: { | |
60 | properties: { | |
61 | name: { | |
62 | schema: DATASTORE_SCHEMA, | |
63 | }, | |
67f7ffd0 DM |
64 | path: { |
65 | schema: DIR_NAME_SCHEMA, | |
66 | }, | |
688fbe07 DM |
67 | comment: { |
68 | optional: true, | |
454c13ed | 69 | schema: SINGLE_LINE_COMMENT_SCHEMA, |
688fbe07 | 70 | }, |
42fdbe51 DM |
71 | "gc-schedule": { |
72 | optional: true, | |
73 | schema: GC_SCHEDULE_SCHEMA, | |
74 | }, | |
67f7ffd0 DM |
75 | "prune-schedule": { |
76 | optional: true, | |
77 | schema: PRUNE_SCHEDULE_SCHEMA, | |
78 | }, | |
79 | "keep-last": { | |
80 | optional: true, | |
81 | schema: PRUNE_SCHEMA_KEEP_LAST, | |
82 | }, | |
83 | "keep-hourly": { | |
84 | optional: true, | |
85 | schema: PRUNE_SCHEMA_KEEP_HOURLY, | |
86 | }, | |
87 | "keep-daily": { | |
88 | optional: true, | |
89 | schema: PRUNE_SCHEMA_KEEP_DAILY, | |
90 | }, | |
91 | "keep-weekly": { | |
92 | optional: true, | |
93 | schema: PRUNE_SCHEMA_KEEP_WEEKLY, | |
94 | }, | |
95 | "keep-monthly": { | |
96 | optional: true, | |
97 | schema: PRUNE_SCHEMA_KEEP_MONTHLY, | |
98 | }, | |
99 | "keep-yearly": { | |
100 | optional: true, | |
101 | schema: PRUNE_SCHEMA_KEEP_YEARLY, | |
688fbe07 DM |
102 | }, |
103 | }, | |
104 | }, | |
c0ef209a | 105 | access: { |
41bfd249 | 106 | permission: &Permission::Privilege(&["datastore"], PRIV_DATASTORE_ALLOCATE, false), |
c0ef209a | 107 | }, |
688fbe07 DM |
108 | )] |
109 | /// Create new datastore config. | |
67f7ffd0 | 110 | pub fn create_datastore(param: Value) -> Result<(), Error> { |
ea0b8b6e | 111 | |
b56c111e | 112 | let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?; |
652c1190 | 113 | |
688fbe07 | 114 | let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?; |
652c1190 | 115 | |
d0187a51 | 116 | let (mut config, _digest) = datastore::config()?; |
652c1190 | 117 | |
67f7ffd0 DM |
118 | if let Some(_) = config.sections.get(&datastore.name) { |
119 | bail!("datastore '{}' already exists.", datastore.name); | |
652c1190 DM |
120 | } |
121 | ||
688fbe07 DM |
122 | let path: PathBuf = datastore.path.clone().into(); |
123 | ||
124 | let backup_user = crate::backup::backup_user()?; | |
67f7ffd0 | 125 | let _store = ChunkStore::create(&datastore.name, path, backup_user.uid, backup_user.gid)?; |
688fbe07 | 126 | |
67f7ffd0 | 127 | config.set_data(&datastore.name, "datastore", &datastore)?; |
652c1190 DM |
128 | |
129 | datastore::save_config(&config)?; | |
130 | ||
1298618a DM |
131 | jobstate::create_state_file("prune", &datastore.name)?; |
132 | jobstate::create_state_file("garbage_collection", &datastore.name)?; | |
9866de5e | 133 | |
688fbe07 | 134 | Ok(()) |
6ce50400 DM |
135 | } |
136 | ||
c5799e40 DM |
137 | #[api( |
138 | input: { | |
139 | properties: { | |
140 | name: { | |
141 | schema: DATASTORE_SCHEMA, | |
142 | }, | |
143 | }, | |
144 | }, | |
f3ec5dae DM |
145 | returns: { |
146 | description: "The datastore configuration (with config digest).", | |
147 | type: datastore::DataStoreConfig, | |
148 | }, | |
c0ef209a DM |
149 | access: { |
150 | permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_AUDIT, false), | |
151 | }, | |
c5799e40 DM |
152 | )] |
153 | /// Read a datastore configuration. | |
67f7ffd0 DM |
154 | pub fn read_datastore( |
155 | name: String, | |
156 | mut rpcenv: &mut dyn RpcEnvironment, | |
157 | ) -> Result<DataStoreConfig, Error> { | |
c5799e40 | 158 | let (config, digest) = datastore::config()?; |
67f7ffd0 DM |
159 | |
160 | let store_config = config.lookup("datastore", &name)?; | |
161 | rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); | |
162 | ||
163 | Ok(store_config) | |
c5799e40 DM |
164 | } |
165 | ||
0a00f6e0 DM |
166 | #[api()] |
167 | #[derive(Serialize, Deserialize)] | |
42fdbe51 | 168 | #[serde(rename_all="kebab-case")] |
0a00f6e0 DM |
169 | #[allow(non_camel_case_types)] |
170 | /// Deletable property name | |
171 | pub enum DeletableProperty { | |
172 | /// Delete the comment property. | |
173 | comment, | |
42fdbe51 DM |
174 | /// Delete the garbage collection schedule. |
175 | gc_schedule, | |
67f7ffd0 DM |
176 | /// Delete the prune job schedule. |
177 | prune_schedule, | |
178 | /// Delete the keep-last property | |
179 | keep_last, | |
180 | /// Delete the keep-hourly property | |
181 | keep_hourly, | |
182 | /// Delete the keep-daily property | |
183 | keep_daily, | |
184 | /// Delete the keep-weekly property | |
185 | keep_weekly, | |
186 | /// Delete the keep-monthly property | |
187 | keep_monthly, | |
188 | /// Delete the keep-yearly property | |
189 | keep_yearly, | |
0a00f6e0 DM |
190 | } |
191 | ||
c5799e40 DM |
192 | #[api( |
193 | protected: true, | |
194 | input: { | |
195 | properties: { | |
196 | name: { | |
197 | schema: DATASTORE_SCHEMA, | |
198 | }, | |
199 | comment: { | |
200 | optional: true, | |
201 | schema: SINGLE_LINE_COMMENT_SCHEMA, | |
202 | }, | |
42fdbe51 DM |
203 | "gc-schedule": { |
204 | optional: true, | |
205 | schema: GC_SCHEDULE_SCHEMA, | |
206 | }, | |
67f7ffd0 DM |
207 | "prune-schedule": { |
208 | optional: true, | |
209 | schema: PRUNE_SCHEDULE_SCHEMA, | |
210 | }, | |
211 | "keep-last": { | |
212 | optional: true, | |
213 | schema: PRUNE_SCHEMA_KEEP_LAST, | |
214 | }, | |
215 | "keep-hourly": { | |
216 | optional: true, | |
217 | schema: PRUNE_SCHEMA_KEEP_HOURLY, | |
218 | }, | |
219 | "keep-daily": { | |
220 | optional: true, | |
221 | schema: PRUNE_SCHEMA_KEEP_DAILY, | |
222 | }, | |
223 | "keep-weekly": { | |
224 | optional: true, | |
225 | schema: PRUNE_SCHEMA_KEEP_WEEKLY, | |
226 | }, | |
227 | "keep-monthly": { | |
228 | optional: true, | |
229 | schema: PRUNE_SCHEMA_KEEP_MONTHLY, | |
230 | }, | |
231 | "keep-yearly": { | |
232 | optional: true, | |
233 | schema: PRUNE_SCHEMA_KEEP_YEARLY, | |
234 | }, | |
0a00f6e0 DM |
235 | delete: { |
236 | description: "List of properties to delete.", | |
237 | type: Array, | |
238 | optional: true, | |
239 | items: { | |
240 | type: DeletableProperty, | |
241 | } | |
242 | }, | |
002a191a DM |
243 | digest: { |
244 | optional: true, | |
245 | schema: PROXMOX_CONFIG_DIGEST_SCHEMA, | |
246 | }, | |
c5799e40 DM |
247 | }, |
248 | }, | |
c0ef209a | 249 | access: { |
9c7fe29d | 250 | permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_MODIFY, false), |
c0ef209a | 251 | }, |
c5799e40 | 252 | )] |
2ea7bf1b | 253 | /// Update datastore config. |
c5799e40 DM |
254 | pub fn update_datastore( |
255 | name: String, | |
256 | comment: Option<String>, | |
42fdbe51 | 257 | gc_schedule: Option<String>, |
67f7ffd0 | 258 | prune_schedule: Option<String>, |
872062ee DM |
259 | keep_last: Option<u64>, |
260 | keep_hourly: Option<u64>, | |
261 | keep_daily: Option<u64>, | |
262 | keep_weekly: Option<u64>, | |
263 | keep_monthly: Option<u64>, | |
264 | keep_yearly: Option<u64>, | |
0a00f6e0 | 265 | delete: Option<Vec<DeletableProperty>>, |
002a191a | 266 | digest: Option<String>, |
c5799e40 DM |
267 | ) -> Result<(), Error> { |
268 | ||
b56c111e | 269 | let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?; |
347834df | 270 | |
c5799e40 | 271 | // pass/compare digest |
002a191a DM |
272 | let (mut config, expected_digest) = datastore::config()?; |
273 | ||
274 | if let Some(ref digest) = digest { | |
275 | let digest = proxmox::tools::hex_to_digest(digest)?; | |
276 | crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; | |
277 | } | |
c5799e40 DM |
278 | |
279 | let mut data: datastore::DataStoreConfig = config.lookup("datastore", &name)?; | |
280 | ||
0a00f6e0 DM |
281 | if let Some(delete) = delete { |
282 | for delete_prop in delete { | |
283 | match delete_prop { | |
284 | DeletableProperty::comment => { data.comment = None; }, | |
42fdbe51 | 285 | DeletableProperty::gc_schedule => { data.gc_schedule = None; }, |
67f7ffd0 DM |
286 | DeletableProperty::prune_schedule => { data.prune_schedule = None; }, |
287 | DeletableProperty::keep_last => { data.keep_last = None; }, | |
288 | DeletableProperty::keep_hourly => { data.keep_hourly = None; }, | |
289 | DeletableProperty::keep_daily => { data.keep_daily = None; }, | |
290 | DeletableProperty::keep_weekly => { data.keep_weekly = None; }, | |
291 | DeletableProperty::keep_monthly => { data.keep_monthly = None; }, | |
292 | DeletableProperty::keep_yearly => { data.keep_yearly = None; }, | |
0a00f6e0 DM |
293 | } |
294 | } | |
295 | } | |
296 | ||
c5799e40 DM |
297 | if let Some(comment) = comment { |
298 | let comment = comment.trim().to_string(); | |
299 | if comment.is_empty() { | |
300 | data.comment = None; | |
301 | } else { | |
302 | data.comment = Some(comment); | |
303 | } | |
304 | } | |
c5799e40 | 305 | |
d7a122a0 DC |
306 | let mut gc_schedule_changed = false; |
307 | if gc_schedule.is_some() { | |
308 | gc_schedule_changed = data.gc_schedule != gc_schedule; | |
309 | data.gc_schedule = gc_schedule; | |
310 | } | |
311 | ||
9866de5e DC |
312 | let mut prune_schedule_changed = false; |
313 | if prune_schedule.is_some() { | |
d7a122a0 | 314 | prune_schedule_changed = data.prune_schedule != prune_schedule; |
9866de5e DC |
315 | data.prune_schedule = prune_schedule; |
316 | } | |
d7a122a0 | 317 | |
67f7ffd0 DM |
318 | if keep_last.is_some() { data.keep_last = keep_last; } |
319 | if keep_hourly.is_some() { data.keep_hourly = keep_hourly; } | |
320 | if keep_daily.is_some() { data.keep_daily = keep_daily; } | |
321 | if keep_weekly.is_some() { data.keep_weekly = keep_weekly; } | |
322 | if keep_monthly.is_some() { data.keep_monthly = keep_monthly; } | |
323 | if keep_yearly.is_some() { data.keep_yearly = keep_yearly; } | |
42fdbe51 | 324 | |
c5799e40 DM |
325 | config.set_data(&name, "datastore", &data)?; |
326 | ||
327 | datastore::save_config(&config)?; | |
328 | ||
d7a122a0 | 329 | // we want to reset the statefiles, to avoid an immediate action in some cases |
9866de5e | 330 | // (e.g. going from monthly to weekly in the second week of the month) |
d7a122a0 | 331 | if gc_schedule_changed { |
1298618a | 332 | jobstate::create_state_file("garbage_collection", &name)?; |
d7a122a0 DC |
333 | } |
334 | ||
9866de5e | 335 | if prune_schedule_changed { |
1298618a | 336 | jobstate::create_state_file("prune", &name)?; |
9866de5e DC |
337 | } |
338 | ||
c5799e40 DM |
339 | Ok(()) |
340 | } | |
341 | ||
688fbe07 DM |
342 | #[api( |
343 | protected: true, | |
344 | input: { | |
345 | properties: { | |
346 | name: { | |
347 | schema: DATASTORE_SCHEMA, | |
348 | }, | |
c0ef209a DM |
349 | digest: { |
350 | optional: true, | |
351 | schema: PROXMOX_CONFIG_DIGEST_SCHEMA, | |
352 | }, | |
688fbe07 DM |
353 | }, |
354 | }, | |
c0ef209a | 355 | access: { |
92dd02aa | 356 | permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_ALLOCATE, false), |
c0ef209a | 357 | }, |
688fbe07 DM |
358 | )] |
359 | /// Remove a datastore configuration. | |
c0ef209a | 360 | pub fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Error> { |
34d3ba52 | 361 | |
b56c111e | 362 | let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?; |
34d3ba52 | 363 | |
c0ef209a DM |
364 | let (mut config, expected_digest) = datastore::config()?; |
365 | ||
366 | if let Some(ref digest) = digest { | |
367 | let digest = proxmox::tools::hex_to_digest(digest)?; | |
368 | crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; | |
369 | } | |
34d3ba52 | 370 | |
688fbe07 DM |
371 | match config.sections.get(&name) { |
372 | Some(_) => { config.sections.remove(&name); }, | |
34d3ba52 DM |
373 | None => bail!("datastore '{}' does not exist.", name), |
374 | } | |
375 | ||
376 | datastore::save_config(&config)?; | |
377 | ||
d7a122a0 | 378 | // ignore errors |
1298618a DM |
379 | let _ = jobstate::remove_state_file("prune", &name); |
380 | let _ = jobstate::remove_state_file("garbage_collection", &name); | |
9866de5e | 381 | |
688fbe07 | 382 | Ok(()) |
34d3ba52 DM |
383 | } |
384 | ||
c5799e40 DM |
385 | const ITEM_ROUTER: Router = Router::new() |
386 | .get(&API_METHOD_READ_DATASTORE) | |
387 | .put(&API_METHOD_UPDATE_DATASTORE) | |
388 | .delete(&API_METHOD_DELETE_DATASTORE); | |
389 | ||
255f378a | 390 | pub const ROUTER: Router = Router::new() |
688fbe07 DM |
391 | .get(&API_METHOD_LIST_DATASTORES) |
392 | .post(&API_METHOD_CREATE_DATASTORE) | |
c5799e40 | 393 | .match_all("name", &ITEM_ROUTER); |