runservice drivers: define ExecConfig AttachStdin

This commit is contained in:
Simone Gotti 2019-05-10 16:50:31 +02:00
parent f8b0061844
commit 7adca3ea30
6 changed files with 77 additions and 58 deletions

View File

@ -395,7 +395,7 @@ func (dp *DockerPod) Exec(ctx context.Context, execConfig *ExecConfig) (Containe
Env: makeEnvSlice(execConfig.Env), Env: makeEnvSlice(execConfig.Env),
Tty: execConfig.Tty, Tty: execConfig.Tty,
WorkingDir: execConfig.WorkingDir, WorkingDir: execConfig.WorkingDir,
AttachStdin: true, AttachStdin: execConfig.AttachStdin,
AttachStdout: execConfig.Stdout != nil, AttachStdout: execConfig.Stdout != nil,
AttachStderr: execConfig.Stderr != nil, AttachStderr: execConfig.Stderr != nil,
User: execConfig.User, User: execConfig.User,
@ -451,11 +451,18 @@ func (e *DockerContainerExec) Wait(ctx context.Context) (int, error) {
// ignore error, we'll use the exit code of the exec // ignore error, we'll use the exit code of the exec
<-e.endCh <-e.endCh
resp, err := e.client.ContainerExecInspect(ctx, e.execID) var exitCode int
if err != nil { for {
return -1, err resp, err := e.client.ContainerExecInspect(ctx, e.execID)
if err != nil {
return -1, err
}
if !resp.Running {
exitCode = resp.ExitCode
break
}
time.Sleep(500 * time.Millisecond)
} }
exitCode := resp.ExitCode
e.hresp.Close() e.hresp.Close()

View File

@ -137,7 +137,6 @@ func TestDockerPod(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
@ -180,7 +179,6 @@ func TestDockerPod(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)

View File

@ -96,13 +96,14 @@ type ContainerConfig struct {
} }
type ExecConfig struct { type ExecConfig struct {
Cmd []string Cmd []string
Env map[string]string Env map[string]string
WorkingDir string WorkingDir string
User string User string
Stdout io.Writer AttachStdin bool
Stderr io.Writer Stdout io.Writer
Tty bool Stderr io.Writer
Tty bool
} }
func toolboxExecPath(toolboxDir string, arch common.Arch) (string, error) { func toolboxExecPath(toolboxDir string, arch common.Arch) (string, error) {

View File

@ -670,7 +670,7 @@ func (p *K8sPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExe
VersionedParams(&corev1.PodExecOptions{ VersionedParams(&corev1.PodExecOptions{
Container: mainContainerName, Container: mainContainerName,
Command: cmd, Command: cmd,
Stdin: true, Stdin: execConfig.AttachStdin,
Stdout: execConfig.Stdout != nil, Stdout: execConfig.Stdout != nil,
Stderr: execConfig.Stderr != nil, Stderr: execConfig.Stderr != nil,
TTY: execConfig.Tty, TTY: execConfig.Tty,
@ -683,9 +683,14 @@ func (p *K8sPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExe
reader, writer := io.Pipe() reader, writer := io.Pipe()
var stdin io.Reader
if execConfig.AttachStdin {
stdin = reader
}
go func() { go func() {
err := exec.Stream(remotecommand.StreamOptions{ err := exec.Stream(remotecommand.StreamOptions{
Stdin: reader, Stdin: stdin,
Stdout: execConfig.Stdout, Stdout: execConfig.Stdout,
Stderr: execConfig.Stderr, Stderr: execConfig.Stderr,
Tty: execConfig.Tty, Tty: execConfig.Tty,

View File

@ -81,14 +81,15 @@ func TestK8sPod(t *testing.T) {
} }
defer pod.Remove(ctx) defer pod.Remove(ctx)
var buf bytes.Buffer
ce, err := pod.Exec(ctx, &ExecConfig{ ce, err := pod.Exec(ctx, &ExecConfig{
Cmd: []string{"ls"}, Cmd: []string{"ls"},
Stdout: &buf,
}) })
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
@ -131,7 +132,6 @@ func TestK8sPod(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
ce.Stdin().Close()
code, err := ce.Wait(ctx) code, err := ce.Wait(ctx)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)

View File

@ -70,10 +70,11 @@ func (e *Executor) createFile(ctx context.Context, pod driver.Pod, command, user
var buf bytes.Buffer var buf bytes.Buffer
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Stdout: &buf, AttachStdin: true,
Stderr: outf, Stdout: &buf,
User: user, Stderr: outf,
User: user,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -161,13 +162,14 @@ func (e *Executor) doRunStep(ctx context.Context, s *types.RunStep, t *types.Exe
} }
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: environment, Env: environment,
WorkingDir: workingDir, WorkingDir: workingDir,
User: user, User: user,
Stdout: outf, AttachStdin: true,
Stderr: outf, Stdout: outf,
Tty: true, Stderr: outf,
Tty: true,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -211,11 +213,12 @@ func (e *Executor) doSaveToWorkspaceStep(ctx context.Context, s *types.SaveToWor
} }
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
Stdout: archivef, AttachStdin: true,
Stderr: logf, Stdout: archivef,
Stderr: logf,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -271,10 +274,11 @@ func (e *Executor) expandDir(ctx context.Context, t *types.ExecutorTask, pod dri
stdout := &bytes.Buffer{} stdout := &bytes.Buffer{}
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
Stdout: stdout, AttachStdin: true,
Stderr: logf, Stdout: stdout,
Stderr: logf,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -298,10 +302,11 @@ func (e *Executor) mkdir(ctx context.Context, t *types.ExecutorTask, pod driver.
cmd := append([]string{toolboxContainerPath, "mkdir"}, args...) cmd := append([]string{toolboxContainerPath, "mkdir"}, args...)
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
Stdout: logf, AttachStdin: true,
Stderr: logf, Stdout: logf,
Stderr: logf,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -333,11 +338,12 @@ func (e *Executor) template(ctx context.Context, t *types.ExecutorTask, pod driv
} }
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
Stdout: stdout, AttachStdin: true,
Stderr: logf, Stdout: stdout,
Stderr: logf,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -379,11 +385,12 @@ func (e *Executor) unarchive(ctx context.Context, t *types.ExecutorTask, source
} }
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
Stdout: logf, AttachStdin: true,
Stderr: logf, Stdout: logf,
Stderr: logf,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)
@ -496,11 +503,12 @@ func (e *Executor) doSaveCacheStep(ctx context.Context, s *types.SaveCacheStep,
} }
execConfig := &driver.ExecConfig{ execConfig := &driver.ExecConfig{
Cmd: cmd, Cmd: cmd,
Env: t.Environment, Env: t.Environment,
WorkingDir: workingDir, WorkingDir: workingDir,
Stdout: archivef, AttachStdin: true,
Stderr: logf, Stdout: archivef,
Stderr: logf,
} }
ce, err := pod.Exec(ctx, execConfig) ce, err := pod.Exec(ctx, execConfig)