2018-06-12 10:09:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2018-12-10 10:59:24 +00:00
|
|
|
"novit.nc/direktil/pkg/localconfig"
|
2018-06-12 10:09:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-06-13 02:26:00 +00:00
|
|
|
hostsToken = flag.String("hosts-token", "", "Token to give to access /hosts (open is none)")
|
|
|
|
|
2018-06-12 10:09:47 +00:00
|
|
|
reHost = regexp.MustCompile("^/hosts/([^/]+)/([^/]+)$")
|
|
|
|
|
|
|
|
trustXFF = flag.Bool("trust-xff", true, "Trust the X-Forwarded-For header")
|
|
|
|
)
|
|
|
|
|
2018-06-13 02:26:00 +00:00
|
|
|
func authorizeHosts(r *http.Request) bool {
|
|
|
|
if *hostsToken == "" {
|
|
|
|
// access is open
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
reqToken := r.Header.Get("Authorization")
|
|
|
|
|
|
|
|
return reqToken == "Bearer "+*hostsToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func forbidden(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("denied access to %s from %s", r.RequestURI, r.RemoteAddr)
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
2018-06-12 10:09:47 +00:00
|
|
|
func serveHostByIP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
host, cfg := hostByIP(w, r)
|
|
|
|
if host == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-16 03:33:30 +00:00
|
|
|
what := strings.TrimLeft(r.URL.Path, "/")
|
2018-06-12 10:09:47 +00:00
|
|
|
|
|
|
|
renderHost(w, r, what, host, cfg)
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:59:24 +00:00
|
|
|
func hostByIP(w http.ResponseWriter, r *http.Request) (*localconfig.Host, *localconfig.Config) {
|
2018-06-12 10:09:47 +00:00
|
|
|
remoteAddr := r.RemoteAddr
|
|
|
|
|
|
|
|
if *trustXFF {
|
2018-11-02 04:30:12 +00:00
|
|
|
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
2018-06-12 10:09:47 +00:00
|
|
|
remoteAddr = strings.Split(xff, ",")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hostIP, _, err := net.SplitHostPort(remoteAddr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
hostIP = remoteAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := readConfig()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
host := cfg.HostByIP(hostIP)
|
|
|
|
|
|
|
|
if host == nil {
|
|
|
|
log.Print("no host found for IP ", hostIP)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return host, cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveHosts(w http.ResponseWriter, r *http.Request) {
|
2018-06-13 02:26:00 +00:00
|
|
|
if !authorizeHosts(r) {
|
|
|
|
forbidden(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-12 10:09:47 +00:00
|
|
|
cfg, err := readConfig()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-03 07:35:52 +00:00
|
|
|
renderJSON(w, cfg.Hosts)
|
2018-06-12 10:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serveHost(w http.ResponseWriter, r *http.Request) {
|
2018-06-13 02:26:00 +00:00
|
|
|
if !authorizeHosts(r) {
|
|
|
|
forbidden(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-12 10:09:47 +00:00
|
|
|
match := reHost.FindStringSubmatch(r.URL.Path)
|
|
|
|
if match == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hostName, what := match[1], match[2]
|
|
|
|
|
|
|
|
cfg, err := readConfig()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
host := cfg.Host(hostName)
|
|
|
|
|
|
|
|
if host == nil {
|
|
|
|
host = cfg.HostByMAC(hostName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if host == nil {
|
|
|
|
log.Printf("no host with name or MAC %q", hostName)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderHost(w, r, what, host, cfg)
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:59:24 +00:00
|
|
|
func renderHost(w http.ResponseWriter, r *http.Request, what string, host *localconfig.Host, cfg *localconfig.Config) {
|
2018-07-03 07:35:52 +00:00
|
|
|
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
|
|
|
|
}
|
2018-06-12 10:09:47 +00:00
|
|
|
|
|
|
|
switch what {
|
|
|
|
case "ipxe":
|
|
|
|
w.Header().Set("Content-Type", "text/x-ipxe")
|
|
|
|
case "config":
|
|
|
|
w.Header().Set("Content-Type", "text/vnd.yaml")
|
|
|
|
default:
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch what {
|
|
|
|
case "ipxe":
|
|
|
|
err = renderIPXE(w, ctx)
|
|
|
|
|
|
|
|
case "kernel":
|
|
|
|
err = renderKernel(w, r, ctx)
|
|
|
|
|
|
|
|
case "initrd":
|
2018-12-10 10:59:24 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildInitrd)
|
2018-06-12 10:09:47 +00:00
|
|
|
|
|
|
|
case "boot.iso":
|
2018-12-10 10:59:24 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildBootISO)
|
2018-06-12 10:09:47 +00:00
|
|
|
|
2018-07-03 07:35:52 +00:00
|
|
|
case "boot.tar":
|
2018-12-10 10:59:24 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildBootTar)
|
2018-07-03 07:35:52 +00:00
|
|
|
|
2018-11-15 07:07:10 +00:00
|
|
|
case "boot.img":
|
2018-11-13 03:44:15 +00:00
|
|
|
err = renderCtx(w, r, ctx, what, buildBootImg)
|
|
|
|
|
2018-11-15 07:07:10 +00:00
|
|
|
case "boot.img.gz":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildBootImgGZ)
|
|
|
|
|
|
|
|
case "boot.img.lz4":
|
|
|
|
err = renderCtx(w, r, ctx, what, buildBootImgLZ4)
|
|
|
|
|
2018-06-12 10:09:47 +00:00
|
|
|
case "config":
|
|
|
|
err = renderConfig(w, r, ctx)
|
|
|
|
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if isNotFound(err) {
|
|
|
|
log.Printf("host %s: %s: %v", what, host.Name, err)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
} else {
|
|
|
|
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderJSON(w http.ResponseWriter, v interface{}) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(v)
|
|
|
|
}
|
2018-07-07 01:22:35 +00:00
|
|
|
|
|
|
|
func serveClusters(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cfg, err := readConfig()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterNames := make([]string, len(cfg.Clusters))
|
|
|
|
for i, cluster := range cfg.Clusters {
|
|
|
|
clusterNames[i] = cluster.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, clusterNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveCluster(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// "/clusters/<name>/<what>" split => "", "clusters", "<name>", "<what>"
|
|
|
|
p := strings.Split(r.URL.Path, "/")
|
|
|
|
|
|
|
|
if len(p) != 4 {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-07 05:39:55 +00:00
|
|
|
clusterName := p[2]
|
2018-12-10 10:59:24 +00:00
|
|
|
what := p[3]
|
2018-07-07 01:22:35 +00:00
|
|
|
|
|
|
|
cfg, err := readConfig()
|
|
|
|
if err != nil {
|
2018-12-10 10:59:24 +00:00
|
|
|
log.Print("failed to read config: ", err)
|
2018-07-07 01:22:35 +00:00
|
|
|
http.Error(w, "", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := cfg.Cluster(clusterName)
|
|
|
|
if cluster == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch what {
|
|
|
|
case "addons":
|
2018-12-10 10:59:24 +00:00
|
|
|
if len(cluster.Addons) == 0 {
|
2018-07-07 01:22:35 +00:00
|
|
|
log.Printf("cluster %q has no addons defined", clusterName)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:59:24 +00:00
|
|
|
w.Write([]byte(cluster.Addons))
|
2018-07-07 01:22:35 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}
|