This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exec.go
108 lines (96 loc) · 3.19 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package exec
import (
"bytes"
"io"
"io/ioutil"
"github.com/docker/engine-api/types"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/bootstrap/docker/dockerhelper"
"github.com/openshift/origin/pkg/bootstrap/docker/errors"
)
// ExecHelper allows execution of commands on a running Docker container
type ExecHelper struct {
client dockerhelper.Interface
container string
}
// ExecCommand is a command to execute with the helper
type ExecCommand struct {
helper *ExecHelper
cmd []string
input io.Reader
}
// NewExecHelper creates a new ExecHelper
func NewExecHelper(client dockerhelper.Interface, container string) *ExecHelper {
return &ExecHelper{
client: client,
container: container,
}
}
// Command creates a new command to execute
func (h *ExecHelper) Command(cmd ...string) *ExecCommand {
return &ExecCommand{
helper: h,
cmd: cmd,
}
}
// Input sets an input reader on the exec command
func (c *ExecCommand) Input(in io.Reader) *ExecCommand {
c.input = in
return c
}
// Output executes the command and returns seprate stderr and stdout
func (c *ExecCommand) Output() (string, string, error) {
stdOut, errOut := &bytes.Buffer{}, &bytes.Buffer{}
err := exec(c.helper, c.cmd, c.input, stdOut, errOut)
return stdOut.String(), errOut.String(), err
}
// CombinedOutput executes the command and returns a single output
func (c *ExecCommand) CombinedOutput() (string, error) {
out := &bytes.Buffer{}
err := exec(c.helper, c.cmd, c.input, out, out)
return out.String(), err
}
// Run executes the command
func (c *ExecCommand) Run() error {
return exec(c.helper, c.cmd, c.input, ioutil.Discard, ioutil.Discard)
}
func exec(h *ExecHelper, cmd []string, stdIn io.Reader, stdOut, errOut io.Writer) error {
glog.V(4).Infof("Remote exec on container: %s\nCommand: %v", h.container, cmd)
response, err := h.client.ContainerExecCreate(h.container, types.ExecConfig{
AttachStdin: stdIn != nil,
AttachStdout: true,
AttachStderr: true,
Cmd: cmd,
})
if err != nil {
return errors.NewError("Cannot create exec for command %v on container %s", cmd, h.container).WithCause(err)
}
glog.V(5).Infof("Created exec %q", response.ID)
logOut, logErr := &bytes.Buffer{}, &bytes.Buffer{}
outStream := io.MultiWriter(stdOut, logOut)
errStream := io.MultiWriter(errOut, logErr)
glog.V(5).Infof("Starting exec %q and blocking", response.ID)
err = h.client.ContainerExecAttach(response.ID, stdIn, outStream, errStream)
if err != nil {
return errors.NewError("Cannot start exec for command %v on container %s", cmd, h.container).WithCause(err)
}
if glog.V(5) {
glog.Infof("Exec %q completed", response.ID)
if logOut.Len() > 0 {
glog.Infof("Stdout:\n%s", logOut.String())
}
if logErr.Len() > 0 {
glog.Infof("Stderr:\n%s", logErr.String())
}
}
glog.V(5).Infof("Inspecting exec %q", response.ID)
info, err := h.client.ContainerExecInspect(response.ID)
if err != nil {
return errors.NewError("Cannot inspect result of exec for command %v on container %s", cmd, h.container).WithCause(err)
}
glog.V(5).Infof("Exec %q info: %#v", response.ID, info)
if info.ExitCode != 0 {
return newExecError(err, info.ExitCode, logOut.String(), logErr.String(), h.container, cmd)
}
return nil
}