Skip to content

Commit

Permalink
Merge pull request #10 from aws4embeddedlinux/bash-error-handling
Browse files Browse the repository at this point in the history
Bash error handling
  • Loading branch information
alinadima authored Sep 13, 2023
2 parents 7e8e24c + 74a4096 commit b08fdbd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
15 changes: 8 additions & 7 deletions assets/create-ec2-ami.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
set -e
set -eo pipefail
[ "$DEBUG" == 'true' ] && set -x

ARGC=$#
Expand Down Expand Up @@ -65,13 +65,14 @@ fi

IMPORT_TASK_ID=$(echo "${command_output}" | jq -r '.ImportTaskId')

IMPORT_STATUS=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID | jq -r '.ImportSnapshotTasks[].SnapshotTaskDetail.Status')
IMPORT_STATUS=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID --query 'ImportSnapshotTasks[].SnapshotTaskDetail.Status' --output text)
x=0
rm image-import.json
echo $IMPORT_STATUS
while [ "$IMPORT_STATUS" = "active" ] && [ $x -lt 120 ]
do
IMPORT_STATUS=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID | jq -r '.ImportSnapshotTasks[].SnapshotTaskDetail.Status')
IMPORT_STATUS_MSG=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID | jq -r '.ImportSnapshotTasks[].SnapshotTaskDetail.StatusMessage')
IMPORT_STATUS=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID --query 'ImportSnapshotTasks[].SnapshotTaskDetail.Status' --output text)
IMPORT_STATUS_MSG=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID --query 'ImportSnapshotTasks[].SnapshotTaskDetail.StatusMessage' --output text)
echo "Import Status: ${IMPORT_STATUS} / ${IMPORT_STATUS_MSG}"
x=$(( $x + 1 ))
sleep 15
Expand All @@ -84,7 +85,7 @@ else
echo "Import Failed, exiting"; exit 2;
fi

SNAPSHOT_ID=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID | jq -r '.ImportSnapshotTasks[].SnapshotTaskDetail.SnapshotId')
SNAPSHOT_ID=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID --query 'ImportSnapshotTasks[].SnapshotTaskDetail.SnapshotId' --output text)

aws ec2 wait snapshot-completed --snapshot-ids $SNAPSHOT_ID

Expand Down Expand Up @@ -121,13 +122,13 @@ cat <<EOF > register-ami.json
EOF

