homeassistant-core-v5-install.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env bash
  2. if [ "$VERBOSE" == "yes" ]; then set -x; fi
  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. silent() { "$@" > /dev/null 2>&1; }
  23. function error_exit() {
  24. trap - ERR
  25. local reason="Unknown failure occurred."
  26. local msg="${1:-$reason}"
  27. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  28. echo -e "$flag $msg" 1>&2
  29. exit $EXIT
  30. }
  31. function msg_info() {
  32. local msg="$1"
  33. echo -ne " ${HOLD} ${YW}${msg}..."
  34. }
  35. function msg_ok() {
  36. local msg="$1"
  37. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  38. }
  39. function msg_error() {
  40. local msg="$1"
  41. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  42. }
  43. msg_info "Setting up Container OS"
  44. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  45. locale-gen >/dev/null
  46. while [ "$(hostname -I)" = "" ]; do
  47. echo 1>&2 -en "${CROSS}${RD} No Network! "
  48. sleep $RETRY_EVERY
  49. ((NUM--))
  50. if [ $NUM -eq 0 ]; then
  51. echo 1>&2 -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. set +e
  58. alias die=''
  59. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  60. msg_error "Internet NOT Connected"
  61. read -r -p "Would you like to continue anyway? <y/N> " prompt
  62. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  63. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  64. else
  65. echo -e " 🖧 Check Network Settings"
  66. exit 1
  67. fi
  68. fi
  69. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  70. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
  71. alias die='EXIT=$? LINE=$LINENO error_exit'
  72. set -e
  73. msg_info "Updating Container OS"
  74. $STD apt-get update
  75. $STD apt-get -y upgrade
  76. msg_ok "Updated Container OS"
  77. msg_info "Installing Dependencies (Patience)"
  78. $STD apt-get install -y \
  79. make \
  80. build-essential \
  81. libjpeg-dev \
  82. libpcap-dev \
  83. libssl-dev \
  84. zlib1g-dev \
  85. libbz2-dev \
  86. libreadline-dev \
  87. libsqlite3-dev \
  88. libmariadb-dev-compat \
  89. autoconf \
  90. git \
  91. curl \
  92. sudo \
  93. llvm \
  94. libncursesw5-dev \
  95. xz-utils \
  96. tzdata \
  97. bluez \
  98. tk-dev \
  99. libxml2-dev \
  100. libxmlsec1-dev \
  101. libffi-dev \
  102. libopenjp2-7 \
  103. libtiff5 \
  104. libturbojpeg0-dev \
  105. liblzma-dev
  106. msg_ok "Installed Dependencies"
  107. msg_info "Installing Linux D-Bus Message Broker"
  108. cat <<EOF >>/etc/apt/sources.list
  109. deb http://deb.debian.org/debian bullseye-backports main contrib non-free
  110. deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free
  111. EOF
  112. $STD apt-get update
  113. $STD apt-get -t bullseye-backports install -y dbus-broker
  114. $STD systemctl enable --now dbus-broker.service
  115. msg_ok "Installed Linux D-Bus Message Broker"
  116. msg_info "Installing pyenv"
  117. $STD git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  118. set +e
  119. echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
  120. echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
  121. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)"\nfi' >> ~/.bashrc
  122. msg_ok "Installed pyenv"
  123. . ~/.bashrc
  124. set -e
  125. msg_info "Installing Python 3.10.8"
  126. $STD pyenv install 3.10.8
  127. pyenv global 3.10.8
  128. msg_ok "Installed Python 3.10.8"
  129. msg_info "Installing Home Assistant-Core"
  130. mkdir /srv/homeassistant
  131. cd /srv/homeassistant
  132. python3 -m venv .
  133. source bin/activate
  134. $STD pip install --upgrade pip
  135. $STD python3 -m pip install wheel
  136. $STD pip install mysqlclient
  137. $STD pip install psycopg2-binary
  138. $STD pip install homeassistant
  139. msg_ok "Installed Home Assistant-Core"
  140. msg_info "Creating Service"
  141. cat <<EOF >/etc/systemd/system/homeassistant.service
  142. [Unit]
  143. Description=Home Assistant
  144. After=network-online.target
  145. [Service]
  146. Type=simple
  147. WorkingDirectory=/root/.homeassistant
  148. ExecStart=/srv/homeassistant/bin/hass -c "/root/.homeassistant"
  149. RestartForceExitStatus=100
  150. [Install]
  151. WantedBy=multi-user.target
  152. EOF
  153. $STD systemctl enable homeassistant
  154. msg_ok "Created Service"
  155. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  156. if [[ $PASS != $ ]]; then
  157. msg_info "Customizing Container"
  158. chmod -x /etc/update-motd.d/*
  159. touch ~/.hushlogin
  160. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  161. mkdir -p $(dirname $GETTY_OVERRIDE)
  162. cat <<EOF >$GETTY_OVERRIDE
  163. [Service]
  164. ExecStart=
  165. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  166. EOF
  167. systemctl daemon-reload
  168. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  169. msg_ok "Customized Container"
  170. fi
  171. if [[ "${SSH_ROOT}" == "yes" ]]; then
  172. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  173. systemctl restart sshd
  174. fi
  175. msg_info "Cleaning up"
  176. $STD apt-get autoremove
  177. $STD apt-get autoclean
  178. msg_ok "Cleaned"