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

93 lines
1.9 KiB
Go
Raw Normal View History

2019-02-01 07:28:08 +00:00
package main
import (
"log"
"path"
restful "github.com/emicklei/go-restful"
2019-02-04 04:06:02 +00:00
"novit.nc/direktil/local-server/pkg/mime"
2019-02-01 07:28:08 +00:00
)
type wsHost struct {
prefix string
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{
// raw configuration
2019-02-04 04:06:02 +00:00
b("config").
Produces(mime.YAML).
Doc("Get the host's configuration"),
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).
Doc("Get the host's boot disk image"),
b("boot.img.gz").
Produces(mime.DISK + "+gzip").
Doc("Get the host's boot disk image (gzip compressed)"),
b("boot.img.lz4").
Produces(mime.DISK + "+lz4").
Doc("Get the host'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).
Doc("Get the host'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).
Doc("Get the host'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).
Doc("Get the host's IPXE code (for netboot)"),
b("kernel").
Produces(mime.OCTET).
Doc("Get the host's kernel (ie: for netboot)"),
b("initrd").
Produces(mime.OCTET).
Doc("Get the host'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
}
}
func (ws *wsHost) render(req *restful.Request, resp *restful.Response) {
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
}
host := cfg.Host(hostname)
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
}
what := path.Base(req.Request.URL.Path)
renderHost(resp.ResponseWriter, req.Request, what, host, cfg)
}