-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.go
More file actions
366 lines (304 loc) · 12.9 KB
/
main.go
File metadata and controls
366 lines (304 loc) · 12.9 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
// @Title Waitron
// @Version 2
// @Description Endpoints for server provisioning
// @License BSD
// @LicenseUrl http://opensource.org/licenses/BSD-2-Clause
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"waitron/config"
"waitron/waitron"
"github.com/gorilla/handlers"
"github.com/julienschmidt/httprouter"
)
type result struct {
Token string `json:",omitempty"`
Error string `json:",omitempty"`
State string `json:",omitempty"`
}
// @Title definitionHandler
// @Description Return the waitron configuration details for a machine
// @Summary Return the waitron configuration details for a machine. Note that "build type" is technically not required, depending on your config.
// @Param hostname path string true "Hostname"
// @Param type path string true "Build Type"
// @Success 200 {object} string "Machine config in JSON format."
// @Failure 404 {object} string "Unable to find host definition for '<hostname>' '<build_type>' '<error>'"
// @Failure 500 {object} string "Bad machine data for '<hostname>' '<build_type>' '<error>'"
// @Router /definition/{hostname}/{type} [GET]
func definitionHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
hostname := ps.ByName("hostname")
btype := ps.ByName("type")
m, err := w.GetMergedMachine(hostname, "", btype, nil)
if err != nil || m == nil {
http.Error(response, fmt.Sprintf("Unable to find host definition for '%s' '%s'. %s", hostname, btype, err.Error()), 404)
return
}
result, err := json.Marshal(m)
if err != nil {
http.Error(response, fmt.Sprintf("Bad machine data for '%s' '%s'. %s", hostname, btype, err.Error()), 500)
return
}
fmt.Fprintf(response, string(result))
}
// @Title jobDefinitionHandler
// @Description Return details for the specified job token
// @Summary Return details for the specified job token
// @Param token path string true "Token"
// @Success 200 {object} string "Job details in JSON format."
// @Failure 404 {object} string "Job not found"
// @Router /job/{token} [GET]
func jobDefinitionHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
token := ps.ByName("token")
jb, err := w.GetJobBlob(token)
if err != nil {
http.Error(response, fmt.Sprintf("Unable to find valid job for %s. %s", token, err.Error()), 404)
return
}
response.Write(jb)
}
// @Title templateHandler
// @Description Render either the finish or the preseed template
// @Summary Render either the finish or the preseed template
// @Param hostname path string true "Hostname"
// @Param template path string true "The template to be rendered"
// @Param token path string true "Token"
// @Success 200 {object} string "Rendered template"
// @Failure 400 {object} string "Unable to render template"
// @Router /template/{template}/{hostname}/{token} [GET]
func templateHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
/* This eventually should to change to a PUT/POST because it causes changes. */
renderedTemplate, err := w.RenderStageTemplate(ps.ByName("token"), ps.ByName("template"))
if err != nil {
http.Error(response, "Unable to render template", 400)
return
}
fmt.Fprintf(response, renderedTemplate)
}
// @Title buildHandler
// @Description Put the server in build mode
// @Summary Put the server in build mode
// @Accept json
// @Produce json
// @Param hostname path string true "Hostname"
// @Param type path string true "Build Type"
// @Param {object} body string true "Machine definition if desired. Can be used to override nearly all properties of a compiled machine. See examples directory for machine definition."
// @Success 200 {object} string "{"State": "OK", "Token": <UUID of the build>}"
// @Failure 500 {object} string "Failed to set build mode on hostname"
// @Router /build/{hostname}/{type} [PUT]
func buildHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
hostname := ps.ByName("hostname")
btype := ps.ByName("type")
body := http.MaxBytesReader(response, request.Body, 1024*1024) // 1MB limit on posted machine definition. Even that seems insane.
machineDefinition, err := ioutil.ReadAll(body)
if err != nil {
http.Error(response, fmt.Sprintf("Failed to set build mode for %s - %s while reading request body: %s", hostname, btype, err.Error()), 500)
return
}
token, err := w.Build(hostname, btype, machineDefinition)
if err != nil {
http.Error(response, fmt.Sprintf("Failed to set build mode for %s - %s: %s", hostname, btype, err.Error()), 500)
return
}
result, _ := json.Marshal(&result{State: "OK", Token: token})
fmt.Fprintf(response, string(result))
}
// @Title doneHandler
// @Description Remove the server from build mode
// @Summary Remove the server from build mode
// @Param hostname path string true "Hostname"
// @Param token path string true "Token"
// @Success 200 {object} string "{"State": "OK"}"
// @Failure 500 {object} string "Failed to finish build mode"
// @Router /done/{hostname}/{token} [GET]
func doneHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
/* This eventually should to change to a PUT/POST because it causes changes. */
err := w.FinishBuild(ps.ByName("hostname"), ps.ByName("token"))
if err != nil {
http.Error(response, "Failed to finish build.", 500)
return
}
result, _ := json.Marshal(&result{State: "OK"})
fmt.Fprintf(response, string(result))
}
// @Title cancelHandler
// @Description Remove the server from build mode
// @Summary Remove the server from build mode
// @Accept json
// @Produce json
// @Param hostname path string true "Hostname"
// @Param token path string true "Token"
// @Param {object} body string true "Machine definition if desired. Can be used to override nearly all properties of a compiled machine. See examples directory for machine definition."
// @Success 200 {object} string "{"State": "OK"}"
// @Failure 500 {object} string "Failed to cancel build mode"
// @Router /cancel/{hostname}/{token} [PUT]
func cancelHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
/* This eventually should to change to a PUT/POST because it causes changes. */
err := w.CancelBuild(ps.ByName("hostname"), ps.ByName("token"))
if err != nil {
http.Error(response, "Failed to cancel build mode", 500)
return
}
result, _ := json.Marshal(&result{State: "OK"})
fmt.Fprintf(response, string(result))
}
// @Title hostStatus
// @Description Build status of the server
// @Summary Build status of the server
// @Param hostname path string true "Hostname"
// @Success 200 {object} string "The status: (installing or installed)"
// @Failure 404 {object} string "Failed to find active job for host"
// @Router /status/{hostname} [GET]
func hostStatus(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
hostname := ps.ByName("hostname")
s, err := w.GetMachineStatus(hostname)
if err != nil {
http.Error(response, fmt.Sprintf("Failed to find active job for %s. Try search by job ID. %s", hostname, err.Error()), 404)
return
}
fmt.Fprintf(response, s)
}
// @Title status
// @Description Dictionary with jobs and status
// @Summary Dictionary with jobs and status
// @Success 200 {object} string "Dictionary with jobs and status"
// @Success 500 {object} string "The error encountered"
// @Router /status [GET]
func status(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
result, err := w.GetJobsHistoryBlob()
if err != nil {
http.Error(response, err.Error(), 500)
return
}
response.Write(result)
}
// @Title cleanHistory
// @Description Clear all completed jobs from the in-memory history of Waitron
// @Summary Clear all completed jobs from the in-memory history of Waitron
// @Success 200 {object} string "{"State": "OK"}"
// @Failure 500 {object} string "Failed to clean history"
// @Router /cleanhistory [PUT]
func cleanHistory(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
err := w.CleanHistory()
if err != nil {
http.Error(response, "Failed to clean history", 500)
return
}
result, _ := json.Marshal(&result{State: "OK"})
response.Write(result)
}
// @Title pixieHandler
// @Description Dictionary with kernel, intrd(s) and commandline for pixiecore
// @Summary Dictionary with kernel, intrd(s) and commandline for pixiecore
// @Param macaddr path string true "MacAddress"
// @Success 200 {object} string "Dictionary with kernel, intrd(s) and commandline for pixiecore"
// @Failure 500 {object} string "failed to get pxe config: <error>"
// @Router /v1/boot/{macaddr} [GET]
func pixieHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
pxeconfig, err := w.GetPxeConfig(ps.ByName("macaddr"))
if err != nil {
http.Error(response, "failed to get pxe config: "+err.Error(), 500)
return
}
result, _ := json.Marshal(pxeconfig)
response.Write(result)
}
// @Title healthHandler
// @Description Check that Waitron is running
// @Summary Check that Waitron is running
// @Success 200 {object} string "{"State": "OK"}"
// @Router /health [GET]
func healthHandler(response http.ResponseWriter, request *http.Request, ps httprouter.Params, w *waitron.Waitron) {
result, _ := json.Marshal(&result{State: "OK"})
fmt.Fprintf(response, string(result))
}
func main() {
configPath := flag.String("config", "", "Path to config file.")
address := flag.String("address", "", "Address to listen for requests.")
port := flag.String("port", "9090", "Port to listen for requests.")
flag.Parse()
configFile := *configPath
if configFile == "" {
if configFile = os.Getenv("CONFIG_FILE"); configFile == "" {
log.Fatal("environment variables CONFIG_FILE must be set or use --config")
}
}
configuration, err := config.LoadConfig(configFile)
if err != nil {
log.Fatal(err)
}
w := waitron.New(configuration)
if err := w.Init(); err != nil {
log.Fatal(err)
}
r := httprouter.New()
r.PUT("/build/:hostname",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
buildHandler(response, request, ps, w)
})
r.PUT("/build/:hostname/:type",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
buildHandler(response, request, ps, w)
})
r.GET("/status/:hostname",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
hostStatus(response, request, ps, w)
})
r.GET("/status",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
status(response, request, ps, w)
})
r.PUT("/cleanhistory",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
cleanHistory(response, request, ps, w)
})
r.GET("/definition/:hostname",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
definitionHandler(response, request, ps, w)
})
r.GET("/definition/:hostname/:type",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
definitionHandler(response, request, ps, w)
})
r.GET("/job/:token",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
jobDefinitionHandler(response, request, ps, w)
})
r.GET("/done/:hostname/:token",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
doneHandler(response, request, ps, w)
})
r.PUT("/cancel/:hostname/:token",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
cancelHandler(response, request, ps, w)
})
r.GET("/template/:template/:hostname/:token",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
templateHandler(response, request, ps, w)
})
r.GET("/v1/boot/:macaddr",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
pixieHandler(response, request, ps, w)
})
r.GET("/health",
func(response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
healthHandler(response, request, ps, w)
})
if configuration.StaticFilesPath != "" {
fs := http.FileServer(http.Dir(configuration.StaticFilesPath))
r.Handler("GET", "/files/:filename", http.StripPrefix("/files/", fs))
log.Println("Serving static files from " + configuration.StaticFilesPath)
}
if err := w.Run(); err != nil {
log.Fatal(fmt.Sprintf("waitron instance failed to run: %v", err))
}
log.Println("Starting Server on " + *address + ":" + *port)
log.Fatal(http.ListenAndServe(*address+":"+*port, handlers.LoggingHandler(w.GetLogger(), r)))
// This is practically a lie since nothing is properly catching signals AFAIK, but maybe in
w.Stop()
}