From e6b2e940882d13f13db58d60330b61458f2957b1 Mon Sep 17 00:00:00 2001 From: troinine Date: Thu, 23 Nov 2023 22:01:05 +0200 Subject: [PATCH 1/6] feat: support for defining market type for the allocated instance --- README.md | 2 ++ action.yml | 9 +++++++-- src/aws.js | 12 ++++++++++++ src/config.js | 10 +++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e3b4e890..c38e73cf 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,8 @@ Now you're ready to go! | `iam-role-name` | Optional. Used only with the `start` mode. | IAM role name to attach to the created EC2 runner.

This allows the runner to have permissions to run additional actions within the AWS account, without having to manage additional GitHub secrets and AWS users.

Setting this requires additional AWS permissions for the role launching the instance (see above). | | `aws-resource-tags` | Optional. Used only with the `start` mode. | Specifies tags to add to the EC2 instance and any attached storage.

This field is a stringified JSON array of tag objects, each containing a `Key` and `Value` field (see example below).

Setting this requires additional AWS permissions for the role launching the instance (see above). | | `runner-home-dir` | Optional. Used only with the `start` mode. | Specifies a directory where pre-installed actions-runner software and scripts are located.

| +| `market-type` | Optional. Used only with the `start` mode. | Specifies the market (purchasing) option for the instance. Allowed values: `spot`. The default is to use an on-demand instance. +| | `pre-runner-script` | Optional. Used only with the `start` mode. | Specifies bash commands to run before the runner starts. It's useful for installing dependencies with apt-get, yum, dnf, etc. For example:
          - name: Start EC2 runner
with:
mode: start
...
pre-runner-script: \|
sudo yum update -y && \
sudo yum install docker git libicu -y
sudo systemctl enable docker


