Skip to content

Commit

Permalink
Merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
alinadima committed Oct 10, 2023
1 parent cec562d commit 323cb13
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions assets/create-ec2-ami.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ IMG_DIR="${TMPDIR}/deploy/images/${MACHINE_NAME}"

TESTDATA_JSON="${IMG_DIR}/${IMAGE_NAME}-${MACHINE_NAME}${TESTDATA_JSON_EXTEN}.testdata.json"

DISTRO=$(jq -r '.DISTRO' $TESTDATA_JSON)
DISTRO_CODENAME=$(jq -r '.DISTRO_CODENAME' $TESTDATA_JSON)
DISTRO_NAME=$(jq -r '.DISTRO_NAME' $TESTDATA_JSON)
DISTRO_VERSION=$(jq -r '.DISTRO_VERSION' $TESTDATA_JSON)
BUILDNAME=$(jq -r '.BUILDNAME' $TESTDATA_JSON)
TARGET_ARCH=$(jq -r '.TARGET_ARCH' $TESTDATA_JSON)
IMAGE_NAME=$(jq -r '.IMAGE_NAME' $TESTDATA_JSON)
IMAGE_ROOTFS_SIZE=$(jq -r '.IMAGE_ROOTFS_SIZE' $TESTDATA_JSON)


echo DISTRO=$DISTRO
echo DISTRO_CODENAME=$DISTRO_CODENAME
echo DISTRO_NAME=$DISTRO_NAME
echo DISTRO_VERSION=$DISTRO_VERSION
echo BUILDNAME=$BUILDNAME
echo TARGET_ARCH=$TARGET_ARCH
echo IMAGE_ROOTFS_SIZE=$IMAGE_ROOTFS_SIZE
echo AMI_DISK_SIZE_GB=$AMI_DISK_SIZE_GB
DISTRO=$(jq -r '.DISTRO' "$TESTDATA_JSON")
DISTRO_CODENAME=$(jq -r '.DISTRO_CODENAME' "$TESTDATA_JSON")
DISTRO_NAME=$(jq -r '.DISTRO_NAME' "$TESTDATA_JSON")
DISTRO_VERSION=$(jq -r '.DISTRO_VERSION' "$TESTDATA_JSON")
BUILDNAME=$(jq -r '.BUILDNAME' "$TESTDATA_JSON")
TARGET_ARCH=$(jq -r '.TARGET_ARCH' "$TESTDATA_JSON")
IMAGE_NAME=$(jq -r '.IMAGE_NAME' "$TESTDATA_JSON")
IMAGE_ROOTFS_SIZE=$(jq -r '.IMAGE_ROOTFS_SIZE' "$TESTDATA_JSON")


echo DISTRO="$DISTRO"
echo DISTRO_CODENAME="$DISTRO_CODENAME"
echo DISTRO_NAME="$DISTRO_NAME"
echo DISTRO_VERSION="$DISTRO_VERSION"
echo BUILDNAME="$BUILDNAME"
echo TARGET_ARCH="$TARGET_ARCH"
echo IMAGE_ROOTFS_SIZE="$IMAGE_ROOTFS_SIZE"
echo AMI_DISK_SIZE_GB="$AMI_DISK_SIZE_GB"


echo "Pushing image ${IMAGE_NAME}${IMAGE_EXTEN}.wic.vhd to s3://${IMPORT_BUCKET_NAME}"
aws s3 cp ${IMG_DIR}/${IMAGE_NAME}${IMAGE_EXTEN}.wic.vhd s3://${IMPORT_BUCKET_NAME}
aws s3 cp "${IMG_DIR}/${IMAGE_NAME}${IMAGE_EXTEN}.wic.vhd" "s3://${IMPORT_BUCKET_NAME}"

cat <<EOF > image-import.json
{
Expand All @@ -58,7 +58,7 @@ cat <<EOF > image-import.json
EOF
echo "Importing image file into snapshot "

command_output=$(aws ec2 import-snapshot --disk-container "file://image-import.json" --tag-specifications "ResourceType=import-snapshot-task,Tags=[{Key=CreatedBy,Value=$CREATED_BY_TAG}]" --role-name $ROLE_NAME --encrypted)
command_output=$(aws ec2 import-snapshot --disk-container "file://image-import.json" --tag-specifications "ResourceType=import-snapshot-task,Tags=[{Key=CreatedBy,Value=$CREATED_BY_TAG}]" --role-name "$ROLE_NAME" --encrypted)
command_exit_code=$?

if [[ "$command_exit_code" -ne 0 ]]; then
Expand All @@ -67,16 +67,16 @@ fi

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

IMPORT_STATUS=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID --query 'ImportSnapshotTasks[].SnapshotTaskDetail.Status' --output text)
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
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 --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)
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 ))
x=$(( x + 1 ))
sleep 15
done
if [ $x -eq 120 ]; then
Expand All @@ -87,9 +87,9 @@ else
echo "Import Failed, exiting"; exit 2;
fi

SNAPSHOT_ID=$(aws ec2 describe-import-snapshot-tasks --import-task-ids $IMPORT_TASK_ID --query 'ImportSnapshotTasks[].SnapshotTaskDetail.SnapshotId' --output text)
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
aws ec2 wait snapshot-completed --snapshot-ids "$SNAPSHOT_ID"

if [[ "$TARGET_ARCH" == "x86_64" ]]; then
ARCHITECTURE="x86_64"
Expand Down Expand Up @@ -124,10 +124,10 @@ 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}" --query 'Images[].ImageId' --output text)
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
aws ec2 deregister-image --image-id "${IMAGE_ID}" > /dev/null 2>&1
fi
echo "Registering AMI with Snapshot $SNAPSHOT_ID"
AMI_ID=$(aws ec2 register-image --name "${AMI_NAME}" --cli-input-json="file://register-ami.json" --query 'ImageId' --output text)
Expand All @@ -146,7 +146,7 @@ do
BACKUP_STATUS=$(aws ec2 describe-store-image-tasks --query 'StoreTaskState' --output text)
PROGRESS_PERCENTAGE=$(aws ec2 describe-store-image-tasks --query 'ProgressPercentage' --output text)
echo "Backup Status: ${BACKUP_STATUS} / ${PROGRESS_PERCENTAGE} % completed."
x=$x + 1
x=$(( x + 1 ))
sleep 15
done
if [ $x -eq 120 ]; then
Expand Down

0 comments on commit 323cb13

Please sign in to comment.