/
docker_exec.go
51 lines (42 loc) · 1.07 KB
/
docker_exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package orchestrator
import (
"bytes"
"context"
"fmt"
"time"
"github.com/ory/dockertest/v3/docker"
)
// ExecCommand executes a command on the validator container
// and outputs the stdout and stderr to the console
func (o *Orchestrator) ExecCommand(cmd []string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
validator := o.chain.validators[1]
exec, err := o.dkrPool.Client.CreateExec(docker.CreateExecOptions{
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: validator.dockerResource.Container.ID,
User: "root",
Cmd: cmd,
})
if err != nil {
return err
}
var (
outBuf bytes.Buffer
errBuf bytes.Buffer
)
err = o.dkrPool.Client.StartExec(exec.ID, docker.StartExecOptions{
Context: ctx,
Detach: false,
OutputStream: &outBuf,
ErrorStream: &errBuf,
})
if err != nil {
return fmt.Errorf("exec command failed; stdout: %s, stderr: %s", outBuf.String(), errBuf.String())
}
fmt.Println(errBuf.String())
fmt.Println(outBuf.String())
return nil
}