influxdb-install.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env bash
  2. YW=$(echo "\033[33m")
  3. RD=$(echo "\033[01;31m")
  4. BL=$(echo "\033[36m")
  5. GN=$(echo "\033[1;92m")
  6. CL=$(echo "\033[m")
  7. RETRY_NUM=10
  8. RETRY_EVERY=3
  9. NUM=$RETRY_NUM
  10. CM="${GN}✓${CL}"
  11. CROSS="${RD}✗${CL}"
  12. BFR="\\r\\033[K"
  13. HOLD="-"
  14. set -o errexit
  15. set -o errtrace
  16. set -o nounset
  17. set -o pipefail
  18. shopt -s expand_aliases
  19. alias die='EXIT=$? LINE=$LINENO error_exit'
  20. trap die ERR
  21. function error_exit() {
  22. trap - ERR
  23. local reason="Unknown failure occurred."
  24. local msg="${1:-$reason}"
  25. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  26. echo -e "$flag $msg" 1>&2
  27. exit $EXIT
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. while [ "$(hostname -I)" = "" ]; do
  45. echo 1>&2 -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. ((NUM--))
  48. if [ $NUM -eq 0 ]; then
  49. echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  50. exit 1
  51. fi
  52. done
  53. msg_ok "Set up Container OS"
  54. msg_ok "Network Connected: ${BL}$(hostname -I)"
  55. set +e
  56. alias die=''
  57. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  58. msg_error "Internet NOT Connected"
  59. read -r -p "Would you like to continue anyway? <y/N> " prompt
  60. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  61. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  62. else
  63. echo -e " 🖧 Check Network Settings"
  64. exit 1
  65. fi
  66. fi
  67. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  68. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
  69. alias die='EXIT=$? LINE=$LINENO error_exit'
  70. set -e
  71. msg_info "Updating Container OS"
  72. apt-get update &>/dev/null
  73. apt-get -y upgrade &>/dev/null
  74. msg_ok "Updated Container OS"
  75. msg_info "Installing Dependencies"
  76. apt-get install -y curl &>/dev/null
  77. apt-get install -y sudo &>/dev/null
  78. apt-get install -y lsb-base &>/dev/null
  79. apt-get install -y lsb-release &>/dev/null
  80. apt-get install -y gnupg2 &>/dev/null
  81. msg_ok "Installed Dependencies"
  82. msg_info "Setting up InfluxDB Repository"
  83. wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add - &>/dev/null
  84. echo "deb https://repos.influxdata.com/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/influxdb.list &>/dev/null
  85. msg_ok "Set up InfluxDB Repository"
  86. read -r -p "Which version of InfluxDB to install? (1 or 2) " prompt
  87. if [[ $prompt == "2" ]]; then
  88. INFLUX="2"
  89. else
  90. INFLUX="1"
  91. fi
  92. msg_info "Installing InfluxDB"
  93. apt-get update &>/dev/null
  94. if [[ $INFLUX == "2" ]]; then
  95. apt-get install -y influxdb2 &>/dev/null
  96. else
  97. apt-get install -y influxdb &>/dev/null
  98. fi
  99. msg_ok "Installed InfluxDB"
  100. read -r -p "Would you like to add Telegraf? <y/N> " prompt
  101. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  102. TELEGRAF="Y"
  103. else
  104. TELEGRAF="N"
  105. fi
  106. if [[ $TELEGRAF == "Y" ]]; then
  107. msg_info "Installing Telegraf"
  108. apt-get install -y telegraf &>/dev/null
  109. msg_ok "Installed Telegraf"
  110. fi
  111. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  112. if [[ $PASS != $ ]]; then
  113. msg_info "Customizing Container"
  114. rm /etc/motd
  115. rm /etc/update-motd.d/10-uname
  116. touch ~/.hushlogin
  117. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  118. mkdir -p $(dirname $GETTY_OVERRIDE)
  119. cat <<EOF >$GETTY_OVERRIDE
  120. [Service]
  121. ExecStart=
  122. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  123. EOF
  124. systemctl daemon-reload
  125. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  126. msg_ok "Customized Container"
  127. fi
  128. if [[ "${SSH_ROOT}" == "yes" ]]; then
  129. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  130. systemctl restart sshd
  131. fi
  132. msg_info "Cleaning up"
  133. apt-get autoremove >/dev/null
  134. apt-get autoclean >/dev/null
  135. msg_ok "Cleaned"