zigbee2mqtt-v5-install.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 git
  78. $STD apt-get install -y make
  79. $STD apt-get install -y g++
  80. $STD apt-get install -y gcc
  81. msg_ok "Installed Dependencies"
  82. msg_info "Setting up Node.js Repository"
  83. $STD bash <(curl -fsSL https://deb.nodesource.com/setup_18.x)
  84. msg_ok "Set up Node.js Repository"
  85. msg_info "Installing Node.js"
  86. $STD apt-get install -y nodejs
  87. msg_ok "Installed Node.js"
  88. msg_info "Setting up Zigbee2MQTT Repository"
  89. $STD git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
  90. msg_ok "Set up Zigbee2MQTT Repository"
  91. read -r -p "Switch to Edge/dev branch? (y/N) " prompt
  92. if [[ $prompt == "y" ]]; then
  93. DEV="y"
  94. else
  95. DEV="n"
  96. fi
  97. msg_info "Installing Zigbee2MQTT"
  98. cd /opt/zigbee2mqtt
  99. if [[ $DEV == "y" ]]; then
  100. $STD git checkout dev
  101. fi
  102. $STD npm ci
  103. msg_ok "Installed Zigbee2MQTT"
  104. msg_info "Creating Service"
  105. service_path="/etc/systemd/system/zigbee2mqtt.service"
  106. echo "[Unit]
  107. Description=zigbee2mqtt
  108. After=network.target
  109. [Service]
  110. Environment=NODE_ENV=production
  111. ExecStart=/usr/bin/npm start
  112. WorkingDirectory=/opt/zigbee2mqtt
  113. StandardOutput=inherit
  114. StandardError=inherit
  115. Restart=always
  116. User=root
  117. [Install]
  118. WantedBy=multi-user.target" >$service_path
  119. $STD systemctl enable zigbee2mqtt.service
  120. msg_ok "Created Service"
  121. echo "export TERM='xterm-256color'" >>/root/.bashrc
  122. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  123. msg_info "Customizing Container"
  124. if [ "$PCT_OSTYPE" == "debian" ]; then rm -rf /etc/motd /etc/update-motd.d/10-uname; else chmod -x /etc/update-motd.d/*; fi
  125. touch ~/.hushlogin
  126. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  127. mkdir -p $(dirname $GETTY_OVERRIDE)
  128. cat <<EOF >$GETTY_OVERRIDE
  129. [Service]
  130. ExecStart=
  131. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  132. EOF
  133. systemctl daemon-reload
  134. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  135. msg_ok "Customized Container"
  136. fi
  137. if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  138. msg_info "Cleaning up"
  139. $STD apt-get autoremove
  140. $STD apt-get autoclean
  141. msg_ok "Cleaned"