influxdb-v5-install.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env bash
  2. if [ "$VERBOSE" == "yes" ]; then set -x; STD=""; fi
  3. if [ "$VERBOSE" != "yes" ]; then STD="silent"; fi
  4. silent() { "$@" > /dev/null 2>&1; }
  5. if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi
  6. YW=$(echo "\033[33m")
  7. RD=$(echo "\033[01;31m")
  8. BL=$(echo "\033[36m")
  9. GN=$(echo "\033[1;92m")
  10. CL=$(echo "\033[m")
  11. RETRY_NUM=10
  12. RETRY_EVERY=3
  13. CM="${GN}✓${CL}"
  14. CROSS="${RD}✗${CL}"
  15. BFR="\\r\\033[K"
  16. HOLD="-"
  17. set -Eeuo pipefail
  18. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  19. function error_handler() {
  20. local exit_code="$?"
  21. local line_number="$1"
  22. local command="$2"
  23. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  24. echo -e "\n$error_message\n"
  25. }
  26. function msg_info() {
  27. local msg="$1"
  28. echo -ne " ${HOLD} ${YW}${msg}..."
  29. }
  30. function msg_ok() {
  31. local msg="$1"
  32. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  33. }
  34. function msg_error() {
  35. local msg="$1"
  36. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  37. }
  38. msg_info "Setting up Container OS "
  39. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  40. locale-gen >/dev/null
  41. for ((i=RETRY_NUM; i>0; i--)); do
  42. if [ "$(hostname -I)" != "" ]; then
  43. break
  44. fi
  45. echo 1>&2 -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. done
  48. if [ "$(hostname -I)" = "" ]; then
  49. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  50. echo -e " 🖧 Check Network Settings"
  51. exit 1
  52. fi
  53. msg_ok "Set up Container OS"
  54. msg_ok "Network Connected: ${BL}$(hostname -I)"
  55. set +e
  56. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  57. msg_error "Internet NOT Connected"
  58. read -r -p "Would you like to continue anyway? <y/N> " prompt
  59. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  60. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  61. else
  62. echo -e " 🖧 Check Network Settings"
  63. exit 1
  64. fi
  65. fi
  66. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  67. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  68. set -e
  69. msg_info "Updating Container OS"
  70. $STD apt-get update
  71. $STD apt-get -y upgrade
  72. msg_ok "Updated Container OS"
  73. msg_info "Installing Dependencies"
  74. $STD apt-get install -y curl
  75. $STD apt-get install -y sudo
  76. $STD apt-get install -y mc
  77. $STD apt-get install -y lsb-base
  78. $STD apt-get install -y lsb-release
  79. $STD apt-get install -y gnupg2
  80. msg_ok "Installed Dependencies"
  81. msg_info "Setting up InfluxDB Repository"
  82. wget -q https://repos.influxdata.com/influxdata-archive_compat.key
  83. cat influxdata-archive_compat.key | gpg --dearmor | tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
  84. sh -c 'echo "deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main" > /etc/apt/sources.list.d/influxdata.list'
  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. $STD apt-get update
  94. if [[ $INFLUX == "2" ]]; then
  95. $STD apt-get install -y influxdb2
  96. else
  97. $STD apt-get install -y influxdb
  98. fi
  99. $STD systemctl enable --now influxdb
  100. msg_ok "Installed InfluxDB"
  101. read -r -p "Would you like to add Telegraf? <y/N> " prompt
  102. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  103. TELEGRAF="Y"
  104. else
  105. TELEGRAF="N"
  106. fi
  107. if [[ $TELEGRAF == "Y" ]]; then
  108. msg_info "Installing Telegraf"
  109. $STD apt-get install -y telegraf
  110. msg_ok "Installed Telegraf"
  111. fi
  112. echo "export TERM='xterm-256color'" >>/root/.bashrc
  113. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  114. msg_info "Customizing Container"
  115. if [ "$PCT_OSTYPE" == "debian" ]; then rm -rf /etc/motd /etc/update-motd.d/10-uname; else chmod -x /etc/update-motd.d/*; fi
  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 sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  129. msg_info "Cleaning up"
  130. $STD apt-get autoremove
  131. $STD apt-get autoclean
  132. msg_ok "Cleaned"