/
ec2.ts
412 lines (378 loc) · 13.2 KB
/
ec2.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import * as cdk from 'aws-cdk-lib';
import {
aws_ec2 as ec2,
aws_iam as iam,
aws_logs as logs,
aws_stepfunctions as stepfunctions,
aws_stepfunctions_tasks as stepfunctions_tasks,
Duration,
RemovalPolicy,
Stack,
} from 'aws-cdk-lib';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { IntegrationPattern } from 'aws-cdk-lib/aws-stepfunctions';
import { Construct } from 'constructs';
import {
BaseProvider,
IAmiBuilder,
IRunnerProvider,
IRunnerProviderStatus,
Os,
RunnerAmi,
RunnerProviderProps,
RunnerRuntimeParameters,
RunnerVersion,
} from './common';
import { AmiBuilder } from './image-builders/ami';
// this script is specifically made so `poweroff` is absolutely always called
// each `{}` is a variable coming from `params` below
const linuxUserDataTemplate = `#!/bin/bash -x
TASK_TOKEN="{}"
heartbeat () {
while true; do
aws stepfunctions send-task-heartbeat --task-token "$TASK_TOKEN"
sleep 60
done
}
setup_logs () {
cat <<EOF > /tmp/log.conf || exit 1
{
"logs": {
"log_stream_name": "unknown",
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/runner.log",
"log_group_name": "{}",
"log_stream_name": "{}",
"timezone": "UTC"
}
]
}
}
}
}
EOF
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/tmp/log.conf || exit 2
}
action () {
sudo -Hu runner /home/runner/config.sh --unattended --url "https://{}/{}/{}" --token "{}" --ephemeral --work _work --labels "{}" {} --name "{}" || exit 1
sudo --preserve-env=AWS_REGION -Hu runner /home/runner/run.sh || exit 2
}
heartbeat &
if setup_logs && action | tee /var/log/runner.log 2>&1; then
aws stepfunctions send-task-success --task-token "$TASK_TOKEN" --task-output '{"ok": true}'
else
aws stepfunctions send-task-failure --task-token "$TASK_TOKEN"
fi
poweroff
`.replace(/{/g, '\\{').replace(/}/g, '\\}').replace(/\\{\\}/g, '{}');
// this script is specifically made so `poweroff` is absolutely always called
// each `{}` is a variable coming from `params` below and their order should match the linux script
const windowsUserDataTemplate = `<powershell>
$TASK_TOKEN = "{}"
Start-Job -ScriptBlock {
while (1) {
aws stepfunctions send-task-heartbeat --task-token "$using:TASK_TOKEN"
sleep 60
}
}
function setup_logs () {
echo '{
"logs": {
"log_stream_name": "unknown",
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/actions/runner.log",
"log_group_name": "{}",
"log_stream_name": "{}",
"timezone": "UTC"
}
]
}
}
}
}' | Out-File -Encoding ASCII $Env:TEMP/log.conf
& "C:/Program Files/Amazon/AmazonCloudWatchAgent/amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m ec2 -s -c file:$Env:TEMP/log.conf
}
function action () {
cd /actions
./config.cmd --unattended --url "https://{}/{}/{}" --token "{}" --ephemeral --work _work --labels "{}" {} --name "{}" 2>&1 | Out-File -Encoding ASCII -Append /actions/runner.log
if ($LASTEXITCODE -ne 0) { return 1 }
./run.cmd 2>&1 | Out-File -Encoding ASCII -Append /actions/runner.log
if ($LASTEXITCODE -ne 0) { return 2 }
return 0
}
setup_logs
$r = action
if ($r -eq 0) {
aws stepfunctions send-task-success --task-token "$TASK_TOKEN" --task-output '{ }'
} else {
aws stepfunctions send-task-failure --task-token "$TASK_TOKEN"
}
Stop-Computer -ComputerName localhost -Force
</powershell>
`.replace(/{/g, '\\{').replace(/}/g, '\\}').replace(/\\{\\}/g, '{}');
/**
* Properties for {@link Ec2Runner} construct.
*/
export interface Ec2RunnerProps extends RunnerProviderProps {
/**
* AMI builder that creates AMIs with GitHub runner pre-configured. On Linux, a user named `runner` is expected to exist with access to Docker.
*
* @default AMI builder for Ubuntu Linux on the same subnet as configured by {@link vpc} and {@link subnetSelection}
*/
readonly amiBuilder?: IAmiBuilder;
/**
* GitHub Actions labels used for this provider.
*
* These labels are used to identify which provider should spawn a new on-demand runner. Every job sends a webhook with the labels it's looking for
* based on runs-on. We match the labels from the webhook with the labels specified here. If all the labels specified here are present in the
* job's labels, this provider will be chosen and spawn a new runner.
*
* @default ['ec2']
*/
readonly labels?: string[];
/**
* Instance type for launched runner instances.
*
* @default m5.large
*/
readonly instanceType?: ec2.InstanceType;
/**
* Size of volume available for launched runner instances. This modifies the boot volume size and doesn't add any additional volumes.
*
* @default 30GB
*/
readonly storageSize?: cdk.Size;
/**
* Security Group to assign to launched runner instances.
*
* @default a new security group
*
* @deprecated use {@link securityGroups}
*/
readonly securityGroup?: ec2.ISecurityGroup;
/**
* Security groups to assign to launched runner instances.
*
* @default a new security group
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* Subnet where the runner instances will be launched.
*
* @default default subnet of account's default VPC
*
* @deprecated use {@link vpc} and {@link subnetSelection}
*/
readonly subnet?: ec2.ISubnet;
/**
* VPC where runner instances will be launched.
*
* @default default account VPC
*/
readonly vpc?: ec2.IVpc;
/**
* Where to place the network interfaces within the VPC. Only the first matched subnet will be used.
*
* @default default VPC subnet
*/
readonly subnetSelection?: ec2.SubnetSelection;
/**
* Use spot instances to save money. Spot instances are cheaper but not always available and can be stopped prematurely.
*
* @default false
*/
readonly spot?: boolean;
/**
* Set a maximum price for spot instances.
*
* @default no max price (you will pay current spot price)
*/
readonly spotMaxPrice?: string;
}
/**
* GitHub Actions runner provider using EC2 to execute jobs.
*
* This construct is not meant to be used by itself. It should be passed in the providers property for GitHubRunners.
*/
export class Ec2Runner extends BaseProvider implements IRunnerProvider {
/**
* Labels associated with this provider.
*/
readonly labels: string[];
/**
* Grant principal used to add permissions to the runner role.
*/
readonly grantPrincipal: iam.IPrincipal;
private readonly ami: RunnerAmi;
private readonly logGroup: logs.LogGroup;
private readonly role: iam.Role;
private readonly instanceType: ec2.InstanceType;
private readonly storageSize: cdk.Size;
private readonly spot: boolean;
private readonly spotMaxPrice: string | undefined;
private readonly vpc: ec2.IVpc;
private readonly subnet?: ec2.ISubnet;
private readonly securityGroups: ec2.ISecurityGroup[];
constructor(scope: Construct, id: string, props: Ec2RunnerProps) {
super(scope, id);
this.labels = props.labels ?? ['ec2'];
this.vpc = props.vpc ?? ec2.Vpc.fromLookup(this, 'Default VPC', { isDefault: true });
this.securityGroups = props.securityGroup ? [props.securityGroup] : (props.securityGroups ?? [new ec2.SecurityGroup(this, 'SG', { vpc: this.vpc })]);
this.subnet = props.subnet ?? props.vpc?.selectSubnets(props.subnetSelection).subnets[0];
this.instanceType = props.instanceType ?? ec2.InstanceType.of(ec2.InstanceClass.M5, ec2.InstanceSize.LARGE);
this.storageSize = props.storageSize ?? cdk.Size.gibibytes(30); // 30 is the minimum for Windows
this.spot = props.spot ?? false;
this.spotMaxPrice = props.spotMaxPrice;
const amiBuilder = props.amiBuilder ?? new AmiBuilder(this, 'Image Builder', {
vpc: props.vpc,
subnetSelection: props.subnetSelection,
securityGroups: this.securityGroups,
});
this.ami = amiBuilder.bind();
if (!this.ami.architecture.instanceTypeMatch(this.instanceType)) {
throw new Error(`AMI architecture (${this.ami.architecture.name}) doesn't match runner instance type (${this.instanceType} / ${this.instanceType.architecture})`);
}
this.grantPrincipal = this.role = new iam.Role(this, 'Role', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'),
],
});
this.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['states:SendTaskFailure', 'states:SendTaskSuccess', 'states:SendTaskHeartbeat'],
resources: ['*'], // no support for stateMachine.stateMachineArn :(
}));
this.logGroup = new logs.LogGroup(
this,
'Logs',
{
retention: props.logRetention ?? RetentionDays.ONE_MONTH,
removalPolicy: RemovalPolicy.DESTROY,
},
);
this.logGroup.grantWrite(this);
}
/**
* Generate step function task(s) to start a new runner.
*
* Called by GithubRunners and shouldn't be called manually.
*
* @param parameters workflow job details
*/
getStepFunctionTask(parameters: RunnerRuntimeParameters): stepfunctions.IChainable {
// we need to build user data in two steps because passing the template as the first parameter to stepfunctions.JsonPath.format fails on syntax
const params = [
stepfunctions.JsonPath.taskToken,
this.logGroup.logGroupName,
parameters.runnerNamePath,
parameters.githubDomainPath,
parameters.ownerPath,
parameters.repoPath,
parameters.runnerTokenPath,
this.labels.join(','),
this.ami.runnerVersion.is(RunnerVersion.latest()) ? '' : '--disableupdate',
parameters.runnerNamePath,
];
const passUserData = new stepfunctions.Pass(this, `${this.labels.join(', ')} data`, {
parameters: {
userdataTemplate: this.ami.os.is(Os.WINDOWS) ? windowsUserDataTemplate : linuxUserDataTemplate,
},
resultPath: stepfunctions.JsonPath.stringAt('$.ec2'),
});
// we can't use fleets because they don't let us override user data, security groups or even disk size
// we can't use requestSpotInstances because it doesn't support launch templates, and it's deprecated
const run = new stepfunctions_tasks.CallAwsService(this, this.labels.join(', '), {
integrationPattern: IntegrationPattern.WAIT_FOR_TASK_TOKEN,
service: 'ec2',
action: 'runInstances',
heartbeat: Duration.minutes(5),
parameters: {
LaunchTemplate: {
LaunchTemplateId: this.ami.launchTemplate.launchTemplateId,
},
MinCount: 1,
MaxCount: 1,
InstanceType: this.instanceType.toString(),
UserData: stepfunctions.JsonPath.base64Encode(
stepfunctions.JsonPath.format(
stepfunctions.JsonPath.stringAt('$.ec2.userdataTemplate'),
...params,
),
),
InstanceInitiatedShutdownBehavior: ec2.InstanceInitiatedShutdownBehavior.TERMINATE,
IamInstanceProfile: {
Arn: new iam.CfnInstanceProfile(this, 'Instance Profile', {
roles: [this.role.roleName],
}).attrArn,
},
MetadataOptions: {
HttpTokens: 'required',
},
SecurityGroupIds: this.securityGroups.map(sg => sg.securityGroupId),
SubnetId: this.subnet?.subnetId,
BlockDeviceMappings: [{
DeviceName: '/dev/sda1',
Ebs: {
DeleteOnTermination: true,
VolumeSize: this.storageSize.toGibibytes(),
},
}],
InstanceMarketOptions: this.spot ? {
MarketType: 'spot',
SpotOptions: {
MaxPrice: this.spotMaxPrice,
SpotInstanceType: 'one-time',
},
} : undefined,
},
iamResources: ['*'],
});
return passUserData.next(run);
}
grantStateMachine(stateMachineRole: iam.IGrantable) {
stateMachineRole.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['iam:PassRole'],
resources: [this.role.roleArn],
conditions: {
StringEquals: {
'iam:PassedToService': 'ec2.amazonaws.com',
},
},
}));
stateMachineRole.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['ec2:createTags'],
resources: [Stack.of(this).formatArn({
service: 'ec2',
resource: '*',
})],
}));
}
status(statusFunctionRole: iam.IGrantable): IRunnerProviderStatus {
statusFunctionRole.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['ec2:DescribeLaunchTemplateVersions'],
resources: ['*'],
}));
return {
type: this.constructor.name,
labels: this.labels,
securityGroups: this.securityGroups.map(sg => sg.securityGroupId),
roleArn: this.role.roleArn,
ami: {
launchTemplate: this.ami.launchTemplate.launchTemplateId || 'unknown',
amiBuilderLogGroup: this.ami.logGroup?.logGroupName,
},
};
}
/**
* The network connections associated with this resource.
*/
public get connections(): ec2.Connections {
return new ec2.Connections({ securityGroups: this.securityGroups });
}
}