From 1758454f0cc8f76cb0861c90f97bddbfa4296b92 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 14 Mar 2025 19:20:55 +0000 Subject: [PATCH] fix: wipe PostgreSQL data to ensure unique deployment IDs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The GCP Marketplace VM images were all showing the same deployment ID (68aaf8cf-de7f-452f-8f23-32a9ba7fa864) because Coder was started during the image build process, which generated and saved a deployment ID in the PostgreSQL database. This change: 1. Stops the Coder service in the cleanup script 2. Wipes the PostgreSQL data directory 3. Reinitializes the database structure 4. Ensures PostgreSQL is stopped before image creation Fixes #180 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- files/scripts/999-cleanup.sh | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/files/scripts/999-cleanup.sh b/files/scripts/999-cleanup.sh index 7261b82..5018812 100755 --- a/files/scripts/999-cleanup.sh +++ b/files/scripts/999-cleanup.sh @@ -30,6 +30,68 @@ rm -f /home/ubuntu/.ssh/authorized_keys chown root:root / chmod o-w / +# Stop Coder service to ensure clean shutdown before wiping data +systemctl stop coder + +# Clean PostgreSQL data directory to remove deployment ID +# This ensures each marketplace installation gets a unique deployment ID +# Fix for: https://github.com/coder/packages/issues/180 +# Problem: All marketplace images had the same deployment ID because Coder was started +# during image build, which generated and saved a deployment ID in the PostgreSQL database +PG_VERSION=15 # Version set in 013-postgresql.sh +PG_DATA_DIR="/var/lib/postgresql/${PG_VERSION}/main" +if [ -d "$PG_DATA_DIR" ]; then + echo "Stopping PostgreSQL service..." + systemctl stop postgresql + + echo "Wiping PostgreSQL data directory to remove Coder deployment ID..." + # Backup pg_hba.conf and postgresql.conf + cp "$PG_DATA_DIR/pg_hba.conf" "/tmp/pg_hba.conf.bak" 2>/dev/null + cp "$PG_DATA_DIR/postgresql.conf" "/tmp/postgresql.conf.bak" 2>/dev/null + + # Remove data directory + rm -rf "$PG_DATA_DIR" + + # Recreate data directory + mkdir -p "$PG_DATA_DIR" + chown -R postgres:postgres "$PG_DATA_DIR" + chmod 700 "$PG_DATA_DIR" + + # Initialize PostgreSQL database + echo "Initializing fresh PostgreSQL database..." + sudo -u postgres /usr/lib/postgresql/$PG_VERSION/bin/initdb -D "$PG_DATA_DIR" + + # Restore configuration if it existed + if [ -f "/tmp/pg_hba.conf.bak" ]; then + cp "/tmp/pg_hba.conf.bak" "$PG_DATA_DIR/pg_hba.conf" + chown postgres:postgres "$PG_DATA_DIR/pg_hba.conf" + fi + + if [ -f "/tmp/postgresql.conf.bak" ]; then + cp "/tmp/postgresql.conf.bak" "$PG_DATA_DIR/postgresql.conf" + chown postgres:postgres "$PG_DATA_DIR/postgresql.conf" + fi + + # Start PostgreSQL temporarily to recreate database structure + systemctl start postgresql + + # Wait for PostgreSQL to start + sleep 5 + + # Recreate database and user (same as in 013-postgresql.sh) + echo "Recreating database structure..." + sudo -u postgres psql <