examples/deploy-qemu-vm: show progress when waiting

This improves the user experience on VM startup, which can take a few
seconds.
This commit is contained in:
Erik Arvstedt 2021-03-08 15:11:16 +01:00 committed by Jonas Nick
parent ccba86a0f0
commit 908af3bfb8
No known key found for this signature in database
GPG Key ID: 4861DBF262123605
3 changed files with 31 additions and 9 deletions

View File

@ -38,14 +38,13 @@ sshPort=60734
runVM $tmpDir/vm $vmNumCPUs $vmMemoryMiB $sshPort runVM $tmpDir/vm $vmNumCPUs $vmMemoryMiB $sshPort
vmWaitForSSH vmWaitForSSH
echo "Waiting until services are ready..." printf "Waiting until services are ready"
c ' c "
attempts=300 $(cat qemu-vm/wait-until.sh)
while ! systemctl is-active clightning &> /dev/null; do waitUntil 'systemctl is-active clightning &> /dev/null' 100
((attempts-- == 0)) && { echo "timeout"; exit 1; } "
sleep 0.2 echo
done
'
echo echo
echo "Bitcoind service:" echo "Bitcoind service:"
c systemctl status bitcoind c systemctl status bitcoind

View File

@ -1,5 +1,7 @@
qemuDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd) qemuDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
source "$qemuDir/wait-until.sh"
tmpDir=/tmp/nix-bitcoin-qemu-vm tmpDir=/tmp/nix-bitcoin-qemu-vm
mkdir -p $tmpDir mkdir -p $tmpDir
@ -31,7 +33,7 @@ runVM() {
vmWaitForSSH() { vmWaitForSSH() {
echo echo
printf "Waiting for SSH connection..." printf "Waiting for SSH connection..."
while ! c : 2>/dev/null; do :; done waitUntil "c : 2>/dev/null" 500
echo echo
} }

View File

@ -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; }