| diff --git a/action.yml b/action.yml index 09fa9591..7d97d7de 100644 --- a/action.yml +++ b/action.yml @@ -22,7 +22,7 @@ inputs: required: false ec2-instance-type: description: >- - EC2 Instance Type. + EC2 Instance Type. This input is required if you use the 'start' mode. required: false subnet-id: @@ -32,7 +32,7 @@ inputs: required: false security-group-id: description: >- - EC2 Security Group Id. + EC2 Security Group Id. The security group should belong to the same VPC as the specified subnet. The runner doesn't require any inbound traffic. However, outbound traffic should be allowed. This input is required if you use the 'start' mode. @@ -69,6 +69,11 @@ inputs: description: >- Specifies bash commands to run before the runner starts. It's useful for installing dependencies with apt-get, yum, dnf, etc. required: false + market-type: + description: >- + Specifies the market (purchasing) option for the instance: + - 'spot' - Use a spot instance + required: false outputs: label: diff --git a/src/aws.js b/src/aws.js index bcf53646..de8bf459 100644 --- a/src/aws.js +++ b/src/aws.js @@ -32,6 +32,17 @@ function buildUserDataScript(githubRegistrationToken, label) { } } +function buildMarketOptions() { + if (config.marketType === 'spot') { + return { + MarketType: config.marketType, + SpotInstanceType: 'one-time', + }; + } + + return undefined; +} + async function startEc2Instance(label, githubRegistrationToken) { const ec2 = new AWS.EC2(); @@ -47,6 +58,7 @@ async function startEc2Instance(label, githubRegistrationToken) { SecurityGroupIds: [config.input.securityGroupId], IamInstanceProfile: { Name: config.input.iamRoleName }, TagSpecifications: config.tagSpecifications, + InstanceMarketOptions: buildMarketOptions(), }; try { diff --git a/src/config.js b/src/config.js index 1100f51e..884b892f 100644 --- a/src/config.js +++ b/src/config.js @@ -15,12 +15,16 @@ class Config { iamRoleName: core.getInput('iam-role-name'), runnerHomeDir: core.getInput('runner-home-dir'), preRunnerScript: core.getInput('pre-runner-script'), + marketType: core.getInput('market-type'), }; const tags = JSON.parse(core.getInput('aws-resource-tags')); this.tagSpecifications = null; if (tags.length > 0) { - this.tagSpecifications = [{ResourceType: 'instance', Tags: tags}, {ResourceType: 'volume', Tags: tags}]; + this.tagSpecifications = [ + { ResourceType: 'instance', Tags: tags }, + { ResourceType: 'volume', Tags: tags }, + ]; } // the values of github.context.repo.owner and github.context.repo.repo are taken from @@ -47,6 +51,10 @@ class Config { if (!this.input.ec2ImageId || !this.input.ec2InstanceType || !this.input.subnetId || !this.input.securityGroupId) { throw new Error(`Not all the required inputs are provided for the 'start' mode`); } + + if (this.marketType.trim().length > 0 && this.input.marketType !== 'spot') { + throw new Error(`Invalid 'market-type' input. Allowed values: spot.`); + } } else if (this.input.mode === 'stop') { if (!this.input.label || !this.input.ec2InstanceId) { throw new Error(`Not all the required inputs are provided for the 'stop' mode`); From 88ae486104180cabfdfc65edd8edffe09ee32f6c Mon Sep 17 00:00:00 2001 From: troinine Date: Thu, 23 Nov 2023 22:03:57 +0200 Subject: [PATCH 2/6] docs: polish README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index c38e73cf..28c4d159 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,6 @@ Now you're ready to go! | `aws-resource-tags` | Optional. Used only with the `start` mode. | Specifies tags to add to the EC2 instance and any attached storage.

This field is a stringified JSON array of tag objects, each containing a `Key` and `Value` field (see example below).

Setting this requires additional AWS permissions for the role launching the instance (see above). | | `runner-home-dir` | Optional. Used only with the `start` mode. | Specifies a directory where pre-installed actions-runner software and scripts are located.

| | `market-type` | Optional. Used only with the `start` mode. | Specifies the market (purchasing) option for the instance. Allowed values: `spot`. The default is to use an on-demand instance. -| | `pre-runner-script` | Optional. Used only with the `start` mode. | Specifies bash commands to run before the runner starts. It's useful for installing dependencies with apt-get, yum, dnf, etc. For example:
          - name: Start EC2 runner
with:
mode: start
...
pre-runner-script: \|
sudo yum update -y && \
sudo yum install docker git libicu -y
sudo systemctl enable docker


| From 1f0e989f0274c2d6d9aa69c07115a06ca38f1a49 Mon Sep 17 00:00:00 2001 From: troinine Date: Fri, 24 Nov 2023 10:53:52 +0200 Subject: [PATCH 3/6] build: package with market-type feature --- dist/index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index 409180d4..d722f8f1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62832,6 +62832,17 @@ function buildUserDataScript(githubRegistrationToken, label) { } } +function buildMarketOptions() { + if (config.marketType === 'spot') { + return { + MarketType: config.marketType, + SpotInstanceType: 'one-time', + }; + } + + return undefined; +} + async function startEc2Instance(label, githubRegistrationToken) { const ec2 = new AWS.EC2(); @@ -62847,6 +62858,7 @@ async function startEc2Instance(label, githubRegistrationToken) { SecurityGroupIds: [config.input.securityGroupId], IamInstanceProfile: { Name: config.input.iamRoleName }, TagSpecifications: config.tagSpecifications, + InstanceMarketOptions: buildMarketOptions(), }; try { @@ -62923,12 +62935,16 @@ class Config { iamRoleName: core.getInput('iam-role-name'), runnerHomeDir: core.getInput('runner-home-dir'), preRunnerScript: core.getInput('pre-runner-script'), + marketType: core.getInput('market-type'), }; const tags = JSON.parse(core.getInput('aws-resource-tags')); this.tagSpecifications = null; if (tags.length > 0) { - this.tagSpecifications = [{ResourceType: 'instance', Tags: tags}, {ResourceType: 'volume', Tags: tags}]; + this.tagSpecifications = [ + { ResourceType: 'instance', Tags: tags }, + { ResourceType: 'volume', Tags: tags }, + ]; } // the values of github.context.repo.owner and github.context.repo.repo are taken from @@ -62955,6 +62971,10 @@ class Config { if (!this.input.ec2ImageId || !this.input.ec2InstanceType || !this.input.subnetId || !this.input.securityGroupId) { throw new Error(`Not all the required inputs are provided for the 'start' mode`); } + + if (this.marketType.trim().length > 0 && this.input.marketType !== 'spot') { + throw new Error(`Invalid 'market-type' input. Allowed values: spot.`); + } } else if (this.input.mode === 'stop') { if (!this.input.label || !this.input.ec2InstanceId) { throw new Error(`Not all the required inputs are provided for the 'stop' mode`); From 8fc3496d4228c648e3a84a7a4a5262d4a579fee0 Mon Sep 17 00:00:00 2001 From: troinine Date: Fri, 24 Nov 2023 11:06:50 +0200 Subject: [PATCH 4/6] fix: fix validation for marketType as undefined can still be returned it seems --- dist/index.js | 2 +- src/config.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index d722f8f1..f401eb41 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62972,7 +62972,7 @@ class Config { throw new Error(`Not all the required inputs are provided for the 'start' mode`); } - if (this.marketType.trim().length > 0 && this.input.marketType !== 'spot') { + if (this.marketType?.length > 0 && this.input.marketType !== 'spot') { throw new Error(`Invalid 'market-type' input. Allowed values: spot.`); } } else if (this.input.mode === 'stop') { diff --git a/src/config.js b/src/config.js index 884b892f..cb3661f9 100644 --- a/src/config.js +++ b/src/config.js @@ -52,7 +52,7 @@ class Config { throw new Error(`Not all the required inputs are provided for the 'start' mode`); } - if (this.marketType.trim().length > 0 && this.input.marketType !== 'spot') { + if (this.marketType?.length > 0 && this.input.marketType !== 'spot') { throw new Error(`Invalid 'market-type' input. Allowed values: spot.`); } } else if (this.input.mode === 'stop') { From a41d43e5997e7e2d86631b6b580f5354dd72032a Mon Sep 17 00:00:00 2001 From: troinine Date: Fri, 24 Nov 2023 11:17:08 +0200 Subject: [PATCH 5/6] fix: config stores inputs in the input object --- dist/index.js | 4 ++-- src/aws.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index f401eb41..ea2e708c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62833,9 +62833,9 @@ function buildUserDataScript(githubRegistrationToken, label) { } function buildMarketOptions() { - if (config.marketType === 'spot') { + if (config.input.marketType === 'spot') { return { - MarketType: config.marketType, + MarketType: config.input.marketType, SpotInstanceType: 'one-time', }; } diff --git a/src/aws.js b/src/aws.js index de8bf459..7c790aa3 100644 --- a/src/aws.js +++ b/src/aws.js @@ -33,9 +33,9 @@ function buildUserDataScript(githubRegistrationToken, label) { } function buildMarketOptions() { - if (config.marketType === 'spot') { + if (config.input.marketType === 'spot') { return { - MarketType: config.marketType, + MarketType: config.input.marketType, SpotInstanceType: 'one-time', }; } From b52d82f0ad0056c6b65e24db2bc24e7e4bd7d0be Mon Sep 17 00:00:00 2001 From: troinine Date: Fri, 24 Nov 2023 11:22:22 +0200 Subject: [PATCH 6/6] fix: set spot options correctly --- dist/index.js | 4 +++- src/aws.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index ea2e708c..adb174f4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62836,7 +62836,9 @@ function buildMarketOptions() { if (config.input.marketType === 'spot') { return { MarketType: config.input.marketType, - SpotInstanceType: 'one-time', + SpotOptions: { + SpotInstanceType: 'one-time', + }, }; } diff --git a/src/aws.js b/src/aws.js index 7c790aa3..4578ae63 100644 --- a/src/aws.js +++ b/src/aws.js @@ -36,7 +36,9 @@ function buildMarketOptions() { if (config.input.marketType === 'spot') { return { MarketType: config.input.marketType, - SpotInstanceType: 'one-time', + SpotOptions: { + SpotInstanceType: 'one-time', + }, }; }