-
Notifications
You must be signed in to change notification settings - Fork 20
/
run.js
121 lines (110 loc) · 3.39 KB
/
run.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
const fs = require("fs");
const getAzureInstances = require("./azure");
const getAWSInstances = require("./aws");
const getGCPInstances = require("./gcp");
const azureInstances = require("./input/azure.json");
const awsInstances = require("./input/aws.json");
const gcpInstances = "./input/gcp.txt";
const azurePricing = require("./input/azure-pricing.json");
const gcpPricing = require("./input/gcp-pricing.json");
const awsPricing = require("./input/aws-pricing.json");
const { program } = require("commander");
program
.command("crunch")
.option("-o, --output <file name>", "Output file name.", "instances.json")
.option(
"-c, --cloud-providers <providers...>",
"Includes only those providers. Default: all",
"all"
)
.option("-t, --time", "Includes the provisioning time for each compute unit.")
.action((options) => {
const cloudProviders = (
Array.isArray(options.cloudProviders)
? options.cloudProviders
: [options.cloudProviders]
)
.map((it) => it.toLowerCase())
.filter((it) => ["aws", "gcp", "azure", "all"].includes(it));
const instances = (
cloudProviders.includes("all") ? ["aws", "gcp", "azure"] : cloudProviders
)
.reduce((acc, it) => {
switch (it) {
case "gcp":
return [
...acc,
...getGCPInstances(
fs.readFileSync(gcpInstances, "utf-8"),
gcpPricing.gcp_price_list,
!!options.time
),
];
case "aws":
return [
...acc,
...getAWSInstances(
awsInstances.InstanceTypes,
awsPricing,
!!options.time
),
];
case "azure":
return [
...acc,
...getAzureInstances(
azureInstances,
azurePricing.data,
!!options.time
),
];
default:
return acc;
}
}, [])
.reduce((acc, it) => {
acc[it.id] = it;
return acc;
}, {});
if (
Object.values(instances).some(
(it) => typeof it.costPerHour === "undefined"
)
) {
console.log(
`Invalid prices for:\n${Object.values(instances)
.filter((it) => typeof it.costPerHour !== "undefined")
.map((it) => `- ${it.name} (${it.cloudProvider})`)
.join("\n")}`
);
}
if (
Object.values(instances)
.map((it) => it.id)
.filter(onlyUnique).length !== Object.values(instances).length
) {
console.log("Collisions in the IDs");
}
if (Object.values(instances).length > 0) {
const content = Object.values(instances).reduce((acc, it) => {
if (typeof it.costPerHour !== undefined) {
acc[it.id] = it;
}
return acc;
}, {});
fs.writeFileSync(options.output, JSON.stringify(content), "utf8");
console.log(`output has been written to ${options.output}`);
} else {
console.log("No instances exported.");
return;
}
const longestName = Math.max.apply(
null,
Object.values(instances).map((it) => it.name.length)
);
console.log(`The longest instance name has ${longestName} characters`);
});
program.parse(process.argv);
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}