Skip to content

Commit

Permalink
fix(ci): ssh'ing into instances (#6136)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludamad authored May 1, 2024
1 parent f060fa6 commit af3192d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
28 changes: 10 additions & 18 deletions .github/spot-runner-action/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,22 @@ class Ec2Instance {
getLaunchTemplate() {
return __awaiter(this, void 0, void 0, function* () {
const client = yield this.getEc2Client();
// NOTE: This should be deterministic or we will create a launch template each time
const userData = yield new userdata_1.UserData(this.config).getUserData();
const ec2InstanceTypeHash = this.getHashOfStringArray(this.config.ec2InstanceType.concat([userData, JSON.stringify(this.tags), this.config.ec2KeyName]));
const launchTemplateName = "aztec-packages-spot-" + this.config.ec2AmiId + "-" + ec2InstanceTypeHash;
const launchTemplateParams = {
LaunchTemplateName: launchTemplateName,
LaunchTemplateData: {
ImageId: this.config.ec2AmiId,
InstanceInitiatedShutdownBehavior: "terminate",
InstanceRequirements: {
// We do not know what the instance types correspond to
// just let the user send a list of allowed instance types
VCpuCount: { Min: 0 },
MemoryMiB: { Min: 0 },
AllowedInstanceTypes: this.config.ec2InstanceType,
},
SecurityGroupIds: [this.config.ec2SecurityGroupId],
KeyName: this.config.ec2KeyName,
UserData: userData,
TagSpecifications: [
Expand All @@ -276,33 +277,20 @@ class Ec2Instance {
],
},
};
let arr = [];
try {
arr = (yield client
.describeLaunchTemplates({
LaunchTemplateNames: [launchTemplateName],
})
.promise()).LaunchTemplates || [];
}
catch (err) {
core.info("Launch templates describe error, note this will be likely resolved by creating the template in the next step: " + err);
}
core.info("Launch templates found: " + JSON.stringify(arr, null, 2));
if (arr.length <= 0) {
core.info("Creating launch template: " + launchTemplateName);
yield client.createLaunchTemplate(launchTemplateParams).promise();
}
core.info("Creating launch template: " + launchTemplateName);
yield client.createLaunchTemplate(launchTemplateParams).promise();
return launchTemplateName;
});
}
requestMachine(useOnDemand) {
return __awaiter(this, void 0, void 0, function* () {
// Note advice re max bid: "If you specify a maximum price, your instances will be interrupted more frequently than if you do not specify this parameter."
const launchTemplateName = yield this.getLaunchTemplate();
const availabilityZone = yield this.getSubnetAz();
const fleetLaunchConfig = {
LaunchTemplateSpecification: {
Version: "$Latest",
LaunchTemplateName: yield this.getLaunchTemplate(),
LaunchTemplateName: launchTemplateName,
},
Overrides: this.config.ec2InstanceType.map((instanceType) => ({
InstanceType: instanceType,
Expand All @@ -323,6 +311,10 @@ class Ec2Instance {
const client = yield this.getEc2Client();
const fleet = yield client.createFleet(createFleetRequest).promise();
const instances = ((fleet === null || fleet === void 0 ? void 0 : fleet.Instances) || [])[0] || {};
// cleanup
yield client.deleteLaunchTemplate({
LaunchTemplateName: launchTemplateName,
});
return (instances.InstanceIds || [])[0];
});
}
Expand Down
30 changes: 10 additions & 20 deletions .github/spot-runner-action/src/ec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export class Ec2Instance {
async getLaunchTemplate(): Promise<string> {
const client = await this.getEc2Client();

// NOTE: This should be deterministic or we will create a launch template each time
const userData = await new UserData(this.config).getUserData();
const ec2InstanceTypeHash = this.getHashOfStringArray(
this.config.ec2InstanceType.concat([userData, JSON.stringify(this.tags), this.config.ec2KeyName])
Expand All @@ -170,13 +169,15 @@ export class Ec2Instance {
LaunchTemplateName: launchTemplateName,
LaunchTemplateData: {
ImageId: this.config.ec2AmiId,
InstanceInitiatedShutdownBehavior: "terminate",
InstanceRequirements: {
// We do not know what the instance types correspond to
// just let the user send a list of allowed instance types
VCpuCount: { Min: 0 },
MemoryMiB: { Min: 0 },
AllowedInstanceTypes: this.config.ec2InstanceType,
},
SecurityGroupIds: [this.config.ec2SecurityGroupId],
KeyName: this.config.ec2KeyName,
UserData: userData,
TagSpecifications: [
Expand All @@ -195,34 +196,19 @@ export class Ec2Instance {
],
},
};
let arr: any[] = [];

try {
arr = (
await client
.describeLaunchTemplates({
LaunchTemplateNames: [launchTemplateName],
})
.promise()
).LaunchTemplates || [];
} catch (err) {
core.info("Launch templates describe error, note this will be likely resolved by creating the template in the next step: " + err);
}
core.info("Launch templates found: " + JSON.stringify(arr, null, 2));
if (arr.length <= 0) {
core.info("Creating launch template: " + launchTemplateName);
await client.createLaunchTemplate(launchTemplateParams).promise();
}
core.info("Creating launch template: " + launchTemplateName);
await client.createLaunchTemplate(launchTemplateParams).promise();
return launchTemplateName;
}

async requestMachine(useOnDemand: boolean): Promise<string|undefined> {
// Note advice re max bid: "If you specify a maximum price, your instances will be interrupted more frequently than if you do not specify this parameter."
const launchTemplateName = await this.getLaunchTemplate();
const availabilityZone = await this.getSubnetAz();
const fleetLaunchConfig: FleetLaunchTemplateConfigRequest = {
LaunchTemplateSpecification: {
Version: "$Latest",
LaunchTemplateName: await this.getLaunchTemplate(),
LaunchTemplateName: launchTemplateName,
},
Overrides: this.config.ec2InstanceType.map((instanceType) => ({
InstanceType: instanceType,
Expand All @@ -243,6 +229,10 @@ export class Ec2Instance {
const client = await this.getEc2Client();
const fleet = await client.createFleet(createFleetRequest).promise();
const instances: CreateFleetInstance = (fleet?.Instances || [])[0] || {};
// cleanup
await client.deleteLaunchTemplate({
LaunchTemplateName: launchTemplateName,
});
return (instances.InstanceIds || [])[0];
}

Expand Down

0 comments on commit af3192d

Please sign in to comment.