|
| 1 | +#!/bin/bash |
| 2 | +# Record binary sizes to DynamoDB after a main merge. |
| 3 | +# Reads sizes from S3 (binaries already uploaded by BuildAndUpload job). |
| 4 | +# |
| 5 | +# Required env: S3_BUCKET, COMMIT_HASH, COMMIT_DATE, TAG (optional) |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +TABLE_NAME="CWABinarySizes" |
| 10 | +S3_BUCKET="${S3_BUCKET:?S3_BUCKET is required}" |
| 11 | +COMMIT_HASH="${COMMIT_HASH:?COMMIT_HASH is required}" |
| 12 | +COMMIT_DATE="${COMMIT_DATE:?COMMIT_DATE is required}" |
| 13 | +TAG="${TAG:-}" |
| 14 | +REGION="us-west-2" |
| 15 | + |
| 16 | +S3_PREFIX="integration-test/binary/${COMMIT_HASH}/" |
| 17 | + |
| 18 | +echo "Collecting binary sizes from S3 (${COMMIT_HASH:0:12})..." |
| 19 | + |
| 20 | +ITEM_JSON=$( |
| 21 | + aws s3 ls "s3://${S3_BUCKET}/${S3_PREFIX}" --recursive --region "$REGION" | |
| 22 | + awk '{print $3, $4}' | |
| 23 | + python3 - "$S3_PREFIX" "$COMMIT_HASH" "$COMMIT_DATE" "$TAG" <<'PYEOF' |
| 24 | +import sys, json |
| 25 | +
|
| 26 | +prefix = sys.argv[1] |
| 27 | +commit_hash = sys.argv[2] |
| 28 | +commit_date = sys.argv[3] |
| 29 | +tag = sys.argv[4] |
| 30 | +
|
| 31 | +skip_ext = ('.sig', '.jar', '.rpm', '.deb', '.pkg', '.msi', '.tar.gz', '.gz', '.zip') |
| 32 | +skip_names = ('CWAGENT_VERSION', 'buildMSI.zip') |
| 33 | +
|
| 34 | +binaries = {} |
| 35 | +for line in sys.stdin: |
| 36 | + line = line.strip() |
| 37 | + if not line: |
| 38 | + continue |
| 39 | + parts = line.split(' ', 1) |
| 40 | + if len(parts) != 2: |
| 41 | + continue |
| 42 | + size, key = int(parts[0]), parts[1] |
| 43 | + rel_path = key[len(prefix):] if key.startswith(prefix) else key |
| 44 | + segments = rel_path.split('/') |
| 45 | + if len(segments) != 2: |
| 46 | + continue |
| 47 | + filename = segments[1] |
| 48 | + if any(filename.endswith(ext) for ext in skip_ext): |
| 49 | + continue |
| 50 | + if filename in skip_names: |
| 51 | + continue |
| 52 | + binaries[rel_path] = {"N": str(size)} |
| 53 | +
|
| 54 | +if not binaries: |
| 55 | + sys.exit(1) |
| 56 | +
|
| 57 | +record_type = "release" if tag else "commit" |
| 58 | +item = { |
| 59 | + "CommitHash": {"S": commit_hash}, |
| 60 | + "CommitDate": {"N": commit_date}, |
| 61 | + "Branch": {"S": "main"}, |
| 62 | + "RecordType": {"S": record_type}, |
| 63 | + "Binaries": {"M": binaries}, |
| 64 | +} |
| 65 | +if tag: |
| 66 | + item["Tag"] = {"S": tag} |
| 67 | +
|
| 68 | +print(json.dumps(item)) |
| 69 | +PYEOF |
| 70 | +) || { |
| 71 | + echo "No binaries found in S3, skipping." |
| 72 | + exit 0 |
| 73 | +} |
| 74 | + |
| 75 | +RECORD_TYPE=$(echo "$ITEM_JSON" | python3 -c "import json,sys; print(json.load(sys.stdin)['RecordType']['S'])") |
| 76 | + |
| 77 | +if [[ "$RECORD_TYPE" == "release" ]]; then |
| 78 | + aws dynamodb put-item \ |
| 79 | + --table-name "$TABLE_NAME" \ |
| 80 | + --item "$ITEM_JSON" \ |
| 81 | + --region "$REGION" |
| 82 | +else |
| 83 | + err=$(aws dynamodb put-item \ |
| 84 | + --table-name "$TABLE_NAME" \ |
| 85 | + --item "$ITEM_JSON" \ |
| 86 | + --condition-expression "attribute_not_exists(CommitHash) OR RecordType <> :release" \ |
| 87 | + --expression-attribute-values '{":release": {"S": "release"}}' \ |
| 88 | + --region "$REGION" 2>&1) || { |
| 89 | + if echo "$err" | grep -q "ConditionalCheckFailedException"; then |
| 90 | + echo "Skipped (existing release entry)" |
| 91 | + else |
| 92 | + echo "$err" >&2 |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + } |
| 96 | +fi |
| 97 | + |
| 98 | +echo "Recorded binary sizes for ${COMMIT_HASH:0:12} (type=${RECORD_TYPE}, tag=${TAG:-none})" |
0 commit comments