Skip to content

Commit 9f46ca0

Browse files
committed
fix: complete error logging migration and fix grep pattern bug
This commit completes the robustness fixes from commit 1cdc1a1 by addressing two critical issues identified in code review: 1. **Fix grep pattern bug in get_running_services_count()** - Changed from: `grep -c " Up"` - Changed to: `grep -cE " Up\b"` (word boundary) - FIXES: Previous pattern caused false positives on stopped containers with "Up" in command name (e.g., "UpgradeDB", "Uploader") - Now correctly matches only State column, not Command column - Prevents operational issues where backup/restore blocked incorrectly 2. **Complete error() function migration (5 critical errors)** - Updated deploy.sh (3 errors): * "Failed to pull images from GHCR" (line 194) * "Backup failed" (line 213) * "Services unhealthy after timeout" (line 249) - Updated rollback.sh (2 errors): * "Backup restoration failed" (line 172) * "Rollback health check failed" (line 213) - FIXES: Critical operational errors now visible in quiet mode - All deployment/rollback failures now properly logged 3. **Consistency improvements (7 helper functions)** - Updated to use error() instead of direct stderr: * acquire_lock() - flock requirement and lock conflict errors * require_jq() - dependency check * require_curl() - dependency check * validate_compose_file() - file existence check * determine_backup_name() - missing backup error * validate_and_check_backup() - removed duplicate error message * require_services_stopped() - services running error - Ensures consistent error handling and logging across all functions - All errors now include timestamps and log to LOG_FILE **Testing:** - Grep pattern tested against docker-compose output formats: * Simple "Up" (end of line) - ✅ matches * "Up (healthy)" - ✅ matches * "Up 5 seconds" - ✅ matches * "Up ports..." - ✅ matches * "UpgradeDB" in command - ✅ does NOT match (false positive fixed) - Error logging tested in quiet mode - all errors visible **Impact:** - Eliminates grep pattern false positive bug - Ensures critical errors never suppressed in quiet mode - Improves operational visibility and troubleshooting - No breaking changes to functionality These fixes complete the robustness improvements and make the code production-ready for merge.
1 parent 1cdc1a1 commit 9f46ca0

3 files changed

Lines changed: 21 additions & 20 deletions

File tree

