/
daemon.go
371 lines (321 loc) · 9.33 KB
/
daemon.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
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
367
368
369
370
371
package daemon
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/Webstrates/golem-herder/container"
"github.com/Webstrates/golem-herder/metering"
jwt "github.com/dgrijalva/jwt-go"
docker "github.com/fsouza/go-dockerclient"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
)
var (
// upgrader upgrades HTTP 1.1 connection to WebSocket
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(*http.Request) bool { return true }, // allow all origins
}
)
// Options contains configuration options for the daemon spawn.
type Options struct {
Meter *metering.Meter
Restart bool
Ports []int
Files map[string][]byte
StdOut chan []byte
StdErr chan []byte
Done chan bool
}
// Info is information about a running deamon.
type Info struct {
Name string
Address string
Ports map[int]int
}
// Spawn a daemon with the given options.
func Spawn(token *jwt.Token, name, image string, options Options) (*Info, error) {
// Construct a new unique (for this token) id from name and token id
// - we'll assume that token has already been validated
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return nil, fmt.Errorf("Could not read from token claims")
}
// Change the unique name generation if you want to e.g. restrict to one container of each kind
uname := fmt.Sprintf("%s-%v", name, claims["jti"])
// Get random outside ports
ports := map[int]int{}
invertedPorts := map[int]int{}
for _, insidePort := range options.Ports {
outsidePort := container.GetAvailableHostPort()
ports[outsidePort] = insidePort
invertedPorts[insidePort] = outsidePort
}
// Labels for container
labels := map[string]string{
"subject": claims["sub"].(string),
"token": token.Raw,
"tokenid": fmt.Sprintf("%v", claims["jti"]),
}
done := make(chan bool, 5) // does not need to be synchronized
c, err := container.RunDaemonized(uname, image, "latest", ports, options.Files, labels, options.Restart, options.StdOut, options.StdErr, done)
if err != nil {
return nil, err
}
go func() {
// Let someone else now that we're done
defer func() {
options.Done <- true
}()
for {
t0 := time.Now().UnixNano()
select {
case <-time.After(time.Second):
ms := (time.Now().UnixNano() - t0) / 1e9
if err := options.Meter.Record(int(ms)); err != nil {
log.WithError(err).Warn("Could not record time spent - kill and exit")
if err := container.Kill(container.WithName(uname), false, false); err != nil {
log.WithError(err).Warn("Error killing container")
}
return
}
case <-done:
return
}
}
}()
return &Info{Address: c.NetworkSettings.Networks["bridge"].IPAddress, Name: uname, Ports: invertedPorts}, nil
}
// Attach will attach to an already running daemon and forward stdout/err and allow for stdin
func Attach(token *jwt.Token, name string, in <-chan []byte, out chan<- []byte) error {
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return fmt.Errorf("Could not extract claims from token")
}
predicate := container.And(container.WithName(name), container.WithLabel("subject", claims["sub"].(string)))
cs, err := container.List(nil, predicate, true)
if err != nil {
return err
}
if len(cs) == 0 {
return fmt.Errorf("Could not find container with name: %s", name)
}
c := cs[0]
// io, io, io
return container.Attach(c, out, out, in)
}
// AttachHandler handles attach requests
func AttachHandler(w http.ResponseWriter, r *http.Request, token *jwt.Token) {
vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
log.Warn("No name given")
http.Error(w, "No name given", 404)
return
}
log.WithField("name", name).Info("Attaching")
// Upgrade HTTP connection to WebSocket
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.WithError(err).Panic("Error upgrading connection")
return
}
// Connect websocket and channel
in := make(chan []byte)
out := make(chan []byte)
// -- from websocket -> stdin
go func() {
for {
_, data, err := conn.ReadMessage()
if err != nil {
log.WithError(err).WithField("container", name).Warn("Error reading from stdin-websocket")
close(in)
return
}
in <- data
}
}()
// -- from stdout/err -> websocket
go func() {
for line := range out {
err := conn.WriteMessage(websocket.TextMessage, line)
if err != nil {
log.WithError(err).WithField("container", name).Warn("Error writing to stdout-websocket")
return
}
}
}()
if err := Attach(token, name, in, out); err != nil {
log.WithError(err).Warn("Could not attach to %s", name)
conn.Close()
}
}
// List the daemons running on this token.
func List(token *jwt.Token) ([]docker.APIContainers, error) {
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return nil, fmt.Errorf("Could not extract claims from token")
}
return container.List(nil, container.WithLabel("subject", claims["sub"].(string)), true)
}
// SpawnHandler handles spawn requests
func SpawnHandler(w http.ResponseWriter, r *http.Request, token *jwt.Token) {
// Read name, image
name := r.FormValue("name")
image := r.FormValue("image")
ps := r.FormValue("ports")
var ports []int
if err := json.Unmarshal([]byte(ps), &ports); err != nil {
http.Error(w, "Could not unmarshal ports - "+err.Error(), 400)
return
}
// Consider rest of form values as files
files := map[string][]byte{}
for key, values := range r.Form {
if key != "name" && key != "image" && key != "ports" && key != "token" && len(values) > 0 {
files[key] = []byte(values[0])
}
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
http.Error(w, "Could extract claims from token", 500)
return
}
crd, ok := claims["crd"].(float64)
if !ok {
http.Error(w, "Could not extract \"crd\" (credits) from token", 500)
return
}
exp, ok := claims["exp"].(float64)
if !ok {
http.Error(w, "Could not extract \"exp\" (expiration) from token", 500)
return
}
// Construct meter - one meter pr sub(ject) aka email
subject := claims["sub"].(string)
tokenID := claims["jti"].(string)
credits := int(crd)
expiration := int(exp)
m, err := metering.NewMeter(subject, tokenID, expiration, credits)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// Check if meter has resources before spawning
if credits, err := m.Credits(); err != nil || credits <= 0 {
http.Error(w, "Not even running on fumes", 402 /* Payment required */)
return
}
done := make(chan bool)
go func() {
<-done
log.WithField("name", name).Info("Daemon is now done")
}()
options := Options{
Meter: m,
Restart: true,
Ports: ports,
Files: files,
StdOut: nil,
StdErr: nil,
Done: done,
}
// TODO support content in similar fashion to lambdaed minions
info, err := Spawn(token, name, image, options)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
s, err := json.Marshal(info)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Write(s)
}
// Kill a container with the given name iff it is owned by the owner of the token
func Kill(name string, wipe bool, token *jwt.Token) error {
// Check of subject label is the same as in the token
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return fmt.Errorf("Could extract claims from token")
}
subject := claims["sub"].(string)
containers, err := container.List(nil, container.And(container.WithName(name), container.WithLabel("subject", subject)), false)
if err != nil {
return err
}
if len(containers) != 1 {
return fmt.Errorf("Could not find container to kill")
}
return container.Kill(container.WithID(containers[0].ID), wipe, wipe)
}
// ListHandler handles list requests
func ListHandler(w http.ResponseWriter, r *http.Request, token *jwt.Token) {
containers, err := List(token)
if err != nil {
http.Error(w, err.Error(), 500)
}
s, err := json.Marshal(containers)
if err != nil {
http.Error(w, err.Error(), 500)
}
w.Write(s)
}
// KillHandler handles kill requests
func KillHandler(w http.ResponseWriter, r *http.Request, token *jwt.Token) {
vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
http.Error(w, "No name given", 404)
return
}
err := Kill(name, r.URL.Query().Get("wipe") == "true", token)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
// ProxyHandler proxies requests to daemons
func ProxyHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
log.Warn("No name given")
http.Error(w, "No name given", 404)
return
}
predicate := container.And(container.WithName(name), container.WithState("running"))
containers, err := container.List(nil, predicate, true)
if err != nil {
http.Error(w, "Failed to look for container", 500)
return
}
if len(containers) != 1 {
http.Error(w, "No container found", 404)
return
}
container := containers[0]
if len(container.Ports) < 1 {
http.Error(w, "No exposed ports found", 500)
return
}
port := container.Ports[0].PublicPort
log.WithField("status", container.Status).
WithField("ports", container.Ports).
WithField("networks", container.Networks).
Info("Found container")
target, err := url.Parse(fmt.Sprintf("http://localhost:%d", port))
if err != nil {
http.Error(w, "Invalid proxy endpoint", 404)
return
}
log.WithField("container", name).Info("Proxy request made")
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.ServeHTTP(w, r)
}