unity-builder/src/model/cli/cli.ts

159 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-02-01 02:31:20 +00:00
import { Command } from 'commander-ts';
import { BuildParameters, CloudRunner, ImageTag, Input } from '..';
import * as core from '@actions/core';
import { ActionYamlReader } from '../input-readers/action-yaml';
import CloudRunnerLogger from '../cloud-runner/services/cloud-runner-logger';
import { CliFunction, GetAllCliModes, GetCliFunctions } from './cli-decorator';
import { RemoteClientLogger } from './remote-client/remote-client-services/remote-client-logger';
import { SetupCloudRunnerRepository } from './remote-client/setup-cloud-runner-repository';
import * as SDK from 'aws-sdk';
2022-04-04 21:05:46 +00:00
import { Caching } from './remote-client/remote-client-services/caching';
2022-04-04 21:44:44 +00:00
import CloudRunnerQueryOverride from '../cloud-runner/services/cloud-runner-query-override';
2022-04-06 23:33:26 +00:00
import { LFSHashing } from './remote-client/remote-client-services/lfs-hashing';
2022-02-01 02:31:20 +00:00
export class CLI {
2022-04-04 22:09:26 +00:00
public static options;
static get cliMode() {
return CLI.options !== undefined && CLI.options.mode !== undefined && CLI.options.mode !== '';
}
2022-04-05 02:58:57 +00:00
public static query(key, alternativeKey) {
if (CLI.options && CLI.options[key] !== undefined) {
return CLI.options[key];
}
if (CLI.options && alternativeKey && CLI.options[alternativeKey] !== undefined) {
return CLI.options[alternativeKey];
}
return;
2022-04-04 22:09:26 +00:00
}
2022-02-01 02:31:20 +00:00
public static InitCliMode() {
2022-02-01 02:31:20 +00:00
const program = new Command();
program.version('0.0.1');
const properties = Object.getOwnPropertyNames(Input);
const actionYamlReader: ActionYamlReader = new ActionYamlReader();
for (const element of properties) {
program.option(`--${element} <${element}>`, actionYamlReader.GetActionYamlValue(element));
}
program.option(
'-m, --mode <mode>',
GetAllCliModes()
.map((x) => `${x.key} (${x.description})`)
.join(` | `),
);
2022-04-04 22:09:26 +00:00
program.option('--populateOverride <populateOverride>', 'should use override query to pull input false by default');
2022-04-04 21:44:44 +00:00
program.option('--cachePushFrom <cachePushFrom>', 'cache push from source folder');
program.option('--cachePushTo <cachePushTo>', 'cache push to caching folder');
program.option('--artifactName <artifactName>', 'caching artifact name');
2022-02-01 02:31:20 +00:00
program.parse(process.argv);
CLI.options = program.opts();
2022-04-04 22:09:26 +00:00
return CLI.cliMode;
}
2022-02-01 02:31:20 +00:00
static async RunCli(): Promise<void> {
Input.githubInputEnabled = false;
2022-04-04 22:09:26 +00:00
if (CLI.options['populateOverride'] === `true`) {
2022-04-04 21:44:44 +00:00
await CloudRunnerQueryOverride.PopulateQueryOverrideInput();
2022-04-04 21:05:46 +00:00
}
CLI.logInput();
const results = GetCliFunctions(CLI.options.mode);
CloudRunnerLogger.log(`Entrypoint: ${results.key}`);
CLI.options.versioning = 'None';
return await results.target[results.propertyKey]();
}
private static logInput() {
core.info(`\n`);
core.info(`INPUT:`);
const properties = Object.getOwnPropertyNames(Input);
for (const element of properties) {
if (
Input[element] !== undefined &&
Input[element] !== '' &&
typeof Input[element] !== `function` &&
element !== 'length' &&
element !== 'cliOptions' &&
element !== 'prototype'
) {
core.info(`${element} ${Input[element]}`);
}
}
core.info(`\n`);
2022-02-01 02:31:20 +00:00
}
@CliFunction(`cli`, `runs a cloud runner build`)
public static async CLIBuild(): Promise<string> {
const buildParameter = await BuildParameters.create();
const baseImage = new ImageTag(buildParameter);
return await CloudRunner.run(buildParameter, baseImage.toString());
}
@CliFunction(`remote-cli`, `sets up a repository, usually before a game-ci build`)
static async runRemoteClientJob() {
const buildParameter = JSON.parse(process.env.BUILD_PARAMETERS || '{}');
RemoteClientLogger.log(`Build Params:
${JSON.stringify(buildParameter, undefined, 4)}
`);
CloudRunner.buildParameters = buildParameter;
2022-02-01 02:31:20 +00:00
await SetupCloudRunnerRepository.run();
}
2022-04-04 19:54:48 +00:00
@CliFunction(`cache-push`, `push to cache`)
2022-04-04 21:05:46 +00:00
static async cachePush() {
try {
const buildParameter = JSON.parse(process.env.BUILD_PARAMETERS || '{}');
CloudRunner.buildParameters = buildParameter;
2022-04-06 23:54:57 +00:00
await Caching.PushToCache(
CLI.options['cachePushTo'],
CLI.options['cachePushFrom'],
CLI.options['artifactName'] || '',
);
} catch (error: any) {
CloudRunnerLogger.log(`${error}`);
}
2022-04-04 21:05:46 +00:00
}
2022-02-01 02:31:20 +00:00
2022-04-04 19:54:48 +00:00
@CliFunction(`cache-pull`, `pull from cache`)
2022-04-06 22:52:48 +00:00
static async cachePull() {
try {
const buildParameter = JSON.parse(process.env.BUILD_PARAMETERS || '{}');
CloudRunner.buildParameters = buildParameter;
await Caching.PullFromCache(
CLI.options['cachePushFrom'],
CLI.options['cachePushTo'],
2022-04-06 23:54:57 +00:00
CLI.options['artifactName'] || '',
2022-04-06 22:52:48 +00:00
);
} catch (error: any) {
CloudRunnerLogger.log(`${error}`);
}
}
2022-02-01 02:31:20 +00:00
@CliFunction(`garbage-collect-aws`, `garbage collect aws`)
static async garbageCollectAws() {
process.env.AWS_REGION = Input.region;
const CF = new SDK.CloudFormation();
2022-04-06 23:06:06 +00:00
const stacks = (await CF.listStacks().promise()).StackSummaries?.filter(
(_x) => _x.StackStatus !== 'DELETE_COMPLETE',
);
if (stacks === undefined) {
return;
}
CloudRunnerLogger.log(`Cloud Formation stacks`);
for (const element of stacks) {
CloudRunnerLogger.log(JSON.stringify(element, undefined, 4));
await CF.deleteStack({ StackName: element.StackName }).promise();
}
CloudRunnerLogger.log(`ECS Clusters`);
const ecs = new SDK.ECS();
CloudRunnerLogger.log(JSON.stringify(await ecs.listClusters().promise(), undefined, 4));
CloudRunnerLogger.log(JSON.stringify(await ecs.describeClusters().promise(), undefined, 4));
2022-02-01 02:31:20 +00:00
}
2022-04-06 23:33:26 +00:00
@CliFunction(`hash`, `hash all folder contents`)
static async hash() {
const folder = CLI.options['cachePushFrom'];
LFSHashing.hashAllFiles(folder);
}
2022-02-01 02:31:20 +00:00
}