Skip to content

Commit 3b7d15e

Browse files
authored
Report final AMI disk usage to workflow job (#2384)
* nix-provision: Remove unnecessary sudos This was left over from #2380 from when I did some commit re-ordering. * ci/testinfra: Drop docker stuff We don't use docker in the tests so no need to do any of these actions/install packages. * AMI: Report disk usage I've had a few occasions where I wanted to see disk usage changes and had to create a one-of PR to do so. Since AMI image size is important to us we should at least show it somewhere. I might post as a comment in the future, holding off for now because I'd like to post just one comment with the Postgres Extension Dependency Analysis info too. Note: I'm aware that if there's any issue getting, outputting, parsing the disk usage it'll break the build and I'm ok with that. I want to see disk usage and if we can't get it then something is wrong and should be fixed.
1 parent 3bb5f88 commit 3b7d15e

5 files changed

Lines changed: 82 additions & 13 deletions

File tree

.github/actions/build-ami/action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ outputs:
3737
execution_id:
3838
description: 'The execution ID for this build'
3939
value: ${{ steps.set-execution-id.outputs.execution_id }}
40+
disk_usage_json:
41+
description: 'The stage 2 AMI root disk usage as json'
42+
value: ${{ steps.build-stage2.outputs.disk_usage_json }}
4043

4144
runs:
4245
using: "composite"

.github/workflows/testinfra-ami-build.yml

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ env:
1919
AWS_REGION: ap-southeast-1
2020

2121
jobs:
22-
prepare:
22+
gen-matrix:
2323
runs-on: blacksmith-2vcpu-ubuntu-2404
2424
outputs:
2525
postgres_versions: ${{ steps.set-versions.outputs.postgres_versions }}
@@ -37,11 +37,11 @@ jobs:
3737
echo "postgres_versions=$VERSIONS" >> "$GITHUB_OUTPUT"
3838
3939
test-ami-nix:
40-
needs: prepare
40+
needs: gen-matrix
4141
strategy:
4242
fail-fast: false
4343
matrix:
44-
postgres_version: ${{ fromJson(needs.prepare.outputs.postgres_versions) }}
44+
postgres_version: ${{ fromJson(needs.gen-matrix.outputs.postgres_versions) }}
4545
target:
4646
- arch: amd64
4747
instance_type: c6i.4xlarge
@@ -100,20 +100,35 @@ jobs:
100100
postgres_version: ${{ matrix.postgres_version }}
101101
region: ${{ env.AWS_REGION }}
102102

103-
- run: docker context create builders
104-
105-
- uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
103+
- name: Save AMI disk usage
104+
env:
105+
ARCH: ${{ matrix.target.arch }}
106+
DISK_USAGE_JSON: ${{ steps.build-ami.outputs.disk_usage_json }}
107+
POSTGRES_VERSION: ${{ matrix.postgres_version }}
108+
run: >-
109+
jq -cnr
110+
--arg arch "$ARCH"
111+
--arg version "$POSTGRES_VERSION"
112+
--argjson disk_usage "$DISK_USAGE_JSON"
113+
'{$version,$arch,bytes:$disk_usage.bytes,human:$disk_usage.human}'
114+
>"ami-disk-usage-$POSTGRES_VERSION-$ARCH.json"
115+
116+
- name: Upload AMI disk usage
117+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
106118
with:
107-
endpoint: builders
119+
name: ami-disk-usage-${{ matrix.postgres_version }}-${{ matrix.target.arch }}
120+
path: ami-disk-usage-*.json
121+
overwrite: true
122+
retention-days: 1
108123

109124
- name: Run tests
110125
timeout-minutes: 10
111126
env:
112127
AMI_ID: ${{ steps.build-ami.outputs.stage2_ami_id }}
113128
EXECUTION_ID: ${{ steps.build-ami.outputs.execution_id }}
114129
run: |
115-
# TODO: use poetry for pkg mgmt
116-
pip3 install boto3 "boto3-stubs[essential]" docker ec2instanceconnectcli pytest "pytest-testinfra[paramiko,docker]" requests
130+
# TODO: use uv for pkg mgmt
131+
pip3 install boto3 'boto3-stubs[essential]' ec2instanceconnectcli pytest 'pytest-testinfra[paramiko]' requests
117132
pytest -vv -s testinfra/test_ami_nix.py
118133
119134
- name: Cleanup resources on build cancellation
@@ -173,3 +188,26 @@ jobs:
173188
else
174189
echo "No stage 2 AMI to clean up"
175190
fi
191+
192+
report-disk-usage:
193+
needs: test-ami-nix
194+
runs-on: blacksmith-2vcpu-ubuntu-2404
195+
steps:
196+
- name: Download AMI disk usage
197+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
198+
with:
199+
pattern: ami-disk-usage-*
200+
path: ami-disk-usage
201+
merge-multiple: true
202+
203+
- name: Report AMI disk usage
204+
run: |
205+
cat <<EOF >>"$GITHUB_STEP_SUMMARY"
206+
### AMI Root Disk Usage
207+
208+
| Version | Arch | Human | Bytes |
209+
| ------- | ---- | ----- | ----- |
210+
EOF
211+
cat ami-disk-usage/ami-disk-usage-*.json |
212+
sort -V |
213+
jq -rs '.[]|{version,arch,human,bytes}|"| \(join("|")) |"' >>"$GITHUB_STEP_SUMMARY"

ebssurrogate/scripts/nix-provision.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ exec 1>&2
99

1010
function install_packages {
1111
# Setup Ansible on host VM
12-
sudo apt-get update && sudo apt-get install -y software-properties-common
12+
apt-get update && apt-get install -y software-properties-common
1313

1414
# Install EC2-specific packages that were deferred from stage 1
1515
# These packages have post-install scripts that need EC2 metadata service access
1616
# which only works on a real running EC2 instance (not in chroot)
17-
sudo apt-get install -y ec2-hibinit-agent ec2-instance-connect hibagent
17+
apt-get install -y ec2-hibinit-agent ec2-instance-connect hibagent
1818

1919
# Manually add GPG key with explicit keyserver
20-
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 93C4A3FD7BB9C367
20+
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 93C4A3FD7BB9C367
2121

2222
# Add repository and install
2323
# TODO (darora): temporarily disabling while Launchpad is under ddos attack and very frequently timing out
2424
# sudo add-apt-repository --yes ppa:ansible/ansible
2525
# sudo apt-get update
26-
sudo apt-get install -y ansible
26+
apt-get install -y ansible
2727

2828
ansible-galaxy collection install community.general
2929
}
@@ -72,7 +72,14 @@ function cleanup_packages {
7272
# sudo add-apt-repository --yes --remove ppa:ansible/ansible
7373
}
7474

75+
function report_disk_usage {
76+
read -r dub _ < <(du -sx -B1 /)
77+
read -r duh _ < <(du -sx -h /)
78+
printf '::notice::disk_usage bytes=%s human=%s\n' "$dub" "$duh" | tee -a /tmp/ansible.log
79+
}
80+
7581
install_packages
7682
install_nix
7783
execute_stage2_playbook
7884
cleanup_packages
85+
report_disk_usage

nix/packages/build-ami.nix

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ writeShellApplication {
194194
-var "source_ami=$STAGE1_AMI_ID" \
195195
"$@"
196196
197+
disk_usage_notice=$(grep '^::notice::disk_usage ' /tmp/ansible-stage2.log | tail -n 1 || true)
198+
disk_usage_notice_pattern='^::notice::disk_usage bytes=([0-9]+) human=([0-9]+(\.[0-9]+)?[MGT]?)$'
199+
if [[ $disk_usage_notice =~ $disk_usage_notice_pattern ]]; then
200+
disk_usage_bytes=''${BASH_REMATCH[1]}
201+
disk_usage_human=''${BASH_REMATCH[2]}
202+
else
203+
echo "Error: Missing or invalid disk usage notice in stage 2 log: '$disk_usage_notice'" >&2
204+
exit 1
205+
fi
206+
echo "::notice::AMI Disk Usage $disk_usage_human $disk_usage_bytes"
207+
if [[ -n ''${GITHUB_OUTPUT:-} ]]; then
208+
disk_usage_json=$(jq -cnr --arg bytes "$disk_usage_bytes" --arg human "$disk_usage_human" '{$bytes,$human}')
209+
echo "disk_usage_json=$disk_usage_json" >>"$GITHUB_OUTPUT"
210+
fi
211+
197212
if [ -n "''${PACKER_EXECUTION_ID:-}" ]; then
198213
STAGE2_AMI_ID=$(aws ec2 describe-images \
199214
--region "$REGION" \

stage2-nix-psql.pkr.hcl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,10 @@ build {
132132
script = "ebssurrogate/scripts/nix-provision.sh"
133133
execute_command = "sudo -S sh -c '. {{.EnvVarFile}} && cd /tmp/ansible-playbook && {{.Path}}'"
134134
}
135+
136+
provisioner "file" {
137+
source = "/tmp/ansible.log"
138+
destination = "/tmp/ansible-stage2.log"
139+
direction = "download"
140+
}
135141
}

0 commit comments

Comments
 (0)