]> git.proxmox.com Git - proxmox-backup.git/blob - src/config/datastore.rs
completion: fix ACL path completion
[proxmox-backup.git] / src / config / datastore.rs
1 use anyhow::{Error};
2 use lazy_static::lazy_static;
3 use std::collections::HashMap;
4 use serde::{Serialize, Deserialize};
5
6 use proxmox::api::{
7 api,
8 schema::*,
9 section_config::{
10 SectionConfig,
11 SectionConfigData,
12 SectionConfigPlugin,
13 }
14 };
15
16 use proxmox::tools::{fs::replace_file, fs::CreateOptions};
17
18 use crate::api2::types::*;
19
20 lazy_static! {
21 static ref CONFIG: SectionConfig = init();
22 }
23
24 // fixme: define better schemas
25 pub const DIR_NAME_SCHEMA: Schema = StringSchema::new("Directory name").schema();
26
27 #[api(
28 properties: {
29 name: {
30 schema: DATASTORE_SCHEMA,
31 },
32 path: {
33 schema: DIR_NAME_SCHEMA,
34 },
35 comment: {
36 optional: true,
37 schema: SINGLE_LINE_COMMENT_SCHEMA,
38 },
39 "gc-schedule": {
40 optional: true,
41 schema: GC_SCHEDULE_SCHEMA,
42 },
43 "prune-schedule": {
44 optional: true,
45 schema: PRUNE_SCHEDULE_SCHEMA,
46 },
47 "verify-schedule": {
48 optional: true,
49 schema: VERIFY_SCHEDULE_SCHEMA,
50 },
51 "keep-last": {
52 optional: true,
53 schema: PRUNE_SCHEMA_KEEP_LAST,
54 },
55 "keep-hourly": {
56 optional: true,
57 schema: PRUNE_SCHEMA_KEEP_HOURLY,
58 },
59 "keep-daily": {
60 optional: true,
61 schema: PRUNE_SCHEMA_KEEP_DAILY,
62 },
63 "keep-weekly": {
64 optional: true,
65 schema: PRUNE_SCHEMA_KEEP_WEEKLY,
66 },
67 "keep-monthly": {
68 optional: true,
69 schema: PRUNE_SCHEMA_KEEP_MONTHLY,
70 },
71 "keep-yearly": {
72 optional: true,
73 schema: PRUNE_SCHEMA_KEEP_YEARLY,
74 },
75 }
76 )]
77 #[serde(rename_all="kebab-case")]
78 #[derive(Serialize,Deserialize)]
79 /// Datastore configuration properties.
80 pub struct DataStoreConfig {
81 pub name: String,
82 #[serde(skip_serializing_if="Option::is_none")]
83 pub comment: Option<String>,
84 pub path: String,
85 #[serde(skip_serializing_if="Option::is_none")]
86 pub gc_schedule: Option<String>,
87 #[serde(skip_serializing_if="Option::is_none")]
88 pub prune_schedule: Option<String>,
89 #[serde(skip_serializing_if="Option::is_none")]
90 pub verify_schedule: Option<String>,
91 #[serde(skip_serializing_if="Option::is_none")]
92 pub keep_last: Option<u64>,
93 #[serde(skip_serializing_if="Option::is_none")]
94 pub keep_hourly: Option<u64>,
95 #[serde(skip_serializing_if="Option::is_none")]
96 pub keep_daily: Option<u64>,
97 #[serde(skip_serializing_if="Option::is_none")]
98 pub keep_weekly: Option<u64>,
99 #[serde(skip_serializing_if="Option::is_none")]
100 pub keep_monthly: Option<u64>,
101 #[serde(skip_serializing_if="Option::is_none")]
102 pub keep_yearly: Option<u64>,
103 }
104
105 fn init() -> SectionConfig {
106 let obj_schema = match DataStoreConfig::API_SCHEMA {
107 Schema::Object(ref obj_schema) => obj_schema,
108 _ => unreachable!(),
109 };
110
111 let plugin = SectionConfigPlugin::new("datastore".to_string(), Some(String::from("name")), obj_schema);
112 let mut config = SectionConfig::new(&DATASTORE_SCHEMA);
113 config.register_plugin(plugin);
114
115 config
116 }
117
118 pub const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
119 pub const DATASTORE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.datastore.lck";
120
121 pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
122
123 let content = proxmox::tools::fs::file_read_optional_string(DATASTORE_CFG_FILENAME)?;
124 let content = content.unwrap_or(String::from(""));
125
126 let digest = openssl::sha::sha256(content.as_bytes());
127 let data = CONFIG.parse(DATASTORE_CFG_FILENAME, &content)?;
128 Ok((data, digest))
129 }
130
131 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
132 let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?;
133
134 let backup_user = crate::backup::backup_user()?;
135 let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640);
136 // set the correct owner/group/permissions while saving file
137 // owner(rw) = root, group(r)= backup
138 let options = CreateOptions::new()
139 .perm(mode)
140 .owner(nix::unistd::ROOT)
141 .group(backup_user.gid);
142
143 replace_file(DATASTORE_CFG_FILENAME, raw.as_bytes(), options)?;
144
145 Ok(())
146 }
147
148 // shell completion helper
149 pub fn complete_datastore_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
150 match config() {
151 Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
152 Err(_) => return vec![],
153 }
154 }
155
156 pub fn complete_acl_path(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
157 let mut list = Vec::new();
158
159 list.push(String::from("/"));
160 list.push(String::from("/datastore"));
161 list.push(String::from("/datastore/"));
162
163 if let Ok((data, _digest)) = config() {
164 for id in data.sections.keys() {
165 list.push(format!("/datastore/{}", id));
166 }
167 }
168
169 list
170 }
171
172 pub fn complete_calendar_event(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
173 // just give some hints about possible values
174 ["minutely", "hourly", "daily", "mon..fri", "0:0"]
175 .iter().map(|s| String::from(*s)).collect()
176 }