This commit is contained in:
Mikaël Cluseau 2019-02-04 15:46:03 +11:00
parent 0ed062a7ff
commit d79683b130
4 changed files with 42 additions and 60 deletions

View File

@ -1,29 +0,0 @@
package main
import (
"fmt"
"os"
)
type notFoundError struct {
ref string
}
func (e notFoundError) Error() string {
return fmt.Sprintf("not found: %s", e.ref)
}
var _ error = notFoundError{}
func isNotFound(err error) bool {
if err == nil {
return false
}
if os.IsNotExist(err) {
return true
}
_, ok := err.(notFoundError)
return ok
}

View File

@ -16,6 +16,7 @@ var trustXFF = flag.Bool("trust-xff", true, "Trust the X-Forwarded-For header")
type wsHost struct { type wsHost struct {
prefix string prefix string
hostDoc string
getHost func(req *restful.Request) string getHost func(req *restful.Request) string
} }
@ -25,53 +26,56 @@ func (ws *wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteB
} }
for _, rb := range []*restful.RouteBuilder{ for _, rb := range []*restful.RouteBuilder{
rws.GET(ws.prefix).To(ws.get).
Doc("Get the " + ws.hostDoc + "'s details"),
// raw configuration // raw configuration
b("config"). b("config").
Produces(mime.YAML). Produces(mime.YAML).
Doc("Get the host's configuration"), Doc("Get the " + ws.hostDoc + "'s configuration"),
// metal/local HDD install // metal/local HDD install
b("boot.img"). b("boot.img").
Produces(mime.DISK). Produces(mime.DISK).
Doc("Get the host's boot disk image"), Doc("Get the " + ws.hostDoc + "'s boot disk image"),
b("boot.img.gz"). b("boot.img.gz").
Produces(mime.DISK + "+gzip"). Produces(mime.DISK + "+gzip").
Doc("Get the host's boot disk image (gzip compressed)"), Doc("Get the " + ws.hostDoc + "'s boot disk image (gzip compressed)"),
b("boot.img.lz4"). b("boot.img.lz4").
Produces(mime.DISK + "+lz4"). Produces(mime.DISK + "+lz4").
Doc("Get the host's boot disk image (lz4 compressed)"), Doc("Get the " + ws.hostDoc + "'s boot disk image (lz4 compressed)"),
// metal/local HDD upgrades // metal/local HDD upgrades
b("boot.tar"). b("boot.tar").
Produces(mime.TAR). Produces(mime.TAR).
Doc("Get the host's /boot archive (ie: for metal upgrades)"), Doc("Get the " + ws.hostDoc + "'s /boot archive (ie: for metal upgrades)"),
// read-only ISO support // read-only ISO support
b("boot.iso"). b("boot.iso").
Produces(mime.ISO). Produces(mime.ISO).
Doc("Get the host's boot CD-ROM image"), Doc("Get the " + ws.hostDoc + "'s boot CD-ROM image"),
// netboot support // netboot support
b("ipxe"). b("ipxe").
Produces(mime.IPXE). Produces(mime.IPXE).
Doc("Get the host's IPXE code (for netboot)"), Doc("Get the " + ws.hostDoc + "'s IPXE code (for netboot)"),
b("kernel"). b("kernel").
Produces(mime.OCTET). Produces(mime.OCTET).
Doc("Get the host's kernel (ie: for netboot)"), Doc("Get the " + ws.hostDoc + "'s kernel (ie: for netboot)"),
b("initrd"). b("initrd").
Produces(mime.OCTET). Produces(mime.OCTET).
Doc("Get the host's initial RAM disk (ie: for netboot)"), Doc("Get the " + ws.hostDoc + "'s initial RAM disk (ie: for netboot)"),
} { } {
alterRB(rb) alterRB(rb)
rws.Route(rb) rws.Route(rb)
} }
} }
func (ws *wsHost) render(req *restful.Request, resp *restful.Response) { func (ws *wsHost) host(req *restful.Request, resp *restful.Response) (host *localconfig.Host, cfg *localconfig.Config) {
hostname := ws.getHost(req) hostname := ws.getHost(req)
if hostname == "" { if hostname == "" {
wsNotFound(req, resp) wsNotFound(req, resp)
@ -84,12 +88,29 @@ func (ws *wsHost) render(req *restful.Request, resp *restful.Response) {
return return
} }
host := cfg.Host(hostname) host = cfg.Host(hostname)
if host == nil { if host == nil {
log.Print("no host named ", hostname) log.Print("no host named ", hostname)
wsNotFound(req, resp) wsNotFound(req, resp)
return return
} }
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
}
what := path.Base(req.Request.URL.Path) what := path.Base(req.Request.URL.Path)
@ -105,15 +126,9 @@ func renderHost(w http.ResponseWriter, r *http.Request, what string, host *local
} }
switch what { switch what {
case "ipxe":
w.Header().Set("Content-Type", "text/x-ipxe")
case "config": case "config":
w.Header().Set("Content-Type", "text/vnd.yaml") err = renderConfig(w, r, ctx)
default:
w.Header().Set("Content-Type", "application/octet-stream")
}
switch what {
case "ipxe": case "ipxe":
err = renderIPXE(w, ctx) err = renderIPXE(w, ctx)
@ -138,20 +153,12 @@ func renderHost(w http.ResponseWriter, r *http.Request, what string, host *local
case "boot.img.lz4": case "boot.img.lz4":
err = renderCtx(w, r, ctx, what, buildBootImgLZ4) err = renderCtx(w, r, ctx, what, buildBootImgLZ4)
case "config":
err = renderConfig(w, r, ctx)
default: default:
http.NotFound(w, r) http.NotFound(w, r)
} }
if err != nil { if err != nil {
if isNotFound(err) { log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
log.Printf("host %s: %s: %v", what, host.Name, err) http.Error(w, "", http.StatusServiceUnavailable)
http.NotFound(w, r)
} else {
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
http.Error(w, "", http.StatusServiceUnavailable)
}
} }
} }

