2018-06-12 10:09:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
2018-12-10 10:59:24 +00:00
|
|
|
"novit.nc/direktil/pkg/localconfig"
|
2018-06-12 10:09:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
reHost = regexp.MustCompile("^/hosts/([^/]+)/([^/]+)$")
|
|
|
|
|
|
|
|
trustXFF = flag.Bool("trust-xff", true, "Trust the X-Forwarded-For header")
|
|
|
|
)
|
|
|
|
|
2018-12-10 10:59:24 +00:00
|
|
|
func renderHost(w http.ResponseWriter, r *http.Request, what string, host *localconfig.Host, cfg *localconfig.Config) {
|
2018-07-03 07:35:52 +00:00
|
|
|
ctx, err := newRenderContext(host, cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
2018-06-12 10:09:47 +00:00
|
|
|
|
|
|
|
switch what {
|
|
|
|
case "ipxe":
|
|
|
|
w.Header().Set("Content-Type", "text/x-ipxe")
|
|
|
|
case "config":
|
|
|
|
w.Header().Set("Content-Type", "text/vnd.yaml")
|
|
|
|
default:
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch what {
|
|
|
|
case "ipxe":
|
|
|
|
err = renderIPXE(w, ctx)
|
|
|
|
|
|
|
|
case "kernel":
|
|
|
|
err = renderKernel(w, r, ctx)
|
|
|
|
|
|
|
|
case "initrd":
|
2018-12-10 10:59:24 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildInitrd)
|
2018-06-12 10:09:47 +00:00
|
|
|
|
|
|
|
case "boot.iso":
|
2018-12-10 10:59:24 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildBootISO)
|
2018-06-12 10:09:47 +00:00
|
|
|
|
2018-07-03 07:35:52 +00:00
|
|
|
case "boot.tar":
|
2018-12-10 10:59:24 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildBootTar)
|
2018-07-03 07:35:52 +00:00
|
|
|
|
2018-11-15 07:07:10 +00:00
|
|
|
case "boot.img":
|
2018-11-13 03:44:15 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildBootImg)
|
|
|
|
|
2018-11-15 07:07:10 +00:00
|
|
|
case "boot.img.gz":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildBootImgGZ)
|
|
|
|
|
|
|
|
case "boot.img.lz4":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildBootImgLZ4)
|
|
|
|
|
2018-06-12 10:09:47 +00:00
|
|
|
case "config":
|
|
|
|
err = renderConfig(w, r, ctx)
|
|
|
|
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if isNotFound(err) {
|
|
|
|
log.Printf("host %s: %s: %v", what, host.Name, err)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
} else {
|
|
|
|
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|