downloader.go 2.8 KB
Newer Older
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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
	"crypto"
	"net/url"
	"os"
	"path/filepath"

	"github.com/golang/glog"
	download "github.com/jimmidyson/go-download"
	"github.com/pkg/errors"
28
	"k8s.io/minikube/pkg/minikube/console"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	"k8s.io/minikube/pkg/minikube/constants"
)

const fileScheme = "file"

type ISODownloader interface {
	GetISOFileURI(isoURL string) string
	CacheMinikubeISOFromURL(isoURL string) error
}

type DefaultDownloader struct{}

func (f DefaultDownloader) GetISOFileURI(isoURL string) string {
	urlObj, err := url.Parse(isoURL)
	if err != nil {
		return isoURL
	}
	if urlObj.Scheme == fileScheme {
		return isoURL
	}
49
	isoPath := filepath.Join(constants.GetMinipath(), "cache", "iso", filepath.Base(isoURL))
50 51 52 53 54
	// As this is a file URL there should be no backslashes regardless of platform running on.
	return "file://" + filepath.ToSlash(isoPath)
}

func (f DefaultDownloader) CacheMinikubeISOFromURL(isoURL string) error {
55
	if !f.ShouldCacheMinikubeISO(isoURL) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		glog.Infof("Not caching ISO, using %s", isoURL)
		return nil
	}

	options := download.FileOptions{
		Mkdirs: download.MkdirAll,
		Options: download.Options{
			ProgressBars: &download.ProgressBarOptions{
				MaxWidth: 80,
			},
		},
	}

	// Validate the ISO if it was the default URL, before writing it to disk.
	if isoURL == constants.DefaultIsoUrl {
		options.Checksum = constants.DefaultIsoShaUrl
		options.ChecksumHash = crypto.SHA256
	}

75
	console.OutStyle("iso-download", "Downloading Minikube ISO ...")
76
	if err := download.ToFile(isoURL, f.GetISOCacheFilepath(isoURL), options); err != nil {
77
		return errors.Wrap(err, isoURL)
78 79 80 81 82
	}

	return nil
}

83
func (f DefaultDownloader) ShouldCacheMinikubeISO(isoURL string) bool {
84
	// store the minikube-iso inside the .minikube dir
85 86 87 88 89 90 91 92

	urlObj, err := url.Parse(isoURL)
	if err != nil {
		return false
	}
	if urlObj.Scheme == fileScheme {
		return false
	}
93
	if f.IsMinikubeISOCached(isoURL) {
94 95 96 97 98
		return false
	}
	return true
}

99
func (f DefaultDownloader) GetISOCacheFilepath(isoURL string) string {
100
	return filepath.Join(constants.GetMinipath(), "cache", "iso", filepath.Base(isoURL))
101 102
}

103 104
func (f DefaultDownloader) IsMinikubeISOCached(isoURL string) bool {
	if _, err := os.Stat(f.GetISOCacheFilepath(isoURL)); os.IsNotExist(err) {
105 106 107 108
		return false
	}
	return true
}