forked from digitalocean/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
101 lines (84 loc) · 2.1 KB
/
register.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
package workerserver
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/auth"
"github.com/concourse/atc/metric"
)
type IntMetric int
func (i IntMetric) String() string {
return strconv.Itoa(int(i))
}
func (s *Server) RegisterWorker(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("register-worker")
var registration atc.Worker
if !auth.IsSystem(r) {
w.WriteHeader(http.StatusForbidden)
return
}
err := json.NewDecoder(r.Body).Decode(®istration)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = registration.Validate()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, err.Error())
return
}
var ttl time.Duration
ttlStr := r.URL.Query().Get("ttl")
if len(ttlStr) > 0 {
ttl, err = time.ParseDuration(ttlStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "malformed ttl")
return
}
}
if registration.Name == "" {
registration.Name = registration.GardenAddr
}
metric.WorkerContainers{
WorkerName: registration.Name,
Containers: registration.ActiveContainers,
}.Emit(s.logger)
metric.WorkerVolumes{
WorkerName: registration.Name,
Volumes: registration.ActiveVolumes,
}.Emit(s.logger)
if registration.Team != "" {
team, found, err := s.dbTeamFactory.FindTeam(registration.Team)
if err != nil {
logger.Error("failed-to-get-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Error("team-not-found", errors.New("team-not-found"), lager.Data{"team-name": registration.Team})
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = team.SaveWorker(registration, ttl)
if err != nil {
logger.Error("failed-to-save-worker", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
} else {
_, err := s.dbWorkerFactory.SaveWorker(registration, ttl)
if err != nil {
logger.Error("failed-to-save-worker", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
}