-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_reboot.go
60 lines (51 loc) · 1.52 KB
/
pre_reboot.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
package server
import (
"context"
"net/http"
"github.com/lucab/exp-locksmith2/internal/lock"
"github.com/sirupsen/logrus"
)
const (
// PreRebootEndpoint is the endpoint for requesting a semaphore lock.
PreRebootEndpoint = "/v1/pre-reboot"
)
// PreReboot is the handler for the `/v1/pre-reboot` endpoint.
func (sc *ServerConfig) PreReboot() http.Handler {
handler := func(w http.ResponseWriter, req *http.Request) {
logrus.Debug("got pre-reboot request")
if sc == nil {
http.Error(w, errNilServerConfig.Error(), 500)
return
}
nodeIdentity, err := validateIdentity(req)
if err != nil {
logrus.Errorln("failed to validate client identity: ", err)
http.Error(w, err.Error(), 400)
return
}
logrus.WithFields(logrus.Fields{
"group": nodeIdentity.Group,
"UUID": nodeIdentity.UUID,
}).Debug("processing client pre-reboot request")
ctx, cancel := context.WithTimeout(context.Background(), sc.LockTimeout)
defer cancel()
lockManager, err := lock.NewManager(ctx, sc.EtcdURLs, nodeIdentity.Group, sc.SemaphoreSlots)
if err != nil {
logrus.Errorln("failed to initialize semaphore manager: ", err)
http.Error(w, err.Error(), 500)
return
}
defer lockManager.Close()
err = lockManager.RecursiveLock(ctx, nodeIdentity.UUID)
if err != nil {
logrus.Errorln(err)
http.Error(w, err.Error(), 500)
return
}
logrus.WithFields(logrus.Fields{
"group": nodeIdentity.Group,
"UUID": nodeIdentity.UUID,
}).Debug("green-flag to pre-reboot request")
}
return http.HandlerFunc(handler)
}