From 13944f38114edee1d591ce8d883ccbd7e091b1be Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 12 Jun 2015 16:49:15 +0100 Subject: [PATCH] Refactor is-running check so we can re-use more of the code --- common/docker/client.go | 25 +++++++++++++++---------- nameserver/http.go | 15 +++------------ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/common/docker/client.go b/common/docker/client.go index 2c71fb42..bb96cf29 100644 --- a/common/docker/client.go +++ b/common/docker/client.go @@ -60,16 +60,21 @@ func (c *Client) AddObserver(ob ContainerObserver) error { return nil } -// IsContainerRunning returns true if the container is up and running -func (c *Client) IsContainerRunning(idStr string) (bool, error) { - // check with Docker whether the container is really running - container, err := c.InspectContainer(idStr) - if err != nil { - if _, notThere := err.(*docker.NoSuchContainer); notThere { - return false, nil - } - return false, err +// IsContainerNotRunning returns true if we have checked with Docker that the ID is not running +func (c *Client) IsContainerNotRunning(idStr string) bool { + if !IsContainerID(idStr) { + Debug.Printf("[docker] '%s' does not seem to be a container id", idStr) + return false } - return container.State.Running, nil + // check with Docker whether the container is really running + container, err := c.InspectContainer(idStr) + if err == nil { + return !container.State.Running + } + if _, notThere := err.(*docker.NoSuchContainer); notThere { + return true + } + Error.Printf("[docker] Could not check container status: %s", err) + return false } diff --git a/nameserver/http.go b/nameserver/http.go index 2361cd6a..a1501d98 100644 --- a/nameserver/http.go +++ b/nameserver/http.go @@ -75,18 +75,9 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, dockerC } // oh, I already know this. whatever. } - if dockerCli != nil { - if !docker.IsContainerID(idStr) { - Debug.Printf("[http] '%s' does not seem to be a container id", idStr) - return - } - - if running, err := dockerCli.IsContainerRunning(idStr); err != nil { - Error.Printf("[http] Could not check container status: %s", err) - } else if !running { - Info.Printf("[http] '%s' is not running: removing", idStr) - server.Zone.DeleteRecords(idStr, name, ip) - } + if dockerCli != nil && dockerCli.IsContainerNotRunning(idStr) { + Info.Printf("[http] '%s' is not running: removing", idStr) + server.Zone.DeleteRecords(idStr, name, ip) } })