From ee4a3574411da874aab0c50af36546e98175ace4 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Tue, 20 Oct 2015 21:00:03 -0400 Subject: [PATCH] Returning active host when swarm is active Signed-off-by: Dave Henderson --- commands/active.go | 37 +++++++++++++++++++++++++++++++++ commands/active_test.go | 45 +++++++++++++++++++++++++++++++++++++++++ commands/ls.go | 35 -------------------------------- 3 files changed, 82 insertions(+), 35 deletions(-) create mode 100644 commands/active_test.go diff --git a/commands/active.go b/commands/active.go index 49d191181f..3e7ac50f38 100644 --- a/commands/active.go +++ b/commands/active.go @@ -1,9 +1,15 @@ package commands import ( + "errors" "fmt" + "os" + "strings" "github.com/docker/machine/cli" + "github.com/docker/machine/libmachine/host" + "github.com/docker/machine/libmachine/persist" + "github.com/docker/machine/libmachine/state" ) func cmdActive(c *cli.Context) { @@ -21,3 +27,34 @@ func cmdActive(c *cli.Context) { fmt.Println(host.Name) } } + +func getActiveHost(store persist.Store) (*host.Host, error) { + hosts, err := listHosts(store) + if err != nil { + return nil, err + } + + hostListItems := getHostListItems(hosts) + + for _, item := range hostListItems { + if item.Active { + return loadHost(store, item.Name) + } + } + + return nil, errors.New("Active host not found") +} + +// IsActive provides a single function for determining if a host is active +// based on both the url and if the host is stopped. +func isActive(h *host.Host, currentState state.State, url string) (bool, error) { + dockerHost := os.Getenv("DOCKER_HOST") + + // TODO: hard-coding the swarm port is a travesty... + deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1) + if dockerHost == url || deSwarmedHost == url { + return currentState == state.Running, nil + } + + return false, nil +} diff --git a/commands/active_test.go b/commands/active_test.go new file mode 100644 index 0000000000..cdf1bd0f0c --- /dev/null +++ b/commands/active_test.go @@ -0,0 +1,45 @@ +package commands + +import ( + "os" + "testing" + + "github.com/docker/machine/drivers/fakedriver" + "github.com/docker/machine/libmachine/host" + "github.com/docker/machine/libmachine/state" + "github.com/stretchr/testify/assert" +) + +var ( + h = host.Host{ + Name: "foo", + DriverName: "fakedriver", + Driver: &fakedriver.Driver{ + MockState: state.Running, + }, + } +) + +func TestIsActive(t *testing.T) { + cases := []struct { + dockerHost string + state state.State + expected bool + }{ + {"", state.Running, false}, + {"tcp://5.6.7.8:2376", state.Running, false}, + {"tcp://1.2.3.4:2376", state.Stopped, false}, + {"tcp://1.2.3.4:2376", state.Running, true}, + {"tcp://1.2.3.4:3376", state.Running, true}, + } + + for _, c := range cases { + os.Unsetenv("DOCKER_HOST") + if c.dockerHost != "" { + os.Setenv("DOCKER_HOST", c.dockerHost) + } + actual, err := isActive(&h, c.state, "tcp://1.2.3.4:2376") + assert.Equal(t, c.expected, actual, "IsActive(%s, \"%s\") should return %v, but didn't", c.state, c.dockerHost, c.expected) + assert.NoError(t, err) + } +} diff --git a/commands/ls.go b/commands/ls.go index 71fcbee788..db0b97e429 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -14,7 +14,6 @@ import ( "github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/log" - "github.com/docker/machine/libmachine/persist" "github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/swarm" "github.com/skarademir/naturalsort" @@ -228,27 +227,6 @@ func matchesName(host *host.Host, names []string) bool { return false } -func getActiveHost(store persist.Store) (*host.Host, error) { - hosts, err := listHosts(store) - if err != nil { - return nil, err - } - - hostListItems := getHostListItems(hosts) - - for _, item := range hostListItems { - if item.Active { - h, err := loadHost(store, item.Name) - if err != nil { - return nil, err - } - return h, nil - } - } - - return nil, errors.New("Active host not found") -} - func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) { stateCh := make(chan state.State) urlCh := make(chan string) @@ -336,19 +314,6 @@ func getHostListItems(hostList []*host.Host) []HostListItem { return hostListItems } -// IsActive provides a single function for determining if a host is active -// based on both the url and if the host is stopped. -func isActive(h *host.Host, currentState state.State, url string) (bool, error) { - dockerHost := os.Getenv("DOCKER_HOST") - - running := currentState == state.Running - correctURL := url == dockerHost - - isActive := running && correctURL - - return isActive, nil -} - func sortHostListItemsByName(items []HostListItem) { m := make(map[string]HostListItem, len(items)) s := make([]string, len(items))