Skip to content

Commit

Permalink
Add inspect command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KevSlashNull authored Jan 6, 2025
1 parent 765614c commit ccdd1fa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/commands/inspect.rs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ mod changevm;
mod config;
mod create;
mod delete;
mod inspect;
mod list;
mod start;

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;
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -159,6 +161,7 @@ struct Cli {
enum Command {
Start(StartCmd),
Create(CreateCmd),
Inspect(InspectCmd),
List(ListCmd),
Delete(DeleteCmd),
#[command(name = "changevm")]
Expand All @@ -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),
Expand Down

0 comments on commit ccdd1fa

Please sign in to comment.