|
| 1 | +#!/usr/bin/env bash |
| 2 | +# AWS resource-name isolation and failed-deploy discovery helpers. |
| 3 | + |
| 4 | +hf_new_smoke_run_id() { |
| 5 | + local seconds seed digest |
| 6 | + seconds=$(date +%s) |
| 7 | + seed="${seconds}:$$:${RANDOM}:${BASHPID:-$$}" |
| 8 | + digest=$(printf '%s' "$seed" | sha256sum | awk '{print substr($1,1,16)}') |
| 9 | + printf '%s-%s\n' "$seconds" "$digest" |
| 10 | +} |
| 11 | + |
| 12 | +hf_sam_deploy_bucket_name() { |
| 13 | + local account_id="$1" region="$2" run_id="$3" digest |
| 14 | + digest=$(printf '%s' "$run_id" | sha256sum | awk '{print substr($1,1,20)}') |
| 15 | + printf 'hf-sam-%s-%s-%s\n' "$account_id" "$region" "$digest" |
| 16 | +} |
| 17 | + |
| 18 | +hf_derive_project_name() { |
| 19 | + local stack_name="$1" prefix digest |
| 20 | + prefix=$(printf '%s' "$stack_name" | |
| 21 | + tr -c '[:alnum:]-' '-' | |
| 22 | + sed -E 's/^-+//; s/-+$//' | |
| 23 | + cut -c1-36) |
| 24 | + [ -n "$prefix" ] || prefix="hf-smoke" |
| 25 | + digest=$(printf '%s' "$stack_name" | sha256sum | awk '{print substr($1,1,12)}') |
| 26 | + printf '%s-%s\n' "$prefix" "$digest" |
| 27 | +} |
| 28 | + |
| 29 | +hf_known_absent() { |
| 30 | + local pattern="$1" output_file="$2" |
| 31 | + grep -Eiq "$pattern" "$output_file" |
| 32 | +} |
| 33 | + |
| 34 | +hf_assert_command_absent() { |
| 35 | + local label="$1" absent_pattern="$2" |
| 36 | + shift 2 |
| 37 | + local output_file status detail |
| 38 | + output_file=$(mktemp) |
| 39 | + if "$@" >"$output_file" 2>&1; then |
| 40 | + echo "ERROR: destructive-isolation collision: $label already exists" >&2 |
| 41 | + rm -f "$output_file" |
| 42 | + return 1 |
| 43 | + else |
| 44 | + status=$? |
| 45 | + fi |
| 46 | + if ! hf_known_absent "$absent_pattern" "$output_file"; then |
| 47 | + detail=$(tr '\n' ' ' < "$output_file" | cut -c1-240) |
| 48 | + echo "ERROR: could not prove $label absent (exit=$status): $detail" >&2 |
| 49 | + rm -f "$output_file" |
| 50 | + return 2 |
| 51 | + fi |
| 52 | + rm -f "$output_file" |
| 53 | +} |
| 54 | + |
| 55 | +hf_assert_named_list_absent() { |
| 56 | + local label="$1" |
| 57 | + shift |
| 58 | + local output_file output status detail |
| 59 | + output_file=$(mktemp) |
| 60 | + if output=$("$@" 2>"$output_file"); then |
| 61 | + if [ -n "$output" ]; then |
| 62 | + echo "ERROR: destructive-isolation collision: $label already exists ($output)" >&2 |
| 63 | + rm -f "$output_file" |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + else |
| 67 | + status=$? |
| 68 | + detail=$(tr '\n' ' ' < "$output_file" | cut -c1-240) |
| 69 | + echo "ERROR: could not verify $label absence (exit=$status): $detail" >&2 |
| 70 | + rm -f "$output_file" |
| 71 | + return 2 |
| 72 | + fi |
| 73 | + rm -f "$output_file" |
| 74 | +} |
| 75 | + |
| 76 | +# Fail closed unless every exact name this smoke run can destructively clean |
| 77 | +# is absent. Call before arming cleanup or creating any AWS resource. |
| 78 | +hf_assert_deploy_isolation() { |
| 79 | + local stack_name="$1" project_name="$2" |
| 80 | + local function_name="${project_name}-render" |
| 81 | + local lambda_log="/aws/lambda/${function_name}" |
| 82 | + local states_log="/aws/states/${function_name}" |
| 83 | + |
| 84 | + hf_assert_command_absent "CloudFormation stack $stack_name" "does not exist" \ |
| 85 | + aws cloudformation describe-stacks --stack-name "$stack_name" && |
| 86 | + hf_assert_command_absent "Lambda function $function_name" \ |
| 87 | + "ResourceNotFoundException|Function not found" \ |
| 88 | + aws lambda get-function --function-name "$function_name" && |
| 89 | + hf_assert_named_list_absent "Step Functions state machine $function_name" \ |
| 90 | + aws stepfunctions list-state-machines \ |
| 91 | + --query "stateMachines[?name=='$function_name'].stateMachineArn" --output text && |
| 92 | + hf_assert_named_list_absent "log group $lambda_log" \ |
| 93 | + aws logs describe-log-groups --log-group-name-prefix "$lambda_log" \ |
| 94 | + --query "logGroups[?logGroupName=='$lambda_log'].logGroupName" --output text && |
| 95 | + hf_assert_named_list_absent "log group $states_log" \ |
| 96 | + aws logs describe-log-groups --log-group-name-prefix "$states_log" \ |
| 97 | + --query "logGroups[?logGroupName=='$states_log'].logGroupName" --output text |
| 98 | +} |
| 99 | + |
| 100 | +# Atomically reserve the exact stack name before SAM can create or update it. |
| 101 | +# CloudFormation's create-stack call is the compare-and-set: only one concurrent |
| 102 | +# smoke run can acquire a name that both preflight checks observed as absent. |
| 103 | +hf_reserve_smoke_stack() { |
| 104 | + local stack_name="$1" run_id="$2" |
| 105 | + aws cloudformation create-stack \ |
| 106 | + --stack-name "$stack_name" \ |
| 107 | + --template-body \ |
| 108 | + '{"Resources":{"SmokeOwnershipHandle":{"Type":"AWS::CloudFormation::WaitConditionHandle"}}}' \ |
| 109 | + --tags "Key=HyperframesSmokeRun,Value=$run_id" >/dev/null && |
| 110 | + aws cloudformation wait stack-create-complete --stack-name "$stack_name" |
| 111 | +} |
| 112 | + |
| 113 | +# Print "owned" when the stack has this run's ownership tag and |
| 114 | +# "absent" when there is no stack. Any foreign/missing tag or AWS API error |
| 115 | +# fails closed so a cleanup trap cannot delete a concurrent run's resources. |
| 116 | +hf_stack_ownership_status() { |
| 117 | + local stack_name="$1" run_id="$2" output_file error_file owner status detail |
| 118 | + output_file=$(mktemp) |
| 119 | + error_file=$(mktemp) |
| 120 | + if aws cloudformation describe-stacks \ |
| 121 | + --stack-name "$stack_name" \ |
| 122 | + --query "Stacks[0].Tags[?Key=='HyperframesSmokeRun'].Value | [0]" \ |
| 123 | + --output text >"$output_file" 2>"$error_file"; then |
| 124 | + owner=$(tr -d '\r\n' <"$output_file") |
| 125 | + rm -f "$output_file" "$error_file" |
| 126 | + if [ "$owner" != "$run_id" ]; then |
| 127 | + echo "ERROR: refusing cleanup: stack ownership is '${owner:-missing}', expected '$run_id'" >&2 |
| 128 | + return 3 |
| 129 | + fi |
| 130 | + printf 'owned\n' |
| 131 | + return |
| 132 | + else |
| 133 | + status=$? |
| 134 | + fi |
| 135 | + if hf_known_absent "does not exist" "$error_file"; then |
| 136 | + rm -f "$output_file" "$error_file" |
| 137 | + printf 'absent\n' |
| 138 | + return |
| 139 | + fi |
| 140 | + detail=$(tr '\n' ' ' <"$error_file" | cut -c1-240) |
| 141 | + echo "ERROR: could not verify stack ownership (exit=$status): $detail" >&2 |
| 142 | + rm -f "$output_file" "$error_file" |
| 143 | + return 2 |
| 144 | +} |
| 145 | + |
| 146 | +# Return a JSON object with any physical resources CloudFormation managed to |
| 147 | +# create, even when stack outputs were never populated. A genuinely absent |
| 148 | +# stack is an empty result; auth/network/query failures are errors. |
| 149 | +hf_discover_stack_resources() { |
| 150 | + local stack_name="$1" output_file error_file status detail |
| 151 | + output_file=$(mktemp) |
| 152 | + error_file=$(mktemp) |
| 153 | + if aws cloudformation list-stack-resources \ |
| 154 | + --stack-name "$stack_name" --output json >"$output_file" 2>"$error_file"; then |
| 155 | + jq '{ |
| 156 | + renderBucket: ( |
| 157 | + [.StackResourceSummaries[]? |
| 158 | + | select(.LogicalResourceId == "RenderBucket") |
| 159 | + | .PhysicalResourceId][0] // "" |
| 160 | + ), |
| 161 | + stateMachineArn: ( |
| 162 | + [.StackResourceSummaries[]? |
| 163 | + | select(.LogicalResourceId == "RenderStateMachine") |
| 164 | + | .PhysicalResourceId][0] // "" |
| 165 | + ) |
| 166 | + }' "$output_file" |
| 167 | + rm -f "$output_file" "$error_file" |
| 168 | + return |
| 169 | + else |
| 170 | + status=$? |
| 171 | + fi |
| 172 | + if hf_known_absent "does not exist" "$error_file"; then |
| 173 | + printf '{"renderBucket":"","stateMachineArn":""}\n' |
| 174 | + rm -f "$output_file" "$error_file" |
| 175 | + return |
| 176 | + fi |
| 177 | + detail=$(tr '\n' ' ' < "$error_file" | cut -c1-240) |
| 178 | + echo "ERROR: failed to discover physical stack resources (exit=$status): $detail" >&2 |
| 179 | + rm -f "$output_file" "$error_file" |
| 180 | + return 2 |
| 181 | +} |
0 commit comments