@spica/cli 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/index.js +3469 -37
  2. package/package.json +56 -27
  3. package/index.d.ts +0 -2
  4. package/index.js.map +0 -1
  5. package/src/commands/asset/apply.d.ts +0 -2
  6. package/src/commands/asset/apply.js +0 -48
  7. package/src/commands/asset/apply.js.map +0 -1
  8. package/src/commands/asset/delete.d.ts +0 -2
  9. package/src/commands/asset/delete.js +0 -32
  10. package/src/commands/asset/delete.js.map +0 -1
  11. package/src/commands/bucket/orm.d.ts +0 -45
  12. package/src/commands/bucket/orm.js +0 -478
  13. package/src/commands/bucket/orm.js.map +0 -1
  14. package/src/commands/context/ls.d.ts +0 -2
  15. package/src/commands/context/ls.js +0 -23
  16. package/src/commands/context/ls.js.map +0 -1
  17. package/src/commands/context/remove.d.ts +0 -2
  18. package/src/commands/context/remove.js +0 -16
  19. package/src/commands/context/remove.js.map +0 -1
  20. package/src/commands/context/set.d.ts +0 -2
  21. package/src/commands/context/set.js +0 -39
  22. package/src/commands/context/set.js.map +0 -1
  23. package/src/commands/context/switch.d.ts +0 -2
  24. package/src/commands/context/switch.js +0 -22
  25. package/src/commands/context/switch.js.map +0 -1
  26. package/src/commands/function/orm.d.ts +0 -2
  27. package/src/commands/function/orm.js +0 -63
  28. package/src/commands/function/orm.js.map +0 -1
  29. package/src/commands/project/ls.d.ts +0 -2
  30. package/src/commands/project/ls.js +0 -55
  31. package/src/commands/project/ls.js.map +0 -1
  32. package/src/commands/project/remove.d.ts +0 -2
  33. package/src/commands/project/remove.js +0 -82
  34. package/src/commands/project/remove.js.map +0 -1
  35. package/src/commands/project/start.d.ts +0 -2
  36. package/src/commands/project/start.js +0 -409
  37. package/src/commands/project/start.js.map +0 -1
  38. package/src/commands/project/sync.d.ts +0 -171
  39. package/src/commands/project/sync.js +0 -699
  40. package/src/commands/project/sync.js.map +0 -1
  41. package/src/commands/project/upgrade.d.ts +0 -2
  42. package/src/commands/project/upgrade.js +0 -174
  43. package/src/commands/project/upgrade.js.map +0 -1
  44. package/src/compile.d.ts +0 -22
  45. package/src/compile.js +0 -115
  46. package/src/compile.js.map +0 -1
  47. package/src/config.d.ts +0 -9
  48. package/src/config.js +0 -53
  49. package/src/config.js.map +0 -1
  50. package/src/console.d.ts +0 -14
  51. package/src/console.js +0 -47
  52. package/src/console.js.map +0 -1
  53. package/src/context.d.ts +0 -17
  54. package/src/context.js +0 -54
  55. package/src/context.js.map +0 -1
  56. package/src/function/helpers.d.ts +0 -2
  57. package/src/function/helpers.js +0 -12
  58. package/src/function/helpers.js.map +0 -1
  59. package/src/function/index.d.ts +0 -2
  60. package/src/function/index.js +0 -3
  61. package/src/function/index.js.map +0 -1
  62. package/src/function/interface.d.ts +0 -28
  63. package/src/function/interface.js +0 -3
  64. package/src/function/interface.js.map +0 -1
  65. package/src/function/modifier.d.ts +0 -41
  66. package/src/function/modifier.js +0 -64
  67. package/src/function/modifier.js.map +0 -1
  68. package/src/function/triggers/http/index.d.ts +0 -7
  69. package/src/function/triggers/http/index.js +0 -8
  70. package/src/function/triggers/http/index.js.map +0 -1
  71. package/src/function/triggers/http/services/axios.d.ts +0 -46
  72. package/src/function/triggers/http/services/axios.js +0 -163
  73. package/src/function/triggers/http/services/axios.js.map +0 -1
  74. package/src/function/triggers/http/services/index.d.ts +0 -3
  75. package/src/function/triggers/http/services/index.js +0 -4
  76. package/src/function/triggers/http/services/index.js.map +0 -1
  77. package/src/function/triggers/http/transformer.d.ts +0 -16
  78. package/src/function/triggers/http/transformer.js +0 -49
  79. package/src/function/triggers/http/transformer.js.map +0 -1
  80. package/src/function/triggers/index.d.ts +0 -4
  81. package/src/function/triggers/index.js +0 -6
  82. package/src/function/triggers/index.js.map +0 -1
  83. package/src/http.d.ts +0 -11
  84. package/src/http.js +0 -30
  85. package/src/http.js.map +0 -1
  86. package/src/project.d.ts +0 -11
  87. package/src/project.js +0 -63
  88. package/src/project.js.map +0 -1
  89. package/src/range.d.ts +0 -1
  90. package/src/range.js +0 -13
  91. package/src/range.js.map +0 -1
  92. package/src/representative.d.ts +0 -13
  93. package/src/representative.js +0 -120
  94. package/src/representative.js.map +0 -1
  95. package/src/status.d.ts +0 -19
  96. package/src/status.js +0 -39
  97. package/src/status.js.map +0 -1
  98. package/src/validator.d.ts +0 -5
  99. package/src/validator.js +0 -31
  100. package/src/validator.js.map +0 -1
