component/dis: Check if instance alive

This commit is contained in:
Tom Wiesing 2022-09-15 18:11:19 +02:00
parent 37cdd201f0
commit 492a0c0404
No known key found for this signature in database
8 changed files with 145 additions and 22 deletions

View file

@ -1,26 +1,70 @@
package dis
import (
"encoding/json"
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/component/instances"
"github.com/tkw1536/goprogram/stream"
"golang.org/x/sync/errgroup"
)
func (dis Dis) info(io stream.IOStream) (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
all, err := dis.Instances.All()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal server error"))
io.EPrintln(err)
return
}
for _, wk := range all {
w.WriteHeader(http.StatusOK)
w.Write([]byte(wk.Slug))
w.Write([]byte("\n"))
}
}), nil
func (dis *Dis) info(io stream.IOStream) (http.Handler, error) {
return http.HandlerFunc(dis.handleDis), nil
}
const disLimit = 2
func (dis *Dis) handleDis(w http.ResponseWriter, r *http.Request) {
// make sure the user is authorized
if !dis.authDis(r) {
w.Header().Add("WWW-Authenticate", `Basic realm="WissKI Distillery Admin"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
// create a new error group
var errgroup errgroup.Group
errgroup.SetLimit(disLimit)
// list all the instances
all, err := dis.Instances.All()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal server error"))
return
}
// get all of their info!
infos := make([]instances.Info, len(all))
for i, instance := range all {
{
i := i
instance := instance
errgroup.Go(func() (err error) {
infos[i], err = instance.Info()
return err
})
}
}
// if some info call failed
if err := errgroup.Wait(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal server error"))
w.Write([]byte("\n"))
return
}
// and return the json
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(infos)
}
func (dis *Dis) authDis(r *http.Request) bool {
user, pass, ok := r.BasicAuth()
return ok && user == dis.Config.DisAdminUser && pass == dis.Config.DisAdminPassword
}

View file

@ -1,4 +1,4 @@
FROM docker.io/library/alpine
FROM docker.io/library/docker:20.10-cli
COPY wdcli /wdcli
EXPOSE 8888

View file

@ -1,7 +1,7 @@
version: "3.7"
services:
wdresolve:
dis:
build: .
restart: always
environment:
@ -16,6 +16,8 @@ services:
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL}
volumes:
# TODO: Mount docker socket properly!
- "/var/run/docker.sock:/var/run/docker.sock"
- "${CONFIG_PATH}:${CONFIG_PATH}:ro"
- "${DEPLOY_ROOT}:${DEPLOY_ROOT}:ro"
- "${GLOBAL_AUTHORIZED_KEYS_FILE}:${GLOBAL_AUTHORIZED_KEYS_FILE}:ro"