-
Notifications
You must be signed in to change notification settings - Fork 9
/
manager.ts
384 lines (366 loc) · 14.8 KB
/
manager.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
import { SecurityGroup } from "@pepperize/cdk-security-group";
import { Stack } from "aws-cdk-lib";
import {
AmazonLinuxCpuType,
AmazonLinuxEdition,
AmazonLinuxGeneration,
AmazonLinuxStorage,
AmazonLinuxVirt,
CloudFormationInit,
IMachineImage,
InitCommand,
InitConfig,
InitFile,
InitPackage,
InitService,
InitServiceRestartHandle,
InstanceClass,
InstanceSize,
InstanceType,
MachineImage,
UserData,
} from "aws-cdk-lib/aws-ec2";
import { IRole, ManagedPolicy, PolicyDocument, Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { IBucket } from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
import { DockerMachineVersion } from "./docker-machine-version";
import { GitlabRunnerAutoscalingJobRunner } from "./job-runner";
import { Network } from "./network";
import { ConfigurationMapper, GlobalConfiguration, RunnerConfiguration } from "../runner-configuration";
export interface GitlabRunnerAutoscalingManagerBaseProps {
/**
* An Amazon Machine Image ID for the Manager EC2 instance. If empty the latest Amazon 2 Image will be looked up.
*
* Should be RHEL flavor like Amazon Linux 2 with yum available for instance initialization.
*
* @see https://cloudinit.readthedocs.io/en/latest/
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html
*/
readonly machineImage?: IMachineImage;
/**
* Instance type for manager EC2 instance. It's a combination of a class and size.
* @default InstanceType.of(InstanceClass.T3, InstanceSize.NANO)
*/
readonly instanceType?: InstanceType;
/**
* A set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance. You won't be able to ssh into an instance without the Key Pair.
*/
readonly keyPairName?: string;
readonly dockerMachineVersion?: DockerMachineVersion;
}
export interface GitlabRunnerAutoscalingManagerProps extends GitlabRunnerAutoscalingManagerBaseProps {
readonly globalConfiguration?: GlobalConfiguration;
readonly runners: GitlabRunnerAutoscalingJobRunner[];
readonly network: Network;
readonly cacheBucket: IBucket;
readonly role?: IRole;
readonly runnersSecurityGroup: SecurityGroup;
}
const DEFAULT_SSH_KEY_PATH = "/etc/gitlab-runner/ssh-custom";
/**
* Settings for the manager (coordinator)
*
* Manager coordinates the placement of runner (job executor) instances
*/
export class GitlabRunnerAutoscalingManager extends Construct {
readonly machineImage: IMachineImage;
readonly instanceType: InstanceType;
readonly keyPairName?: string;
readonly runners: GitlabRunnerAutoscalingJobRunner[];
readonly network: Network;
readonly runnersSecurityGroupName: string;
readonly role: IRole;
readonly initConfig: CloudFormationInit;
readonly userData: UserData;
readonly cacheBucket: IBucket;
readonly globalConfiguration: GlobalConfiguration;
constructor(scope: Construct, id: string, props: GitlabRunnerAutoscalingManagerProps) {
super(scope, id);
this.globalConfiguration =
props.globalConfiguration ||
({
concurrent: 10,
checkInterval: 0,
logFormat: "runner",
logLevel: "info",
} as GlobalConfiguration);
this.machineImage =
props.machineImage ??
MachineImage.latestAmazonLinux({
generation: AmazonLinuxGeneration.AMAZON_LINUX_2,
edition: AmazonLinuxEdition.STANDARD,
virtualization: AmazonLinuxVirt.HVM,
storage: AmazonLinuxStorage.EBS,
cpuType: AmazonLinuxCpuType.X86_64,
});
this.instanceType = props.instanceType ?? InstanceType.of(InstanceClass.T3, InstanceSize.NANO);
this.keyPairName = props.keyPairName;
this.runners = props.runners;
this.network = props.network;
this.cacheBucket = props.cacheBucket;
this.runnersSecurityGroupName = props.runnersSecurityGroup.securityGroupName;
this.role =
props.role ||
new Role(scope, "ManagerRole", {
assumedBy: new ServicePrincipal("ec2.amazonaws.com", {}),
managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName("AmazonSSMManagedInstanceCore")],
inlinePolicies: {
Cache: PolicyDocument.fromJson({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: ["s3:ListObjects*", "s3:GetObject*", "s3:DeleteObject*", "s3:PutObject*"],
Resource: [`${this.cacheBucket.bucketArn}/*`],
},
{
Effect: "Allow",
Action: ["s3:ListBucket"],
Resource: [`${this.cacheBucket.bucketArn}`],
},
],
}),
Runners: PolicyDocument.fromJson({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: ["ec2:CreateKeyPair", "ec2:DeleteKeyPair", "ec2:ImportKeyPair", "ec2:Describe*"],
Resource: ["*"],
},
{
Effect: "Allow",
Action: ["ec2:CreateTags", "ssm:UpdateInstanceInformation"],
Resource: ["*"],
Condition: {
StringLike: {
"aws:RequestTag/Name": "*gitlab-runner-*",
},
"ForAllValues:StringEquals": {
"aws:TagKeys": ["Name"],
},
},
},
{
Effect: "Allow",
Action: ["ec2:RequestSpotInstances", "ec2:CancelSpotInstanceRequests"],
Resource: ["*"],
Condition: {
StringEqualsIfExists: {
"ec2:Region": `${Stack.of(this).region}`,
},
ArnEqualsIfExists: {
"ec2:Vpc": `arn:aws:ec2:${Stack.of(this).region}:${Stack.of(this).account}:vpc/${
this.network.vpc.vpcId
}`,
},
},
},
{
Effect: "Allow",
Action: ["ec2:RunInstances"],
Resource: ["*"],
Condition: {
"ForAllValues:StringEquals": {
"ec2:InstanceType": (this.runners || []).map((runner) => {
const runnersInstanceType =
(this.runners && runner.instanceType) || InstanceType.of(InstanceClass.T3, InstanceSize.MICRO);
return runnersInstanceType.toString();
}),
},
},
},
{
Effect: "Allow",
Action: ["ec2:TerminateInstances", "ec2:StopInstances", "ec2:StartInstances", "ec2:RebootInstances"],
Resource: ["*"],
Condition: {
StringLike: {
"ec2:ResourceTag/Name": "*gitlab-runner-*",
},
},
},
{
Effect: "Allow",
Action: ["iam:PassRole"],
Resource: ["*"],
Condition: {
"ForAllValues:StringEquals": {
"aws:TagKeys": ["RunnersRole"],
},
},
},
],
}),
ECRLogin: PolicyDocument.fromJson({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: ["ecr:BatchGetImage", "ecr:GetAuthorizationToken", "ecr:GetDownloadUrlForLayer"],
Resource: "*",
},
],
}),
},
});
this.userData = UserData.forLinux({});
this.userData.addCommands(
`yum update -y aws-cfn-bootstrap`, // !/bin/bash -xe
`yum install -y amazon-ecr-credential-helper`
);
// https://github.com/awslabs/amazon-ecr-credential-helper
const userDataRunners = UserData.forLinux({});
userDataRunners.addCommands(
`[ ! -z "$(which apt-get)" ] && apt-get update && apt-get install -y amazon-ecr-credential-helper`,
`[ ! -z "$(which yum)" ] && yum install -y amazon-ecr-credential-helper`
);
const gitlabRunnerConfigRestartHandle = new InitServiceRestartHandle();
gitlabRunnerConfigRestartHandle._addFile("/etc/gitlab-runner/config.toml");
const rsyslogConfigRestartHandle = new InitServiceRestartHandle();
rsyslogConfigRestartHandle._addFile("/etc/rsyslog.d/25-gitlab-runner.conf");
/**
* Config set keys
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html#aws-resource-init-configsets
*/
const REPOSITORIES = "repositories";
const PACKAGES = "packages";
const CONFIG = "config";
const RESTART = "restart";
this.initConfig = CloudFormationInit.fromConfigSets({
configSets: {
default: [REPOSITORIES, PACKAGES, CONFIG, RESTART],
},
configs: {
[REPOSITORIES]: new InitConfig([
InitCommand.shellCommand(
"curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | bash",
{ key: "10-gitlab-runner" }
),
]),
[PACKAGES]: new InitConfig([
InitPackage.yum("docker"),
InitPackage.yum("gitlab-runner"),
InitPackage.yum("tzdata"),
InitPackage.yum("jq"),
InitCommand.shellCommand(
`curl -L https://gitlab-docker-machine-downloads.s3.amazonaws.com/${
props.dockerMachineVersion?.version ?? DockerMachineVersion.V0_16_2_GITLAB_15.version
}/docker-machine-\`uname -s\`-\`uname -m\` > /tmp/docker-machine && install /tmp/docker-machine /usr/bin/docker-machine`,
//"curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-`uname -s`-`uname -m` > /tmp/docker-machine && install /tmp/docker-machine /usr/bin/docker-machine",
{ key: "10-docker-machine" }
),
InitCommand.shellCommand("gitlab-runner start", {
key: "20-gitlab-runner-start",
}),
]),
[CONFIG]: new InitConfig([
InitFile.fromString(
"/etc/gitlab-runner/config.toml",
ConfigurationMapper.withDefaults({
globalConfiguration: this.globalConfiguration,
runnersConfiguration: this.runners.map((runner) => {
const configuration = runner.configuration;
return {
...configuration,
machine: {
...configuration.machine,
machineOptions: {
sshKeypath:
// drivers/amazonec2/amazonec2.go SSHPrivateKeyPath
runner.keyPair
? `${DEFAULT_SSH_KEY_PATH}/${configuration.machine?.machineOptions?.keypairName}`
: "",
...configuration.machine?.machineOptions,
instanceType: runner.instanceType.toString(),
ami: runner.machineImage.getImage(scope).imageId,
region: Stack.of(this).region,
vpcId: this.network.vpc.vpcId,
zone: this.network.availabilityZone.slice(-1),
subnetId: this.network.subnet.subnetId,
securityGroup: this.runnersSecurityGroupName,
privateAddressOnly:
configuration.machine?.machineOptions?.privateAddressOnly ?? this.network.hasPrivateSubnets(),
usePrivateAddress: configuration.machine?.machineOptions?.usePrivateAddress ?? true,
iamInstanceProfile: runner.instanceProfile.ref,
userdata: "/etc/gitlab-runner/user_data_runners",
engineInstallUrl:
configuration.machine?.machineOptions?.engineInstallUrl ??
"https://releases.rancher.com/install-docker/20.10.21.sh",
},
},
cache: {
s3: {
serverAddress: `s3.${Stack.of(this).urlSuffix}`,
bucketName: `${this.cacheBucket.bucketName}`,
bucketLocation: `${Stack.of(this).region}`,
authenticationType: "iam",
},
},
} as RunnerConfiguration;
}),
}).toToml(),
{
owner: "gitlab-runner",
group: "gitlab-runner",
mode: "000600",
}
),
InitFile.fromString(
"/etc/rsyslog.d/25-gitlab-runner.conf",
`:programname, isequal, "gitlab-runner" /var/log/gitlab-runner.log`,
{
owner: "root",
group: "root",
mode: "000644",
}
),
InitService.enable("gitlab-runner", {
ensureRunning: true,
enabled: true,
serviceRestartHandle: gitlabRunnerConfigRestartHandle,
}),
InitService.enable("rsyslog", {
ensureRunning: true,
enabled: true,
serviceRestartHandle: rsyslogConfigRestartHandle,
}),
InitCommand.shellCommand(
// Download custom EC2 key pair for manager <> runner ssh connect
this.runners
.map((runner) => {
if (!runner.keyPair) {
return "";
}
runner.keyPair.grantRead(this.role);
const region = Stack.of(this).region;
const secretArn = runner.keyPair.secretArn;
const keyPairName = runner.configuration.machine!.machineOptions!.keypairName;
const sshKeyPath = runner.configuration.machine!.machineOptions!.sshKeypath ?? DEFAULT_SSH_KEY_PATH;
return [
`mkdir -p ${sshKeyPath};`,
`echo $(aws secretsmanager get-secret-value --region ${region} --secret-id ${secretArn} --query SecretString --output text) | jq -r '."${keyPairName}"' > ${sshKeyPath}/${keyPairName};`,
`echo $(aws secretsmanager get-secret-value --region ${region} --secret-id ${secretArn} --query SecretString --output text) | jq -r '."${keyPairName}.pub"' > ${sshKeyPath}/${keyPairName}.pub;`,
].join("\n");
})
.filter((s) => s.length > 0)
.join("\n"),
{
key: "999-retrieve-ec2-key-pair",
}
),
InitFile.fromString("/etc/gitlab-runner/user_data_runners", userDataRunners.render(), {
owner: "gitlab-runner",
group: "gitlab-runner",
mode: "000600",
}),
]),
[RESTART]: new InitConfig([
InitCommand.shellCommand("gitlab-runner restart", {
key: "10-gitlab-runner-restart",
}),
]),
},
});
}
}