unity-builder/src/model/aws.js

130 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-01-31 19:22:28 +00:00
/* eslint-disable no-plusplus */
/* eslint-disable no-await-in-loop */
import * as SDK from 'aws-sdk';
2021-02-07 00:37:34 +00:00
import { CloudWatch, CloudWatchLogs } from 'aws-sdk';
2021-01-31 20:03:11 +00:00
2021-01-31 19:22:28 +00:00
const fs = require('fs');
const core = require('@actions/core');
2021-02-07 00:37:34 +00:00
const Shortid = require('shortid');
2021-01-31 19:22:28 +00:00
class AWS {
static async runBuildJob(buildParameters, baseImage) {
2021-01-31 22:11:13 +00:00
await this.run(
buildParameters.awsStackName,
'alpine/git',
2021-02-05 23:33:28 +00:00
['clone', `https://github.com/${process.env.GITHUB_REPOSITORY}.git`, `repo`],
2021-01-31 22:11:13 +00:00
[
{
name: 'GITHUB_SHA',
value: process.env.GITHUB_SHA,
},
],
);
await this.run(
buildParameters.awsStackName,
baseImage.toString(),
['bin/bash', '-c', 'echo "test"'],
2021-02-07 00:37:34 +00:00
[],
2021-01-31 22:11:13 +00:00
);
2021-01-31 19:22:28 +00:00
}
2021-01-31 22:11:13 +00:00
static async run(stackName, image, commands, environment) {
2021-01-31 19:22:28 +00:00
const ECS = new SDK.ECS();
const CF = new SDK.CloudFormation();
2021-01-31 22:11:13 +00:00
const alphanumericImageName = image.toString().replace(/[^\da-z]/gi, '');
2021-02-07 00:37:34 +00:00
const taskDefStackName = `${stackName}-taskDef-${alphanumericImageName}-${new Shortid()}`;
2021-01-31 19:22:28 +00:00
const stackExists =
(await CF.listStacks().promise()).StackSummaries.find(
(x) => x.StackName === taskDefStackName,
) !== undefined;
if (!stackExists) {
core.info("Task Definition doesn't exist, creating a task definition stack");
const taskDefCloudFormation = fs.readFileSync(`${__dirname}/task-def-formation.yml`, 'utf8');
2021-01-31 19:22:28 +00:00
await CF.createStack({
StackName: taskDefStackName,
TemplateBody: taskDefCloudFormation,
Parameters: [
{
ParameterKey: 'ImageUrl',
ParameterValue: image,
},
2021-02-07 00:37:34 +00:00
{
ParameterKey: 'Entrypoint',
ParameterValue: commands,
},
2021-01-31 19:22:28 +00:00
],
}).promise();
await CF.waitFor('stackCreateComplete', { StackName: taskDefStackName }).promise();
} else {
core.info('Task definition stack exists already');
}
const taskDefResources = await CF.describeStackResources({
StackName: taskDefStackName,
}).promise();
const baseResources = await CF.describeStackResources({ StackName: stackName }).promise();
const clusterName = baseResources.StackResources.find(
(x) => x.LogicalResourceId === 'ECSCluster',
).PhysicalResourceId;
const task = await ECS.runTask({
cluster: clusterName,
taskDefinition: taskDefResources.StackResources.find(
(x) => x.LogicalResourceId === 'TaskDefinition',
).PhysicalResourceId,
platformVersion: '1.4.0',
launchType: 'FARGATE',
overrides: {
containerOverrides: [
{
name: 'example',
2021-01-31 22:11:13 +00:00
environment,
2021-01-31 19:22:28 +00:00
},
],
},
networkConfiguration: {
awsvpcConfiguration: {
subnets: [
baseResources.StackResources.find((x) => x.LogicalResourceId === 'PublicSubnetOne')
.PhysicalResourceId,
baseResources.StackResources.find((x) => x.LogicalResourceId === 'PublicSubnetTwo')
.PhysicalResourceId,
],
assignPublicIp: 'ENABLED',
securityGroups: [
baseResources.StackResources.find(
(x) => x.LogicalResourceId === 'ContainerSecurityGroup',
).PhysicalResourceId,
],
},
},
}).promise();
await ECS.waitFor('tasksRunning', {
cluster: clusterName,
2021-01-31 20:43:20 +00:00
tasks: [task.tasks[0].taskArn],
2021-01-31 19:22:28 +00:00
}).promise();
core.info(`Build job is running, `);
2021-01-31 19:22:28 +00:00
// watching logs
2021-01-31 19:22:28 +00:00
await ECS.waitFor('tasksStopped', {
cluster: clusterName,
2021-01-31 20:43:20 +00:00
tasks: [task.tasks[0].taskArn],
2021-01-31 19:22:28 +00:00
}).promise();
core.info('Build job has ended');
}
2021-02-05 23:14:45 +00:00
2021-02-05 23:24:01 +00:00
static onlog(batch) {
batch.forEach((log) => {
core.info(`log: ${log}`);
});
2021-02-05 23:14:45 +00:00
}
2021-01-31 19:22:28 +00:00
}
export default AWS;