summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 8de475156bfb217531044e5ee898b0fd85e62fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Tool configuration.

use crate::util::{check_network_names, expand_optional_pathbuf, expand_optional_pathbufs};
use log::debug;
use serde::{Deserialize, Serialize};
use std::default::Default;
use std::fs;
use std::path::{Path, PathBuf};

/// Configuration from configuration file.
#[derive(Default, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Configuration {
    /// Base image, if provided.
    pub default_base_image: Option<PathBuf>,

    /// Default size of new VM image, in GiB.
    pub default_image_gib: Option<u64>,

    /// Default memory to give to new VM, in MiB.
    pub default_memory_mib: Option<u64>,

    /// Default number of CPUs for a new VM.
    pub default_cpus: Option<u64>,

    /// Should host certificates be generated for new VMs?
    pub default_generate_host_certificate: Option<bool>,

    /// Should new VM be started automatically on host boot?
    pub default_autostart: Option<bool>,

    /// List of default networks to add to hosts.
    pub default_networks: Option<Vec<String>>,

    /// Directory where new VM images should be created, if given.
    pub image_directory: Option<PathBuf>,

    /// List of path names of SSH public keys to put into the default
    /// user's `authorized_keys` file.
    pub authorized_keys: Option<Vec<PathBuf>>,

    /// Path name to SSH CA key for creating SSH host certificates.
    pub ca_key: Option<PathBuf>,

    /// Path name to SSH CA public key for verifying SSH user certificates.
    pub user_ca_pubkey: Option<PathBuf>,

    /// Should SSH authorized keys files be allowed by default?
    pub default_allow_authorized_keys: Option<bool>,
}

/// Errors from this module.
#[derive(Debug, thiserror::Error)]
pub enum ConfigurationError {
    /// Network name error.
    #[error(transparent)]
    NetworkNameError(#[from] crate::util::NetworkNameError),

    /// Error reading configuration file.
    #[error("couldn't read configuration file {0}")]
    ReadError(PathBuf, #[source] std::io::Error),

    /// YAML parsing error.
    #[error(transparent)]
    YamlError(#[from] serde_yaml::Error),

    /// Error expanding a ~user in a path name.
    #[error(transparent)]
    HomeDirError(#[from] home_dir::Error),
}

impl Configuration {
    /// Read configuration from named file.
    pub fn from_file(filename: &Path) -> Result<Self, ConfigurationError> {
        if filename.exists() {
            debug!("reading configuration file {}", filename.display());
            let config = fs::read(filename)
                .map_err(|err| ConfigurationError::ReadError(filename.to_path_buf(), err))?;
            let mut config: Configuration = serde_yaml::from_slice(&config)?;
            if let Some(ref networks) = config.default_networks {
                check_network_names(networks)?;
            }
            config.expand_tildes()?;
            config.fill_in_missing_networks();
            debug!("config: {:#?}", config);
            Ok(config)
        } else {
            Ok(Self::default())
        }
    }

    fn fill_in_missing_networks(&mut self) {
        if self.default_networks.is_none() {
            self.default_networks = Some(vec!["network=default".to_string()]);
        }
    }

    fn expand_tildes(&mut self) -> Result<(), home_dir::Error> {
        expand_optional_pathbuf(&mut self.default_base_image)?;
        expand_optional_pathbuf(&mut self.image_directory)?;
        expand_optional_pathbuf(&mut self.image_directory)?;
        expand_optional_pathbuf(&mut self.ca_key)?;
        expand_optional_pathbuf(&mut self.user_ca_pubkey)?;
        expand_optional_pathbufs(&mut self.authorized_keys)?;
        Ok(())
    }
}