Move daemon ping to use native Go code

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire 2017-01-09 12:21:33 -08:00
parent 814a351e93
commit da59746ec6

View File

@ -4,22 +4,28 @@ import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"net/http/httputil"
"os" "os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
var (
errDockerPingNotOK = errors.New("Docker /_ping did not return OK")
)
const ( const (
healthcheckTimeout = 5 * time.Second healthcheckTimeout = 5 * time.Second
dockerSock = "/var/run/docker.sock" dockerSock = "/var/run/docker.sock"
lgtm = "LGTM" dockerPingOK = "LGTM"
httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change
bucket = "editionsdiagnostics" bucket = "editionsdiagnostics"
sessionIDField = "session" sessionIDField = "session"
@ -39,22 +45,53 @@ func init() {
// for cloud editions. // for cloud editions.
type HTTPDiagnosticListener struct{} type HTTPDiagnosticListener struct{}
// UnixSocketRoundTripper provides a way to make HTTP request to Docker socket
// directly.
type UnixSocketRoundTripper struct{}
// RoundTrip dials the Docker UNIX socket to make a HTTP request.
func (u UnixSocketRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
dial, err := net.Dial("unix", dockerSock)
if err != nil {
return nil, err
}
conn := httputil.NewClientConn(dial, nil)
defer conn.Close()
return conn.Do(req)
}
// 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) {
client := &http.Client{
Transport: &UnixSocketRoundTripper{},
}
req, err := http.NewRequest(http.MethodGet, "/_ping", nil)
if err != nil {
http.Error(w, "Error creating HTTP request to talk to Docker", http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout) ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout)
defer cancel() defer cancel()
// Vendoring the Docker Go client is overkill for this req = req.WithContext(ctx)
// 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) errCh := make(chan error)
go func() { go func() {
_, err := cmd.CombinedOutput() resp, err := client.Do(req)
errCh <- err if err != nil {
errCh <- err
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errCh <- errDockerPingNotOK
return
}
errCh <- nil
}() }()
select { select {
@ -68,7 +105,7 @@ func (h HTTPDiagnosticListener) Listen() {
return return
} }
if _, err := w.Write([]byte(lgtm)); err != nil { if _, err := w.Write([]byte(dockerPingOK)); err != nil {
log.Println("Error writing HTTP success response:", err) log.Println("Error writing HTTP success response:", err)
return return
} }