/
destroy.js
267 lines (246 loc) · 7.47 KB
/
destroy.js
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2015, Joyent, Inc.
*/
/*
* This is the workflow for destroying an SDC VM.
*/
var async = require('async');
var common = require('./job-common');
var restify = require('restify');
var VERSION = '7.1.0';
/*
* Sets up a CNAPI VM action request. Take a look at common.zoneAction. Here you
* can set parameters such as:
* - request endpoint (usually the VM endpoint)
* - jobid (so CNAPI can post status updates back to the job info object)
* - requestMethod
* - expects (if you want to check a specific running status of the machine)
*/
function setupRequest(job, cb) {
job.endpoint = '/servers/' +
job.params['server_uuid'] + '/vms/' +
job.params['vm_uuid'] + '?jobid=' + job.uuid;
job.currentVm = job.params.currentVm;
job.params.jobid = job.uuid;
job.requestMethod = 'del';
job.expects = 'destroyed';
job.addedToUfds = true;
job.action = 'destroy';
job.server_uuid = job.params['server_uuid'];
return cb(null, 'Request has been setup!');
}
/*
* Allow the VM to be marked as destroyed preemptively on VMAPI as soon as
* CNAPI accepts our destroy task
*/
function markVmAsDestroyed(job, cb) {
if (!job.currentVm) {
cb(null, 'Skipping task -- VM missing from job');
return;
}
if (!job.currentVm.docker) {
cb(null, 'Skipping task for non-Docker VMs');
return;
}
var vmapi = restify.createJsonClient({
url: vmapiUrl,
headers: { 'x-request-id': job.params['x-request-id'] }
});
var path = '/vms/' + job.currentVm.uuid;
job.currentVm.state = 'destroyed';
vmapi.put(path, job.currentVm, function (err, req, res, vm) {
if (err) {
cb(err, 'Could not mark VM as destroyed');
} else {
cb(null, 'VM marked as destroyed');
}
});
}
/*
* Removes a list of NICs from the machine.
*
* IMPORTANT: because workflow does not allow us to use 'common.removeNics' from
* within a task this code unfortunately must be duplicated instead. If you make
* changes here, make sure to apply the same changes in remove-nics.js.
*/
function removeNics(job, cb) {
var macs = [];
var napi;
if (!job.currentVm) {
cb(null, 'Skipping task -- VM missing from job');
return;
}
if (!job.currentVm.nics || !Array.isArray(job.currentVm.nics)) {
cb(null, 'Skipping task -- VM is missing .nics');
return;
}
job.currentVm.nics.forEach(function _appendMac(nic) {
if (nic.mac) {
macs.push(nic.mac);
}
});
job.log.warn({macs: macs, vm: job.currentVm.uuid},
'deleting NICs from NAPI');
/*
* Common code begins here.
*/
if (!macs || !Array.isArray(macs) || macs.length === 0) {
cb('No MAC addresses to remove');
return;
}
napi = new sdcClients.NAPI({
url: napiUrl,
headers: { 'x-request-id': job.params['x-request-id'] }
});
async.mapSeries(macs, function (mac, next) {
napi.deleteNic(mac, function (err) {
/*
* when net-agent catches this first, we'll fail to delete it but
* ignore since we want to be idempotent.
*
* Unfortunately sometimes NAPI returns 'body.code' and sometimes it
* only returns an error message.
*/
if (!err) {
/* no error: deleted! */
next();
} else if (err && err.body && err.body.code &&
err.body.code === 'ResourceNotFound') {
/*
* Sometimes when NIC doesn't exist, we get 404 and body.code
* with value 'ResourceNotFound'.
*/
next();
} else if (err && err.body && err.body.message &&
err.body.message.match(/^napi_nics::.*does not exist$/)) {
/*
* Other times it returns just a 500 and a message like:
*
* 'napi_nics::159123443660586 does not exist'
*/
next();
} else {
next(err);
}
});
}, function (err2) {
if (err2) {
cb(err2);
} else {
cb(null, 'NICs removed');
}
});
}
var workflow = module.exports = {
name: 'destroy-' + VERSION,
version: VERSION,
chain: [ {
name: 'common.validate_params',
timeout: 10,
retry: 1,
body: common.validateForZoneAction,
modules: {}
}, {
name: 'common.setup_request',
timeout: 10,
retry: 1,
body: setupRequest,
modules: {}
}, {
name: 'cnapi.acquire_vm_ticket',
timeout: 10,
retry: 1,
body: common.acquireVMTicket,
modules: { sdcClients: 'sdc-clients' }
}, {
name: 'cnapi.wait_on_vm_ticket',
timeout: 120,
retry: 1,
body: common.waitOnVMTicket,
modules: { sdcClients: 'sdc-clients' }
}, {
name: 'cnapi.destroy_vm',
timeout: 10,
retry: 1,
body: common.zoneAction,
modules: { restify: 'restify' }
}, {
name: 'vmapi.mark_vm_as_destroyed',
timeout: 10,
retry: 1,
body: markVmAsDestroyed,
modules: { restify: 'restify' }
}, {
name: 'cnapi.wait_task',
timeout: 120,
retry: 1,
body: common.waitTask,
modules: { sdcClients: 'sdc-clients' }
}, {
name: 'vmapi.refresh_vm',
modules: { restify: 'restify' },
body: common.refreshVm
}, {
name: 'napi.remove_nics',
timeout: 30,
retry: 1,
body: removeNics,
modules: { sdcClients: 'sdc-clients', async: 'async' }
}, {
name: 'fwapi.update',
timeout: 10,
retry: 1,
body: common.updateFwapi,
modules: { sdcClients: 'sdc-clients' }
}, {
name: 'cnapi.release_vm_ticket',
timeout: 60,
retry: 1,
body: common.releaseVMTicket,
modules: { sdcClients: 'sdc-clients' }
}],
timeout: 180,
onerror: [ {
name: 'vmapi.refresh_vm',
modules: { restify: 'restify' },
body: common.refreshVm
}, {
name: 'on_error.release_vm_ticket',
modules: { sdcClients: 'sdc-clients' },
body: function (job, cb) {
if (!job.ticket) {
return cb();
}
var cnapi = new sdcClients.CNAPI({
url: cnapiUrl,
headers: { 'x-request-id': job.params['x-request-id'] }
});
cnapi.waitlistTicketRelease(job.ticket.uuid, function (err) {
cb('Error executing job');
return;
});
}
}],
oncancel: [ {
name: 'on_cancel.release_vm_ticket',
modules: { sdcClients: 'sdc-clients' },
body: function (job, cb) {
if (!job.ticket) {
return cb();
}
var cnapi = new sdcClients.CNAPI({
url: cnapiUrl,
headers: { 'x-request-id': job.params['x-request-id'] }
});
cnapi.waitlistTicketRelease(job.ticket.uuid, function (err) {
cb();
return;
});
}
} ]
};