diff --git a/examples/deploy-qemu-vm.sh b/examples/deploy-qemu-vm.sh index 7e68c2f..c0a005e 100755 --- a/examples/deploy-qemu-vm.sh +++ b/examples/deploy-qemu-vm.sh @@ -38,14 +38,13 @@ sshPort=60734 runVM $tmpDir/vm $vmNumCPUs $vmMemoryMiB $sshPort vmWaitForSSH -echo "Waiting until services are ready..." -c ' -attempts=300 -while ! systemctl is-active clightning &> /dev/null; do - ((attempts-- == 0)) && { echo "timeout"; exit 1; } - sleep 0.2 -done -' +printf "Waiting until services are ready" +c " +$(cat qemu-vm/wait-until.sh) +waitUntil 'systemctl is-active clightning &> /dev/null' 100 +" +echo + echo echo "Bitcoind service:" c systemctl status bitcoind diff --git a/examples/qemu-vm/run-vm.sh b/examples/qemu-vm/run-vm.sh index b195635..0181796 100644 --- a/examples/qemu-vm/run-vm.sh +++ b/examples/qemu-vm/run-vm.sh @@ -1,5 +1,7 @@ qemuDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd) +source "$qemuDir/wait-until.sh" + tmpDir=/tmp/nix-bitcoin-qemu-vm mkdir -p $tmpDir @@ -31,7 +33,7 @@ runVM() { vmWaitForSSH() { echo printf "Waiting for SSH connection..." - while ! c : 2>/dev/null; do :; done + waitUntil "c : 2>/dev/null" 500 echo } diff --git a/examples/qemu-vm/wait-until.sh b/examples/qemu-vm/wait-until.sh new file mode 100644 index 0000000..b419349 --- /dev/null +++ b/examples/qemu-vm/wait-until.sh @@ -0,0 +1,21 @@ +# Wait until $condition is true, retrying every $intervalMs milliseconds. +# Print a '.' character every second as a progress indicator. +waitUntil() { + condition=$1 + intervalMs=$2 + + lastDotTime=$(getTimeMs) + while ! { t0=$(getTimeMs); eval "$condition"; }; do + now=$(getTimeMs) + if ((now - lastDotTime >= 1000)); then + printf . + lastDotTime=$now + fi + toSleep=$((t0 + intervalMs - now)) + if ((toSleep > 0)); then + sleep $((toSleep / 1000)).$((toSleep % 1000)); + fi + done +} + +getTimeMs() { date +%s%3N; }