-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathgenerate-addons.js
137 lines (118 loc) · 4.31 KB
/
generate-addons.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
const fs = require("fs");
const path = require("path");
const semverCompare = require('semver/functions/rcompare')
const AWS = require('aws-sdk');
const installerVersions = require("../web/src/installers/versions");
const ID = process.env.AWS_ACCESS_KEY_ID;
const SECRET = process.env.AWS_SECRET_ACCESS_KEY;
const BUCKET_NAME = process.env.S3_BUCKET;
const FOLDER = process.env.DIST_FOLDER;
const VERSION_TAG = process.env.VERSION_TAG;
const s3 = new AWS.S3({
accessKeyId: ID,
secretAccessKey: SECRET
});
const uploadFile = (file) => {
const fileName = file.split("/")[1];
const fileContent = fs.readFileSync(file);
let params = {
Bucket: BUCKET_NAME,
Key: FOLDER + "/" + VERSION_TAG + "/" + fileName,
Body: fileContent
};
s3.upload(params, (err, data) => {
if (err) throw err;
console.log("\x1b[32m%s\x1b[0m", "Successfully uploaded " + fileName + " to " + data.Location);
});
params = {
Bucket: BUCKET_NAME,
Key: FOLDER + "/" + fileName,
Body: fileContent
};
s3.upload(params, (err, data) => {
if (err) throw err;
console.log("\x1b[32m%s\x1b[0m", "Successfully uploaded " + fileName + " to " + data.Location);
});
}
const preferredVersions = installerVersions.InstallerVersions;
let addons = [];
let supportedVersions = new Object();
const specDir = "./addons";
fs.readdir(specDir, (err, files) => {
if (err) throw err;
files.forEach((file) => {
let sv = [];
const subDirPath = specDir + "/" + file;
if (fs.statSync(subDirPath).isDirectory()) {
const subFiles = fs.readdirSync(subDirPath);
subFiles.forEach((subFile) => {
const filepath = path.join(subDirPath, subFile);
const sf = fs.statSync(filepath);
if (sf.isDirectory()) {
if (subFile !== "template" && subFile !== "build-images") {
sv.push(subFile)
}
} else if (sf.isFile()) {
if (subFile === "categories.json") {
const content = fs.readFileSync(filepath);
const addonContent = JSON.parse(content);
if (typeof addonContent === "object" && Object.entries(addonContent).length !== 0) {
addons.push(addonContent);
}
}
}
});
// Sorting doesn't work if there is an 'alpha' and 'nightly' version (like in kotsadm) so we have to remove it before sorting
// This is pretty specific to kotsadm so if more add-ons end up with alpha versions we may need to refactor this to support them
const hasAlphaVersion = sv.includes("alpha");
const hasNightlyVersion = sv.includes("nightly");
if (hasAlphaVersion) {
sv.pop();
}
if (hasNightlyVersion) {
sv.pop();
}
// minio tags are not valid semvers
if (file !== "minio") {
sv.sort(semverCompare);
}
if (hasAlphaVersion) {
sv.push("alpha");
}
if (hasNightlyVersion) {
sv.push("nightly");
}
if (preferredVersions[file]) {
sv = preferredVersions[file];
}
sv.unshift("latest");
supportedVersions[file] = sv
}
});
supportedVersions.kubernetes = preferredVersions.kubernetes;
supportedVersions.kubernetes.unshift("latest");
supportedVersions.fio = ["host"]; // fio serves as a signal that the 'fio' host package should be included
// Build JSON files
const addonsFile = {
_comment: `This file is generated, do not change! Last generated on ${new Date()}. To regenerate run 'make generate-addons'`,
addOns: addons
};
const supportVersionsFile = {
_comment: `This file is generated, do not change! Last generated on ${new Date()}. To regenerate run 'make generate-addons'`,
supportedVersions
}
// Write finalized JSON files
fs.writeFile("./addons-gen.json", JSON.stringify(addonsFile), (err) => {
if (err) throw err;
console.log("\x1b[34m%s\x1b[0m", "Add-ons generated:", addonsFile.addOns.length);
console.log("\x1b[32m%s\x1b[0m", "Successfully generated addons-gen.json");
// Upload files to s3
uploadFile("./addons-gen.json");
});
fs.writeFile("./supported-versions-gen.json", JSON.stringify(supportVersionsFile), (err) => {
if (err) throw err;
console.log("\x1b[32m%s\x1b[0m", "Successfully generated supported-versions-gen.json");
// Upload files to s3
uploadFile("./supported-versions-gen.json");
});
});