1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 09:04:12 +02:00

interactive exec

This commit is contained in:
Robin Appelman 2021-03-13 20:24:17 +01:00
commit f6b1010b55
4 changed files with 81 additions and 11 deletions

31
Cargo.lock generated
View file

@ -65,8 +65,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]] [[package]]
name = "bollard" name = "bollard"
version = "0.10.1" version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/icewind1991/bollard?branch=exec-input#31651016742357aa632944b05bb032cd0ce7d8ed"
checksum = "699194c00f3a2effd3358d47f880646818e3d483190b17ebcdf598c654fb77e9"
dependencies = [ dependencies = [
"base64", "base64",
"bollard-stubs", "bollard-stubs",
@ -385,6 +384,7 @@ dependencies = [
"maplit", "maplit",
"min-id", "min-id",
"parse-display", "parse-display",
"termion",
"tokio", "tokio",
] ]
@ -640,6 +640,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]] [[package]]
name = "object" name = "object"
version = "0.23.0" version = "0.23.0"
@ -807,6 +813,15 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "redox_termios"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
dependencies = [
"redox_syscall",
]
[[package]] [[package]]
name = "redox_users" name = "redox_users"
version = "0.4.0" version = "0.4.0"
@ -985,6 +1000,18 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "termion"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
dependencies = [
"libc",
"numtoa",
"redox_syscall",
"redox_termios",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.24" version = "1.0.24"

View file

@ -5,11 +5,12 @@ authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
bollard = "0.10" bollard = { version = "0.10", git = "https://github.com/icewind1991/bollard", branch = "exec-input" }
color-eyre = "0.5" color-eyre = "0.5"
min-id = "0.1" min-id = "0.1"
maplit = "1" maplit = "1"
camino = "1" camino = "1"
tokio = { version = "1" , features = ["fs", "macros"] } tokio = { version = "1" , features = ["fs", "macros"] }
parse-display = "0.4" parse-display = "0.4"
futures-util = "0.3" futures-util = "0.3"
termion = "1"

View file

@ -12,11 +12,17 @@ use futures_util::stream::StreamExt;
use min_id::generate_id; use min_id::generate_id;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use std::io::Write;
use std::io::{stdout, Read};
use std::net::IpAddr; use std::net::IpAddr;
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
use std::str::FromStr; use std::str::FromStr;
use std::time::Duration; use std::time::Duration;
use termion::async_stdin;
use termion::raw::IntoRawMode;
use tokio::fs::{create_dir_all, remove_dir_all, write}; use tokio::fs::{create_dir_all, remove_dir_all, write};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::task::spawn;
use tokio::time::sleep; use tokio::time::sleep;
#[derive(Default, Debug, Eq, PartialEq)] #[derive(Default, Debug, Eq, PartialEq)]
@ -293,18 +299,45 @@ impl Cloud {
user: Some("haze".to_string()), user: Some("haze".to_string()),
attach_stdout: Some(true), attach_stdout: Some(true),
attach_stderr: Some(true), attach_stderr: Some(true),
attach_stdin: Some(true),
tty: Some(true),
..Default::default() ..Default::default()
}; };
let message = docker.create_exec(&self.id, config).await?; let message = docker.create_exec(&self.id, config).await?;
let mut exec = docker.start_exec(&message.id, None); if let StartExecResults::AttachedTTY {
while let Some(res) = exec.next().await { mut output,
match res? { mut input,
StartExecResults::Attached { log } => { } = docker.start_exec(&message.id, None, true).await?
print!("{}", log); {
// pipe stdin into the docker exec stream input
spawn(async move {
let mut stdin = async_stdin().bytes();
loop {
if let Some(Ok(byte)) = stdin.next() {
input.write(&[byte]).await.ok();
} else {
sleep(Duration::from_nanos(10)).await;
}
} }
_ => {} });
// set stdout in raw mode so we can do tty stuff
let stdout = stdout();
let mut stdout = stdout.lock().into_raw_mode()?;
// pipe docker exec output into stdout
let mut buff = [0; 128];
while let Ok(read) = output.read(&mut buff).await {
if read == 0 {
break;
}
stdout.write(&buff[0..read])?;
stdout.flush()?;
} }
} else {
unreachable!();
} }
Ok(()) Ok(())
} }
} }

View file

@ -72,7 +72,16 @@ async fn main() -> Result<()> {
} }
HazeCommand::Exec => { HazeCommand::Exec => {
let cloud = get_by_filter(&mut docker, None, &config).await?; let cloud = get_by_filter(&mut docker, None, &config).await?;
cloud.exec(&mut docker, args.options).await?; cloud
.exec(
&mut docker,
if args.options.is_empty() {
vec!["bash".to_string()]
} else {
args.options
},
)
.await?;
} }
HazeCommand::Occ => { HazeCommand::Occ => {
let cloud = get_by_filter(&mut docker, None, &config).await?; let cloud = get_by_filter(&mut docker, None, &config).await?;