@spica/cli 0.18.17 → 0.18.18-pre2

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 (101) hide show
  1. package/index.js +3516 -38
  2. package/package.json +56 -31
  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 -56
  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 -35
  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 -443
  37. package/src/commands/project/start.js.map +0 -1
  38. package/src/commands/project/sync.d.ts +0 -201
  39. package/src/commands/project/sync.js +0 -785
  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 -7
  99. package/src/validator.js +0 -52
  100. package/src/validator.js.map +0 -1
  101. package/tsconfig.app.tsbuildinfo +0 -1
@@ -1,785 +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, validateSyncIds, 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, bucketIds, functionIds, apikeyIds, policyIds, envVarIds, dryRun, 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
- EnvironmentVariableSynchronizer
32
- ];
33
- const synchronizers = [];
34
- for (const Ctor of coreSynchronizers) {
35
- const synchronizer = new Ctor(sourceService, targetService, {
36
- bucketIds: transformIDs(bucketIds),
37
- functionIds: transformIDs(functionIds),
38
- apikeyIds: transformIDs(apikeyIds),
39
- policyIds: transformIDs(policyIds),
40
- envVarIds: transformIDs(envVarIds)
41
- });
42
- const subSynchronizers = await synchronizer.initialize().catch(e => {
43
- return Promise.reject(returnErrorMessage(e));
44
- });
45
- synchronizers.push(synchronizer);
46
- synchronizers.push(...subSynchronizers);
47
- }
48
- for (const name of modules) {
49
- const moduleSynchronizers = synchronizers.filter(s => s.moduleName == name);
50
- for (const synchronizer of moduleSynchronizers) {
51
- const { insertions, updations, deletions } = await synchronizer.analyze().catch(e => {
52
- return Promise.reject(returnErrorMessage(e));
53
- });
54
- if (dryRun) {
55
- printActions({
56
- insertions,
57
- updations,
58
- deletions,
59
- field: synchronizer.primaryField,
60
- moduleName: synchronizer.getDisplayableModuleName()
61
- });
62
- }
63
- else {
64
- try {
65
- await synchronizer.synchronize();
66
- }
67
- catch (error) {
68
- Promise.reject(returnErrorMessage(error));
69
- }
70
- console.log(`\n${synchronizer.getDisplayableModuleName()} synchronization has been completed!`.toUpperCase());
71
- }
72
- }
73
- }
74
- }
75
- export default function (program) {
76
- return program
77
- .command("project sync", `Synchronize selected module objects between two spica instances(local or remote).
78
- ${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.
79
- Also this command will perform adding, overwriting and removing actions of the target instance and it's irreversible.
80
- We highly recommend you to use --dry-run=true and check the changes that will be applied before start.`)
81
- .option("--source-url", "API address of the instance where objects will be synchronized from", {
82
- required: true,
83
- validator: CaporalValidator.STRING
84
- })
85
- .option("--source-apikey", "Apikey of the instance where objects will be synchronized from", {
86
- required: true,
87
- validator: CaporalValidator.STRING
88
- })
89
- .option("--target-url", "API address of the instance where objects will be synchronized to", {
90
- required: true,
91
- validator: CaporalValidator.STRING
92
- })
93
- .option("--target-apikey", "API address of the instance where objects will be synchronized to", {
94
- required: true,
95
- validator: CaporalValidator.STRING
96
- })
97
- .option("--modules", `Module names of objects that will be synchronized. Available modules: ${green(availableSyncModules.join(","))}`, {
98
- required: true,
99
- validator: validateSyncModules
100
- })
101
- .option("--bucket-ids <ids>", "Buckets that will be synchronized (comma-separated)", {
102
- required: false,
103
- validator: validateSyncIds
104
- })
105
- .option("--function-ids <ids>", "Functions that will be synchronized (comma-separated)", {
106
- required: false,
107
- validator: validateSyncIds
108
- })
109
- .option("--apikey-ids <ids>", "Apikeys that will be synchronized (comma-separated)", {
110
- required: false,
111
- validator: validateSyncIds
112
- })
113
- .option("--policy-ids <ids>", "Policies that will be synchronized (comma-separated)", {
114
- required: false,
115
- validator: validateSyncIds
116
- })
117
- .option("--env-var-ids <ids>", "Environment variables that will be synchronized (comma-separated)", {
118
- required: false,
119
- validator: validateSyncIds
120
- })
121
- .option("--dry-run", "Shows the changes that will be applied to the target instance.", {
122
- default: false
123
- })
124
- .option("--ignore-errors", "Set true if you don't want to interrupt sync process because of failed requests.", {
125
- default: false
126
- })
127
- .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", {
128
- default: 100,
129
- validator: CaporalValidator.NUMBER
130
- })
131
- .action(sync);
132
- }
133
- export class ResourceGroupComparisor {
134
- constructor(sources, targets, uniqueField = "_id", ignoredFields = []) {
135
- this.sources = sources;
136
- this.targets = targets;
137
- this.uniqueField = uniqueField;
138
- this.ignoredFields = ignoredFields;
139
- this.existings = [];
140
- this.existingIds = [];
141
- this.existings = targets.filter(target => sources.some(source => source[this.uniqueField] == target[this.uniqueField]));
142
- this.existingIds = this.existings.map(existing => existing[this.uniqueField]);
143
- }
144
- updations() {
145
- const updations = [];
146
- for (const existing of this.existings) {
147
- const source = this.sources.find(source => source[this.uniqueField] == existing[this.uniqueField]);
148
- if (this.ignoredFields.length) {
149
- this.ignoredFields.forEach(field => {
150
- delete source[field];
151
- delete existing[field];
152
- });
153
- }
154
- if (!isEqual(source, existing)) {
155
- updations.push(source);
156
- }
157
- }
158
- return updations;
159
- }
160
- insertions() {
161
- return this.sources.filter(source => this.existingIds.indexOf(source[this.uniqueField]) == -1);
162
- }
163
- deletions() {
164
- return this.targets.filter(target => this.existingIds.indexOf(target[this.uniqueField]) == -1);
165
- }
166
- }
167
- export class FunctionSynchronizer {
168
- constructor(sourceService, targetService, options) {
169
- this.sourceService = sourceService;
170
- this.targetService = targetService;
171
- this.options = options;
172
- this.moduleName = "function";
173
- this.primaryField = "name";
174
- this.insertions = [];
175
- this.updations = [];
176
- this.deletions = [];
177
- }
178
- async initialize() {
179
- const synchronizers = [];
180
- const sourceFns = await getFilteredResources("function", this.sourceService, this.options.functionIds);
181
- for (const fn of sourceFns) {
182
- synchronizers.push(new FunctionDependencySynchronizer(this.sourceService, this.targetService, fn));
183
- }
184
- synchronizers.push(new FunctionIndexSynchronizer(this.sourceService, this.targetService));
185
- return synchronizers;
186
- }
187
- async analyze() {
188
- console.log();
189
- let sourceFns = await spin({
190
- text: "Fetching functions from source instance",
191
- op: () => getFilteredResources("function", this.sourceService, this.options.functionIds)
192
- });
193
- const targetFns = await spin({
194
- text: "Fetching functions from target instance",
195
- op: () => getFilteredResources("function", this.targetService, this.options.functionIds)
196
- });
197
- const decider = new ResourceGroupComparisor(sourceFns, targetFns);
198
- this.insertions = decider.insertions();
199
- this.updations = decider.updations();
200
- this.deletions = decider.deletions();
201
- return {
202
- insertions: this.insertions,
203
- updations: this.updations,
204
- deletions: this.deletions
205
- };
206
- }
207
- async synchronize() {
208
- console.log();
209
- const insertPromiseFactories = this.insertions.map(fn => () => this.targetService.post("function", fn).catch(e => handleRejection({
210
- action: "insert",
211
- e,
212
- objectName: this.moduleName + " " + fn.name
213
- })));
214
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting functions to the target instance");
215
- const updatePromiseFactories = this.updations.map(fn => () => this.targetService.put(`function/${fn._id}`, fn).catch(e => handleRejection({
216
- action: "update",
217
- e,
218
- objectName: this.moduleName + " " + fn.name
219
- })));
220
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating target instance functions");
221
- const deletePromiseFactories = this.deletions.map(fn => () => this.targetService.delete(`function/${fn._id}`).catch(e => handleRejection({
222
- action: "delete",
223
- objectName: this.moduleName + " " + fn.name,
224
- e
225
- })));
226
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting target instance functions");
227
- }
228
- getDisplayableModuleName() {
229
- return this.moduleName;
230
- }
231
- }
232
- export class FunctionDependencySynchronizer {
233
- constructor(sourceService, targetService, fn) {
234
- this.sourceService = sourceService;
235
- this.targetService = targetService;
236
- this.fn = fn;
237
- this.moduleName = "function";
238
- this.subModuleName = "dependency";
239
- this.primaryField = "name";
240
- this.insertions = [];
241
- this.updations = [];
242
- this.deletions = [];
243
- }
244
- initialize() {
245
- return Promise.resolve([]);
246
- }
247
- async analyze() {
248
- const deleteTypes = deps => deps.map(d => {
249
- delete d.types;
250
- return d;
251
- });
252
- const sourceDeps = await this.sourceService
253
- .get(`function/${this.fn._id}/dependencies`)
254
- .then(deleteTypes);
255
- const targetDeps = await this.targetService
256
- .get(`function/${this.fn._id}/dependencies`)
257
- .then(deleteTypes)
258
- .catch(e => {
259
- if (isNotFoundException(e)) {
260
- return [];
261
- }
262
- return Promise.reject(e.data);
263
- });
264
- const decider = new ResourceGroupComparisor(sourceDeps, targetDeps, "name");
265
- this.insertions = decider.insertions();
266
- this.updations = decider.updations();
267
- this.deletions = decider.deletions();
268
- return {
269
- insertions: this.insertions,
270
- updations: this.updations,
271
- deletions: this.deletions
272
- };
273
- }
274
- async synchronize() {
275
- console.log();
276
- const promiseFactories = [];
277
- const insertBody = [...this.insertions, ...this.updations].reduce((acc, dep) => {
278
- const depName = `${dep.name}@${dep.version.slice(1)}`;
279
- acc.name.push(depName);
280
- return acc;
281
- }, { name: [] });
282
- if (insertBody.name.length) {
283
- promiseFactories.push(() => this.targetService.post(`function/${this.fn._id}/dependencies`, insertBody).catch(e => {
284
- handleRejection({
285
- action: "insert",
286
- e,
287
- objectName: this.getDisplayableModuleName()
288
- });
289
- }));
290
- }
291
- const deletePromiseFactories = this.deletions.map(dep => () => this.targetService.delete(`function/${this.fn._id}/dependencies/${dep.name}`).catch(e => handleRejection({
292
- action: "update",
293
- e,
294
- objectName: this.getDisplayableModuleName()
295
- })));
296
- if (deletePromiseFactories.length) {
297
- promiseFactories.push(...deletePromiseFactories);
298
- }
299
- return spinUntilPromiseEnd(promiseFactories, `Updating function '${this.fn.name}' dependencies`);
300
- }
301
- getDisplayableModuleName() {
302
- return `${this.moduleName} '${this.fn.name}' ${this.subModuleName}`;
303
- }
304
- }
305
- export class FunctionIndexSynchronizer {
306
- constructor(sourceService, targetService) {
307
- this.sourceService = sourceService;
308
- this.targetService = targetService;
309
- this.moduleName = "function";
310
- this.subModuleName = "index";
311
- this.primaryField = "name";
312
- this.insertions = [];
313
- this.updations = [];
314
- this.deletions = [];
315
- }
316
- initialize() {
317
- return Promise.resolve([]);
318
- }
319
- async analyze() {
320
- const sourceFns = await this.sourceService.get("function");
321
- const sourceFnIndexes = await Promise.all(sourceFns.map(fn => this.sourceService.get(`function/${fn._id}/index`).then(res => {
322
- return {
323
- _id: fn._id,
324
- name: fn.name,
325
- index: res.index
326
- };
327
- })));
328
- const targetFnIndexes = await Promise.all(sourceFns.map(fn => this.targetService
329
- .get(`function/${fn._id}/index`)
330
- .then(res => {
331
- return {
332
- _id: fn._id,
333
- name: fn.name,
334
- index: res.index
335
- };
336
- })
337
- .catch(e => {
338
- if (isNotFoundException(e)) {
339
- return false;
340
- }
341
- return Promise.reject(e.data);
342
- }))).then(indexes => indexes.filter(Boolean));
343
- const decider = new ResourceGroupComparisor(sourceFnIndexes, targetFnIndexes);
344
- this.insertions = decider.insertions();
345
- this.updations = decider.updations();
346
- this.deletions = decider.deletions();
347
- return {
348
- insertions: this.insertions,
349
- updations: this.updations,
350
- deletions: this.deletions
351
- };
352
- }
353
- synchronize() {
354
- const promiseFactories = [...this.insertions, ...this.updations].map(fn => () => this.targetService.post(`function/${fn._id}/index`, { index: fn.index }).catch(e => handleRejection({
355
- action: "insert",
356
- objectName: this.getDisplayableModuleName() + " " + fn.name,
357
- e
358
- })));
359
- return spinUntilPromiseEnd(promiseFactories, "Writing indexes to the target instance functions");
360
- }
361
- getDisplayableModuleName() {
362
- return this.moduleName + " " + this.subModuleName;
363
- }
364
- }
365
- export class BucketDataSynchronizer {
366
- constructor(sourceService, targetService, bucket) {
367
- this.sourceService = sourceService;
368
- this.targetService = targetService;
369
- this.bucket = bucket;
370
- this.moduleName = "bucket-data";
371
- this.insertions = [];
372
- this.updations = [];
373
- this.deletions = [];
374
- this.primaryField = this.bucket.primary;
375
- }
376
- initialize() {
377
- return Promise.resolve([]);
378
- }
379
- async analyze() {
380
- const params = {
381
- localize: false
382
- };
383
- const sourceData = await this.sourceService.get(`bucket/${this.bucket._id}/data`, {
384
- params
385
- });
386
- const targetData = await this.targetService
387
- .get(`bucket/${this.bucket._id}/data`, {
388
- params
389
- })
390
- .catch(e => {
391
- if (isNotFoundException(e)) {
392
- return [];
393
- }
394
- return Promise.reject(e.data);
395
- });
396
- const decider = new ResourceGroupComparisor(sourceData, targetData);
397
- this.insertions = decider.insertions();
398
- this.updations = decider.updations();
399
- this.deletions = decider.deletions();
400
- return {
401
- insertions: this.insertions,
402
- updations: this.updations,
403
- deletions: this.deletions
404
- };
405
- }
406
- async synchronize() {
407
- console.log();
408
- const insertPromiseFactories = this.insertions.map(data => () => this.targetService.post(`bucket/${this.bucket._id}/data`, data).catch(e => handleRejection({
409
- action: "insert",
410
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField],
411
- e
412
- })));
413
- await spinUntilPromiseEnd(insertPromiseFactories, `Inserting bucket ${this.bucket.title} data to the target instance`);
414
- const updatePromiseFactories = this.updations.map(data => () => this.targetService.put(`bucket/${this.bucket._id}/data/${data._id}`, data).catch(e => handleRejection({
415
- action: "update",
416
- e,
417
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField]
418
- })));
419
- await spinUntilPromiseEnd(updatePromiseFactories, `Updating bucket ${this.bucket.title} data on the target instance`);
420
- const deletePromiseFactories = this.deletions.map(data => () => this.targetService.delete(`bucket/${this.bucket._id}/data/${data._id}`).catch(e => handleRejection({
421
- action: "delete",
422
- objectName: this.getDisplayableModuleName() + " " + data[this.primaryField],
423
- e
424
- })));
425
- await spinUntilPromiseEnd(deletePromiseFactories, `Deleting bucket ${this.bucket.title} data from the target instance`);
426
- }
427
- getDisplayableModuleName() {
428
- return `${this.moduleName} '${this.bucket.title}'`;
429
- }
430
- }
431
- export class BucketSynchronizer {
432
- constructor(sourceService, targetService, options) {
433
- this.sourceService = sourceService;
434
- this.targetService = targetService;
435
- this.options = options;
436
- this.moduleName = "bucket";
437
- this.primaryField = "title";
438
- this.insertions = [];
439
- this.updations = [];
440
- this.deletions = [];
441
- }
442
- async initialize() {
443
- const synchronizers = [];
444
- const sourceBuckets = await getFilteredResources("bucket", this.sourceService, this.options.bucketIds);
445
- for (const bucket of sourceBuckets) {
446
- synchronizers.push(new BucketDataSynchronizer(this.sourceService, this.targetService, bucket));
447
- }
448
- return synchronizers;
449
- }
450
- async analyze() {
451
- console.log();
452
- const sourceBuckets = await spin({
453
- text: "Fetching buckets from source instance",
454
- op: () => getFilteredResources("bucket", this.sourceService, this.options.bucketIds)
455
- });
456
- const targetBuckets = await spin({
457
- text: "Fetching buckets from target instance",
458
- op: () => getFilteredResources("bucket", this.targetService, this.options.bucketIds)
459
- });
460
- const decider = new ResourceGroupComparisor(sourceBuckets, targetBuckets);
461
- this.insertions = decider.insertions();
462
- this.updations = decider.updations();
463
- this.deletions = decider.deletions();
464
- return {
465
- insertions: this.insertions,
466
- updations: this.updations,
467
- deletions: this.deletions
468
- };
469
- }
470
- async synchronize() {
471
- console.log();
472
- const insertPromiseFactories = this.insertions.map(bucket => () => this.targetService.post("bucket", bucket).catch(e => handleRejection({
473
- action: "insert",
474
- objectName: this.getDisplayableModuleName() + " " + bucket.title,
475
- e
476
- })));
477
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting buckets to the target instance");
478
- const updatePromiseFactories = this.updations.map(bucket => () => this.targetService.put(`bucket/${bucket._id}`, bucket).catch(e => handleRejection({
479
- action: "update",
480
- objectName: this.getDisplayableModuleName() + " " + bucket.title,
481
- e
482
- })));
483
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating buckets on the target instance");
484
- const deletePromiseFactories = this.deletions.map(bucket => () => this.targetService.delete(`bucket/${bucket._id}`).catch(e => handleRejection({
485
- action: "delete",
486
- objectName: bucket.title,
487
- e
488
- })));
489
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting bucket from the target instance");
490
- }
491
- getDisplayableModuleName() {
492
- return this.moduleName;
493
- }
494
- }
495
- export class ApikeySynchronizer {
496
- constructor(sourceService, targetService, options) {
497
- this.sourceService = sourceService;
498
- this.targetService = targetService;
499
- this.options = options;
500
- this.moduleName = "apikey";
501
- this.primaryField = "name";
502
- this.insertions = [];
503
- this.updations = [];
504
- this.deletions = [];
505
- }
506
- initialize() {
507
- return Promise.resolve([]);
508
- }
509
- async analyze() {
510
- console.log();
511
- const sourceApikeys = await spin({
512
- text: "Fetching apikeys from source instance",
513
- op: () => getFilteredResources("passport/apikey", this.sourceService, this.options.apikeyIds, true)
514
- });
515
- const targetApikeys = await spin({
516
- text: "Fetching apikeys from target instance",
517
- op: () => getFilteredResources("passport/apikey", this.targetService, this.options.apikeyIds, true)
518
- });
519
- const decider = new ResourceGroupComparisor(sourceApikeys, targetApikeys);
520
- this.insertions = decider.insertions();
521
- this.updations = decider.updations();
522
- this.deletions = decider.deletions();
523
- return {
524
- insertions: this.insertions,
525
- updations: this.updations,
526
- deletions: this.deletions
527
- };
528
- }
529
- async synchronize() {
530
- console.log();
531
- const insertPromiseFactories = this.insertions.map(apikey => () => {
532
- const insertRejectionHandler = e => handleRejection({
533
- action: "insert",
534
- objectName: this.getDisplayableModuleName() + " " + apikey.name,
535
- e
536
- });
537
- const policies = [...apikey.policies];
538
- delete apikey.policies;
539
- const apikeyInsertPromise = this.targetService
540
- .post("passport/apikey", apikey)
541
- .catch(e => insertRejectionHandler(e));
542
- return apikeyInsertPromise.then(() => {
543
- const policyAttachPromises = policies.map(policy => this.targetService
544
- .put(`passport/apikey/${apikey._id}/policy/${policy}`)
545
- .catch(e => insertRejectionHandler(e)));
546
- return Promise.all(policyAttachPromises);
547
- });
548
- });
549
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting apikeys to the target instance");
550
- const updatePromiseFactories = this.updations.map(apikey => () => {
551
- const rejectionHandler = e => {
552
- handleRejection({
553
- action: "update",
554
- objectName: this.getDisplayableModuleName() + " " + apikey.name,
555
- e
556
- });
557
- };
558
- const policies = [...apikey.policies];
559
- delete apikey.policies;
560
- const apikeyDeletePromise = this.targetService
561
- .delete(`passport/apikey/${apikey._id}`)
562
- .catch(e => rejectionHandler(e));
563
- const apikeyInsertPromise = apikeyDeletePromise.then(() => this.targetService.post(`passport/apikey`, apikey).catch(e => rejectionHandler(e)));
564
- return apikeyInsertPromise.then(() => {
565
- const policyAttachPromises = policies.map(policy => this.targetService
566
- .put(`passport/apikey/${apikey._id}/policy/${policy}`)
567
- .catch(e => rejectionHandler(e)));
568
- return Promise.all(policyAttachPromises);
569
- });
570
- });
571
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating apikeys on the target instance");
572
- const deletePromiseFactories = this.deletions.map(apikey => () => this.targetService.delete(`passport/apikey/${apikey._id}`).catch(e => handleRejection({
573
- action: "delete",
574
- objectName: apikey.name,
575
- e
576
- })));
577
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting apikeys from the target instance");
578
- }
579
- getDisplayableModuleName() {
580
- return this.moduleName;
581
- }
582
- }
583
- export class PolicySynchronizer {
584
- constructor(sourceService, targetService, options) {
585
- this.sourceService = sourceService;
586
- this.targetService = targetService;
587
- this.options = options;
588
- this.moduleName = "policy";
589
- this.primaryField = "name";
590
- this.insertions = [];
591
- this.updations = [];
592
- this.deletions = [];
593
- }
594
- initialize() {
595
- return Promise.resolve([]);
596
- }
597
- async analyze() {
598
- console.log();
599
- const sourcePolicies = await spin({
600
- text: "Fetching policies from source instance",
601
- op: () => getFilteredResources("passport/policy", this.sourceService, this.options.policyIds, true)
602
- });
603
- const targetPolicies = await spin({
604
- text: "Fetching policies from target instance",
605
- op: () => getFilteredResources("passport/policy", this.targetService, this.options.policyIds, true)
606
- });
607
- const decider = new ResourceGroupComparisor(sourcePolicies, targetPolicies);
608
- this.insertions = decider.insertions();
609
- this.updations = decider.updations();
610
- this.deletions = decider.deletions();
611
- return {
612
- insertions: this.insertions,
613
- updations: this.updations,
614
- deletions: this.deletions
615
- };
616
- }
617
- async synchronize() {
618
- console.log();
619
- const insertPromiseFactories = this.insertions.map(policy => () => {
620
- delete policy.system;
621
- return this.targetService.post("passport/policy", policy).catch(e => handleRejection({
622
- action: "insert",
623
- objectName: this.getDisplayableModuleName() + " " + policy.name,
624
- e
625
- }));
626
- });
627
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting policies to the target instance");
628
- const updatePromiseFactories = this.updations.map(policy => () => {
629
- delete policy.system;
630
- return this.targetService.put(`passport/policy/${policy._id}`, policy).catch(e => handleRejection({
631
- action: "update",
632
- objectName: this.getDisplayableModuleName() + " " + policy.name,
633
- e
634
- }));
635
- });
636
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating policies on the target instance");
637
- const deletePromiseFactories = this.deletions.map(policy => () => this.targetService.delete(`passport/policy/${policy._id}`).catch(e => handleRejection({
638
- action: "delete",
639
- objectName: policy.name,
640
- e
641
- })));
642
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting policies from the target instance");
643
- }
644
- getDisplayableModuleName() {
645
- return this.moduleName;
646
- }
647
- }
648
- export class EnvironmentVariableSynchronizer {
649
- constructor(sourceService, targetService, options) {
650
- this.sourceService = sourceService;
651
- this.targetService = targetService;
652
- this.options = options;
653
- this.moduleName = "env-var";
654
- this.primaryField = "key";
655
- this.insertions = [];
656
- this.updations = [];
657
- this.deletions = [];
658
- }
659
- initialize() {
660
- return Promise.resolve([]);
661
- }
662
- async analyze() {
663
- console.log();
664
- const sourceEnvVars = await spin({
665
- text: "Fetching env vars from source instance",
666
- op: () => getFilteredResources("env-var", this.sourceService, this.options.envVarIds)
667
- });
668
- const targetEnvVars = await spin({
669
- text: "Fetching env vars from target instance",
670
- op: () => getFilteredResources("env-var", this.targetService, this.options.envVarIds)
671
- });
672
- const decider = new ResourceGroupComparisor(sourceEnvVars, targetEnvVars);
673
- this.insertions = decider.insertions();
674
- this.updations = decider.updations();
675
- this.deletions = decider.deletions();
676
- return {
677
- insertions: this.insertions,
678
- updations: this.updations,
679
- deletions: this.deletions
680
- };
681
- }
682
- async synchronize() {
683
- console.log();
684
- const insertPromiseFactories = this.insertions.map(envVar => () => this.targetService.post("env-var", envVar).catch(e => handleRejection({
685
- action: "insert",
686
- objectName: this.getDisplayableModuleName() + " " + envVar.key,
687
- e
688
- })));
689
- await spinUntilPromiseEnd(insertPromiseFactories, "Inserting env vars to the target instance");
690
- const updatePromiseFactories = this.updations.map(envVar => () => this.targetService.put(`env-var/${envVar._id}`, envVar).catch(e => handleRejection({
691
- action: "update",
692
- objectName: this.getDisplayableModuleName() + " " + envVar.key,
693
- e
694
- })));
695
- await spinUntilPromiseEnd(updatePromiseFactories, "Updating env vars on the target instance");
696
- const deletePromiseFactories = this.deletions.map(envVar => () => this.targetService.delete(`env-var/${envVar._id}`).catch(e => handleRejection({
697
- action: "delete",
698
- objectName: envVar.key,
699
- e
700
- })));
701
- await spinUntilPromiseEnd(deletePromiseFactories, "Deleting env vars from the target instance");
702
- }
703
- getDisplayableModuleName() {
704
- return this.moduleName;
705
- }
706
- }
707
- function printActions({ insertions, updations, deletions, field, moduleName }) {
708
- const joinActions = (actions) => {
709
- const MAX_LINES = 20;
710
- actions = actions.map(a => `- ${a[field]}`);
711
- if (actions.length > MAX_LINES) {
712
- actions = actions.slice(0, MAX_LINES);
713
- actions.push("...more");
714
- }
715
- return actions.join("\n");
716
- };
717
- console.log();
718
- console.log(`----- ${moduleName.toUpperCase()} -----`);
719
- console.log(`\n* Found ${bold(insertions.length)} objects to ${bold("insert")}:
720
- ${joinActions(insertions)}`);
721
- console.log(`\n* Found ${bold(updations.length)} objects to ${bold("update")}:
722
- ${joinActions(updations)}`);
723
- console.log(`\n* Found ${bold(deletions.length)} objects to ${bold("delete")}:
724
- ${joinActions(deletions)}`);
725
- }
726
- export var SendingPromiseStrategy;
727
- (function (SendingPromiseStrategy) {
728
- SendingPromiseStrategy[SendingPromiseStrategy["All"] = 0] = "All";
729
- SendingPromiseStrategy[SendingPromiseStrategy["OneByOne"] = 1] = "OneByOne";
730
- SendingPromiseStrategy[SendingPromiseStrategy["Batch"] = 2] = "Batch";
731
- SendingPromiseStrategy[SendingPromiseStrategy["Retry"] = 3] = "Retry";
732
- })(SendingPromiseStrategy || (SendingPromiseStrategy = {}));
733
- function spinUntilPromiseEnd(promiseFactories, label) {
734
- if (!promiseFactories.length) {
735
- return;
736
- }
737
- return spin({
738
- text: label,
739
- op: async (spinner) => {
740
- let progress = 0;
741
- let count = 0;
742
- const concurrency = Math.min(promiseFactories.length, CONCURRENCY_LIMIT);
743
- let batchPromises = [];
744
- for (let i = 0; i < promiseFactories.length; i++) {
745
- batchPromises.push(promiseFactories[i]);
746
- if (batchPromises.length == concurrency || i == promiseFactories.length - 1) {
747
- await Promise.all(batchPromises.map(b => b()));
748
- batchPromises = [];
749
- }
750
- count++;
751
- progress = (100 / promiseFactories.length) * count;
752
- spinner.text = `${label} (%${Math.round(progress)})`;
753
- }
754
- return;
755
- }
756
- });
757
- }
758
- function handleRejection({ action, objectName, e }) {
759
- const errorMessage = returnErrorMessage(e);
760
- const msg = `
761
- Failed to ${action} ${bold(objectName)}.
762
- ${errorMessage}`;
763
- if (IGNORE_ERRORS) {
764
- return console.warn(msg);
765
- }
766
- else {
767
- return Promise.reject(msg);
768
- }
769
- }
770
- function returnErrorMessage(e) {
771
- return e.data ? e.data.message || e.data : e;
772
- }
773
- function isNotFoundException(e) {
774
- const code = 404;
775
- return e.status == code || e.statusCode == code || (e.data && e.data.statusCode == code);
776
- }
777
- async function getFilteredResources(path, service, ids, paginate) {
778
- const res = await service.get(path);
779
- const resources = paginate ? res.data : res;
780
- return ids ? resources.filter(resource => ids.includes(resource._id)) : resources;
781
- }
782
- function transformIDs(ids) {
783
- return ids === "string" ? ids.split(",") : [];
784
- }
785
- //# sourceMappingURL=sync.js.map