-
Notifications
You must be signed in to change notification settings - Fork 456
/
launch.go
144 lines (120 loc) · 3.23 KB
/
launch.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package vm
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/netip"
"os"
"os/signal"
"syscall"
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
"github.com/siderolabs/talos/pkg/provision/internal/inmemhttp"
)
// ReadConfig loads configuration from stdin.
func ReadConfig(config interface{}) error {
d := json.NewDecoder(os.Stdin)
if err := d.Decode(config); err != nil {
return fmt.Errorf("error decoding config from stdin: %w", err)
}
if d.More() {
return errors.New("extra unexpected input on stdin")
}
return os.Stdin.Close()
}
// ConfigureSignals configures signal handling for the process.
func ConfigureSignals() chan os.Signal {
signal.Ignore(syscall.SIGHUP)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
return c
}
func httpPostWrapper(f func() error) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
if req.Body != nil {
_, _ = io.Copy(io.Discard, req.Body) //nolint:errcheck
req.Body.Close() //nolint:errcheck
}
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusNotImplemented)
return
}
err := f()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
w.WriteHeader(http.StatusOK)
},
)
}
func httpGetWrapper(f func(w io.Writer)) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
if req.Body != nil {
_, _ = io.Copy(io.Discard, req.Body) //nolint:errcheck
req.Body.Close() //nolint:errcheck
}
switch req.Method {
case http.MethodHead:
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
case http.MethodGet:
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
f(w)
default:
w.WriteHeader(http.StatusNotImplemented)
}
},
)
}
// NewHTTPServer creates new inmemhttp.Server and mounts config file into it.
func NewHTTPServer(gatewayAddr netip.Addr, port int, config []byte, controller Controller) (inmemhttp.Server, error) {
httpServer, err := inmemhttp.NewServer(nethelpers.JoinHostPort(gatewayAddr.String(), port))
if err != nil {
return nil, fmt.Errorf("error launching in-memory HTTP server: %w", err)
}
if err = httpServer.AddFile("config.yaml", config); err != nil {
return nil, err
}
if controller != nil {
for _, method := range []struct {
pattern string
f func() error
}{
{
pattern: "/poweron",
f: controller.PowerOn,
},
{
pattern: "/poweroff",
f: controller.PowerOff,
},
{
pattern: "/reboot",
f: controller.Reboot,
},
{
pattern: "/pxeboot",
f: controller.PXEBootOnce,
},
} {
httpServer.AddHandler(method.pattern, httpPostWrapper(method.f))
}
httpServer.AddHandler(
"/status", httpGetWrapper(
func(w io.Writer) {
json.NewEncoder(w).Encode(controller.Status()) //nolint:errcheck
},
),
)
}
return httpServer, nil
}