Skip to content

Commit bad563a

Browse files
samrosepcnc
andauthored
Sam/nix and conventional ami (supabase#1012)
* feat: nix-ami-changes * chore: version bump * chore: remap branch for ami build * chore: bump version * chore: bump version to trigger build * feat: use /var/lib/postgresql as home for postgres user * fix: makre sure bashrc exists * fix: minor refactor * chore: moving to a different PR * chore: bump version and remove deprecated workflow * feat: parallel testinfra-nix just for ami test * chore: testing just testinfra-nix workflow * chore: re-run build * chore: re-trigger testinfra * fix: wait for AMI to reach available state * fix: use ami id in stage 3 testinfra ami-test * fix: env vars * chore: bump version * chore: restore packer build * chore: create a parallel test * chore: bump version * fix: capture and use ami name * fix: aws regions * chore: capture ami name * chore: force_deregister all ami prior to create new * fix: pass same ami name each time * fix: manage concurrency of testinfra builds * fix: no args on stage 2 * fix: re-intro original testinfra * Revert "fix: re-intro original testinfra" This reverts commit f719e66. * chore: push to re-trigger build * chore: update instance name * fix: location of pg_isready binary * fix: re-intro conventional ami infra test + more symlinks where expected * fix: dealing with symlink creation issues * fix: try concurrency rules on on all large builds * chore; try with no concurrency rules * chore: rerun * chore: rebasing on develop Sam/nix and conventional consolidate (supabase#1025) * feat: consolidate ansible and use vars to toggle AMI builds * fix: resolving merge conflict * chore: merge conflict * Revert "chore: merge conflict" This reverts commit ddc6b1d. * fix: update ansible location for script * fix: ansible consolidated location * fix: set up modes on system-setup * fix: set vars * fix: python True and False in extra_vars * fix: adj vars * fix: set all ami vars * fix: args as json * fix: nixpkg_mode * fix: refining mode rules * fix: consolidate create dirs * fix: cleaning up modes * fix: systemd psql service reload targets * fix: starting postgres issues * fix: timing for pgsodium_getkey script * fix: packer file upload on stage 2 * fix: consolidation of ansible location * fix: stage2 fix hostname * fix: limit stage that tasks run on * fix: setting hosts only on stage 2 nix ami * fix: rewrite hosts in ansible to allow for re-use of playbook file * chore: trigger checks * fix: pgsodium getkey is different for deb vs nix builds * fix: consolidated files location * fix: on stage2 postgres server is already started at this point * fix: without env vars * fix: vars on the right mode * fix: dedupe * fix: locales * fix: locales * chore: try step with no env vars * fix: no need to start pg at this point stage2 * fix: yaml * fix: more cleanup of modes * fix: snapd already absent at this point + consolidate tasks * fix: already absent at this point * fix: service not present at this stage * fix: disable different services for first boot depending on mode * fix: pg already restarted at this point in stage 2 * fix: no start on stage2 * fix: try to start in stage2 * chore: include env vars for stage2 * fix: stop before starting * fix: debpkg mode only * fix: should use conventional path * fix: need to locale-gen prior to initdb * fix: nix build needs .env * fix: stage2 treatment of pgsodium_getket * chore: re-introduce permission checks via osquery * fix: correct the path to files --------- Co-authored-by: Sam Rose <samuel@supabase.io> * Sam/timescale and wrappers (supabase#1052) * fix: was using the wrong sha256 hash for version * chore: updating wrappers version * itests: make sure we run the current commit on psql bundle test --------- Co-authored-by: Sam Rose <samuel@supabase.io> * fix: locale gen and ami deregister on any testinfra run (supabase#1055) * fix: locale gen and ami deregister on any testinfra run * fix: use more manual approach --------- Co-authored-by: Sam Rose <samuel@supabase.io> * chore: update pg_upgrade initiate.sh to support nix-based upgrades (supabase#1057) * chore: package nix flake revision in pg_upgrade binaries tarball when building the nix AMI (supabase#1058) * chore: activate release workflow * chore: bump version --------- Co-authored-by: Sam Rose <samuel@supabase.io> Co-authored-by: Paul Cioanca <paul.cioanca@supabase.io>
1 parent 77100e2 commit bad563a

35 files changed

+2558
-146
lines changed

.github/workflows/ami-release-nix.yml

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
name: Release AMI Nix
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
paths:
8+
- '.github/workflows/ami-release-nix.yml'
9+
- 'common-nix.vars.pkr.hcl'
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
strategy:
15+
matrix:
16+
include:
17+
- runner: arm-runner
18+
arch: arm64
19+
ubuntu_release: focal
20+
ubuntu_version: 20.04
21+
mcpu: neoverse-n1
22+
runs-on: ${{ matrix.runner }}
23+
timeout-minutes: 150
24+
permissions:
25+
contents: write
26+
packages: write
27+
id-token: write
28+
29+
steps:
30+
- name: Checkout Repo
31+
uses: actions/checkout@v3
32+
33+
- name: Run checks if triggered manually
34+
if: ${{ github.event_name == 'workflow_dispatch' }}
35+
# Update `ci.yaml` too if changing constraints.
36+
run: |
37+
SUFFIX=$(sed -E 's/postgres-version = "[0-9\.]+(.*)"/\1/g' common-nix.vars.pkr.hcl)
38+
if [[ -z $SUFFIX ]] ; then
39+
echo "Version must include non-numeric characters if built manually."
40+
exit 1
41+
fi
42+
43+
# extensions are build in nix prior to this step
44+
# so we can just use the binaries from the nix store
45+
# for postgres, extensions and wrappers
46+
47+
- name: Build AMI stage 1
48+
run: |
49+
packer init amazon-arm64-nix.pkr.hcl
50+
GIT_SHA=${{github.sha}}
51+
packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" -var "ansible_arguments=" amazon-arm64-nix.pkr.hcl
52+
53+
- name: Build AMI stage 2
54+
run: |
55+
packer init stage2-nix-psql.pkr.hcl
56+
GIT_SHA=${{github.sha}}
57+
packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" stage2-nix-psql.pkr.hcl
58+
59+
- name: Grab release version
60+
id: process_release_version
61+
run: |
62+
VERSION=$(sed -e 's/postgres-version = "\(.*\)"/\1/g' common-nix.vars.pkr.hcl)
63+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
64+
65+
- name: Create nix flake revision tarball
66+
run: |
67+
GIT_SHA=${{github.sha}}
68+
MAJOR_VERSION=$(echo "${{ steps.process_release_version.outputs.version }}" | cut -d. -f1)
69+
70+
mkdir -p "/tmp/pg_upgrade_bin/${MAJOR_VERSION}"
71+
echo "$GIT_SHA" >> "/tmp/pg_upgrade_bin/${MAJOR_VERSION}/nix_flake_version"
72+
tar -czf "/tmp/pg_binaries.tar.gz" -C "/tmp/pg_upgrade_bin" .
73+
74+
- name: configure aws credentials - staging
75+
uses: aws-actions/configure-aws-credentials@v4
76+
with:
77+
role-to-assume: ${{ secrets.DEV_AWS_ROLE }}
78+
aws-region: "us-east-1"
79+
80+
- name: Upload software manifest to s3 staging
81+
run: |
82+
cd ansible
83+
ansible-playbook -i localhost \
84+
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
85+
-e "internal_artifacts_bucket=${{ secrets.ARTIFACTS_BUCKET }}" \
86+
manifest-playbook.yml
87+
88+
- name: Upload nix flake revision to s3 staging
89+
run: |
90+
aws s3 cp /tmp/pg_binaries.tar.gz s3://${{ secrets.ARTIFACTS_BUCKET }}/upgrades/postgres/supabase-postgres-${{ steps.process_release_version.outputs.version }}/20.04.tar.gz
91+
92+
#Our self hosted github runner already has permissions to publish images
93+
#but they're limited to only that;
94+
#so if we want s3 access we'll need to config credentials with the below steps
95+
# (which overwrites existing perms) after the ami build
96+
97+
- name: configure aws credentials - prod
98+
uses: aws-actions/configure-aws-credentials@v4
99+
with:
100+
role-to-assume: ${{ secrets.PROD_AWS_ROLE }}
101+
aws-region: "us-east-1"
102+
103+
- name: Upload software manifest to s3 prod
104+
run: |
105+
cd ansible
106+
ansible-playbook -i localhost \
107+
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
108+
-e "internal_artifacts_bucket=${{ secrets.PROD_ARTIFACTS_BUCKET }}" \
109+
manifest-playbook.yml
110+
111+
- name: Upload nix flake revision to s3 prod
112+
run: |
113+
aws s3 cp /tmp/pg_binaries.tar.gz s3://${{ secrets.PROD_ARTIFACTS_BUCKET }}/upgrades/postgres/supabase-postgres-${{ steps.process_release_version.outputs.version }}/20.04.tar.gz
114+
115+
- name: Create release
116+
uses: softprops/action-gh-release@v1
117+
with:
118+
name: ${{ steps.process_release_version.outputs.version }}
119+
tag_name: ${{ steps.process_release_version.outputs.version }}
120+
target_commitish: ${{github.sha}}
121+
122+
- name: Slack Notification on Failure
123+
if: ${{ failure() }}
124+
uses: rtCamp/action-slack-notify@v2
125+
env:
126+
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
127+
SLACK_USERNAME: 'gha-failures-notifier'
128+
SLACK_COLOR: 'danger'
129+
SLACK_MESSAGE: 'Building Postgres AMI failed'
130+
SLACK_FOOTER: ''
131+
132+
- name: Cleanup resources on build cancellation
133+
if: ${{ cancelled() }}
134+
run: |
135+
aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -n 1 -I {} aws ec2 terminate-instances --instance-ids {}

.github/workflows/nix-build.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ jobs:
2525
steps:
2626

2727
- name: Check out code
28-
uses: actions/checkout@v3
28+
uses: actions/checkout@v4
29+
with:
30+
ref: ${{ github.event.pull_request.head.ref || github.ref }}
31+
fetch-depth: 0
32+
fetch-tags: true
2933
- name: aws-creds
3034
uses: aws-actions/configure-aws-credentials@v4
3135
with:

.github/workflows/nix-cache-upload.yml

-52
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: Publish pg_upgrade_scripts
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
- sam/nix-and-conventional-ami
8+
paths:
9+
- '.github/workflows/publish-pgupgrade-scripts.yml'
10+
- 'common-nix.vars.pkr.hcl'
11+
workflow_dispatch:
12+
13+
permissions:
14+
id-token: write
15+
16+
jobs:
17+
publish-staging:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout Repo
22+
uses: actions/checkout@v3
23+
24+
- name: Grab release version
25+
id: process_release_version
26+
run: |
27+
VERSION=$(grep 'postgres-version' common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
28+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
29+
30+
- name: Create a tarball containing pg_upgrade scripts
31+
run: |
32+
mkdir -p /tmp/pg_upgrade_scripts
33+
cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts
34+
tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts
35+
36+
- name: configure aws credentials - staging
37+
uses: aws-actions/configure-aws-credentials@v1
38+
with:
39+
role-to-assume: ${{ secrets.DEV_AWS_ROLE }}
40+
aws-region: "us-east-1"
41+
42+
- name: Upload pg_upgrade scripts to s3 staging
43+
run: |
44+
aws s3 cp /tmp/pg_upgrade_scripts.tar.gz s3://${{ secrets.ARTIFACTS_BUCKET }}/upgrades/postgres/supabase-postgres-${{ steps.process_release_version.outputs.version }}/pg_upgrade_scripts.tar.gz
45+
46+
- name: Slack Notification on Failure
47+
if: ${{ failure() }}
48+
uses: rtCamp/action-slack-notify@v2
49+
env:
50+
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
51+
SLACK_USERNAME: 'gha-failures-notifier'
52+
SLACK_COLOR: 'danger'
53+
SLACK_MESSAGE: 'Publishing pg_upgrade scripts failed'
54+
SLACK_FOOTER: ''
55+
56+
publish-prod:
57+
runs-on: ubuntu-latest
58+
if: github.ref_name == 'develop'
59+
60+
steps:
61+
- name: Checkout Repo
62+
uses: actions/checkout@v3
63+
64+
- name: Grab release version
65+
id: process_release_version
66+
run: |
67+
VERSION=$(grep 'postgres-version' common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
68+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
69+
70+
- name: Create a tarball containing pg_upgrade scripts
71+
run: |
72+
mkdir -p /tmp/pg_upgrade_scripts
73+
cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts
74+
tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts
75+
76+
- name: configure aws credentials - prod
77+
uses: aws-actions/configure-aws-credentials@v1
78+
with:
79+
role-to-assume: ${{ secrets.PROD_AWS_ROLE }}
80+
aws-region: "us-east-1"
81+
82+
- name: Upload pg_upgrade scripts to s3 prod
83+
run: |
84+
aws s3 cp /tmp/pg_upgrade_scripts.tar.gz s3://${{ secrets.PROD_ARTIFACTS_BUCKET }}/upgrades/postgres/supabase-postgres-${{ steps.process_release_version.outputs.version }}/pg_upgrade_scripts.tar.gz
85+
86+
- name: Slack Notification on Failure
87+
if: ${{ failure() }}
88+
uses: rtCamp/action-slack-notify@v2
89+
env:
90+
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
91+
SLACK_USERNAME: 'gha-failures-notifier'
92+
SLACK_COLOR: 'danger'
93+
SLACK_MESSAGE: 'Publishing pg_upgrade scripts failed'
94+
SLACK_FOOTER: ''

.github/workflows/testinfra-nix.yml

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: Testinfra Integration Tests Nix
2+
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
7+
jobs:
8+
test-ami-nix:
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
include:
13+
- runner: arm-runner
14+
arch: arm64
15+
ubuntu_release: focal
16+
ubuntu_version: 20.04
17+
mcpu: neoverse-n1
18+
runs-on: ${{ matrix.runner }}
19+
timeout-minutes: 150
20+
permissions:
21+
contents: write
22+
packages: write
23+
id-token: write
24+
25+
steps:
26+
- name: Checkout Repo
27+
uses: actions/checkout@v4
28+
29+
- id: args
30+
uses: mikefarah/yq@master
31+
with:
32+
cmd: yq 'to_entries | map(select(.value|type == "!!str")) | map(.key + "=" + .value) | join("\n")' 'ansible/vars.yml'
33+
34+
- run: docker context create builders
35+
36+
- uses: docker/setup-buildx-action@v3
37+
with:
38+
endpoint: builders
39+
40+
- name: Build AMI stage 1
41+
run: |
42+
packer init amazon-arm64-nix.pkr.hcl
43+
GIT_SHA=${{github.sha}}
44+
packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" -var "ansible_arguments=" -var "postgres-version=ci-ami-test" -var "region=ap-southeast-1" -var 'ami_regions=["ap-southeast-1"]' -var "force-deregister=true" amazon-arm64-nix.pkr.hcl
45+
46+
- name: Build AMI stage 2
47+
run: |
48+
packer init stage2-nix-psql.pkr.hcl
49+
GIT_SHA=${{github.sha}}
50+
packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" -var "postgres-version=ci-ami-test" -var "region=ap-southeast-1" -var 'ami_regions=["ap-southeast-1"]' -var "force-deregister=true" -var "git_sha=${GITHUB_SHA}" stage2-nix-psql.pkr.hcl
51+
52+
- name: Run tests
53+
timeout-minutes: 10
54+
run: |
55+
# TODO: use poetry for pkg mgmt
56+
pip3 install boto3 boto3-stubs[essential] docker ec2instanceconnectcli pytest pytest-testinfra[paramiko,docker] requests
57+
pytest -vv -s testinfra/test_ami_nix.py
58+
59+
- name: Cleanup resources on build cancellation
60+
if: ${{ cancelled() }}
61+
run: |
62+
aws ec2 --region ap-southeast-1 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -n 1 -I {} aws ec2 terminate-instances --region ap-southeast-1 --instance-ids {}
63+
64+
- name: Cleanup resources on build cancellation
65+
if: ${{ always() }}
66+
run: |
67+
aws ec2 --region ap-southeast-1 describe-instances --filters "Name=tag:testinfra-run-id,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -n 1 -I {} aws ec2 terminate-instances --region ap-southeast-1 --instance-ids {} || true
68+
69+
- name: Cleanup AMIs
70+
if: always()
71+
run: |
72+
# Define AMI name patterns
73+
STAGE1_AMI_NAME="supabase-postgres-ci-ami-test-stage-1"
74+
STAGE2_AMI_NAME="supabase-postgres-ci-ami-test-nix"
75+
76+
# Function to deregister AMIs by name pattern
77+
deregister_ami_by_name() {
78+
local ami_name_pattern=$1
79+
local ami_ids=$(aws ec2 describe-images --region ap-southeast-1 --owners self --filters "Name=name,Values=${ami_name_pattern}" --query 'Images[*].ImageId' --output text)
80+
for ami_id in $ami_ids; do
81+
echo "Deregistering AMI: $ami_id"
82+
aws ec2 deregister-image --region ap-southeast-1 --image-id $ami_id
83+
done
84+
}
85+
86+
# Deregister AMIs
87+
deregister_ami_by_name "$STAGE1_AMI_NAME"
88+
deregister_ami_by_name "$STAGE2_AMI_NAME"

0 commit comments

Comments
 (0)