Skip to content

Commit fc150da

Browse files
committed
refactor
1 parent 480bc64 commit fc150da

File tree

1 file changed

+63
-76
lines changed
  • modules/runners/lambdas/runners/src/scale-runners

1 file changed

+63
-76
lines changed

modules/runners/lambdas/runners/src/scale-runners/scale-up.ts

Lines changed: 63 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,44 @@ export interface ActionRequestMessage {
1515
installationId: number;
1616
}
1717

18-
function generateRunnerServiceConfig(
19-
runnerExtraLabels: string | undefined,
20-
runnerGroup: string | undefined,
21-
ghesBaseUrl: string,
22-
ephemeral: boolean,
23-
token: any,
24-
runnerType: 'Org' | 'Repo',
25-
payload: ActionRequestMessage,
26-
) {
27-
const labelsArgument = runnerExtraLabels !== undefined ? `--labels ${runnerExtraLabels} ` : '';
28-
const runnerGroupArgument = runnerGroup !== undefined ? `--runnergroup ${runnerGroup} ` : '';
29-
const configBaseUrl = ghesBaseUrl ? ghesBaseUrl : 'https://github.com';
30-
const ephemeralArgument = ephemeral ? '--ephemeral ' : '';
18+
interface CreateGitHubRunnerConfig {
19+
ephemeral: boolean;
20+
ghesBaseUrl: string;
21+
runnerExtraLabels: string | undefined;
22+
runnerGroup: string | undefined;
23+
runnerOwner: string;
24+
runnerType: 'Org' | 'Repo';
25+
}
26+
27+
interface CreateEC2RunnerConfig {
28+
environment: string;
29+
subnets: string[];
30+
launchTemplateName: string;
31+
ec2instanceCriteria: RunnerInputParameters['ec2instanceCriteria'];
32+
}
33+
34+
function generateRunnerServiceConfig(githubRunnerConfig: CreateGitHubRunnerConfig, token: any) {
35+
const labelsArgument =
36+
githubRunnerConfig.runnerExtraLabels !== undefined ? `--labels ${githubRunnerConfig.runnerExtraLabels} ` : '';
37+
const runnerGroupArgument =
38+
githubRunnerConfig.runnerGroup !== undefined ? `--runnergroup ${githubRunnerConfig.runnerGroup} ` : '';
39+
const configBaseUrl = githubRunnerConfig.ghesBaseUrl ? githubRunnerConfig.ghesBaseUrl : 'https://github.com';
40+
const ephemeralArgument = githubRunnerConfig.ephemeral ? '--ephemeral ' : '';
3141
const runnerArgs = `--token ${token} ${labelsArgument}${ephemeralArgument}`;
32-
return runnerType === 'Org'
33-
? `--url ${configBaseUrl}/${payload.repositoryOwner} ${runnerArgs}${runnerGroupArgument}`.trim()
34-
: `--url ${configBaseUrl}/${payload.repositoryOwner}/${payload.repositoryName} ${runnerArgs}`.trim();
42+
return githubRunnerConfig.runnerType === 'Org'
43+
? `--url ${configBaseUrl}/${githubRunnerConfig.runnerOwner} ${runnerArgs}${runnerGroupArgument}`.trim()
44+
: `--url ${configBaseUrl}/${githubRunnerConfig.runnerOwner} ${runnerArgs}`.trim();
3545
}
3646

37-
async function getGithubRunnerRegistrationToken(
38-
enableOrgLevel: boolean,
39-
githubInstallationClient: Octokit,
40-
payload: ActionRequestMessage,
41-
) {
42-
const registrationToken = enableOrgLevel
43-
? await githubInstallationClient.actions.createRegistrationTokenForOrg({ org: payload.repositoryOwner })
44-
: await githubInstallationClient.actions.createRegistrationTokenForRepo({
45-
owner: payload.repositoryOwner,
46-
repo: payload.repositoryName,
47-
});
48-
const token = registrationToken.data.token;
49-
return token;
47+
async function getGithubRunnerRegistrationToken(githubRunnerConfig: CreateGitHubRunnerConfig, ghClient: Octokit) {
48+
const registrationToken =
49+
githubRunnerConfig.runnerType === 'Org'
50+
? await ghClient.actions.createRegistrationTokenForOrg({ org: githubRunnerConfig.runnerOwner })
51+
: await ghClient.actions.createRegistrationTokenForRepo({
52+
owner: githubRunnerConfig.runnerOwner.split('/')[0],
53+
repo: githubRunnerConfig.runnerOwner.split('/')[1],
54+
});
55+
return registrationToken.data.token;
5056
}
5157

5258
async function getInstallationId(ghesApiUrl: string, enableOrgLevel: boolean, payload: ActionRequestMessage) {
@@ -95,41 +101,20 @@ async function isJobQueued(githubInstallationClient: Octokit, payload: ActionReq
95101
return isQueued;
96102
}
97103

98-
async function createRunners(
99-
enableOrgLevel: boolean,
100-
githubInstallationClient: Octokit,
101-
payload: ActionRequestMessage,
102-
runnerExtraLabels: string | undefined,
103-
runnerGroup: string | undefined,
104-
ghesBaseUrl: string,
105-
ephemeral: boolean,
106-
runnerType: 'Org' | 'Repo',
107-
environment: string,
108-
runnerOwner: string,
109-
subnets: string[],
110-
launchTemplateName: string,
111-
ec2instanceCriteria: RunnerInputParameters['ec2instanceCriteria'],
104+
export async function createRunners(
105+
githubRunnerConfig: CreateGitHubRunnerConfig,
106+
ec2RunnerConfig: CreateEC2RunnerConfig,
107+
ghClient: Octokit,
112108
): Promise<void> {
113-
const token = await getGithubRunnerRegistrationToken(enableOrgLevel, githubInstallationClient, payload);
114-
115-
const runnerServiceConfig = generateRunnerServiceConfig(
116-
runnerExtraLabels,
117-
runnerGroup,
118-
ghesBaseUrl,
119-
ephemeral,
120-
token,
121-
runnerType,
122-
payload,
123-
);
109+
const token = await getGithubRunnerRegistrationToken(githubRunnerConfig, ghClient);
110+
111+
const runnerServiceConfig = generateRunnerServiceConfig(githubRunnerConfig, token);
124112

125113
await createRunner({
126-
environment,
127114
runnerServiceConfig,
128-
runnerOwner,
129-
runnerType,
130-
subnets,
131-
launchTemplateName,
132-
ec2instanceCriteria,
115+
runnerType: githubRunnerConfig.runnerType,
116+
runnerOwner: githubRunnerConfig.runnerOwner,
117+
...ec2RunnerConfig,
133118
});
134119
}
135120

@@ -197,24 +182,26 @@ export async function scaleUp(eventSource: string, payload: ActionRequestMessage
197182
logger.info(`Attempting to launch a new runner`, LogFields.print());
198183

199184
await createRunners(
200-
enableOrgLevel,
201-
githubInstallationClient,
202-
payload,
203-
runnerExtraLabels,
204-
runnerGroup,
205-
ghesBaseUrl,
206-
ephemeral,
207-
runnerType,
208-
environment,
209-
runnerOwner,
210-
subnets,
211-
launchTemplateName,
212185
{
213-
instanceTypes,
214-
targetCapacityType: instanceTargetTargetCapacityType,
215-
maxSpotPrice: instanceMaxSpotPrice,
216-
instanceAllocationStrategy: instanceAllocationStrategy,
186+
ephemeral,
187+
ghesBaseUrl,
188+
runnerExtraLabels,
189+
runnerGroup,
190+
runnerOwner,
191+
runnerType,
217192
},
193+
{
194+
ec2instanceCriteria: {
195+
instanceTypes,
196+
targetCapacityType: instanceTargetTargetCapacityType,
197+
maxSpotPrice: instanceMaxSpotPrice,
198+
instanceAllocationStrategy: instanceAllocationStrategy,
199+
},
200+
environment,
201+
launchTemplateName,
202+
subnets,
203+
},
204+
githubInstallationClient,
218205
);
219206
} else {
220207
logger.info('No runner will be created, maximum number of runners reached.', LogFields.print());

0 commit comments

Comments
 (0)