Skip to content

Commit

Permalink
feat: improve update script
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Hacene committed Nov 26, 2024
1 parent 5989866 commit b922d9a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
backend:
container_name: backend
image: ghcr.io/intuitem/ciso-assistant-community/backend:latest
image: ghcr.io/intuitem/ciso-assistant-community/backend:${VERSION:-latest}
restart: always
environment:
- ALLOWED_HOSTS=backend,localhost
Expand All @@ -19,7 +19,7 @@ services:
- PROTOCOL_HEADER=x-forwarded-proto
- HOST_HEADER=x-forwarded-host

image: ghcr.io/intuitem/ciso-assistant-community/frontend:latest
image: ghcr.io/intuitem/ciso-assistant-community/frontend:${VERSION:-latest}
depends_on:
- backend

Expand Down
85 changes: 73 additions & 12 deletions update-ciso-assistant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,84 @@

# Variables
DB_FILE="db/ciso-assistant.sqlite3"
BACKUP_FILE="../ciso-assistant-backup.sqlite3"
BACKEND_IMAGE="ghcr.io/intuitem/ciso-assistant-community/backend:latest"
FRONTEND_IMAGE="ghcr.io/intuitem/ciso-assistant-community/frontend:latest"
BACKUP_DIR="../ciso-assistant-backups"
VERSION=${1:-latest}
CURRENT_VERSION=$(docker ps -a --format "{{.Image}}" | grep "ghcr.io/intuitem/ciso-assistant-community/backend" | head -n 1 | cut -d ':' -f2 || echo "latest")
BACKUP_FILE="$BACKUP_DIR/ciso-assistant-backup-$CURRENT_VERSION.sqlite3"
RESTORE_BACKUP_FILE="$BACKUP_DIR/ciso-assistant-backup-$VERSION.sqlite3"
BACKEND_IMAGE="ghcr.io/intuitem/ciso-assistant-community/backend:$VERSION"
FRONTEND_IMAGE="ghcr.io/intuitem/ciso-assistant-community/frontend:$VERSION"

# Backup database
[ -f "$DB_FILE" ] && cp "$DB_FILE" "$BACKUP_FILE" || { echo "Database not found. Skipping."; exit 1; }
echo "Using backend image: $BACKEND_IMAGE"
echo "Using frontend image: $FRONTEND_IMAGE"

# Update Docker images
# Create backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
echo "Creating backup directory at $BACKUP_DIR..."
mkdir -p "$BACKUP_DIR" || { echo "Error: Failed to create backup directory."; exit 1; }
echo "Backup directory created."
fi

# Backup the current database (overwrite if exists)
if [ -f "$DB_FILE" ]; then
echo "Backing up the database for current version '$CURRENT_VERSION' (overwriting existing backup)..."
cp "$DB_FILE" "$BACKUP_FILE" || { echo "Error: Database backup failed."; exit 1; }
echo "Database backed up to $BACKUP_FILE."
else
echo "Database file not found. Skipping backup."
fi

# Stop and remove containers (after backup)
docker compose down
docker images | grep -E "ghcr.io/intuitem/ciso-assistant-community/(backend|frontend).*latest" | awk '{print $3}' | xargs -r docker rmi -f
docker pull "$BACKEND_IMAGE" "$FRONTEND_IMAGE"

# Start containers
docker compose up -d
# Remove all images
docker images | grep -E "ghcr.io/intuitem/ciso-assistant-community/(backend|frontend)" | awk '{print $3}' | xargs -r docker rmi -f

# Choice to restore the database from the selected version's backup if it exists
if [ -f "$RESTORE_BACKUP_FILE" ]; then
read -p "Backup found for version '$VERSION'. Do you want to restore the database from this backup? (y/n): " restore_choice
if [[ "$restore_choice" =~ ^[Yy]$ ]]; then
echo "Restoring database from backup for version '$VERSION'..."
cp "$RESTORE_BACKUP_FILE" "$DB_FILE" || { echo "Error: Database restoration failed."; exit 1; }
echo "Database restored from $RESTORE_BACKUP_FILE."
else
echo "Skipping database restoration."
if [ "$(echo -e "$VERSION\n$CURRENT_VERSION" | sort -V | head -n 1)" == "$CURRENT_VERSION" ] && ["$VERSION" != "$CURRENT_VERSION"]; then
NEW_DB=true
fi
fi
else
echo "No backup found for version '$VERSION'. Proceeding with a new database."
NEW_DB=true
fi

# If using a version other than 'latest', remove the database to avoid migration conflicts (only if NEW_DB is not true)
if [ "$NEW_DB" = true ]; then
echo "Removing database file to create a fresh one for version '$VERSION'..."
rm -f "$DB_FILE" || { echo "Error: Failed to remove database file."; exit 1; }
echo "Database file removed."
fi

# Pull and run the specified version
docker pull "$BACKEND_IMAGE" "$FRONTEND_IMAGE"
VERSION=$VERSION docker compose up -d

# Run Django migrations
BACKEND_CONTAINER=$(docker ps --filter "ancestor=$BACKEND_IMAGE" --format "{{.ID}}")
[ -n "$BACKEND_CONTAINER" ] && docker exec "$BACKEND_CONTAINER" poetry run python manage.py migrate || { echo "Migration failed."; exit 1; }
if [ -n "$BACKEND_CONTAINER" ]; then
echo "Running Django migrations..."
docker exec "$BACKEND_CONTAINER" poetry run python manage.py migrate || docker exec "$BACKEND_CONTAINER" python manage.py migrate || { echo "Error: Migration failed."; exit 1; }
echo "Migrations completed successfully."

# Create superuser interactively if the database is new and the version is older
if [ "$NEW_DB" = true ]; then
echo "Initializing your superuser account..."
docker compose exec backend poetry run python manage.py createsuperuser || docker exec -it "$BACKEND_CONTAINER" python manage.py createsuperuser || { echo "Error: Failed to create superuser."; exit 1; }
echo "Superuser created successfully."
fi
else
echo "Error: Backend container not found. Cannot run migrations or create superuser."
exit 1
fi

echo "Update and migration completed successfully."
echo "Update and migration for version '$VERSION' completed successfully."

0 comments on commit b922d9a

Please sign in to comment.