From 072991e2e5126a1645bcaac467c96f56461c1c04 Mon Sep 17 00:00:00 2001 From: Aaron Prindle Date: Tue, 19 Jul 2016 11:41:58 -0700 Subject: [PATCH] Have iso caching working --- cmd/minikube/cmd/root.go | 7 +++-- pkg/minikube/cluster/cluster.go | 47 +++++++++++++++++++++++++++++ pkg/minikube/constants/constants.go | 2 -- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index ba4b4892ae..95ca12496d 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -34,7 +34,10 @@ import ( var dirs = [...]string{ constants.Minipath, constants.MakeMiniPath("certs"), - constants.MakeMiniPath("machines")} + constants.MakeMiniPath("machines"), + constants.MakeMiniPath("iso"), + constants.MakeMiniPath("localkube"), + constants.MakeMiniPath("config")} var ( showLibmachineLogs bool @@ -77,7 +80,7 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { viper.SetConfigName("config") - viper.AddConfigPath(constants.ConfigFilePath) + viper.AddConfigPath(constants.MakeMiniPath("config")) viper.ReadInConfig() viper.SetDefault(config.WantUpdateNotification, true) viper.SetDefault(config.ReminderWaitPeriodInHours, 24) diff --git a/pkg/minikube/cluster/cluster.go b/pkg/minikube/cluster/cluster.go index caa8eb75b0..09e153fa12 100644 --- a/pkg/minikube/cluster/cluster.go +++ b/pkg/minikube/cluster/cluster.go @@ -20,8 +20,11 @@ import ( "encoding/json" "flag" "fmt" + "io" "io/ioutil" "net" + "net/http" + "os" "path/filepath" "strings" "time" @@ -300,9 +303,53 @@ func createVirtualboxHost(config MachineConfig) drivers.Driver { return d } +func isISONotCachedInMinikubeDir(isoFilePath string) bool { + if _, err := os.Stat(isoFilePath); os.IsNotExist(err) { + return true + } + return false +} + +func getISOFilePathFromConfig(config MachineConfig) string { + split := strings.Split(config.MinikubeISO, "/") + isoFilename := split[len(split)-1] + return filepath.Join(constants.Minipath, "iso", isoFilename) +} + +func modifyConfigForCachedIso(isoFilePath string, config *MachineConfig) error { + if isISONotCachedInMinikubeDir(isoFilePath) { + // download miniube-iso to .minikube dir + response, err := http.Get(config.MinikubeISO) + if err != nil { + return err + } else { + out, err := os.Create(isoFilePath) + if err != nil { + return err + } + defer out.Close() + defer response.Body.Close() + _, err = io.Copy(out, response.Body) + if err != nil { + return err + } + } + } + config.MinikubeISO = "file://" + isoFilePath + return nil +} + func createHost(api libmachine.API, config MachineConfig) (*host.Host, error) { var driver interface{} + //check cache for iso and change iso method appropriately + fmt.Println(config.MinikubeISO) + if config.MinikubeISO != "" { // "" is a test param + if err := modifyConfigForCachedIso(getISOFilePathFromConfig(config), &config); err != nil { + return nil, err + } + } + switch config.VMDriver { case "virtualbox": driver = createVirtualboxHost(config) diff --git a/pkg/minikube/constants/constants.go b/pkg/minikube/constants/constants.go index 59994fbad8..24f6dc9480 100644 --- a/pkg/minikube/constants/constants.go +++ b/pkg/minikube/constants/constants.go @@ -69,5 +69,3 @@ const ( RemoteLocalKubeErrPath = "/var/lib/localkube/localkube.err" RemoteLocalKubeOutPath = "/var/lib/localkube/localkube.out" ) - -var ConfigFilePath = MakeMiniPath("config")