This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 395
/
setup.go
151 lines (128 loc) · 3.03 KB
/
setup.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"archive/tar"
"compress/gzip"
"io"
"net/http"
"os"
"path"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
)
const (
downloadURL = "https://download.docker.com/linux/static/stable/x86_64/docker-"
rcDownloadURL = "https://test.docker.com/builds/Linux/x86_64/docker-"
)
// GetDockerBinary ensures that we have the right version docker client
// for communicating with the Docker Daemon
func (d *Daemon) GetDockerBinary() error {
// name of docker binary that is needed
d.BinaryName = "docker-" + d.Version
log.Infof("looking for docker binary named: %s", d.BinaryName)
filename := path.Join("/bin", d.BinaryName)
if _, err := os.Stat(filename); os.IsNotExist(err) {
log.Infof("docker binary (version %s) not found.", d.Version)
log.Infof("downloading %s...", d.BinaryName)
out, err := os.Create(filename)
if err != nil {
return err
}
defer out.Close()
// determine if we're using an RC build of docker
url := downloadURL
if strings.Contains(d.Version, "rc") {
url = rcDownloadURL
}
// the method of downloading it is different for version >= 1.11.0
// (in which case it is an archive containing multiple binaries)
versionComp, err := compareVersions(d.Version, "1.11.0")
if err != nil {
return err
}
if versionComp >= 0 {
err = getClient(out, url+d.Version+".tgz", extractClient)
if err != nil {
return err
}
} else {
err = getClient(out, url+d.Version, copyClient)
if err != nil {
return err
}
}
err = os.Chmod(filename, 0700)
if err != nil {
return err
}
} else {
log.Infof("docker binary (version %s) found!", d.Version)
}
return nil
}
// Utility functions
type copier func(out *os.File, resp *http.Response) error
func compareVersions(v1 string, v2 string) (comp int, err error) {
v1Parts := strings.Split(v1, ".")
v2Parts := strings.Split(v2, ".")
for i := 0; i < len(v1Parts) && i < len(v2Parts); i++ {
v1int, err := strconv.Atoi(v1Parts[i])
if err != nil {
return -2, err
}
v2int, err := strconv.Atoi(v2Parts[i])
if err != nil {
return -2, err
}
if v1int < v2int {
return -1, nil
} else if v1int > v2int {
return 1, nil
}
}
if len(v1Parts) < len(v2Parts) {
return -1, nil
} else if len(v1Parts) > len(v2Parts) {
return 1, nil
}
return 0, nil
}
func getClient(out *os.File, URL string, cp copier) error {
resp, err := http.Get(URL)
if err != nil {
return err
}
defer resp.Body.Close()
err = cp(out, resp)
return err
}
func copyClient(out *os.File, resp *http.Response) error {
_, err := io.Copy(out, resp.Body)
return err
}
func extractClient(out *os.File, resp *http.Response) error {
gr, err := gzip.NewReader(resp.Body)
defer gr.Close()
if err != nil {
return err
}
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if hdr.Typeflag == tar.TypeReg && hdr.Name == "docker/docker" {
_, err = io.Copy(out, tr)
if err != nil {
return err
}
break
}
// logrus.Println("not yet")
}
return nil
}