eclipse/cmd/eclipse-httpd/main.go

512 lines
16 KiB
Go
Raw Normal View History

2024-01-17 21:52:40 +00:00
// Copyright (C) 2023-2024 Umorpha Systems
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import (
"context"
2024-01-18 02:47:08 +00:00
"embed"
2024-01-18 16:17:10 +00:00
"encoding/json"
2024-01-18 19:08:10 +00:00
"errors"
2024-01-17 21:52:40 +00:00
"fmt"
2024-01-18 01:08:22 +00:00
"html/template"
2024-01-18 16:17:10 +00:00
"io"
2024-01-18 19:55:53 +00:00
"io/fs"
2024-01-17 21:52:40 +00:00
"log"
2024-01-18 16:17:10 +00:00
"mime"
"mime/multipart"
2024-01-17 21:52:40 +00:00
"net/http"
2024-01-18 04:15:20 +00:00
"net/url"
2024-01-17 21:52:40 +00:00
"os"
2024-01-18 19:08:10 +00:00
"path"
2024-01-17 21:52:40 +00:00
"path/filepath"
2024-01-18 04:15:20 +00:00
"strconv"
2024-01-17 21:52:40 +00:00
"strings"
2024-01-18 19:55:53 +00:00
"time"
2024-01-17 21:52:40 +00:00
2024-01-18 19:08:10 +00:00
"git.lukeshu.com/go/containers/typedsync"
2024-01-17 21:52:40 +00:00
"github.com/datawire/ocibuild/pkg/cliutil"
"github.com/golang/gddo/httputil"
2024-01-17 21:52:40 +00:00
"github.com/spf13/cobra"
source "git.mothstuff.lol/lukeshu/eclipse"
"git.mothstuff.lol/lukeshu/eclipse/lib/common"
"git.mothstuff.lol/lukeshu/eclipse/lib/leader"
2024-01-17 21:52:40 +00:00
)
func main() {
argparser := &cobra.Command{
Use: os.Args[0] + " [flags]",
2024-01-20 21:08:44 +00:00
Short: "The leader HTTP server",
2024-01-18 07:43:02 +00:00
Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
2024-01-17 21:52:40 +00:00
SilenceErrors: true, // we'll handle this ourselves after .ExecuteContext()
SilenceUsage: true, // FlagErrorFunc will handle this
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}
argparser.SetFlagErrorFunc(cliutil.FlagErrorFunc)
argparser.SetHelpTemplate(cliutil.HelpTemplate)
var cfgFile string
2024-01-19 21:02:27 +00:00
argparser.Flags().StringVar(&cfgFile, "config", source.DefaultLeaderConfigFile,
2024-01-17 21:52:40 +00:00
"Config file to use")
_ = argparser.MarkFlagFilename("config", "yml", "yaml")
2024-01-17 21:52:40 +00:00
var socket string
argparser.Flags().StringVar(&socket, "socket", "tcp::8080",
"the socket to listen on; a \"family:address\" pair, where the\n"+
"\"family\" is one of \"tcp\", \"tcp4\", \"tcp6\", \"unix\", \"unixpacket\"\n"+
"(the usual Go net.Listen() network names), or \"fd\" (to listen\n"+
"on an already-open file descriptor). The \"address\" part for\n"+
"the \"fd\" family is one of\n"+
" 1. a file descriptor number (or the special names \"stdin\",\n"+
" \"stdout\", or \"stderr\" to refer to 0, 1, or 2\n"+
" respectively),\n"+
" 2. \"systemd:N\" to refer to the Nth file descriptor passed in\n"+
" via systemd (or \"systemd\" as shorthand for \"systemd:0\")\n"+
" (see sd_listend_fds(3)),\n"+
" 3. \"systemd:NAME\" to refer to a named file descriptor passed\n"+
" in via systemd (see sd_listend_fds_with_names(3)).\n")
argparser.RunE = func(cmd *cobra.Command, args []string) error {
stype, saddr, ok := strings.Cut(socket, ":")
if !ok {
return cliutil.FlagErrorFunc(cmd, fmt.Errorf("invalid address: %q", socket))
}
return Serve(cfgFile, stype, saddr)
}
ctx := context.Background()
if err := argparser.ExecuteContext(ctx); err != nil {
fmt.Fprintf(os.Stderr, "%v: error: %v\n", argparser.CommandPath(), err)
os.Exit(1)
}
}
func Serve(cfgFile, stype, saddr string) (err error) {
maybeSetErr := func(_err error) {
if err == nil && _err != nil {
err = _err
}
}
cfg, err := leader.LoadConfig(cfgFile)
2024-01-17 21:52:40 +00:00
if err != nil {
return err
}
2024-01-18 05:49:09 +00:00
db, err := cfg.DB()
2024-01-17 21:52:40 +00:00
if err != nil {
return err
}
2024-01-18 05:49:09 +00:00
defer func() { maybeSetErr(db.Close()) }()
2024-01-17 21:52:40 +00:00
sock, err := netListen(stype, saddr)
if err != nil {
return err
}
2024-01-18 21:17:26 +00:00
if err := db.GCRunningJobs(); err != nil {
return err
}
2024-01-23 18:48:57 +00:00
// Even tnough most of the routing is happening in big
// `switch` statements, the ServeMux is important because it
// cleans the paths, which would sort of be a pain to do
// ourselves.
2024-01-17 21:52:40 +00:00
router := http.NewServeMux()
2024-01-23 18:48:57 +00:00
router.Handle("/", &ServerState{db: db})
router.Handle("/webhooks/", http.StripPrefix("/webhooks/", &Webhooks{cfgFile: cfgFile}))
2024-01-17 21:52:40 +00:00
log.Printf("Serving on %v...", sock.Addr())
return http.Serve(sock, router)
}
2024-01-19 02:47:10 +00:00
// HTTP utils //////////////////////////////////////////////////////////////////
func httpAddSlash(w http.ResponseWriter, r *http.Request) {
u := &url.URL{Path: r.URL.Path + "/", RawQuery: r.URL.RawQuery}
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
}
type _httpError struct {
Err error
Code int
}
func (e *_httpError) Error() string {
return fmt.Sprintf("HTTP %d: %v", e.Code, e.Err)
}
func httpError(err error, code int) *_httpError {
return &_httpError{Code: code, Err: err}
}
func writeError(err error) *_httpError {
return &_httpError{Code: -100, Err: err}
}
func writeOrServerError(err error) *_httpError {
return &_httpError{Code: http.StatusInternalServerError, Err: err}
}
func httpNotFound() *_httpError {
return httpError(errors.New("404 page not found"), http.StatusNotFound)
}
2024-01-23 18:48:57 +00:00
// ServerState /////////////////////////////////////////////////////////////////
2024-01-19 02:47:10 +00:00
2024-01-18 02:47:08 +00:00
type ServerState struct {
db *leader.DB
running typedsync.Map[common.JobID, chan struct{}]
2024-01-18 02:47:08 +00:00
}
2024-01-17 21:52:40 +00:00
2024-01-18 02:08:47 +00:00
//go:embed style.css
var fileStyle []byte
2024-01-17 21:52:40 +00:00
//go:embed favicon.ico
var fileFavicon []byte
2024-01-18 04:15:20 +00:00
//go:embed html
2024-01-18 02:47:08 +00:00
var tmplFS embed.FS
2024-01-18 01:08:22 +00:00
2024-01-18 04:15:20 +00:00
var (
tmplIndex = template.Must(template.ParseFS(tmplFS, "html/pages/index.html.tmpl", "html/lib/*.html.tmpl"))
tmplJobs = template.Must(template.ParseFS(tmplFS, "html/pages/jobs.html.tmpl", "html/lib/*.html.tmpl"))
tmplJob = template.Must(template.ParseFS(tmplFS, "html/pages/job.html.tmpl", "html/lib/*.html.tmpl"))
)
2024-01-19 02:47:10 +00:00
func (o *ServerState) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := o.serveHTTP(w, r); err != nil {
if err.Code != -100 {
http.Error(w, err.Error(), err.Code)
}
if err.Code/100 == 5 {
log.Printf("error: %q: %v", r.URL, err)
}
2024-01-18 04:15:20 +00:00
}
2024-01-18 02:47:08 +00:00
}
2024-01-18 01:08:22 +00:00
2024-01-19 02:47:10 +00:00
func (o *ServerState) serveHTTP(w http.ResponseWriter, r *http.Request) *_httpError {
switch {
case r.URL.Path == "/":
if r.Method != http.MethodGet {
return httpError(errors.New("only GET is supported"), http.StatusMethodNotAllowed)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := tmplIndex.Execute(w, nil); err != nil {
return writeOrServerError(err)
}
case r.URL.Path == "/style.css":
if r.Method != http.MethodGet {
return httpError(errors.New("only GET is supported"), http.StatusMethodNotAllowed)
}
w.Header().Set("Content-Type", "text/css")
if _, err := w.Write(fileStyle); err != nil {
return writeError(err)
}
case r.URL.Path == "/favicon.ico":
if r.Method != http.MethodGet {
return httpError(errors.New("only GET is supported"), http.StatusMethodNotAllowed)
}
w.Header().Set("Content-Type", "image/vnd.microsoft.icon")
if _, err := w.Write(fileFavicon); err != nil {
return writeError(err)
}
case r.URL.Path == "/eclipse.tar":
if r.Method != http.MethodGet {
return httpError(errors.New("only GET is supported"), http.StatusMethodNotAllowed)
}
w.Header().Set("Content-Type", "application/x-tar")
if _, err := w.Write(source.Tarball); err != nil {
return writeError(err)
}
case r.URL.Path == "/jobs":
httpAddSlash(w, r)
case r.URL.Path == "/jobs/":
2024-01-18 01:08:22 +00:00
switch r.Method {
case http.MethodGet:
2024-01-23 00:24:31 +00:00
params := r.URL.Query()
status := common.JobStatus(-1)
2024-01-23 00:24:31 +00:00
_ = status.UnmarshalText([]byte(params.Get("status"))) // allowed to fail
wait, _ := strconv.ParseBool(params.Get("wait")) // allowed to fail
log.Printf("status=%v wait=%v", status, wait)
var jobs []common.Job
for i := 0; i == 0 || (wait && len(jobs) == 0); i++ {
var err error
jobs, err = o.db.ListJobs(status)
if err != nil {
return httpError(err, http.StatusInternalServerError)
}
if wait && len(jobs) == 0 {
if i%30 == 0 {
// Send a keepalive, every 30-seconds-ish (nginx's
// default HTTP 504 timeout is 60s, we could say
// that you need to disable it, but this feels
// butter).
w.WriteHeader(http.StatusContinue)
}
time.Sleep(1 * time.Second)
}
}
2024-01-23 00:24:31 +00:00
switch httputil.NegotiateContentType(r, []string{"application/json", "text/html"}, "application/json") {
case "text/html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := tmplJobs.Execute(w, map[string]any{"Jobs": jobs}); err != nil {
return writeOrServerError(err)
}
case "application/json":
jsonBytes, err := json.Marshal(jobs)
if err != nil {
return httpError(err, http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(jsonBytes); err != nil {
return writeError(err)
}
default:
return httpError(errors.New("only text/html and application/json are supported"), http.StatusNotAcceptable)
2024-01-18 04:15:20 +00:00
}
2024-01-18 01:08:22 +00:00
case http.MethodPost:
2024-01-19 02:47:10 +00:00
return httpError(errors.New("TODO: allow POSTing to /jobs/ to create a job"), http.StatusNotImplemented)
2024-01-18 01:08:22 +00:00
default:
2024-01-19 02:47:10 +00:00
return httpError(errors.New("only GET and POST are supported"), http.StatusMethodNotAllowed)
2024-01-18 01:08:22 +00:00
}
2024-01-19 02:47:10 +00:00
case strings.HasPrefix(r.URL.Path, "/jobs/"):
rest := strings.TrimPrefix(r.URL.Path, "/jobs/")
jobIDStr, file, haveSlash := strings.Cut(rest, "/")
jobID, err := strconv.Atoi(jobIDStr)
if err != nil {
return httpNotFound()
}
job, err := o.db.GetJob(common.JobID(jobID))
2024-01-19 02:47:10 +00:00
if err != nil {
return httpNotFound()
}
if !haveSlash {
httpAddSlash(w, r)
return nil
}
switch file {
case "":
switch r.Method {
case http.MethodGet:
dirfs := job.DirFS()
artifactNames := []string{}
if err := fs.WalkDir(dirfs, "artifacts", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Type() == 0 {
artifactNames = append(artifactNames, strings.TrimPrefix(path, "artifacts/"))
}
return nil
}); err != nil && !os.IsNotExist(err) {
return httpError(err, http.StatusInternalServerError)
2024-01-18 19:55:53 +00:00
}
2024-01-19 02:47:10 +00:00
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := tmplJob.Execute(w, map[string]any{
"Job": job,
"ArtifactNames": artifactNames,
}); err != nil {
return writeOrServerError(err)
2024-01-18 19:55:53 +00:00
}
2024-01-19 02:47:10 +00:00
case http.MethodPost:
// TODO: Validate API key
return o.runJob(w, r, job)
default:
return httpError(errors.New("only GET and POST are supported"), http.StatusMethodNotAllowed)
2024-01-18 16:17:10 +00:00
}
2024-01-18 07:43:02 +00:00
default:
2024-01-19 02:47:10 +00:00
if r.Method != http.MethodGet {
return httpError(errors.New("only GET is supported"), http.StatusMethodNotAllowed)
2024-01-18 19:55:53 +00:00
}
if file == "log.txt" && job.Status == common.StatusRunning {
2024-01-19 02:47:10 +00:00
ch, _ := o.running.Load(job.ID)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2024-01-19 03:04:09 +00:00
if err := tailf(w, ch, filepath.Join(job.Dir, "log.txt")); err != nil {
return writeOrServerError(err)
2024-01-18 19:55:53 +00:00
}
2024-01-19 02:47:10 +00:00
} else {
r.URL.Path = "/" + file
http.FileServer(http.Dir(job.Dir)).ServeHTTP(w, r)
2024-01-18 19:55:53 +00:00
}
2024-01-18 19:08:10 +00:00
}
2024-01-19 02:47:10 +00:00
default:
return httpNotFound()
2024-01-18 19:08:10 +00:00
}
2024-01-19 02:47:10 +00:00
return nil
2024-01-18 19:08:10 +00:00
}
2024-01-19 03:04:09 +00:00
// tailf is like the `tail -f --retry -n +1`, but a Go function.
func tailf(w http.ResponseWriter, cancel <-chan struct{}, filename string) error {
ctl := http.NewResponseController(w)
fh, _ := os.Open(filename)
if fh != nil {
if _, err := io.Copy(w, fh); err != nil {
return err
}
if err := ctl.Flush(); err != nil {
return err
}
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for keepGoing := true; keepGoing; {
select {
case <-cancel:
keepGoing = false
case <-ticker.C:
}
if fh == nil {
fh, _ = os.Open(filename)
}
if fh != nil {
if _, err := io.Copy(w, fh); err != nil {
return err
}
if err := ctl.Flush(); err != nil {
return err
}
}
}
return nil
}
func (o *ServerState) runJob(w http.ResponseWriter, r *http.Request, job common.Job) (_reterr *_httpError) {
2024-01-18 21:17:26 +00:00
log.Printf("running job: %q", r.URL)
ctl := http.NewResponseController(w)
if err := ctl.EnableFullDuplex(); err != nil {
2024-01-19 02:47:10 +00:00
return httpError(err, http.StatusInternalServerError)
2024-01-18 21:17:26 +00:00
}
2024-01-18 19:08:10 +00:00
ch := make(chan struct{})
if _, conflict := o.running.LoadOrStore(job.ID, ch); conflict {
2024-01-19 02:47:10 +00:00
return httpError(errors.New("job has already started (ch)"), http.StatusConflict)
2024-01-18 19:08:10 +00:00
}
defer func() {
close(ch)
o.running.Delete(job.ID)
}()
2024-01-23 04:10:47 +00:00
swapped, err := o.db.SwapJobStatus(job.ID, common.StatusNew, common.StatusRunning, "")
2024-01-18 19:08:10 +00:00
if err != nil {
2024-01-19 02:47:10 +00:00
return httpError(err, http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
if !swapped {
2024-01-19 02:47:10 +00:00
return httpError(errors.New("job has already started (db)"), http.StatusConflict)
2024-01-18 19:08:10 +00:00
}
defer func() {
2024-01-23 04:10:47 +00:00
if _reterr != nil {
if _, err := o.db.SwapJobStatus(job.ID, common.StatusRunning, common.StatusError, _reterr.Err.Error()); err != nil {
_reterr = httpError(err, http.StatusInternalServerError)
}
2024-01-18 19:08:10 +00:00
}
}()
bs, err := json.Marshal(job)
if err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("encoding job description: %w", err), http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("invalid Content-Type: %q: %w", r.Header.Get("Content-Type"), err), http.StatusBadRequest)
2024-01-18 19:08:10 +00:00
}
if mediaType != "multipart/mixed" {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("invalid Content-Type: %q", r.Header.Get("Content-Type")), http.StatusBadRequest)
2024-01-18 19:08:10 +00:00
}
reader := multipart.NewReader(r.Body, params["boundary"])
2024-01-19 04:54:39 +00:00
if strings.Contains(r.Header.Get("Expect"), "100-continue") {
// The stdlib net/http server is supposed to automatically do this if needed, but it
// triggers this based on us reading the request body. Normally that would be a
// good trigger; usually you *must* have read the body before you start writing the
// response... but ctl.EnableFullDuplex() broke that assumption. If we start
// writing before we read, the net/http server would skip straight to the "HTTP 200"
// response, skipping the require "HTTP 100" response. (as of Go 1.21.6)
//
// So manually emit the "HTTP 100" response if it looks like we need it. Our
// strings.Contains() check is a little loose--it's OK for is to emit this even if
// it wasn't asked for.
w.WriteHeader(http.StatusContinue)
}
2024-01-18 19:08:10 +00:00
w.Header().Set("Content-Type", "application/json")
2024-01-19 01:32:01 +00:00
// This is the line between "can return a status code" and "can't" ///////////////
2024-01-18 19:08:10 +00:00
if _, err := w.Write(bs); err != nil {
2024-01-19 02:47:10 +00:00
return writeError(fmt.Errorf("write job description: %w", err))
2024-01-18 19:08:10 +00:00
}
2024-01-18 21:17:26 +00:00
if err := ctl.Flush(); err != nil {
2024-01-19 02:47:10 +00:00
return writeError(fmt.Errorf("write job description: %w", err))
2024-01-18 21:17:26 +00:00
}
2024-01-18 19:08:10 +00:00
var status string
for {
part, err := reader.NextPart()
if err != nil {
if err == io.EOF {
break
}
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("read next part: %w", err), http.StatusBadRequest)
2024-01-18 19:08:10 +00:00
}
2024-01-19 01:23:49 +00:00
log.Printf("part: %q", part.Header.Get("Content-Disposition"))
2024-01-18 21:17:26 +00:00
disposition, params, err := mime.ParseMediaType(part.Header.Get("Content-Disposition"))
2024-01-18 19:08:10 +00:00
if err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("invalid Content-Disposition: %q: %w", part.Header.Get("Content-Disposition"), err), http.StatusBadRequest)
2024-01-18 19:08:10 +00:00
}
filename := path.Clean(params["filename"])
switch {
case disposition == "attachment" && filename == "log.txt" || strings.HasPrefix(filename, "artifacts/"):
fsFilename := filepath.Join(job.Dir, filepath.FromSlash(filename))
if err := os.MkdirAll(filepath.Dir(fsFilename), 0755); err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("saving file: %q: %w", filename, err), http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
fh, err := os.Create(fsFilename)
if err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("saving file: %q: %w", filename, err), http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
2024-01-19 02:47:10 +00:00
if _, err := io.Copy(fh, part); err != nil {
return writeOrServerError(fmt.Errorf("saving file: %q: %w", filename, err))
2024-01-18 19:08:10 +00:00
}
if err := fh.Close(); err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("saving file: %q: %w", filename, err), http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
case disposition == "form-data" && params["name"] == "status":
statusBytes, err := io.ReadAll(part)
if err != nil {
2024-01-23 03:56:09 +00:00
return httpError(fmt.Errorf("reading status: %w", err), http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
status = strings.TrimSpace(string(statusBytes))
default:
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("invalid Content-Disposition: %q", part.Header.Get("Content-Disposition")), http.StatusBadRequest)
2024-01-18 19:08:10 +00:00
}
if err := part.Close(); err != nil {
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("closing part: %w", err), http.StatusBadRequest)
2024-01-18 19:08:10 +00:00
}
}
switch status {
case "0":
2024-01-23 04:10:47 +00:00
if _, err := o.db.SwapJobStatus(job.ID, common.StatusRunning, common.StatusSucceeded, ""); err != nil {
2024-01-19 02:47:10 +00:00
return httpError(err, http.StatusInternalServerError)
2024-01-18 19:08:10 +00:00
}
case "":
2024-01-19 02:47:10 +00:00
return httpError(fmt.Errorf("no status: %w", io.ErrUnexpectedEOF), http.StatusBadRequest)
2024-01-18 04:15:20 +00:00
default:
2024-01-23 04:10:47 +00:00
if _, err := o.db.SwapJobStatus(job.ID, common.StatusRunning, common.StatusFailed, status); err != nil {
2024-01-19 02:47:10 +00:00
return httpError(err, http.StatusInternalServerError)
2024-01-18 07:43:02 +00:00
}
2024-01-18 01:08:22 +00:00
}
2024-01-18 19:08:10 +00:00
return nil
2024-01-18 01:08:22 +00:00
}