2024-07-18 23:13:29 -07:00
|
|
|
pub mod device;
|
|
|
|
pub mod host;
|
|
|
|
pub mod image;
|
|
|
|
pub mod zone;
|
|
|
|
|
|
|
|
use crate::cli::device::DeviceCommand;
|
|
|
|
use crate::cli::host::HostCommand;
|
|
|
|
use crate::cli::image::ImageCommand;
|
|
|
|
use crate::cli::zone::ZoneCommand;
|
2024-03-23 09:48:53 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2024-07-18 23:13:29 -07:00
|
|
|
use clap::Parser;
|
2024-03-27 02:54:39 +00:00
|
|
|
use krata::{
|
|
|
|
client::ControlClientProvider,
|
|
|
|
events::EventStream,
|
2024-07-18 20:47:18 -07:00
|
|
|
v1::control::{control_service_client::ControlServiceClient, ResolveZoneRequest},
|
2024-03-23 09:48:53 +00:00
|
|
|
};
|
|
|
|
use tonic::{transport::Channel, Request};
|
2024-03-15 15:59:18 +00:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
2024-06-21 01:10:45 -07:00
|
|
|
#[command(version, about = "Control the krata isolation engine")]
|
2024-03-15 15:59:18 +00:00
|
|
|
pub struct ControlCommand {
|
2024-04-05 17:00:02 -07:00
|
|
|
#[arg(
|
|
|
|
short,
|
|
|
|
long,
|
2024-06-20 19:57:18 -07:00
|
|
|
help = "The connection URL to the krata isolation engine",
|
2024-04-05 17:00:02 -07:00
|
|
|
default_value = "unix:///var/lib/krata/daemon.socket"
|
|
|
|
)]
|
2024-03-15 15:59:18 +00:00
|
|
|
connection: String,
|
|
|
|
|
|
|
|
#[command(subcommand)]
|
2024-07-18 23:13:29 -07:00
|
|
|
command: ControlCommands,
|
2024-03-15 15:59:18 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 23:13:29 -07:00
|
|
|
#[derive(Parser)]
|
|
|
|
pub enum ControlCommands {
|
|
|
|
Zone(ZoneCommand),
|
|
|
|
Image(ImageCommand),
|
|
|
|
Device(DeviceCommand),
|
|
|
|
Host(HostCommand),
|
2024-03-15 15:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ControlCommand {
|
|
|
|
pub async fn run(self) -> Result<()> {
|
2024-03-31 01:11:50 +00:00
|
|
|
let client = ControlClientProvider::dial(self.connection.parse()?).await?;
|
|
|
|
let events = EventStream::open(client.clone()).await?;
|
2024-03-15 15:59:18 +00:00
|
|
|
|
|
|
|
match self.command {
|
2024-07-18 23:13:29 -07:00
|
|
|
ControlCommands::Zone(zone) => zone.run(client, events).await,
|
2024-04-22 13:13:43 -07:00
|
|
|
|
2024-07-18 23:13:29 -07:00
|
|
|
ControlCommands::Image(image) => image.run(client, events).await,
|
2024-04-29 10:02:20 -07:00
|
|
|
|
2024-07-18 23:13:29 -07:00
|
|
|
ControlCommands::Device(device) => device.run(client, events).await,
|
2024-06-29 15:43:08 -07:00
|
|
|
|
2024-07-18 23:13:29 -07:00
|
|
|
ControlCommands::Host(snoop) => snoop.run(client, events).await,
|
2024-03-15 15:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 09:48:53 +00:00
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
pub async fn resolve_zone(
|
2024-03-23 09:48:53 +00:00
|
|
|
client: &mut ControlServiceClient<Channel>,
|
|
|
|
name: &str,
|
|
|
|
) -> Result<String> {
|
|
|
|
let reply = client
|
2024-07-18 20:47:18 -07:00
|
|
|
.resolve_zone(Request::new(ResolveZoneRequest {
|
2024-03-23 09:48:53 +00:00
|
|
|
name: name.to_string(),
|
|
|
|
}))
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
if let Some(zone) = reply.zone {
|
|
|
|
Ok(zone.id)
|
2024-03-23 09:48:53 +00:00
|
|
|
} else {
|
2024-07-18 20:47:18 -07:00
|
|
|
Err(anyhow!("unable to resolve zone '{}'", name))
|
2024-03-23 09:48:53 +00:00
|
|
|
}
|
|
|
|
}
|