unifi-install.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env bash
  2. #https://community.ui.com/questions/UniFi-Installation-Scripts-or-UniFi-Easy-Update-Script-or-UniFi-Lets-Encrypt-or-UniFi-Easy-Encrypt-/ccbc7530-dd61-40a7-82ec-22b17f027776
  3. YW=`echo "\033[33m"`
  4. RD=`echo "\033[01;31m"`
  5. BL=`echo "\033[36m"`
  6. GN=`echo "\033[1;92m"`
  7. CL=`echo "\033[m"`
  8. RETRY_NUM=10
  9. RETRY_EVERY=3
  10. NUM=$RETRY_NUM
  11. CM="${GN}✓${CL}"
  12. CROSS="${RD}✗${CL}"
  13. BFR="\\r\\033[K"
  14. HOLD="-"
  15. set -o errexit
  16. set -o errtrace
  17. set -o nounset
  18. set -o pipefail
  19. shopt -s expand_aliases
  20. alias die='EXIT=$? LINE=$LINENO error_exit'
  21. trap die ERR
  22. function error_exit() {
  23. trap - ERR
  24. local reason="Unknown failure occurred."
  25. local msg="${1:-$reason}"
  26. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  27. echo -e "$flag $msg" 1>&2
  28. exit $EXIT
  29. }
  30. function msg_info() {
  31. local msg="$1"
  32. echo -ne " ${HOLD} ${YW}${msg}..."
  33. }
  34. function msg_ok() {
  35. local msg="$1"
  36. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  37. }
  38. function msg_error() {
  39. local msg="$1"
  40. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  41. }
  42. msg_info "Setting up Container OS "
  43. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  44. locale-gen >/dev/null
  45. while [ "$(hostname -I)" = "" ]; do
  46. 1>&2 echo -en "${CROSS}${RD} No Network! "
  47. sleep $RETRY_EVERY
  48. ((NUM--))
  49. if [ $NUM -eq 0 ]
  50. then
  51. 1>&2 echo -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  52. exit 1
  53. fi
  54. done
  55. msg_ok "Set up Container OS"
  56. msg_ok "Network Connected: ${BL}$(hostname -I)"
  57. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else msg_error "Internet NOT Connected"; exit 1; fi;
  58. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  59. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi;
  60. msg_info "Updating Container OS"
  61. apt-get update &>/dev/null
  62. apt-get -y upgrade &>/dev/null
  63. msg_ok "Updated Container OS"
  64. msg_info "Installing Dependencies"
  65. apt-get install -y curl &>/dev/null
  66. apt-get install -y sudo &>/dev/null
  67. msg_ok "Installed Dependencies"
  68. read -r -p "Local Controller? <Y/n> " prompt
  69. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]
  70. then
  71. LOCAL="--local-controller"
  72. else
  73. LOCAL=""
  74. fi
  75. msg_info "Installing UniFi Network Application (Patience)"
  76. wget -qL https://get.glennr.nl/unifi/install/install_latest/unifi-latest.sh && bash unifi-latest.sh --skip --add-repository $LOCAL &>/dev/null
  77. msg_ok "Installed UniFi Network Application"
  78. PASS=$(grep -w "root" /etc/shadow | cut -b6);
  79. if [[ $PASS != $ ]]; then
  80. msg_info "Customizing Container"
  81. rm /etc/motd
  82. rm /etc/update-motd.d/10-uname
  83. touch ~/.hushlogin
  84. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  85. mkdir -p $(dirname $GETTY_OVERRIDE)
  86. cat << EOF > $GETTY_OVERRIDE
  87. [Service]
  88. ExecStart=
  89. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  90. EOF
  91. systemctl daemon-reload
  92. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  93. msg_ok "Customized Container"
  94. fi
  95. msg_info "Cleaning up"
  96. apt-get autoremove >/dev/null
  97. apt-get autoclean >/dev/null
  98. rm -rf /var/{cache,log}/* /var/lib/apt/lists/*
  99. msg_ok "Cleaned"