From 809e9f5284499a2a902991b144fcc3a3b41854c8 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Mon, 26 Oct 2015 15:00:34 -0700 Subject: [PATCH] Extracted the logic of figuring out the shell and fixing the logic to find out if the check is a Docker check --- command/agent/check.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/command/agent/check.go b/command/agent/check.go index 45dc3bfc69..5ae218bf27 100644 --- a/command/agent/check.go +++ b/command/agent/check.go @@ -82,7 +82,7 @@ func (c *CheckType) IsTCP() bool { } func (c *CheckType) IsDocker() bool { - return c.DockerContainerId != "" && c.Shell != "" && c.Interval != 0 + return c.DockerContainerId != "" && c.Interval != 0 } // CheckNotifier interface is used by the CheckMonitor @@ -547,11 +547,7 @@ func (c *CheckDocker) Start() { //figure out the shell if c.Shell == "" { - if otherShell := os.Getenv("SHELL"); otherShell != "" { - c.Shell = otherShell - } else { - c.Shell = "/bin/sh" - } + c.Shell = shell() } c.cmd = []string{c.Shell, "-c", c.Script} @@ -602,9 +598,7 @@ func (c *CheckDocker) check() { exec *docker.Exec err error ) - if exec, err = c.dockerClient.CreateExec(execOpts); err == nil { - exec = exec - } else { + if exec, err = c.dockerClient.CreateExec(execOpts); err != nil { c.Logger.Printf("[DEBUG] agent: Error while creating Exec: %s", err.Error()) c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, fmt.Sprintf("Unable to create Exec, error: %s", err.Error())) return @@ -613,7 +607,7 @@ func (c *CheckDocker) check() { err = c.dockerClient.StartExec(exec.ID, docker.StartExecOptions{Detach: false, Tty: false}) if err != nil { c.Logger.Printf("[DEBUG] Error in executing health checks: %s", err.Error()) - c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, fmt.Sprintf("Unable to start exec: %s", err.Error())) + c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, fmt.Sprintf("Unable to start Exec: %s", err.Error())) return } @@ -632,3 +626,11 @@ func (c *CheckDocker) check() { } } + +func shell() string { + if otherShell := os.Getenv("SHELL"); otherShell != "" { + return otherShell + } else { + return "/bin/sh" + } +}