/
hatchery.go
346 lines (308 loc) · 8.66 KB
/
hatchery.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
package sdk
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"sync"
"time"
"go.opencensus.io/stats"
)
type AuthConsumerHatcherySigninRequest struct {
Token string `json:"token"`
Name string `json:"name"`
HatcheryType string `json:"type"`
HTTPURL string `json:"http_url"`
Config ServiceConfig `json:"config" db:"config" cli:"-" mapstructure:"config"`
PublicKey []byte `json:"public_key"`
Version string `json:"version"`
}
type AuthConsumerHatcherySigninResponse struct {
Uptodate bool `json:"up_to_date"`
APIURL string `json:"api_url"`
Token string `json:"token"`
Hatchery Hatchery `json:"hatchery"`
Region string `json:"region"`
}
type HatcheryStatus struct {
ID int64 `json:"id" db:"id" cli:"id,key"`
HatcheryID string `json:"hatchery_id" db:"hatchery_id" cli:"hatchery_id"`
SessionID string `json:"session_id" db:"session_id" cli:"session_id"`
Status MonitoringStatus `json:"monitoring_status" db:"monitoring_status"`
}
type Hatchery struct {
ID string `json:"id" db:"id" cli:"id,key"`
Name string `json:"name" db:"name" cli:"name"`
ModelType string `json:"model_type" db:"model_type" cli:"model_type"`
Config ServiceConfig `json:"config" db:"config"`
LastHeartbeat time.Time `json:"last_heartbeat,omitempty" db:"last_heartbeat" cli:"last_heartbeat"`
PublicKey []byte `json:"public_key" db:"public_key"`
HTTPURL string `json:"http_url" db:"http_url"`
}
type HatcheryGetResponse struct {
Hatchery
ConsumerExpiration string `json:"consumer_expiration,omitempty" db:"-" cli:"consumer_expiration,omitempty"`
Token string `json:"token,omitempty" db:"-" cli:"token,omitempty"`
}
type HatcheryMetrics struct {
Jobs *stats.Int64Measure
JobsWebsocket *stats.Int64Measure
JobsProcessed *stats.Int64Measure
SpawningWorkers *stats.Int64Measure
SpawnedWorkers *stats.Int64Measure
SpawningWorkersErrors *stats.Int64Measure
JobReceivedInQueuePollingWSv1 *stats.Int64Measure
JobReceivedInQueuePollingWSv2 *stats.Int64Measure
ChanV1JobAdd *stats.Int64Measure
ChanV2JobAdd *stats.Int64Measure
ChanWorkerStarterPop *stats.Int64Measure
PendingWorkers *stats.Int64Measure
RegisteringWorkers *stats.Int64Measure
CheckingWorkers *stats.Int64Measure
WaitingWorkers *stats.Int64Measure
BuildingWorkers *stats.Int64Measure
DisabledWorkers *stats.Int64Measure
}
type HatcheryPendingWorkerCreation struct {
mapSpawnJobRequest map[string]struct{}
mapSpawnJobRequestMutex *sync.Mutex
}
func (c *HatcheryPendingWorkerCreation) Init() {
c.mapSpawnJobRequest = make(map[string]struct{})
c.mapSpawnJobRequestMutex = new(sync.Mutex)
}
func (c *HatcheryPendingWorkerCreation) SetJobInPendingWorkerCreation(id string) int {
c.mapSpawnJobRequestMutex.Lock()
c.mapSpawnJobRequest[id] = struct{}{}
size := len(c.mapSpawnJobRequest)
c.mapSpawnJobRequestMutex.Unlock()
return size
}
func (c *HatcheryPendingWorkerCreation) NbJobInPendingWorkerCreation() int {
c.mapSpawnJobRequestMutex.Lock()
size := len(c.mapSpawnJobRequest)
c.mapSpawnJobRequestMutex.Unlock()
return size
}
func (c *HatcheryPendingWorkerCreation) RemoveJobFromPendingWorkerCreation(id string) {
c.mapSpawnJobRequestMutex.Lock()
delete(c.mapSpawnJobRequest, id)
c.mapSpawnJobRequestMutex.Unlock()
}
func (c *HatcheryPendingWorkerCreation) IsJobAlreadyPendingWorkerCreation(id string) bool {
c.mapSpawnJobRequestMutex.Lock()
_, has := c.mapSpawnJobRequest[id]
c.mapSpawnJobRequestMutex.Unlock()
return has
}
type HatcheryConfig map[string]interface{}
func (hc HatcheryConfig) Value() (driver.Value, error) {
j, err := json.Marshal(hc)
return j, WrapError(err, "cannot marshal HatcheryConfig")
}
func (hc *HatcheryConfig) Scan(src interface{}) error {
if src == nil {
return nil
}
source, ok := src.([]byte)
if !ok {
return WithStack(fmt.Errorf("type assertion .([]byte) failed (%T)", src))
}
return WrapError(JSONUnmarshal(source, hc), "cannot unmarshal HatcheryConfig")
}
type WorkerStarterWorkerModel struct {
ModelV1 *Model
// Worker model v2
ModelV2 *V2WorkerModel
PreCmd string
Cmd string
Shell string
PostCmd string
DockerSpec V2WorkerModelDockerSpec
OpenstackSpec V2WorkerModelOpenstackSpec
VSphereSpec V2WorkerModelVSphereSpec
Commit string
Flavor string
Memory int64
}
func (w WorkerStarterWorkerModel) GetName() string {
if w.ModelV1 != nil {
return w.ModelV1.Name
} else {
return w.ModelV2.Name
}
}
func (w WorkerStarterWorkerModel) GetFlavor(reqs RequirementList, defaultFlavor string) string {
switch {
case w.ModelV1 != nil:
if w.ModelV1.ModelVirtualMachine.Flavor != "" {
return w.ModelV1.ModelVirtualMachine.Flavor
}
case w.ModelV2 != nil:
if w.Flavor != "" {
return w.Flavor
}
for _, r := range reqs {
if r.Type == FlavorRequirement && r.Value != "" {
return r.Value
}
}
}
return defaultFlavor
}
func (w WorkerStarterWorkerModel) GetOpenstackImage() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelVirtualMachine.Image
case w.ModelV2 != nil:
return w.OpenstackSpec.Image
}
return ""
}
func (w WorkerStarterWorkerModel) GetDockerImage() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelDocker.Image
case w.ModelV2 != nil:
return w.DockerSpec.Image
}
return ""
}
func (w WorkerStarterWorkerModel) GetShell() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelDocker.Shell
case w.ModelV2 != nil:
return w.Shell
}
return ""
}
func (w WorkerStarterWorkerModel) GetCmd() string {
switch {
case w.ModelV1 != nil:
if w.ModelV1.ModelVirtualMachine.Cmd != "" {
return w.ModelV1.ModelVirtualMachine.Cmd
}
return w.ModelV1.ModelDocker.Cmd
case w.ModelV2 != nil:
return w.Cmd
}
return ""
}
func (w WorkerStarterWorkerModel) GetPreCmd() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelVirtualMachine.PreCmd
case w.ModelV2 != nil:
return w.PreCmd
}
return ""
}
func (w WorkerStarterWorkerModel) GetPostCmd() string {
switch {
case w.ModelV1 != nil:
if w.ModelV1.ModelVirtualMachine.PostCmd != "" {
return w.ModelV1.ModelVirtualMachine.PostCmd
}
case w.ModelV2 != nil:
return w.PostCmd
}
return ""
}
func (w WorkerStarterWorkerModel) GetDockerEnvs() map[string]string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelDocker.Envs
case w.ModelV2 != nil:
return w.DockerSpec.Envs
}
return nil
}
func (w WorkerStarterWorkerModel) GetDockerUsername() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelDocker.Username
case w.ModelV2 != nil:
return w.DockerSpec.Username
}
return ""
}
func (w WorkerStarterWorkerModel) IsPrivate() bool {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelDocker.Private
case w.ModelV2 != nil:
return false
}
return false
}
func (w WorkerStarterWorkerModel) GetDockerPassword() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelDocker.Password
case w.ModelV2 != nil:
return w.DockerSpec.Password
}
return ""
}
func (w WorkerStarterWorkerModel) GetPath() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.Path()
case w.ModelV2 != nil:
return w.ModelV2.Name
}
return ""
}
func (w WorkerStarterWorkerModel) GetFullPath() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.Group.Name + "/" + w.ModelV1.Name
case w.ModelV2 != nil:
return w.ModelV2.Name
}
return ""
}
func (w WorkerStarterWorkerModel) GetVSphereImage() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelVirtualMachine.Image
case w.ModelV2 != nil:
return w.VSphereSpec.Image
}
return ""
}
func (w WorkerStarterWorkerModel) GetVSphereUsername() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelVirtualMachine.User
case w.ModelV2 != nil:
return w.VSphereSpec.Username
}
return ""
}
func (w WorkerStarterWorkerModel) GetVSpherePassword() string {
switch {
case w.ModelV1 != nil:
return w.ModelV1.ModelVirtualMachine.Password
case w.ModelV2 != nil:
return w.VSphereSpec.Password
}
return ""
}
func (w WorkerStarterWorkerModel) GetLastModified() string {
switch {
case w.ModelV1 != nil:
return fmt.Sprintf("%d", w.ModelV1.UserLastModified.Unix())
case w.ModelV2 != nil:
return w.Commit
}
return ""
}
func IsJobIDForRegister(jobID string) bool {
if IsValidUUID(jobID) {
return false
}
jobIDint, _ := strconv.Atoi(jobID)
return jobIDint == 0 || jobIDint < 0
}