AMI_NAME=$(echo "${IMAGE_NAME}-${DISTRO}-${DISTRO_CODENAME}-${DISTRO_VERSION}-${BUILDNAME}-${ARCHITECTURE}" | cut -c -128 | sed -e s/+/-/g)
IMAGE_ID=$(aws ec2 describe-images --filters Name=name,Values=${AMI_NAME} | jq -r '.Images[].ImageId')
IMAGE_ID=$(aws ec2 describe-images --filters Name=name,Values=${AMI_NAME} --query 'Images[].ImageId' --output text)
if [ "$IMAGE_ID" != "" ]; then
echo "Deregistering existing image $IMAGE_ID"
aws ec2 deregister-image --image-id ${IMAGE_ID} 2>&1 > /dev/null
fi
echo "Registering AMI with Snapshot $SNAPSHOT_ID"
AMI_ID=$(aws ec2 register-image --name ${AMI_NAME} --cli-input-json="file://register-ami.json" | jq -r '.ImageId')
AMI_ID=$(aws ec2 register-image --name ${AMI_NAME} --cli-input-json="file://register-ami.json" --query 'ImageId' --output text)
echo "AMI name: $AMI_NAME"
echo "AMI ID: $AMI_ID"
rm register-ami.json
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/build-image-data.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`Build Image Data Snapshot 1`] = `
"Properties": {
"Content": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-central-1",
"S3Key": "arbitrary-file.zip",
"S3Key": "arbitrary-file.ext",
},
"Description": "/opt/awscli/aws",
},
Expand All @@ -38,7 +38,7 @@ exports[`Build Image Data Snapshot 1`] = `
"cdk-hnb659fds-assets-111111111111-eu-central-1",
],
"SourceObjectKeys": [
"arbitrary-file.zip",
"arbitrary-file.ext",
],
},
"Type": "Custom::CDKBucketDeployment",
Expand Down Expand Up @@ -266,7 +266,7 @@ exports[`Build Image Data Snapshot 1`] = `
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-central-1",
"S3Key": "arbitrary-file.zip",
"S3Key": "arbitrary-file.ext",
},
"Environment": {
"Variables": {
Expand Down Expand Up @@ -297,7 +297,7 @@ exports[`Build Image Data Snapshot 1`] = `
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-central-1",
"S3Key": "arbitrary-file.zip",
"S3Key": "arbitrary-file.ext",
},
"Description": {
"Fn::Join": [
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/demo-pipeline.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ exports[`Demo Pipeline Poky AMI Pipeline - check role name trim 1`] = `
{
"Name": "SCRIPT_URL",
"Type": "PLAINTEXT",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/cb07978d00156c655bc2617e9ff6d544ec346fc8bfd37b01b644b4694d399265.sh",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/arbitrary-file.ext",
},
],
"Image": {
Expand Down Expand Up @@ -1936,7 +1936,7 @@ def handler(event, context):
"BranchName": "main",
"S3": {
"Bucket": "cdk-hnb659fds-assets-12341234-eu-central-1",
"Key": "8328421057d696d22d2b958f640836605dbbf21ce1473e4db341cffcd075ae25.zip",
"Key": "arbitrary-file.ext",
},
},
"RepositoryName": "layer-repo-PokyAmiPipeline2ExportsOutputFnGetAttPipelineVpc0543904ACidrBlock70DEC992",
Expand Down Expand Up @@ -2291,7 +2291,7 @@ exports[`Demo Pipeline Snapshot Poky AMI Pipeline 1`] = `
{
"Name": "SCRIPT_URL",
"Type": "PLAINTEXT",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/cb07978d00156c655bc2617e9ff6d544ec346fc8bfd37b01b644b4694d399265.sh",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/arbitrary-file.ext",
},
],
"Image": {
Expand Down Expand Up @@ -4056,7 +4056,7 @@ def handler(event, context):
"BranchName": "main",
"S3": {
"Bucket": "cdk-hnb659fds-assets-12341234-eu-central-1",
"Key": "8328421057d696d22d2b958f640836605dbbf21ce1473e4db341cffcd075ae25.zip",
"Key": "arbitrary-file.ext",
},
},
"RepositoryName": "layer-repo-MyTestStack",
Expand Down
9 changes: 2 additions & 7 deletions test/build-image-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { BuildImageDataStack } from '../lib/build-image-data';
import { normalizedTemplateFromStack } from './util';

describe('Build Image Data', () => {
const props = {
Expand All @@ -27,13 +28,7 @@ describe('Build Image Data', () => {
const app = new cdk.App();
const stack = new BuildImageDataStack(app, 'MyTestStack', props);
/* We must change some randomly generated file names used in the S3 asset construct. */
const templateWithRandomKeys = Template.fromStack(stack);
const templateWithConstKeys = JSON.parse(
JSON.stringify(templateWithRandomKeys.toJSON()).replace(
/[a-z0-9]{64}\.zip/g,
'arbitrary-file.zip'
)
);
const templateWithConstKeys = normalizedTemplateFromStack(stack);
expect(templateWithConstKeys).toMatchSnapshot();
});
});
5 changes: 3 additions & 2 deletions test/demo-pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DemoPipelineStack } from '../lib/demo-pipeline';
import { Repository } from 'aws-cdk-lib/aws-ecr';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { ProjectKind } from '../lib';
import { normalizedTemplateFromStack } from './util';

describe('Demo Pipeline', () => {
const env = { account: '12341234', region: 'eu-central-1' };
Expand Down Expand Up @@ -35,7 +36,7 @@ describe('Demo Pipeline', () => {
vpc,
projectKind: ProjectKind.PokyAmi,
});
const template = Template.fromStack(stack);
const template = normalizedTemplateFromStack(stack);
expect(template).toMatchSnapshot();
});

Expand All @@ -55,7 +56,7 @@ describe('Demo Pipeline', () => {
projectKind: ProjectKind.PokyAmi,
}
);
const template = Template.fromStack(stack);
const template = normalizedTemplateFromStack(stack);
expect(template).toMatchSnapshot();
});
});
17 changes: 17 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';

/**
* Remove hashed asset values and put a normalized name in instead.
*/
export function normalizedTemplateFromStack(stack: cdk.Stack): Template {
const templateWithRandomKeys = Template.fromStack(stack);
const templateWithConstKeys = JSON.parse(
JSON.stringify(templateWithRandomKeys.toJSON()).replace(
/[a-z0-9]{64}\.(zip|sh)/g,
'arbitrary-file.ext'
)
);

return templateWithConstKeys;
}

0 comments on commit b08fdbd

Please sign in to comment.