Merge pull request #1656 from mattjmcnaughton/1651-single-check-for-active-machine

Use a single function for checking active machine
This commit is contained in:
Evan Hazlett 2015-08-10 10:01:09 -04:00
commit 0c96ea0de2
2 changed files with 38 additions and 8 deletions

View File

@ -125,15 +125,10 @@ func (s Filestore) GetActive() (*Host, error) {
return nil, err 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) hostListItems := GetHostListItems(hosts)
for _, item := range hostListItems { for _, item := range hostListItems {
if dockerHost == item.URL { if item.Active {
host, err := s.Get(item.Name) host, err := s.Get(item.Name)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -279,6 +279,37 @@ func (h *Host) GetURL() (string, error) {
return h.Driver.GetURL() 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 { func (h *Host) LoadConfig() error {
data, err := ioutil.ReadFile(filepath.Join(h.StorePath, "config.json")) data, err := ioutil.ReadFile(filepath.Join(h.StorePath, "config.json"))
if err != nil { 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{ stateQueryChan <- HostListItem{
Name: host.Name, Name: host.Name,
Active: dockerHost == url && currentState != state.Stopped, Active: isActive,
DriverName: host.Driver.DriverName(), DriverName: host.Driver.DriverName(),
State: currentState, State: currentState,
URL: url, URL: url,