esphome_setup.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. # Update container OS
  28. msg "Updating container OS..."
  29. apt update &>/dev/null
  30. apt-get -qqy upgrade &>/dev/null
  31. # Install prerequisites
  32. msg "Installing Prerequisites..."
  33. apt-get -qqy install \
  34. curl \
  35. sudo &>/dev/null
  36. # Installing pip3
  37. msg "Installing pip3..."
  38. apt-get install python3-pip -y &>/dev/null
  39. # Install ESPHome;
  40. msg "Installing ESPHome..."
  41. pip3 install esphome &>/dev/null
  42. # Installing ESPHome Dashboard
  43. msg "Installing ESPHome Dashboard..."
  44. pip3 install tornado esptool &>/dev/null
  45. echo "Creating service file esphomeDashboard.service"
  46. service_path="/etc/systemd/system/esphomeDashboard.service"
  47. echo "[Unit]
  48. Description=ESPHome Dashboard
  49. After=network.target
  50. [Service]
  51. ExecStart=/usr/local/bin/esphome /root/config/ dashboard
  52. Restart=always
  53. User=root
  54. [Install]
  55. WantedBy=multi-user.target" > $service_path
  56. systemctl enable esphomeDashboard.service &>/dev/null
  57. systemctl start esphomeDashboard
  58. # Customize container
  59. msg "Customizing Container..."
  60. rm /etc/motd # Remove message of the day after login
  61. rm /etc/update-motd.d/10-uname # Remove kernel information after login
  62. touch ~/.hushlogin # Remove 'Last login: ' and mail notification after login
  63. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  64. mkdir -p $(dirname $GETTY_OVERRIDE)
  65. cat << EOF > $GETTY_OVERRIDE
  66. [Service]
  67. ExecStart=
  68. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  69. EOF
  70. systemctl daemon-reload
  71. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  72. # Cleanup container
  73. msg "Cleanup..."
  74. rm -rf /esphome_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*