Skip to content

Commit

Permalink
Use storage: number when parsing account configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Nov 11, 2024
1 parent 4f9a86e commit 88f0495
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
30 changes: 30 additions & 0 deletions .tools/test/stacks/config/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .tools/test/stacks/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^22.9.0",
"typescript": "^5.6.3",
"yaml": "^2.4.2"
}
}
39 changes: 32 additions & 7 deletions .tools/test/stacks/config/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ interface AccountConfig {
// https://docs.aws.amazon.com/batch/latest/APIReference/API_ResourceRequirement.html
vcpus?: string; // Count
memory?: string; // MiB, but limited based on vCPU count, see docs
storage?: string; // GiB, 20GiB to 200GiB
storage?: number; // GiB, 20GiB to 200GiB
}

type AccountConfigYaml = {
[K in keyof AccountConfig]: string
}

interface AccountConfigs {
Expand All @@ -20,17 +24,38 @@ interface AccountConfigs {
export function readAccountConfig(filePath: string): AccountConfigs {
try {
const fileContents = fs.readFileSync(filePath, "utf8");
const data: AccountConfigs = parse(fileContents);

Object.values(data).forEach((config) => {
if (!config.account_id || !config.status) {
throw new Error("Validation failed: Missing required account fields.");
const data = Object.entries(parse(fileContents) as Record<string, AccountConfigYaml>).reduce((data, [name, config]) => {
const {account_id, status, vcpus, memory, storage} = config;
if (!account_id) {
throw new Error(`Validation failed: Missing account_id field in ${name}`);
}
switch (status) {
case "enabled": // fallthrough
case "disabled":
break;
default:
throw new Error(`Validation failed: invalid status ${status} in ${name}`)
}
});
data[name] = {
account_id,
status,
vcpus,
memory,
storage: numberOrDefault(storage, 20),
}
return data;
}, {} as Record<string, AccountConfig>)

return data;
} catch (error) {
console.error("Failed to read or parse the YAML file:", { error });
throw error;
}
}

function numberOrDefault(storage: string | undefined, defaultValue: number) {
const batchStorage = Number(storage);
const batchStorageNumber = isNaN(batchStorage) ? defaultValue : batchStorage;
return batchStorageNumber;
}

26 changes: 26 additions & 0 deletions .tools/test/stacks/config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"lib": ["es2020", "dom"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": ["./node_modules/@types"],
"resolveJsonModule": true,
"esModuleInterop": true
},
"exclude": ["node_modules", "cdk.out"]
}
9 changes: 4 additions & 5 deletions .tools/test/stacks/plugin/typescript/plugin_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class PluginStack extends cdk.Stack {
// https://docs.aws.amazon.com/batch/latest/APIReference/API_ResourceRequirement.html
this.batchMemory = acctConfig[`${toolName}`]?.memory ?? "16384"; // MiB
this.batchVcpus = acctConfig[`${toolName}`]?.vcpus ?? "4"; // CPUs
const batchStorage = Number(acctConfig[`${toolName}`]?.storage ?? undefined);
this.batchStorage = isNaN(batchStorage) ? 20 : batchStorage; // GiB
this.batchStorage = acctConfig[`${toolName}`]?.storage ?? 20; // GiB
}

const [jobDefinition, jobQueue] = this.initBatchFargate();
Expand Down Expand Up @@ -142,7 +141,7 @@ class PluginStack extends cdk.Stack {
},
],
ephemeralStorage: {
sizeInGiB: this.batchStorage,
sizeInGib: this.batchStorage,
},
environment: variableConfigJson,
},
Expand Down Expand Up @@ -193,7 +192,7 @@ class PluginStack extends cdk.Stack {
);

return new lambda.Function(this, `SubmitBatchJob-${toolName}`, {
runtime: lambda.Runtime.PYTHON_3_9,
runtime: lambda.Runtime.PYTHON_3_8,
handler: "submit_job.handler",
code: lambda.Code.fromAsset("lambda"),
environment: {
Expand Down Expand Up @@ -315,7 +314,7 @@ class PluginStack extends cdk.Stack {

// Define the Lambda function.
const lambdaFunction = new lambda.Function(this, "BatchJobCompleteLambda", {
runtime: lambda.Runtime.PYTHON_3_9,
runtime: lambda.Runtime.PYTHON_3_8,
handler: "export_logs.handler",
role: executionRole,
code: lambda.Code.fromAsset("lambda"),
Expand Down

0 comments on commit 88f0495

Please sign in to comment.