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

     1  #!/bin/bash -e
     2  
     3  show_help() {
     4      echo "usage: remote.exec [--user <USER>] [--pass <PASS>] <CMD>"
     5      echo ""
     6      echo "Available options:"
     7      echo "  -h --help   show this help message."
     8      echo ""
     9  }
    10  
    11  _load_config() {
    12      local CFG_FILE
    13      CFG_FILE="$(remote.setup get-config-path)"
    14      if [ ! -f "$CFG_FILE" ]; then
    15          echo "remote.exec: config file \"$CFG_FILE\" not found, please run remote.setup command first"
    16          return 1
    17      fi
    18      # shellcheck disable=SC1090
    19      . "$CFG_FILE"
    20  }
    21  
    22  _get_pass() {
    23      local SSH_PASS
    24      if [ -n "$TESTS_REMOTE_PASS" ]; then
    25          echo "sshpass -p $TESTS_REMOTE_PASS"
    26      fi
    27  }
    28  
    29  _get_cert() {
    30      if [ -n "$TESTS_REMOTE_PASS" ]; then
    31          return
    32      elif [ -n "$TESTS_REMOTE_CERT" ]; then
    33          echo "-i $TESTS_REMOTE_CERT"
    34      fi
    35  }
    36  
    37  remote_exec() {
    38      local user pass
    39      local timeout=10
    40      while [ $# -gt 0 ]; do
    41          case "$1" in
    42              -h|--help)
    43                  show_help
    44                  exit
    45                  ;;
    46              --user)
    47                  user="$2"
    48                  shift 2
    49                  ;;
    50              --pass)
    51                  pass="$2"
    52                  shift 2
    53                  ;;
    54              --timeout)
    55                  timeout="$2"
    56                  shift 2
    57                  ;;
    58              -*)
    59                  echo "remote.exec: unknown option $1" >&2
    60                  exit 1
    61                  ;;
    62              *)
    63                  break    
    64                  ;;
    65          esac
    66      done
    67  
    68      _load_config
    69      if [ -n "$user" ]; then
    70          TESTS_REMOTE_USER="$user"
    71      fi
    72      if [ -n "$pass" ]; then
    73          TESTS_REMOTE_PASS="$pass"
    74      fi
    75  
    76      local SSH_PASS SSH_CERT
    77      SSH_PASS="$(_get_pass)"
    78      SSH_CERT="$(_get_cert)"
    79  
    80      # shellcheck disable=SC2153,SC2086
    81      $SSH_PASS ssh $SSH_CERT -p "$TESTS_REMOTE_PORT" -o LogLevel=ERROR -o ServerAliveInterval=10 -o ConnectTimeout="$timeout" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$TESTS_REMOTE_USER"@"$TESTS_REMOTE_HOST" "$@"
    82  }
    83  
    84  main() {    
    85      remote_exec "$@"
    86  }
    87  
    88  main "$@"