‎scripts/deploy.sh‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ log "Using IMAGE_TAG: $IMAGE_TAG"
191191
# Pull prebuilt images from GHCR (while services are still running)
192192
log "Pulling prebuilt images from GHCR..."
193193
if ! dc_run pull 2>&1 | tee -a "$LOG_FILE"; then
194-
log "ERROR: Failed to pull images from GHCR"
194+
error "Failed to pull images from GHCR"
195195
log "This usually means:"
196196
log " - Images for tag '$IMAGE_TAG' don't exist in GHCR"
197197
log " - Network connectivity issues"
@@ -210,7 +210,7 @@ if [ "$SKIP_BACKUP" -eq 0 ]; then
210210
# Create backup (stops services, creates backup, but doesn't restart)
211211
log "Creating backup before upgrade..."
212212
BACKUP_NAME=$("$SCRIPT_DIR/backup.sh" --stop --yes --quiet --no-lock) || {
213-
log "ERROR: Backup failed"
213+
error "Backup failed"
214214
notify "Deployment failed: Backup error"
215215
# Try to restart services
216216
dc_run up -d
@@ -246,7 +246,7 @@ if wait_for_health "$HEALTH_CHECK_TIMEOUT_DEPLOY"; then
246246
notify "Deployment successful: $(format_version_short "$NEW_VERSION")"
247247
cleanup_old_logs "$DEPLOY_LOG_DIR" "deploy-*.log" 30
248248
else
249-
log "ERROR: Services unhealthy after timeout"
249+
error "Services unhealthy after timeout"
250250
log "Unhealthy services:"
251251
dc_run ps | tee -a "$LOG_FILE"
252252

‎scripts/docker-helper.sh‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ get_running_services_count() {
8484
else
8585
# v1.27.4 and earlier - parse text output
8686
# Count lines with "Up" status, excluding header
87-
# Match " Up" (with space before, no space after) to catch:
88-
# - "Up" (simple running)
87+
# Use word boundary (\b) to match " Up" followed by non-word char:
88+
# - "Up" (simple running, end of line)
8989
# - "Up (health: starting)" (with health check)
9090
# - "Up 5 seconds" (with uptime)
91-
dc_run ps 2>/dev/null | grep -c " Up" || echo "0"
91+
# - "Up 0.0.0.0:..." (with port mappings)
92+
# Prevents false positives like "UpgradeDB" in command column
93+
dc_run ps 2>/dev/null | grep -cE " Up\b" || echo "0"
9294
fi
9395
}
9496

@@ -213,7 +215,7 @@ acquire_lock() {
213215
local lock_name="${LOCK_NAME:-deployment}"
214216

215217
if ! command -v flock &> /dev/null; then
216-
echo "ERROR: flock is required but not installed." >&2
218+
error "flock is required but not installed."
217219
echo "Install with: apt-get install util-linux" >&2
218220
exit 1
219221
fi
@@ -222,7 +224,7 @@ acquire_lock() {
222224
exec 200>"$LOCK_FILE"
223225

224226
if ! flock -n 200; then
225-
echo "ERROR: Another $lock_name operation is in progress" >&2
227+
error "Another $lock_name operation is in progress"
226228
echo "If you're sure no other operation is running, remove: $LOCK_FILE" >&2
227229
exit 1
228230
fi
@@ -395,7 +397,7 @@ cleanup_old_logs() {
395397
# Usage: require_jq
396398
require_jq() {
397399
if ! command -v jq &> /dev/null; then
398-
echo "ERROR: jq is required but not installed" >&2
400+
error "jq is required but not installed"
399401
echo "Install with: apt-get install jq or brew install jq" >&2
400402
exit 1
401403
fi
@@ -405,7 +407,7 @@ require_jq() {
405407
# Usage: require_curl
406408
require_curl() {
407409
if ! command -v curl &> /dev/null; then
408-
echo "ERROR: curl is required but not installed" >&2
410+
error "curl is required but not installed"
409411
echo "Install with: apt-get install curl or brew install curl" >&2
410412
exit 1
411413
fi
@@ -415,7 +417,7 @@ require_curl() {
415417
# Usage: validate_compose_file
416418
validate_compose_file() {
417419
if [ ! -f "$PROJECT_DIR/docker-compose.yml" ]; then
418-
echo "ERROR: docker-compose.yml not found. Not in project root?" >&2
420+
error "docker-compose.yml not found. Not in project root?"
419421
exit 1
420422
fi
421423
}
@@ -540,10 +542,10 @@ determine_backup_name() {
540542
fi
541543

542544
if [ -z "$backup_name" ]; then
543-
echo "ERROR: No backup specified and no recent backup found" >&2
544-
echo ""
545-
echo "Please specify a backup name or use --list to see available backups."
545+
error "No backup specified and no recent backup found"
546546
echo ""
547+
echo "Please specify a backup name or use --list to see available backups." >&2
548+
echo "" >&2
547549
list_backups
548550
return 1
549551
fi
@@ -561,8 +563,7 @@ validate_and_check_backup() {
561563
fi
562564

563565
if ! validate_backup_name "$backup_name"; then
564-
echo "ERROR: Invalid backup name format: $backup_name" >&2
565-
echo "Backup names must contain only: letters, numbers, dash (-), underscore (_)" >&2
566+
# validate_backup_name already printed the error
566567
exit 1
567568
fi
568569
}
@@ -575,9 +576,9 @@ require_services_stopped() {
575576
running_count=$(get_running_services_count)
576577

577578
if [ "$running_count" -gt 0 ]; then
578-
echo "ERROR: Services are still running. Stop them first:" >&2
579+
error "Services are still running. Stop them first:"
579580
echo " $DOCKER_COMPOSE_CMD down" >&2
580-
echo ""
581+
echo "" >&2
581582
echo "Or use deploy.sh/rollback.sh which handle this automatically." >&2
582583
return 1
583584
fi

‎scripts/rollback.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ mark_services_stopped
169169
# Restore database backup using restore.sh (services already stopped, don't start after)
170170
log "Restoring databases from backup: $BACKUP_NAME"
171171
if ! "$SCRIPT_DIR/restore.sh" --yes --quiet --no-lock "$BACKUP_NAME"; then
172-
log "ERROR: Backup restoration failed"
172+
error "Backup restoration failed"
173173
log "Attempting to start services without database restoration..."
174174
notify "CRITICAL: Rollback backup restoration failed. Attempting service recovery..."
175175
# Try to start services anyway - better than leaving system completely down
@@ -210,7 +210,7 @@ if wait_for_health "$HEALTH_CHECK_TIMEOUT_ROLLBACK"; then
210210

211211
notify "Rollback completed successfully to $BACKUP_NAME (version: $(format_version_short "$PREV_VERSION"))"
212212
else
213-
log "ERROR: Rollback health check failed after ${HEALTH_CHECK_TIMEOUT_ROLLBACK}s"
213+
error "Rollback health check failed after ${HEALTH_CHECK_TIMEOUT_ROLLBACK}s"
214214
log "Services status:"
215215
dc_run ps | tee -a "$ROLLBACK_LOG"
216216

0 commit comments

Comments
 (0)