-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathaws.js
99 lines (87 loc) · 3.33 KB
/
aws.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const AWS = require('aws-sdk');
const core = require('@actions/core');
const config = require('./config');
// User data scripts are run as the root user
function buildUserDataScript(githubRegistrationToken, label) {
if (config.input.runnerHomeDir) {
// If runner home directory is specified, we expect the actions-runner software (and dependencies)
// to be pre-installed in the AMI, so we simply cd into that directory and then start the runner
return [
'#!/bin/bash',
`cd "${config.input.runnerHomeDir}"`,
'export RUNNER_ALLOW_RUNASROOT=1',
'export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1',
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`,
'./run.sh',
];
} else {
return [
'#!/bin/bash',
'mkdir actions-runner && cd actions-runner',
'case $(uname -m) in aarch64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}',
'curl -O -L https://github.com/actions/runner/releases/download/v2.284.0/actions-runner-linux-${RUNNER_ARCH}-2.284.0.tar.gz',
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-2.284.0.tar.gz',
'export RUNNER_ALLOW_RUNASROOT=1',
'export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1',
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`,
'./run.sh',
];
}
}
async function startEc2Instance(label, githubRegistrationToken) {
const ec2 = new AWS.EC2();
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,
};
try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceIds = result.Instances.map(inst => inst.InstanceId);
core.info(`AWS EC2 instances ${ec2InstanceIds} are started`);
return ec2InstanceIds;
} catch (error) {
core.error('AWS EC2 instance starting error');
throw error;
}
}
async function terminateEc2Instance() {
const ec2 = new AWS.EC2();
const params = {
InstanceIds: config.input.ec2InstanceIds,
};
try {
await ec2.terminateInstances(params).promise();
core.info(`AWS EC2 instances ${config.input.ec2InstanceIds} are terminated`);
return;
} catch (error) {
core.error(`AWS EC2 instances ${config.input.ec2InstanceIds} termination error`);
throw error;
}
}
async function waitForInstanceRunning(ec2InstanceIds) {
const ec2 = new AWS.EC2();
const params = {
InstanceIds: ec2InstanceIds,
};
try {
await ec2.waitFor('instanceRunning', params).promise();
core.info(`AWS EC2 instances ${ec2InstanceIds} are up and running`);
return;
} catch (error) {
core.error(`AWS EC2 instances ${ec2InstanceIds} initialization error`);
throw error;
}
}
module.exports = {
startEc2Instance,
terminateEc2Instance,
waitForInstanceRunning,
};