Skip to content

Commit

Permalink
fix: fix ec2-types gen script & values
Browse files Browse the repository at this point in the history
- includes test snapshot updates
  • Loading branch information
hoonoh committed Dec 6, 2021
1 parent a97803f commit 0fe628e
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 124 deletions.
116 changes: 69 additions & 47 deletions scripts/generate-ec2-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,46 @@ import prettier from 'prettier';

import { getGlobalSpotPrices } from '../src/lib/core';

const familyGeneral = ['a', 't', 'm', 'mac'];
const familyGeneral = [
//
'a', // a1 ...
't', // t1, t2, t3a...
'm', // m1, m2, m6g ...
'mac', // mac, mac2 ...
];

const familyCompute = ['c'];
const familyCompute = [
//
'c', // c1, c2, c5n, c5gn ...
];

const familyMemory = ['r', 'u-', 'x', 'z'];
const familyMemory = [
//
'r', // r3, r4, r5a ...
'u-', // u-6tb1, u-9tb1 ...
'x', // x1, x1e, x1edn ...
'z', // z1d ...
];

const familyStorage = ['d', 'h', 'i'];
const familyStorage = [
//
'd', // d2, d3, d3en ...
'h', // h1 ...
'i', // i3, i3en, i4i ...
'im', // im4gn ...
'is', // is4gen ...
];

const familyAcceleratedComputing = ['f', 'g', 'inf', 'p'];
const familyAcceleratedComputing = [
//
'dl', // dl1 ...
'f', // f1 ...
'g', // g5, g4dn, g5g ...
'inf', // inf1 ...
'p', // p2, p3, p4 ...
'trn', // trn1 ...
'vt', // vt1 ...
];

