This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
register.go
108 lines (90 loc) · 2.31 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
106
107
108
package workerserver
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/api/accessor"
"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
acc := accessor.GetAccessor(r)
if !acc.IsSystem() {
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
}
if registration.CertsPath != nil && *registration.CertsPath == "" {
registration.CertsPath = nil
}
metric.WorkerContainers{
WorkerName: registration.Name,
Containers: registration.ActiveContainers,
Platform: registration.Platform,
}.Emit(s.logger)
metric.WorkerVolumes{
WorkerName: registration.Name,
Volumes: registration.ActiveVolumes,
Platform: registration.Platform,
}.Emit(s.logger)
if registration.Team != "" {
team, found, err := s.teamFactory.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)
}