homeassistant-core-install.sh 6.0 KB

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