const familyOrder = [
...familyGeneral,
Expand All @@ -22,37 +53,10 @@ const familyOrder = [
...familyAcceleratedComputing,
];

const sizeOrder = [
'nano',
'micro',
'small',
'medium',
'large',
'xlarge',
'2xlarge',
'3xlarge',
'4xlarge',
'6xlarge',
'8xlarge',
'9xlarge',
'10xlarge',
'12xlarge',
'16xlarge',
'18xlarge',
'24xlarge',
'32xlarge',
'metal',
];

// assume instance naming rule as:
// [family(string)][generation/size(high-memory, aka`u-*`)(int)].[instanceSize(string)]
// [family(string with no numbers)][generation or type(string starting with number)].[instanceSize(string)]
const getFamilyPrefix = (family: string) => {
let rtn = '';
while (!'0123456789'.includes(family.substr(0, 1))) {
rtn += family.substr(0, 1);
family = family.substr(1);
}
return rtn;
return family.match(/^(\D+)\d/)?.[1] || '';
};

const sortFamilies = (f1: string, f2: string): number => {
Expand All @@ -71,10 +75,28 @@ const sortFamilies = (f1: string, f2: string): number => {
return rtn;
};

const getSizeOrderIndex = (size: string) => {
const smallSizeOrder = [
//
'nano',
'micro',
'small',
'medium',
'large',
];
if (size === 'metal') {
return 100;
}
if (size.endsWith('xlarge')) {
return smallSizeOrder.length - 1 + parseInt(size.match(/(\d{1,})?xlarge/)?.[1] || '0');
}
return smallSizeOrder.indexOf(size);
};

const sortSizes = (s1: string, s2: string): number => {
let rtn = 0;
const i1 = sizeOrder.indexOf(s1);
const i2 = sizeOrder.indexOf(s2);
const i1 = getSizeOrderIndex(s1);
const i2 = getSizeOrderIndex(s2);
if (i1 < 0) throw new Error(`unexpected instance size ${s1}`);
if (i2 < 0) throw new Error(`unexpected instance size ${s2}`);
if (i1 < i2) rtn = -1;
Expand All @@ -86,14 +108,14 @@ const sortInstances = (i1: string, i2: string): number => {
let rtn = 0;
const [f1, s1] = i1.split('.');
const [f2, s2] = i2.split('.');
let sc1 = sizeOrder.indexOf(s1);
let sc2 = sizeOrder.indexOf(s2);
let sc1 = getSizeOrderIndex(s1);
let sc2 = getSizeOrderIndex(s2);
if (f1[0] === f2[0]) {
if (f1 < f2) sc1 -= 100;
if (f1 > f2) sc1 += 100;
} else {
sc1 += familyOrder.indexOf(getFamilyPrefix(f1)) * 100;
sc2 += familyOrder.indexOf(getFamilyPrefix(f2)) * 100;
sc1 += familyOrder.indexOf(getFamilyPrefix(f1)) * 1000;
sc2 += familyOrder.indexOf(getFamilyPrefix(f2)) * 1000;
}
if (sc1 < sc2) rtn = -1;
if (sc1 > sc2) rtn = 1;
Expand Down Expand Up @@ -127,7 +149,9 @@ const getEc2Types = async (): Promise<string> => {
if (!family || !size || instanceType.split('.').length !== 2) {
throw new Error(`unexpected instanceType: ${instanceType}`);
}
if (!sizeOrder.includes(size)) throw new Error(`unexpected instance size ${size}`);
if (getSizeOrderIndex(size) < 0) {
throw new Error(`unexpected instance size ${size}`);
}

// instanceFamilies.push(family);
const groupByFamily = (refArray: string[], targSet: Set<string>, familyName: string) => {
Expand Down Expand Up @@ -202,9 +226,7 @@ const getEc2Types = async (): Promise<string> => {
return output;
};

if (require.main && require.main.filename === module.filename) {
(async (): Promise<void> => {
const targetPath = resolve(__dirname, '../src/constants/ec2-types.ts');
writeFileSync(targetPath, await getEc2Types());
})();
}
(async (): Promise<void> => {
const targetPath = resolve(__dirname, '../src/constants/ec2-types.ts');
writeFileSync(targetPath, await getEc2Types());
})();
101 changes: 90 additions & 11 deletions src/constants/ec2-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const instanceFamilyGeneral = [
'm5dn',
'm5n',
'm5zn',
'm6a',
'm6g',
'm6gd',
'm6i',
Expand All @@ -33,6 +34,7 @@ export const instanceFamilyCompute = [
'c6g',
'c6gd',
'c6gn',
'c6i',
] as const;

export const instanceFamilyMemory = [
Expand All @@ -47,26 +49,41 @@ export const instanceFamilyMemory = [
'r5n',
'r6g',
'r6gd',
'r6i',
'x1',
'x1e',
'x2gd',
'z1d',
] as const;

export const instanceFamilyStorage = ['d2', 'd3', 'd3en', 'h1', 'i2', 'i3', 'i3en'] as const;
export const instanceFamilyStorage = [
'd2',
'd3',
'd3en',
'h1',
'i2',
'i3',
'i3en',
'im4gn',
'is4gen',
] as const;

export const instanceFamilyAcceleratedComputing = [
'dl1',
'f1',
'g2',
'g3',
'g3s',
'g4ad',
'g4dn',
'g5',
'g5g',
'inf1',
'p2',
'p3',
'p3dn',
'p4d',
'vt1',
] as const;

export const instanceFamily = {
Expand Down Expand Up @@ -108,6 +125,7 @@ export const instanceSizes = [
'18xlarge',
'24xlarge',
'32xlarge',
'48xlarge',
'metal',
] as const;

Expand Down Expand Up @@ -224,6 +242,16 @@ export const allInstances = [
'm5zn.6xlarge',
'm5zn.12xlarge',
'm5zn.metal',
'm6a.large',
'm6a.xlarge',
'm6a.2xlarge',
'm6a.4xlarge',
'm6a.8xlarge',
'm6a.12xlarge',
'm6a.16xlarge',
'm6a.24xlarge',
'm6a.32xlarge',
'm6a.48xlarge',
'm6g.medium',
'm6g.large',
'm6g.xlarge',
Expand Down Expand Up @@ -251,6 +279,7 @@ export const allInstances = [
'm6i.16xlarge',
'm6i.24xlarge',
'm6i.32xlarge',
'm6i.metal',
'c1.medium',
'c1.xlarge',
'c3.large',
Expand Down Expand Up @@ -330,6 +359,16 @@ export const allInstances = [
'c6gn.8xlarge',
'c6gn.12xlarge',
'c6gn.16xlarge',
'c6i.large',
'c6i.xlarge',
'c6i.2xlarge',
'c6i.4xlarge',
'c6i.8xlarge',
'c6i.12xlarge',
'c6i.16xlarge',
'c6i.24xlarge',
'c6i.32xlarge',
'c6i.metal',
'r3.large',
'r3.xlarge',
'r3.2xlarge',
Expand Down Expand Up @@ -420,6 +459,16 @@ export const allInstances = [
'r6gd.12xlarge',
'r6gd.16xlarge',
'r6gd.metal',
'r6i.large',
'r6i.xlarge',
'r6i.2xlarge',
'r6i.4xlarge',
'r6i.8xlarge',
'r6i.12xlarge',
'r6i.16xlarge',
'r6i.24xlarge',
'r6i.32xlarge',
'r6i.metal',
'x1.16xlarge',
'x1.32xlarge',
'x1e.xlarge',
Expand Down Expand Up @@ -481,31 +530,58 @@ export const allInstances = [
'i3en.12xlarge',
'i3en.24xlarge',
'i3en.metal',
'im4gn.large',
'im4gn.xlarge',
'im4gn.2xlarge',
'im4gn.4xlarge',
'im4gn.8xlarge',
'im4gn.16xlarge',
'is4gen.medium',
'is4gen.4xlarge',
'dl1.24xlarge',
'f1.2xlarge',
'f1.4xlarge',
'f1.16xlarge',
'g2.2xlarge',
'g2.8xlarge',
'g3.4xlarge',
'g3.8xlarge',
'g4ad.xlarge',
'g4ad.8xlarge',
'g4ad.16xlarge',
'g4dn.8xlarge',
'g5.xlarge',
'g5.2xlarge',
'g5.4xlarge',
'g5.8xlarge',
'g5.12xlarge',
'g5.48xlarge',
'g5g.xlarge',
'g5g.8xlarge',
'g5g.16xlarge',
'g5g.metal',
'inf1.xlarge',
'inf1.2xlarge',
'inf1.6xlarge',
'inf1.24xlarge',
'is4gen.large',
'is4gen.xlarge',
'is4gen.2xlarge',
'is4gen.8xlarge',
'g2.2xlarge',
'g3.4xlarge',
'g3.16xlarge',
'g3s.xlarge',
'g4ad.xlarge',
'g4ad.2xlarge',
'g4ad.4xlarge',
'g4ad.8xlarge',
'g4ad.16xlarge',
'g4dn.xlarge',
'g4dn.2xlarge',
'g4dn.4xlarge',
'g4dn.8xlarge',
'g4dn.12xlarge',
'g4dn.16xlarge',
'g4dn.metal',
'inf1.xlarge',
'inf1.2xlarge',
'inf1.6xlarge',
'inf1.24xlarge',
'g5.16xlarge',
'g5.24xlarge',
'g5g.2xlarge',
'g5g.4xlarge',
'p2.xlarge',
'p2.8xlarge',
'p2.16xlarge',
Expand All @@ -514,6 +590,9 @@ export const allInstances = [
'p3.16xlarge',
'p3dn.24xlarge',
'p4d.24xlarge',
'vt1.3xlarge',
'vt1.6xlarge',
'vt1.24xlarge',
] as const;

export type InstanceType = typeof allInstances[number];
Loading

0 comments on commit 0fe628e

Please sign in to comment.