|
| 1 | +#!/usr/bin/env bash |
| 2 | +# AWS resource-name isolation and failed-deploy discovery helpers. |
| 3 | + |
| 4 | +hf_derive_project_name() { |
| 5 | + local stack_name="$1" prefix digest |
| 6 | + prefix=$(printf '%s' "$stack_name" | |
| 7 | + tr -c '[:alnum:]-' '-' | |
| 8 | + sed -E 's/^-+//; s/-+$//' | |
| 9 | + cut -c1-36) |
| 10 | + [ -n "$prefix" ] || prefix="hf-smoke" |
| 11 | + digest=$(printf '%s' "$stack_name" | sha256sum | awk '{print substr($1,1,12)}') |
| 12 | + printf '%s-%s\n' "$prefix" "$digest" |
| 13 | +} |
| 14 | + |
| 15 | +hf_known_absent() { |
| 16 | + local pattern="$1" output_file="$2" |
| 17 | + grep -Eiq "$pattern" "$output_file" |
| 18 | +} |
| 19 | + |
| 20 | +hf_assert_command_absent() { |
| 21 | + local label="$1" absent_pattern="$2" |
| 22 | + shift 2 |
| 23 | + local output_file status detail |
| 24 | + output_file=$(mktemp) |
| 25 | + if "$@" >"$output_file" 2>&1; then |
| 26 | + echo "ERROR: destructive-isolation collision: $label already exists" >&2 |
| 27 | + rm -f "$output_file" |
| 28 | + return 1 |
| 29 | + else |
| 30 | + status=$? |
| 31 | + fi |
| 32 | + if ! hf_known_absent "$absent_pattern" "$output_file"; then |
| 33 | + detail=$(tr '\n' ' ' < "$output_file" | cut -c1-240) |
| 34 | + echo "ERROR: could not prove $label absent (exit=$status): $detail" >&2 |
| 35 | + rm -f "$output_file" |
| 36 | + return 2 |
| 37 | + fi |
| 38 | + rm -f "$output_file" |
| 39 | +} |
| 40 | + |
| 41 | +hf_assert_named_list_absent() { |
| 42 | + local label="$1" |
| 43 | + shift |
| 44 | + local output_file output status detail |
| 45 | + output_file=$(mktemp) |
| 46 | + if output=$("$@" 2>"$output_file"); then |
| 47 | + if [ -n "$output" ]; then |
| 48 | + echo "ERROR: destructive-isolation collision: $label already exists ($output)" >&2 |
| 49 | + rm -f "$output_file" |
| 50 | + return 1 |
| 51 | + fi |
| 52 | + else |
| 53 | + status=$? |
| 54 | + detail=$(tr '\n' ' ' < "$output_file" | cut -c1-240) |
| 55 | + echo "ERROR: could not verify $label absence (exit=$status): $detail" >&2 |
| 56 | + rm -f "$output_file" |
| 57 | + return 2 |
| 58 | + fi |
| 59 | + rm -f "$output_file" |
| 60 | +} |
| 61 | + |
| 62 | +# Fail closed unless every exact name this smoke run can destructively clean |
| 63 | +# is absent. Call before arming cleanup or creating any AWS resource. |
| 64 | +hf_assert_deploy_isolation() { |
| 65 | + local stack_name="$1" project_name="$2" |
| 66 | + local function_name="${project_name}-render" |
| 67 | + local lambda_log="/aws/lambda/${function_name}" |
| 68 | + local states_log="/aws/states/${function_name}" |
| 69 | + |
| 70 | + hf_assert_command_absent "CloudFormation stack $stack_name" "does not exist" \ |
| 71 | + aws cloudformation describe-stacks --stack-name "$stack_name" && |
| 72 | + hf_assert_command_absent "Lambda function $function_name" \ |
| 73 | + "ResourceNotFoundException|Function not found" \ |
| 74 | + aws lambda get-function --function-name "$function_name" && |
| 75 | + hf_assert_named_list_absent "Step Functions state machine $function_name" \ |
| 76 | + aws stepfunctions list-state-machines \ |
| 77 | + --query "stateMachines[?name=='$function_name'].stateMachineArn" --output text && |
| 78 | + hf_assert_named_list_absent "log group $lambda_log" \ |
| 79 | + aws logs describe-log-groups --log-group-name-prefix "$lambda_log" \ |
| 80 | + --query "logGroups[?logGroupName=='$lambda_log'].logGroupName" --output text && |
| 81 | + hf_assert_named_list_absent "log group $states_log" \ |
| 82 | + aws logs describe-log-groups --log-group-name-prefix "$states_log" \ |
| 83 | + --query "logGroups[?logGroupName=='$states_log'].logGroupName" --output text |
| 84 | +} |
| 85 | + |
| 86 | +# Return a JSON object with any physical resources CloudFormation managed to |
| 87 | +# create, even when stack outputs were never populated. A genuinely absent |
| 88 | +# stack is an empty result; auth/network/query failures are errors. |
| 89 | +hf_discover_stack_resources() { |
| 90 | + local stack_name="$1" output_file error_file status detail |
| 91 | + output_file=$(mktemp) |
| 92 | + error_file=$(mktemp) |
| 93 | + if aws cloudformation list-stack-resources \ |
| 94 | + --stack-name "$stack_name" --output json >"$output_file" 2>"$error_file"; then |
| 95 | + jq '{ |
| 96 | + renderBucket: ( |
| 97 | + [.StackResourceSummaries[]? |
| 98 | + | select(.LogicalResourceId == "RenderBucket") |
| 99 | + | .PhysicalResourceId][0] // "" |
| 100 | + ), |
| 101 | + stateMachineArn: ( |
| 102 | + [.StackResourceSummaries[]? |
| 103 | + | select(.LogicalResourceId == "RenderStateMachine") |
| 104 | + | .PhysicalResourceId][0] // "" |
| 105 | + ) |
| 106 | + }' "$output_file" |
| 107 | + rm -f "$output_file" "$error_file" |
| 108 | + return |
| 109 | + else |
| 110 | + status=$? |
| 111 | + fi |
| 112 | + if hf_known_absent "does not exist" "$error_file"; then |
| 113 | + printf '{"renderBucket":"","stateMachineArn":""}\n' |
| 114 | + rm -f "$output_file" "$error_file" |
| 115 | + return |
| 116 | + fi |
| 117 | + detail=$(tr '\n' ' ' < "$error_file" | cut -c1-240) |
| 118 | + echo "ERROR: failed to discover physical stack resources (exit=$status): $detail" >&2 |
| 119 | + rm -f "$output_file" "$error_file" |
| 120 | + return 2 |
| 121 | +} |
0 commit comments