forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_driver.go
339 lines (276 loc) · 6.75 KB
/
base_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
package drivers
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"
)
type BaseDriver struct {
Builtin bool
URL string
DriverHash string
DriverName string
BinaryPrefix string
}
func (d *BaseDriver) Name() string {
return d.DriverName
}
func (d *BaseDriver) Hash() string {
return d.DriverHash
}
func (d *BaseDriver) Checksum() string {
return d.DriverName
}
func (d *BaseDriver) FriendlyName() string {
return strings.TrimPrefix(d.DriverName, d.BinaryPrefix)
}
func (d *BaseDriver) 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 *BaseDriver) Stage() error {
if err := d.getError(); err != nil {
return err
}
return d.setError(d.stage())
}
func (d *BaseDriver) 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 *BaseDriver) 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 *BaseDriver) ClearError() {
errFile := d.cacheFile() + ".error"
os.Remove(errFile)
}
func (d *BaseDriver) stage() error {
if d.Builtin {
return nil
}
cacheFilePrefix := d.cacheFile()
driverName, err := isInstalled(cacheFilePrefix)
if err != nil || driverName != "" {
d.DriverName = 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.DriverHash)
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.DriverHash); !ok {
return fmt.Errorf("hash does not match, got %s, expected %s", got, d.DriverHash)
}
if err := tempFile.Close(); err != nil {
return err
}
driverName, err = d.copyBinary(cacheFilePrefix, tempFile.Name())
if err != nil {
return err
}
d.DriverName = driverName
return nil
}
func (d *BaseDriver) Exists() bool {
if d.DriverName == "" {
return false
}
if d.Builtin {
return true
}
binaryPath := path.Join(binDir(), d.DriverName)
_, err := os.Stat(binaryPath)
return err == 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 *BaseDriver) 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), d.BinaryPrefix) {
return "", fmt.Errorf("invalid URL %s, path should be of the format %s*", d.URL, d.BinaryPrefix)
}
s := strings.TrimPrefix(path.Base(u.Path), d.BinaryPrefix)
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 %sNAME", d.URL, d.BinaryPrefix)
}
driverName = d.BinaryPrefix + 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), d.BinaryPrefix) {
file = p
}
return nil
}))
if file == "" {
return "", fmt.Errorf("failed to find driver in archive. There must be a file of form %s*", d.BinaryPrefix)
}
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 *BaseDriver) srcBinName() string {
return d.cacheFile() + "-" + d.DriverName
}
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 *BaseDriver) 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 *BaseDriver) cacheFile() string {
key := sha256Bytes([]byte(d.URL + d.DriverHash))
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{}))
}