Fix faulty daemon availability check

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire 2017-01-06 15:44:35 -08:00
parent 473ef04bd9
commit 814a351e93

View File

@ -3,23 +3,26 @@ package main
import ( import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
const ( const (
dockerSock = "/var/run/docker.sock" healthcheckTimeout = 5 * time.Second
lgtm = "LGTM" dockerSock = "/var/run/docker.sock"
httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change lgtm = "LGTM"
bucket = "editionsdiagnostics" httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change
sessionIDField = "session" bucket = "editionsdiagnostics"
sessionIDField = "session"
) )
var ( var (
@ -39,10 +42,32 @@ type HTTPDiagnosticListener struct{}
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints // Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints
func (h HTTPDiagnosticListener) Listen() { func (h HTTPDiagnosticListener) Listen() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(dockerSock); os.IsNotExist(err) { ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout)
http.Error(w, "Docker socket not found -- daemon is down", http.StatusServiceUnavailable) defer cancel()
// Vendoring the Docker Go client is overkill for this
// small program, and we can fairly safely rely on the
// `docker` client's existence locally, so we just
// shell out here.
cmd := exec.CommandContext(ctx, "docker", "info")
errCh := make(chan error)
go func() {
_, err := cmd.CombinedOutput()
errCh <- err
}()
select {
case err := <-errCh:
if err != nil {
http.Error(w, "Docker daemon ping error", http.StatusInternalServerError)
return
}
case <-ctx.Done():
http.Error(w, "Docker daemon ping timed out", http.StatusServiceUnavailable)
return return
} }
if _, err := w.Write([]byte(lgtm)); err != nil { if _, err := w.Write([]byte(lgtm)); err != nil {
log.Println("Error writing HTTP success response:", err) log.Println("Error writing HTTP success response:", err)
return return