summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-02-27 07:54:40 +0200
committerLars Wirzenius <liw@liw.fi>2021-02-28 12:24:08 +0200
commit1c5c97532c91faf8a59bbe7ebcfc645f653db697 (patch)
tree5d690b3e826472c8515710dce3d9bd256106b249 /src/bin
parent98c482d7903d059a6598a6e7280f0e7b431eac29 (diff)
downloadvmadm-1c5c97532c91faf8a59bbe7ebcfc645f653db697.tar.gz
feat: export cloud-init config, set SSH host keys
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/vmadm.rs60
1 files changed, 52 insertions, 8 deletions
diff --git a/src/bin/vmadm.rs b/src/bin/vmadm.rs
index 0da9af9..75e0cdf 100644
--- a/src/bin/vmadm.rs
+++ b/src/bin/vmadm.rs
@@ -17,14 +17,28 @@ const SSH_PORT: i32 = 22;
#[derive(StructOpt, Debug)]
enum Cli {
New {
- #[structopt(help = "create a new virtual machine", parse(from_os_str))]
+ #[structopt(parse(from_os_str))]
spec: PathBuf,
},
List,
Delete {
- #[structopt(help = "create a new virtual machine", parse(from_os_str))]
+ #[structopt(parse(from_os_str))]
spec: PathBuf,
},
+ CloudInit {
+ #[structopt(parse(from_os_str))]
+ spec: PathBuf,
+
+ #[structopt(parse(from_os_str))]
+ dirname: PathBuf,
+ },
+ CloudInitIso {
+ #[structopt(parse(from_os_str))]
+ spec: PathBuf,
+
+ #[structopt(parse(from_os_str))]
+ iso: PathBuf,
+ },
}
fn main() -> anyhow::Result<()> {
@@ -33,6 +47,8 @@ fn main() -> anyhow::Result<()> {
Cli::New { spec } => new(&spec)?,
Cli::List => list()?,
Cli::Delete { spec } => delete(&spec)?,
+ Cli::CloudInit { spec, dirname } => cloud_init(&spec, &dirname)?,
+ Cli::CloudInitIso { spec, iso } => cloud_init_iso(&spec, &iso)?,
}
Ok(())
}
@@ -44,13 +60,8 @@ fn new(spec: &Path) -> anyhow::Result<()> {
let spec = fs::read(spec)?;
let spec: Specification = serde_yaml::from_slice(&spec)?;
- debug!("reading specified SSH public keys");
- let ssh_keys = spec.ssh_keys()?;
-
info!("creating cloud-init config");
- let mut init = CloudInitConfig::default();
- init.set_hostname(&spec.name);
- init.set_authorized_keys(&ssh_keys);
+ let init = CloudInitConfig::from(&spec)?;
info!(
"creating VM image {} from {}",
@@ -154,3 +165,36 @@ fn delete(spec: &Path) -> anyhow::Result<()> {
}
Ok(())
}
+
+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(())
+}