All files create-service.ts

0% Statements 0/22
0% Branches 0/4
0% Functions 0/7
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                                                                                                                                                                                             
import {Observable} from 'rxjs'
import CloudFormation, {CreateStackInput} from 'aws-sdk/clients/cloudformation'
import STS from 'aws-sdk/clients/sts'
 
import {DOCKERLESS_EVENT, SERVICE_YML} from '@utils/constants'
 
import {toId} from '@utils/string-utils'
import {GlobalDockerlessState} from '@src/dockerless-state'
import {ContainerValue} from '@utils/types/dockerless-config'
 
import waitForResourceCreated from '@modules/deploy-rx/actions/wait-for-resource-created'
 
const createService = (
	cloudFormationClient: CloudFormation,
	stsClient: STS,
	container: ContainerValue
): Observable<DOCKERLESS_EVENT> =>
	new Observable<DOCKERLESS_EVENT>(subscriber => {
		const stackName = `dockerless-${toId(GlobalDockerlessState.config.serviceName)}-${toId(
			container.containerName
		)}-${GlobalDockerlessState.stage}-service`
 
		const createService = async () => {
			const localImageTagName = `${toId(GlobalDockerlessState.config.serviceName)}-${toId(
				container.containerName
			)}-${GlobalDockerlessState.stage}`
			const awsAccountId = (await stsClient.getCallerIdentity().promise()).Account
			const remoteImageTagName = `${awsAccountId}.dkr.ecr.${process.env.AWS_DEFAULT_REGION}.amazonaws.com/${localImageTagName}:latest`
 
			const cfParams: CreateStackInput = {
				StackName: stackName,
				TemplateBody: SERVICE_YML,
				Parameters: [
					{
						ParameterKey: 'VPCStackName',
						ParameterValue: `dockerless-${toId(GlobalDockerlessState.config.serviceName)}-${
							GlobalDockerlessState.stage
						}-vpc`
					},
					{
						ParameterKey: 'SharedResourcesStackName',
						ParameterValue: 'dockerless-shared-resources'
					},
					{
						ParameterKey: 'ClusterStackName',
						ParameterValue: `dockerless-${toId(GlobalDockerlessState.config.serviceName)}-${
							GlobalDockerlessState.stage
						}-cluster`
					},
					{
						ParameterKey: 'ImageUrl',
						ParameterValue: remoteImageTagName
					},
					{
						ParameterKey: 'ServiceName',
						ParameterValue: `${toId(GlobalDockerlessState.config.serviceName)}-${toId(
							container.containerName
						)}-${GlobalDockerlessState.stage}`
					},
					{
						ParameterKey: 'ContainerSize',
						ParameterValue: container.size
					},
					{
						ParameterKey: 'ContainerPort',
						ParameterValue: container.port.toString()
					},
					{
						ParameterKey: 'DesiredCount',
						ParameterValue: container.desiredInstances.toString()
					},
					{
						ParameterKey: 'DesiredCount',
						ParameterValue: container.desiredInstances.toString()
					},
					{
						ParameterKey: 'NetworkType',
						ParameterValue: container.networkType || 'public'
					},
					{
						ParameterKey: 'Protocol',
						ParameterValue: container.portProtocol
					},
					{
						ParameterKey: 'LoadBalancerType',
						ParameterValue: container.portProtocol === 'HTTP' ? 'application' : 'network'
					}
				],
				Capabilities: ['CAPABILITY_NAMED_IAM']
			}
 
			await cloudFormationClient.createStack(cfParams).promise()
		}
 
		const waitForSharedResourcesCreated = (): void => {
			waitForResourceCreated(cloudFormationClient, stackName).subscribe({
				error: e => subscriber.error(e),
				complete: () => {
					subscriber.next(DOCKERLESS_EVENT.createServiceSuccess)
					subscriber.complete()
				}
			})
		}
 
		createService()
			.then(waitForSharedResourcesCreated)
			.catch(e => subscriber.error(e))
	})
 
export default createService