-
Notifications
You must be signed in to change notification settings - Fork 346
/
docker.go
134 lines (120 loc) · 4.14 KB
/
docker.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2019 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exec
import (
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
"sync"
)
// TODO: we could use the Docker Go SDK for this, but it seems like a big dependency to pull in just
// to run some "docker exec" commands.
// RunDockerExecCommand runs the provided command on the specified host using "docker exec". Returns
// the exit code of the command, along with the contents of stdout and stderr as strings. Note that
// if the command returns a non-zero error code, this function does not report it as an error.
func RunDockerExecCommand(container, cmd, workdir string, envs map[string]string, stdin string) (
code int, stdout string, stderr string, err error,
) {
args := []string{"exec"}
// Set environment variables.
for e, v := range envs {
args = append(args, "-e", e+"="+v)
}
if stdin != "" {
// Interactive mode to get input from stdin.
args = append(args, "-i")
}
args = append(args, "-w", workdir, container)
if strings.Contains(cmd, "/bin/sh") {
// Just split in to "/bin/sh" "-c" and "actual_cmd"
// This is useful for passing piped commands in to os/exec interface.
args = append(args, strings.SplitN(cmd, " ", 3)...)
} else {
args = append(args, strings.Fields(cmd)...)
}
dockerCmd := exec.Command("docker", args...)
stdoutPipe, err := dockerCmd.StdoutPipe()
if err != nil {
return 0, "", "", fmt.Errorf("error when connecting to stdout: %v", err)
}
stderrPipe, err := dockerCmd.StderrPipe()
if err != nil {
return 0, "", "", fmt.Errorf("error when connecting to stderr: %v", err)
}
if stdin != "" {
stdinPipe, err := dockerCmd.StdinPipe()
if err != nil {
return 0, "", "", fmt.Errorf("error when connecting to stdin: %v", err)
}
_, err = io.WriteString(stdinPipe, stdin)
stdinPipe.Close()
if err != nil {
return 0, "", "", fmt.Errorf("error when writing to stdin: %v", err)
}
}
if err := dockerCmd.Start(); err != nil {
return 0, "", "", fmt.Errorf("error when starting command: %v", err)
}
var stdoutBytes, stderrBytes []byte
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
stdoutBytes, _ = ioutil.ReadAll(stdoutPipe)
}()
go func() {
defer wg.Done()
stderrBytes, _ = ioutil.ReadAll(stderrPipe)
}()
wg.Wait()
if err := dockerCmd.Wait(); err != nil {
if e, ok := err.(*exec.ExitError); ok {
return e.ExitCode(), string(stdoutBytes), string(stderrBytes), nil
}
return 0, "", "", err
}
// command is successful
return 0, string(stdoutBytes), string(stderrBytes), nil
}
// RunDockerPsFilterCommand runs the provided command on the specified host using "docker ps filter". Returns
// the exit code of the command, along with the contents of stdout and stderr as strings.
func RunDockerPsFilterCommand(filter string) (
code int, stdout string, stderr string, err error,
) {
args := []string{"ps", "--filter", filter}
dockerCmd := exec.Command("docker", args...)
stdoutPipe, err := dockerCmd.StdoutPipe()
if err != nil {
return 0, "", "", fmt.Errorf("error when connecting to stdout: %v", err)
}
stderrPipe, err := dockerCmd.StderrPipe()
if err != nil {
return 0, "", "", fmt.Errorf("error when connecting to stderr: %v", err)
}
if err := dockerCmd.Start(); err != nil {
return 0, "", "", fmt.Errorf("error when starting command: %v", err)
}
stdoutBytes, _ := ioutil.ReadAll(stdoutPipe)
stderrBytes, _ := ioutil.ReadAll(stderrPipe)
if err := dockerCmd.Wait(); err != nil {
if e, ok := err.(*exec.ExitError); ok {
return e.ExitCode(), string(stdoutBytes), string(stderrBytes), nil
}
return 0, "", "", err
}
// command is successful
return 0, string(stdoutBytes), string(stderrBytes), nil
}