mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 05:10:55 +00:00
kratactl: make things nicer to use from a scripting standpoint
This commit is contained in:
parent
e25cbf087d
commit
7543fccfaf
@ -1,25 +1,77 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use krata::control::{control_service_client::ControlServiceClient, DestroyGuestRequest};
|
use krata::{
|
||||||
|
common::GuestStatus,
|
||||||
|
control::{
|
||||||
|
control_service_client::ControlServiceClient, watch_events_reply::Event,
|
||||||
|
DestroyGuestRequest,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use log::error;
|
||||||
use tonic::{transport::Channel, Request};
|
use tonic::{transport::Channel, Request};
|
||||||
|
|
||||||
use crate::cli::resolve_guest;
|
use crate::{cli::resolve_guest, events::EventStream};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub struct DestroyCommand {
|
pub struct DestroyCommand {
|
||||||
|
#[arg(short = 'W', long)]
|
||||||
|
wait: bool,
|
||||||
#[arg()]
|
#[arg()]
|
||||||
guest: String,
|
guest: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DestroyCommand {
|
impl DestroyCommand {
|
||||||
pub async fn run(self, mut client: ControlServiceClient<Channel>) -> Result<()> {
|
pub async fn run(
|
||||||
|
self,
|
||||||
|
mut client: ControlServiceClient<Channel>,
|
||||||
|
events: EventStream,
|
||||||
|
) -> Result<()> {
|
||||||
let guest_id: String = resolve_guest(&mut client, &self.guest).await?;
|
let guest_id: String = resolve_guest(&mut client, &self.guest).await?;
|
||||||
let _ = client
|
let _ = client
|
||||||
.destroy_guest(Request::new(DestroyGuestRequest { guest_id }))
|
.destroy_guest(Request::new(DestroyGuestRequest {
|
||||||
|
guest_id: guest_id.clone(),
|
||||||
|
}))
|
||||||
.await?
|
.await?
|
||||||
.into_inner();
|
.into_inner();
|
||||||
println!("destroyed guest: {}", self.guest);
|
if self.wait {
|
||||||
|
wait_guest_destroyed(&guest_id, events).await?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn wait_guest_destroyed(id: &str, events: EventStream) -> Result<()> {
|
||||||
|
let mut stream = events.subscribe();
|
||||||
|
while let Ok(event) = stream.recv().await {
|
||||||
|
match event {
|
||||||
|
Event::GuestChanged(changed) => {
|
||||||
|
let Some(guest) = changed.guest else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if guest.id != id {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(state) = guest.state else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(ref error) = state.error_info {
|
||||||
|
if state.status() == GuestStatus::Failed {
|
||||||
|
error!("destroy failed: {}", error.message);
|
||||||
|
std::process::exit(1);
|
||||||
|
} else {
|
||||||
|
error!("guest error: {}", error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.status() == GuestStatus::Destroyed {
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
@ -24,6 +24,8 @@ pub struct LauchCommand {
|
|||||||
env: Option<Vec<String>>,
|
env: Option<Vec<String>>,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
attach: bool,
|
attach: bool,
|
||||||
|
#[arg(short = 'W', long)]
|
||||||
|
wait: bool,
|
||||||
#[arg()]
|
#[arg()]
|
||||||
oci: String,
|
oci: String,
|
||||||
#[arg(allow_hyphen_values = true, trailing_var_arg = true)]
|
#[arg(allow_hyphen_values = true, trailing_var_arg = true)]
|
||||||
@ -53,8 +55,12 @@ impl LauchCommand {
|
|||||||
.await?
|
.await?
|
||||||
.into_inner();
|
.into_inner();
|
||||||
let id = response.guest_id;
|
let id = response.guest_id;
|
||||||
let code = if self.attach {
|
|
||||||
|
if self.wait || self.attach {
|
||||||
wait_guest_started(&id, events.clone()).await?;
|
wait_guest_started(&id, events.clone()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let code = if self.attach {
|
||||||
let input = StdioConsoleStream::stdin_stream(id.clone()).await;
|
let input = StdioConsoleStream::stdin_stream(id.clone()).await;
|
||||||
let output = client.console_data(input).await?.into_inner();
|
let output = client.console_data(input).await?.into_inner();
|
||||||
let stdout_handle =
|
let stdout_handle =
|
||||||
@ -68,7 +74,7 @@ impl LauchCommand {
|
|||||||
x = exit_hook_task => x?
|
x = exit_hook_task => x?
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("created guest: {}", id);
|
println!("{}", id);
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
StdioConsoleStream::restore_terminal_mode();
|
StdioConsoleStream::restore_terminal_mode();
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
use clap::{Parser, ValueEnum};
|
use clap::{Parser, ValueEnum};
|
||||||
use cli_tables::Table;
|
use cli_tables::Table;
|
||||||
use krata::{
|
use krata::{
|
||||||
common::{guest_image_spec::Image, Guest},
|
common::{guest_image_spec::Image, Guest},
|
||||||
control::{control_service_client::ControlServiceClient, ListGuestsRequest},
|
control::{
|
||||||
|
control_service_client::ControlServiceClient, ListGuestsRequest, ResolveGuestRequest,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
@ -28,6 +30,8 @@ enum ListFormat {
|
|||||||
pub struct ListCommand {
|
pub struct ListCommand {
|
||||||
#[arg(short, long, default_value = "cli-table")]
|
#[arg(short, long, default_value = "cli-table")]
|
||||||
format: ListFormat,
|
format: ListFormat,
|
||||||
|
#[arg()]
|
||||||
|
guest: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ListCommand {
|
impl ListCommand {
|
||||||
@ -36,19 +40,34 @@ impl ListCommand {
|
|||||||
mut client: ControlServiceClient<Channel>,
|
mut client: ControlServiceClient<Channel>,
|
||||||
_events: EventStream,
|
_events: EventStream,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let response = client
|
let guests = if let Some(ref guest) = self.guest {
|
||||||
.list_guests(Request::new(ListGuestsRequest {}))
|
let reply = client
|
||||||
.await?
|
.resolve_guest(Request::new(ResolveGuestRequest {
|
||||||
.into_inner();
|
name: guest.clone(),
|
||||||
|
}))
|
||||||
|
.await?
|
||||||
|
.into_inner();
|
||||||
|
if let Some(guest) = reply.guest {
|
||||||
|
vec![guest]
|
||||||
|
} else {
|
||||||
|
return Err(anyhow!("unable to resolve guest '{}'", guest));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
client
|
||||||
|
.list_guests(Request::new(ListGuestsRequest {}))
|
||||||
|
.await?
|
||||||
|
.into_inner()
|
||||||
|
.guests
|
||||||
|
};
|
||||||
|
|
||||||
match self.format {
|
match self.format {
|
||||||
ListFormat::CliTable => {
|
ListFormat::CliTable => {
|
||||||
self.print_guest_table(response.guests)?;
|
self.print_guest_table(guests)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
ListFormat::Json | ListFormat::JsonPretty | ListFormat::Yaml => {
|
ListFormat::Json | ListFormat::JsonPretty | ListFormat::Yaml => {
|
||||||
let mut values = Vec::new();
|
let mut values = Vec::new();
|
||||||
for guest in response.guests {
|
for guest in guests {
|
||||||
let message = proto2dynamic(guest)?;
|
let message = proto2dynamic(guest)?;
|
||||||
values.push(serde_json::to_value(message)?);
|
values.push(serde_json::to_value(message)?);
|
||||||
}
|
}
|
||||||
@ -64,14 +83,14 @@ impl ListCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ListFormat::Jsonl => {
|
ListFormat::Jsonl => {
|
||||||
for guest in response.guests {
|
for guest in guests {
|
||||||
let message = proto2dynamic(guest)?;
|
let message = proto2dynamic(guest)?;
|
||||||
println!("{}", serde_json::to_string(&message)?);
|
println!("{}", serde_json::to_string(&message)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ListFormat::KeyValue => {
|
ListFormat::KeyValue => {
|
||||||
self.print_key_value(response.guests)?;
|
self.print_key_value(guests)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +137,9 @@ impl ListCommand {
|
|||||||
])?;
|
])?;
|
||||||
}
|
}
|
||||||
if table.num_records() == 1 {
|
if table.num_records() == 1 {
|
||||||
println!("no guests have been launched");
|
if self.guest.is_none() {
|
||||||
|
println!("no guests have been launched");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("{}", table.to_string());
|
println!("{}", table.to_string());
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ impl ControlCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Commands::Destroy(destroy) => {
|
Commands::Destroy(destroy) => {
|
||||||
destroy.run(client).await?;
|
destroy.run(client, events).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Attach(attach) => {
|
Commands::Attach(attach) => {
|
||||||
@ -93,6 +93,6 @@ pub async fn resolve_guest(
|
|||||||
if let Some(guest) = reply.guest {
|
if let Some(guest) = reply.guest {
|
||||||
Ok(guest.id)
|
Ok(guest.id)
|
||||||
} else {
|
} else {
|
||||||
Err(anyhow!("unable to resolve guest {}", name))
|
Err(anyhow!("unable to resolve guest '{}'", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user