/
docker.go
96 lines (80 loc) · 3.09 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
package util
import (
"fmt"
"os/exec"
"regexp"
"strings"
"time"
"github.com/hashicorp/go-version"
)
// GetRawCurrentDockerVersion returns the entire semver string from the docker version cli
func GetRawCurrentDockerVersion() string {
output, _ := Command("docker", "--version").Output() // nolint: gosec
re := regexp.MustCompile("Docker version (.*),")
return re.FindAllStringSubmatch(string(output), -1)[0][1]
}
// GetCurrentDockerVersion returns a Version based in the Docker semver
func GetCurrentDockerVersion() *version.Version {
versionNumber := GetRawCurrentDockerVersion()
return version.Must(version.NewVersion(versionNumber))
}
// GetDockerClientAPIVersion returns a Version for the docker client API version
func GetDockerClientAPIVersion() *version.Version {
output, _ := Command("docker", "version", "--format", "{{.Client.APIVersion}}").Output() // nolint: gosec
re := regexp.MustCompile(`^([\d|\.]+)`)
versionNumber := re.FindAllStringSubmatch(string(output), -1)[0][1]
return version.Must(version.NewVersion(versionNumber))
}
// GetDockerServerAPIVersion returns a Version for the docker server API version
func GetDockerServerAPIVersion() (*version.Version, error) {
output, err := Command("docker", "version", "--format", "{{.Server.APIVersion}}").Output()
if err != nil {
return nil, err
}
return version.Must(version.NewVersion(strings.TrimSpace(string(output)))), nil
}
// GetDockerServerMinAPIVersion returns the minimum compatability version for the docker server
func GetDockerServerMinAPIVersion() (*version.Version, error) {
output, err := Command("docker", "version", "--format", "{{.Server.MinAPIVersion}}").Output()
if err != nil {
return nil, err
}
return version.Must(version.NewVersion(strings.TrimSpace(string(output)))), nil
}
// ContainerRunning determines if the named container is live.
func ContainerRunning(name string) bool {
filter := fmt.Sprintf("name=^/%s", name)
if out, err := Command("docker", "ps", "-aq", "--filter", filter).Output(); err == nil {
id := strings.TrimSpace(string(out))
/* #nosec */
_, code, err := CaptureCommand(exec.Command("docker", "top", id))
return code == 0 && err == nil
}
return false
}
// ImageOlderThan determines the age of the Docker Image and whether the image is older than the designated timestamp.
func ImageOlderThan(image string, elapsedSeconds float64) (bool, float64, error) {
output, err := Command("docker", "inspect", "--format", "{{.Created}}", image).Output()
if err != nil {
return false, 0, err
}
datestring := strings.TrimSpace(string(output))
datetime, err := time.Parse(time.RFC3339, datestring)
if err != nil {
return false, 0, err
}
seconds := time.Since(datetime).Seconds()
return seconds > elapsedSeconds, seconds, nil
}
// GetBridgeIP returns the IP address of the Docker bridge network gateway
func GetBridgeIP() (string, error) {
output, err := Command("docker", "network", "inspect", "bridge", "--format", "{{(index .IPAM.Config 0).Gateway}}").Output()
if err != nil {
return "", err
}
bip := strings.Trim(string(output), "\n")
if bip == "" {
bip = "172.17.0.1"
}
return bip, nil
}