Skip to content

Commit

Permalink
Show remote host info and proper progress
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Oct 29, 2020
1 parent 55e8e33 commit 4197935
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func validateUser(drvName string) {

// memoryLimits returns the amount of memory allocated to the system and hypervisor, the return value is in MiB
func memoryLimits(drvName string) (int, int, error) {
info, cpuErr, memErr, diskErr := machine.CachedHostInfo()
info, cpuErr, memErr, diskErr := machine.LocalHostInfo()
if cpuErr != nil {
klog.Warningf("could not get system cpu info while verifying memory limits, which might be okay: %v", cpuErr)
}
Expand Down
43 changes: 41 additions & 2 deletions pkg/minikube/machine/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package machine
import (
"io/ioutil"
"os/exec"
"strconv"
"strings"

"github.com/docker/machine/libmachine/provision"
"github.com/shirou/gopsutil/cpu"
Expand All @@ -39,8 +41,8 @@ type HostInfo struct {
DiskSize int64
}

// CachedHostInfo returns system information such as memory,CPU, DiskSize
func CachedHostInfo() (*HostInfo, error, error, error) {
// LocalHostInfo returns system information such as memory,CPU, DiskSize
func LocalHostInfo() (*HostInfo, error, error, error) {
var cpuErr, memErr, diskErr error
i, cpuErr := cachedCPUInfo()
if cpuErr != nil {
Expand All @@ -63,6 +65,43 @@ func CachedHostInfo() (*HostInfo, error, error, error) {
return &info, cpuErr, memErr, diskErr
}

// RemoteHostInfo returns system information such as memory,CPU, DiskSize
func RemoteHostInfo(r command.Runner) (*HostInfo, error, error, error) {
rr, cpuErr := r.RunCmd(exec.Command("nproc"))
if cpuErr != nil {
klog.Warningf("Unable to get CPU info: %v", cpuErr)
}
nproc := rr.Stdout.String()
ncpus, err := strconv.Atoi(strings.TrimSpace(nproc))
if err != nil {
klog.Warningf("Failed to parse CPU info: %v", err)
}
rr, memErr := r.RunCmd(exec.Command("free", "-m"))
if memErr != nil {
klog.Warningf("Unable to get mem info: %v", memErr)
}
free := rr.Stdout.String()
memory, err := util.ParseMemFree(free)
if err != nil {
klog.Warningf("Unable to parse mem info: %v", err)
}
rr, diskErr := r.RunCmd(exec.Command("df", "-m"))
if diskErr != nil {
klog.Warningf("Unable to get disk info: %v", diskErr)
}
df := rr.Stdout.String()
disksize, err := util.ParseDiskFree(df)
if err != nil {
klog.Warningf("Unable to parse disk info: %v", err)
}

var info HostInfo
info.CPUs = ncpus
info.Memory = memory
info.DiskSize = disksize
return &info, cpuErr, memErr, diskErr
}

// showLocalOsRelease shows systemd information about the current linux distribution, on the local host
func showLocalOsRelease() {
osReleaseOut, err := ioutil.ReadFile("/etc/os-release")
Expand Down
24 changes: 21 additions & 3 deletions pkg/minikube/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func createHost(api libmachine.API, cfg *config.ClusterConfig, n *config.Node) (
See https://minikube.sigs.k8s.io/docs/reference/drivers/vmware/ for more information.
To disable this message, run [minikube config set ShowDriverDeprecationNotification false]`)
}
showHostInfo(*cfg)
if cfg.Driver != driver.Generic {
showHostInfo(nil, *cfg)
}
def := registry.Driver(cfg.Driver)
if def.Empty() {
return nil, fmt.Errorf("unsupported/missing driver: %s", cfg.Driver)
Expand Down Expand Up @@ -170,6 +172,9 @@ func createHost(api libmachine.API, cfg *config.ClusterConfig, n *config.Node) (
return nil, errors.Wrap(err, "creating host")
}
klog.Infof("duration metric: libmachine.API.Create for %q took %s", cfg.Name, time.Since(cstart))
if cfg.Driver == driver.Generic {
showHostInfo(h, *cfg)
}

if err := postStartSetup(h, *cfg); err != nil {
return h, errors.Wrap(err, "post-start")
Expand Down Expand Up @@ -321,16 +326,29 @@ func acquireMachinesLock(name string, drv string) (mutex.Releaser, error) {
}

// showHostInfo shows host information
func showHostInfo(cfg config.ClusterConfig) {
func showHostInfo(h *host.Host, cfg config.ClusterConfig) {
machineType := driver.MachineType(cfg.Driver)
if driver.BareMetal(cfg.Driver) {
info, cpuErr, memErr, DiskErr := CachedHostInfo()
info, cpuErr, memErr, DiskErr := LocalHostInfo()
if cpuErr == nil && memErr == nil && DiskErr == nil {
register.Reg.SetStep(register.RunningLocalhost)
out.T(style.StartingNone, "Running on localhost (CPUs={{.number_of_cpus}}, Memory={{.memory_size}}MB, Disk={{.disk_size}}MB) ...", out.V{"number_of_cpus": info.CPUs, "memory_size": info.Memory, "disk_size": info.DiskSize})
}
return
}
if cfg.Driver == driver.Generic {
r, err := CommandRunner(h)
if err != nil {
klog.Warningf("error getting command runner: %v", err)
return
}
info, cpuErr, memErr, DiskErr := RemoteHostInfo(r)
if cpuErr == nil && memErr == nil && DiskErr == nil {
register.Reg.SetStep(register.RunningRemotely)
out.T(style.StartingGeneric, "Running remotely (CPUs={{.number_of_cpus}}, Memory={{.memory_size}}MB, Disk={{.disk_size}}MB) ...", out.V{"number_of_cpus": info.CPUs, "memory_size": info.Memory, "disk_size": info.DiskSize})
}
return
}
if driver.IsKIC(cfg.Driver) { // TODO:medyagh add free disk space on docker machine
register.Reg.SetStep(register.CreatingContainer)
out.T(style.StartingVM, "Creating {{.driver_name}} {{.machine_type}} (CPUs={{.number_of_cpus}}, Memory={{.memory_size}}MB) ...", out.V{"driver_name": cfg.Driver, "number_of_cpus": cfg.CPUs, "memory_size": cfg.Memory, "machine_type": machineType})
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/out/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
DownloadingArtifacts RegStep = "Downloading Artifacts"
StartingNode RegStep = "Starting Node"
RunningLocalhost RegStep = "Running on Localhost"
RunningRemotely RegStep = "Running Remotely"
LocalOSRelease RegStep = "Local OS Release"
CreatingContainer RegStep = "Creating Container"
CreatingVM RegStep = "Creating VM"
Expand Down Expand Up @@ -71,6 +72,7 @@ func init() {
LocalOSRelease,
CreatingContainer,
CreatingVM,
RunningRemotely,
PreparingKubernetes,
ConfiguringLHEnv,
VerifyingKubernetes,
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ var Config = map[Enum]Options{
Resetting: {Prefix: "🔄 "},
Shutdown: {Prefix: "🛑 "},
StartingNone: {Prefix: "🤹 "},
StartingGeneric: {Prefix: "🔗 "},
StartingVM: {Prefix: "🔥 "},
Tip: {Prefix: "💡 "},
Unmount: {Prefix: "🔥 "},
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/style/style_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const (
Shutdown
Sparkle
StartingNone
StartingGeneric
StartingVM
Stopped
Stopping
Expand Down
42 changes: 42 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/user"
"path/filepath"
"strconv"
"strings"

"github.com/blang/semver"
units "github.com/docker/go-units"
Expand Down Expand Up @@ -62,6 +63,47 @@ func ConvertUnsignedBytesToMB(byteSize uint64) int64 {
return int64(byteSize / units.MiB)
}

// ParseMemFree parses the output of the `free -m` command
func ParseMemFree(out string) (int64, error) {
// total used free shared buff/cache available
//Mem: 1987 706 194 1 1086 1173
//Swap: 0 0 0
outlines := strings.Split(out, "\n")
l := len(outlines)
for _, line := range outlines[1 : l-1] {
parsedLine := strings.Fields(line)
t, err := strconv.ParseInt(parsedLine[1], 10, 64)
if err != nil {
return 0, err
}
m := strings.Trim(parsedLine[0], ":")
if m == "Mem" {
return t, nil
}
}
return 0, nil
}

// ParseDiskFree parses the output of the `df -m` command
func ParseDiskFree(out string) (int64, error) {
// Filesystem 1M-blocks Used Available Use% Mounted on
// /dev/sda1 39643 3705 35922 10% /
outlines := strings.Split(out, "\n")
l := len(outlines)
for _, line := range outlines[1 : l-1] {
parsedLine := strings.Fields(line)
t, err := strconv.ParseInt(parsedLine[1], 10, 64)
if err != nil {
return 0, err
}
m := parsedLine[5]
if m == "/" {
return t, nil
}
}
return 0, nil
}

// GetBinaryDownloadURL returns a suitable URL for the platform
func GetBinaryDownloadURL(version, platform string) string {
switch platform {
Expand Down

0 comments on commit 4197935

Please sign in to comment.