-
Notifications
You must be signed in to change notification settings - Fork 26
/
active.go
67 lines (52 loc) · 1.4 KB
/
active.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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"
)
var (
errTooManyArguments = errors.New("Error: Too many arguments given")
)
func cmdActive(c *cli.Context) error {
if len(c.Args()) > 0 {
return errTooManyArguments
}
store := getStore(c)
host, err := getActiveHost(store)
if err != nil {
return fmt.Errorf("Error getting active host: %s", err)
}
if host != nil {
fmt.Println(host.Name)
}
return nil
}
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
}