github.com/canonical/ubuntu-image@v0.0.0-20240430122802-2202fe98b290/tests/lib/external/snapd-testing-tools/remote/remote.retry (about)

     1  #!/bin/bash -e
     2  
     3  show_help() {
     4      echo "usage: remote.retry [--wait WAIT] [-n|--attempts ATTEMPTS] <CMD>"
     5      echo ""
     6      echo "Available options:"
     7      echo "  -h --help   show this help message."
     8      echo ""
     9  }
    10  
    11  remote_retry(){
    12      local attempts=$1
    13      local wait=$2
    14      local cmd=$3
    15  
    16      while ! remote.exec "$cmd"; do
    17          attempts=$(( attempts - 1 ))
    18          if [ $attempts -le 0 ]; then
    19              echo "remote.retry: timed out retrying command"
    20              return 1
    21          fi
    22          sleep "$wait"
    23      done
    24  }
    25  
    26  main() {
    27      local wait attempts cmd
    28      wait=1
    29      attempts=30
    30  
    31      while [ $# -gt 0 ]; do
    32          case "$1" in
    33              -h|--help)
    34                  show_help
    35                  exit
    36                  ;;
    37              --wait)
    38                  wait=$2
    39                  shift 2
    40                  ;;
    41              --attempts|-n)
    42                  attempts=$2
    43                  shift 2
    44                  ;;
    45              *)
    46                  break
    47                  ;;
    48          esac
    49      done
    50  
    51      if [ $# -eq 0 ]; then
    52          echo "remote.retry: command to retry not specified"
    53          return 1
    54      fi
    55  
    56      remote_retry "$attempts" "$wait" "$@"
    57  }
    58  
    59  main "$@"