Skip to main content

rustic_rs/
config.rs

1//! Rustic Config
2//!
3//! See instructions in `commands.rs` to specify the path to your
4//! application's configuration file and/or command-line options
5//! for specifying it.
6
7pub(crate) mod hooks;
8pub(crate) mod logging;
9pub(crate) mod progress_options;
10
11use std::{
12    collections::BTreeMap,
13    fmt::{self, Display, Formatter},
14    path::PathBuf,
15};
16
17use abscissa_core::{FrameworkError, FrameworkErrorKind, config::Config, path::AbsPathBuf};
18use anyhow::{Result, anyhow};
19use clap::{Parser, ValueHint};
20use conflate::Merge;
21use directories::ProjectDirs;
22use itertools::Itertools;
23use jiff::{Timestamp, Zoned, tz::TimeZone};
24use log::Level;
25use reqwest::Url;
26use rustic_core::SnapshotGroupCriterion;
27use serde::{Deserialize, Serialize};
28use serde_with::{DisplayFromStr, serde_as};
29#[cfg(not(all(feature = "mount", feature = "webdav")))]
30use toml::Value;
31
32#[cfg(feature = "mount")]
33use crate::commands::mount::MountCmd;
34#[cfg(feature = "webdav")]
35use crate::commands::webdav::WebDavCmd;
36
37use crate::{
38    commands::{backup::BackupCmd, copy::CopyCmd, forget::ForgetOptions},
39    config::{hooks::Hooks, logging::LoggingOptions, progress_options::ProgressOptions},
40    filtering::SnapshotFilter,
41    repository::AllRepositoryOptions,
42};
43
44/// Rustic Configuration
45///
46/// Further documentation can be found [here](https://github.com/rustic-rs/rustic/blob/main/config/README.md).
47///
48/// # Example
49// TODO: add example
50#[derive(Clone, Default, Debug, Parser, Deserialize, Serialize, Merge)]
51#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
52pub struct RusticConfig {
53    /// Global options
54    #[clap(flatten, next_help_heading = "Global options")]
55    pub global: GlobalOptions,
56
57    /// Repository options
58    #[clap(flatten, next_help_heading = "Repository options")]
59    pub repository: AllRepositoryOptions,
60
61    /// Snapshot filter options
62    #[clap(flatten, next_help_heading = "Snapshot filter options")]
63    pub snapshot_filter: SnapshotFilter,
64
65    /// Backup options
66    #[clap(skip)]
67    pub backup: BackupCmd,
68
69    /// Copy options
70    #[clap(skip)]
71    pub copy: CopyCmd,
72
73    /// Forget options
74    #[clap(skip)]
75    pub forget: ForgetOptions,
76
77    /// mount options
78    #[cfg(feature = "mount")]
79    #[clap(skip)]
80    pub mount: MountCmd,
81    #[cfg(not(feature = "mount"))]
82    #[clap(skip)]
83    #[merge(skip)]
84    pub mount: Option<Value>,
85
86    /// webdav options
87    #[cfg(feature = "webdav")]
88    #[clap(skip)]
89    pub webdav: WebDavCmd,
90    #[cfg(not(feature = "webdav"))]
91    #[clap(skip)]
92    #[merge(skip)]
93    pub webdav: Option<Value>,
94}
95
96impl Display for RusticConfig {
97    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
98        let config = toml::to_string_pretty(self)
99            .unwrap_or_else(|_| "<Error serializing config>".to_string());
100
101        write!(f, "{config}",)
102    }
103}
104
105impl RusticConfig {
106    /// Merge a profile into the current config by reading the corresponding config file.
107    /// Also recursively merge all profiles given within this config file.
108    ///
109    /// # Arguments
110    ///
111    /// * `profile` - name of the profile to merge
112    /// * `merge_logs` - Vector to collect logs during merging
113    /// * `level_missing` - The log level to use if this profile is missing. Recursive calls will produce a Warning.
114    pub fn merge_profile(
115        &mut self,
116        profile: &str,
117        merge_logs: &mut Vec<(Level, String)>,
118        level_missing: Level,
119    ) -> Result<(), FrameworkError> {
120        let profile_filename = profile.to_string() + ".toml";
121        let paths = get_config_paths(&profile_filename);
122
123        if let Some(path) = paths.iter().find(|path| path.exists()) {
124            merge_logs.push((Level::Info, format!("using config {}", path.display())));
125            let config_content = std::fs::read_to_string(AbsPathBuf::canonicalize(path)?)?;
126            let config_content = if self.global.profile_substitute_env {
127                subst::substitute(&config_content, &subst::Env).map_err(|e| {
128                    abscissa_core::error::context::Context::new(
129                        FrameworkErrorKind::ParseError,
130                        Some(Box::new(e)),
131                    )
132                })?
133            } else {
134                config_content
135            };
136            let mut config = Self::load_toml(config_content)?;
137            // if "use_profile" is defined in config file, merge the referenced profiles first
138            for profile in &config.global.use_profiles.clone() {
139                config.merge_profile(profile, merge_logs, Level::Warn)?;
140            }
141            self.merge(config);
142        } else {
143            let paths_string = paths.iter().map(|path| path.display()).join(", ");
144            merge_logs.push((
145                level_missing,
146                format!(
147                    "using no config file, none of these exist: {}",
148                    &paths_string
149                ),
150            ));
151        };
152        Ok(())
153    }
154}
155
156/// Global options
157///
158/// These options are available for all commands.
159#[serde_as]
160#[derive(Default, Debug, Parser, Clone, Deserialize, Serialize, Merge)]
161#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
162pub struct GlobalOptions {
163    /// Substitute environment variables in profiles
164    #[clap(long, global = true, env = "RUSTIC_PROFILE_SUBSTITUTE_ENV")]
165    #[merge(strategy=conflate::bool::overwrite_false)]
166    pub profile_substitute_env: bool,
167
168    /// Config profile to use. This parses the file `<PROFILE>.toml` in the config directory.
169    /// [default: "rustic"]
170    #[clap(
171        short = 'P',
172        long = "use-profile",
173        global = true,
174        value_name = "PROFILE",
175        env = "RUSTIC_USE_PROFILE"
176    )]
177    #[merge(strategy=conflate::vec::append)]
178    pub use_profiles: Vec<String>,
179
180    /// Group snapshots by any combination of host,label,paths,tags, e.g. to find the latest snapshot [default: "host,label,paths"]
181    #[clap(
182        long,
183        short = 'g',
184        global = true,
185        value_name = "CRITERION",
186        env = "RUSTIC_GROUP_BY"
187    )]
188    #[serde_as(as = "Option<DisplayFromStr>")]
189    #[merge(strategy=conflate::option::overwrite_none)]
190    pub group_by: Option<SnapshotGroupCriterion>,
191
192    /// Only show what would be done without modifying anything. Does not affect read-only commands.
193    #[clap(long, short = 'n', global = true, env = "RUSTIC_DRY_RUN")]
194    #[merge(strategy=conflate::bool::overwrite_false)]
195    pub dry_run: bool,
196
197    /// Additional to dry run, but still issue warm-up command if configured
198    #[clap(long, global = true, env = "RUSTIC_DRY_RUN_WARMUP")]
199    #[merge(strategy=conflate::bool::overwrite_false)]
200    pub dry_run_warmup: bool,
201
202    /// Check if index matches pack files and read pack headers if necessary
203    #[clap(long, global = true, env = "RUSTIC_CHECK_INDEX")]
204    #[merge(strategy=conflate::bool::overwrite_false)]
205    pub check_index: bool,
206
207    /// Settings to customize logging
208    #[clap(flatten)]
209    #[serde(flatten)]
210    pub logging_options: LoggingOptions,
211
212    /// Settings to customize progress bars
213    #[clap(flatten)]
214    #[serde(flatten)]
215    pub progress_options: ProgressOptions,
216
217    /// Hooks
218    #[clap(skip)]
219    pub hooks: Hooks,
220
221    /// List of environment variables to set (only in config file)
222    #[clap(skip)]
223    #[merge(strategy = conflate::btreemap::append_or_ignore)]
224    pub env: BTreeMap<String, String>,
225
226    /// Push metrics to a Prometheus Pushgateway
227    #[serde_as(as = "Option<DisplayFromStr>")]
228    #[clap(long, global = true, env = "RUSTIC_PROMETHEUS", value_name = "PUSHGATEWAY_URL", value_hint = ValueHint::Url)]
229    #[merge(strategy=conflate::option::overwrite_none)]
230    pub prometheus: Option<Url>,
231
232    /// Authenticate to Prometheus Pushgateway using this user
233    #[clap(long, value_name = "USER", env = "RUSTIC_PROMETHEUS_USER")]
234    #[merge(strategy=conflate::option::overwrite_none)]
235    pub prometheus_user: Option<String>,
236
237    /// Authenticate to Prometheus Pushgateway using this password
238    #[clap(long, value_name = "PASSWORD", env = "RUSTIC_PROMETHEUS_PASS")]
239    #[merge(strategy=conflate::option::overwrite_none)]
240    pub prometheus_pass: Option<String>,
241
242    /// Additional labels to set to generated metrics
243    #[clap(skip)]
244    #[merge(strategy=conflate::btreemap::append_or_ignore)]
245    pub metrics_labels: BTreeMap<String, String>,
246
247    /// OpenTelemetry metrics endpoint (HTTP Protobuf)
248    #[serde_as(as = "Option<DisplayFromStr>")]
249    #[clap(long, global = true, env = "RUSTIC_OTEL", value_name = "ENDPOINT_URL", value_hint = ValueHint::Url)]
250    #[merge(strategy=conflate::option::overwrite_none)]
251    pub opentelemetry: Option<Url>,
252
253    /// Show time offsets instead of converting to system time zone
254    #[clap(long, global = true, env = "RUSTIC_SHOW_TIME_OFFSET")]
255    #[merge(strategy=conflate::bool::overwrite_false)]
256    pub show_time_offset: bool,
257}
258
259pub fn parse_labels(s: &str) -> Result<BTreeMap<String, String>> {
260    s.split(',')
261        .filter_map(|s| {
262            let s = s.trim();
263            (!s.is_empty()).then_some(s)
264        })
265        .map(|s| -> Result<_> {
266            let pos = s.find('=').ok_or_else(|| {
267                anyhow!("invalid prometheus label definition: no `=` found in `{s}`")
268            })?;
269            Ok((s[..pos].to_owned(), s[pos + 1..].to_owned()))
270        })
271        .try_collect()
272}
273
274impl GlobalOptions {
275    pub fn is_metrics_configured(&self) -> bool {
276        self.prometheus.is_some() || self.opentelemetry.is_some()
277    }
278
279    pub fn format_timestamp(&self, timestamp: Timestamp) -> String {
280        self.format_time(&timestamp.to_zoned(TimeZone::UTC))
281            .to_string()
282    }
283
284    pub fn format_time(&self, time: &Zoned) -> impl Display {
285        if self.show_time_offset {
286            time.strftime("%Y-%m-%d %H:%M:%S%z")
287        } else {
288            let tz = TimeZone::system();
289            if time.offset() == tz.to_offset(time.timestamp()) {
290                time.strftime("%Y-%m-%d %H:%M:%S")
291            } else {
292                time.with_time_zone(tz).strftime("%Y-%m-%d %H:%M:%S*")
293            }
294        }
295    }
296}
297
298/// Get the paths to the config file
299///
300/// # Arguments
301///
302/// * `filename` - name of the config file
303///
304/// # Returns
305///
306/// A vector of [`PathBuf`]s to the config files
307fn get_config_paths(filename: &str) -> Vec<PathBuf> {
308    [
309        ProjectDirs::from("", "", "rustic")
310            .map(|project_dirs| project_dirs.config_dir().to_path_buf()),
311        get_global_config_path(),
312        Some(PathBuf::from(".")),
313    ]
314    .into_iter()
315    .filter_map(|path| {
316        path.map(|mut p| {
317            p.push(filename);
318            p
319        })
320    })
321    .collect()
322}
323
324/// Get the path to the global config directory on Windows.
325///
326/// # Returns
327///
328/// The path to the global config directory on Windows.
329/// If the environment variable `PROGRAMDATA` is not set, `None` is returned.
330#[cfg(target_os = "windows")]
331fn get_global_config_path() -> Option<PathBuf> {
332    std::env::var_os("PROGRAMDATA").map(|program_data| {
333        let mut path = PathBuf::from(program_data);
334        path.push(r"rustic\config");
335        path
336    })
337}
338
339/// Get the path to the global config directory on ios and wasm targets.
340///
341/// # Returns
342///
343/// `None` is returned.
344#[cfg(any(target_os = "ios", target_arch = "wasm32"))]
345fn get_global_config_path() -> Option<PathBuf> {
346    None
347}
348
349/// Get the path to the global config directory on non-Windows,
350/// non-iOS, non-wasm targets.
351///
352/// # Returns
353///
354/// "/etc/rustic" is returned.
355#[cfg(not(any(target_os = "windows", target_os = "ios", target_arch = "wasm32")))]
356fn get_global_config_path() -> Option<PathBuf> {
357    Some(PathBuf::from("/etc/rustic"))
358}
359
360#[cfg(test)]
361mod tests {
362    use super::*;
363    use insta::{assert_debug_snapshot, assert_snapshot};
364
365    #[test]
366    fn test_default_config_passes() {
367        let config = RusticConfig::default();
368
369        assert_debug_snapshot!(config);
370    }
371
372    #[test]
373    fn test_default_config_display_passes() {
374        let config = RusticConfig::default();
375
376        assert_snapshot!(config);
377    }
378
379    #[test]
380    fn test_global_env_roundtrip_passes() {
381        let mut config = RusticConfig::default();
382
383        for i in 0..10 {
384            let _ = config
385                .global
386                .env
387                .insert(format!("KEY{i}"), format!("VALUE{i}"));
388        }
389
390        let serialized = toml::to_string(&config).unwrap();
391
392        // Check Serialization
393        assert_snapshot!(serialized);
394
395        let deserialized: RusticConfig = toml::from_str(&serialized).unwrap();
396        // Check Deserialization and Display
397        assert_snapshot!(deserialized);
398
399        // Check Debug
400        assert_debug_snapshot!(deserialized);
401    }
402}