From ccdd1fa18bb3db5a01f66dac17a5957adcec0fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kev=20=F0=9F=90=B6?= Date: Mon, 6 Jan 2025 14:45:25 +0100 Subject: [PATCH] Add inspect command This adds a `krunvm inspect` command that runs `buildah inspect` under the hood, so users can find information about the VM, such as the image metadata (e.g. image name and digest) or mount point. --- src/commands/inspect.rs | 52 +++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 2 ++ src/main.rs | 6 ++++- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/commands/inspect.rs diff --git a/src/commands/inspect.rs b/src/commands/inspect.rs new file mode 100644 index 0000000..8911bf1 --- /dev/null +++ b/src/commands/inspect.rs @@ -0,0 +1,52 @@ +// Copyright 2021 Red Hat, Inc. +// SPDX-License-Identifier: Apache-2.0 + +use std::process::Command; + +use crate::{ + utils::{get_buildah_args, BuildahCommand}, + KrunvmConfig, +}; +use clap::Args; + +/// Run `buildah inspect` on an existing microVM +#[derive(Args, Debug)] +pub struct InspectCmd { + /// Name of the microVM to be inspected + name: String, +} + +impl InspectCmd { + pub fn run(self, cfg: &mut KrunvmConfig) { + let vmcfg = match cfg.vmconfig_map.get(&self.name) { + None => { + println!("No VM found with that name"); + std::process::exit(-1); + } + Some(vmcfg) => vmcfg, + }; + + let mut args = get_buildah_args(cfg, BuildahCommand::Inspect); + args.push(vmcfg.container.clone()); + + let output = Command::new("buildah") + .args(&args) + .stderr(std::process::Stdio::inherit()) + .output(); + + if output.is_err() { + println!("Failed to inspect VM"); + std::process::exit(1); + } + + let output = match String::from_utf8(output.unwrap().stdout) { + Err(err) => { + println!("Failed to parse `buildah inspect` output: #{err}."); + std::process::exit(1); + } + Ok(output) => output, + }; + + println!("{output}"); + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 256e206..6e93314 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,6 +2,7 @@ mod changevm; mod config; mod create; mod delete; +mod inspect; mod list; mod start; @@ -9,5 +10,6 @@ pub use changevm::ChangeVmCmd; pub use config::ConfigCmd; pub use create::CreateCmd; pub use delete::DeleteCmd; +pub use inspect::InspectCmd; pub use list::ListCmd; pub use start::StartCmd; diff --git a/src/main.rs b/src/main.rs index 0531136..3be6e5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,9 @@ use std::fs::File; #[cfg(target_os = "macos")] use std::io::{self, Read, Write}; -use crate::commands::{ChangeVmCmd, ConfigCmd, CreateCmd, DeleteCmd, ListCmd, StartCmd}; +use crate::commands::{ + ChangeVmCmd, ConfigCmd, CreateCmd, DeleteCmd, InspectCmd, ListCmd, StartCmd, +}; use clap::{Parser, Subcommand}; use serde_derive::{Deserialize, Serialize}; #[cfg(target_os = "macos")] @@ -159,6 +161,7 @@ struct Cli { enum Command { Start(StartCmd), Create(CreateCmd), + Inspect(InspectCmd), List(ListCmd), Delete(DeleteCmd), #[command(name = "changevm")] @@ -176,6 +179,7 @@ fn main() { check_unshare(); match cli_args.command { + Command::Inspect(cmd) => cmd.run(&mut cfg), Command::Start(cmd) => cmd.run(&cfg), Command::Create(cmd) => cmd.run(&mut cfg), Command::List(cmd) => cmd.run(&cfg),