summaryrefslogtreecommitdiff
path: root/src/bin/vmadm.rs
blob: 8fe5540f485b0c53278d7c58bf44b1442b7ccf75 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use anyhow::Context;
use directories_next::ProjectDirs;
use vmadm::cmd;
use vmadm::config::Configuration;
use vmadm::spec::Specification;
//use bytesize::GIB;
use log::debug;
//use std::fs;
use std::path::{Path, PathBuf};
//use std::thread;
use structopt::StructOpt;
//use vmadm::cloudinit::CloudInitConfig;
//use vmadm::image::VirtualMachineImage;
//use vmadm::install::{virt_install, VirtInstallArgs};
//use vmadm::spec::Specification;

const QUALIFIER: &str = "";
const ORG: &str = "";
const APP: &str = "vmadm";

#[derive(StructOpt, Debug)]
struct Cli {
    #[structopt(subcommand)]
    cmd: Command,
}

#[derive(StructOpt, Debug)]
enum Command {
    New {
        #[structopt(flatten)]
        common: CommonOptions,

        #[structopt(parse(from_os_str))]
        spec: PathBuf,
    },

    List {
        #[structopt(flatten)]
        common: CommonOptions,
    },

    Delete {
        #[structopt(flatten)]
        common: CommonOptions,

        #[structopt(parse(from_os_str))]
        spec: PathBuf,
    },

    CloudInit {
        #[structopt(flatten)]
        common: CommonOptions,

        #[structopt(parse(from_os_str))]
        spec: PathBuf,

        #[structopt(parse(from_os_str))]
        dirname: PathBuf,
    },
}

#[derive(StructOpt, Debug)]
struct CommonOptions {
    #[structopt(short, long, parse(from_os_str))]
    config: Option<PathBuf>,
}

fn main() -> anyhow::Result<()> {
    pretty_env_logger::init_custom_env("VMADM_LOG");
    let cli = Cli::from_args();
    debug!("{:#?}", cli);

    match cli.cmd {
        Command::New { common, spec } => {
            let specs = get_specs(&common, &spec)?;
            cmd::new(&specs)?;
        }

        Command::List { common } => {
            let config = config(&common)?;
            cmd::list(&config)?;
        }

        Command::Delete { common, spec } => {
            let specs = get_specs(&common, &spec)?;
            cmd::delete(&specs)?;
        }

        Command::CloudInit {
            common,
            spec,
            dirname,
        } => {
            let specs = get_specs(&common, &spec)?;
            cmd::cloud_init(&specs, &dirname)?;
        }
    }
    Ok(())
}

fn get_specs(common: &CommonOptions, spec: &Path) -> anyhow::Result<Vec<Specification>> {
    let config = config(&common)?;
    let specs = Specification::from_file(&config, &spec)?;
    Ok(specs)
}

fn config(common: &CommonOptions) -> anyhow::Result<Configuration> {
    let filename = config_filename(common);
    let config = Configuration::from_file(&filename)
        .with_context(|| format!("reading configuration file {}", filename.display()))?;
    Ok(config)
}

fn config_filename(common: &CommonOptions) -> PathBuf {
    if let Some(ref filename) = common.config {
        filename.to_path_buf()
    } else {
        if let Some(dirs) = ProjectDirs::from(QUALIFIER, ORG, APP) {
            dirs.config_dir().join("config.yaml")
        } else {
            PathBuf::from("xxx")
        }
    }
}

// fn cloud_init(spec: &Path, dirname: &Path) -> anyhow::Result<()> {
//     info!("generating cloud-init configuration");

//     debug!("reading specification from {}", spec.display());
//     let spec = fs::read(spec)?;
//     let spec: Specification = serde_yaml::from_slice(&spec)?;
//     debug!("spec:\n{:#?}", spec);

//     info!("creating cloud-init config");
//     let init = CloudInitConfig::from(&spec)?;

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

//     Ok(())
// }

// fn cloud_init_iso(spec: &Path, iso: &Path) -> anyhow::Result<()> {
//     info!("generating cloud-init ISO");

//     debug!("reading specification from {}", spec.display());
//     let spec = fs::read(spec)?;
//     let spec: Specification = serde_yaml::from_slice(&spec)?;
//     debug!("spec:\n{:#?}", spec);

//     info!("creating cloud-init config");
//     let init = CloudInitConfig::from(&spec)?;
//     init.create_iso(iso)?;

//     Ok(())
// }