|
Packit Service |
509fd4 |
#!/bin/bash
|
|
Packit Service |
509fd4 |
set -euo pipefail
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
OSBUILD_COMPOSER_TEST_DATA=/usr/share/tests/osbuild-composer/
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
source /etc/os-release
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Colorful output.
|
|
Packit Service |
509fd4 |
function greenprint {
|
|
Packit Service |
509fd4 |
echo -e "\033[1;32m${1}\033[0m"
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Provision the software under tet.
|
|
Packit Service |
509fd4 |
/usr/libexec/osbuild-composer-test/provision.sh
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Apply lorax patch to work around pytoml issues in RHEL 8.x.
|
|
Packit Service |
509fd4 |
# See BZ 1843704 or https://github.com/weldr/lorax/pull/1030 for more details.
|
|
Packit Service |
509fd4 |
if [[ $ID == rhel ]]; then
|
|
Packit Service |
509fd4 |
sudo sed -r -i 's#toml.load\(args\[3\]\)#toml.load(open(args[3]))#' \
|
|
Packit Service |
509fd4 |
/usr/lib/python3.6/site-packages/composer/cli/compose.py
|
|
Packit Service |
509fd4 |
sudo rm -f /usr/lib/python3.6/site-packages/composer/cli/compose.pyc
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# We need awscli to talk to AWS.
|
|
Packit Service |
509fd4 |
if ! hash aws; then
|
|
Packit Service |
509fd4 |
greenprint "Installing awscli"
|
|
Packit Service |
509fd4 |
pushd /tmp
|
|
Packit Service |
509fd4 |
curl -Ls --retry 5 --output awscliv2.zip \
|
|
Packit Service |
509fd4 |
https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip
|
|
Packit Service |
509fd4 |
unzip awscliv2.zip > /dev/null
|
|
Packit Service |
509fd4 |
sudo ./aws/install > /dev/null
|
|
Packit Service |
509fd4 |
aws --version
|
|
Packit Service |
509fd4 |
popd
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
TEST_UUID=$(uuidgen)
|
|
Packit Service |
509fd4 |
IMAGE_KEY=osbuild-composer-aws-test-${TEST_UUID}
|
|
Packit Service |
509fd4 |
AWS_CMD="aws --region $AWS_REGION --output json --color on"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Jenkins sets WORKSPACE to the job workspace, but if this script runs
|
|
Packit Service |
509fd4 |
# outside of Jenkins, we can set up a temporary directory instead.
|
|
Packit Service |
509fd4 |
if [[ ${WORKSPACE:-empty} == empty ]]; then
|
|
Packit Service |
509fd4 |
WORKSPACE=$(mktemp -d)
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Set up temporary files.
|
|
Packit Service |
509fd4 |
TEMPDIR=$(mktemp -d)
|
|
Packit Service |
509fd4 |
AWS_CONFIG=${TEMPDIR}/aws.toml
|
|
Packit Service |
509fd4 |
BLUEPRINT_FILE=${TEMPDIR}/blueprint.toml
|
|
Packit Service |
509fd4 |
AWS_INSTANCE_JSON=${TEMPDIR}/aws-instance.json
|
|
Packit Service |
509fd4 |
COMPOSE_START=${TEMPDIR}/compose-start-${IMAGE_KEY}.json
|
|
Packit Service |
509fd4 |
COMPOSE_INFO=${TEMPDIR}/compose-info-${IMAGE_KEY}.json
|
|
Packit Service |
509fd4 |
AMI_DATA=${TEMPDIR}/ami-data-${IMAGE_KEY}.json
|
|
Packit Service |
509fd4 |
INSTANCE_DATA=${TEMPDIR}/instance-data-${IMAGE_KEY}.json
|
|
Packit Service |
509fd4 |
INSTANCE_CONSOLE=${TEMPDIR}/instance-console-${IMAGE_KEY}.json
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Check for the smoke test file on the AWS instance that we start.
|
|
Packit Service |
509fd4 |
smoke_test_check () {
|
|
Packit Service |
509fd4 |
# Ensure the ssh key has restricted permissions.
|
|
Packit Service |
509fd4 |
SSH_KEY=${OSBUILD_COMPOSER_TEST_DATA}keyring/id_rsa
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
SMOKE_TEST=$(sudo ssh -i "${SSH_KEY}" redhat@"${1}" 'cat /etc/smoke-test.txt')
|
|
Packit Service |
509fd4 |
if [[ $SMOKE_TEST == smoke-test ]]; then
|
|
Packit Service |
509fd4 |
echo 1
|
|
Packit Service |
509fd4 |
else
|
|
Packit Service |
509fd4 |
echo 0
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Get the compose log.
|
|
Packit Service |
509fd4 |
get_compose_log () {
|
|
Packit Service |
509fd4 |
COMPOSE_ID=$1
|
|
Packit Service |
509fd4 |
LOG_FILE=${WORKSPACE}/osbuild-${ID}-${VERSION_ID}-aws.log
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Download the logs.
|
|
Packit Service |
509fd4 |
sudo composer-cli compose log "$COMPOSE_ID" | tee "$LOG_FILE" > /dev/null
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Get the compose metadata.
|
|
Packit Service |
509fd4 |
get_compose_metadata () {
|
|
Packit Service |
509fd4 |
COMPOSE_ID=$1
|
|
Packit Service |
509fd4 |
METADATA_FILE=${WORKSPACE}/osbuild-${ID}-${VERSION_ID}-aws.json
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Download the metadata.
|
|
Packit Service |
509fd4 |
sudo composer-cli compose metadata "$COMPOSE_ID" > /dev/null
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Find the tarball and extract it.
|
|
Packit Service |
509fd4 |
TARBALL=$(basename "$(find . -maxdepth 1 -type f -name "*-metadata.tar")")
|
|
Packit Service |
509fd4 |
tar -xf "$TARBALL"
|
|
Packit Service |
509fd4 |
rm -f "$TARBALL"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Move the JSON file into place.
|
|
Packit Service |
509fd4 |
cat "${COMPOSE_ID}".json | jq -M '.' | tee "$METADATA_FILE" > /dev/null
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Get the console screenshot from the AWS instance.
|
|
Packit Service |
509fd4 |
store_instance_screenshot () {
|
|
Packit Service |
509fd4 |
INSTANCE_ID=${1}
|
|
Packit Service |
509fd4 |
LOOP_COUNTER=${2}
|
|
Packit Service |
509fd4 |
SCREENSHOT_FILE=${WORKSPACE}/console-screenshot-${ID}-${VERSION_ID}-${LOOP_COUNTER}.jpg
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 get-console-screenshot --instance-id "${1}" > "$INSTANCE_CONSOLE"
|
|
Packit Service |
509fd4 |
jq -r '.ImageData' "$INSTANCE_CONSOLE" | base64 -d - > "$SCREENSHOT_FILE"
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Write an AWS TOML file
|
|
Packit Service |
509fd4 |
tee "$AWS_CONFIG" > /dev/null << EOF
|
|
Packit Service |
509fd4 |
provider = "aws"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
[settings]
|
|
Packit Service |
509fd4 |
accessKeyID = "${AWS_ACCESS_KEY_ID}"
|
|
Packit Service |
509fd4 |
secretAccessKey = "${AWS_SECRET_ACCESS_KEY}"
|
|
Packit Service |
509fd4 |
bucket = "${AWS_BUCKET}"
|
|
Packit Service |
509fd4 |
region = "${AWS_REGION}"
|
|
Packit Service |
509fd4 |
key = "${IMAGE_KEY}"
|
|
Packit Service |
509fd4 |
EOF
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Write a basic blueprint for our image.
|
|
Packit Service |
509fd4 |
tee "$BLUEPRINT_FILE" > /dev/null << EOF
|
|
Packit Service |
509fd4 |
name = "bash"
|
|
Packit Service |
509fd4 |
description = "A base system with bash"
|
|
Packit Service |
509fd4 |
version = "0.0.1"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
[[packages]]
|
|
Packit Service |
509fd4 |
name = "bash"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
[customizations.services]
|
|
Packit Service |
509fd4 |
enabled = ["sshd", "cloud-init", "cloud-init-local", "cloud-config", "cloud-final"]
|
|
Packit Service |
509fd4 |
EOF
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Prepare the blueprint for the compose.
|
|
Packit Service |
509fd4 |
greenprint "๐ Preparing blueprint"
|
|
Packit Service |
509fd4 |
sudo composer-cli blueprints push "$BLUEPRINT_FILE"
|
|
Packit Service |
509fd4 |
sudo composer-cli blueprints depsolve bash
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Get worker unit file so we can watch the journal.
|
|
Packit Service |
509fd4 |
WORKER_UNIT=$(sudo systemctl list-units | grep -o -E "osbuild.*worker.*\.service")
|
|
Packit Service |
509fd4 |
sudo journalctl -af -n 1 -u "${WORKER_UNIT}" &
|
|
Packit Service |
509fd4 |
WORKER_JOURNAL_PID=$!
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Start the compose and upload to AWS.
|
|
Packit Service |
509fd4 |
greenprint "๐ Starting compose"
|
|
Packit Service |
509fd4 |
sudo composer-cli --json compose start bash ami "$IMAGE_KEY" "$AWS_CONFIG" | tee "$COMPOSE_START"
|
|
Packit Service |
509fd4 |
COMPOSE_ID=$(jq -r '.build_id' "$COMPOSE_START")
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Wait for the compose to finish.
|
|
Packit Service |
509fd4 |
greenprint "โฑ Waiting for compose to finish: ${COMPOSE_ID}"
|
|
Packit Service |
509fd4 |
while true; do
|
|
Packit Service |
509fd4 |
sudo composer-cli --json compose info "${COMPOSE_ID}" | tee "$COMPOSE_INFO" > /dev/null
|
|
Packit Service |
509fd4 |
COMPOSE_STATUS=$(jq -r '.queue_status' "$COMPOSE_INFO")
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Is the compose finished?
|
|
Packit Service |
509fd4 |
if [[ $COMPOSE_STATUS != RUNNING ]] && [[ $COMPOSE_STATUS != WAITING ]]; then
|
|
Packit Service |
509fd4 |
break
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Wait 30 seconds and try again.
|
|
Packit Service |
509fd4 |
sleep 30
|
|
Packit Service |
509fd4 |
done
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Capture the compose logs from osbuild.
|
|
Packit Service |
509fd4 |
greenprint "๐ฌ Getting compose log and metadata"
|
|
Packit Service |
509fd4 |
get_compose_log "$COMPOSE_ID"
|
|
Packit Service |
509fd4 |
get_compose_metadata "$COMPOSE_ID"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Did the compose finish with success?
|
|
Packit Service |
509fd4 |
if [[ $COMPOSE_STATUS != FINISHED ]]; then
|
|
Packit Service |
509fd4 |
echo "Something went wrong with the compose. ๐ข"
|
|
Packit Service |
509fd4 |
exit 1
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Find the image that we made in AWS.
|
|
Packit Service |
509fd4 |
greenprint "๐ Search for created AMI"
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 describe-images \
|
|
Packit Service |
509fd4 |
--owners self \
|
|
Packit Service |
509fd4 |
--filters Name=name,Values="${IMAGE_KEY}" \
|
|
Packit Service |
509fd4 |
| tee "$AMI_DATA" > /dev/null
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
AMI_IMAGE_ID=$(jq -r '.Images[].ImageId' "$AMI_DATA")
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Stop watching the worker journal.
|
|
Packit Service |
509fd4 |
sudo kill ${WORKER_JOURNAL_PID}
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# NOTE(mhayden): Getting TagSpecifications to play along with bash's
|
|
Packit Service |
509fd4 |
# parsing of curly braces and square brackets is nuts, so we just write some
|
|
Packit Service |
509fd4 |
# json and pass it to the aws command.
|
|
Packit Service |
509fd4 |
tee "$AWS_INSTANCE_JSON" > /dev/null << EOF
|
|
Packit Service |
509fd4 |
{
|
|
Packit Service |
509fd4 |
"TagSpecifications": [
|
|
Packit Service |
509fd4 |
{
|
|
Packit Service |
509fd4 |
"ResourceType": "instance",
|
|
Packit Service |
509fd4 |
"Tags": [
|
|
Packit Service |
509fd4 |
{
|
|
Packit Service |
509fd4 |
"Key": "Name",
|
|
Packit Service |
509fd4 |
"Value": "${IMAGE_KEY}"
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
]
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
]
|
|
Packit Service |
509fd4 |
}
|
|
Packit Service |
509fd4 |
EOF
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Build instance in AWS with our image.
|
|
Packit Service |
509fd4 |
greenprint "๐ท๐ป Building instance in AWS"
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 run-instances \
|
|
Packit Service |
509fd4 |
--associate-public-ip-address \
|
|
Packit Service |
509fd4 |
--key-name personal_servers \
|
|
Packit Service |
509fd4 |
--image-id "${AMI_IMAGE_ID}" \
|
|
Packit Service |
509fd4 |
--instance-type t3a.micro \
|
|
Packit Service |
509fd4 |
--user-data file://"${OSBUILD_COMPOSER_TEST_DATA}"/cloud-init/user-data \
|
|
Packit Service |
509fd4 |
--cli-input-json file://"${AWS_INSTANCE_JSON}" > /dev/null
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Wait for the instance to finish building.
|
|
Packit Service |
509fd4 |
greenprint "โฑ Waiting for AWS instance to be marked as running"
|
|
Packit Service |
509fd4 |
while true; do
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 describe-instances \
|
|
Packit Service |
509fd4 |
--filters Name=image-id,Values="${AMI_IMAGE_ID}" \
|
|
Packit Service |
509fd4 |
| tee "$INSTANCE_DATA" > /dev/null
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
INSTANCE_STATUS=$(jq -r '.Reservations[].Instances[].State.Name' "$INSTANCE_DATA")
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Break the loop if our instance is running.
|
|
Packit Service |
509fd4 |
if [[ $INSTANCE_STATUS == running ]]; then
|
|
Packit Service |
509fd4 |
break
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Sleep for 10 seconds and try again.
|
|
Packit Service |
509fd4 |
sleep 10
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
done
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Get data about the instance we built.
|
|
Packit Service |
509fd4 |
INSTANCE_ID=$(jq -r '.Reservations[].Instances[].InstanceId' "$INSTANCE_DATA")
|
|
Packit Service |
509fd4 |
PUBLIC_IP=$(jq -r '.Reservations[].Instances[].PublicIpAddress' "$INSTANCE_DATA")
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Wait for the node to come online.
|
|
Packit Service |
509fd4 |
greenprint "โฑ Waiting for AWS instance to respond to ssh"
|
|
Packit Service |
509fd4 |
for LOOP_COUNTER in {0..30}; do
|
|
Packit Service |
509fd4 |
if ssh-keyscan "$PUBLIC_IP" > /dev/null 2>&1; then
|
|
Packit Service |
509fd4 |
echo "SSH is up!"
|
|
Packit Service |
509fd4 |
ssh-keyscan "$PUBLIC_IP" | sudo tee -a /root/.ssh/known_hosts
|
|
Packit Service |
509fd4 |
break
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Get a screenshot of the instance console.
|
|
Packit Service |
509fd4 |
echo "Getting instance screenshot..."
|
|
Packit Service |
509fd4 |
store_instance_screenshot "$INSTANCE_ID" "$LOOP_COUNTER" || true
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# ssh-keyscan has a 5 second timeout by default, so the pause per loop
|
|
Packit Service |
509fd4 |
# is 10 seconds when you include the following `sleep`.
|
|
Packit Service |
509fd4 |
echo "Retrying in 5 seconds..."
|
|
Packit Service |
509fd4 |
sleep 5
|
|
Packit Service |
509fd4 |
done
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Check for our smoke test file.
|
|
Packit Service |
509fd4 |
greenprint "๐ Checking for smoke test file"
|
|
Packit Service |
509fd4 |
for LOOP_COUNTER in {0..10}; do
|
|
Packit Service |
509fd4 |
RESULTS="$(smoke_test_check "$PUBLIC_IP")"
|
|
Packit Service |
509fd4 |
if [[ $RESULTS == 1 ]]; then
|
|
Packit Service |
509fd4 |
echo "Smoke test passed! ๐ฅณ"
|
|
Packit Service |
509fd4 |
break
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
sleep 5
|
|
Packit Service |
509fd4 |
done
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Clean up our mess.
|
|
Packit Service |
509fd4 |
greenprint "๐งผ Cleaning up"
|
|
Packit Service |
509fd4 |
SNAPSHOT_ID=$(jq -r '.Images[].BlockDeviceMappings[].Ebs.SnapshotId' "$AMI_DATA")
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 terminate-instances --instance-id "${INSTANCE_ID}"
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 deregister-image --image-id "${AMI_IMAGE_ID}"
|
|
Packit Service |
509fd4 |
$AWS_CMD ec2 delete-snapshot --snapshot-id "${SNAPSHOT_ID}"
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Also delete the compose so we don't run out of disk space
|
|
Packit Service |
509fd4 |
sudo composer-cli compose delete "${COMPOSE_ID}" > /dev/null
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
# Use the return code of the smoke test to determine if we passed or failed.
|
|
Packit Service |
509fd4 |
# On rhel continue with the cloudapi test
|
|
Packit Service |
509fd4 |
if [[ $RESULTS == 1 ]]; then
|
|
Packit Service |
509fd4 |
greenprint "๐ Success"
|
|
Packit Service |
509fd4 |
exit 0
|
|
Packit Service |
509fd4 |
elif [[ $RESULTS != 1 ]]; then
|
|
Packit Service |
509fd4 |
greenprint "โ Failed"
|
|
Packit Service |
509fd4 |
exit 1
|
|
Packit Service |
509fd4 |
fi
|
|
Packit Service |
509fd4 |
|
|
Packit Service |
509fd4 |
exit 0
|