-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.go
88 lines (75 loc) · 2.63 KB
/
boot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package endpoint
import (
"fmt"
"net"
"net/http"
"strings"
"github.com/metal-stack/metal-core/internal/rest"
"github.com/metal-stack/metal-core/pkg/domain"
"github.com/emicklei/go-restful/v3"
"github.com/metal-stack/metal-lib/zapup"
"go.uber.org/zap"
)
func (h *endpointHandler) Boot(request *restful.Request, response *restful.Response) {
mac := request.PathParameter("mac")
zapup.MustRootLogger().Debug("Request Metal-API for a machine",
zap.String("MAC", mac),
)
sc, machines := h.APIClient().FindMachines(mac)
if sc == http.StatusOK {
if len(machines) == 0 {
rest.Respond(response, http.StatusOK, createBootDiscoveryImageResponse(h))
return
}
if len(machines) == 1 {
if machines[0].Allocation == nil {
rest.Respond(response, http.StatusOK, createBootDiscoveryImageResponse(h))
return
}
// Machine was already in the installation phase but crashed before finalizing allocation
// we can boot into metal-hammer again.
if !*machines[0].Allocation.Succeeded {
rest.Respond(response, http.StatusOK, createBootDiscoveryImageResponse(h))
return
}
zapup.MustRootLogger().Error("machine tries to pxe boot which is not expected.",
zap.Int("statusCode", sc),
zap.String("MAC", mac),
zap.String("machineID", *machines[0].ID),
)
return
}
zapup.MustRootLogger().Error("more than one machines with same mac found, not booting machine.",
zap.Int("statusCode", sc),
zap.String("MAC", mac),
)
// FIXME this should not happen, we should consider returning a rec overy image for digging into to root cause.
} else {
zapup.MustRootLogger().Error("Request Metal-API for a machine", zap.String("MAC", mac), zap.Int("StatusCode", sc))
}
}
func createBootDiscoveryImageResponse(e *endpointHandler) domain.BootResponse {
cfg := e.Config
cidr, _, _ := net.ParseCIDR(cfg.CIDR)
metalCoreAddress := fmt.Sprintf("METAL_CORE_ADDRESS=%v:%d", cidr.String(), cfg.Port)
metalAPIURL := fmt.Sprintf("METAL_API_URL=%s://%s:%d%s", cfg.ApiProtocol, cfg.ApiIP, cfg.ApiPort, cfg.ApiBasePath)
bc := e.BootConfig
// try to update boot config
s, err := e.APIClient().FindPartition(cfg.PartitionID)
if err == nil {
bc.MetalHammerImageURL = s.Bootconfig.Imageurl
bc.MetalHammerKernelURL = s.Bootconfig.Kernelurl
bc.MetalHammerCommandLine = s.Bootconfig.Commandline
}
cmdline := []string{bc.MetalHammerCommandLine, metalCoreAddress, metalAPIURL}
if strings.ToUpper(cfg.LogLevel) == "DEBUG" {
cmdline = append(cmdline, "DEBUG=1")
}
return domain.BootResponse{
Kernel: bc.MetalHammerKernelURL,
InitRamDisk: []string{
bc.MetalHammerImageURL,
},
CommandLine: strings.Join(cmdline, " "),
}
}