summaryrefslogtreecommitdiff
path: root/src/cmd/cloud_init.rs
blob: b266eb415cca60aa1b5741ddf92adea458f37099 (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
//! The `cloud-init` sub-command.

use crate::cloudinit::{CloudInitConfig, CloudInitError};
use crate::spec::Specification;
use log::{debug, info};
use std::path::Path;

/// Errors from this module.
#[derive(Debug, thiserror::Error)]
pub enum CloudInitCommandError {
    /// Error in the cloud-init configuration.
    #[error(transparent)]
    CloudInitError(#[from] CloudInitError),

    /// Error doing I/O.
    #[error(transparent)]
    IoError(#[from] std::io::Error),
}

/// The `cloud-init` sub-command.
///
/// This sub-command generates the cloud-init configuration based on
/// specifications provided by the caller and writes them to files in
/// a directory named by the caller.
pub fn cloud_init(specs: &[Specification], dirname: &Path) -> Result<(), CloudInitCommandError> {
    for spec in specs {
        let dirname = dirname.join(&spec.name);
        info!(
            "generating cloud-init configuration for {} into {}",
            spec.name,
            dirname.display()
        );

        let init = CloudInitConfig::from(&spec)?;

        debug!("creating directory {}", dirname.display());
        std::fs::create_dir_all(&dirname)?;
        init.create_dir(&dirname)?;
    }

    Ok(())
}