Skip to content

Sam/nix and conventional ami #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 45 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f5ce8af
feat: nix-ami-changes
samrose Jun 24, 2024
fc6f057
chore: version bump
samrose Jun 24, 2024
12ab014
chore: remap branch for ami build
samrose Jun 24, 2024
91ec53b
chore: bump version
samrose Jun 24, 2024
e014e22
chore: bump version to trigger build
samrose Jun 24, 2024
5c6d777
feat: use /var/lib/postgresql as home for postgres user
samrose Jun 24, 2024
e2fa329
fix: makre sure bashrc exists
samrose Jun 25, 2024
426954a
fix: minor refactor
samrose Jun 25, 2024
d872002
chore: moving to a different PR
samrose Jun 25, 2024
ea28a10
chore: bump version and remove deprecated workflow
samrose Jun 25, 2024
3d55e1a
feat: parallel testinfra-nix just for ami test
samrose Jun 25, 2024
f15580d
chore: testing just testinfra-nix workflow
samrose Jun 25, 2024
887d8da
chore: re-run build
samrose Jun 25, 2024
34fe052
chore: re-trigger testinfra
samrose Jun 25, 2024
7ed3651
fix: wait for AMI to reach available state
samrose Jun 25, 2024
463079b
fix: use ami id in stage 3 testinfra ami-test
samrose Jun 25, 2024
e43f0f2
fix: env vars
samrose Jun 25, 2024
9b38c4b
chore: bump version
samrose Jun 25, 2024
7237270
chore: restore packer build
samrose Jun 26, 2024
05a52f1
chore: create a parallel test
samrose Jun 26, 2024
d86253c
chore: bump version
samrose Jun 26, 2024
3f83665
fix: capture and use ami name
samrose Jun 26, 2024
c51bb54
fix: aws regions
samrose Jun 26, 2024
d57add2
chore: capture ami name
samrose Jun 26, 2024
a1fc00b
chore: force_deregister all ami prior to create new
samrose Jun 26, 2024
2b42a8b
fix: pass same ami name each time
samrose Jun 26, 2024
00b69b1
fix: manage concurrency of testinfra builds
samrose Jun 26, 2024
2c4f72f
fix: no args on stage 2
samrose Jun 26, 2024
c527c59
fix: re-intro original testinfra
samrose Jun 26, 2024
a4753dc
Revert "fix: re-intro original testinfra"
samrose Jun 26, 2024
7355a29
chore: push to re-trigger build
samrose Jun 26, 2024
20c0224
chore: update instance name
samrose Jun 26, 2024
2369eb1
fix: location of pg_isready binary
samrose Jul 1, 2024
61b86a6
fix: re-intro conventional ami infra test + more symlinks where expected
samrose Jul 1, 2024
1a36906
fix: dealing with symlink creation issues
samrose Jul 1, 2024
27f2aee
fix: try concurrency rules on on all large builds
samrose Jul 1, 2024
c3b5aa9
chore; try with no concurrency rules
samrose Jul 1, 2024
b9b4741
chore: rerun
samrose Jul 1, 2024
25a2fa2
chore: rebasing on develop
samrose Jul 12, 2024
02f1f4f
Sam/timescale and wrappers (#1052)
samrose Jul 18, 2024
12852b2
fix: locale gen and ami deregister on any testinfra run (#1055)
samrose Jul 18, 2024
16cd5c9
chore: update pg_upgrade initiate.sh to support nix-based upgrades (#…
pcnc Jul 19, 2024
2edcf2d
chore: package nix flake revision in pg_upgrade binaries tarball when…
pcnc Jul 19, 2024
0c05f2c
chore: activate release workflow
samrose Jul 19, 2024
055afd8
chore: bump version
samrose Jul 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions .github/workflows/ami-release-nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
name: Release AMI Nix

on:
push:
branches:
- develop
paths:
- '.github/workflows/ami-release-nix.yml'
- 'common-nix.vars.pkr.hcl'
workflow_dispatch:

jobs:
build:
strategy:
matrix:
include:
- runner: arm-runner
arch: arm64
ubuntu_release: focal
ubuntu_version: 20.04
mcpu: neoverse-n1
runs-on: ${{ matrix.runner }}
timeout-minutes: 150
permissions:
contents: write
packages: write
id-token: write

steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Run checks if triggered manually
if: ${{ github.event_name == 'workflow_dispatch' }}
# Update `ci.yaml` too if changing constraints.
run: |
SUFFIX=$(sed -E 's/postgres-version = "[0-9\.]+(.*)"/\1/g' common-nix.vars.pkr.hcl)
if [[ -z $SUFFIX ]] ; then
echo "Version must include non-numeric characters if built manually."
exit 1
fi

# extensions are build in nix prior to this step
# so we can just use the binaries from the nix store
# for postgres, extensions and wrappers

- name: Build AMI stage 1
run: |
packer init amazon-arm64-nix.pkr.hcl
GIT_SHA=${{github.sha}}
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

- name: Build AMI stage 2
run: |
packer init stage2-nix-psql.pkr.hcl
GIT_SHA=${{github.sha}}
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

- name: Grab release version
id: process_release_version
run: |
VERSION=$(sed -e 's/postgres-version = "\(.*\)"/\1/g' common-nix.vars.pkr.hcl)
echo "version=$VERSION" >> "$GITHUB_OUTPUT"

- name: Create nix flake revision tarball
run: |
GIT_SHA=${{github.sha}}
MAJOR_VERSION=$(echo "${{ steps.process_release_version.outputs.version }}" | cut -d. -f1)

mkdir -p "/tmp/pg_upgrade_bin/${MAJOR_VERSION}"
echo "$GIT_SHA" >> "/tmp/pg_upgrade_bin/${MAJOR_VERSION}/nix_flake_version"
tar -czf "/tmp/pg_binaries.tar.gz" -C "/tmp/pg_upgrade_bin" .

- name: configure aws credentials - staging
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.DEV_AWS_ROLE }}
aws-region: "us-east-1"

- name: Upload software manifest to s3 staging
run: |
cd ansible
ansible-playbook -i localhost \
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
-e "internal_artifacts_bucket=${{ secrets.ARTIFACTS_BUCKET }}" \
manifest-playbook.yml

- name: Upload nix flake revision to s3 staging
run: |
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

#Our self hosted github runner already has permissions to publish images
#but they're limited to only that;
#so if we want s3 access we'll need to config credentials with the below steps
# (which overwrites existing perms) after the ami build

- name: configure aws credentials - prod
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.PROD_AWS_ROLE }}
aws-region: "us-east-1"

