Skip to content

Commit

Permalink
Download kvm2 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
josedonizetti committed Aug 15, 2019
1 parent 74a060a commit 90a12d5
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 77 deletions.
12 changes: 12 additions & 0 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"

"github.com/docker/machine/libmachine"
Expand Down Expand Up @@ -117,6 +118,12 @@ func Execute() {
flag.Usage = translate.T(flag.Usage)
})

if runtime.GOOS == "linux" {
// add minikube binaries to the path
targetDir := constants.MakeMiniPath("bin")
addToPath(targetDir)
}

if err := RootCmd.Execute(); err != nil {
// Cobra already outputs the error, typically because the user provided an unknown command.
os.Exit(exit.BadUsage)
Expand Down Expand Up @@ -290,3 +297,8 @@ func getClusterBootstrapper(api libmachine.API, bootstrapperName string) (bootst

return b, nil
}

func addToPath(dir string) {
path := os.Getenv("PATH")
os.Setenv("PATH", fmt.Sprintf("%s:%s", dir, path))
}
46 changes: 17 additions & 29 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"os/exec"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
Expand All @@ -43,6 +42,7 @@ import (
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/minikube/bootstrapper"
"k8s.io/minikube/pkg/minikube/bootstrapper/kubeadm"
"k8s.io/minikube/pkg/minikube/cluster"
Expand Down Expand Up @@ -259,7 +259,8 @@ func runStart(cmd *cobra.Command, args []string) {
registryMirror = viper.GetStringSlice("registry_mirror")
}

if err := cmdcfg.IsValidDriver(runtime.GOOS, viper.GetString(vmDriver)); err != nil {
driver := viper.GetString(vmDriver)
if err := cmdcfg.IsValidDriver(runtime.GOOS, driver); err != nil {
exit.WithCodeT(
exit.Failure,
"The driver '{{.driver}}' is not supported on {{.os}}",
Expand All @@ -269,7 +270,7 @@ func runStart(cmd *cobra.Command, args []string) {

validateConfig()
validateUser()
validateDriverVersion(viper.GetString(vmDriver))
validateDriverVersion(driver)

k8sVersion, isUpgrade := getKubernetesVersion()
config, err := generateCfgFromFlags(cmd, k8sVersion)
Expand Down Expand Up @@ -928,10 +929,20 @@ func validateDriverVersion(vmDriver string) {
driverDocumentation string
)

minikubeVersion, err := version.GetSemverVersion()
if err != nil {
out.WarningT("Error parsing minukube version: {{.error}}", out.V{"error": err})
return
}

switch vmDriver {
case constants.DriverKvm2:
driverExecutable = fmt.Sprintf("docker-machine-driver-%s", constants.DriverKvm2)
driverDocumentation = fmt.Sprintf("%s#%s", constants.DriverDocumentation, "kvm2-upgrade")
targetDir := constants.MakeMiniPath("bin")
err := drivers.Download("docker-machine-driver-kvm2", targetDir, minikubeVersion)
if err != nil {
out.WarningT("Error downloading driver: {{.error}}", out.V{"error": err})
}
return
case constants.DriverHyperkit:
driverExecutable = fmt.Sprintf("docker-machine-driver-%s", constants.DriverHyperkit)
driverDocumentation = fmt.Sprintf("%s#%s", constants.DriverDocumentation, "hyperkit-upgrade")
Expand All @@ -949,7 +960,7 @@ func validateDriverVersion(vmDriver string) {
return
}

v := extractVMDriverVersion(string(output))
v := drivers.ExtractVMDriverVersion(string(output))

// if the driver doesn't have return any version, it is really old, we force a upgrade.
if len(v) == 0 {
Expand All @@ -966,33 +977,10 @@ func validateDriverVersion(vmDriver string) {
return
}

minikubeVersion, err := version.GetSemverVersion()
if err != nil {
out.WarningT("Error parsing minikube version: {{.error}}", out.V{"error": err})
return
}

if vmDriverVersion.LT(minikubeVersion) {
out.WarningT(
"There's a new version for '{{.driver_executable}}'. Please consider upgrading. {{.documentation_url}}",
out.V{"driver_executable": driverExecutable, "documentation_url": driverDocumentation},
)
}
}

// extractVMDriverVersion extracts the driver version.
// KVM and Hyperkit drivers support the 'version' command, that display the information as:
// version: vX.X.X
// commit: XXXX
// This method returns the version 'vX.X.X' or empty if the version isn't found.
func extractVMDriverVersion(s string) string {
versionRegex := regexp.MustCompile(`version:(.*)`)
matches := versionRegex.FindStringSubmatch(s)

if len(matches) != 2 {
return ""
}

v := strings.TrimSpace(matches[1])
return strings.TrimPrefix(v, version.VersionPrefix)
}
45 changes: 0 additions & 45 deletions cmd/minikube/cmd/start_test.go

This file was deleted.

99 changes: 99 additions & 0 deletions pkg/drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"syscall"

"github.com/blang/semver"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/golang/glog"
"github.com/hashicorp/go-getter"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/version"

"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/util"
)

const (
driverKVMDownloadURL = "https://storage.googleapis.com/minikube/releases/latest/docker-machine-driver-kvm2"
)

// GetDiskPath returns the path of the machine disk image
Expand Down Expand Up @@ -136,3 +150,88 @@ func fixPermissions(path string) error {
}
return nil
}

// Download downloads driver if it is not present, or if the version is old
func Download(driver, destination string, minikubeVersion semver.Version) error {
_, err := exec.LookPath(driver)
// if file driver doesn't exist, download it
if err != nil {
return download(driver, destination)
}

cmd := exec.Command(driver, "version")
output, err := cmd.Output()
// if driver doesnt support 'version', it is old, download it
if err != nil {
return download(driver, destination)
}

v := ExtractVMDriverVersion(string(output))

// if the driver doesn't return any version, download it
if len(v) == 0 {
return download(driver, destination)
}

vmDriverVersion, err := semver.Make(v)
if err != nil {
return errors.Wrap(err, "can't parse driver version")
}

// if the current driver version is older, download newer
if vmDriverVersion.LT(minikubeVersion) {
return download(driver, destination)
}

return nil
}

func download(driver, destination string) error {
// only support kvm2 for now
if driver != "docker-machine-driver-kvm2" {
return nil
}

out.T(out.Happy, "Downloading driver {{.driver}}:", out.V{"driver": driver})

targetFilepath := path.Join(destination, "docker-machine-driver-kvm2")
os.Remove(targetFilepath)

url := driverKVMDownloadURL

opts := []getter.ClientOption{getter.WithProgress(util.DefaultProgressBar)}
client := &getter.Client{
Src: url,
Dst: targetFilepath,
Mode: getter.ClientModeFile,
Options: opts,
}

if err := client.Get(); err != nil {
return errors.Wrapf(err, "can't download driver %s from: %s", driver, url)
}

err := os.Chmod(targetFilepath, 0777)
if err != nil {
return errors.Wrap(err, "chmod error")
}

return nil
}

// ExtractVMDriverVersion extracts the driver version.
// KVM and Hyperkit drivers support the 'version' command, that display the information as:
// version: vX.X.X
// commit: XXXX
// This method returns the version 'vX.X.X' or empty if the version isn't found.
func ExtractVMDriverVersion(s string) string {
versionRegex := regexp.MustCompile(`version:(.*)`)
matches := versionRegex.FindStringSubmatch(s)

if len(matches) != 2 {
return ""
}

v := strings.TrimSpace(matches[1])
return strings.TrimPrefix(v, version.VersionPrefix)
}
24 changes: 24 additions & 0 deletions pkg/drivers/drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,27 @@ func Test_createDiskImage(t *testing.T) {
t.Errorf("Disk size is %v, want %v", fi.Size(), sizeInBytes)
}
}

func TestExtractVMDriverVersion(t *testing.T) {
v := ExtractVMDriverVersion("")
if len(v) != 0 {
t.Error("Expected empty string")
}

v = ExtractVMDriverVersion("random text")
if len(v) != 0 {
t.Error("Expected empty string")
}

expectedVersion := "1.2.3"

v = ExtractVMDriverVersion("version: v1.2.3")
if expectedVersion != v {
t.Errorf("Expected version: %s, got: %s", expectedVersion, v)
}

v = ExtractVMDriverVersion("version: 1.2.3")
if expectedVersion != v {
t.Errorf("Expected version: %s, got: %s", expectedVersion, v)
}
}
2 changes: 1 addition & 1 deletion pkg/util/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (f DefaultDownloader) CacheMinikubeISOFromURL(url string) error {
// Predictable temp destination so that resume can function
tmpDst := dst + ".download"

opts := []getter.ClientOption{getter.WithProgress(defaultProgressBar)}
opts := []getter.ClientOption{getter.WithProgress(DefaultProgressBar)}
client := &getter.Client{
Src: urlWithChecksum,
Dst: tmpDst,
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ limitations under the License.
*/

// This file implements a go-getter wrapper for cheggaaa progress bar

// based on:
// https://github.com/hashicorp/go-getter/blob/master/cmd/go-getter/progress_tracking.go

package util

import (
Expand All @@ -29,7 +29,7 @@ import (
"github.com/hashicorp/go-getter"
)

var defaultProgressBar getter.ProgressTracker = &progressBar{}
var DefaultProgressBar getter.ProgressTracker = &progressBar{}

type progressBar struct {
lock sync.Mutex
Expand Down

0 comments on commit 90a12d5

Please sign in to comment.