Use a single function for checking active machine

Fix https://github.com/docker/machine/issues/1651

As pointed out in the issue above, the `active` and `ls` commands used
different methods for determing the active machine. This commit defines
a single method on the `host` struct called `IsActive` which provides
a uniform check for machine activness. `IsActive` returns true only
if `DOCKER_HOST == url` and the state is not stopped - previously the
`active` command only checked the url.

* Add a single `host` method `IsActive` for determining if a machine is
  active.

Signed-off-by: Matt McNaughton <mattjmcnaughton@gmail.com>
This commit is contained in:
Matt McNaughton 2015-08-07 15:58:51 -04:00
parent 1c6960f5ca
commit 003770f2d4
2 changed files with 38 additions and 8 deletions

View File

@ -125,15 +125,10 @@ func (s Filestore) GetActive() (*Host, error) {
return nil, err
}
dockerHost := os.Getenv("DOCKER_HOST")
if dockerHost == "" {
return nil, errors.New("No active host found: DOCKER_HOST not set")
}
hostListItems := GetHostListItems(hosts)
for _, item := range hostListItems {
if dockerHost == item.URL {
if item.Active {
host, err := s.Get(item.Name)
if err != nil {
return nil, err

View File

@ -279,6 +279,37 @@ func (h *Host) GetURL() (string, error) {
return h.Driver.GetURL()
}
// IsActive provides a single method for determining if a host is active based
// on both the url and if the host is stopped.
func (h *Host) IsActive() (bool, error) {
currentState, err := h.Driver.GetState()
if err != nil {
log.Errorf("error getting state for host %s: %s", h.Name, err)
return false, err
}
url, err := h.GetURL()
if err != nil {
if err == drivers.ErrHostIsNotRunning {
url = ""
} else {
log.Errorf("error getting URL for host %s: %s", h.Name, err)
return false, err
}
}
dockerHost := os.Getenv("DOCKER_HOST")
notStopped := currentState != state.Stopped
correctURL := url == dockerHost
isActive := notStopped && correctURL
return isActive, nil
}
func (h *Host) LoadConfig() error {
data, err := ioutil.ReadFile(filepath.Join(h.StorePath, "config.json"))
if err != nil {
@ -374,11 +405,15 @@ func attemptGetHostState(host Host, stateQueryChan chan<- HostListItem) {
}
}
dockerHost := os.Getenv("DOCKER_HOST")
isActive, err := host.IsActive()
if err != nil {
log.Errorf("error determining if host is active for host %s: %s",
host.Name, err)
}
stateQueryChan <- HostListItem{
Name: host.Name,
Active: dockerHost == url && currentState != state.Stopped,
Active: isActive,
DriverName: host.Driver.DriverName(),
State: currentState,
URL: url,