local-server/cmd/dkl-local-server/ws-host.go

199 lines
4.8 KiB
Go
Raw Normal View History

2019-02-01 07:28:08 +00:00
package main
import (
2019-02-04 04:11:11 +00:00
"flag"
2019-02-01 07:28:08 +00:00
"log"
2019-02-04 04:11:11 +00:00
"net/http"
2019-02-01 07:28:08 +00:00
"path"
restful "github.com/emicklei/go-restful"
2019-02-04 04:06:02 +00:00
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/pkg/localconfig"
"novit.tech/direktil/local-server/pkg/mime"
2019-02-01 07:28:08 +00:00
)
2023-05-18 17:55:52 +00:00
var (
allowDetectedHost = flag.Bool("allow-detected-host", false, "Allow access to host assets from its IP (insecure but enables unattended netboot)")
trustXFF = flag.Bool("trust-xff", false, "Trust the X-Forwarded-For header")
)
2019-02-04 04:11:11 +00:00
2019-02-01 07:28:08 +00:00
type wsHost struct {
2019-02-04 04:46:03 +00:00
hostDoc string
2023-02-13 14:57:30 +00:00
getHost func(req *restful.Request) (hostName string, err error)
2019-02-01 07:28:08 +00:00
}
2023-05-18 17:55:52 +00:00
func (ws wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteBuilder)) {
2019-02-04 02:56:43 +00:00
b := func(what string) *restful.RouteBuilder {
2023-05-18 17:55:52 +00:00
return rws.GET("/" + what).To(ws.render)
2019-02-04 02:56:43 +00:00
}
for _, rb := range []*restful.RouteBuilder{
2023-05-18 17:55:52 +00:00
rws.GET("").To(ws.get).
2019-12-16 07:00:57 +00:00
Doc("Get the "+ws.hostDoc+"'s details").
Returns(200, "OK", localconfig.Host{}),
2019-02-04 04:46:03 +00:00
2019-02-04 02:56:43 +00:00
// raw configuration
2019-02-04 04:06:02 +00:00
b("config").
Produces(mime.YAML).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s configuration"),
2019-02-04 02:56:43 +00:00
2019-02-06 03:27:20 +00:00
b("config.json").
Doc("Get the " + ws.hostDoc + "'s configuration (as JSON)"),
2019-02-04 02:56:43 +00:00
// metal/local HDD install
2019-02-04 04:06:02 +00:00
b("boot.img").
Produces(mime.DISK).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s boot disk image"),
2019-02-04 04:06:02 +00:00
b("boot.img.gz").
Produces(mime.DISK + "+gzip").
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s boot disk image (gzip compressed)"),
2019-02-04 04:06:02 +00:00
b("boot.img.lz4").
Produces(mime.DISK + "+lz4").
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s boot disk image (lz4 compressed)"),
2019-02-04 02:56:43 +00:00
// metal/local HDD upgrades
2019-02-04 04:06:02 +00:00
b("boot.tar").
Produces(mime.TAR).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s /boot archive (ie: for metal upgrades)"),
2022-05-31 09:52:26 +00:00
b("boot-efi.tar").
Produces(mime.TAR).
Doc("Get the " + ws.hostDoc + "'s /boot archive (ie: for metal upgrades)"),
2019-02-04 02:56:43 +00:00
// read-only ISO support
2019-02-04 04:06:02 +00:00
b("boot.iso").
Produces(mime.ISO).
2020-03-04 23:06:49 +00:00
Param(cmdlineParam).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s boot CD-ROM image"),
2019-02-04 02:56:43 +00:00
// netboot support
2019-02-04 04:06:02 +00:00
b("ipxe").
Produces(mime.IPXE).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s IPXE code (for netboot)"),
2019-02-04 04:06:02 +00:00
2023-08-20 09:08:42 +00:00
// boot support
2019-02-04 04:06:02 +00:00
b("kernel").
Produces(mime.OCTET).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s kernel (ie: for netboot)"),
2019-02-04 04:06:02 +00:00
b("initrd").
Produces(mime.OCTET).
2019-02-04 04:46:03 +00:00
Doc("Get the " + ws.hostDoc + "'s initial RAM disk (ie: for netboot)"),
2022-04-28 01:33:19 +00:00
// - bootstrap config
b("bootstrap-config").
Produces(mime.YAML).
Doc("Get the " + ws.hostDoc + "'s bootstrap configuration"),
b("bootstrap-config.json").
Doc("Get the " + ws.hostDoc + "'s bootstrap configuration (as JSON)"),
// - bootstrap
b("bootstrap.tar").
Produces(mime.TAR).
Doc("Get the " + ws.hostDoc + "'s bootstrap seed archive"),
2019-02-01 07:28:08 +00:00
} {
2019-02-04 02:56:43 +00:00
alterRB(rb)
rws.Route(rb)
2019-02-01 07:28:08 +00:00
}
}
2023-05-18 17:55:52 +00:00
func (ws wsHost) host(req *restful.Request, resp *restful.Response) (host *localconfig.Host, cfg *localconfig.Config) {
2023-02-13 14:57:30 +00:00
hostname, err := ws.getHost(req)
if err != nil {
wsError(resp, err)
return
}
2019-02-01 07:28:08 +00:00
if hostname == "" {
2023-02-13 17:07:10 +00:00
wsNotFound(resp)
2019-02-01 07:28:08 +00:00
return
}
2023-02-13 14:57:30 +00:00
cfg, err = readConfig()
2019-02-01 07:28:08 +00:00
if err != nil {
2019-02-04 02:56:43 +00:00
wsError(resp, err)
2019-02-01 07:28:08 +00:00
return
}
2019-02-04 04:46:03 +00:00
host = cfg.Host(hostname)
2019-02-01 07:28:08 +00:00
if host == nil {
log.Print("no host named ", hostname)
2023-02-13 17:07:10 +00:00
wsNotFound(resp)
2019-02-01 07:28:08 +00:00
return
}
2019-02-04 04:46:03 +00:00
return
}
2023-05-18 17:55:52 +00:00
func (ws wsHost) get(req *restful.Request, resp *restful.Response) {
2019-02-04 04:46:03 +00:00
host, _ := ws.host(req, resp)
if host == nil {
return
}
resp.WriteEntity(host)
}
2023-05-18 17:55:52 +00:00
func (ws wsHost) render(req *restful.Request, resp *restful.Response) {
2019-02-04 04:46:03 +00:00
host, cfg := ws.host(req, resp)
if host == nil {
return
}
2019-02-01 07:28:08 +00:00
what := path.Base(req.Request.URL.Path)
renderHost(resp.ResponseWriter, req.Request, what, host, cfg)
}
2019-02-04 04:11:11 +00:00
func renderHost(w http.ResponseWriter, r *http.Request, what string, host *localconfig.Host, cfg *localconfig.Config) {
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
}
switch what {
case "config":
2019-02-06 03:27:20 +00:00
err = renderConfig(w, r, ctx, false)
case "config.json":
err = renderConfig(w, r, ctx, true)
2019-02-04 04:11:11 +00:00
case "ipxe":
err = renderIPXE(w, ctx)
case "kernel":
err = renderKernel(w, r, ctx)
2023-08-20 09:08:42 +00:00
// boot v2
case "bootstrap-config":
err = renderBootstrapConfig(w, r, ctx, false)
case "bootstrap-config.json":
err = renderBootstrapConfig(w, r, ctx, true)
2019-02-04 04:11:11 +00:00
case "initrd":
err = renderCtx(w, r, ctx, what, buildInitrd)
2023-08-20 09:08:42 +00:00
case "bootstrap.tar":
err = renderCtx(w, r, ctx, what, buildBootstrap)
2019-02-04 04:11:11 +00:00
case "boot.iso":
err = renderCtx(w, r, ctx, what, buildBootISO)
case "boot.tar":
err = renderCtx(w, r, ctx, what, buildBootTar)
2022-05-31 09:52:26 +00:00
case "boot-efi.tar":
err = renderCtx(w, r, ctx, what, buildBootEFITar)
2019-02-04 04:11:11 +00:00
case "boot.img":
err = renderCtx(w, r, ctx, what, buildBootImg)
case "boot.img.gz":
err = renderCtx(w, r, ctx, what, buildBootImgGZ)
case "boot.img.lz4":
err = renderCtx(w, r, ctx, what, buildBootImgLZ4)
default:
http.NotFound(w, r)
}
if err != nil {
2019-02-04 04:46:03 +00:00
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
http.Error(w, "", http.StatusServiceUnavailable)
2019-02-04 04:11:11 +00:00
}
}