8000 Sam/build ami local by samrose · Pull Request #1547 · supabase/postgres · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: make sure ec2 instance terminates at end of run no matter how e…
…nded
  • Loading branch information
samrose committed May 1, 2025
commit b87c3fd010d7dfeeb1fe191e929420beaef4c98d
58 changes: 44 additions & 14 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -758,17 +758,33 @@

# Cleanup instances from AMI builds
cleanup_instances() {
echo "Terminating EC2 instances with tag testinfra-run-id=$RUN_ID..."
aws ec2 --region $REGION describe-instances \
--filters "Name=tag:packerExecutionId,Values=$RUN_ID" \
--filters "Name=tag:testinfra-run-id,Values=$RUN_ID" \
--query "Reservations[].Instances[].InstanceId" \
--output text | xargs -r aws ec2 terminate-instances \
--region $REGION --instance-ids || true
}

trap cleanup_instances EXIT
# Set up traps for various signals to ensure cleanup
trap cleanup_instances EXIT HUP INT QUIT TERM

# Create and activate virtual environment
VENV_DIR=$(mktemp -d)
trap 'rm -rf "$VENV_DIR"' EXIT HUP INT QUIT TERM
python3 -m venv "$VENV_DIR"
source "$VENV_DIR/bin/activate"

# Install required Python packages
echo "Installing required Python packages..."
pip install boto3 boto3-stubs[essential] docker ec2instanceconnectcli pytest paramiko requests

# Run the tests with aws-vault
echo "Running tests for AMI: $RANDOM_STRING using AWS Vault profile: supabase-dev"
aws-vault exec supabase-dev -- pytest -vv -s testinfra/test_ami_nix.py

# Print the AMI name for use with run-testinfra
echo "supabase-postgres-$RANDOM_STRING"
# Deactivate virtual environment (cleanup is handled by trap)
deactivate
EOL
chmod +x $out/bin/build-test-ami
'';
Expand Down Expand Up @@ -797,7 +813,7 @@
1. Check if aws-vault is installed and configured
2. Set up the required environment variables
3. Create and activate a virtual environment
4. Install required Python packages
4. Install required Python packages from pip
5. Run the tests with aws-vault credentials
6. Clean up the virtual environment

Expand Down Expand Up @@ -868,32 +884,46 @@
# Function to terminate EC2 instances
terminate_instances() {
echo "Terminating EC2 instances with tag testinfra-run-id=$RUN_ID..."
aws ec2 --region ap-southeast-1 describe-instances \
aws-vault exec supabase-dev -- aws ec2 --region ap-southeast-1 describe-instances \
--filters "Name=tag:testinfra-run-id,Values=$RUN_ID" \
--query "Reservations[].Instances[].InstanceId" \
--output text | xargs -r aws ec2 terminate-instances \
--output text | xargs -r aws-vault exec supabase-dev -- aws ec2 terminate-instances \
--region ap-southeast-1 --instance-ids || true
}

# Set up trap to terminate instances on script exit
trap terminate_instances EXIT
# Set up traps for various signals to ensure cleanup
trap terminate_instances EXIT HUP INT QUIT TERM

# Create and activate virtual environment
VENV_DIR=$(mktemp -d)
trap 'rm -rf "$VENV_DIR"' EXIT
trap 'rm -rf "$VENV_DIR"' EXIT HUP INT QUIT TERM
python3 -m venv "$VENV_DIR"
source "$VENV_DIR/bin/activate"

# Install required Python packages
echo "Installing required Python packages..."
pip install boto3 boto3-stubs[essential] docker ec2instanceconnectcli pytest paramiko requests

# Run the tests with aws-vault
echo "Running tests for AMI: $AMI_NAME using AWS Vault profile: $AWS_VAULT_PROFILE"
aws-vault exec "$AWS_VAULT_PROFILE" -- pytest -vv -s testinfra/test_ami_nix.py
# Function to run tests and ensure cleanup
run_tests() {
local exit_code=0
echo "Running tests for AMI: $AMI_NAME using AWS Vault profile: $AWS_VAULT_PROFILE"
aws-vault exec "$AWS_VAULT_PROFILE" -- pytest -vv -s testinfra/test_ami_nix.py || exit_code=$?
return $exit_code
}

# Deactivate virtual environment (cleanup is handled by trap)
# Run tests and capture exit code
run_tests
test_exit_code=$?

# Deactivate virtual environment
deactivate

# Explicitly call cleanup
terminate_instances

# Exit with the test exit code
exit $test_exit_code
EOL
chmod +x $out/bin/run-testinfra
'';
Expand Down
Loading
0