Skip to content

Sam/build ami local #1547

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 21 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ result*
.vscode/

db/schema.sql
common-nix.vars.pkr.hcl
2 changes: 1 addition & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,569 changes: 1,035 additions & 534 deletions flake.nix

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions nix/docs/development-workflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# PostgreSQL Development Workflow

This document outlines the workflow for developing and testing PostgreSQL in an ec2 instance using the tools provided in this repo.

## Prerequisites

- Nix installed and configured
- AWS credentials configured with aws-vault (you must set up aws-vault beforehand)
- GitHub access to the repository

## Workflow Steps

### 1. Trigger Remote Build and Cache

To build, test, and cache your changes in the Supabase Nix binary cache:

```bash
# From your branch
nix run .#trigger-nix-build
```

This will:
- Trigger a GitHub Actions workflow
- Build PostgreSQL and extensions
- Run nix flake check tests (evaluation of nix code, pg_regress and migrations tests)
- Cache the results in the Supabase Nix binary cache
- Watch the workflow progress until completion

The workflow will run on the branch you're currently on.

If you're on a feature different branch, you'll be prompted to confirm before proceeding.

### 2. Build AMI

After the build is complete and cached, build the AMI:

```bash
# Build AMI for PostgreSQL 15
aws-vault exec <profile-name> -- nix run .#build-test-ami 15

# Or for PostgreSQL 17
aws-vault exec <profile-name> -- nix run .#build-test-ami 17

# Or for PostgreSQL orioledb-17
aws-vault exec <profile-name> -- nix run .#build-test-ami orioledb-17
```

This will:
- Build two AMI stages using Packer
- Clean up temporary instances after AMI builds
- Output the final AMI name (e.g., `supabase-postgres-abc123`)

**Important**: Take note of the AMI name output at the end, as you'll need it for the next step.

### 3. Run Testinfra

Run the testinfra tests against the AMI:

```bash
# Run tests against the AMI
nix run .#run-testinfra -- --aws-vault-profile <profile-name> --ami-name supabase-postgres-abc123
```

This will:
- Create a Python virtual environment
- Install required Python packages
- Create an EC2 instance from the AMI
- Run the test suite
- Automatically terminate the EC2 instance when done

The script handles:
- Setting up AWS credentials via aws-vault
- Creating and managing the Python virtual environment
- Running the tests
- Cleaning up EC2 instances
- Proper error handling and cleanup on interruption

### 4. Optional: Cleanup AMI

If you want to clean up the AMI after testing:

```bash
# Clean up the AMI
aws-vault exec <profile-name> -- nix run .#cleanup-ami supabase-postgres-abc123
```

This will:
- Deregister the AMI
- Clean up any associated resources

## Troubleshooting

### Common Issues

1. **AWS Credentials**
- Ensure aws-vault is properly configured
- Use the `--aws-vault-profile` argument to specify your AWS profile
- Default profile is "staging" if not specified

2. **EC2 Instance Not Terminating**
- The script includes multiple safeguards for cleanup
- If instances aren't terminated, check AWS console and terminate manually

3. **Test Failures**
- Check the test output for specific failures
- Ensure you're using the correct AMI name
- Verify AWS region and permissions

### Environment Variables

The following environment variables are used:
- `AWS_VAULT`: AWS Vault profile name (default: staging)
- `AWS_REGION`: AWS region (default: ap-southeast-1)
- `AMI_NAME`: Name of the AMI to test

## Best Practices

1. **Branch Management**
- Use feature branches for development
- Merge to develop for testing
- Use release branches for version-specific changes

2. **Resource Cleanup**
- Always run the cleanup step after testing
- Monitor AWS console for any lingering resources
- Use the cleanup-ami command when done with an AMI

3. **Testing**
- Run tests locally before pushing changes
- Verify AMI builds before running testinfra
- Check test output for any warnings or errors

## Additional Commands

```bash
# Show available commands
nix run .#show-commands

# Update README with latest command information
nix run .#update-readme
```
5 changes: 4 additions & 1 deletion nix/ext/pg_jsonschema.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ buildPgrxExtension_0_12_6 rec {
env = lib.optionalAttrs stdenv.isDarwin {
POSTGRES_LIB = "${postgresql}/lib";
RUSTFLAGS = "-C link-arg=-undefined -C link-arg=dynamic_lookup";
PGPORT = "5433";
PGPORT = toString (5441 +
(if builtins.match ".*_.*" postgresql.version != null then 1 else 0) + # +1 for OrioleDB
((builtins.fromJSON (builtins.substring 0 2 postgresql.version)) - 15) * 2); # +2 for each major version

};

cargoLock = {
Expand Down
Loading