fix(ha/cluster-init): fix /proc/drbd sync progress parsing and TTY display
Check NixOS configurations / eval-hosts (push) Successful in 10m29s

The sync progress grep was silently failing because /proc/drbd formats the
percentage line as "sync'ed:  5.2%" (two spaces after colon), but the
pattern matched only one.  Use [[:space:]]+ throughout to tolerate any
whitespace.

Also make the display TTY-aware: interactive sessions get the spinning
\r in-place update; piped/logged sessions get a plain log line printed
once per unique percentage point (avoiding scroll spam).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-29 00:51:35 +10:00
co-authored by Claude Sonnet 4.6
parent e085d4707c
commit 6d16eaff89
+27 -10
View File
@@ -240,25 +240,42 @@ log "Forcing $NODE1 to DRBD Primary for initial sync..."
drbdadm primary ha-data --force drbdadm primary ha-data --force
log "Waiting for DRBD initial sync to complete (32 GB may take 1020 min)..." log "Waiting for DRBD initial sync to complete (32 GB may take 1020 min)..."
_sync_spin=0 log " (monitor with: watch -n3 cat /proc/drbd)"
_sync_chars=('|' '/' '-' $'\\') _sync_chars=('|' '/' '-' $'\\')
_sync_last_pct=""
_sync_iter=0
_is_tty=false; [[ -t 1 ]] && _is_tty=true
while true; do while true; do
_dstate=$(drbdadm dstate ha-data 2>/dev/null || echo "unknown") _dstate=$(drbdadm dstate ha-data 2>/dev/null || echo "unknown")
if echo "$_dstate" | grep -q "UpToDate/UpToDate"; then if echo "$_dstate" | grep -q "UpToDate/UpToDate"; then
printf "\r%-80s\r" "" # clear progress line $_is_tty && printf "\r%-80s\r" ""
log "DRBD initial sync complete (dstate: $_dstate)" log "DRBD initial sync complete (dstate: $_dstate)"
break break
fi fi
_pct=$(grep -oE "sync'ed: [0-9.]+%" /proc/drbd 2>/dev/null | grep -oE "[0-9.]+" | head -1 || echo "") # /proc/drbd uses variable whitespace: "sync'ed: 5.2%" (two spaces).
_eta=$(grep -oE "finish: [0-9:]+" /proc/drbd 2>/dev/null | sed 's/finish: //' | head -1 || echo "") # Use [[:space:]]+ to tolerate any amount of whitespace after the colon.
_spd=$(grep -oE "speed: [0-9,]+" /proc/drbd 2>/dev/null | sed 's/speed: //' | head -1 || echo "") _pct=$(grep -oE "sync'ed:[[:space:]]+[0-9.]+" /proc/drbd 2>/dev/null | grep -oE "[0-9.]+" | head -1 || echo "")
_sc="${_sync_chars[$_sync_spin]}" _eta=$(grep -oE "finish:[[:space:]]+[0-9:]+" /proc/drbd 2>/dev/null | grep -oE "[0-9:]+$" | head -1 || echo "")
_sync_spin=$(( (_sync_spin + 1) % 4 )) _spd=$(grep -oE "speed:[[:space:]]+[0-9,]+" /proc/drbd 2>/dev/null | grep -oE "[0-9,]+$" | head -1 || echo "")
_sync_iter=$(( _sync_iter + 1 ))
_sc="${_sync_chars[$_sync_iter % 4]}"
if [[ -n "$_pct" ]]; then if [[ -n "$_pct" ]]; then
printf "\r [%s] syncing: %s%% done — ETA %s @ %s K/s " \ _msg=" [${_sc}] syncing: ${_pct}% done — ETA ${_eta:-??:??:??} @ ${_spd:-?} K/s"
"$_sc" "$_pct" "${_eta:-??:??:??}" "${_spd:-?}" if $_is_tty; then
printf "\r%-80s" "$_msg"
elif [[ "$_pct" != "$_sync_last_pct" ]]; then
log "$_msg"
_sync_last_pct="$_pct"
fi
else else
printf "\r [%s] waiting for sync to start (dstate: %s) " "$_sc" "$_dstate" # /proc/drbd shows cs:SyncSource but no percent line yet — or something unexpected.
_msg=" [${_sc}] dstate: ${_dstate} — waiting for sync progress in /proc/drbd"
if $_is_tty; then
printf "\r%-80s" "$_msg"
elif (( _sync_iter % 5 == 1 )); then
log "$_msg"
fi
fi fi
sleep 3 sleep 3
done done