forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b2d.go
542 lines (449 loc) · 13.2 KB
/
b2d.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package mcnutils
import (
"archive/tar"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/version"
)
const (
defaultURL = "https://api.github.com/repos/boot2docker/boot2docker/releases"
defaultISOFilename = "boot2docker.iso"
defaultVolumeIDOffset = int64(0x8028)
versionPrefix = "-v"
defaultVolumeIDLength = 32
)
var (
GithubAPIToken string
)
var (
errGitHubAPIResponse = errors.New(`failure getting a version tag from the Github API response (are you getting rate limited by Github?)`)
)
var (
AUFSBugB2DVersions = map[string]string{
"v1.9.1": "https://github.com/docker/docker/issues/18180",
}
)
func defaultTimeout(network, addr string) (net.Conn, error) {
return net.Dial(network, addr)
}
func getClient() *http.Client {
transport := http.Transport{
DisableKeepAlives: true,
Proxy: http.ProxyFromEnvironment,
Dial: defaultTimeout,
}
return &http.Client{
Transport: &transport,
}
}
func getRequest(apiURL string) (*http.Request, error) {
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, err
}
if GithubAPIToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("token %s", GithubAPIToken))
}
return req, nil
}
// releaseGetter is a client that gets release information of a product and downloads it.
type releaseGetter interface {
// filename returns filename of the product.
filename() string
// getReleaseTag gets a release tag from the given URL.
getReleaseTag(apiURL string) (string, error)
// getReleaseURL gets the latest release download URL from the given URL.
getReleaseURL(apiURL string) (string, error)
// download downloads a file from the given dlURL and saves it under dir.
download(dir, file, dlURL string) error
}
// b2dReleaseGetter implements the releaseGetter interface for getting the release of Boot2Docker.
type b2dReleaseGetter struct {
isoFilename string
}
func (b *b2dReleaseGetter) filename() string {
if b == nil {
return ""
}
return b.isoFilename
}
// getReleaseTag gets the release tag of Boot2Docker from apiURL.
func (*b2dReleaseGetter) getReleaseTag(apiURL string) (string, error) {
if apiURL == "" {
apiURL = defaultURL
}
if !version.RC() {
// Just go straight to the convenience URL for "/latest" if we
// are a non-release candidate version. "/latest" won't return
// non-RCs, so that's what we use for stable releases of
// Machine.
apiURL = apiURL + "/latest"
}
client := getClient()
req, err := getRequest(apiURL)
if err != nil {
return "", err
}
rsp, err := client.Do(req)
if err != nil {
return "", err
}
defer rsp.Body.Close()
// If we call the API endpoint
// "/repos/boot2docker/boot2docker/releases" without specifying
// "/latest", we will receive a list of releases instead of a single
// one, and we should decode accordingly.
if version.RC() {
var tags []struct {
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(rsp.Body).Decode(&tags); err != nil {
return "", err
}
t := tags[0]
if t.TagName == "" {
return "", errGitHubAPIResponse
}
return t.TagName, nil
}
// Otherwise, we get back just one release, which we can decode to get
// the tag.
var t struct {
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(rsp.Body).Decode(&t); err != nil {
return "", err
}
if t.TagName == "" {
return "", errGitHubAPIResponse
}
return t.TagName, nil
}
// getReleaseURL gets the latest release URL of Boot2Docker.
func (b *b2dReleaseGetter) getReleaseURL(apiURL string) (string, error) {
if apiURL == "" {
apiURL = defaultURL
}
// match github (enterprise) release urls:
// https://api.github.com/repos/../../releases or
// https://some.github.enterprise/api/v3/repos/../../releases
re := regexp.MustCompile("(https?)://([^/]+)(/api/v3)?/repos/([^/]+)/([^/]+)/releases")
matches := re.FindStringSubmatch(apiURL)
if len(matches) != 6 {
// does not match a github releases api URL
return apiURL, nil
}
scheme, host, org, repo := matches[1], matches[2], matches[4], matches[5]
if host == "api.github.com" {
host = "github.com"
}
tag, err := b.getReleaseTag(apiURL)
if err != nil {
return "", err
}
log.Infof("Latest release for %s/%s/%s is %s", host, org, repo, tag)
bugURL, ok := AUFSBugB2DVersions[tag]
if ok {
log.Warnf(`
Boot2Docker %s has a known issue with AUFS.
See here for more details: %s
Consider specifying another storage driver (e.g. 'overlay') using '--engine-storage-driver' instead.
`, tag, bugURL)
}
url := fmt.Sprintf("%s://%s/%s/%s/releases/download/%s/%s", scheme, host, org, repo, tag, b.isoFilename)
return url, nil
}
func (*b2dReleaseGetter) download(dir, file, isoURL string) error {
u, err := url.Parse(isoURL)
var src io.ReadCloser
if u.Scheme == "file" || u.Scheme == "" {
s, err := os.Open(u.Path)
if err != nil {
return err
}
src = s
} else {
client := getClient()
s, err := client.Get(isoURL)
if err != nil {
return err
}
src = &ReaderWithProgress{
ReadCloser: s.Body,
out: os.Stdout,
expectedLength: s.ContentLength,
}
}
defer src.Close()
// Download to a temp file first then rename it to avoid partial download.
f, err := ioutil.TempFile(dir, file+".tmp")
if err != nil {
return err
}
defer func() {
if err := removeFileIfExists(f.Name()); err != nil {
log.Warnf("Error removing file: %s", err)
}
}()
if _, err := io.Copy(f, src); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
// Dest is the final path of the boot2docker.iso file.
dest := filepath.Join(dir, file)
// Windows can't rename in place, so remove the old file before
// renaming the temporary downloaded file.
if err := removeFileIfExists(dest); err != nil {
return err
}
return os.Rename(f.Name(), dest)
}
// iso is an ISO volume.
type iso interface {
// path returns the path of the ISO.
path() string
// exists reports whether the ISO exists.
exists() bool
// version returns version information of the ISO.
version() (string, error)
}
// b2dISO represents a Boot2Docker ISO. It implements the ISO interface.
type b2dISO struct {
// path of Boot2Docker ISO
commonIsoPath string
// offset and length of ISO volume ID
// cf. http://serverfault.com/questions/361474/is-there-a-way-to-change-a-iso-files-volume-id-from-the-command-line
volumeIDOffset int64
volumeIDLength int
}
func (b *b2dISO) path() string {
if b == nil {
return ""
}
return b.commonIsoPath
}
func (b *b2dISO) exists() bool {
if b == nil {
return false
}
_, err := os.Stat(b.commonIsoPath)
return !os.IsNotExist(err)
}
// version scans the volume ID in b and returns its version tag.
func (b *b2dISO) version() (string, error) {
if b == nil {
return "", nil
}
iso, err := os.Open(b.commonIsoPath)
if err != nil {
return "", err
}
defer iso.Close()
isoMetadata := make([]byte, b.volumeIDLength)
_, err = iso.ReadAt(isoMetadata, b.volumeIDOffset)
if err != nil {
return "", err
}
trimmedVersion := strings.TrimSpace(string(isoMetadata))
versionIndex := strings.Index(trimmedVersion, versionPrefix)
if versionIndex == -1 {
return "", fmt.Errorf("Did not find prefix %q in version string", versionPrefix)
}
// Original magic file string looks similar to this: "Boot2Docker-v0.1.0 "
// This will return "v0.1.0" given the above string
vers := trimmedVersion[versionIndex+1:]
log.Debug("local Boot2Docker ISO version: ", vers)
return vers, nil
}
func removeFileIfExists(name string) error {
if _, err := os.Stat(name); err == nil {
if err := os.Remove(name); err != nil {
return fmt.Errorf("Error removing temporary download file: %s", err)
}
}
return nil
}
type B2dUtils struct {
releaseGetter
iso
storePath string
imgCachePath string
}
func NewB2dUtils(storePath string) *B2dUtils {
imgCachePath := filepath.Join(storePath, "cache")
return &B2dUtils{
releaseGetter: &b2dReleaseGetter{isoFilename: defaultISOFilename},
iso: &b2dISO{
commonIsoPath: filepath.Join(imgCachePath, defaultISOFilename),
volumeIDOffset: defaultVolumeIDOffset,
volumeIDLength: defaultVolumeIDLength,
},
storePath: storePath,
imgCachePath: imgCachePath,
}
}
// DownloadISO downloads boot2docker ISO image for the given tag and save it at dest.
func (b *B2dUtils) DownloadISO(dir, file, isoURL string) error {
log.Infof("Downloading %s from %s...", b.path(), isoURL)
return b.download(dir, file, isoURL)
}
type ReaderWithProgress struct {
io.ReadCloser
out io.Writer
bytesTransferred int64
expectedLength int64
nextPercentToPrint int64
}
func (r *ReaderWithProgress) Read(p []byte) (int, error) {
n, err := r.ReadCloser.Read(p)
if n > 0 {
r.bytesTransferred += int64(n)
percentage := r.bytesTransferred * 100 / r.expectedLength
for percentage >= r.nextPercentToPrint {
if r.nextPercentToPrint%10 == 0 {
fmt.Fprintf(r.out, "%d%%", r.nextPercentToPrint)
} else if r.nextPercentToPrint%2 == 0 {
fmt.Fprint(r.out, ".")
}
r.nextPercentToPrint += 2
}
}
return n, err
}
func (r *ReaderWithProgress) Close() error {
fmt.Fprintln(r.out)
return r.ReadCloser.Close()
}
func (b *B2dUtils) DownloadLatestBoot2Docker(apiURL string) error {
latestReleaseURL, err := b.getReleaseURL(apiURL)
if err != nil {
return err
}
return b.DownloadISOFromURL(latestReleaseURL)
}
func (b *B2dUtils) DownloadISOFromURL(latestReleaseURL string) error {
return b.DownloadISO(b.imgCachePath, b.filename(), latestReleaseURL)
}
func (b *B2dUtils) UpdateISOCache(isoURL string) error {
// recreate the cache dir if it has been manually deleted
if _, err := os.Stat(b.imgCachePath); os.IsNotExist(err) {
log.Infof("Image cache directory does not exist, creating it at %s...", b.imgCachePath)
if err := os.Mkdir(b.imgCachePath, 0700); err != nil {
return err
}
}
exists := b.exists()
if isoURL != "" {
if exists {
// Warn that the b2d iso won't be updated if isoURL is set
log.Warnf("Boot2Docker URL was explicitly set to %q at create time, so Docker Machine cannot upgrade this machine to the latest version.", isoURL)
}
// Non-default B2D are not cached
return nil
}
if !exists {
log.Info("No default Boot2Docker ISO found locally, downloading the latest release...")
return b.DownloadLatestBoot2Docker("")
}
latest := b.isLatest()
if !latest {
log.Info("Default Boot2Docker ISO is out-of-date, downloading the latest release...")
return b.DownloadLatestBoot2Docker("")
}
return nil
}
func (b *B2dUtils) CopyIsoToMachineDir(isoURL, machineName string) error {
if err := b.UpdateISOCache(isoURL); err != nil {
return err
}
// TODO: This is a bit off-color.
machineDir := filepath.Join(b.storePath, "machines", machineName)
machineIsoPath := filepath.Join(machineDir, b.filename())
// By default just copy the existing "cached" iso to the machine's directory...
if isoURL == "" {
log.Infof("Copying %s to %s...", b.path(), machineIsoPath)
return CopyFile(b.path(), machineIsoPath)
}
// if ISO is specified, check if it matches a github releases url or fallback to a direct download
downloadURL, err := b.getReleaseURL(isoURL)
if err != nil {
return err
}
return b.DownloadISO(machineDir, b.filename(), downloadURL)
}
// isLatest checks the latest release tag and
// reports whether the local ISO cache is the latest version.
//
// It returns false if failing to get the local ISO version
// and true if failing to fetch the latest release tag.
func (b *B2dUtils) isLatest() bool {
localVer, err := b.version()
if err != nil {
log.Warn("Unable to get the local Boot2Docker ISO version: ", err)
return false
}
latestVer, err := b.getReleaseTag("")
if err != nil {
log.Warn("Unable to get the latest Boot2Docker ISO release version: ", err)
return true
}
return localVer == latestVer
}
// MakeDiskImage makes a boot2docker VM disk image.
// See https://github.com/boot2docker/boot2docker/blob/master/rootfs/rootfs/etc/rc.d/automount
func MakeDiskImage(publicSSHKeyPath string) (*bytes.Buffer, error) {
magicString := "boot2docker, please format-me"
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
// magicString first so the automount script knows to format the disk
file := &tar.Header{Name: magicString, Size: int64(len(magicString))}
log.Debug("Writing magic tar header")
if err := tw.WriteHeader(file); err != nil {
return nil, err
}
if _, err := tw.Write([]byte(magicString)); err != nil {
return nil, err
}
// .ssh/key.pub => authorized_keys
file = &tar.Header{Name: ".ssh", Typeflag: tar.TypeDir, Mode: 0700}
if err := tw.WriteHeader(file); err != nil {
return nil, err
}
log.Debug("Writing SSH key tar header")
pubKey, err := ioutil.ReadFile(publicSSHKeyPath)
if err != nil {
return nil, err
}
file = &tar.Header{Name: ".ssh/authorized_keys", Size: int64(len(pubKey)), Mode: 0644}
if err := tw.WriteHeader(file); err != nil {
return nil, err
}
if _, err := tw.Write([]byte(pubKey)); err != nil {
return nil, err
}
file = &tar.Header{Name: ".ssh/authorized_keys2", Size: int64(len(pubKey)), Mode: 0644}
if err := tw.WriteHeader(file); err != nil {
return nil, err
}
if _, err := tw.Write([]byte(pubKey)); err != nil {
return nil, err
}
if err := tw.Close(); err != nil {
return nil, err
}
return buf, nil
}