- name: Upload software manifest to s3 prod
run: |
cd ansible
ansible-playbook -i localhost \
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
-e "internal_artifacts_bucket=${{ secrets.PROD_ARTIFACTS_BUCKET }}" \
manifest-playbook.yml

- name: Upload nix flake revision to s3 prod
run: |
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

- name: Create release
uses: softprops/action-gh-release@v1
with:
name: ${{ steps.process_release_version.outputs.version }}
tag_name: ${{ steps.process_release_version.outputs.version }}
target_commitish: ${{github.sha}}

- name: Slack Notification on Failure
if: ${{ failure() }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
SLACK_USERNAME: 'gha-failures-notifier'
SLACK_COLOR: 'danger'
SLACK_MESSAGE: 'Building Postgres AMI failed'
SLACK_FOOTER: ''

- name: Cleanup resources on build cancellation
if: ${{ cancelled() }}
run: |
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 {}
6 changes: 5 additions & 1 deletion .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ jobs:
steps:

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref || github.ref }}
fetch-depth: 0
fetch-tags: true
- name: aws-creds
uses: aws-actions/configure-aws-credentials@v4
with:
Expand Down
52 changes: 0 additions & 52 deletions .github/workflows/nix-cache-upload.yml

This file was deleted.

94 changes: 94 additions & 0 deletions .github/workflows/publish-nix-pgupgrade-scripts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Publish pg_upgrade_scripts

on:
push:
branches:
- develop
- sam/nix-and-conventional-ami
paths:
- '.github/workflows/publish-pgupgrade-scripts.yml'
- 'common-nix.vars.pkr.hcl'
workflow_dispatch:

permissions:
id-token: write