View File

@ -21,8 +21,10 @@ func buildWS() *restful.WebService {
// clusters API // clusters API
ws.Route(ws.GET("/clusters").Filter(adminAuth).To(wsListClusters). ws.Route(ws.GET("/clusters").Filter(adminAuth).To(wsListClusters).
Doc("List clusters")) Doc("List clusters"))
ws.Route(ws.GET("/clusters/{cluster-name}").Filter(adminAuth).To(wsCluster). ws.Route(ws.GET("/clusters/{cluster-name}").Filter(adminAuth).To(wsCluster).
Doc("Get cluster details")) Doc("Get cluster details"))
ws.Route(ws.GET("/clusters/{cluster-name}/addons").Filter(adminAuth).To(wsClusterAddons). ws.Route(ws.GET("/clusters/{cluster-name}/addons").Filter(adminAuth).To(wsClusterAddons).
Produces(mime.YAML). Produces(mime.YAML).
Doc("Get cluster addons"). Doc("Get cluster addons").
@ -34,14 +36,16 @@ func buildWS() *restful.WebService {
Doc("List hosts")) Doc("List hosts"))
(&wsHost{ (&wsHost{
prefix: "", prefix: "/me",
hostDoc: "detected host",
getHost: detectHost, getHost: detectHost,
}).register(ws, func(rb *restful.RouteBuilder) { }).register(ws, func(rb *restful.RouteBuilder) {
rb.Notes("In this case, the host is detected from the remote IP") rb.Notes("In this case, the host is detected from the remote IP")
}) })
(&wsHost{ (&wsHost{
prefix: "/hosts/{host-name}", prefix: "/hosts/{host-name}",
hostDoc: "given host",
getHost: func(req *restful.Request) string { getHost: func(req *restful.Request) string {
return req.PathParameter("host-name") return req.PathParameter("host-name")
}, },

View File

@ -5,6 +5,6 @@ const (
TAR = "application/tar" TAR = "application/tar"
DISK = "application/x-diskimage" DISK = "application/x-diskimage"
ISO = "application/x-iso9660-image" ISO = "application/x-iso9660-image"
IPXE = "application/x-ipxe" IPXE = "text/x-ipxe"
OCTET = "application/octet-stream" OCTET = "application/octet-stream"
) )