/
register.go
77 lines (64 loc) · 1.67 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/ovh/cds/engine/api/worker"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
)
// Workers need to register to main api so they can run actions
func register(cdsURI string, name string, uk string) error {
log.Notice("Registering [%s] at [%s]\n", name, cdsURI)
sdk.InitEndpoint(cdsURI)
path := "/worker"
requirements, err := sdk.GetRequirements()
if err != nil {
log.Warning("register> unable to get requirements")
}
binaryCapabilities := LoopPath(requirements)
in := worker.RegistrationForm{
Name: name,
UserKey: uk,
Model: model,
Hatchery: hatchery,
BinaryCapabilities: binaryCapabilities,
}
body, err := json.MarshalIndent(in, " ", " ")
if err != nil {
log.Notice("register: Cannot marshal body: %s\n", err)
return err
}
data, code, err := sdk.Request("POST", path, body)
if err != nil {
log.Notice("Cannot register worker: %s\n", err)
return err
}
if code == http.StatusUnauthorized {
// Nothing to do here, better exit
time.Sleep(10 * time.Second)
sdk.Exit("register> Got HTTP %d, exiting\n", code)
}
if code >= 300 {
return fmt.Errorf("HTTP %d", code)
}
var w sdk.Worker
json.Unmarshal(data, &w)
WorkerID = w.ID
sdk.Authorization(w.ID)
log.Notice("Registered: %s\n", data)
return nil
}
// LoopPath return the list of evailable command in path
func LoopPath(reqs []sdk.Requirement) []string {
binaries := []string{}
for _, req := range reqs {
if req.Type == sdk.BinaryRequirement {
if b, _ := checkBinaryRequirement(req); b {
binaries = append(binaries, req.Value)
}
}
}
return binaries
}