@@ -1,699 +0,0 @@
1
- import caporalCore from "@caporal/core";
2
- const { CaporalValidator } = caporalCore;
3
- import { spin } from "../../console.js";
4
- import { httpService } from "../../http.js";
5
- import { availableSyncModules, validateSyncModules } from "../../validator.js";
6
- import { bold, green, red } from "colorette";
7
- import lodash from "lodash";
8
- const { isEqual } = lodash;
9
- let IGNORE_ERRORS;
10
- let CONCURRENCY_LIMIT;
11
- async function sync({ options: { sourceUrl, sourceApikey, targetUrl, targetApikey, modules, dryRun, syncFnEnv, ignoreErrors, concurrencyLimit } }) {
12
- IGNORE_ERRORS = ignoreErrors;
13
- CONCURRENCY_LIMIT = concurrencyLimit;
14
- const sourceService = httpService.create({
15
- baseUrl: sourceUrl.toString(),
16
- authorization: `APIKEY ${sourceApikey}`
17
- });
18
- const targetService = httpService.create({
19
- baseUrl: targetUrl.toString(),
20
- authorization: `APIKEY ${targetApikey}`
21
- });
22
- modules = modules
23
- .toString()
24
- .split(",")
25
- .map(m => m.trim());
26
- const coreSynchronizers = [
27
- FunctionSynchronizer,
28
- BucketSynchronizer,
29
- ApikeySynchronizer,
30
- PolicySynchronizer
31
- ];
32
- const synchronizers = [];
33
- for (const Ctor of coreSynchronizers) {
34
- const synchronizer = new Ctor(sourceService, targetService, { syncFnEnv });
35
- const subSynchronizers = await synchronizer.initialize().catch(e => {
36
- return Promise.reject(returnErrorMessage(e));
37
- });
38
- synchronizers.push(synchronizer);
39
- synchronizers.push(...subSynchronizers);
40
- }
41
- for (const name of modules) {
42
- const moduleSynchronizers = synchronizers.filter(s => s.moduleName == name);
43
- if (!moduleSynchronizers.length) {
44
- return Promise.reject(`Module ${name} does not exist.`);
45
- }
46
- for (const synchronizer of moduleSynchronizers) {
47
- const { insertions, updations, deletions } = await synchronizer.analyze().catch(e => {
48
- return Promise.reject(returnErrorMessage(e));
49
- });
50
- if (dryRun) {
51
- printActions({
52
- insertions,
53
- updations,
54
- deletions,
55
- field: synchronizer.primaryField,
56
- moduleName: synchronizer.getDisplayableModuleName()
57
- });
58
- }
59
- else {
60
- await synchronizer.synchronize().catch(e => Promise.reject(returnErrorMessage(e)));
61
- console.log(`\n${synchronizer.getDisplayableModuleName()} synchronization has been completed!`.toUpperCase());
62
- }
63
- }
64
- }
65
- }
66
- export default function (program) {
67
- return program
68
- .command("project sync", `Synchronize selected module objects between two spica instances(local or remote).
69
- ${red("ATTENTION")}: Source and target instance versions must be minimum v0.9.19 and for the best results both instance versions should be the same.
70
- Also this command will perform adding, overwriting and removing actions of the target instance and it's irreversible.
71
- We highly recommend you to use --dry-run=true and check the changes that will be applied before start.`)
72
- .option("--source-url", "API address of the instance where objects will be synchronized from", {
73
- required: true,
74
- validator: CaporalValidator.STRING
75
- })
76
- .option("--source-apikey", "Apikey of the instance where objects will be synchronized from", {
77
- required: true,
78
- validator: CaporalValidator.STRING
79
- })
80
- .option("--target-url", "API address of the instance where objects will be synchronized to", {
81
- required: true,
82
- validator: CaporalValidator.STRING
83
- })
84
- .option("--target-apikey", "API address of the instance where objects will be synchronized to", {
85
- required: true,
86
- validator: CaporalValidator.STRING
87
- })
88
- .option("--modules", `Module names of objects that will be synchronized. Available modules: ${green(availableSyncModules.join(","))}`, {
89
- required: true,
90
- validator: validateSyncModules
91
- })
92
- .option("--dry-run", "Shows the changes that will be applied to the target instance.", {
93
- default: false
94
- })
95
- .option("--sync-fn-env", "Set true if you need to sync function environment variables as well.", {
96
- default: false
97
- })
98
- .option("--ignore-errors", "Set true if you don't want to interrupt sync process because of failed requests.", {
99
- default: false
100
- })
101
- .option("--concurrency-limit", "Increase(if you want to speed up), decrease(if you get some error like ECONNRESET) this value to adjust how many parallel requests that will be sent to the target instance", {
102
- default: 100,
103
- validator: CaporalValidator.NUMBER
104
- })
105
- .action(sync);
106
- }
107
- export class ResourceGroupComparisor {
108
- constructor(sources, targets, uniqueField = "_id", ignoredFields = []) {
109
- this.sources = sources;
110
- this.targets = targets;
111
- this.uniqueField = uniqueField;
112
- this.ignoredFields = ignoredFields;
113
- this.existings = [];
114
- this.existingIds = [];
115
- this.existings = targets.filter(target => sources.some(source => source[this.uniqueField] == target[this.uniqueField]));
116
- this.existingIds = this.existings.map(existing => existing[this.uniqueField]);
117
- }
118
- updations() {
119
- const updations = [];
120
- for (const existing of this.existings) {
121
- const source = this.sources.find(source => source[this.uniqueField] == existing[this.uniqueField]);
122
- if (this.ignoredFields.length) {
123
- this.ignoredFields.forEach(field => {
124
- delete source[field];
125
- delete existing[field];
126
- });
127
- }
128
- if (!isEqual(source, existing)) {
129
- updations.push(source);
130
- }
131
- }
132
- return updations;
133
- }
134
- insertions() {
135
- return this.sources.filter(source => this.existingIds.indexOf(source[this.uniqueField]) == -1);
136
- }
137
- deletions() {
138
- return this.targets.filter(target => this.existingIds.indexOf(target[this.uniqueField]) == -1);
139
- }
140
- }
141
- export class FunctionSynchronizer {
142
- constructor(sourceService, targetService, options) {
143
- this.sourceService = sourceService;
144
- this.targetService = targetService;
145
- this.options = options;
146
- this.moduleName = "function";
147
- this.primaryField = "name";
148
- this.insertions = [];
149
- this.updations = [];
150
- this.deletions = [];
151
- }
152
- async initialize() {
153
- const synchronizers = [];
154
- const sourceFns = await this.sourceService.get("function");
155
- for (const fn of sourceFns) {
156
- synchronizers.push(new FunctionDependencySynchronizer(this.sourceService, this.targetService, fn));
157
- }
158
- synchronizers.push(new FunctionIndexSynchronizer(this.sourceService, this.targetService));
159
- return synchronizers;
160
- }
161
- async analyze() {
162
- console.log();
163
- let sourceFns = await spin({
164
- text: "Fetching functions from source instance",
165
- op: () => this.sourceService.get("function")
166
- });
167
- const targetFns = await spin({
168
- text: "Fetching functions from target instance",
169
- op: () => this.targetService.get("function")
170
- });
171
- if (!this.options.syncFnEnv) {
172
- sourceFns = sourceFns.map(fn => {
173
- fn.env = {};
174
- return fn;
175
- });
176
- for (const target of targetFns) {
177
- const index = sourceFns.findIndex(srcFn => srcFn._id == target._id);
178
- if (index != -1) {
179
- sourceFns[index].env = target.env;
180
- }
181
- }
182
- }
183
- const decider = new ResourceGroupComparisor(sourceFns, targetFns);
184
- this.insertions = decider.insertions();
185
- this.updations = decider.updations();
186
- this.deletions = decider.deletions();
187
- return {
188
- insertions: this.insertions,
189
- updations: this.updations,
190
- deletions: this.deletions
191
- };
192
- }
193
- async synchronize() {
194
- console.log();
195
- const insertPromiseFactories = this.insertions.map(fn => () => this.targetService.post("function", fn).catch(e => handleRejection({
196
- action: "insert",
197
- e,
198
- objectName: this.moduleName + " " + fn.name
199
- })));
200
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting functions to the target instance");
201
- const updatePromiseFactories = this.updations.map(fn => () => this.targetService.put(`function/${fn._id}`, fn).catch(e => handleRejection({
202
- action: "update",
203
- e,
204
- objectName: this.moduleName + " " + fn.name
205
- })));
206
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating target instance functions");
207
- const deletePromiseFactories = this.deletions.map(fn => () => this.targetService.delete(`function/${fn._id}`).catch(e => handleRejection({
208
- action: "delete",
209
- objectName: this.moduleName + " " + fn.name,
210
- e
211
- })));
212
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting target instance functions");
213
- }
214
- getDisplayableModuleName() {
215
- return this.moduleName;
216
- }
217
- }
218
- export class FunctionDependencySynchronizer {
219
- constructor(sourceService, targetService, fn) {
220
- this.sourceService = sourceService;
221
- this.targetService = targetService;
222
- this.fn = fn;
223
- this.moduleName = "function";
224
- this.subModuleName = "dependency";
225
- this.primaryField = "name";
226
- this.insertions = [];
227
- this.updations = [];
228
- this.deletions = [];
229
- }
230
- initialize() {
231
- return Promise.resolve([]);
232
- }
233
- async analyze() {
234
- const deleteTypes = deps => deps.map(d => {
235
- delete d.types;
236
- return d;
237
- });
238
- const sourceDeps = await this.sourceService
239
- .get(`function/${this.fn._id}/dependencies`)
240
- .then(deleteTypes);
241
- const targetDeps = await this.targetService
242
- .get(`function/${this.fn._id}/dependencies`)
243
- .then(deleteTypes)
244
- .catch(e => {
245
- if (isNotFoundException(e)) {
246
- return [];
247
- }
248
- return Promise.reject(e.data);
249
- });
250
- const decider = new ResourceGroupComparisor(sourceDeps, targetDeps, "name");
251
- this.insertions = decider.insertions();
252
- this.updations = decider.updations();
253
- this.deletions = decider.deletions();
254
- return {
255
- insertions: this.insertions,
256
- updations: this.updations,
257
- deletions: this.deletions
258
- };
259
- }
260
- async synchronize() {
261
- console.log();
262
- const promiseFactories = [];
263
- const insertBody = [...this.insertions, ...this.updations].reduce((acc, dep) => {
264
- const depName = `${dep.name}@${dep.version.slice(1)}`;
265
- acc.name.push(depName);
266
- return acc;
267
- }, { name: [] });
268
- if (insertBody.name.length) {
269
- promiseFactories.push(() => this.targetService.post(`function/${this.fn._id}/dependencies`, insertBody).catch(e => {
270
- handleRejection({
271
- action: "insert",
272
- e,
273
- objectName: this.getDisplayableModuleName()
274
- });
275
- }));
276
- }
277
- const deletePromiseFactories = this.deletions.map(dep => () => this.targetService.delete(`function/${this.fn._id}/dependencies/${dep.name}`).catch(e => handleRejection({
278
- action: "update",
279
- e,
280
- objectName: this.getDisplayableModuleName()
281
- })));
282
- if (deletePromiseFactories.length) {
283
- promiseFactories.push(...deletePromiseFactories);
284
- }
285
- return spinUntilPromiseEnd(promiseFactories, `Updating function '${this.fn.name}' dependencies`);
286
- }
287
- getDisplayableModuleName() {
288
- return `${this.moduleName} '${this.fn.name}' ${this.subModuleName}`;
289
- }
290
- }
291
- export class FunctionIndexSynchronizer {
292
- constructor(sourceService, targetService) {
293
- this.sourceService = sourceService;
294
- this.targetService = targetService;
295
- this.moduleName = "function";
296
- this.subModuleName = "index";
297
- this.primaryField = "name";
298
- this.insertions = [];
299
- this.updations = [];
300
- this.deletions = [];
301
- }
302
- initialize() {
303
- return Promise.resolve([]);
304
- }
305
- async analyze() {
306
- const sourceFns = await this.sourceService.get("function");
307
- const sourceFnIndexes = await Promise.all(sourceFns.map(fn => this.sourceService.get(`function/${fn._id}/index`).then(res => {
308
- return {
309
- _id: fn._id,
310
- name: fn.name,
311
- index: res.index
312
- };
313
- })));
314
- const targetFnIndexes = await Promise.all(sourceFns.map(fn => this.targetService
315
- .get(`function/${fn._id}/index`)
316
- .then(res => {
317
- return {
318
- _id: fn._id,
319
- name: fn.name,
320
- index: res.index
321
- };
322
- })
323
- .catch(e => {
324
- if (isNotFoundException(e)) {
325
- return false;
326
- }
327
- return Promise.reject(e.data);
328
- }))).then(indexes => indexes.filter(Boolean));
329
- const decider = new ResourceGroupComparisor(sourceFnIndexes, targetFnIndexes);
330
- this.insertions = decider.insertions();
331
- this.updations = decider.updations();
332
- this.deletions = decider.deletions();
333
- return {
334
- insertions: this.insertions,
335
- updations: this.updations,
336
- deletions: this.deletions
337
- };
338
- }
339
- synchronize() {
340
- const promiseFactories = [...this.insertions, ...this.updations].map(fn => () => this.targetService.post(`function/${fn._id}/index`, { index: fn.index }).catch(e => handleRejection({
341
- action: "insert",
342
- objectName: this.getDisplayableModuleName() + " " + fn.name,
343
- e
344
- })));
345
- return spinUntilPromiseEnd(promiseFactories, "Writing indexes to the target instance functions");
346
- }
347
- getDisplayableModuleName() {
348
- return this.moduleName + " " + this.subModuleName;
349
- }
350
- }
351
- export class BucketDataSynchronizer {
352
- constructor(sourceService, targetService, bucket) {
353
- this.sourceService = sourceService;
354
- this.targetService = targetService;
355
- this.bucket = bucket;
356
- this.moduleName = "bucket-data";
357
- this.insertions = [];
358
- this.updations = [];
359
- this.deletions = [];
360
- this.primaryField = this.bucket.primary;
361
- }
362
- initialize() {
363
- return Promise.resolve([]);
364
- }
365
- async analyze() {
366
- const params = {
367
- localize: false
368
- };
369
- const sourceData = await this.sourceService.get(`bucket/${this.bucket._id}/data`, {
370
- params
371
- });
372
- const targetData = await this.targetService
373
- .get(`bucket/${this.bucket._id}/data`, {
374
- params
375
- })
376
- .catch(e => {
377
- if (isNotFoundException(e)) {
378
- return [];
379
- }
380
- return Promise.reject(e.data);
381
- });
382
- const decider = new ResourceGroupComparisor(sourceData, targetData);
383
- this.insertions = decider.insertions();
384
- this.updations = decider.updations();
385
- this.deletions = decider.deletions();
386
- return {
387
- insertions: this.insertions,
388
- updations: this.updations,
389
- deletions: this.deletions
390
- };
391
- }
392
- async synchronize() {
393
- console.log();
394
- const insertPromiseFactories = this.insertions.map(data => () => this.targetService.post(`bucket/${this.bucket._id}/data`, data).catch(e => handleRejection({
395
- action: "insert",
396
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField],
397
- e
398
- })));
399
- await spinUntilPromiseEnd(insertPromiseFactories, `Inserting bucket ${this.bucket.title} data to the target instance`);
400
- const updatePromiseFactories = this.updations.map(data => () => this.targetService.put(`bucket/${this.bucket._id}/data/${data._id}`, data).catch(e => handleRejection({
401
- action: "update",
402
- e,
403
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField]
404
- })));
405
- await spinUntilPromiseEnd(updatePromiseFactories, `Updating bucket ${this.bucket.title} data on the target instance`);
406
- const deletePromiseFactories = this.deletions.map(data => () => this.targetService.delete(`bucket/${this.bucket._id}/data/${data._id}`).catch(e => handleRejection({
407
- action: "delete",
408
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField],
409
- e
410
- })));
411
- await spinUntilPromiseEnd(deletePromiseFactories, `Deleting bucket ${this.bucket.title} data from the target instance`);
412
- }
413
- getDisplayableModuleName() {
414
- return `${this.moduleName} '${this.bucket.title}'`;
415
- }
416
- }
417
- export class BucketSynchronizer {
418
- constructor(sourceService, targetService) {
419
- this.sourceService = sourceService;
420
- this.targetService = targetService;
421
- this.moduleName = "bucket";
422
- this.primaryField = "title";
423
- this.insertions = [];
424
- this.updations = [];
425
- this.deletions = [];
426
- }
427
- async initialize() {
428
- const synchronizers = [];
429
- const sourceBuckets = await this.sourceService.get("bucket");
430
- for (const bucket of sourceBuckets) {
431
- synchronizers.push(new BucketDataSynchronizer(this.sourceService, this.targetService, bucket));
432
- }
433
- return synchronizers;
434
- }
435
- async analyze() {
436
- console.log();
437
- const sourceBuckets = await spin({
438
- text: "Fetching buckets from source instance",
439
- op: () => this.sourceService.get("bucket")
440
- });
441
- const targetBuckets = await spin({
442
- text: "Fetching buckets from target instance",
443
- op: () => this.targetService.get("bucket")
444
- });
445
- const decider = new ResourceGroupComparisor(sourceBuckets, targetBuckets);
446
- this.insertions = decider.insertions();
447
- this.updations = decider.updations();
448
- this.deletions = decider.deletions();
449
- return {
450
- insertions: this.insertions,
451
- updations: this.updations,
452
- deletions: this.deletions
453
- };
454
- }
455
- async synchronize() {
456
- console.log();
457
- const insertPromiseFactories = this.insertions.map(bucket => () => this.targetService.post("bucket", bucket).catch(e => handleRejection({
458
- action: "insert",
459
- objectName: this.getDisplayableModuleName() + " " + bucket.title,
460
- e
461
- })));
462
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting buckets to the target instance");
463
- const updatePromiseFactories = this.updations.map(bucket => () => this.targetService.put(`bucket/${bucket._id}`, bucket).catch(e => handleRejection({
464
- action: "update",
465
- objectName: this.getDisplayableModuleName() + " " + bucket.title,
466
- e
467
- })));
468
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating buckets on the target instance");
469
- const deletePromiseFactories = this.deletions.map(bucket => () => this.targetService.delete(`bucket/${bucket._id}`).catch(e => handleRejection({
470
- action: "delete",
471
- objectName: bucket.title,
472
- e
473
- })));
474
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting bucket from the target instance");
475
- }
476
- getDisplayableModuleName() {
477
- return this.moduleName;
478
- }
479
- }
480
- export class ApikeySynchronizer {
481
- constructor(sourceService, targetService) {
482
- this.sourceService = sourceService;
483
- this.targetService = targetService;
484
- this.moduleName = "apikey";
485
- this.primaryField = "name";
486
- this.insertions = [];
487
- this.updations = [];
488
- this.deletions = [];
489
- }
490
- initialize() {
491
- return Promise.resolve([]);
492
- }
493
- async analyze() {
494
- console.log();
495
- const sourceApikeys = await spin({
496
- text: "Fetching apikeys from source instance",
497
- op: () => this.sourceService
498
- .get("passport/apikey")
499
- .then(r => r.data)
500
- });
501
- const targetApikeys = await spin({
502
- text: "Fetching apikeys from target instance",
503
- op: () => this.targetService
504
- .get("passport/apikey")
505
- .then(r => r.data)
506
- });
507
- const decider = new ResourceGroupComparisor(sourceApikeys, targetApikeys);
508
- this.insertions = decider.insertions();
509
- this.updations = decider.updations();
510
- this.deletions = decider.deletions();
511
- return {
512
- insertions: this.insertions,
513
- updations: this.updations,
514
- deletions: this.deletions
515
- };
516
- }
517
- async synchronize() {
518
- console.log();
519
- const insertPromiseFactories = this.insertions.map(apikey => () => {
520
- const insertRejectionHandler = e => handleRejection({
521
- action: "insert",
522
- objectName: this.getDisplayableModuleName() + " " + apikey.name,
523
- e
524
- });
525
- const apikeyInsertPromise = this.targetService
526
- .post("passport/apikey", apikey)
527
- .catch(e => insertRejectionHandler(e));
528
- return apikeyInsertPromise.then(() => {
529
- const policyAttachPromises = apikey.policies.map(policy => this.targetService
530
- .put(`passport/apikey/${apikey._id}/policy/${policy}`)
531
- .catch(e => insertRejectionHandler(e)));
532
- return Promise.all(policyAttachPromises);
533
- });
534
- });
535
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting apikeys to the target instance");
536
- const updatePromiseFactories = this.updations.map(apikey => () => {
537
- const rejectionHandler = e => {
538
- handleRejection({
539
- action: "update",
540
- objectName: this.getDisplayableModuleName() + " " + apikey.name,
541
- e
542
- });
543
- };
544
- const apikeyDeletePromise = this.targetService
545
- .delete(`passport/apikey/${apikey._id}`)
546
- .catch(e => rejectionHandler(e));
547
- const apikeyInsertPromise = apikeyDeletePromise.then(() => this.targetService.post(`passport/apikey`, apikey).catch(e => rejectionHandler(e)));
548
- return apikeyInsertPromise.then(() => {
549
- const policyAttachPromises = apikey.policies.map(policy => this.targetService
550
- .put(`passport/apikey/${apikey._id}/policy/${policy}`)
551
- .catch(e => rejectionHandler(e)));
552
- return Promise.all(policyAttachPromises);
553
- });
554
- });
555
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating apikeys on the target instance");
556
- const deletePromiseFactories = this.deletions.map(apikey => () => this.targetService.delete(`passport/apikey/${apikey._id}`).catch(e => handleRejection({
557
- action: "delete",
558
- objectName: apikey.name,
559
- e
560
- })));
561
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting apikeys from the target instance");
562
- }
563
- getDisplayableModuleName() {
564
- return this.moduleName;
565
- }
566
- }
567
- export class PolicySynchronizer {
568
- constructor(sourceService, targetService) {
569
- this.sourceService = sourceService;
570
- this.targetService = targetService;
571
- this.moduleName = "policy";
572
- this.primaryField = "name";
573
- this.insertions = [];
574
- this.updations = [];
575
- this.deletions = [];
576
- }
577
- initialize() {
578
- return Promise.resolve([]);
579
- }
580
- async analyze() {
581
- console.log();
582
- const sourcePolicies = await spin({
583
- text: "Fetching policies from source instance",
584
- op: () => this.sourceService
585
- .get("passport/policy")
586
- .then(r => r.data)
587
- });
588
- const targetPolicies = await spin({
589
- text: "Fetching policies from target instance",
590
- op: () => this.targetService
591
- .get("passport/policy")
592
- .then(r => r.data)
593
- });
594
- const decider = new ResourceGroupComparisor(sourcePolicies, targetPolicies);
595
- this.insertions = decider.insertions();
596
- this.updations = decider.updations();
597
- this.deletions = decider.deletions();
598
- return {
599
- insertions: this.insertions,
600
- updations: this.updations,
601
- deletions: this.deletions
602
- };
603
- }
604
- async synchronize() {
605
- console.log();
606
- const insertPromiseFactories = this.insertions.map(policy => () => this.targetService.post("passport/policy", policy).catch(e => handleRejection({
607
- action: "insert",
608
- objectName: this.getDisplayableModuleName() + " " + policy.name,
609
- e
610
- })));
611
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting policies to the target instance");
612
- const updatePromiseFactories = this.updations.map(policy => () => this.targetService.put(`passport/policy/${policy._id}`, policy).catch(e => handleRejection({
613
- action: "update",
614
- objectName: this.getDisplayableModuleName() + " " + policy.name,
615
- e
616
- })));
617
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating policies on the target instance");
618
- const deletePromiseFactories = this.deletions.map(policy => () => this.targetService.delete(`passport/policy/${policy._id}`).catch(e => handleRejection({
619
- action: "delete",
620
- objectName: policy.name,
621
- e
622
- })));
623
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting policies from the target instance");
624
- }
625
- getDisplayableModuleName() {
626
- return this.moduleName;
627
- }
628
- }
629
- function printActions({ insertions, updations, deletions, field, moduleName }) {
630
- const joinActions = (actions) => {
631
- const MAX_LINES = 20;
632
- actions = actions.map(a => `- ${a[field]}`);
633
- if (actions.length > MAX_LINES) {
634
- actions = actions.slice(0, MAX_LINES);
635
- actions.push("...more");
636
- }
637
- return actions.join("\n");
638
- };
639
- console.log();
640
- console.log(`----- ${moduleName.toUpperCase()} -----`);
641
- console.log(`\n* Found ${bold(insertions.length)} objects to ${bold("insert")}:
642
- ${joinActions(insertions)}`);
643
- console.log(`\n* Found ${bold(updations.length)} objects to ${bold("update")}:
644
- ${joinActions(updations)}`);
645
- console.log(`\n* Found ${bold(deletions.length)} objects to ${bold("delete")}:
646
- ${joinActions(deletions)}`);
647
- }
648
- export var SendingPromiseStrategy;
649
- (function (SendingPromiseStrategy) {
650
- SendingPromiseStrategy[SendingPromiseStrategy["All"] = 0] = "All";
651
- SendingPromiseStrategy[SendingPromiseStrategy["OneByOne"] = 1] = "OneByOne";
652
- SendingPromiseStrategy[SendingPromiseStrategy["Batch"] = 2] = "Batch";
653
- SendingPromiseStrategy[SendingPromiseStrategy["Retry"] = 3] = "Retry";
654
- })(SendingPromiseStrategy || (SendingPromiseStrategy = {}));
655
- function spinUntilPromiseEnd(promiseFactories, label) {
656
- if (!promiseFactories.length) {
657
- return;
658
- }
659
- return spin({
660
- text: label,
661
- op: async (spinner) => {
662
- let progress = 0;
663
- let count = 0;
664
- const concurrency = Math.min(promiseFactories.length, CONCURRENCY_LIMIT);
665
- let batchPromises = [];
666
- for (let i = 0; i < promiseFactories.length; i++) {
667
- batchPromises.push(promiseFactories[i]);
668
- if (batchPromises.length == concurrency || i == promiseFactories.length - 1) {
669
- await Promise.all(batchPromises.map(b => b()));
670
- batchPromises = [];
671
- }
672
- count++;
673
- progress = (100 / promiseFactories.length) * count;
674
- spinner.text = `${label} (%${Math.round(progress)})`;
675
- }
676
- return;
677
- }
678
- });
679
- }
680
- function handleRejection({ action, objectName, e }) {
681
- const errorMessage = returnErrorMessage(e);
682
- const msg = `
683
- Failed to ${action} ${bold(objectName)}.
684
- ${errorMessage}`;
685
- if (IGNORE_ERRORS) {
686
- return console.warn(msg);
687
- }
688
- else {
689
- return Promise.reject(msg);
690
- }
691
- }
692
- function returnErrorMessage(e) {
693
- return e.data ? e.data.message || e.data : e;
694
- }
695
- function isNotFoundException(e) {
696
- const code = 404;
697
- return e.status == code || e.statusCode == code || (e.data && e.data.statusCode == code);
698
- }
699
- //# sourceMappingURL=sync.js.map