forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drain.go
149 lines (120 loc) · 3.74 KB
/
drain.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
package action
import (
"errors"
boshas "github.com/cloudfoundry/bosh-agent/agent/applier/applyspec"
boshscript "github.com/cloudfoundry/bosh-agent/agent/script"
boshdrain "github.com/cloudfoundry/bosh-agent/agent/script/drain"
boshjobsuper "github.com/cloudfoundry/bosh-agent/jobsupervisor"
boshnotif "github.com/cloudfoundry/bosh-agent/notification"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type DrainAction struct {
jobScriptProvider boshscript.JobScriptProvider
notifier boshnotif.Notifier
specService boshas.V1Service
jobSupervisor boshjobsuper.JobSupervisor
logTag string
logger boshlog.Logger
cancelCh chan struct{}
}
type DrainType string
const (
DrainTypeUpdate DrainType = "update"
DrainTypeStatus DrainType = "status"
DrainTypeShutdown DrainType = "shutdown"
)
func NewDrain(
notifier boshnotif.Notifier,
specService boshas.V1Service,
jobScriptProvider boshscript.JobScriptProvider,
jobSupervisor boshjobsuper.JobSupervisor,
logger boshlog.Logger,
) DrainAction {
return DrainAction{
notifier: notifier,
specService: specService,
jobScriptProvider: jobScriptProvider,
jobSupervisor: jobSupervisor,
logTag: "Drain Action",
logger: logger,
cancelCh: make(chan struct{}, 1),
}
}
func (a DrainAction) IsAsynchronous(_ ProtocolVersion) bool {
return true
}
func (a DrainAction) IsPersistent() bool {
return false
}
func (a DrainAction) IsLoggable() bool {
return true
}
func (a DrainAction) Run(drainType DrainType, newSpecs ...boshas.V1ApplySpec) (int, error) {
currentSpec, err := a.specService.Get()
if err != nil {
return 0, bosherr.WrapError(err, "Getting current spec")
}
params, err := a.determineParams(drainType, currentSpec, newSpecs)
if err != nil {
return 0, err
}
a.logger.Debug(a.logTag, "Unmonitoring")
err = a.jobSupervisor.Unmonitor()
if err != nil {
return 0, bosherr.WrapError(err, "Unmonitoring services")
}
//TODO write health.json
var scripts []boshscript.Script
for _, job := range currentSpec.Jobs() {
script := a.jobScriptProvider.NewDrainScript(job.BundleName(), params)
scripts = append(scripts, script)
}
script := a.jobScriptProvider.NewParallelScript("drain", scripts)
resultsCh := make(chan error, 1)
go func() { resultsCh <- script.Run() }()
select {
case result := <-resultsCh:
a.logger.Debug(a.logTag, "Got a result")
return 0, result
case <-a.cancelCh:
a.logger.Debug(a.logTag, "Got a cancel request")
return 0, script.Cancel()
}
}
func (a DrainAction) determineParams(drainType DrainType, currentSpec boshas.V1ApplySpec, newSpecs []boshas.V1ApplySpec) (boshdrain.ScriptParams, error) {
var newSpec *boshas.V1ApplySpec
var params boshdrain.ScriptParams
if len(newSpecs) > 0 {
newSpec = &newSpecs[0]
}
switch drainType {
case DrainTypeStatus:
// Status was used in the past when dynamic drain was implemented in the Director.
// Now that we implement it in the agent, we should never get a call for this type.
return params, bosherr.Error("Unexpected call with drain type 'status'")
case DrainTypeUpdate:
if newSpec == nil {
return params, bosherr.Error("Drain update requires new spec")
}
params = boshdrain.NewUpdateParams(currentSpec, *newSpec)
case DrainTypeShutdown:
err := a.notifier.NotifyShutdown()
if err != nil {
return params, bosherr.WrapError(err, "Notifying shutdown")
}
params = boshdrain.NewShutdownParams(currentSpec, newSpec)
}
return params, nil
}
func (a DrainAction) Resume() (interface{}, error) {
return nil, errors.New("not supported")
}
func (a DrainAction) Cancel() error {
a.logger.Debug(a.logTag, "Cancelling drain action")
select {
case a.cancelCh <- struct{}{}:
default:
}
return nil
}