-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
278 lines (236 loc) · 8.13 KB
/
http.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
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
package agent
// the HTTP interface
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/pprof"
"os"
"runtime"
runtime_pprof "runtime/pprof"
"github.com/bakins/logrus-middleware"
"github.com/bakins/net-http-recover"
"github.com/gorilla/context"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/mistifyio/kvite"
)
type (
// HTTPResponse is a wrapper for http.ResponseWriter which provides access
// to several convenience methods
HTTPResponse struct {
http.ResponseWriter
}
// HTTPError is an enhanced error struct for http error responses
HTTPError struct {
Message string `json:"message"`
Code int `json:"code"`
Stack []string `json:"stack"`
}
)
func (e *HTTPError) Error() string {
return fmt.Sprintf("http error code: %d, message: %s", e.Code, e.Message)
}
const ctxKey string = "agentContext"
var (
// ErrNotFound is the error for a resouce not found
ErrNotFound = errors.New("not found")
)
// AttachProfiler enables debug profiling exposed on http api endpoints
func AttachProfiler(router *mux.Router) {
router.HandleFunc("/debug/pprof/", pprof.Index)
for _, profile := range runtime_pprof.Profiles() {
router.Handle(fmt.Sprintf("/debug/pprof/%s", profile.Name()), pprof.Handler(profile.Name()))
}
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
}
// Run prepares and runs the http server
func Run(ctx *Context, address string) error {
r := mux.NewRouter()
r.StrictSlash(true)
AttachProfiler(r)
logrusMiddleware := logrusmiddleware.Middleware{
Name: "agent",
}
commonMiddleware := alice.New(
func(h http.Handler) http.Handler {
return logrusMiddleware.Handler(h, "")
},
handlers.CompressHandler,
func(h http.Handler) http.Handler {
return recovery.Handler(os.Stderr, h, true)
},
func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
context.Set(r, ctxKey, ctx)
h.ServeHTTP(w, r)
})
},
)
guestMiddleware := alice.New(
getGuestMiddleware,
guestRunnerMiddleware,
)
// General
r.HandleFunc("/metadata", getMetadata).Methods("GET")
r.HandleFunc("/metadata", setMetadata).Methods("PATCH")
r.HandleFunc("/images", listImages).Queries("type", "{type:[a-zA-Z]+}").Methods("GET")
r.HandleFunc("/images", listImages).Methods("GET")
r.HandleFunc("/images", fetchImage).Methods("POST")
r.HandleFunc("/images/{id}", getImage).Queries("type", "{type:[a-zA-Z]+}").Methods("GET")
r.HandleFunc("/images/{id}", getImage).Methods("GET")
r.HandleFunc("/images/{id}", deleteImage).Queries("type", "{type:[a-zA-Z]+}").Methods("DELETE")
r.HandleFunc("/images/{id}", deleteImage).Methods("DELETE")
r.HandleFunc("/jobs", getLatestJobs).Methods("GET").Queries("limit", "{limit:[0-9]+}").Methods("GET")
r.HandleFunc("/jobs", getLatestJobs).Methods("GET")
r.HandleFunc("/jobs/{jobID}", getJobStatus).Methods("GET")
// Guest Routes
r.HandleFunc("/guests", listGuests).Methods("GET")
r.HandleFunc("/guests", createGuest).Methods("POST") // Special setup
// Since mux requires all routes to start with "/", can't put this bare
// one in the guest subrouter cleanly
r.Handle("/guests/{id}", guestMiddleware.ThenFunc(getGuest)).Methods("GET")
// Specific guest, but don't need the guest middlewares, so register
// separately from the subrouter
r.HandleFunc("/guests/{id}/jobs", getLatestGuestJobs).Queries("limit", "{limit:[0-9]+}").Methods("GET")
r.HandleFunc("/guests/{id}/jobs", getLatestGuestJobs).Methods("GET")
r.HandleFunc("/guests/{id}/jobs/{jobID}", getJobStatus).Methods("GET")
// Guest subrouter
// Since middleware needs to be applied, a basic subrouter can't be used.
// Instead, create a new router with the prefix and then register that with
// the main router.
gr := mux.NewRouter().PathPrefix("/guests/{id}").Subrouter()
r.Handle("/guests/{id}/{path:.*}", guestMiddleware.Then(gr))
gr.HandleFunc("/metadata", getGuestMetadata).Methods("GET")
gr.HandleFunc("/metadata", setGuestMetadata).Methods("PATCH")
gr.HandleFunc("/metrics/cpu", getCPUMetrics).Methods("GET")
gr.HandleFunc("/metrics/disk", getDiskMetrics).Methods("GET")
gr.HandleFunc("/metrics/nic", getNicMetrics).Methods("GET")
for _, action := range []string{"shutdown", "reboot", "restart", "poweroff", "start", "suspend", "delete"} {
gr.HandleFunc(fmt.Sprintf("/%s", action), generateGuestAction(action)).Methods("POST")
}
for _, prefix := range []string{"", "/disks/{disk}"} {
gr.HandleFunc(fmt.Sprintf("%s/snapshots", prefix), listSnapshots).Methods("GET")
gr.HandleFunc(fmt.Sprintf("%s/snapshots", prefix), createSnapshot).Methods("POST")
gr.HandleFunc(fmt.Sprintf("%s/snapshots/{name}", prefix), getSnapshot).Methods("GET")
gr.HandleFunc(fmt.Sprintf("%s/snapshots/{name}", prefix), deleteSnapshot).Methods("DELETE")
gr.HandleFunc(fmt.Sprintf("%s/snapshots/{name}/rollback", prefix), rollbackSnapshot).Methods("POST")
gr.HandleFunc(fmt.Sprintf("%s/snapshots/{name}/download", prefix), downloadSnapshot).Methods("GET")
}
s := &http.Server{
Addr: address,
Handler: commonMiddleware.Then(r),
MaxHeaderBytes: 1 << 20,
}
return s.ListenAndServe()
}
func getMetadata(w http.ResponseWriter, r *http.Request) {
hr := HTTPResponse{w}
ctx := getContext(r)
metadata := make(map[string]string)
err := ctx.db.Transaction(func(tx *kvite.Tx) error {
b, err := tx.Bucket("hypervisor-metadata")
if err != nil {
return err
}
return b.ForEach(func(k string, v []byte) error {
metadata[string(k)] = string(v)
return nil
})
})
if err != nil {
hr.JSONError(http.StatusInternalServerError, err)
return
}
hr.JSON(http.StatusOK, metadata)
}
func setMetadata(w http.ResponseWriter, r *http.Request) {
hr := HTTPResponse{w}
ctx := getContext(r)
var metadata map[string]string
err := json.NewDecoder(r.Body).Decode(&metadata)
if err != nil {
hr.JSONError(http.StatusBadRequest, err)
return
}
err = ctx.db.Transaction(func(tx *kvite.Tx) error {
for key, value := range metadata {
b, err := tx.Bucket("hypervisor-metadata")
if err != nil {
return err
}
if value == "" {
if err := b.Delete(key); err != nil {
return err
}
} else {
if err := b.Put(key, []byte(value)); err != nil {
return err
}
}
}
return nil
})
if err != nil {
hr.JSONError(http.StatusInternalServerError, err)
return
}
getMetadata(w, r)
}
// JSON writes appropriate headers and JSON body to the http response
func (hr *HTTPResponse) JSON(code int, obj interface{}) {
hr.Header().Set("Content-Type", "application/json")
hr.WriteHeader(code)
encoder := json.NewEncoder(hr)
if err := encoder.Encode(obj); err != nil {
hr.JSONError(http.StatusInternalServerError, err)
}
}
// JSONError prepares an HTTPError with a stack trace and writes it with
// HTTPResponse.JSON
func (hr *HTTPResponse) JSONError(code int, err error) {
httpError := NewHTTPError(code, err)
// Remove this function call from the stack
httpError.Stack = httpError.Stack[1:]
hr.JSON(code, httpError)
}
// NewHTTPError prepares an HTTPError with a stack trace
func NewHTTPError(code int, err error) *HTTPError {
httpError := &HTTPError{
Message: err.Error(),
Code: code,
Stack: make([]string, 0, 4),
}
// Loop through the callers to build the stack. Skip the first one, which
// is this function and continue until there are no more callers
for i := 1; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Look up the function name (form of package.Name)
fnName := runtime.FuncForPC(pc).Name()
// Add the line to the stack array
httpError.Stack = append(httpError.Stack, fmt.Sprintf("%s:%d (%s)", file, line, fnName))
}
return httpError
}
// JSONMsg is a convenience method to write a JSON response with just a message
// string
func (hr *HTTPResponse) JSONMsg(code int, msg string) {
msgObj := map[string]string{
"message": msg,
}
hr.JSON(code, msgObj)
}
// getContext retrieves a Context value for a request
func getContext(r *http.Request) *Context {
if value := context.Get(r, ctxKey); value != nil {
return value.(*Context)
}
return nil
}