-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathcode.js
More file actions
453 lines (420 loc) · 18.8 KB
/
code.js
File metadata and controls
453 lines (420 loc) · 18.8 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
var fs = require('fs');
var request = require('request');
var auth = require('./auth');
var console = require('./log');
var ocapi = require('./ocapi');
var webdav = require('./webdav');
// enable request debugging
if ( process.env.DEBUG ) {
require('request-debug')(request);
}
/**
* Activates a custom code version on an instance.
*
* @param {String} instance instance to activate the code versions on
* @param {String} version the code version to activate
* @param {String} token oauth token
* @param {Function} callback callback function to execute
*/
function activateVersion(instance, version, token, callback) {
var endpoint = '/s/-/dw/data/' + ocapi.getOcapiVersion() + '/code_versions/' + version;
// build the request options
var options = ocapi.getOptions(instance, endpoint, token, 'PATCH');
// the patch body
options['body'] = { active : true };
// just do the request and pass the callback
request(options, callback);
}
/**
* Retrieves a list of all code versions from an instance.
*
* @param {String} instance instance to retrieve code versions from
* @param {String} token oauth token
* @param {Function} callback callback function to execute
*/
function getVersions(instance, token, callback) {
var endpoint = '/s/-/dw/data/' + ocapi.getOcapiVersion() + '/code_versions'
// build the request options
var options = ocapi.getOptions(instance, endpoint, token);
// just do the request and pass the callback
request.get(options, callback);
}
/**
* Renders a list of code versions in human readable way and writes output to the console.
*
* @param {Array} code_versions List of code versions to render
*/
function renderCodeVersions(code_versions) {
// render totals
console.info("Number of code versions: %s", code_versions.total);
// render some details for each
if (code_versions.total > 0) {
var data = [['ID','Activation Time','Active','Compatibility Mode','Last Modification Time','Total Size']];
for (var c of code_versions.data) {
data.push([c.id,c.activation_time,c.active,c.compatibility_mode,c.last_modification_time,c.total_size]);
}
console.table(data);
}
}
/**
* Deploys a new code version to an instance.
*
* @param {String} instance The instance to deploy code to
* @param {String} archive The path to the ZIP archive to deploy
* @param {String} token oauth token
* @param {Object} options The options parameter can contains client certificate buffer and related passphrase in case of two factor authentication
* @param {Function} callback Callback function to execute, with the error as parameter passed
*/
function deployCode(instance, archive, token, options, callback) {
// check parameters
if (typeof(instance) !== 'string') {
throw new TypeError('Parameter instance is missing or not of type String');
}
if (typeof(archive) !== 'string') {
throw new TypeError('Parameter archive is missing or not of type String');
}
if (typeof(token) !== 'string') {
throw new TypeError('Parameter token is missing or not of type String');
}
if (typeof(options) !== 'object') {
options = {};
}
if (typeof(callback) !== 'function') {
throw new TypeError('Parameter callback is missing or not of type Function');
}
var file = archive;
// check if file exists locally
if (!fs.existsSync(file)) {
callback(new Error(`File "${file}" does not exist`));
return;
} else {
var stat = fs.statSync(file);
if (!stat.isFile()) {
callback(new Error(`File "${file}" does not exist or is not a file`));
return;
}
}
// by default we do not ignore local file paths for code upload
// this will acknowledge any dirs and sub dirs and will retain them
// when deploying code onto the server (e.g. it will create those dirs
// and sub dirs if they do not exist)
var ignoreLocalFilePath = false;
// however, if we upload a zipped custom code file, we ignore the local
// path forcing the zip file to be uploaded to the webdav code repo as is
if (require('path').extname(file) === '.zip') {
ignoreLocalFilePath = true;
}
// initiate the post request first...
webdav.postFile(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, function (err, res, body) {
webdav.checkExist(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, function (err, res, body) {
if(err) {
callback(new Error('Code upload failed'));
return;
} else if(res.statusCode !== 200) {
callback(new Error('Code upload failed - statusCode: ' + res.statusCode));
return;
}
ocapi.ensureValidToken(err, res, function(err, res) {
// note, server respond with a 401 (Authorization required) in case the WebDAV Client permission is not set
if (res && res.statusCode >= 400) {
callback(new Error(`Deploy code ${file} failed (upload step): `
+ `${res.statusCode} (${res.statusMessage})`));
return;
} else if (err) {
callback(new Error(`Deploy code ${file} failed (upload step): ${err}`));
return;
}
// ...and unzip the archive afterwards
webdav.unzip(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options,
function (err, res, body) {
// note, server respond with a 401 (Authorization required) in case the WebDAV Client permission is not set
if (res && res.statusCode >= 400) {
callback(new Error(`Deploy code ${file} failed (unzip step): `
+ `${res.statusCode} (${res.statusMessage})`));
return;
} else if (err) {
callback(new Error(`Deploy code ${file} failed (unzip step): ${err}`));
return;
}
// this assumes that archive file carries the same name as the packaged code version
var newVersion = require('path').basename(file);
if (ignoreLocalFilePath) {
newVersion = require('path').basename(file, '.zip');
}
// If the code is successfully deployed, we need to remove the uploaded ZIP file
webdav.deleteFile(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options,
function(err, res, body) {
if (err) {
callback(new Error(`Delete ZIP file ${file} after deployment `
+ `failed (deleteFile step): ${err}`));
return;
} else {
if (res && res.statusCode === 204) {
callback(undefined, newVersion);
return;
} else {
callback(new Error(`Delete ZIP file ${file} after deployment failed
(deleteFile step): ${res.statusCode} (${res.statusMessage})`));
}
}
});
});
});
}, function() {
deployCode(instance, archive, token, options, callback);
});
});
}
/**
* Deletes an existing code version
*
* @param {String} instance the instance to delete the code version from
* @param {String} version the code version to delete
* @param {Function} callback the callback to execute, the error is available as argument to the callback function
*/
function deleteCodeVersion(instance, version, callback) {
// build the request options
var endpoint = '/s/-/dw/data/' + ocapi.getOcapiVersion() + '/code_versions/' + version;
// build the request options
var options = ocapi.getOptions(instance, endpoint, auth.getToken(), 'DELETE');
// do the request
request(options, function (err, res, body) {
var errback = ocapi.captureCommonErrors(err, res);
if ( errback ) {
callback(errback, []);
return;
} else if ( err ) {
callback(new Error(`Deleting code version ${version} failed: ${err}`));
return;
} else if (res.statusCode >= 400) {
callback(new Error(`Deleting code version ${version} failed: ${res.statusCode}`));
return;
}
// do the callback without error
callback(undefined);
});
}
module.exports.cli = {
/**
* Returns a list of all code version on the instance.
*
* @param {String} instance the instance to retrieve the list of code versions from
* @param {Boolean} asJson whether to format the output as json, default is false
* @param {String} sortBy the field to sort code versions by
*/
list : function (instance, asJson, sortBy) {
getVersions(instance, auth.getToken(), function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && res.statusCode == 200) {
// apply sorting
if (sortBy) {
res.body.data = require('./json').sort(res.body.data, sortBy);
}
if (asJson) {
console.json(res.body);
return;
}
renderCodeVersions(res.body);
return;
}
// in case of errors
var result = { error : 'Cannot read code versions', fault : res.body.fault };
if (asJson) {
console.json(result);
return;
}
console.error(result['error']);
console.debug(result['fault']);
}, function() {
list(instance, asJson, sortby);
});
});
},
/**
* Deploys a new code version to an instance with optional code activation.
*
* @param {String} instance The instance to deploy code to
* @param {String} archive The path to the ZIP archive to deploy
* @param {Object} options The options parameter can contains client certificate buffer and related passphrase in case of two factor authentication
* @param {Boolean} activate Whether to activate the uploaded code version or not, false by default
*/
deploy : function (instance, archive, options, activate) {
deployCode(instance, archive, auth.getToken(), options, function(err, newVersion) {
if (err) {
console.error(err.message);
} else if (!activate) {
console.info('Code archive %s successfully deployed to %s. You may activate the code by running ' +
'`sfcc-ci code:activate %s -i %s`.', archive, instance, newVersion, instance);
} else {
console.info('Code archive %s successfully deployed to %s.', archive, instance);
}
// optionally activate
if (!err && activate) {
// check for "cert." version of host name and patch if needed for subsequent code activation step
if (instance.indexOf('cert.', 0) === 0) {
instance = instance.substring(5);
console.debug(`Cert host name detected. Using ${instance} for code activation.`);
}
module.exports.cli.activate(instance, newVersion);
}
});
},
/**
* Activate the custom code version on an instance.
*
* @param {String} instance The instance to activate the code on
* @param {String} code_version The code version to activate
*/
activate : function (instance, code_version) {
activateVersion(instance, code_version, auth.getToken(), function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && res.statusCode == 200 && !res.fault) {
console.info('Code version %s activated on %s',
code_version, instance);
} else if (res && res.body && res.body.fault &&
res.body.fault.type == 'CodeVersionModificationException') {
console.warn('Code version %s already active on %s',
code_version, instance);
} else {
console.error('Activating code version %s on %s failed: %s (%s)',
code_version, instance, res.body.fault.type, res.body.fault.message);
}
}, function() {
module.exports.cli.activate(instance, code_version);
});
});
},
/**
* Delete a code version
*
* @param {String} instance the instance to delete the code version from
* @param {String} version the code version to delete
* @param {Boolean} asJson optional flag to force output in json, false by default
*/
delete : function(instance, version, asJson) {
deleteCodeVersion(instance, version, function(err) {
if (err) {
if (asJson) {
console.json({error: err.message});
} else {
console.error(err.message);
}
return;
}
// the result
var result = {
message : `Code version ${version} deleted from ${instance}.`,
};
if (asJson) {
console.json(result);
return;
}
console.info(result['message']);
});
}
};
module.exports.api = {
/**
* Get all custom code versions deployed on a Commerce Cloud instance.
*
* @param {String} instance The instance to activate the code on
* @param {String} token The Oauth token to use for authentication
* @param {Function} callback Callback function executed as a result. The error and the code versions will be passed as parameters to the callback function.
*/
list : function (instance, token, callback) {
// check parameters
if (typeof(instance) !== 'string') {
throw new TypeError('Parameter instance is missing or not of type String');
}
if (typeof(token) !== 'string') {
throw new TypeError('Parameter token is missing or not of type String');
}
if (typeof(callback) !== 'function') {
throw new TypeError('Parameter callback is missing or not of type Function');
}
getVersions(instance, token, function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && res.statusCode == 200 && !res.fault) {
// Success
callback(undefined, res.body);
return;
}
// any errors
callback(new Error(err), undefined);
return;
});
});
},
/**
* Deploys a custom code archive onto a Commerce Cloud instance
*
* @param {String} instance The instance to activate the code on
* @param {String} archive The path to the ZIP archive to deploy
* @param {String} token The Oauth token to use for authentication
* @param {Object} options The options parameter can contains client certificate buffer and related passphrase in case of two factor authentication
* @param {Function} callback Callback function executed as a result. The error will be passed as parameter to the callback function.
*/
deploy: function (instance, archive, token, options, callback) {
deployCode(instance, archive, token, options, callback);
},
/**
* Activate the custom code version on a Commerce Cloud instance. If the code version is already
* active, no error is available.
*
* @param {String} instance The instance to activate the code on
* @param {String} code_version The code version to activate
* @param {String} token The Oauth token to use for authentication
* @param {Function} callback Callback function executed as a result. The error will be passed as parameter to the callback function.
*/
activate : function (instance, code_version, token, callback) {
// check parameters
if (typeof(instance) !== 'string') {
throw new TypeError('Parameter instance is missing or not of type String');
}
if (typeof(code_version) !== 'string') {
throw new TypeError('Parameter code_version is missing or not of type String');
}
if (typeof(token) !== 'string') {
throw new TypeError('Parameter token is missing or not of type String');
}
if (typeof(callback) !== 'function') {
throw new TypeError('Parameter callback is missing or not of type Function');
}
activateVersion(instance, code_version, token, function (err, res) {
if(err) {
getVersions(instance, token, function(err, res, body) {
for (let index = 0; index < body.data.length; index++) {
const version = body.data[index];
if(version.active && version.id == code_version){
callback(undefined)
return;
}
}
callback(new Error(err));
return;
})
} else {
ocapi.ensureValidToken(err, res, function(err, res) {
console.log({res, err})
if (!err && res.statusCode == 200 && !res.fault) {
// Success
callback(undefined);
return;
} else if (res.statusCode == 400 && res.body.fault.type == 'CodeVersionModificationException') {
// Exception: Code version already active won't end up in error
callback(undefined);
return;
}
callback(new Error(err));
return;
});
}
});
}
};