diff --git a/cli/commands.go b/cli/commands.go index d01d64d9..be6b6320 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -10,6 +10,7 @@ import ( "os" "strconv" "strings" + "time" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -270,7 +271,9 @@ func CreateCluster(c *cli.Context) error { * Registry (optional) * Create the (optional) registry container */ + var registryNameExists *dnsNameCheck if clusterSpec.RegistryEnabled { + registryNameExists = newAsyncNameExists(clusterSpec.RegistryName, 1*time.Second) if _, err = createRegistry(*clusterSpec); err != nil { deleteCluster() return err @@ -325,7 +328,11 @@ func CreateCluster(c *cli.Context) error { if clusterSpec.RegistryEnabled { log.Printf("A local registry has been started as %s:%d", clusterSpec.RegistryName, clusterSpec.RegistryPort) - log.Printf("Make sure you have an alias in your /etc/hosts file like '127.0.0.1 %s'", clusterSpec.RegistryName) + + exists, err := registryNameExists.Exists() + if !exists || err != nil { + log.Printf("Make sure you have an alias in your /etc/hosts file like '127.0.0.1 %s'", clusterSpec.RegistryName) + } } log.Printf(`You can now use the cluster with: diff --git a/cli/util.go b/cli/util.go index a9fae4bb..62db0651 100644 --- a/cli/util.go +++ b/cli/util.go @@ -126,3 +126,36 @@ func fileExists(filename string) bool { _, err := os.Stat(filename) return !os.IsNotExist(err) } + +type dnsNameCheck struct { + res chan bool + err chan error + timeout time.Duration +} + +func newAsyncNameExists(name string, timeout time.Duration) *dnsNameCheck { + d := &dnsNameCheck{ + res: make(chan bool), + err: make(chan error), + timeout: timeout, + } + go func() { + addresses, err := net.LookupHost(name) + if err != nil { + d.err <- err + } + d.res <- len(addresses) > 0 + }() + return d +} + +func (d dnsNameCheck) Exists() (bool, error) { + select { + case r := <-d.res: + return r, nil + case e := <-d.err: + return false, e + case <-time.After(d.timeout): + return false, nil + } +}