2024-03-15 15:59:18 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use clap::Parser;
|
2024-03-27 02:54:39 +00:00
|
|
|
use krata::{
|
|
|
|
events::EventStream,
|
|
|
|
v1::{
|
|
|
|
common::GuestStatus,
|
|
|
|
control::{
|
|
|
|
control_service_client::ControlServiceClient, watch_events_reply::Event,
|
|
|
|
DestroyGuestRequest,
|
|
|
|
},
|
2024-03-23 10:09:00 +00:00
|
|
|
},
|
|
|
|
};
|
2024-03-15 15:59:18 +00:00
|
|
|
|
2024-03-23 10:09:00 +00:00
|
|
|
use log::error;
|
2024-03-15 15:59:18 +00:00
|
|
|
use tonic::{transport::Channel, Request};
|
|
|
|
|
2024-03-27 02:54:39 +00:00
|
|
|
use crate::cli::resolve_guest;
|
2024-03-15 15:59:18 +00:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
2024-04-06 00:00:02 +00:00
|
|
|
#[command(about = "Destroy a guest")]
|
2024-03-15 15:59:18 +00:00
|
|
|
pub struct DestroyCommand {
|
2024-04-06 00:00:02 +00:00
|
|
|
#[arg(
|
|
|
|
short = 'W',
|
|
|
|
long,
|
|
|
|
help = "Wait for the destruction of the guest to complete"
|
|
|
|
)]
|
2024-03-23 10:09:00 +00:00
|
|
|
wait: bool,
|
2024-04-06 00:00:02 +00:00
|
|
|
#[arg(help = "Guest to destroy, either the name or the uuid")]
|
2024-03-15 15:59:18 +00:00
|
|
|
guest: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DestroyCommand {
|
2024-03-23 10:09:00 +00:00
|
|
|
pub async fn run(
|
|
|
|
self,
|
|
|
|
mut client: ControlServiceClient<Channel>,
|
|
|
|
events: EventStream,
|
|
|
|
) -> Result<()> {
|
2024-03-23 09:48:53 +00:00
|
|
|
let guest_id: String = resolve_guest(&mut client, &self.guest).await?;
|
2024-03-15 15:59:18 +00:00
|
|
|
let _ = client
|
2024-03-23 10:09:00 +00:00
|
|
|
.destroy_guest(Request::new(DestroyGuestRequest {
|
|
|
|
guest_id: guest_id.clone(),
|
|
|
|
}))
|
2024-03-15 15:59:18 +00:00
|
|
|
.await?
|
|
|
|
.into_inner();
|
2024-03-23 10:09:00 +00:00
|
|
|
if self.wait {
|
|
|
|
wait_guest_destroyed(&guest_id, events).await?;
|
|
|
|
}
|
2024-03-15 15:59:18 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 10:09:00 +00:00
|
|
|
|
|
|
|
async fn wait_guest_destroyed(id: &str, events: EventStream) -> Result<()> {
|
|
|
|
let mut stream = events.subscribe();
|
|
|
|
while let Ok(event) = stream.recv().await {
|
2024-04-12 18:09:26 +00:00
|
|
|
if let Event::GuestChanged(changed) = event {
|
|
|
|
let Some(guest) = changed.guest else {
|
|
|
|
continue;
|
|
|
|
};
|
2024-03-23 10:09:00 +00:00
|
|
|
|
2024-04-12 18:09:26 +00:00
|
|
|
if guest.id != id {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-23 10:09:00 +00:00
|
|
|
|
2024-04-12 18:09:26 +00:00
|
|
|
let Some(state) = guest.state else {
|
|
|
|
continue;
|
|
|
|
};
|
2024-03-23 10:09:00 +00:00
|
|
|
|
2024-04-12 18:09:26 +00:00
|
|
|
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);
|
2024-03-23 10:09:00 +00:00
|
|
|
}
|
2024-04-12 18:09:26 +00:00
|
|
|
}
|
2024-03-23 10:09:00 +00:00
|
|
|
|
2024-04-12 18:09:26 +00:00
|
|
|
if state.status() == GuestStatus::Destroyed {
|
|
|
|
std::process::exit(0);
|
2024-03-23 10:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|