forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_driver.go
406 lines (333 loc) · 8.18 KB
/
dynamic_driver.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package nodedriver
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var dockerMachineDriverPrefix = "docker-machine-driver-"
type Driver struct {
builtin bool
url string
hash string
name string
}
func NewDriver(builtin bool, name, url, hash string) *Driver {
d := &Driver{
builtin: builtin,
name: name,
url: url,
hash: hash,
}
if !strings.HasPrefix(d.name, dockerMachineDriverPrefix) {
d.name = dockerMachineDriverPrefix + d.name
}
return d
}
func (d *Driver) Name() string {
return d.name
}
func (d *Driver) Hash() string {
return d.hash
}
func (d *Driver) Checksum() string {
return d.name
}
func (d *Driver) FriendlyName() string {
return strings.TrimPrefix(d.name, dockerMachineDriverPrefix)
}
func (d *Driver) Remove() error {
cacheFilePrefix := d.cacheFile()
content, err := ioutil.ReadFile(cacheFilePrefix)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
dest := path.Join(binDir(), string(content))
os.Remove(dest)
os.Remove(cacheFilePrefix + "-" + string(content))
os.Remove(cacheFilePrefix)
return nil
}
func (d *Driver) Stage() error {
if err := d.getError(); err != nil {
return err
}
return d.setError(d.stage())
}
func (d *Driver) setError(err error) error {
errFile := d.cacheFile() + ".error"
if err != nil {
os.MkdirAll(path.Dir(errFile), 0700)
ioutil.WriteFile(errFile, []byte(err.Error()), 0600)
}
return err
}
func (d *Driver) getError() error {
errFile := d.cacheFile() + ".error"
if content, err := ioutil.ReadFile(errFile); err == nil {
logrus.Errorf("Returning previous error: %s", content)
d.ClearError()
return errors.New(string(content))
}
return nil
}
func (d *Driver) ClearError() {
errFile := d.cacheFile() + ".error"
os.Remove(errFile)
}
func (d *Driver) stage() error {
if d.builtin {
return nil
}
cacheFilePrefix := d.cacheFile()
driverName, err := isInstalled(cacheFilePrefix)
if err != nil || driverName != "" {
d.name = driverName
return err
}
tempFile, err := ioutil.TempFile("", "machine-driver")
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
defer tempFile.Close()
hasher, err := getHasher(d.hash)
if err != nil {
return err
}
downloadDest := io.Writer(tempFile)
if hasher != nil {
downloadDest = io.MultiWriter(tempFile, hasher)
}
if err := d.download(downloadDest); err != nil {
return err
}
if got, ok := compare(hasher, d.hash); !ok {
return fmt.Errorf("hash does not match, got %s, expected %s", got, d.hash)
}
if err := tempFile.Close(); err != nil {
return err
}
driverName, err = d.copyBinary(cacheFilePrefix, tempFile.Name())
if err != nil {
return err
}
d.name = driverName
return nil
}
func (d *Driver) Exists() bool {
if d.name == "" {
return false
}
if d.builtin {
return true
}
binaryPath := path.Join(binDir(), d.name)
_, err := os.Stat(binaryPath)
return err == nil
}
func (d *Driver) Excutable() error {
if d.name == "" {
return fmt.Errorf("Empty driver name")
}
if d.builtin {
return nil
}
binaryPath := path.Join(binDir(), d.name)
_, err := os.Stat(binaryPath)
if err != nil {
return fmt.Errorf("Driver %s not found", binaryPath)
}
err = exec.Command(binaryPath).Start()
if err != nil {
return errors.Wrapf(err, "Driver binary %s couldn't execute", binaryPath)
}
return nil
}
func (d *Driver) Install() error {
if d.builtin {
return nil
}
binaryPath := path.Join(binDir(), d.name)
tmpPath := binaryPath + "-tmp"
f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return errors.Wrapf(err, "Couldn't open %v for writing", tmpPath)
}
defer f.Close()
src, err := os.Open(d.srcBinName())
if err != nil {
return errors.Wrapf(err, "Couldn't open %v for copying", d.srcBinName())
}
defer src.Close()
logrus.Infof("Copying %v => %v", d.srcBinName(), tmpPath)
_, err = io.Copy(f, src)
if err != nil {
return errors.Wrapf(err, "Couldn't copy %v to %v", d.srcBinName(), tmpPath)
}
err = os.Rename(tmpPath, binaryPath)
if err != nil {
return errors.Wrapf(err, "Couldn't copy driver %v to %v", d.Name(), binaryPath)
}
return nil
}
func isElf(input string) bool {
f, err := os.Open(input)
if err != nil {
return false
}
defer f.Close()
elf := make([]byte, 4)
if _, err := f.Read(elf); err != nil {
return false
}
//support unix binary and mac-os binary mach-o
return bytes.Compare(elf, []byte{0x7f, 0x45, 0x4c, 0x46}) == 0 || bytes.Compare(elf, []byte{0xcf, 0xfa, 0xed, 0xfe}) == 0
}
func (d *Driver) copyBinary(cacheFile, input string) (string, error) {
temp, err := ioutil.TempDir("", "machine-driver-extract")
if err != nil {
return "", err
}
defer os.RemoveAll(temp)
file := ""
driverName := ""
if isElf(input) {
file = input
u, err := url.Parse(d.url)
if err != nil {
return "", err
}
if !strings.HasPrefix(path.Base(u.Path), dockerMachineDriverPrefix) {
return "", fmt.Errorf("invalid URL %s, path should be of the format docker-machine-driver-*", d.url)
}
s := strings.TrimPrefix(path.Base(u.Path), dockerMachineDriverPrefix)
name := strings.FieldsFunc(s, func(r rune) bool {
return r == '-' || r == '_' || r == '.'
})[0]
if name == "" {
return "", fmt.Errorf("invalid URL %s, NAME is empty, path should be of the format docker-machine-driver-NAME", d.url)
}
driverName = dockerMachineDriverPrefix + name
} else {
if err := exec.Command("tar", "xvf", input, "-C", temp).Run(); err != nil {
if err := exec.Command("unzip", "-o", input, "-d", temp).Run(); err != nil {
return "", fmt.Errorf("failed to extract")
}
}
}
filepath.Walk(temp, filepath.WalkFunc(func(p string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasPrefix(path.Base(p), dockerMachineDriverPrefix) {
file = p
}
return nil
}))
if file == "" {
return "", fmt.Errorf("failed to find machine driver in archive. There must be a file of form docker-machine-driver*")
}
if driverName == "" {
driverName = path.Base(file)
}
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
if err := os.MkdirAll(path.Dir(cacheFile), 0755); err != nil {
return "", err
}
driverName = strings.ToLower(driverName)
dest, err := os.Create(cacheFile + "-" + driverName)
if err != nil {
return "", err
}
defer dest.Close()
if _, err := io.Copy(dest, f); err != nil {
return "", err
}
logrus.Infof("Found driver %s", driverName)
return driverName, ioutil.WriteFile(cacheFile, []byte(driverName), 0644)
}
func (d *Driver) srcBinName() string {
return d.cacheFile() + "-" + d.name
}
func binDir() string {
if dl := os.Getenv("CATTLE_DEV_MODE"); dl != "" {
return "./management-state/bin"
}
return "/opt/drivers/management-state/bin"
}
func compare(hash hash.Hash, value string) (string, bool) {
if hash == nil {
return "", true
}
got := hex.EncodeToString(hash.Sum([]byte{}))
expected := strings.TrimSpace(strings.ToLower(value))
return got, got == expected
}
func getHasher(hash string) (hash.Hash, error) {
switch len(hash) {
case 0:
return nil, nil
case 32:
return md5.New(), nil
case 40:
return sha1.New(), nil
case 64:
return sha256.New(), nil
case 128:
return sha512.New(), nil
}
return nil, fmt.Errorf("invalid hash format: %s", hash)
}
func (d *Driver) download(dest io.Writer) error {
logrus.Infof("Download %s", d.url)
resp, err := http.Get(d.url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(dest, resp.Body)
return err
}
func (d *Driver) cacheFile() string {
key := sha256Bytes([]byte(d.url + d.hash))
base := os.Getenv("CATTLE_HOME")
if base == "" {
base = "./management-state"
}
return path.Join(base, "machine-drivers", key)
}
func isInstalled(file string) (string, error) {
content, err := ioutil.ReadFile(file)
if os.IsNotExist(err) {
return "", nil
}
return strings.ToLower(strings.TrimSpace(string(content))), err
}
func sha256Bytes(content []byte) string {
hash := sha256.New()
io.Copy(hash, bytes.NewBuffer(content))
return hex.EncodeToString(hash.Sum([]byte{}))
}