/// Helper to run a command in a container.
///
/// This command is always run with `bash -c` to ensure env vars are set up as expected.
///
/// The stdout/stderr for this command are echoed live to this process' stdout/stderr for now,
/// and also combined into a single string which is returned along with the results of the exec operation
Helper to run a command in a container.
This command is always run with `bash -c` to ensure env vars are set up as expected.

The stdout/stderr for this command are echoed live to this process' stdout/stderr for now,
and also combined into a single string which is returned along with the results of the exec operation
Helper to run a command in a container . This command is always run with ` bash - c ` to ensure env vars are set up as expected . The stdout / stderr for this command are echoed live to this process ' stdout / stderr for now and also combined into a single string which is returned along with the results of the exec operation
pub(crate) async fn exec_in_container<'docker, C, S>(
    docker: &'docker Docker,
    container: &Container<'docker>,
    cmd: C,
) -> Result<(ExecDetails, String)>
where
    C: AsRef<[S]>,
    S: AsRef<str>,
{
    // Make the command a single argument to `bash -c`
    let args = cmd
        .as_ref()
        .iter()
        .map(|c| format!("\"{}\"", c.as_ref()))
        .collect::<Vec<_>>()
        .join(" ");
    debug!(command = %args,
        "Running command in container");
    let exec_options = ExecContainerOptions::builder()
        .cmd(vec!["bash", "-c", &args])
        .attach_stderr(true)
        .attach_stdout(true)
        .build();
    let exec = Exec::create(&docker, container.id(), &exec_options)
        .await
        .wrap_err_with(|| "Error executing command in container")?;
    exec.inspect().await?;
    // For simplicity, we'll bypass the logging framework for the test, and write the output verbatim to our
    // stdout/stderr streams
    let mut stream = exec.start();
    let mut stdout = io::stdout();
    let mut stderr = io::stderr();
    let mut output = Vec::new();
    while let Some(item) = stream.next().await {
        match item {
            Err(e) => {
                error!("Docker exec error: {}", e);
                return Err(e.into());
            }
            Ok(TtyChunk::StdOut(chunk)) => {
                stdout.write_all(&chunk)?;
                output.write_all(&chunk)?;
            }
            Ok(TtyChunk::StdErr(chunk)) => {
                stderr.write_all(&chunk)?;
                output.write_all(&chunk)?;
            }
            Ok(TtyChunk::StdIn(_)) => {
                unreachable!()
            }
        }
    }
    // Presumably, execution has finished
    let results = exec.inspect().await?;
    if results.running {
        return Err(eyre!(
            "Process is still running when it was expected to be finished"
        ));
    }
    Ok((results, String::from_utf8_lossy(&output).to_string()))
}
pub ( crate ) async fn exec_in_container < ' docker , C , S > ( docker : & ' docker Docker , container : & Container < ' docker > , cmd : C , ) -> Result < ( ExecDetails , String ) > where C : AsRef < [ S ] > , S : AsRef < str > , { // Make the command a single argument to `bash -c` let args = cmd . as_ref ( ) . iter ( ) . map ( | c | format ! ( " \" \" " c as_ref ( ) ) ) . collect :: < Vec < _ > > ( ) . join ( " " ) ; debug ! ( command args " " ) ; let exec_options = ExecContainerOptions :: builder ( ) . cmd ( vec ! [ " " " " args ] ) . attach_stderr ( true ) . attach_stdout ( true ) . build ( ) ; let exec = Exec :: create ( & docker , container . id ( ) , & exec_options ) . await . wrap_err_with ( | | " " ) ? ; exec . inspect ( ) . await ? ; // For simplicity, we'll bypass the logging framework for the test, and write the output verbatim to our // stdout/stderr streams let mut stream = exec . start ( ) ; let mut stdout = io :: stdout ( ) ; let mut stderr = io :: stderr ( ) ; let mut output = Vec :: new ( ) ; while let Some ( item ) = stream . next ( ) . await { match item { Err ( e ) => { error ! ( " " e ) ; return Err ( e . into ( ) ) ; } Ok ( TtyChunk :: StdOut ( chunk ) ) => { stdout . write_all ( & chunk ) ? ; output . write_all ( & chunk ) ? ; } Ok ( TtyChunk :: StdErr ( chunk ) ) => { stderr . write_all ( & chunk ) ? ; output . write_all ( & chunk ) ? ; } Ok ( TtyChunk :: StdIn ( _ ) ) => { unreachable ! ( ) } } } // Presumably, execution has finished let results = exec . inspect ( ) . await ? ; if results . running { return Err ( eyre ! ( " " ) ) ; } Ok ( ( results , String :: from_utf8_lossy ( & output ) . to_string ( ) ) ) }
Helper to run a command in a container.
Helper to run a command in a container .
docker
&'docker Docker
container
&Container<'docker>
cmd
C