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

     1  #!/bin/bash -e
     2  
     3  show_help() {
     4      echo "usage: remote.pull <REMOTE_PATH> [LOCAL_PATH]"
     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.pull: 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      if [ -n "$TESTS_REMOTE_PASS" ]; then
    24          echo "sshpass -p $TESTS_REMOTE_PASS"
    25      fi
    26  }
    27  
    28  _get_cert() {
    29      if [ -n "$TESTS_REMOTE_PASS" ]; then
    30          return
    31      elif [ -n "$TESTS_REMOTE_CERT" ]; then
    32          echo "-i $TESTS_REMOTE_CERT"
    33      fi
    34  }
    35  
    36  remote_pull() {
    37      local REMOTE_PATH="$1"
    38      local LOCAL_PATH="${2:-.}"
    39      if [ -z "$REMOTE_PATH" ]; then
    40          echo "remote.pull: remote path is required"
    41      fi
    42  
    43      local SSH_PASS SSH_CERT
    44      SSH_PASS="$(_get_pass)"
    45      SSH_CERT="$(_get_cert)"
    46  
    47      # shellcheck disable=SC2153,SC2086
    48      $SSH_PASS scp $SSH_CERT -P "$TESTS_REMOTE_PORT" -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$TESTS_REMOTE_USER"@"$TESTS_REMOTE_HOST":"$REMOTE_PATH" "$LOCAL_PATH"
    49  }
    50  
    51  main() {
    52      if [ $# -eq 0 ] || [ "$1" == '-h' ] ||  [ "$1" == '--help' ]; then
    53          show_help
    54          exit 0
    55      fi
    56  
    57      _load_config
    58      remote_pull "$@"
    59  }
    60  
    61  main "$@"