-
Notifications
You must be signed in to change notification settings - Fork 2
/
bm.js
495 lines (424 loc) · 13.7 KB
/
bm.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
'use strict';
/** Constants */
const MAX_ATTEMPTS = 3,
RETRY_DELAY = 300,
REQUEST_TIMEOUT = 15000;
/** Modules */
const request = require('request');
const chalk = require('chalk');
/** Internal modules */
const RequestManager = require('./request-manager');
const BMTools = require('./bm-tools');
const Log = require('./logger');
/**
* A WebDav client realizing DELETE, PUT, UNZIP, MKCOL, PROPFIND
* @param {Object} config The configuration object used by Davos
*/
class BM {
constructor(config) {
this.config = config;
this.options = {
method: 'POST',
baseUrl: `https://${ this.config.hostname }/on/demandware.store/Sites-Site/default`,
uri: '/',
contentString: null,
jar: request.jar(),
ignoreErrors: true,
followAllRedirects: true
};
this.bmTools = new BMTools();
this.reqMan = new RequestManager(this.options, this.config);
return this;
}
getCheckProgressConfig(type) {
switch (type) {
case 'site':
return {
uri: '/ViewSiteImpex-Status',
max_attempts: 100,
max_import_attempts: 1,
retry_delay: 1000,
selector: '#unitSelection ~ table:nth-of-type(3)',
label: 'Site Import ({0})'
};
case 'metaValidation':
return {
uri: '/ViewCustomizationImpex-Start',
max_attempts: 100,
max_import_attempts: 1,
retry_delay: 1000,
selector: 'form[name="ImpexForm"] > table:nth-child(6)',
label: 'Meta Data Validation <{0}>'
}
case 'metaImport':
return {
uri: '/ViewCustomizationImpex-Start',
max_attempts: 100,
max_import_attempts: 1,
retry_delay: 1000,
selector: 'form[name="ImpexForm"] > table:nth-child(6)',
label: 'Meta Data Import <{0}>'
}
}
}
doRequest(options, attemptsLeft = MAX_ATTEMPTS, retryDelay = RETRY_DELAY) {
return this.reqMan.doRequest(options, attemptsLeft, retryDelay)
.then((body) => {
return Promise.resolve(body);
}, (err) => {
return Promise.reject(err);
});
}
/**
* HTTP Request BM LOGIN
*/
login() {
const self = this;
return new Promise(((resolve, reject) => {
const options = {
uri: '/ViewApplication-ProcessLogin',
form: {
LoginForm_Login: self.config.username,
LoginForm_Password: self.config.password,
LoginForm_RegistrationDomain: 'Sites'
}
};
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then((body) => {
if (!self.bmTools.isLoggedIn(body)) {
const e = new Error('Not able to login into business manager.')
return reject(e);
}
self.bmTools.parseCsrfToken(body);
resolve();
}, (err) => {
reject(err);
});
}));
}
/**
* HTTP Request BM ENSURE NO IMPORT
*/
ensureNoImport(archiveName) {
const self = this;
const SITE_IMPORT = this.getCheckProgressConfig('site');
return new Promise(((resolve, reject) => {
const options = {
uri: self.bmTools.appendCSRF('/ViewSiteImpex-Status')
};
if (archiveName === undefined || archiveName.length < 1) {
const e = new Error('Invalid archive name.');
return reject(e);
}
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then((body) => {
if (!self.bmTools.isLoggedIn(body)) {
const e = new Error('Not authenticated.');
return reject(e);
}
self.bmTools.parseCsrfToken(body);
const job = self.bmTools.parseBody(body, {
archiveName,
selector: SITE_IMPORT.selector,
processLabel: SITE_IMPORT.label
});
if (job && job.isRunning) {
const e = new Error(`Import already running! Duration: ${ job.duration}`);
return reject(e);
}
resolve();
}, (err) => {
reject(err);
});
}));
}
/**
* HTTP Request BM IMPORT SITES
*/
importSites(archiveName, attemptsLeft) {
const self = this;
const SITE_IMPORT = this.getCheckProgressConfig('site');
if (attemptsLeft === undefined) {
attemptsLeft = SITE_IMPORT.max_import_attempts;
}
return new Promise(((resolve, reject) => {
const options = {
uri: self.bmTools.appendCSRF('/ViewSiteImpex-Dispatch'),
form: {
ImportFileName: archiveName,
import: 'OK',
realmUse: 'false'
}
};
if (archiveName === undefined || archiveName.length < 1) {
const e = new Error('Invalid archive name.');
return reject(e);
}
if (attemptsLeft < 1) {
const e = new Error('Maximum retries reached. Unable to import site.');
return reject(e);
}
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then((body) => {
if (!self.bmTools.isLoggedIn(body)) {
const e = new Error('Not authenticated.');
return reject(e);
}
self.bmTools.parseCsrfToken(body);
if (!self.bmTools.isValidRequest(body)) {
Log.info(chalk.cyan('The request was not validated. Retrying with new csrf token.'));
(function () {
return new Promise(((retryResolve, retryReject) => {
setTimeout(() => {
retryResolve();
}, SITE_IMPORT.retry_delay);
}));
})().then(() => {
return self.importSites(archiveName, --attemptsLeft);
}).then(() => {
resolve();
}, (err) => {
reject(err);
});
} else {
resolve();
}
}, (err) => {
reject(err);
});
}));
}
/**
* HTTP Request BM CHECK IMPORT PROGRESS
*/
checkImportProgress(archiveName, attemptsLeft, importConfig = 'site') {
const self = this;
const config = this.getCheckProgressConfig(importConfig);
if (attemptsLeft === undefined) {
attemptsLeft = config.max_attempts;
}
return new Promise(((resolve, reject) => {
const options = {
uri: self.bmTools.appendCSRF(config.uri)
};
if (archiveName === undefined || archiveName.length < 1) {
const e = new Error('Invalid archive name.');
return reject(e);
}
if (attemptsLeft < 1) {
const e = new Error('Maximum retries reached. Login to BM for more details.');
return reject(e);
}
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then((body) => {
if (!self.bmTools.isLoggedIn(body)) {
const e = new Error('Not authenticated.');
return reject(e);
}
self.bmTools.parseCsrfToken(body);
const job = self.bmTools.parseBody(body, {
archiveName,
selector: config.selector,
processLabel: config.label
});
if (!job) {
const e = new Error('Could not find import job.');
return reject(e);
}
if (job.isRunning) {
Log.info(chalk.cyan(`Job still running. Execution time: ${ job.duration}`));
(function () {
return new Promise(((retryResolve, retryReject) => {
setTimeout(() => {
retryResolve();
}, config.retry_delay);
}));
})().then(() => {
return self.checkImportProgress(archiveName, --attemptsLeft);
}).then(() => {
resolve();
}, (err) => {
reject(err);
});
} else if (job.isError) {
const e = new Error('Import failed! Login to BM for more details.');
return reject(e);
} else if (job.isFinished) {
Log.info(chalk.cyan(`Finished. ${ job.dataErrors || 'No' } data errors. Duration: ${ job.duration}`));
return resolve();
} else {
const e = new Error('Unexpected state!');
return reject(e);
}
}, (err) => {
reject(err);
});
}));
}
/**
* HTTP Request ACTIVATE CODE VERSION
*/
activateCodeVersion() {
const self = this;
return new Promise(((resolve, reject) => {
const options = {
uri: self.bmTools.appendCSRF('/ViewCodeDeployment-Activate'),
form: {
CodeVersionID: self.config.codeVersion
}
};
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then(() => {
resolve();
}, (err) => {
reject(err);
});
}));
}
/**
* WebDav Request Upload Sites Meta
*/
uploadSitesArchive(path) {
return this.uploadImpex(path, '/src/instance');
}
uploadMeta(path) {
return this.uploadImpex(path, '/src/customization');
}
uploadImpex(path, location) {
const self = this;
return new Promise(((resolve, reject) => {
const options = {
method: 'PUT',
baseUrl: `https://${ self.config.hostname }/on/demandware.servlet/webdav/Sites/Impex${ location}`,
uri: path,
// change auth to bearer and token ?
auth: {
user: self.config.username,
password: self.config.password
},
jar: false,
timeout: REQUEST_TIMEOUT,
fromTmpDir: true
};
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then(() => {
resolve();
}, (err) => {
reject(err);
});
}));
}
/**
* WebDav Request Delete Sites Meta
*/
deleteSitesArchive(path) {
return this.deleteImpex(path, '/src/instance');
}
deleteMeta(path) {
return this.deleteImpex(path, '/src/customization');
}
deleteImpex(path, location) {
const self = this;
return new Promise(((resolve, reject) => {
const options = {
method: 'DELETE',
baseUrl: `https://${ self.config.hostname }/on/demandware.servlet/webdav/Sites/Impex${ location}`,
uri: path,
contentString: null,
auth: {
user: self.config.username,
password: self.config.password
},
jar: false,
timeout: REQUEST_TIMEOUT
};
self.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY)
.then(() => {
resolve();
}, (err) => {
reject(err);
});
}));
}
validateMetaImport(filename) {
const options = {
uri: this.bmTools.appendCSRF('/ViewCustomizationImport-Dispatch'),
form: {
SelectedFile: filename,
ProcessPipelineName: 'ProcessObjectTypeImport',
ProcessPipelineStartNode: 'Validate',
JobDescription: 'Validate+custommeta+data+definitions',
JobName: 'ProcessObjectTypeImpex',
validate: ''
},
timeout: REQUEST_TIMEOUT
};
return this.doRequest(options, MAX_ATTEMPTS, RETRY_DELAY);
}
importMeta(filename) {
const cheerio = require('cheerio'),
self = this;
/**
* Selects the first completed validation for import
*/
function selectValidationJob(body) {
const $ = cheerio.load(body);
if (!self.bmTools.isLoggedIn(body)) {
throw 'Not able to login into business manager';
}
self.bmTools.parseCsrfToken(body);
// Check if validation has been done on this file: Prepare label text
let archiveLabel = 'Meta Data Validation <{0}>'.replace('{0}', filename),
$td = $('form[name="ImpexForm"] > table:nth-child(6) tr > td:nth-child(2)'),
importLink;
// Compare target label text with actual result, strip whitespace (to ignore line breaks etc.)
const record = $td.filter(function () {
const normalizedTargetLabel = self.bmTools.removeAllWhiteSpaces(archiveLabel);
const normalizedActualLabel = self.bmTools.removeAllWhiteSpaces($(this).text());
return normalizedActualLabel === normalizedTargetLabel;
});
if (!record || record.length === 0) {
throw `No validation task found for ${ filename}`;
}
importLink = $(record).find('.selection_link').first().attr('href');
Log.info(chalk.cyan('Found validation task'));
// Go to validation form page in order to execute import process
return self.doRequest({
baseUrl: self.bmTools.appendCSRF(importLink),
uri: ''
}).then(importMeta);
}
/**
* Execute meta data import for the validation file
*/
function importMeta(body) {
const $ = cheerio.load(body);
self.bmTools.parseCsrfToken(body);
// if confirm import is disabled this validation import is invalid
if ($('button[name="confirmImport"]').attr('disabled')) {
throw 'Validation errors have been found, unable to import';
}
const formAction = $('form[name="ValidateFileForm"]').attr('action'),
form = {
SelectedFile: $('input[name="SelectedFile"]').attr('value'),
JobConfigurationUUID: $('input[name="JobConfigurationUUID"]').attr('value'),
ProcessPipelineName: $('input[name="ProcessPipelineName"]').attr('value'),
ProcessPipelineStartNode: $('input[name="ProcessPipelineStartNode"]').attr('value'),
JobName: $('input[name="JobName"]').attr('value'),
startImport: '',
// TODO possibly enable this, make customizable
// ClearAttributeDefinitions: true
};
Log.info(chalk.cyan('Importing meta data file'));
return self.doRequest({
baseUrl: self.bmTools.appendCSRF(formAction),
uri: '',
form
});
}
return this.doRequest({
uri: this.bmTools.appendCSRF(this.getCheckProgressConfig('metaImport').uri)
}).then(selectValidationJob);
}
}
module.exports = BM;