mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-24 03:15:36 +00:00
Fix faulty daemon availability check
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
parent
473ef04bd9
commit
814a351e93
@ -3,23 +3,26 @@ package main
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
dockerSock = "/var/run/docker.sock"
|
||||
lgtm = "LGTM"
|
||||
httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change
|
||||
bucket = "editionsdiagnostics"
|
||||
sessionIDField = "session"
|
||||
healthcheckTimeout = 5 * time.Second
|
||||
dockerSock = "/var/run/docker.sock"
|
||||
lgtm = "LGTM"
|
||||
httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change
|
||||
bucket = "editionsdiagnostics"
|
||||
sessionIDField = "session"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -39,10 +42,32 @@ type HTTPDiagnosticListener struct{}
|
||||
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints
|
||||
func (h HTTPDiagnosticListener) Listen() {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := os.Stat(dockerSock); os.IsNotExist(err) {
|
||||
http.Error(w, "Docker socket not found -- daemon is down", http.StatusServiceUnavailable)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout)
|
||||
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
|
||||
}
|
||||
|
||||
if _, err := w.Write([]byte(lgtm)); err != nil {
|
||||
log.Println("Error writing HTTP success response:", err)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user