Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Fix launch template optional API params #3

Merged
merged 2 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55169,17 +55169,35 @@ async function startEc2Instance(label, githubRegistrationToken) {
const userData = buildUserDataScript(githubRegistrationToken, label);

const params = {
ImageId: config.input.ec2ImageId,
InstanceType: config.input.ec2InstanceType,
MinCount: config.input.runnerCount,
MaxCount: config.input.runnerCount,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
};

if (config.input.ec2LaunchTemplate) {
params.LaunchTemplate = {
LaunchTemplateName: config.input.ec2LaunchTemplate
};
}

// when using a launch template any or all of these are optional
if (config.input.ec2ImageId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could probably be done with an array of property names and dynamically checking config.input for them & adding them to params. But it would look like:

const passThrough = ['ec2ImageId', 'ec2InstanceType', 'subnetId', 'securityGroupId', 'iamRoleName'];
Object.assign(params, Object.fromEntries(Object.entries(config.input).filter(([k,v]) => passThrough.includes(k) && v)))

which is a bit dense

params.ImageId = config.input.ec2ImageId;
}
if (config.input.ec2InstanceType) {
params.InstanceType = config.input.ec2InstanceType;
}
if (config.input.subnetId) {
params.SubnetId = config.input.subnetId;
}
if (config.input.securityGroupId) {
params.SecurityGroupIds = [config.input.securityGroupId];
}
if (config.input.iamRoleName) {
params.IamInstanceProfile = { Name: config.input.iamRoleName };
}

try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceIds = result.Instances.map(x => x.InstanceId).join();
Expand Down Expand Up @@ -55254,6 +55272,7 @@ class Config {
iamRoleName: core.getInput('iam-role-name'),
runnerHomeDir: core.getInput('runner-home-dir'),
runnerCount: core.getInput('runner-count'),
ec2LaunchTemplate: core.getInput('ec2-launch-template'),
};

const tags = JSON.parse(core.getInput('aws-resource-tags'));
Expand Down Expand Up @@ -55283,7 +55302,9 @@ class Config {
}

if (this.input.mode === 'start') {
if (!this.input.ec2ImageId || !this.input.ec2InstanceType || !this.input.subnetId || !this.input.securityGroupId) {
const isSet = param => param;
const params = [this.input.ec2ImageId, this.input.ec2InstanceType, this.input.subnetId, this.input.securityGroupId];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit confused about where this line came from: it's from #2, and appears here because the dist got rebuilt.

if (!(this.input.ec2LaunchTemplate || params.every(isSet))) {
throw new Error(`Not all the required inputs are provided for the 'start' mode`);
}
} else if (this.input.mode === 'stop') {
Expand Down
22 changes: 17 additions & 5 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,9 @@ async function startEc2Instance(label, githubRegistrationToken) {
const userData = buildUserDataScript(githubRegistrationToken, label);

const params = {
ImageId: config.input.ec2ImageId,
InstanceType: config.input.ec2InstanceType,
MinCount: config.input.runnerCount,
MaxCount: config.input.runnerCount,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be being dumb, but where do you pass in a LaunchTemplate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It came from the upstream PR we merged, so not in this diff, but just below here on line 52. Click the little expandy button.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is just a fix for the upstream one, which didn't quite work. machulav#70

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! 💡 Thanks for the context 👌


Expand All @@ -55,6 +50,23 @@ async function startEc2Instance(label, githubRegistrationToken) {
};
}

// when using a launch template any or all of these are optional
if (config.input.ec2ImageId) {
params.ImageId = config.input.ec2ImageId;
}
if (config.input.ec2InstanceType) {
params.InstanceType = config.input.ec2InstanceType;
}
if (config.input.subnetId) {
params.SubnetId = config.input.subnetId;
}
if (config.input.securityGroupId) {
params.SecurityGroupIds = [config.input.securityGroupId];
}
if (config.input.iamRoleName) {
params.IamInstanceProfile = { Name: config.input.iamRoleName };
}

try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceIds = result.Instances.map(x => x.InstanceId).join();
Expand Down