jobs:
publish-staging:
runs-on: ubuntu-latest

steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Grab release version
id: process_release_version
run: |
VERSION=$(grep 'postgres-version' common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
echo "version=$VERSION" >> "$GITHUB_OUTPUT"

- name: Create a tarball containing pg_upgrade scripts
run: |
mkdir -p /tmp/pg_upgrade_scripts
cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts
tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts

- name: configure aws credentials - staging
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.DEV_AWS_ROLE }}
aws-region: "us-east-1"

- name: Upload pg_upgrade scripts to s3 staging
run: |
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

- name: Slack Notification on Failure
if: ${{ failure() }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
SLACK_USERNAME: 'gha-failures-notifier'
SLACK_COLOR: 'danger'
SLACK_MESSAGE: 'Publishing pg_upgrade scripts failed'
SLACK_FOOTER: ''

publish-prod:
runs-on: ubuntu-latest
if: github.ref_name == 'develop'

steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Grab release version
id: process_release_version
run: |
VERSION=$(grep 'postgres-version' common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
echo "version=$VERSION" >> "$GITHUB_OUTPUT"

- name: Create a tarball containing pg_upgrade scripts
run: |
mkdir -p /tmp/pg_upgrade_scripts
cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts
tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts

- name: configure aws credentials - prod
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.PROD_AWS_ROLE }}
aws-region: "us-east-1"

- name: Upload pg_upgrade scripts to s3 prod
run: |
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

- name: Slack Notification on Failure
if: ${{ failure() }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
SLACK_USERNAME: 'gha-failures-notifier'
SLACK_COLOR: 'danger'
SLACK_MESSAGE: 'Publishing pg_upgrade scripts failed'
SLACK_FOOTER: ''
88 changes: 88 additions & 0 deletions .github/workflows/testinfra-nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Testinfra Integration Tests Nix

on:
pull_request:
workflow_dispatch:

jobs:
test-ami-nix:
strategy:
fail-fast: false
matrix:
include:
- runner: arm-runner
arch: arm64
ubuntu_release: focal
ubuntu_version: 20.04
mcpu: neoverse-n1
runs-on: ${{ matrix.runner }}
timeout-minutes: 150
permissions:
contents: write
packages: write
id-token: write

steps:
- name: Checkout Repo
uses: actions/checkout@v4

- id: args
uses: mikefarah/yq@master
with:
cmd: yq 'to_entries | map(select(.value|type == "!!str")) | map(.key + "=" + .value) | join("\n")' 'ansible/vars.yml'

- run: docker context create builders

- uses: docker/setup-buildx-action@v3
with:
endpoint: builders

- name: Build AMI stage 1
run: |
packer init amazon-arm64-nix.pkr.hcl
GIT_SHA=${{github.sha}}
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

- name: Build AMI stage 2
run: |
packer init stage2-nix-psql.pkr.hcl
GIT_SHA=${{github.sha}}
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

- name: Run tests
timeout-minutes: 10
run: |
# TODO: use poetry for pkg mgmt
pip3 install boto3 boto3-stubs[essential] docker ec2instanceconnectcli pytest pytest-testinfra[paramiko,docker] requests
pytest -vv -s testinfra/test_ami_nix.py

- name: Cleanup resources on build cancellation
if: ${{ cancelled() }}
run: |
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 {}

- name: Cleanup resources on build cancellation
if: ${{ always() }}
run: |
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

- name: Cleanup AMIs
if: always()
run: |
# Define AMI name patterns
STAGE1_AMI_NAME="supabase-postgres-ci-ami-test-stage-1"
STAGE2_AMI_NAME="supabase-postgres-ci-ami-test-nix"

# Function to deregister AMIs by name pattern
deregister_ami_by_name() {
local ami_name_pattern=$1
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)
for ami_id in $ami_ids; do
echo "Deregistering AMI: $ami_id"
aws ec2 deregister-image --region ap-southeast-1 --image-id $ami_id
done
}

# Deregister AMIs
deregister_ami_by_name "$STAGE1_AMI_NAME"
deregister_ami_by_name "$STAGE2_AMI_NAME"
Loading
Loading