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
|
|
|
)
|
|
|
|
|
2020-03-05 09:55:59 +00:00
|
|
|
var trustXFF = flag.Bool("trust-xff", true, "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 {
|
|
|
|
prefix string
|
2019-02-04 04:46:03 +00:00
|
|
|
hostDoc string
|
2019-02-01 07:28:08 +00:00
|
|
|
getHost func(req *restful.Request) string
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
func (ws *wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteBuilder)) {
|
|
|
|
b := func(what string) *restful.RouteBuilder {
|
|
|
|
return rws.GET(ws.prefix + "/" + what).To(ws.render)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rb := range []*restful.RouteBuilder{
|
2019-02-04 04:46:03 +00:00
|
|
|
rws.GET(ws.prefix).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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// boot v2
|
|
|
|
// - 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)"),
|
|
|
|
// - initrd
|
|
|
|
b("initrd-v2").
|
|
|
|
Produces(mime.OCTET).
|
|
|
|
Doc("Get the " + ws.hostDoc + "'s initial RAM disk (v2)"),
|
|
|
|
// - 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 04:46:03 +00:00
|
|
|
func (ws *wsHost) host(req *restful.Request, resp *restful.Response) (host *localconfig.Host, cfg *localconfig.Config) {
|
2019-02-01 07:28:08 +00:00
|
|
|
hostname := ws.getHost(req)
|
|
|
|
if hostname == "" {
|
2019-02-04 02:56:43 +00:00
|
|
|
wsNotFound(req, resp)
|
2019-02-01 07:28:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := readConfig()
|
|
|
|
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)
|
2019-02-04 02:56:43 +00:00
|
|
|
wsNotFound(req, resp)
|
2019-02-01 07:28:08 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-04 04:46:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *wsHost) get(req *restful.Request, resp *restful.Response) {
|
|
|
|
host, _ := ws.host(req, resp)
|
|
|
|
if host == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteEntity(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *wsHost) render(req *restful.Request, resp *restful.Response) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
case "initrd":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildInitrd)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-04-28 01:33:19 +00:00
|
|
|
// boot v2
|
|
|
|
case "bootstrap-config":
|
|
|
|
err = renderBootstrapConfig(w, r, ctx, false)
|
|
|
|
case "bootstrap-config.json":
|
|
|
|
err = renderBootstrapConfig(w, r, ctx, true)
|
|
|
|
case "initrd-v2":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildInitrdV2)
|
|
|
|
case "bootstrap.tar":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildBootstrap)
|
|
|
|
|
2019-02-04 04:11:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|