esphome_setup.sh 2.5 KB

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