-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
130 lines (110 loc) · 3.29 KB
/
util.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
// Copyright 2015 YP LLC.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
// collection of utility methods used in docker-wrapper
import (
"encoding/json"
"log"
"os"
"os/exec"
"strings"
"syscall"
)
const (
// docker-wrapper is expected to be installed *outside* of below safe PATH so
// we can find the real docker binary in dockerDo
// e.g. /go/bin/docker-wrapper
SafeDockerSearchPath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
)
// parseJsonFromString uses the generic interface{} to read arbitrary data
func parseJsonFromString(jsonstring string) (interface{}, error) {
var f interface{}
err := json.Unmarshal([]byte(jsonstring), &f)
return f, err
}
// ---------------------------------------------------------------------------
// check os.Args for the single word "run"
func simpleIsDockerRunCommand(args []string) bool {
for i := range args {
if args[i] == "run" {
return true
}
}
return false
}
// findBinary uses a restricted PATH to find an executable
func findBinary(name string) (string, error) {
os.Setenv("PATH", SafeDockerSearchPath)
return exec.LookPath(name)
}
// sh runs a subprocess and returns the String output
func sh(name string, argv ...string) (string, error) {
// try to find the binary first
binary, err := findBinary(name)
if err != nil {
panic(err)
}
cmd := exec.Command(binary, argv...)
if isDebugEnabled() {
log.Printf("DEBUG: sh CMD: %q", cmd)
}
out, err := cmd.Output()
return strings.Trim(string(out), " \n"), err
}
// dockerExec execs out to real `docker` binary with argv arguments, replacing
// current process
func dockerExec(argv []string) {
// grab the (pre-update) environment for our Exec later
env := os.Environ()
dockerBinary, err := findBinary("docker")
if err != nil {
panic(err)
}
// setup new args slice for real docker binary cli
// syscall.Exec requires "docker" as first arg also
newArgs := []string{"docker"}
newArgs = append(newArgs, argv...)
if isDebugEnabled() {
log.Printf("DEBUG: docker binary: %s", dockerBinary)
log.Printf("DEBUG: docker args: %v", newArgs)
}
dockerError := syscall.Exec(dockerBinary, newArgs, env)
if dockerError != nil {
panic(dockerError)
}
}
// injectRunArgs takes the current argument list and another argument list to
// inject after the "docker run" portions of the command arguments
func injectRunArgs(args []string, inject_args []string) []string {
if inject_args == nil || len(inject_args) == 0 {
return args
}
newArgs := []string{}
if isDebugEnabled() {
log.Printf("DEBUG: request to inject args: %q", inject_args)
}
// find the run command argument index
runIndex := -1
for i := range args {
if args[i] == "run" {
runIndex = i
}
}
if runIndex == -1 {
// something funny - not docker run? just append all args
newArgs = args
} else {
// split the difference and merge in new args after "run"
newArgs = append(newArgs, args[0:runIndex+1]...)
newArgs = append(newArgs, inject_args...)
newArgs = append(newArgs, args[runIndex+1:]...)
}
return newArgs
}
////////////////////////////////////////
////////////////////////////////////////
// dockerInspect uses docker to inspect an image or a container
func dockerInspect(name string) (string, error) {
return sh("docker", "inspect", name)
}