-
Notifications
You must be signed in to change notification settings - Fork 37
/
packs-integrations.js
539 lines (507 loc) · 21.1 KB
/
packs-integrations.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
const { api, callRateLimitAPI } = require("../src/services/api");
const { packTypeNames, addOnTypes, layerTypes } = require("../src/constants/packs");
const packDescription = require("../static/packs-data/packs_information.json");
const { coerce, rcompare } = require("semver");
const path = require("path");
const mime = require("mime-types");
const { setTimeout } = require("timers/promises");
const BASE_URL = require("../static/scripts/constants.js").BASE_URL;
const fetch = require("node-fetch");
const excludeList = require("../static/packs-data/exclude_packs.json");
const { existsSync, promises, open, mkdirSync, writeFile, close, createWriteStream } = require("node:fs");
import logger from "@docusaurus/logger";
const filterLimit = 100; //Limit for fetching the packs from the Palette API
const dirname = ".docusaurus/packs-integrations/";
const logoDirname = "static/img/packs/";
const filename = "api_pack_response.json";
const options = {
headers: {
"Content-Disposition": "attachment",
ApiKey: process.env.PALETTE_API_KEY,
},
};
let counter = 0;
function generateIntegrationData(allContent) {
const packsData = allContent["docusaurus-plugin-content-docs"].default.loadedVersions[0].docs
.filter((doc) => {
return doc.frontMatter.type === "appTier";
})
.map((doc) => {
return { ...doc.frontMatter, slug: doc.slug, id: doc.id };
});
return packsData;
}
function getPackUidMap(registries) {
let generatedReadMeData = {};
registries.forEach((registry) => {
const proccessedPackReadMeDetails = registry.packValues.reduce(
(packValuesMap, packValue) =>
Object.assign(packValuesMap, {
[packValue.packUid]: {
registryUid: registry.registryUid,
readme: packValue.readme,
deprecated: packValue.annotations?.system_state === "deprecated",
},
}),
{}
);
generatedReadMeData = Object.assign(generatedReadMeData, proccessedPackReadMeDetails);
});
return generatedReadMeData;
}
function filterBasedOnRegistriesInConfig(packRegistries, configRepositories) {
//Filter the pack registries based on the selected registries given in the docusaurus config file
return packRegistries.filter((registry) => {
return configRepositories.find((repo) => repo.uid === registry.registryUid);
});
}
function combineAPICustomPackData(packsMData, packsPaletteDetailsData, customPacksData, repositories, logoPathMap) {
const filteredPalattePackDataMap = packsPaletteDetailsData.reduce((acc, packContent) => {
const packName = packContent.name;
//filtered the packs based on the selected registries given in the docusaurus config file
if (
repositories.find((repo) => repo.uid === packContent.registryUid) &&
((packsMData[packName].spec.layer === "addon" && packsMData[packName].spec.addonType) ||
packsMData[packName].spec.layer !== "addon")
) {
if (Object.hasOwnProperty.call(acc, packName)) {
const packValues = acc[packName];
packValues.registries.push(packContent);
} else {
const packData = {
name: packName,
registries: [packContent],
};
acc[packName] = packData;
}
}
return acc;
}, {});
const filteredPalattePackData = Object.values(filteredPalattePackDataMap);
const preferredRegistryUid = repositories?.[0]?.uid;
return filteredPalattePackData.map((packContent) => {
const packName = packContent.name;
if (packsMData[packName]) {
const packMDValue = packsMData[packName];
const preferredRegistry = packMDValue.spec.registries.find((registry) => registry.uid === preferredRegistryUid);
const latestPackVersion = preferredRegistry
? preferredRegistry.latestVersion
: packMDValue.spec.registries[0].latestVersion;
const packType = packMDValue.spec.layer === "addon" ? packMDValue.spec.addonType : packMDValue.spec.layer;
const layer =
packMDValue.spec.layer === "addon" ? packMDValue.spec.addonType : packTypeNames[packMDValue.spec.layer];
const selectedRegistries = filterBasedOnRegistriesInConfig(packContent.registries, repositories);
const packDetailsMap = getPackUidMap(selectedRegistries);
//union of all the versions of all the supported registries of a pack.
const allSupportedVersions = getAggregatedVersions(selectedRegistries, repositories, packDetailsMap, packName);
return {
name: packName,
title: packMDValue.spec.displayName,
description: customPacksData?.[packName],
packUidMap: packDetailsMap,
cloudTypes: packMDValue.spec.cloudTypes,
type: "integration",
category: [layer],
packType: packType,
logoUrl: logoPathMap[packName],
tags: [],
slug: "/integrations/${packMDValue.spec.name}",
id: "integrations/${packMDValue.spec.name}",
registries: packMDValue.spec.registries.map((registry) => registry.uid),
community: packMDValue.spec.registries[0].annotations?.source === "community",
verified: packMDValue.spec.registries[0].annotations?.source === "spectrocloud",
versions: allSupportedVersions,
// disabled: packMDValue.spec.registries[0].annotations?.disabled === "true",
disabled: packMDValue.spec.registries[0].annotations?.disabled === "true",
deprecated: packMDValue.spec.registries[0].annotations?.system_state === "deprecated",
latestVersion: latestPackVersion,
};
}
});
}
function matchAmbiguousPatch(tag) {
const [major, minor, patch] = tag.split(".");
return major !== undefined && minor !== undefined && patch === "x";
}
export function sanitizeVersion(version) {
let sanitizedVersion = version;
if (version.split(".").length < 3) {
sanitizedVersion = `${version}.0`;
}
return sanitizedVersion
.split(".")
.map((subVersion) => {
if (subVersion.startsWith("0")) {
return subVersion.replace("0", "");
}
return subVersion;
})
.join(".");
}
function sortVersions(tags) {
let sortedVersions = [...tags].sort((pack1, pack2) => {
let version1 = sanitizeVersion(pack1.version);
let version2 = sanitizeVersion(pack2.version);
if (coerce(version1) !== null) {
version1 = coerce(version1).version;
}
if (coerce(version2) !== null) {
version2 = coerce(version2).version;
}
try {
return rcompare(version1, version2, true);
} catch (e) {
return 0;
}
});
return sortedVersions;
}
function getAggregatedVersions(registries, repositories, packUidMap) {
const prefferedRegistryUid = repositories?.[0]?.uid;
//if a pack has multiple registries, then the versions of the pack are aggregated based on the selected registries
//if a same version in multiple registries, the preferred registry is the higher precendence.
const aggregatedTags = registries.reduce((previousVersions, registry) => {
const computedVersions = getComputedVersions(registry.tags);
if (previousVersions.length) {
const versionTags = new Set(previousVersions.map((verssion) => verssion.title));
const computedCommonVersions = computedVersions.filter((version) => versionTags.has(version.title));
if (computedCommonVersions.length) {
//merging the non-overlapping Tags in the previous computed versions and current computing versions
const mergedNonOverlappingParentVersions = [
...computedVersions.filter((version) => !versionTags.has(version.title)),
...previousVersions.filter((version) => !versionTags.has(version.title)),
];
computedCommonVersions.forEach((commonVersion) => {
//Take PreviousVersion Parent version tag from the iterated overlapping tag title
const previousVersiontagdata = previousVersions.find(
(prevVersion) => prevVersion.title === commonVersion.title
);
const previousVersionChildrenSet = new Set(previousVersiontagdata.children.map((verssion) => verssion.title));
const commonComputedChildren = commonVersion.children.filter((child) =>
previousVersionChildrenSet.has(child.title)
);
if (commonComputedChildren.length) {
//merge non overlapping children in each parent version
const mergedNonOverlappingChildren = [
...commonVersion.children.filter((child) => !previousVersionChildrenSet.has(child.title)),
...previousVersiontagdata.children.filter((child) => !previousVersionChildrenSet.has(child.title)),
];
commonComputedChildren.forEach((childComputedVersion) => {
const previousVersionChild = previousVersiontagdata.children.find(
(prevChildVersion) => prevChildVersion.title === childComputedVersion.title
);
const childPreviousVersionPackUid = previousVersionChild.packUid;
const previousVersionRegistryUid = packUidMap[childPreviousVersionPackUid]?.registryUid;
//while doing union of both versions of pack of the registries, not belong to the preferred registry,
//then either of the registry is considered.
if (previousVersionRegistryUid !== prefferedRegistryUid) {
previousVersionChild.packUid = childComputedVersion.packUid;
}
});
//merging non-overlapping children with the previous version children
previousVersiontagdata.children = [...previousVersiontagdata.children, ...mergedNonOverlappingChildren];
} else {
previousVersiontagdata.children = [...previousVersiontagdata.children, ...commonVersion.children];
}
});
//merging the non-overlapping tags with the previous Versions
previousVersions = [...previousVersions, ...mergedNonOverlappingParentVersions];
} else {
previousVersions = [...previousVersions, ...computedVersions];
}
} else {
previousVersions = [...previousVersions, ...computedVersions];
}
return previousVersions;
}, []);
return aggregatedTags;
}
function getComputedVersions(tags) {
const sortedVersions = sortVersions(tags);
const roots = sortedVersions
.filter((version) => {
return matchAmbiguousPatch(version.tag);
})
.map((version) => {
return {
title: version.tag,
value: version.version,
packUid: version.packUid,
};
});
sortedVersions.forEach((version) => {
const parentTags = version?.parentTags || [];
const parent = parentTags.find(matchAmbiguousPatch);
if (!parent) return;
const parentVersion = roots.find((rootVersion) => rootVersion.title === parent);
if (parentVersion) {
parentVersion.children = parentVersion.children || [];
parentVersion.children.push({
title: version.tag,
value: version.version,
packUid: version.packUid,
});
}
});
return roots;
}
function generateCustomData(packsDescription) {
const generatedCustomData = packsDescription.reduce(
(obj, desc) => Object.assign(obj, { [desc.name]: desc.description }),
{}
);
logger.info("Completed generating custom data description.");
return generatedCustomData;
}
function generateRoutes(packsAllData) {
return packsAllData.map((pack) => {
const parentVersion = pack.versions.find((version) => {
return version.children.find((child) => child.title === pack.latestVersion);
});
let path = `/integrations/packs/${pack.name}/${pack.latestVersion}`;
if (parentVersion && parentVersion.title) {
path = `${path}/${parentVersion.title}`;
}
return {
path: path,
exact: false,
component: "@site/src/components/PacksInformation",
metadata: {
sourceFilePath: "../docs/docs-content/integrations/packs.mdx",
},
data: { name: pack.name, version: pack.latestVersion, parent: parentVersion?.title },
};
});
}
async function fetchPackListItems(queryParams, packDataArr, counter, mappedRepos) {
// Loop through the mappedRepos object and extract all the registries provided in the Docusarus config file
const registryUids = [];
for (let i = 0; i < mappedRepos.length; i++) {
registryUids.push(mappedRepos[i].uid);
}
// Provide the registryUids in the payload to fetch the packs ONLY from registries provided in the Docusarus config file
const payload = { filter: { type: ["spectro", "oci"], registryUid: registryUids } };
const response = await callRateLimitAPI(() => api.post(`/v1/packs/search${queryParams}`, payload));
const tempPackArr = packDataArr.concat(response?.data?.items);
if (response?.data?.listmeta?.continue) {
return fetchPackListItems(
`?limit=${filterLimit}&continue=` + response.data.listmeta.continue,
tempPackArr,
counter,
mappedRepos
);
} else {
return tempPackArr;
}
}
async function mapRepositories(repositories) {
const ociRegistries = await api.get("v1/registries/oci/summary");
const packRegistries = await api.get("v1/registries/pack");
const mergedRegistries = [ociRegistries.data?.items || [], packRegistries.data?.items || []];
const results = mergedRegistries.flat();
const repoMap = repositories.reduce((acc, repository) => {
const repoObj = results.find((repo) => {
return repo.metadata.name === repository;
});
if (repoObj) {
acc.push({ name: repoObj.metadata.name, uid: repoObj.metadata.uid });
}
return acc;
}, []);
return repoMap;
}
async function write(res, packName, logoUrlMap) {
return new Promise((resolve, reject) => {
const type = res.headers.get("Content-Type");
if (mime.extension(type) !== "html") {
const destination = path.resolve(logoDirname, packName);
const fileStream = createWriteStream(`${destination}.${mime.extension(type)}`);
res.body.pipe(fileStream);
res.body.on("error", (err) => {
reject(err);
});
fileStream.on("finish", function () {
resolve();
logoUrlMap[packName] = `${packName}.${mime.extension(type)}`;
});
} else {
reject(`Invalid MIME type received for the logo ${packName}`);
}
});
}
async function getLogoUrl(packsAllData, logoUrlMap) {
for (let j = 0; j < packsAllData.length; j++) {
const { registries, name: packName } = packsAllData[j].spec;
for (let i = 0; i < registries.length; i++) {
const registry = registries[i];
let url = registry.logoUrl || `${BASE_URL}/v1/packs/${registry.latestPackUid}/logo`;
url =
url.startsWith("http://") || url.startsWith("https://")
? url
: url.startsWith("/")
? `${BASE_URL}${url}`
: `${BASE_URL}/${url}`;
if (!Object.prototype.hasOwnProperty.call(logoUrlMap, packName)) {
try {
options.headers["Accept"] = "image/png";
const res = await fetch(url, options);
if (!res.ok) {
throw new Error(`Failed to fetch logo for ${packName} from ${url}. Got status ${res.status}`);
}
await write(res, packName, logoUrlMap);
counter++;
if (counter % 10 === 0) {
await setTimeout(1000);
}
} catch (e) {
logger.error(e);
}
}
}
}
}
// If the plugin is disabled, then the packs and integrations data will not be fetched.
// However, the PDE service packs still need to be loaded.
// Otherwise, errors will be thrown when the PDE service packs are accessed.
// The redirect.js file has logic to redirect pack pages in production to the /integrations/ page.
async function pluginPacksAndIntegrationsData(context, options) {
if (process.env.DISABLE_PACKS_INTEGRATIONS === "true") {
logger.warn(
"The Packs Integrations plugin is disabled. To enable it, set the environment variable DISABLE_PACKS_INTEGRATIONS to false."
);
return {
name: "plugin-packs-integrations",
async allContentLoaded({ allContent, actions }) {
const { setGlobalData } = actions;
const integrationsData = generateIntegrationData(allContent);
setGlobalData({ integrations: integrationsData });
},
};
}
return {
name: "plugin-packs-integrations",
async loadContent() {
const repositories = options.repositories || [];
const mappedRepos = await mapRepositories(repositories);
let apiPackResponse = {};
let isFileExists = false;
if (existsSync(dirname) && existsSync(`${dirname}${filename}`)) {
isFileExists = true;
}
let logoUrlMap = {};
if (!isFileExists) {
if (!existsSync(dirname)) {
mkdirSync(dirname, { recursive: true });
}
logger.info("Fetching the list of packs from the Palette API");
let packDataArr = await fetchPackListItems(`?limit=${filterLimit}`, [], 0, mappedRepos);
// Filter out the packs from the exclude list.
packDataArr = packDataArr.filter((pack) => {
if (excludeList.includes(pack.spec.name)) {
// Only uncomment if debugging is required
// logger.warn(`Pack ${pack.spec.name} is excluded from the list`);
return false;
}
return true;
});
logger.info("Downloading each pack's details and README");
packDataArr = packDataArr.filter((pack) => {
return (
layerTypes.includes(pack.spec.layer) ||
(pack.spec.layer === "addon" && addOnTypes.includes(pack.spec.addonType) && pack.spec.registries.length)
);
});
const packUrl = "v1/packs/";
const packMDMap = new Map();
let apiPacksData = [];
const promisesPackDetails = packDataArr.map((packData) => {
packMDMap[packData.spec.name] = packData;
const cloudType = packData.spec.cloudTypes.includes("all") ? "aws" : packData.spec.cloudTypes[0];
const registryPackData = [];
for (const registry of packData.spec.registries) {
const url = `${packUrl}${packData.spec.name}/registries/${registry.uid}?cloudType=${cloudType}&layer=${packData.spec.layer}`;
registryPackData.push(
callRateLimitAPI(() => {
return api.get(url);
})
);
}
return registryPackData;
});
const flatted = promisesPackDetails.flat();
const results = await Promise.allSettled(flatted);
for (const result of results) {
if (result.status === "fulfilled" && result.value?.data) {
apiPacksData.push(result.value.data);
} else {
logger.error("Failed to fetch the details for the following pack " + result.reason.config.url);
}
}
logger.info("Completed fetching all the packs and their details");
logger.info("Fetching the logo for each pack");
//Fetch logos
if (!existsSync(logoDirname)) {
mkdirSync(logoDirname, { recursive: true });
}
await getLogoUrl(packDataArr, logoUrlMap);
logger.info("Completed fetching all pack logos");
apiPackResponse.apiPacksData = apiPacksData;
apiPackResponse.packMDMap = packMDMap;
apiPackResponse.logoUrlMap = logoUrlMap;
open(`${dirname}${filename}`, "w+", (err, fd) => {
try {
writeFile(`${dirname}${filename}`, JSON.stringify(apiPackResponse), (err) => {
if (err) {
logger.error("An error occurred while writing the JSON file:", err);
}
});
} finally {
close(fd, (err1) => {
if (err1) logger.error("An error occurred while closing the file:", err1);
});
}
});
} else {
try {
const data = await promises.readFile(`${dirname}${filename}`);
apiPackResponse = JSON.parse(data);
} catch (e) {
logger.error("An error occurred while reading the JSON file:", e);
}
}
logger.info(`The number of packs identified is: ${Object.keys(apiPackResponse.packMDMap).length}`);
return {
packsPaletteData: apiPackResponse.packMDMap,
packsPaletteDetailsData: apiPackResponse.apiPacksData,
packsDescription: packDescription,
repositories: mappedRepos,
logoFilesPathMap: apiPackResponse.logoUrlMap,
};
},
async contentLoaded({ content, actions }) {
const { setGlobalData, addRoute } = actions;
const { packsPaletteData, packsPaletteDetailsData, packsDescription, repositories, logoFilesPathMap } = content;
const customPacksData = generateCustomData(packsDescription);
const unionPackData = combineAPICustomPackData(
packsPaletteData,
packsPaletteDetailsData,
customPacksData,
repositories,
logoFilesPathMap
);
const routes = generateRoutes(unionPackData);
logger.info("Completed generating routes for all the packs");
routes.map((route) => addRoute(route));
setGlobalData({ packs: unionPackData, repositories: repositories });
logger.success("Packs data successfully loaded 📦");
},
async allContentLoaded({ allContent, actions }) {
const { setGlobalData } = actions;
const integrationsData = generateIntegrationData(allContent);
setGlobalData({ integrations: integrationsData });
},
};
}
module.exports = {
pluginPacksAndIntegrationsData,
};