From 1410c25448f8ccee0009f79ad913c716510358e9 Mon Sep 17 00:00:00 2001 From: Frank Febbraro Date: Fri, 17 Nov 2017 19:40:21 -0800 Subject: [PATCH 1/2] Moved to all pointer functions, as non-pointers make a copy of the struct and the data in a pointer didn't come with it. --- commands/machine.go | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/commands/machine.go b/commands/machine.go index 22f55fc..9eb9eb5 100644 --- a/commands/machine.go +++ b/commands/machine.go @@ -77,7 +77,7 @@ func (m *Machine) Create(driver string, cpuCount string, memSize string, diskSiz } // CheckXhyveRequirements verifies that the correct xhyve environment exists -func (m Machine) CheckXhyveRequirements() error { +func (m *Machine) CheckXhyveRequirements() error { // Is xhyve installed locally if err := util.Command("which", "xhyve").Run(); err != nil { return fmt.Errorf("xhyve is not installed. Install it with 'brew install xhyve'") @@ -92,7 +92,7 @@ func (m Machine) CheckXhyveRequirements() error { } // Start boots the Docker Machine -func (m Machine) Start() error { +func (m *Machine) Start() error { if !m.IsRunning() { m.out.Channel.Verbose.Printf("The machine '%s' is not running, starting...", m.Name) @@ -107,7 +107,7 @@ func (m Machine) Start() error { } // Stop halts the Docker Machine -func (m Machine) Stop() error { +func (m *Machine) Stop() error { if m.IsRunning() { return util.StreamCommand("docker-machine", "stop", m.Name) } @@ -115,12 +115,12 @@ func (m Machine) Stop() error { } // Remove deleted the Docker Machine -func (m Machine) Remove() error { +func (m *Machine) Remove() error { return util.StreamCommand("docker-machine", "rm", "-y", m.Name) } // WaitForDev will wait a period of time for communication with the docker daemon to be established -func (m Machine) WaitForDev() error { +func (m *Machine) WaitForDev() error { maxTries := 10 sleepSecs := 3 @@ -138,7 +138,7 @@ func (m Machine) WaitForDev() error { } // SetEnv will set the Docker proxy variables that determine which machine the docker command communicates -func (m Machine) SetEnv() { +func (m *Machine) SetEnv() { if js := m.GetData(); js != nil { tlsVerify := 0 if js.Get("HostOptions").Get("EngineOptions").Get("TlsVerify").MustBool() { @@ -152,7 +152,7 @@ func (m Machine) SetEnv() { } // UnsetEnv will remove the Docker proxy variables -func (m Machine) UnsetEnv() { +func (m *Machine) UnsetEnv() { os.Unsetenv("DOCKER_TLS_VERIFY") os.Unsetenv("DOCKER_HOST") os.Unsetenv("DOCKER_CERT_PATH") @@ -160,7 +160,7 @@ func (m Machine) UnsetEnv() { } // Exists determines if the Docker Machine exist -func (m Machine) Exists() bool { +func (m *Machine) Exists() bool { if err := util.Command("docker-machine", "status", m.Name).Run(); err != nil { return false } @@ -168,7 +168,7 @@ func (m Machine) Exists() bool { } // IsRunning returns the Docker Machine running status -func (m Machine) IsRunning() bool { +func (m *Machine) IsRunning() bool { if err := util.Command("docker-machine", "env", m.Name).Run(); err != nil { return false } @@ -178,14 +178,17 @@ func (m Machine) IsRunning() bool { // GetData will inspect the Docker Machine and return the parsed JSON describing the machine func (m *Machine) GetData() *simplejson.Json { if m.inspectData != nil { + m.out.Warn("m.inspectData is NOT nil") return m.inspectData } + m.out.Warn("m.inspectData was nil, retrieving") if inspect, inspectErr := util.Command("docker-machine", "inspect", m.Name).Output(); inspectErr == nil { if js, jsonErr := simplejson.NewJson(inspect); jsonErr != nil { m.out.Channel.Error.Fatalf("Failed to parse '%s' JSON: %s", m.Name, jsonErr) } else { m.inspectData = js + m.out.Warn("m.inspectData was set to: %s", m.inspectData) return m.inspectData } } @@ -193,18 +196,18 @@ func (m *Machine) GetData() *simplejson.Json { } // GetIP returns the IP address for the Docker Machine -func (m Machine) GetIP() string { +func (m *Machine) GetIP() string { return m.GetData().Get("Driver").Get("IPAddress").MustString() } // GetHostDNSResolver checks if the VirtualBox host DNS resolver is working. This should work okay // for VMware or other machines without the option, too. -func (m Machine) GetHostDNSResolver() bool { +func (m *Machine) GetHostDNSResolver() bool { return m.GetData().Get("Driver").Get("HostDNSResolver").MustBool(false) } // GetBridgeIP returns the Bridge IP by looking for a bip= option -func (m Machine) GetBridgeIP() string { +func (m *Machine) GetBridgeIP() string { ip := "172.17.0.1" r := regexp.MustCompile("bip=([0-9.]+)/[0-9+]") var matches []string @@ -222,7 +225,7 @@ func (m Machine) GetBridgeIP() string { } // GetDockerVersion returns the Version of Docker running within Docker Machine -func (m Machine) GetDockerVersion() (*version.Version, error) { +func (m *Machine) GetDockerVersion() (*version.Version, error) { b2dOutput, err := util.Command("docker-machine", "version", m.Name).CombinedOutput() if err != nil { return nil, errors.New(strings.TrimSpace(string(b2dOutput))) @@ -232,37 +235,37 @@ func (m Machine) GetDockerVersion() (*version.Version, error) { } // GetDriver returns the virtualization driver name -func (m Machine) GetDriver() string { +func (m *Machine) GetDriver() string { return m.GetData().Get("DriverName").MustString() } // IsXhyve returns if the virt driver is xhyve -func (m Machine) IsXhyve() bool { +func (m *Machine) IsXhyve() bool { return m.GetDriver() == util.Xhyve } // GetCPU returns the number of configured CPU for this Docker Machine -func (m Machine) GetCPU() int { +func (m *Machine) GetCPU() int { return m.GetData().Get("Driver").Get("CPU").MustInt() } // GetMemory returns the amount of configured memory for this Docker Machine -func (m Machine) GetMemory() int { +func (m *Machine) GetMemory() int { return m.GetData().Get("Driver").Get("Memory").MustInt() } // GetDisk returns the disk size in MB -func (m Machine) GetDisk() int { +func (m *Machine) GetDisk() int { return m.GetData().Get("Driver").Get("DiskSize").MustInt() } // GetDiskInGB returns the disk size in GB -func (m Machine) GetDiskInGB() int { +func (m *Machine) GetDiskInGB() int { return m.GetDisk() / 1000 } // GetSysctl returns the configured value for the provided sysctl setting on the Docker Machine -func (m Machine) GetSysctl(setting string) (string, error) { +func (m *Machine) GetSysctl(setting string) (string, error) { output, err := util.Command("docker-machine", "ssh", m.Name, "sudo", "sysctl", "-n", setting).CombinedOutput() if err != nil { return "", err @@ -271,7 +274,7 @@ func (m Machine) GetSysctl(setting string) (string, error) { } // SetSysctl sets the sysctl setting on the Docker Machine -func (m Machine) SetSysctl(key string, val string) error { +func (m *Machine) SetSysctl(key string, val string) error { cmd := fmt.Sprintf("sudo sysctl -w %s=%s", key, val) m.out.Verbose("Modifying Docker Machine kernel settings: %s", cmd) _, err := util.Command("docker-machine", "ssh", m.Name, cmd).CombinedOutput() From 3b0083732cc7e613e8dffadcaeb94091d057369a Mon Sep 17 00:00:00 2001 From: Frank Febbraro Date: Fri, 17 Nov 2017 19:44:35 -0800 Subject: [PATCH 2/2] removing debug messages --- commands/machine.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/commands/machine.go b/commands/machine.go index 9eb9eb5..a4cbd8c 100644 --- a/commands/machine.go +++ b/commands/machine.go @@ -178,17 +178,14 @@ func (m *Machine) IsRunning() bool { // GetData will inspect the Docker Machine and return the parsed JSON describing the machine func (m *Machine) GetData() *simplejson.Json { if m.inspectData != nil { - m.out.Warn("m.inspectData is NOT nil") return m.inspectData } - m.out.Warn("m.inspectData was nil, retrieving") if inspect, inspectErr := util.Command("docker-machine", "inspect", m.Name).Output(); inspectErr == nil { if js, jsonErr := simplejson.NewJson(inspect); jsonErr != nil { m.out.Channel.Error.Fatalf("Failed to parse '%s' JSON: %s", m.Name, jsonErr) } else { m.inspectData = js - m.out.Warn("m.inspectData was set to: %s", m.inspectData) return m.inspectData } }