common.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # |source| this file
  2. #
  3. # Common utilities shared by other scripts in this directory
  4. #
  5. # The following directive disable complaints about unused variables in this
  6. # file:
  7. # shellcheck disable=2034
  8. #
  9. netDir=$(
  10. cd "$(dirname "${BASH_SOURCE[0]}")" || exit
  11. echo "$PWD"
  12. )
  13. netConfigDir="$netDir"/config
  14. mkdir -p "$netConfigDir"
  15. SOLANA_ROOT="$netDir"/..
  16. # shellcheck source=scripts/configure-metrics.sh
  17. source "$SOLANA_ROOT"/scripts/configure-metrics.sh
  18. configFile="$netConfigDir/config"
  19. geoipConfigFile="$netConfigDir/geoip.yml"
  20. entrypointIp=
  21. publicNetwork=
  22. netBasename=
  23. sshPrivateKey=
  24. letsEncryptDomainName=
  25. externalNodeSshKey=
  26. sshOptions=()
  27. validatorIpList=()
  28. validatorIpListPrivate=()
  29. validatorIpListZone=()
  30. clientIpList=()
  31. clientIpListPrivate=()
  32. clientIpListZone=()
  33. blockstreamerIpList=()
  34. blockstreamerIpListPrivate=()
  35. blockstreamerIpListZone=()
  36. buildSshOptions() {
  37. sshOptions=(
  38. -o "ConnectTimeout=20"
  39. -o "BatchMode=yes"
  40. -o "StrictHostKeyChecking=no"
  41. -o "UserKnownHostsFile=/dev/null"
  42. -o "User=solana"
  43. -o "IdentitiesOnly=yes"
  44. -o "IdentityFile=$sshPrivateKey"
  45. -o "LogLevel=ERROR"
  46. )
  47. [[ -z $externalNodeSshKey ]] || sshOptions+=(-o "IdentityFile=$externalNodeSshKey")
  48. }
  49. loadConfigFile() {
  50. [[ -r $configFile ]] || usage "Config file unreadable: $configFile"
  51. # shellcheck source=/dev/null
  52. source "$configFile"
  53. [[ -n "$publicNetwork" ]] || usage "Config file invalid, publicNetwork unspecified: $configFile"
  54. [[ -n "$netBasename" ]] || usage "Config file invalid, netBasename unspecified: $configFile"
  55. [[ -n $sshPrivateKey ]] || usage "Config file invalid, sshPrivateKey unspecified: $configFile"
  56. [[ ${#validatorIpList[@]} -gt 0 ]] || usage "Config file invalid, validatorIpList unspecified: $configFile"
  57. [[ ${#validatorIpListPrivate[@]} -gt 0 ]] || usage "Config file invalid, validatorIpListPrivate unspecified: $configFile"
  58. [[ ${#validatorIpList[@]} -eq ${#validatorIpListPrivate[@]} ]] || usage "Config file invalid, validatorIpList/validatorIpListPrivate length mismatch: $configFile"
  59. if $publicNetwork; then
  60. entrypointIp=${validatorIpList[0]}
  61. else
  62. entrypointIp=${validatorIpListPrivate[0]}
  63. fi
  64. buildSshOptions
  65. configureMetrics
  66. }
  67. # https://gist.github.com/cdown/1163649
  68. urlencode() {
  69. declare s="$1"
  70. declare l=$((${#s} - 1))
  71. for i in $(seq 0 $l); do
  72. declare c="${s:$i:1}"
  73. case $c in
  74. [a-zA-Z0-9.~_-])
  75. echo -n "$c"
  76. ;;
  77. *)
  78. printf '%%%02X' "'$c"
  79. ;;
  80. esac
  81. done
  82. }
  83. SOLANA_CONFIG_DIR=$SOLANA_ROOT/config
  84. # Clear the current cluster configuration
  85. clear_config_dir() {
  86. declare config_dir="$1"
  87. _setup_secondary_mount
  88. (
  89. set -x
  90. rm -rf "${config_dir:?}/" # <-- $i might be a symlink, rm the other side of it first
  91. rm -rf "$config_dir"
  92. mkdir -p "$config_dir"
  93. )
  94. _setup_secondary_mount
  95. }
  96. SECONDARY_DISK_MOUNT_POINT=/mnt/extra-disk
  97. _setup_secondary_mount() {
  98. # If there is a secondary disk, symlink the config/ dir there
  99. (
  100. set -x
  101. if [[ -d $SECONDARY_DISK_MOUNT_POINT ]] && \
  102. [[ -w $SECONDARY_DISK_MOUNT_POINT ]]; then
  103. mkdir -p $SECONDARY_DISK_MOUNT_POINT/config
  104. rm -rf "$SOLANA_CONFIG_DIR"
  105. ln -sfT $SECONDARY_DISK_MOUNT_POINT/config "$SOLANA_CONFIG_DIR"
  106. fi
  107. )
  108. }