@kumori/aurora-backend-handler 1.0.40 → 1.0.42

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.
@@ -7,7 +7,6 @@ import {
7
7
  makeGlobalWebSocketRequest,
8
8
  } from "../websocket-manager";
9
9
  import { Link, Notification, Service } from "@kumori/aurora-interfaces";
10
- import { Revision } from "@kumori/aurora-interfaces/interfaces/revision-interface";
11
10
  let pendingLinks = new Map<string, Link[]>();
12
11
  /**
13
12
  * Function to deploy a service
@@ -16,13 +15,13 @@ let pendingLinks = new Map<string, Link[]>();
16
15
  export const deployService = async (data: Service, token: string) => {
17
16
  try {
18
17
  const url = new URL(
19
- `${environment.apiServer.baseUrl}/api/${environment.apiServer.apiVersion}/tenant/${data.tenant}/service/${data.name}`
18
+ `${environment.apiServer.baseUrl}/api/${environment.apiServer.apiVersion}/tenant/${data.tenant}/service/${data.name}`,
20
19
  );
21
20
  url.searchParams.append("dryrun", "false");
22
21
  url.searchParams.append("accept", "true");
23
22
  url.searchParams.append("wait", "30000");
24
23
  url.searchParams.append("validate", "true");
25
- if(data.dsl){
24
+ if (data.dsl) {
26
25
  url.searchParams.append("dsl", "true");
27
26
  }
28
27
  if (data.serviceData) {
@@ -33,7 +32,7 @@ export const deployService = async (data: Service, token: string) => {
33
32
  JSON.stringify({
34
33
  targetAccount: data.account,
35
34
  targetEnvironment: data.environment,
36
- })
35
+ }),
37
36
  );
38
37
  formData.append("labels", JSON.stringify({ project: data.project }));
39
38
  formData.append("comment", " ");
@@ -48,7 +47,7 @@ export const deployService = async (data: Service, token: string) => {
48
47
  const jsonResponse = await response.json();
49
48
 
50
49
  const isTimeout = jsonResponse?.events?.some(
51
- (event: any) => event.content === "_timeout_"
50
+ (event: any) => event.content === "_timeout_",
52
51
  );
53
52
 
54
53
  if (isTimeout) {
@@ -81,7 +80,7 @@ export const deployService = async (data: Service, token: string) => {
81
80
  const jsonResponse = await response.json();
82
81
 
83
82
  const isTimeout = jsonResponse?.events?.some(
84
- (event: any) => event.content === "_timeout_"
83
+ (event: any) => event.content === "_timeout_",
85
84
  );
86
85
 
87
86
  if (isTimeout) {
@@ -111,7 +110,7 @@ export const redeployService = async (data: Service) => {
111
110
  try {
112
111
  const formData = await deployServiceHelper(data);
113
112
  const url = new URL(
114
- `${environment.apiServer.baseUrl}/api/${environment.apiServer.apiVersion}/tenant/${data.tenant}/service/${data.name}/revision/${data.currentRevision}`
113
+ `${environment.apiServer.baseUrl}/api/${environment.apiServer.apiVersion}/tenant/${data.tenant}/service/${data.name}/revision/${data.currentRevision}`,
115
114
  );
116
115
  url.searchParams.append("dryrun", "false");
117
116
  url.searchParams.append("accept", "true");
@@ -130,7 +129,7 @@ export const redeployService = async (data: Service) => {
130
129
  const jsonResponse = await response.json();
131
130
 
132
131
  const isTimeout = jsonResponse?.events?.some(
133
- (event: any) => event.content === "_timeout_"
132
+ (event: any) => event.content === "_timeout_",
134
133
  );
135
134
 
136
135
  if (isTimeout) {
@@ -163,7 +162,7 @@ export const deleteService = async (data: Service, security: string) => {
163
162
  deleteBody,
164
163
  30000,
165
164
  "DELETE",
166
- data.name
165
+ data.name,
167
166
  );
168
167
  return response;
169
168
  } catch (err) {
@@ -188,7 +187,7 @@ export const restartService = async (data: Service, security: string) => {
188
187
  restartBody,
189
188
  30000,
190
189
  "RESTART",
191
- data.name
190
+ data.name,
192
191
  );
193
192
 
194
193
  const updatedService: Service = {
@@ -211,27 +210,33 @@ export const restartService = async (data: Service, security: string) => {
211
210
  const generateLinkBody = (data: Service, link: Link) => {
212
211
  const originInClient = data.clientChannels.find(
213
212
  (channel) =>
214
- channel.name === link.originChannel || channel.from === link.originChannel
213
+ channel.name === link.originChannel ||
214
+ channel.from === link.originChannel,
215
215
  );
216
216
  const originInServer = data.serverChannels.find(
217
217
  (channel) =>
218
- channel.name === link.originChannel || channel.from === link.originChannel
218
+ channel.name === link.originChannel ||
219
+ channel.from === link.originChannel,
219
220
  );
220
221
  const originInDuplex = data.duplexChannels.find(
221
222
  (channel) =>
222
- channel.name === link.originChannel || channel.from === link.originChannel
223
+ channel.name === link.originChannel ||
224
+ channel.from === link.originChannel,
223
225
  );
224
226
  const targetInClient = data.clientChannels.find(
225
227
  (channel) =>
226
- channel.name === link.targetChannel || channel.from === link.targetChannel
228
+ channel.name === link.targetChannel ||
229
+ channel.from === link.targetChannel,
227
230
  );
228
231
  const targetInServer = data.serverChannels.find(
229
232
  (channel) =>
230
- channel.name === link.targetChannel || channel.from === link.targetChannel
233
+ channel.name === link.targetChannel ||
234
+ channel.from === link.targetChannel,
231
235
  );
232
236
  const targetInDuplex = data.duplexChannels.find(
233
237
  (channel) =>
234
- channel.name === link.targetChannel || channel.from === link.targetChannel
238
+ channel.name === link.targetChannel ||
239
+ channel.from === link.targetChannel,
235
240
  );
236
241
 
237
242
  let linkBody;
@@ -291,7 +296,7 @@ const generateLinkBody = (data: Service, link: Link) => {
291
296
  };
292
297
  } else {
293
298
  console.warn(
294
- `No se encontraron canales para el enlace: origin=${link.origin}, target=${link.target}`
299
+ `No se encontraron canales para el enlace: origin=${link.origin}, target=${link.target}`,
295
300
  );
296
301
  linkBody = {
297
302
  client_tenant: data.tenant,
@@ -319,7 +324,7 @@ export const linkPendingServices = async (service: Service, token: string) => {
319
324
  linkBody,
320
325
  30000,
321
326
  "LINK",
322
- service.name
327
+ service.name,
323
328
  );
324
329
 
325
330
  const notification: Notification = {
@@ -353,7 +358,7 @@ export const linkPendingServices = async (service: Service, token: string) => {
353
358
  };
354
359
  eventHelper.notification.publish.creation(notification);
355
360
  }
356
- })
361
+ }),
357
362
  );
358
363
  }
359
364
  };
@@ -375,7 +380,7 @@ export const requestRevisionData = async (service: Service, token: string) => {
375
380
  "GET_REVISION",
376
381
  service.name,
377
382
  "service",
378
- service
383
+ service,
379
384
  );
380
385
  return response;
381
386
  } catch (err) {
@@ -386,7 +391,7 @@ export const requestRevisionData = async (service: Service, token: string) => {
386
391
  export const updateService = async (
387
392
  data: Service,
388
393
  token: string,
389
- scale?: number
394
+ scale?: number,
390
395
  ) => {
391
396
  try {
392
397
  await initializeGlobalWebSocketClient(token);
@@ -402,132 +407,178 @@ export const updateService = async (
402
407
  // pendingLinks.set(data.name, newLinksToCreate);
403
408
  // await linkPendingServices(serviceWithNewLinks, token);
404
409
  // }
405
-
406
- const parameterObject: Record<string, any> = {};
407
- if (data.parameters && data.parameters.length > 0) {
408
- data.parameters.forEach((param) => {
409
- const key = param.configKey || param.name;
410
- const paramType = param.type?.toLowerCase();
411
- if (paramType === "number" || paramType === "integer") {
412
- parameterObject[key] = Number(param.value) || 0;
413
- } else if (paramType === "boolean") {
414
- parameterObject[key] = param.value === "true";
415
- } else {
416
- parameterObject[key] = param.value;
417
- }
418
- });
410
+ const url = new URL(
411
+ `${environment.apiServer.baseUrl}/api/${environment.apiServer.apiVersion}/tenant/${data.tenant}/service/${data.name}/revision/${data.currentRevision}`,
412
+ );
413
+ if (data.dsl) {
414
+ url.searchParams.append("dsl", "true");
419
415
  }
420
-
421
- const resourceObject: Record<string, any> = {};
422
- if (data.resources && data.resources.length > 0) {
423
- data.resources.forEach((resource) => {
424
- if (resource.type === "volume") {
425
- resourceObject[resource.name] = {
426
- volume: {
427
- kind: "storage",
428
- size: parseInt(resource.value) || 1,
429
- unit: "G",
430
- type: resource.kind,
431
- },
432
- };
433
- } else if (resource.type === "secret") {
434
- resourceObject[resource.name] = {
435
- secret: `${data.tenant}/${resource.value}`,
436
- };
437
- } else if (resource.type === "domain") {
438
- resourceObject[resource.name] = {
439
- domain: `${data.tenant}/${resource.value}`,
440
- };
441
- } else if (resource.type === "ca") {
442
- resourceObject[resource.name] = {
443
- ca: `${data.tenant}/${resource.value}`,
444
- };
445
- } else if (resource.type === "certificate") {
446
- resourceObject[resource.name] = {
447
- certificate: `${data.tenant}/${resource.value}`,
448
- };
449
- } else if (resource.type === "port") {
450
- resourceObject[resource.name] = {
451
- port: `${data.tenant}/${resource.value}`,
452
- };
453
- }
416
+ if (data.serviceData) {
417
+ const formData = new FormData();
418
+ formData.append("type", "update-bundle");
419
+ formData.append("bundle", data.serviceData);
420
+ formData.append(
421
+ "meta",
422
+ JSON.stringify({
423
+ targetAccount: data.account,
424
+ targetEnvironment: data.environment,
425
+ }),
426
+ );
427
+ formData.append("labels", JSON.stringify({ project: data.project }));
428
+ formData.append("comment", " ");
429
+ const response = await fetch(url.toString(), {
430
+ method: "POST",
431
+ body: formData,
454
432
  });
455
- }
456
-
457
- let previousRevision = 1;
458
- if (data.currentRevision) {
459
- previousRevision = parseInt(data.currentRevision.toString(), 10);
460
- if (isNaN(previousRevision)) {
461
- console.warn("currentRevision is not a valid number, using 1");
462
- previousRevision = 1;
433
+ if (!response.ok) {
434
+ throw new Error(`HTTP error! status: ${response.status}`);
463
435
  }
464
- }
465
- else{
466
- previousRevision = getLatestRevision(data.revisions) || 1;
467
- }
468
436
 
469
- const scaleConfig: any = {};
470
- if (data.role && data.role.length > 0) {
471
- scaleConfig.detail = {};
472
- data.role.forEach((role) => {
473
- scaleConfig.detail[role.name] = {
474
- hsize: role.hsize || scale || data.minReplicas || 1,
475
- };
476
- });
437
+ const jsonResponse = await response.json();
438
+
439
+ const isTimeout = jsonResponse?.events?.some(
440
+ (event: any) => event.content === "_timeout_",
441
+ );
442
+
443
+ if (isTimeout) {
444
+ console.error("Timeout en la petición:", {
445
+ isOk: false,
446
+ code: "TIMEOUT",
447
+ error: "_timeout_",
448
+ });
449
+ }
477
450
  } else {
478
- scaleConfig.hsize = scale || data.minReplicas || 1;
479
- }
480
- const meta = {
481
- scaling: {
482
- simple:
483
- data.role?.reduce((acc, role) => {
484
- if (role.scalling && role.name) {
485
- acc[role.name] = {
486
- scale_up: {
487
- cpu: Math.min(parseInt(role.scalling.cpu.up) || 0, 100),
488
- memory: Math.min(parseInt(role.scalling.memory.up) || 0, 100),
489
- },
490
- scale_down: {
491
- cpu: Math.min(parseInt(role.scalling.cpu.down) || 0, 100),
492
- memory: Math.min(
493
- parseInt(role.scalling.memory.down) || 0,
494
- 100
495
- ),
496
- },
497
- hysteresis: parseInt(role.scalling.histeresys) || 0,
498
- min_replicas: role.scalling.instances.min || 0,
499
- max_replicas: role.scalling.instances.max || 0,
500
- };
501
- }
502
- return acc;
503
- }, {} as Record<string, any>) || {},
504
- },
505
- };
451
+ const parameterObject: Record<string, any> = {};
452
+ if (data.parameters && data.parameters.length > 0) {
453
+ data.parameters.forEach((param) => {
454
+ const key = param.configKey || param.name;
455
+ const paramType = param.type?.toLowerCase();
456
+ if (paramType === "number" || paramType === "integer") {
457
+ parameterObject[key] = Number(param.value) || 0;
458
+ } else if (paramType === "boolean") {
459
+ parameterObject[key] = param.value === "true";
460
+ } else {
461
+ parameterObject[key] = param.value;
462
+ }
463
+ });
464
+ }
506
465
 
507
- const updateBody = {
508
- spec: {
509
- type: "update-config",
510
- comment: "Service configuration update",
511
- config: {
512
- parameter: parameterObject,
513
- resource: resourceObject,
514
- resilience: 0,
515
- scale: scaleConfig,
466
+ const resourceObject: Record<string, any> = {};
467
+ if (data.resources && data.resources.length > 0) {
468
+ data.resources.forEach((resource) => {
469
+ if (resource.type === "volume") {
470
+ resourceObject[resource.name] = {
471
+ volume: {
472
+ kind: "storage",
473
+ size: parseInt(resource.value) || 1,
474
+ unit: "G",
475
+ type: resource.kind,
476
+ },
477
+ };
478
+ } else if (resource.type === "secret") {
479
+ resourceObject[resource.name] = {
480
+ secret: `${data.tenant}/${resource.value}`,
481
+ };
482
+ } else if (resource.type === "domain") {
483
+ resourceObject[resource.name] = {
484
+ domain: `${data.tenant}/${resource.value}`,
485
+ };
486
+ } else if (resource.type === "ca") {
487
+ resourceObject[resource.name] = {
488
+ ca: `${data.tenant}/${resource.value}`,
489
+ };
490
+ } else if (resource.type === "certificate") {
491
+ resourceObject[resource.name] = {
492
+ certificate: `${data.tenant}/${resource.value}`,
493
+ };
494
+ } else if (resource.type === "port") {
495
+ resourceObject[resource.name] = {
496
+ port: `${data.tenant}/${resource.value}`,
497
+ };
498
+ }
499
+ });
500
+ }
501
+
502
+ let previousRevision = 1;
503
+ if (data.currentRevision) {
504
+ previousRevision = parseInt(data.currentRevision.toString(), 10);
505
+ if (isNaN(previousRevision)) {
506
+ console.warn("currentRevision is not a valid number, using 1");
507
+ previousRevision = 1;
508
+ }
509
+ } else {
510
+ previousRevision = getLatestRevision(data.revisions) || 1;
511
+ }
512
+
513
+ const scaleConfig: any = {};
514
+ if (data.role && data.role.length > 0) {
515
+ scaleConfig.detail = {};
516
+ data.role.forEach((role) => {
517
+ scaleConfig.detail[role.name] = {
518
+ hsize: role.hsize || scale || data.minReplicas || 1,
519
+ };
520
+ });
521
+ } else {
522
+ scaleConfig.hsize = scale || data.minReplicas || 1;
523
+ }
524
+ const meta = {
525
+ scaling: {
526
+ simple:
527
+ data.role?.reduce(
528
+ (acc, role) => {
529
+ if (role.scalling && role.name) {
530
+ acc[role.name] = {
531
+ scale_up: {
532
+ cpu: Math.min(parseInt(role.scalling.cpu.up) || 0, 100),
533
+ memory: Math.min(
534
+ parseInt(role.scalling.memory.up) || 0,
535
+ 100,
536
+ ),
537
+ },
538
+ scale_down: {
539
+ cpu: Math.min(parseInt(role.scalling.cpu.down) || 0, 100),
540
+ memory: Math.min(
541
+ parseInt(role.scalling.memory.down) || 0,
542
+ 100,
543
+ ),
544
+ },
545
+ hysteresis: parseInt(role.scalling.histeresys) || 0,
546
+ min_replicas: role.scalling.instances.min || 0,
547
+ max_replicas: role.scalling.instances.max || 0,
548
+ };
549
+ }
550
+ return acc;
551
+ },
552
+ {} as Record<string, any>,
553
+ ) || {},
516
554
  },
517
- meta: meta,
518
- },
519
- tenant: data.tenant,
520
- service: data.name,
521
- previous: previousRevision,
522
- };
555
+ };
523
556
 
524
- const response = await makeGlobalWebSocketRequest(
525
- "revision:update_revision",
526
- updateBody,
527
- 30000,
528
- "UPDATE_CONFIG",
529
- data.name
530
- );
557
+ const updateBody = {
558
+ spec: {
559
+ type: "update-config",
560
+ comment: "Service configuration update",
561
+ config: {
562
+ parameter: parameterObject,
563
+ resource: resourceObject,
564
+ resilience: 0,
565
+ scale: scaleConfig,
566
+ },
567
+ meta: meta,
568
+ },
569
+ tenant: data.tenant,
570
+ service: data.name,
571
+ previous: previousRevision,
572
+ };
573
+
574
+ const response = await makeGlobalWebSocketRequest(
575
+ "revision:update_revision",
576
+ updateBody,
577
+ 30000,
578
+ "UPDATE_CONFIG",
579
+ data.name,
580
+ );
581
+ }
531
582
 
532
583
  const updatedService: Service = {
533
584
  ...data,
@@ -578,7 +629,7 @@ export const updateService = async (
578
629
  };
579
630
  export const unlinkServices = async (service: Service, token: string) => {
580
631
  const deleteLinks: Link[] = service.links.filter(
581
- (link) => link.delete === true
632
+ (link) => link.delete === true,
582
633
  );
583
634
  if (deleteLinks.length > 0) {
584
635
  await Promise.all(
@@ -593,7 +644,7 @@ export const unlinkServices = async (service: Service, token: string) => {
593
644
  unlinkBody,
594
645
  30000,
595
646
  "UNLINK",
596
- service.name
647
+ service.name,
597
648
  );
598
649
 
599
650
  const unlinkNotification: Notification = {
@@ -629,7 +680,7 @@ export const unlinkServices = async (service: Service, token: string) => {
629
680
  };
630
681
  eventHelper.notification.publish.creation(notification);
631
682
  }
632
- })
683
+ }),
633
684
  );
634
685
  }
635
686
  };
@@ -654,7 +705,7 @@ export const updateServiceLinks = async (link: Link, token: string) => {
654
705
  linkBody,
655
706
  30000,
656
707
  "UNLINK",
657
- link.origin
708
+ link.origin,
658
709
  );
659
710
 
660
711
  // const unlinkNotification: Notification = {
@@ -700,7 +751,7 @@ export const updateServiceLinks = async (link: Link, token: string) => {
700
751
  linkBody,
701
752
  30000,
702
753
  "LINK",
703
- link.origin
754
+ link.origin,
704
755
  );
705
756
 
706
757
  const notification: Notification = {
@@ -756,7 +807,7 @@ export const changeRevision = async (data: Service, token: string) => {
756
807
  revisionBody,
757
808
  30000,
758
809
  "UPDATE_REVISION",
759
- data.name
810
+ data.name,
760
811
  );
761
812
  const notification: Notification = {
762
813
  type: "success",
@@ -788,10 +839,10 @@ export const changeRevision = async (data: Service, token: string) => {
788
839
  eventHelper.notification.publish.creation(notification);
789
840
  }
790
841
  };
791
- function getLatestRevision(revisions: Revision[]): number | null {
842
+ function getLatestRevision(revisions: string[]): number | null {
792
843
  if (!revisions || revisions.length === 0) {
793
844
  return null;
794
845
  }
795
-
796
- return Math.max(...revisions.map((revision) => Number(revision.id)));
797
- }
846
+
847
+ return Math.max(...revisions.map(Number));
848
+ }