Files
krata/crates/kratactl/src/console.rs

105 lines
3.1 KiB
Rust
Raw Normal View History

use anyhow::Result;
use async_stream::stream;
2024-03-14 14:03:11 +00:00
use krata::{
common::GuestStatus,
control::{watch_events_reply::Event, ConsoleDataReply, ConsoleDataRequest},
2024-03-13 11:34:52 +00:00
};
use log::debug;
2024-03-21 19:54:14 -07:00
#[cfg(unix)]
use std::os::fd::{AsRawFd, FromRawFd};
#[cfg(unix)]
use termion::raw::IntoRawMode;
2024-03-21 19:54:14 -07:00
#[cfg(unix)]
use tokio::fs::File;
use tokio::{
io::{stdin, AsyncReadExt, AsyncWriteExt},
2024-03-13 11:34:52 +00:00
task::JoinHandle,
};
use tokio_stream::{Stream, StreamExt};
use tonic::Streaming;
use crate::events::EventStream;
pub struct StdioConsoleStream;
impl StdioConsoleStream {
pub async fn stdin_stream(guest: String) -> impl Stream<Item = ConsoleDataRequest> {
let mut stdin = stdin();
stream! {
2024-03-06 15:57:56 +00:00
yield ConsoleDataRequest { guest_id: guest, data: vec![] };
let mut buffer = vec![0u8; 60];
loop {
let size = match stdin.read(&mut buffer).await {
Ok(size) => size,
Err(error) => {
debug!("failed to read stdin: {}", error);
break;
}
};
let data = buffer[0..size].to_vec();
if size == 1 && buffer[0] == 0x1d {
break;
}
2024-03-06 15:57:56 +00:00
yield ConsoleDataRequest { guest_id: String::default(), data };
}
}
}
pub async fn stdout(mut stream: Streaming<ConsoleDataReply>) -> Result<()> {
2024-03-21 19:54:14 -07:00
#[cfg(unix)]
let terminal = std::io::stdout().into_raw_mode()?;
#[cfg(unix)]
let mut stdout =
unsafe { File::from_std(std::fs::File::from_raw_fd(terminal.as_raw_fd())) };
#[cfg(not(unix))]
let mut stdout = tokio::io::stdout();
while let Some(reply) = stream.next().await {
let reply = reply?;
if reply.data.is_empty() {
continue;
}
stdout.write_all(&reply.data).await?;
stdout.flush().await?;
}
Ok(())
}
2024-03-13 11:34:52 +00:00
pub async fn guest_exit_hook(
id: String,
events: EventStream,
) -> Result<JoinHandle<Option<i32>>> {
2024-03-13 11:34:52 +00:00
Ok(tokio::task::spawn(async move {
let mut stream = events.subscribe();
while let Ok(event) = stream.recv().await {
match event {
Event::GuestChanged(changed) => {
let Some(guest) = changed.guest else {
2024-03-13 11:34:52 +00:00
continue;
};
let Some(state) = guest.state else {
continue;
};
2024-03-14 14:03:11 +00:00
if guest.id != id {
continue;
}
2024-03-13 11:34:52 +00:00
if let Some(exit_info) = state.exit_info {
return Some(exit_info.code);
}
2024-03-13 11:34:52 +00:00
let status = state.status();
if status == GuestStatus::Destroy || status == GuestStatus::Destroyed {
return Some(10);
2024-03-13 11:34:52 +00:00
}
}
}
}
None
2024-03-13 11:34:52 +00:00
}))
}
}