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

     1  #!/bin/bash -e
     2  
     3  CFG_FILE="${REMOTE_CFG_FILE:-$(pwd)/remote.setup.cfg}"
     4  
     5  show_help() {
     6      echo "usage: remote.setup config --host <host> --port <port> --user <USER> [--pass <PASS>] [--cert <CERT>]"
     7      echo "usage: remote.setup get-config-path"
     8      echo ""
     9      echo "Available options:"
    10      echo "  -h --help   show this help message."
    11      echo ""
    12  }
    13  
    14  get_config_path() {
    15      echo "$CFG_FILE"
    16  }
    17  
    18  config() {
    19      local host port user pass cert
    20      while [ $# -gt 0 ]; do
    21          case "$1" in
    22              -h|--help)
    23                  show_help
    24                  exit
    25                  ;;
    26              --host)
    27                  host="$2"
    28                  shift 2
    29                  ;;
    30              --port)
    31                  port="$2"
    32                  shift 2
    33                  ;;
    34              --user)
    35                  user="$2"
    36                  shift 2
    37                  ;;
    38              --pass)
    39                  pass="$2"
    40                  shift 2
    41                  ;;
    42              --cert)
    43                  cert="$2"
    44                  shift 2
    45                  ;;
    46              *)
    47                  echo "tests.remote: unknown option $1" >&2
    48                  exit 1
    49                  ;;
    50          esac
    51      done
    52  
    53      if [ -z "$host" ] || [ -z "$port" ] || [ -z "$user" ]; then
    54          echo "remote.setup: host, port and user values are required"
    55          exit 1
    56      fi
    57      if [ -n "$pass" ] && [ -z "$(command -v sshpass)" ]; then
    58          echo "remote.setup: sshpass tool is required when password is configured"
    59      fi
    60      if [ -n "$cert" ] && ! [ -f "$cert" ]; then
    61          echo "remote.setup: certificate is set but file does not exist"
    62          exit 1
    63      fi
    64  
    65      rm -f "$CFG_FILE"
    66      echo "export TESTS_REMOTE_HOST=$host" > "$CFG_FILE"
    67      # shellcheck disable=SC2129
    68      echo "export TESTS_REMOTE_PORT=$port" >> "$CFG_FILE"
    69      echo "export TESTS_REMOTE_USER=$user" >> "$CFG_FILE"
    70      echo "export TESTS_REMOTE_PASS=$pass" >> "$CFG_FILE"
    71      echo "export TESTS_REMOTE_CERT=$cert" >> "$CFG_FILE"
    72  }
    73  
    74  main() {
    75      local subcommand="$1"
    76      local action=
    77      while [ $# -gt 0 ]; do
    78          case "$1" in
    79              -h|--help)
    80                  show_help
    81                  exit 0
    82                  ;;
    83              *)
    84                  action=$(echo "$subcommand" | tr '-' '_')
    85                  shift
    86                  break
    87                  ;;
    88          esac
    89      done
    90  
    91      if [ -z "$(declare -f "$action")" ]; then
    92          echo "remote.setup: no such command: $subcommand"
    93          show_help
    94          exit 1
    95      fi
    96  
    97      "$action" "$@"
    98  }
    99  
   100  main "$@"