Skip to content

Commit

Permalink
Merge pull request containers#165 from cevich/get_ci_vm_ec2_search
Browse files Browse the repository at this point in the history
[CI:TOOLING] Add support for AMI name-tag search
  • Loading branch information
cevich authored Aug 9, 2022
2 parents 644044f + 2a3152b commit be64f40
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
40 changes: 40 additions & 0 deletions get_ci_vm/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ init_ec2vm() {
_dbg_envars INST_NAME EC2_INST_TYPE AWSCLI SSH_CMD EC2_SSH_KEY EC2_SSH_ARGS CREATE_CMD CLEANUP_CMD
status "Confirming and/or configuring AWS access."
if ! has_valid_aws_credentials; then setup_aws; fi
select_ec2_inst_image
}
# Updates $EC2_INST_ID on successful lookup of an ec2 instance based on its
Expand Down Expand Up @@ -695,6 +697,44 @@ set_ec2_dns_name() {
return 1
}
# Cirrus-CI supports multiple methods when specifying an EC2 image
# to use. This function supports two of them: Either use the literal
# "ami-*" value, or perform a search against the value as a "Name" tag.
# In the latter case, the newest image returned will be selected
# and $INST_IMAGE will be updated accordingly.
select_ec2_inst_image() {
req_env_vars INST_TYPE INST_IMAGE AWSCLI
# Direct image specification, nothing to do.
if [[ "$INST_IMAGE" =~ ^ami-.+ ]]; then return 0; fi
local _awsoutput _name_filter _result_filter
local -a _qcmd
dbg "Attempting to look up AMI for image name tag '$INST_IMAGE'"
_name_filter="Name=name,Values=$INST_IMAGE"
# Ignore any items not "available", reverse-sort by date, pick 1st item's AMI ID
_result_filter='.Images | map(select(.State == "available")) | sort_by(.CreationDate) | reverse | .[0].ImageId'
# Word-splitting for $AWSCLI is desired
# shellcheck disable=SC2206
_qcmd=(\
$AWSCLI ec2 describe-images --owners self
--filters "$_name_filter" --output json
)
# Empty $AWSCLI input to jq will NOT trigger its `-e`, so double-check.
if _awsoutput=$("${_qcmd[@]}") && \
[[ -n "$_awsoutput" ]] && \
_ami_id=$(jq -r -e "$_result_filter"<<<$_awsoutput) && \
[[ -n "$_ami_id" ]]
then
dbg "Found AMI ID '$_ami_id' with recent name tag '$INST_IMAGE'"
INST_IMAGE="$_ami_id"
else
die "Could not find an available AMI with name tag '$INST_IMAGE': $_awsoutput"
fi
}
# GCP provides a handy wrapper for ssh and scp, for AWS it's all DIY.
# EC2 instance initial reachability/state of the VM after creation is
# unknown for some time, depending on many factors. This function
Expand Down
2 changes: 1 addition & 1 deletion get_ci_vm/good_repo_test_v2/.cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ aws_test_task:
env:
EC2_INST_TYPE: bigone.supervm
ec2_instance:
image: ami-1234567890
image: fedora-podman-aws-arm64-c5495735033528320
type: ${EC2_INST_TYPE}

google_test_task:
Expand Down
32 changes: 32 additions & 0 deletions get_ci_vm/good_repo_test_v2/ami_search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Images": [
{
"CreationDate": "2022-08-30T22:22:22.000Z",
"ImageId": "ami-unavailable",
"State": "some-random-state",
"Name": "fedora-podman-aws-arm64-c5495735033528320"
},
{
"CreationDate": "2020-01-01T01:01:01.000Z",
"ImageId": "ami-oldest",
"State": "available",
"Name": "fedora-podman-aws-arm64-c5495735033528320"
},
{
"CreationDate": "2022-07-25T19:34:17.000Z",
"ImageId": "ami-newest",
"State": "available",
"Name": "fedora-podman-aws-arm64-c5495735033528320"
},
{
"key": "Garbage",
"val": "Junk Data"
},
{
"CreationDate": "2022-07-25T07:34:17.000Z",
"ImageId": "ami-almost-newest",
"State": "available",
"Name": "fedora-podman-aws-arm64-c5495735033528320"
}
]
}
23 changes: 19 additions & 4 deletions get_ci_vm/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,15 @@ testf "Verify mock 'ec2vm' w/o creds attempts to initialize" \
init_ec2vm

mock_init_aws() {
# Don't preserve arguments to make checking easier
# shellcheck disable=SC2145
echo "aws $@"
return 0
# Only care if string is present
# shellcheck disable=SC2199
if [[ "$@" =~ describe-images ]]; then
cat $GOOD_TEST_REPO_V2/ami_search.json
else
# Don't preserve arguments to make checking easier
# shellcheck disable=SC2145
echo "aws $@"
fi
}

mock_init_ec2vm() {
Expand All @@ -325,6 +330,16 @@ testf "Verify mock initialized 'ec2vm' is satisfied with test setup" \
mock_init_ec2vm 0 "" \
init_ec2vm

print_select_ec2_inst_image() {
export A_DEBUG=1
select_ec2_inst_image
echo "$INST_IMAGE"
}

testf "Verify AMI selection by name tag with from fake describe-images data" \
mock_init_ec2vm 0 "ami-newest" \
print_select_ec2_inst_image

# TODO: Add more EC2 tests

# Must be called last
Expand Down

0 comments on commit be64f40

Please sign in to comment.