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
|
|
|
|
|
|
|
"novit.nc/direktil/local-server/pkg/mime"
|
2019-02-04 04:11:11 +00:00
|
|
|
"novit.nc/direktil/pkg/localconfig"
|
2019-02-01 07:28:08 +00:00
|
|
|
)
|
|
|
|
|
2019-02-04 04:11:11 +00:00
|
|
|
var trustXFF = flag.Bool("trust-xff", true, "Trust the X-Forwarded-For header")
|
|
|
|
|
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).
|
|
|
|
Doc("Get the " + ws.hostDoc + "'s details"),
|
|
|
|
|
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)"),
|
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).
|
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)"),
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|