-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathscale-up.ts
464 lines (415 loc) · 16.6 KB
/
scale-up.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { Octokit } from '@octokit/rest';
import { addPersistentContextToChildLogger, createChildLogger } from '@aws-github-runner/aws-powertools-util';
import { getParameter, putParameter } from '@aws-github-runner/aws-ssm-util';
import yn from 'yn';
import { createGithubAppAuth, createGithubInstallationAuth, createOctokitClient } from '../github/auth';
import { createRunner, listEC2Runners } from './../aws/runners';
import { RunnerInputParameters } from './../aws/runners.d';
import ScaleError from './ScaleError';
import { publishRetryMessage } from './job-retry';
import { metricGitHubAppRateLimit } from '../github/rate-limit';
const logger = createChildLogger('scale-up');
export interface RunnerGroup {
name: string;
id: number;
}
interface EphemeralRunnerConfig {
runnerName: string;
runnerGroupId: number;
runnerLabels: string[];
}
export interface ActionRequestMessage {
id: number;
eventType: 'check_run' | 'workflow_job';
repositoryName: string;
repositoryOwner: string;
installationId: number;
repoOwnerType: string;
retryCounter?: number;
}
export interface ActionRequestMessageRetry extends ActionRequestMessage {
retryCounter: number;
}
interface CreateGitHubRunnerConfig {
ephemeral: boolean;
ghesBaseUrl: string;
enableJitConfig: boolean;
runnerLabels: string;
runnerGroup: string;
runnerNamePrefix: string;
runnerOwner: string;
runnerType: 'Org' | 'Repo';
disableAutoUpdate: boolean;
ssmTokenPath: string;
ssmConfigPath: string;
}
interface CreateEC2RunnerConfig {
environment: string;
subnets: string[];
launchTemplateName: string;
ec2instanceCriteria: RunnerInputParameters['ec2instanceCriteria'];
numberOfRunners?: number;
amiIdSsmParameterName?: string;
tracingEnabled?: boolean;
onDemandFailoverOnError?: string[];
}
function generateRunnerServiceConfig(githubRunnerConfig: CreateGitHubRunnerConfig, token: string) {
const config = [
`--url ${githubRunnerConfig.ghesBaseUrl ?? 'https://github.com'}/${githubRunnerConfig.runnerOwner}`,
`--token ${token}`,
];
if (githubRunnerConfig.runnerLabels) {
config.push(`--labels ${githubRunnerConfig.runnerLabels}`.trim());
}
if (githubRunnerConfig.disableAutoUpdate) {
config.push('--disableupdate');
}
if (githubRunnerConfig.runnerType === 'Org' && githubRunnerConfig.runnerGroup !== undefined) {
config.push(`--runnergroup ${githubRunnerConfig.runnerGroup}`);
}
if (githubRunnerConfig.ephemeral) {
config.push(`--ephemeral`);
}
return config;
}
async function getGithubRunnerRegistrationToken(githubRunnerConfig: CreateGitHubRunnerConfig, ghClient: Octokit) {
const registrationToken =
githubRunnerConfig.runnerType === 'Org'
? await ghClient.actions.createRegistrationTokenForOrg({ org: githubRunnerConfig.runnerOwner })
: await ghClient.actions.createRegistrationTokenForRepo({
owner: githubRunnerConfig.runnerOwner.split('/')[0],
repo: githubRunnerConfig.runnerOwner.split('/')[1],
});
const appId = parseInt(await getParameter(process.env.PARAMETER_GITHUB_APP_ID_NAME));
logger.info('App id from SSM', { appId: appId });
return registrationToken.data.token;
}
function removeTokenFromLogging(config: string[]): string[] {
const result: string[] = [];
config.forEach((e) => {
if (e.startsWith('--token')) {
result.push('--token <REDACTED>');
} else {
result.push(e);
}
});
return result;
}
export async function getInstallationId(
ghesApiUrl: string,
enableOrgLevel: boolean,
payload: ActionRequestMessage,
): Promise<number> {
if (payload.installationId !== 0) {
return payload.installationId;
}
const ghAuth = await createGithubAppAuth(undefined, ghesApiUrl);
const githubClient = await createOctokitClient(ghAuth.token, ghesApiUrl);
return enableOrgLevel
? (
await githubClient.apps.getOrgInstallation({
org: payload.repositoryOwner,
})
).data.id
: (
await githubClient.apps.getRepoInstallation({
owner: payload.repositoryOwner,
repo: payload.repositoryName,
})
).data.id;
}
export async function isJobQueued(githubInstallationClient: Octokit, payload: ActionRequestMessage): Promise<boolean> {
let isQueued = false;
if (payload.eventType === 'workflow_job') {
const jobForWorkflowRun = await githubInstallationClient.actions.getJobForWorkflowRun({
job_id: payload.id,
owner: payload.repositoryOwner,
repo: payload.repositoryName,
});
metricGitHubAppRateLimit(jobForWorkflowRun.headers);
isQueued = jobForWorkflowRun.data.status === 'queued';
logger.debug(`The job ${payload.id} is${isQueued ? ' ' : 'not'} queued`);
} else {
throw Error(`Event ${payload.eventType} is not supported`);
}
return isQueued;
}
async function getRunnerGroupId(githubRunnerConfig: CreateGitHubRunnerConfig, ghClient: Octokit): Promise<number> {
// if the runnerType is Repo, then runnerGroupId is default to 1
let runnerGroupId: number | undefined = 1;
if (githubRunnerConfig.runnerType === 'Org' && githubRunnerConfig.runnerGroup !== undefined) {
let runnerGroup: string | undefined;
// check if runner group id is already stored in SSM Parameter Store and
// use it if it exists to avoid API call to GitHub
try {
runnerGroup = await getParameter(
`${githubRunnerConfig.ssmConfigPath}/runner-group/${githubRunnerConfig.runnerGroup}`,
);
} catch (err) {
logger.debug('Handling error:', err as Error);
logger.warn(
`SSM Parameter "${githubRunnerConfig.ssmConfigPath}/runner-group/${githubRunnerConfig.runnerGroup}"
for Runner group ${githubRunnerConfig.runnerGroup} does not exist`,
);
}
if (runnerGroup === undefined) {
// get runner group id from GitHub
runnerGroupId = await getRunnerGroupByName(ghClient, githubRunnerConfig);
// store runner group id in SSM
try {
await putParameter(
`${githubRunnerConfig.ssmConfigPath}/runner-group/${githubRunnerConfig.runnerGroup}`,
runnerGroupId.toString(),
false,
);
} catch (err) {
logger.debug('Error storing runner group id in SSM Parameter Store', err as Error);
throw err;
}
} else {
runnerGroupId = parseInt(runnerGroup);
}
}
return runnerGroupId;
}
async function getRunnerGroupByName(ghClient: Octokit, githubRunnerConfig: CreateGitHubRunnerConfig): Promise<number> {
const runnerGroups: RunnerGroup[] = await ghClient.paginate(`GET /orgs/{org}/actions/runner-groups`, {
org: githubRunnerConfig.runnerOwner,
per_page: 100,
});
const runnerGroupId = runnerGroups.find((runnerGroup) => runnerGroup.name === githubRunnerConfig.runnerGroup)?.id;
if (runnerGroupId === undefined) {
throw new Error(`Runner group ${githubRunnerConfig.runnerGroup} does not exist`);
}
return runnerGroupId;
}
export async function createRunners(
githubRunnerConfig: CreateGitHubRunnerConfig,
ec2RunnerConfig: CreateEC2RunnerConfig,
ghClient: Octokit,
): Promise<void> {
const instances = await createRunner({
runnerType: githubRunnerConfig.runnerType,
runnerOwner: githubRunnerConfig.runnerOwner,
numberOfRunners: 1,
...ec2RunnerConfig,
});
if (instances.length !== 0) {
await createStartRunnerConfig(githubRunnerConfig, instances, ghClient);
}
}
export async function scaleUp(eventSource: string, payload: ActionRequestMessage): Promise<void> {
logger.info(`Received ${payload.eventType} from ${payload.repositoryOwner}/${payload.repositoryName}`);
if (eventSource !== 'aws:sqs') throw Error('Cannot handle non-SQS events!');
const enableOrgLevel = yn(process.env.ENABLE_ORGANIZATION_RUNNERS, { default: true });
const maximumRunners = parseInt(process.env.RUNNERS_MAXIMUM_COUNT || '3');
const runnerLabels = process.env.RUNNER_LABELS || '';
const runnerGroup = process.env.RUNNER_GROUP_NAME || 'Default';
const environment = process.env.ENVIRONMENT;
const ssmTokenPath = process.env.SSM_TOKEN_PATH;
const subnets = process.env.SUBNET_IDS.split(',');
const instanceTypes = process.env.INSTANCE_TYPES.split(',');
const instanceTargetCapacityType = process.env.INSTANCE_TARGET_CAPACITY_TYPE;
const ephemeralEnabled = yn(process.env.ENABLE_EPHEMERAL_RUNNERS, { default: false });
const enableJitConfig = yn(process.env.ENABLE_JIT_CONFIG, { default: ephemeralEnabled });
const disableAutoUpdate = yn(process.env.DISABLE_RUNNER_AUTOUPDATE, { default: false });
const launchTemplateName = process.env.LAUNCH_TEMPLATE_NAME;
const instanceMaxSpotPrice = process.env.INSTANCE_MAX_SPOT_PRICE;
const instanceAllocationStrategy = process.env.INSTANCE_ALLOCATION_STRATEGY || 'lowest-price'; // same as AWS default
const enableJobQueuedCheck = yn(process.env.ENABLE_JOB_QUEUED_CHECK, { default: true });
const amiIdSsmParameterName = process.env.AMI_ID_SSM_PARAMETER_NAME;
const runnerNamePrefix = process.env.RUNNER_NAME_PREFIX || '';
const ssmConfigPath = process.env.SSM_CONFIG_PATH || '';
const tracingEnabled = yn(process.env.POWERTOOLS_TRACE_ENABLED, { default: false });
const onDemandFailoverOnError = process.env.ENABLE_ON_DEMAND_FAILOVER_FOR_ERRORS
? (JSON.parse(process.env.ENABLE_ON_DEMAND_FAILOVER_FOR_ERRORS) as [string])
: [];
if (ephemeralEnabled && payload.eventType !== 'workflow_job') {
logger.warn(`${payload.eventType} event is not supported in combination with ephemeral runners.`);
throw Error(
`The event type ${payload.eventType} is not supported in combination with ephemeral runners.` +
`Please ensure you have enabled workflow_job events.`,
);
}
if (!isValidRepoOwnerTypeIfOrgLevelEnabled(payload, enableOrgLevel)) {
logger.warn(
`Repository ${payload.repositoryOwner}/${payload.repositoryName} does not belong to a GitHub` +
`organization and organization runners are enabled. This is not supported. Not scaling up for this event.` +
`Not throwing error to prevent re-queueing and just ignoring the event.`,
);
return;
}
const ephemeral = ephemeralEnabled && payload.eventType === 'workflow_job';
const runnerType = enableOrgLevel ? 'Org' : 'Repo';
const runnerOwner = enableOrgLevel ? payload.repositoryOwner : `${payload.repositoryOwner}/${payload.repositoryName}`;
addPersistentContextToChildLogger({
runner: {
type: runnerType,
owner: runnerOwner,
namePrefix: runnerNamePrefix,
},
github: {
event: payload.eventType,
workflow_job_id: payload.id.toString(),
},
});
logger.info(`Received event`);
const { ghesApiUrl, ghesBaseUrl } = getGitHubEnterpriseApiUrl();
const installationId = await getInstallationId(ghesApiUrl, enableOrgLevel, payload);
const ghAuth = await createGithubInstallationAuth(installationId, ghesApiUrl);
const githubInstallationClient = await createOctokitClient(ghAuth.token, ghesApiUrl);
if (!enableJobQueuedCheck || (await isJobQueued(githubInstallationClient, payload))) {
let scaleUp = true;
if (maximumRunners !== -1) {
const currentRunners = await listEC2Runners({
environment,
runnerType,
runnerOwner,
});
logger.info(`Current runners: ${currentRunners.length} of ${maximumRunners}`);
scaleUp = currentRunners.length < maximumRunners;
}
if (scaleUp) {
logger.info(`Attempting to launch a new runner`);
await createRunners(
{
ephemeral,
enableJitConfig,
ghesBaseUrl,
runnerLabels,
runnerGroup,
runnerNamePrefix,
runnerOwner,
runnerType,
disableAutoUpdate,
ssmTokenPath,
ssmConfigPath,
},
{
ec2instanceCriteria: {
instanceTypes,
targetCapacityType: instanceTargetCapacityType,
maxSpotPrice: instanceMaxSpotPrice,
instanceAllocationStrategy: instanceAllocationStrategy,
},
environment,
launchTemplateName,
subnets,
amiIdSsmParameterName,
tracingEnabled,
onDemandFailoverOnError,
},
githubInstallationClient,
);
await publishRetryMessage(payload);
} else {
logger.info('No runner will be created, maximum number of runners reached.');
if (ephemeral) {
throw new ScaleError('No runners create: maximum of runners reached.');
}
}
} else {
logger.info('No runner will be created, job is not queued.');
}
}
export function getGitHubEnterpriseApiUrl() {
const ghesBaseUrl = process.env.GHES_URL;
let ghesApiUrl = '';
if (ghesBaseUrl) {
const url = new URL(ghesBaseUrl);
const domain = url.hostname;
if (domain.endsWith('.ghe.com')) {
// Data residency: Prepend 'api.'
ghesApiUrl = `https://api.${domain}`;
} else {
// GitHub Enterprise Server: Append '/api/v3'
ghesApiUrl = `${ghesBaseUrl}/api/v3`;
}
}
logger.debug(`Github Enterprise URLs: api_url - ${ghesApiUrl}; base_url - ${ghesBaseUrl}`);
return { ghesApiUrl, ghesBaseUrl };
}
async function createStartRunnerConfig(
githubRunnerConfig: CreateGitHubRunnerConfig,
instances: string[],
ghClient: Octokit,
) {
if (githubRunnerConfig.enableJitConfig && githubRunnerConfig.ephemeral) {
await createJitConfig(githubRunnerConfig, instances, ghClient);
} else {
await createRegistrationTokenConfig(githubRunnerConfig, instances, ghClient);
}
}
function isValidRepoOwnerTypeIfOrgLevelEnabled(payload: ActionRequestMessage, enableOrgLevel: boolean): boolean {
return !(enableOrgLevel && payload.repoOwnerType !== 'Organization');
}
function addDelay(instances: string[]) {
const delay = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const ssmParameterStoreMaxThroughput = 40;
const isDelay = instances.length >= ssmParameterStoreMaxThroughput;
return { isDelay, delay };
}
async function createRegistrationTokenConfig(
githubRunnerConfig: CreateGitHubRunnerConfig,
instances: string[],
ghClient: Octokit,
) {
const { isDelay, delay } = addDelay(instances);
const token = await getGithubRunnerRegistrationToken(githubRunnerConfig, ghClient);
const runnerServiceConfig = generateRunnerServiceConfig(githubRunnerConfig, token);
logger.debug('Runner service config for non-ephemeral runners', {
runner_service_config: removeTokenFromLogging(runnerServiceConfig),
});
for (const instance of instances) {
await putParameter(`${githubRunnerConfig.ssmTokenPath}/${instance}`, runnerServiceConfig.join(' '), true, {
tags: [{ Key: 'InstanceId', Value: instance }],
});
if (isDelay) {
// Delay to prevent AWS ssm rate limits by being within the max throughput limit
await delay(25);
}
}
}
async function createJitConfig(githubRunnerConfig: CreateGitHubRunnerConfig, instances: string[], ghClient: Octokit) {
const runnerGroupId = await getRunnerGroupId(githubRunnerConfig, ghClient);
const { isDelay, delay } = addDelay(instances);
const runnerLabels = githubRunnerConfig.runnerLabels.split(',');
logger.debug(`Runner group id: ${runnerGroupId}`);
logger.debug(`Runner labels: ${runnerLabels}`);
for (const instance of instances) {
// generate jit config for runner registration
const ephemeralRunnerConfig: EphemeralRunnerConfig = {
runnerName: `${githubRunnerConfig.runnerNamePrefix}${instance}`,
runnerGroupId: runnerGroupId,
runnerLabels: runnerLabels,
};
logger.debug(`Runner name: ${ephemeralRunnerConfig.runnerName}`);
const runnerConfig =
githubRunnerConfig.runnerType === 'Org'
? await ghClient.actions.generateRunnerJitconfigForOrg({
org: githubRunnerConfig.runnerOwner,
name: ephemeralRunnerConfig.runnerName,
runner_group_id: ephemeralRunnerConfig.runnerGroupId,
labels: ephemeralRunnerConfig.runnerLabels,
})
: await ghClient.actions.generateRunnerJitconfigForRepo({
owner: githubRunnerConfig.runnerOwner.split('/')[0],
repo: githubRunnerConfig.runnerOwner.split('/')[1],
name: ephemeralRunnerConfig.runnerName,
runner_group_id: ephemeralRunnerConfig.runnerGroupId,
labels: ephemeralRunnerConfig.runnerLabels,
});
metricGitHubAppRateLimit(runnerConfig.headers);
// store jit config in ssm parameter store
logger.debug('Runner JIT config for ephemeral runner generated.', {
instance: instance,
});
await putParameter(`${githubRunnerConfig.ssmTokenPath}/${instance}`, runnerConfig.data.encoded_jit_config, true, {
tags: [{ Key: 'InstanceId', Value: instance }],
});
if (isDelay) {
// Delay to prevent AWS ssm rate limits by being within the max throughput limit
await delay(25);
}
}
}