Merge pull request #2032 from hairyhenderson/make-active-work-with-swarm-1311

Returning active host when swarm is active
This commit is contained in:
Nathan LeClaire 2015-10-26 12:54:44 -07:00
commit 9fa2f73139
3 changed files with 82 additions and 35 deletions

View File

@ -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
}

45
commands/active_test.go Normal file
View File

@ -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)
}
}

View File

@ -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))