From 07f2972dda44d43eacd053501f3f2bba62d28eb9 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Wed, 3 Jun 2015 10:43:36 -0400 Subject: [PATCH] Warning user that the active machine was restarted and may have a new IP Signed-off-by: Dave Henderson --- commands/restart.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/commands/restart.go b/commands/restart.go index 9b1cb4cd38..efc6f73cfc 100644 --- a/commands/restart.go +++ b/commands/restart.go @@ -1,6 +1,10 @@ package commands import ( + "os" + "strings" + + "github.com/docker/machine/libmachine" "github.com/docker/machine/log" "github.com/codegangsta/cli" @@ -10,4 +14,58 @@ func cmdRestart(c *cli.Context) { if err := runActionWithContext("restart", c); err != nil { log.Fatal(err) } + + warnIfActiveIPChanged(c) +} + +func warnIfActiveIPChanged(c *cli.Context) { + if activeInList(c.Args()) { + activeURL := os.Getenv("DOCKER_HOST") + log.Debugf("checking to see if active host's URL has changed from %s", activeURL) + + h := getActiveHost(c) + u, err := h.GetURL() + if err != nil { + log.Fatal(err) + } + if activeURL != u { + // TODO: hardcoded port here is a giant kludge... + var swarmOpt string + if strings.Contains(activeURL, ":3376") { + swarmOpt = "--swarm " + } + log.Warnf("Active machine was restarted, and has a new IP address.\nRun 'eval \"$(docker-machine env %s%s)\"' again.", swarmOpt, h.Name) + } + } +} + +func activeInList(machines []string) bool { + active := os.Getenv("DOCKER_MACHINE_NAME") + return active != "" && inArray(active, machines) +} + +func getActiveHost(c *cli.Context) *libmachine.Host { + certInfo := getCertPathInfo(c) + store, err := getDefaultStore( + c.GlobalString("storage-path"), + certInfo.CaCertPath, + certInfo.CaKeyPath, + ) + if err != nil { + log.Fatal(err) + } + active, err := store.GetActive() + if err != nil { + log.Fatal(err) + } + return active +} + +func inArray(s string, a []string) bool { + for _, cur := range a { + if cur == s { + return true + } + } + return false }