18
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
19
"github.com/siderolabs/talos/pkg/provision/internal/inmemhttp"
23
func ReadConfig(config interface{}) error {
24
d := json.NewDecoder(os.Stdin)
25
if err := d.Decode(config); err != nil {
26
return fmt.Errorf("error decoding config from stdin: %w", err)
30
return errors.New("extra unexpected input on stdin")
33
return os.Stdin.Close()
37
func ConfigureSignals() chan os.Signal {
38
signal.Ignore(syscall.SIGHUP)
40
c := make(chan os.Signal, 1)
41
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
46
func httpPostWrapper(f func() error) http.Handler {
47
return http.HandlerFunc(
48
func(w http.ResponseWriter, req *http.Request) {
50
_, _ = io.Copy(io.Discard, req.Body)
54
if req.Method != http.MethodPost {
55
w.WriteHeader(http.StatusNotImplemented)
62
w.WriteHeader(http.StatusInternalServerError)
64
fmt.Fprint(w, err.Error())
69
w.WriteHeader(http.StatusOK)
74
func httpGetWrapper(f func(w io.Writer)) http.Handler {
75
return http.HandlerFunc(
76
func(w http.ResponseWriter, req *http.Request) {
78
_, _ = io.Copy(io.Discard, req.Body)
84
w.Header().Add("Content-Type", "application/json")
85
w.WriteHeader(http.StatusOK)
87
w.Header().Add("Content-Type", "application/json")
88
w.WriteHeader(http.StatusOK)
92
w.WriteHeader(http.StatusNotImplemented)
99
func NewHTTPServer(gatewayAddr netip.Addr, port int, config []byte, controller Controller) (inmemhttp.Server, error) {
100
httpServer, err := inmemhttp.NewServer(nethelpers.JoinHostPort(gatewayAddr.String(), port))
102
return nil, fmt.Errorf("error launching in-memory HTTP server: %w", err)
105
if err = httpServer.AddFile("config.yaml", config); err != nil {
109
if controller != nil {
110
for _, method := range []struct {
116
f: controller.PowerOn,
119
pattern: "/poweroff",
120
f: controller.PowerOff,
124
f: controller.Reboot,
128
f: controller.PXEBootOnce,
131
httpServer.AddHandler(method.pattern, httpPostWrapper(method.f))
134
httpServer.AddHandler(
135
"/status", httpGetWrapper(
137
json.NewEncoder(w).Encode(controller.Status())
143
return httpServer, nil