-
Notifications
You must be signed in to change notification settings - Fork 162
/
instance.go
297 lines (260 loc) · 8.26 KB
/
instance.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
package instance
import (
"fmt"
"time"
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
bidisk "github.com/cloudfoundry/bosh-cli/deployment/disk"
biinstancestate "github.com/cloudfoundry/bosh-cli/deployment/instance/state"
bideplmanifest "github.com/cloudfoundry/bosh-cli/deployment/manifest"
bisshtunnel "github.com/cloudfoundry/bosh-cli/deployment/sshtunnel"
bivm "github.com/cloudfoundry/bosh-cli/deployment/vm"
biinstallmanifest "github.com/cloudfoundry/bosh-cli/installation/manifest"
biui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Instance interface {
JobName() string
ID() int
Disks() ([]bidisk.Disk, error)
WaitUntilReady(biinstallmanifest.Registry, biui.Stage) error
UpdateDisks(bideplmanifest.Manifest, biui.Stage) ([]bidisk.Disk, error)
UpdateJobs(bideplmanifest.Manifest, biui.Stage) error
Delete(
pingTimeout time.Duration,
pingDelay time.Duration,
stage biui.Stage,
) error
}
type instance struct {
jobName string
id int
vm bivm.VM
vmManager bivm.Manager
sshTunnelFactory bisshtunnel.Factory
stateBuilder biinstancestate.Builder
logger boshlog.Logger
logTag string
}
func NewInstance(
jobName string,
id int,
vm bivm.VM,
vmManager bivm.Manager,
sshTunnelFactory bisshtunnel.Factory,
stateBuilder biinstancestate.Builder,
logger boshlog.Logger,
) Instance {
return &instance{
jobName: jobName,
id: id,
vm: vm,
vmManager: vmManager,
sshTunnelFactory: sshTunnelFactory,
stateBuilder: stateBuilder,
logger: logger,
logTag: "instance",
}
}
func (i *instance) JobName() string {
return i.jobName
}
func (i *instance) ID() int {
return i.id
}
func (i *instance) Disks() ([]bidisk.Disk, error) {
disks, err := i.vm.Disks()
if err != nil {
return disks, bosherr.WrapError(err, "Listing instance disks")
}
return disks, nil
}
func (i *instance) WaitUntilReady(
registryConfig biinstallmanifest.Registry,
stage biui.Stage,
) error {
stepName := fmt.Sprintf("Waiting for the agent on VM '%s' to be ready", i.vm.CID())
err := stage.Perform(stepName, func() error {
if !registryConfig.IsEmpty() {
sshReadyErrCh := make(chan error)
sshErrCh := make(chan error)
sshTunnelOptions := bisshtunnel.Options{
Host: registryConfig.SSHTunnel.Host,
Port: registryConfig.SSHTunnel.Port,
User: registryConfig.SSHTunnel.User,
Password: registryConfig.SSHTunnel.Password,
PrivateKey: registryConfig.SSHTunnel.PrivateKey,
LocalForwardPort: registryConfig.Port,
RemoteForwardPort: registryConfig.Port,
}
sshTunnel := i.sshTunnelFactory.NewSSHTunnel(sshTunnelOptions)
go sshTunnel.Start(sshReadyErrCh, sshErrCh)
defer func() {
if err := sshTunnel.Stop(); err != nil {
i.logger.Warn(i.logTag, "Failed to stop ssh tunnel: %s", err.Error())
}
}()
err := <-sshReadyErrCh
if err != nil {
return bosherr.WrapError(err, "Starting SSH tunnel")
}
}
return i.vm.WaitUntilReady(10*time.Minute, 500*time.Millisecond)
})
return err
}
func (i *instance) UpdateDisks(deploymentManifest bideplmanifest.Manifest, stage biui.Stage) ([]bidisk.Disk, error) {
diskPool, err := deploymentManifest.DiskPool(i.jobName)
if err != nil {
return []bidisk.Disk{}, bosherr.WrapError(err, "Getting disk pool")
}
disks, err := i.vm.UpdateDisks(diskPool, stage)
if err != nil {
return disks, bosherr.WrapError(err, "Updating disks")
}
return disks, nil
}
func (i *instance) UpdateJobs(
deploymentManifest bideplmanifest.Manifest,
stage biui.Stage,
) error {
initialAgentState, err := i.stateBuilder.BuildInitialState(i.jobName, i.id, deploymentManifest)
if err != nil {
return bosherr.WrapErrorf(err, "Building initial state for instance '%s/%d'", i.jobName, i.id)
}
// apply it to agent to force it to load networking details
err = i.vm.Apply(initialAgentState.ToApplySpec())
if err != nil {
return bosherr.WrapError(err, "Applying the initial agent state")
}
// now that the agent will tell us the address, get new state
resolvedAgentState, err := i.vm.GetState()
if err != nil {
return bosherr.WrapErrorf(err, "Getting state for instance '%s/%d'", i.jobName, i.id)
}
newAgentState, err := i.stateBuilder.Build(i.jobName, i.id, deploymentManifest, stage, resolvedAgentState)
if err != nil {
return bosherr.WrapErrorf(err, "Building state for instance '%s/%d'", i.jobName, i.id)
}
stepName := fmt.Sprintf("Updating instance '%s/%d'", i.jobName, i.id)
err = stage.Perform(stepName, func() error {
err = i.vm.Stop()
if err != nil {
return bosherr.WrapError(err, "Stopping the agent")
}
err = i.vm.Apply(newAgentState.ToApplySpec())
if err != nil {
return bosherr.WrapError(err, "Applying the agent state")
}
err = i.vm.RunScript("pre-start", map[string]interface{}{})
if err != nil {
return bosherr.WrapError(err, "Running the pre-start script")
}
err = i.vm.Start()
if err != nil {
return bosherr.WrapError(err, "Starting the agent")
}
return nil
})
if err != nil {
return err
}
err = i.waitUntilJobsAreRunning(deploymentManifest.Update.UpdateWatchTime, stage)
if err != nil {
return err
}
stepName = fmt.Sprintf("Running the post-start scripts '%s/%d'", i.jobName, i.id)
err = stage.Perform(stepName, func() error {
err = i.vm.RunScript("post-start", map[string]interface{}{})
if err != nil {
return bosherr.WrapError(err, "Running the post-start script")
}
return nil
})
return err
}
func (i *instance) Delete(
pingTimeout time.Duration,
pingDelay time.Duration,
stage biui.Stage,
) error {
vmExists, err := i.vm.Exists()
if err != nil {
return bosherr.WrapErrorf(err, "Checking existance of vm for instance '%s/%d'", i.jobName, i.id)
}
if vmExists {
if err = i.shutdown(pingTimeout, pingDelay, stage); err != nil {
return err
}
}
// non-existent VMs still need to be 'deleted' to clean up related resources owned by the CPI
stepName := fmt.Sprintf("Deleting VM '%s'", i.vm.CID())
return stage.Perform(stepName, func() error {
err := i.vm.Delete()
cloudErr, ok := err.(bicloud.Error)
if ok && cloudErr.Type() == bicloud.VMNotFoundError {
return biui.NewSkipStageError(cloudErr, "VM not found")
}
return err
})
}
func (i *instance) shutdown(
pingTimeout time.Duration,
pingDelay time.Duration,
stage biui.Stage,
) error {
stepName := fmt.Sprintf("Waiting for the agent on VM '%s'", i.vm.CID())
waitingForAgentErr := stage.Perform(stepName, func() error {
if err := i.vm.WaitUntilReady(pingTimeout, pingDelay); err != nil {
return bosherr.WrapError(err, "Agent unreachable")
}
return nil
})
if waitingForAgentErr != nil {
i.logger.Warn(i.logTag, "Gave up waiting for agent: %s", waitingForAgentErr.Error())
return nil
}
if err := i.stopJobs(stage); err != nil {
return err
}
if err := i.unmountDisks(stage); err != nil {
return err
}
return nil
}
func (i *instance) waitUntilJobsAreRunning(updateWatchTime bideplmanifest.WatchTime, stage biui.Stage) error {
start := time.Duration(updateWatchTime.Start) * time.Millisecond
end := time.Duration(updateWatchTime.End) * time.Millisecond
delayBetweenAttempts := 1 * time.Second
maxAttempts := int((end - start) / delayBetweenAttempts)
stepName := fmt.Sprintf("Waiting for instance '%s/%d' to be running", i.jobName, i.id)
return stage.Perform(stepName, func() error {
time.Sleep(start)
return i.vm.WaitToBeRunning(maxAttempts, delayBetweenAttempts)
})
}
func (i *instance) stopJobs(stage biui.Stage) error {
stepName := fmt.Sprintf("Stopping jobs on instance '%s/%d'", i.jobName, i.id)
return stage.Perform(stepName, func() error {
return i.vm.Stop()
})
}
func (i *instance) unmountDisks(stage biui.Stage) error {
disks, err := i.vm.Disks()
if err != nil {
return bosherr.WrapErrorf(err, "Getting VM '%s' disks", i.vm.CID())
}
for _, disk := range disks {
stepName := fmt.Sprintf("Unmounting disk '%s'", disk.CID())
err = stage.Perform(stepName, func() error {
if err := i.vm.UnmountDisk(disk); err != nil {
return bosherr.WrapErrorf(err, "Unmounting disk '%s' from VM '%s'", disk.CID(), i.vm.CID())
}
return nil
})
if err != nil {
return err
}
}
return nil
}