forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
105 lines (88 loc) · 2.41 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
102
103
104
105
package workerserver
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/db"
"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
isSystem, present := r.Context().Value("system").(bool)
if !present || !isSystem {
w.WriteHeader(http.StatusForbidden)
return
}
err := json.NewDecoder(r.Body).Decode(®istration)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var teamID int
if registration.Team != "" {
team, found, err := s.teamDBFactory.GetTeamDB(registration.Team).GetTeam()
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
}
teamID = team.ID
}
if len(registration.GardenAddr) == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "missing address")
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)
_, err = s.db.SaveWorker(db.WorkerInfo{
GardenAddr: registration.GardenAddr,
BaggageclaimURL: registration.BaggageclaimURL,
HTTPProxyURL: registration.HTTPProxyURL,
HTTPSProxyURL: registration.HTTPSProxyURL,
NoProxy: registration.NoProxy,
ActiveContainers: registration.ActiveContainers,
ResourceTypes: registration.ResourceTypes,
Platform: registration.Platform,
Tags: registration.Tags,
TeamID: teamID,
Name: registration.Name,
StartTime: registration.StartTime,
}, ttl)
if err != nil {
logger.Error("failed-to-save-worker", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}