1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

exec fixes

This commit is contained in:
Robin Appelman 2021-03-30 21:11:16 +02:00
commit ec6df26bf7
3 changed files with 21 additions and 14 deletions

2
Cargo.lock generated
View file

@ -65,7 +65,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bollard"
version = "0.10.1"
source = "git+https://github.com/icewind1991/bollard?branch=exec-input#31651016742357aa632944b05bb032cd0ce7d8ed"
source = "git+https://github.com/icewind1991/bollard?branch=marged#f220f0cc1f4e8637ed6402074597fa84c6212d85"
dependencies = [
"base64",
"bollard-stubs",

View file

@ -5,7 +5,7 @@ authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018"
[dependencies]
bollard = { version = "0.10", git = "https://github.com/icewind1991/bollard", branch = "exec-input" }
bollard = { version = "0.10", git = "https://github.com/icewind1991/bollard", branch = "marged" }
color-eyre = "0.5"
maplit = "1"
camino = { version = "1", features = ["serde1"] }

View file

@ -1,13 +1,13 @@
use bollard::container::LogsOptions;
use bollard::exec::{CreateExecOptions, StartExecResults};
use bollard::exec::{CreateExecOptions, ResizeExecOptions, StartExecResults};
use bollard::Docker;
use color_eyre::{eyre::WrapErr, Result};
use futures_util::StreamExt;
use std::io::{stdout, Read, Write};
use std::time::Duration;
use termion::async_stdin;
use termion::raw::IntoRawMode;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use termion::{async_stdin, terminal_size};
use tokio::io::AsyncWriteExt;
use tokio::task::spawn;
use tokio::time::sleep;
@ -18,6 +18,7 @@ pub async fn exec_tty<S1: AsRef<str>, S2: Into<String>>(
cmd: Vec<S2>,
env: Vec<&str>,
) -> Result<i64> {
let tty_size = terminal_size()?;
let cmd = cmd.into_iter().map(S2::into).collect();
let env = env.into_iter().map(String::from).collect();
let config = CreateExecOptions {
@ -34,14 +35,24 @@ pub async fn exec_tty<S1: AsRef<str>, S2: Into<String>>(
.create_exec(container.as_ref(), config)
.await
.wrap_err("Failed to setup exec")?;
if let StartExecResults::AttachedTTY {
if let StartExecResults::Attached {
mut output,
mut input,
} = docker
.start_exec(&message.id, None, true)
.start_exec(&message.id, None)
.await
.wrap_err("Failed to start exec")?
{
docker
.resize_exec(
&message.id,
ResizeExecOptions {
height: tty_size.1,
width: tty_size.0,
},
)
.await?;
// pipe stdin into the docker exec stream input
spawn(async move {
let mut stdin = async_stdin().bytes();
@ -59,12 +70,8 @@ pub async fn exec_tty<S1: AsRef<str>, S2: Into<String>>(
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])?;
while let Some(Ok(output)) = output.next().await {
stdout.write(output.into_bytes().as_ref())?;
stdout.flush()?;
}
} else {
@ -101,7 +108,7 @@ pub async fn exec<S1: AsRef<str>, S2: Into<String>>(
.await
.wrap_err("Failed to setup exec")?;
if let StartExecResults::Attached { mut output, .. } = docker
.start_exec(&message.id, None, false)
.start_exec(&message.id, None)
.await
.wrap_err("Failed to start exec")?
{