| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  | import * as SDK from 'aws-sdk'; | 
					
						
							|  |  |  | import { customAlphabet } from 'nanoid'; | 
					
						
							|  |  |  | import RemoteBuilderSecret from './remote-builder-secret'; | 
					
						
							|  |  |  | import RemoteBuilderEnvironmentVariable from './remote-builder-environment-variable'; | 
					
						
							|  |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as core from '@actions/core'; | 
					
						
							|  |  |  | import RemoteBuilderTaskDef from './remote-builder-task-def'; | 
					
						
							|  |  |  | import RemoteBuilderConstants from './remote-builder-constants'; | 
					
						
							|  |  |  | import AWSBuildRunner from './aws-build-runner'; | 
					
						
							| 
									
										
										
										
											2021-06-18 20:36:45 +00:00
										 |  |  | import { RemoteBuilderProviderInterface } from './remote-builder-provider-interface'; | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  | import BuildParameters from '../build-parameters'; | 
					
						
							| 
									
										
										
										
											2021-08-08 09:20:52 +00:00
										 |  |  | const crypto = require('crypto'); | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 20:36:45 +00:00
										 |  |  | class AWSBuildEnvironment implements RemoteBuilderProviderInterface { | 
					
						
							| 
									
										
										
										
											2021-08-13 19:59:01 +00:00
										 |  |  |   private baseStackName: string; | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor(buildParameters: BuildParameters) { | 
					
						
							| 
									
										
										
										
											2021-08-13 19:59:01 +00:00
										 |  |  |     this.baseStackName = buildParameters.awsBaseStackName; | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-06-19 23:06:44 +00:00
										 |  |  |   cleanupSharedBuildResources( | 
					
						
							| 
									
										
										
										
											2021-06-19 22:15:44 +00:00
										 |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     buildUid: string, | 
					
						
							|  |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     buildParameters: BuildParameters, | 
					
						
							|  |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     branchName: string, | 
					
						
							|  |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[], | 
					
						
							| 
									
										
										
										
											2021-06-19 23:25:46 +00:00
										 |  |  |   ) {} | 
					
						
							| 
									
										
										
										
											2021-06-19 23:06:44 +00:00
										 |  |  |   setupSharedBuildResources( | 
					
						
							| 
									
										
										
										
											2021-06-19 22:15:44 +00:00
										 |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     buildUid: string, | 
					
						
							|  |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     buildParameters: BuildParameters, | 
					
						
							|  |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     branchName: string, | 
					
						
							|  |  |  |     // eslint-disable-next-line no-unused-vars
 | 
					
						
							|  |  |  |     defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[], | 
					
						
							| 
									
										
										
										
											2021-06-19 23:25:46 +00:00
										 |  |  |   ) {} | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 04:49:32 +00:00
										 |  |  |   async runBuildTask( | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     buildId: string, | 
					
						
							|  |  |  |     image: string, | 
					
						
							|  |  |  |     commands: string[], | 
					
						
							|  |  |  |     mountdir: string, | 
					
						
							|  |  |  |     workingdir: string, | 
					
						
							|  |  |  |     environment: RemoteBuilderEnvironmentVariable[], | 
					
						
							|  |  |  |     secrets: RemoteBuilderSecret[], | 
					
						
							| 
									
										
										
										
											2021-06-18 20:36:45 +00:00
										 |  |  |   ): Promise<void> { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     const ECS = new SDK.ECS(); | 
					
						
							|  |  |  |     const CF = new SDK.CloudFormation(); | 
					
						
							|  |  |  |     const entrypoint = ['/bin/sh']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |     const taskDef = await this.setupCloudFormations( | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |       CF, | 
					
						
							|  |  |  |       buildId, | 
					
						
							|  |  |  |       image, | 
					
						
							|  |  |  |       entrypoint, | 
					
						
							|  |  |  |       commands, | 
					
						
							|  |  |  |       mountdir, | 
					
						
							|  |  |  |       workingdir, | 
					
						
							|  |  |  |       secrets, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2021-06-19 21:07:24 +00:00
										 |  |  |       await AWSBuildRunner.runTask(taskDef, ECS, CF, environment, buildId, commands); | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     } finally { | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |       await this.cleanupResources(CF, taskDef); | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   getParameterTemplate(p1) { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     return `
 | 
					
						
							|  |  |  |   ${p1}: | 
					
						
							|  |  |  |     Type: String | 
					
						
							|  |  |  |     Default: '' | 
					
						
							|  |  |  | `;
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   getSecretTemplate(p1) { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     return `
 | 
					
						
							|  |  |  |   ${p1}Secret: | 
					
						
							|  |  |  |     Type: AWS::SecretsManager::Secret | 
					
						
							|  |  |  |     Properties: | 
					
						
							|  |  |  |       Name: !Join [ "", [ '${p1}', !Ref BUILDID ] ] | 
					
						
							|  |  |  |       SecretString: !Ref ${p1} | 
					
						
							|  |  |  | `;
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   getSecretDefinitionTemplate(p1, p2) { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     return `
 | 
					
						
							|  |  |  |             - Name: '${p1}' | 
					
						
							|  |  |  |               ValueFrom: !Ref ${p2}Secret | 
					
						
							|  |  |  | `;
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   insertAtTemplate(template, insertionKey, insertion) { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     const index = template.search(insertionKey) + insertionKey.length + '\n'.length; | 
					
						
							|  |  |  |     template = [template.slice(0, index), insertion, template.slice(index)].join(''); | 
					
						
							|  |  |  |     return template; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   async setupCloudFormations( | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     CF: SDK.CloudFormation, | 
					
						
							|  |  |  |     buildUid: string, | 
					
						
							|  |  |  |     image: string, | 
					
						
							|  |  |  |     entrypoint: string[], | 
					
						
							|  |  |  |     commands: string[], | 
					
						
							|  |  |  |     mountdir: string, | 
					
						
							|  |  |  |     workingdir: string, | 
					
						
							|  |  |  |     secrets: RemoteBuilderSecret[], | 
					
						
							|  |  |  |   ): Promise<RemoteBuilderTaskDef> { | 
					
						
							|  |  |  |     const logid = customAlphabet(RemoteBuilderConstants.alphabet, 9)(); | 
					
						
							|  |  |  |     commands[1] += `
 | 
					
						
							|  |  |  |       echo "${logid}" | 
					
						
							|  |  |  |     `;
 | 
					
						
							| 
									
										
										
										
											2021-08-08 08:48:18 +00:00
										 |  |  |     await this.setupBaseStack(CF); | 
					
						
							| 
									
										
										
										
											2021-08-13 19:59:01 +00:00
										 |  |  |     const taskDefStackName = `${this.baseStackName}-${buildUid}`; | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     let taskDefCloudFormation = this.readTaskCloudFormationTemplate(); | 
					
						
							|  |  |  |     const cleanupTaskDefStackName = `${taskDefStackName}-cleanup`; | 
					
						
							|  |  |  |     const cleanupCloudFormation = fs.readFileSync(`${__dirname}/cloud-formations/cloudformation-stack-ttl.yml`, 'utf8'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       for (const secret of secrets) { | 
					
						
							|  |  |  |         taskDefCloudFormation = this.insertAtTemplate( | 
					
						
							|  |  |  |           taskDefCloudFormation, | 
					
						
							|  |  |  |           'p1 - input', | 
					
						
							|  |  |  |           this.getParameterTemplate(secret.ParameterKey.replace(/[^\dA-Za-z]/g, '')), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         taskDefCloudFormation = this.insertAtTemplate( | 
					
						
							|  |  |  |           taskDefCloudFormation, | 
					
						
							|  |  |  |           'p2 - secret', | 
					
						
							|  |  |  |           this.getSecretTemplate(secret.ParameterKey.replace(/[^\dA-Za-z]/g, '')), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         taskDefCloudFormation = this.insertAtTemplate( | 
					
						
							|  |  |  |           taskDefCloudFormation, | 
					
						
							|  |  |  |           'p3 - container def', | 
					
						
							|  |  |  |           this.getSecretDefinitionTemplate(secret.EnvironmentVariable, secret.ParameterKey.replace(/[^\dA-Za-z]/g, '')), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const mappedSecrets = secrets.map((x) => { | 
					
						
							|  |  |  |         return { ParameterKey: x.ParameterKey.replace(/[^\dA-Za-z]/g, ''), ParameterValue: x.ParameterValue }; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await CF.createStack({ | 
					
						
							|  |  |  |         StackName: taskDefStackName, | 
					
						
							|  |  |  |         TemplateBody: taskDefCloudFormation, | 
					
						
							| 
									
										
										
										
											2021-08-13 18:33:34 +00:00
										 |  |  |         Capabilities: ['CAPABILITY_IAM'], | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |         Parameters: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'ImageUrl', | 
					
						
							|  |  |  |             ParameterValue: image, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'ServiceName', | 
					
						
							|  |  |  |             ParameterValue: taskDefStackName, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'Command', | 
					
						
							| 
									
										
										
										
											2021-06-19 21:07:24 +00:00
										 |  |  |             ParameterValue: 'echo "this template should be overwritten when running a task"', | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'EntryPoint', | 
					
						
							|  |  |  |             ParameterValue: entrypoint.join(','), | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'WorkingDirectory', | 
					
						
							|  |  |  |             ParameterValue: workingdir, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'EFSMountDirectory', | 
					
						
							|  |  |  |             ParameterValue: mountdir, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'BUILDID', | 
					
						
							|  |  |  |             ParameterValue: buildUid, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           ...mappedSecrets, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }).promise(); | 
					
						
							| 
									
										
										
										
											2021-07-12 22:33:45 +00:00
										 |  |  |       core.info('Creating main job...'); | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |       await CF.createStack({ | 
					
						
							|  |  |  |         StackName: cleanupTaskDefStackName, | 
					
						
							|  |  |  |         TemplateBody: cleanupCloudFormation, | 
					
						
							|  |  |  |         Capabilities: ['CAPABILITY_IAM'], | 
					
						
							|  |  |  |         Parameters: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'StackName', | 
					
						
							|  |  |  |             ParameterValue: taskDefStackName, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'DeleteStackName', | 
					
						
							|  |  |  |             ParameterValue: cleanupTaskDefStackName, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'TTL', | 
					
						
							|  |  |  |             ParameterValue: '100', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             ParameterKey: 'BUILDID', | 
					
						
							|  |  |  |             ParameterValue: buildUid, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }).promise(); | 
					
						
							| 
									
										
										
										
											2021-07-12 22:33:45 +00:00
										 |  |  |       core.info('Creating cleanup double checker cron job...'); | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       await CF.waitFor('stackCreateComplete', { StackName: taskDefStackName }).promise(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |       await this.handleStackCreationFailure(error, CF, taskDefStackName, taskDefCloudFormation, secrets); | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       throw error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const taskDefResources = ( | 
					
						
							|  |  |  |       await CF.describeStackResources({ | 
					
						
							|  |  |  |         StackName: taskDefStackName, | 
					
						
							|  |  |  |       }).promise() | 
					
						
							|  |  |  |     ).StackResources; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 19:59:01 +00:00
										 |  |  |     const baseResources = (await CF.describeStackResources({ StackName: this.baseStackName }).promise()).StackResources; | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-12 23:23:19 +00:00
										 |  |  |     // TODO: offer a parameter to decide if you want the guarenteed shutdown or fastest startup time possible
 | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       taskDefStackName, | 
					
						
							|  |  |  |       taskDefCloudFormation, | 
					
						
							|  |  |  |       taskDefStackNameTTL: cleanupTaskDefStackName, | 
					
						
							|  |  |  |       ttlCloudFormation: cleanupCloudFormation, | 
					
						
							|  |  |  |       taskDefResources, | 
					
						
							|  |  |  |       baseResources, | 
					
						
							|  |  |  |       logid, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 08:48:18 +00:00
										 |  |  |   async setupBaseStack(CF: SDK.CloudFormation) { | 
					
						
							| 
									
										
										
										
											2021-08-13 19:59:01 +00:00
										 |  |  |     const baseStackName = this.baseStackName; | 
					
						
							| 
									
										
										
										
											2021-08-08 08:48:18 +00:00
										 |  |  |     const baseStack = fs.readFileSync(`${__dirname}/cloud-formations/base-setup.yml`, 'utf8'); | 
					
						
							| 
									
										
										
										
											2021-08-08 09:20:52 +00:00
										 |  |  |     const hash = crypto.createHash('md5').update(baseStack).digest('hex'); | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Cloud Formation Input
 | 
					
						
							| 
									
										
										
										
											2021-08-08 09:05:16 +00:00
										 |  |  |     const describeStackInput: SDK.CloudFormation.DescribeStacksInput = { | 
					
						
							|  |  |  |       StackName: baseStackName, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     const parameters: SDK.CloudFormation.Parameter[] = [ | 
					
						
							|  |  |  |       { ParameterKey: 'EnvironmentName', ParameterValue: 'development' }, | 
					
						
							|  |  |  |       { ParameterKey: 'Storage', ParameterValue: `${baseStackName}-storage` }, | 
					
						
							| 
									
										
										
										
											2021-08-13 19:00:21 +00:00
										 |  |  |       { ParameterKey: 'Version', ParameterValue: hash }, | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     ]; | 
					
						
							|  |  |  |     const updateInput: SDK.CloudFormation.UpdateStackInput = { | 
					
						
							|  |  |  |       StackName: baseStackName, | 
					
						
							|  |  |  |       TemplateBody: baseStack, | 
					
						
							|  |  |  |       Parameters: parameters, | 
					
						
							| 
									
										
										
										
											2021-08-13 18:55:38 +00:00
										 |  |  |       Capabilities: ['CAPABILITY_IAM'], | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  |     const createStackInput: SDK.CloudFormation.CreateStackInput = { | 
					
						
							|  |  |  |       StackName: baseStackName, | 
					
						
							|  |  |  |       TemplateBody: baseStack, | 
					
						
							|  |  |  |       Parameters: parameters, | 
					
						
							| 
									
										
										
										
											2021-08-13 18:55:38 +00:00
										 |  |  |       Capabilities: ['CAPABILITY_IAM'], | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  |     const stacks = (await CF.listStacks().promise()).StackSummaries?.map((x) => x.StackName); | 
					
						
							|  |  |  |     const stackExists: Boolean = stacks?.includes(baseStackName) || false; | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     const describeStack = async () => { | 
					
						
							|  |  |  |       return await CF.describeStacks(describeStackInput).promise(); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 17:54:27 +00:00
										 |  |  |     if (!stackExists) { | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  |       core.info('stack does not exist'); | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |       await CF.createStack(createStackInput).promise(); | 
					
						
							| 
									
										
										
										
											2021-08-13 20:09:01 +00:00
										 |  |  |       core.info(`created stack (version: ${hash})`); | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     const CFState = await describeStack(); | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  |     let stack = CFState.Stacks?.[0]; | 
					
						
							|  |  |  |     if (!stack) { | 
					
						
							|  |  |  |       throw new Error(`Base stack doesn't exist, even after creation, stackExists check: ${stackExists}`); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |     const stackVersion = stack.Parameters?.find((x) => x.ParameterKey === 'Version')?.ParameterValue; | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (stack.StackStatus === 'CREATE_IN_PROGRESS') { | 
					
						
							|  |  |  |       await CF.waitFor('stackCreateComplete', describeStackInput).promise(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (stackExists) { | 
					
						
							| 
									
										
										
										
											2021-08-13 20:09:01 +00:00
										 |  |  |       core.info(`Base stack exists (version: ${stackVersion}, local version: ${hash})`); | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |       if (hash !== stackVersion) { | 
					
						
							|  |  |  |         core.info(`Updating`); | 
					
						
							| 
									
										
										
										
											2021-08-08 09:05:16 +00:00
										 |  |  |         await CF.updateStack(updateInput).promise(); | 
					
						
							| 
									
										
										
										
											2021-08-08 09:20:52 +00:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |         core.info(`No update required`); | 
					
						
							| 
									
										
										
										
											2021-08-08 09:05:16 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-08-13 18:52:37 +00:00
										 |  |  |       stack = (await describeStack()).Stacks?.[0]; | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  |       if (!stack) { | 
					
						
							|  |  |  |         throw new Error( | 
					
						
							|  |  |  |           `Base stack doesn't exist, even after updating and creation, stackExists check: ${stackExists}`, | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (stack.StackStatus === 'UPDATE_IN_PROGRESS') { | 
					
						
							|  |  |  |         await CF.waitFor('stackUpdateComplete', describeStackInput).promise(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-08-08 09:05:16 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-13 18:16:17 +00:00
										 |  |  |     core.info('base stack exists and is ready'); | 
					
						
							| 
									
										
										
										
											2021-08-08 08:48:18 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   async handleStackCreationFailure( | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     error: any, | 
					
						
							|  |  |  |     CF: SDK.CloudFormation, | 
					
						
							|  |  |  |     taskDefStackName: string, | 
					
						
							|  |  |  |     taskDefCloudFormation: string, | 
					
						
							|  |  |  |     secrets: RemoteBuilderSecret[], | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     core.info(JSON.stringify(secrets, undefined, 4)); | 
					
						
							|  |  |  |     core.info(taskDefCloudFormation); | 
					
						
							|  |  |  |     const events = (await CF.describeStackEvents({ StackName: taskDefStackName }).promise()).StackEvents; | 
					
						
							|  |  |  |     const resources = (await CF.describeStackResources({ StackName: taskDefStackName }).promise()).StackResources; | 
					
						
							|  |  |  |     core.info(JSON.stringify(events, undefined, 4)); | 
					
						
							|  |  |  |     core.info(JSON.stringify(resources, undefined, 4)); | 
					
						
							|  |  |  |     core.error(error); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   readTaskCloudFormationTemplate(): string { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     return fs.readFileSync(`${__dirname}/cloud-formations/task-def-formation.yml`, 'utf8'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:35:22 +00:00
										 |  |  |   async cleanupResources(CF: SDK.CloudFormation, taskDef: RemoteBuilderTaskDef) { | 
					
						
							| 
									
										
										
										
											2021-05-23 04:08:40 +00:00
										 |  |  |     core.info('Cleanup starting'); | 
					
						
							|  |  |  |     await CF.deleteStack({ | 
					
						
							|  |  |  |       StackName: taskDef.taskDefStackName, | 
					
						
							|  |  |  |     }).promise(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await CF.deleteStack({ | 
					
						
							|  |  |  |       StackName: taskDef.taskDefStackNameTTL, | 
					
						
							|  |  |  |     }).promise(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await CF.waitFor('stackDeleteComplete', { | 
					
						
							|  |  |  |       StackName: taskDef.taskDefStackName, | 
					
						
							|  |  |  |     }).promise(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Currently too slow and causes too much waiting
 | 
					
						
							|  |  |  |     await CF.waitFor('stackDeleteComplete', { | 
					
						
							|  |  |  |       StackName: taskDef.taskDefStackNameTTL, | 
					
						
							|  |  |  |     }).promise(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     core.info('Cleanup complete'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export default AWSBuildEnvironment; |