zigbee2mqtt_setup.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. # Setup script environment
  3. set -o errexit #Exit immediately if a pipeline returns a non-zero status
  4. set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
  5. set -o nounset #Treat unset variables as an error
  6. set -o pipefail #Pipe will exit with last non-zero status if applicable
  7. shopt -s expand_aliases
  8. alias die='EXIT=$? LINE=$LINENO error_exit'
  9. trap die ERR
  10. trap 'die "Script interrupted."' INT
  11. function error_exit() {
  12. trap - ERR
  13. local DEFAULT='Unknown failure occured.'
  14. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  15. local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
  16. msg "$FLAG $REASON"
  17. exit $EXIT
  18. }
  19. function msg() {
  20. local TEXT="$1"
  21. echo -e "$TEXT"
  22. }
  23. # Prepare container OS
  24. msg "Setting up Container OS..."
  25. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  26. locale-gen >/dev/null
  27. apt-get -y purge openssh-{client,server} >/dev/null
  28. apt-get autoremove >/dev/null
  29. # Update container OS
  30. msg "Updating container OS..."
  31. apt update &>/dev/null
  32. apt-get -qqy upgrade &>/dev/null
  33. # Install prerequisites
  34. msg "Installing Prerequisites..."
  35. apt-get -qqy install \
  36. curl \
  37. sudo &>/dev/null
  38. # Setup Node.js repository
  39. msg "Setting up Node.js Repository..."
  40. sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - &>/dev/null
  41. # Install Node.js;
  42. msg "Installing Node.js..."
  43. sudo apt-get install -y nodejs git make g++ gcc &>/dev/null
  44. # Clone Zigbee2MQTT repository
  45. msg "Setting up Zigbee2MQTT Repository..."
  46. sudo git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt &>/dev/null
  47. # Install zigbee2mqtt
  48. msg "Installing Zigbee2MQTT..."
  49. cd /opt/zigbee2mqtt &>/dev/null
  50. npm ci &>/dev/null
  51. echo "Creating service file zigbee2mqtt.service"
  52. service_path="/etc/systemd/system/zigbee2mqtt.service"
  53. echo "[Unit]
  54. Description=zigbee2mqtt
  55. After=network.target
  56. [Service]
  57. ExecStart=/usr/bin/npm start
  58. WorkingDirectory=/opt/zigbee2mqtt
  59. StandardOutput=inherit
  60. StandardError=inherit
  61. Restart=always
  62. User=root
  63. [Install]
  64. WantedBy=multi-user.target" > $service_path
  65. # Customize container
  66. msg "Customizing Container..."
  67. rm /etc/motd # Remove message of the day after login
  68. rm /etc/update-motd.d/10-uname # Remove kernel information after login
  69. touch ~/.hushlogin # Remove 'Last login: ' and mail notification after login
  70. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  71. mkdir -p $(dirname $GETTY_OVERRIDE)
  72. cat << EOF > $GETTY_OVERRIDE
  73. [Service]
  74. ExecStart=
  75. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  76. EOF
  77. systemctl daemon-reload
  78. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  79. # Cleanup container
  80. msg "Cleanup..."
  81. rm -rf /zigbee2mqtt_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*