unity-builder/src/model/aws.js

202 lines
5.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:45:33 +00:00
import { nanoid } from 'nanoid';
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 22:44:08 +00:00
const zlib = require('zlib');
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',
['git'],
2021-02-05 23:33:28 +00:00
['clone', `https://github.com/${process.env.GITHUB_REPOSITORY}.git`, `repo`],
'/efsdata',
'/efsdata/',
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"'],
'/efsdata',
'/efsdata/',
2021-02-07 00:37:34 +00:00
[],
2021-01-31 22:11:13 +00:00
);
2021-01-31 19:22:28 +00:00
}
static async run(stackName, image, entrypoint, commands, mountdir, workingdir, environment) {
2021-01-31 19:22:28 +00:00
const ECS = new SDK.ECS();
const CF = new SDK.CloudFormation();
2021-02-07 02:14:58 +00:00
const taskDefStackName = `${stackName}-taskDef-${image}-${nanoid()}`
.toString()
.replace(/[^\da-z]/gi, '');
core.info('Creating build job resources');
2021-02-07 02:08:17 +00:00
const taskDefCloudFormation = fs.readFileSync(`${__dirname}/task-def-formation.yml`, 'utf8');
await CF.createStack({
StackName: taskDefStackName,
TemplateBody: taskDefCloudFormation,
Parameters: [
{
ParameterKey: 'ImageUrl',
ParameterValue: image,
},
2021-02-07 14:33:42 +00:00
{
ParameterKey: 'ServiceName',
ParameterValue: taskDefStackName,
},
{
ParameterKey: 'Command',
ParameterValue: commands.join(','),
},
{
ParameterKey: 'EntryPoint',
ParameterValue: entrypoint.join(','),
},
{
ParameterKey: 'WorkingDirectory',
ParameterValue: workingdir,
},
{
ParameterKey: 'EFSMountDirectory',
ParameterValue: mountdir,
},
2021-02-07 02:08:17 +00:00
],
}).promise();
await CF.waitFor('stackCreateComplete', { StackName: taskDefStackName }).promise();
2021-01-31 19:22:28 +00:00
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',
overrides: {
containerOverrides: [
{
2021-02-07 14:37:59 +00:00
name: taskDefStackName,
2021-01-31 22:11:13 +00:00
environment,
2021-01-31 19:22:28 +00:00
},
],
},
launchType: 'FARGATE',
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();
2021-02-07 18:10:30 +00:00
2021-02-07 18:00:57 +00:00
core.info('Build job is starting');
2021-02-07 18:10:30 +00:00
2021-01-31 19:22:28 +00:00
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();
2021-02-07 18:10:30 +00:00
2021-02-07 18:00:57 +00:00
core.info(`Build job is running`);
2021-01-31 19:22:28 +00:00
// watching logs
2021-02-07 15:42:15 +00:00
const kinesis = new SDK.Kinesis();
2021-02-07 15:27:24 +00:00
const getTaskStatus = async () => {
const tasks = await ECS.describeTasks({
cluster: clusterName,
tasks: [task.tasks[0].taskArn],
}).promise();
return tasks.tasks[0].lastStatus;
};
const stream = await kinesis
.describeStream({
StreamName: taskDefResources.StackResources.find(
(x) => x.LogicalResourceId === 'KinesisStream',
).PhysicalResourceId,
})
.promise();
let iterator = (
await kinesis
.getShardIterator({
ShardIteratorType: 'TRIM_HORIZON',
StreamName: stream.StreamDescription.StreamName,
ShardId: stream.StreamDescription.Shards[0].ShardId,
})
.promise()
).ShardIterator;
2021-02-07 16:36:53 +00:00
core.info(`Task status is ${await getTaskStatus()}`);
while ((await getTaskStatus()) === 'RUNNING') {
const records = await kinesis
.getRecords({
ShardIterator: iterator,
})
.promise();
iterator = records.NextShardIterator;
2021-02-07 16:56:12 +00:00
if (records.Records.length > 0) {
for (let index = 0; index < records.Records.length; index++) {
2021-02-07 22:50:13 +00:00
const json = JSON.parse(
2021-02-07 22:44:08 +00:00
zlib.gunzipSync(Buffer.from(records.Records[index].Data, 'base64')).toString('utf8'),
);
2021-02-07 22:50:13 +00:00
if (json.messageType === 'DATA_MESSAGE') {
2021-02-07 22:55:13 +00:00
for (let logEventsIndex = 0; logEventsIndex < json.logEvents.length; logEventsIndex++) {
core.info(json.logEvents[logEventsIndex].message);
}
2021-02-07 22:50:13 +00:00
}
}
2021-02-07 16:24:06 +00:00
}
2021-02-07 18:57:06 +00:00
await new Promise((resolve) => setTimeout(resolve, 3000));
}
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();
2021-02-07 17:53:37 +00:00
core.info('Build job has ended');
2021-02-07 17:47:51 +00:00
await CF.deleteStack({
StackName: taskDefStackName,
}).promise();
2021-02-07 17:53:37 +00:00
core.info('Cleanup complete');
2021-01-31 19:22:28 +00:00
}
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;