mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 09:04:12 +02:00
interactive exec
This commit is contained in:
parent
e373f33cff
commit
f6b1010b55
4 changed files with 81 additions and 11 deletions
31
Cargo.lock
generated
31
Cargo.lock
generated
|
|
@ -65,8 +65,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
|||
[[package]]
|
||||
name = "bollard"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "699194c00f3a2effd3358d47f880646818e3d483190b17ebcdf598c654fb77e9"
|
||||
source = "git+https://github.com/icewind1991/bollard?branch=exec-input#31651016742357aa632944b05bb032cd0ce7d8ed"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bollard-stubs",
|
||||
|
|
@ -385,6 +384,7 @@ dependencies = [
|
|||
"maplit",
|
||||
"min-id",
|
||||
"parse-display",
|
||||
"termion",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
|
|
@ -640,6 +640,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "numtoa"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.23.0"
|
||||
|
|
@ -807,6 +813,15 @@ dependencies = [
|
|||
"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]]
|
||||
name = "redox_users"
|
||||
version = "0.4.0"
|
||||
|
|
@ -985,6 +1000,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "thiserror"
|
||||
version = "1.0.24"
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@ authors = ["Robin Appelman <robin@icewind.nl>"]
|
|||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bollard = "0.10"
|
||||
bollard = { version = "0.10", git = "https://github.com/icewind1991/bollard", branch = "exec-input" }
|
||||
color-eyre = "0.5"
|
||||
min-id = "0.1"
|
||||
maplit = "1"
|
||||
camino = "1"
|
||||
tokio = { version = "1" , features = ["fs", "macros"] }
|
||||
parse-display = "0.4"
|
||||
futures-util = "0.3"
|
||||
futures-util = "0.3"
|
||||
termion = "1"
|
||||
45
src/cloud.rs
45
src/cloud.rs
|
|
@ -12,11 +12,17 @@ use futures_util::stream::StreamExt;
|
|||
use min_id::generate_id;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::io::{stdout, Read};
|
||||
use std::net::IpAddr;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use termion::async_stdin;
|
||||
use termion::raw::IntoRawMode;
|
||||
use tokio::fs::{create_dir_all, remove_dir_all, write};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::task::spawn;
|
||||
use tokio::time::sleep;
|
||||
|
||||
#[derive(Default, Debug, Eq, PartialEq)]
|
||||
|
|
@ -293,18 +299,45 @@ impl Cloud {
|
|||
user: Some("haze".to_string()),
|
||||
attach_stdout: Some(true),
|
||||
attach_stderr: Some(true),
|
||||
attach_stdin: Some(true),
|
||||
tty: Some(true),
|
||||
..Default::default()
|
||||
};
|
||||
let message = docker.create_exec(&self.id, config).await?;
|
||||
let mut exec = docker.start_exec(&message.id, None);
|
||||
while let Some(res) = exec.next().await {
|
||||
match res? {
|
||||
StartExecResults::Attached { log } => {
|
||||
print!("{}", log);
|
||||
if let StartExecResults::AttachedTTY {
|
||||
mut output,
|
||||
mut input,
|
||||
} = docker.start_exec(&message.id, None, true).await?
|
||||
{
|
||||
// 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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
src/main.rs
11
src/main.rs
|
|
@ -72,7 +72,16 @@ async fn main() -> Result<()> {
|
|||
}
|
||||
HazeCommand::Exec => {
|
||||
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 => {
|
||||
let cloud = get_by_filter(&mut docker, None, &config).await?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue