scrypted-install.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env bash
  2. YW=$(echo "\033[33m")
  3. RD=$(echo "\033[01;31m")
  4. BL=$(echo "\033[36m")
  5. GN=$(echo "\033[1;92m")
  6. CL=$(echo "\033[m")
  7. RETRY_NUM=10
  8. RETRY_EVERY=3
  9. NUM=$RETRY_NUM
  10. CM="${GN}✓${CL}"
  11. CROSS="${RD}✗${CL}"
  12. BFR="\\r\\033[K"
  13. HOLD="-"
  14. set -o errexit
  15. set -o errtrace
  16. set -o nounset
  17. set -o pipefail
  18. shopt -s expand_aliases
  19. alias die='EXIT=$? LINE=$LINENO error_exit'
  20. trap die ERR
  21. function error_exit() {
  22. trap - ERR
  23. local reason="Unknown failure occurred."
  24. local msg="${1:-$reason}"
  25. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  26. echo -e "$flag $msg" 1>&2
  27. exit $EXIT
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. while [ "$(hostname -I)" = "" ]; do
  45. echo 1>&2 -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. ((NUM--))
  48. if [ $NUM -eq 0 ]; then
  49. echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  50. exit 1
  51. fi
  52. done
  53. msg_ok "Set up Container OS"
  54. msg_ok "Network Connected: ${BL}$(hostname -I)"
  55. set +e
  56. alias die=''
  57. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  58. msg_error "Internet NOT Connected"
  59. read -r -p "Would you like to continue anyway? <y/N> " prompt
  60. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  61. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  62. else
  63. echo -e " 🖧 Check Network Settings"
  64. exit 1
  65. fi
  66. fi
  67. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  68. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
  69. alias die='EXIT=$? LINE=$LINENO error_exit'
  70. set -e
  71. msg_info "Updating Container OS"
  72. apt-get update &>/dev/null
  73. apt-get -y upgrade &>/dev/null
  74. msg_ok "Updated Container OS"
  75. msg_info "Installing Dependencies"
  76. apt-get -y install software-properties-common apt-utils &>/dev/null
  77. apt-get -y update &>/dev/null
  78. apt-get -y upgrade &>/dev/null
  79. apt-get -y install \
  80. build-essential \
  81. gcc \
  82. gir1.2-gtk-3.0 \
  83. libcairo2-dev \
  84. libgirepository1.0-dev \
  85. libglib2.0-dev \
  86. libjpeg-dev \
  87. libgif-dev \
  88. libopenjp2-7 \
  89. libpango1.0-dev \
  90. librsvg2-dev \
  91. pkg-config \
  92. curl \
  93. sudo &>/dev/null
  94. msg_ok "Installed Dependencies"
  95. msg_info "Installing GStreamer"
  96. apt-get -y install \
  97. gstreamer1.0-tools \
  98. libgstreamer1.0-dev \
  99. libgstreamer-plugins-base1.0-dev \
  100. libgstreamer-plugins-bad1.0-dev \
  101. gstreamer1.0-plugins-base \
  102. gstreamer1.0-plugins-good \
  103. gstreamer1.0-plugins-bad \
  104. gstreamer1.0-plugins-ugly \
  105. gstreamer1.0-libav \
  106. gstreamer1.0-alsa &>/dev/null
  107. msg_ok "Installed GStreamer"
  108. msg_info "Setting up Node.js Repository"
  109. curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &>/dev/null
  110. msg_ok "Set up Node.js Repository"
  111. msg_info "Installing Node.js"
  112. apt-get install -y nodejs &>/dev/null
  113. msg_ok "Installed Node.js"
  114. msg_info "Installing Python3"
  115. apt-get -y install \
  116. python3 \
  117. python3-dev \
  118. python3-gi \
  119. python3-gst-1.0 \
  120. python3-matplotlib \
  121. python3-numpy \
  122. python3-opencv \
  123. python3-pil \
  124. python3-pip \
  125. python3-setuptools \
  126. python3-skimage \
  127. python3-wheel &>/dev/null
  128. python3 -m pip install --upgrade pip &>/dev/null
  129. python3 -m pip install aiofiles debugpy typing_extensions typing &>/dev/null
  130. msg_ok "Installed Python3"
  131. read -r -p "Would you like to add Coral Edge TPU support? <y/N> " prompt
  132. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  133. CORAL="Y"
  134. else
  135. CORAL="N"
  136. fi
  137. if [[ $CORAL == "Y" ]]; then
  138. msg_info "Adding Coral Edge TPU Support"
  139. echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | tee /etc/apt/sources.list.d/coral-edgetpu.list &>/dev/null
  140. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - &>/dev/null
  141. apt-get -y update &>/dev/null
  142. apt-get -y install libedgetpu1-std &>/dev/null
  143. msg_ok "Coral Edge TPU Support Added"
  144. fi
  145. msg_info "Installing Scrypted"
  146. sudo -u root npx -y scrypted@latest install-server &>/dev/null
  147. msg_info "Installed Scrypted"
  148. msg_info "Creating Service"
  149. service_path="/etc/systemd/system/scrypted.service"
  150. echo "[Unit]
  151. Description=Scrypted service
  152. After=network.target
  153. [Service]
  154. User=root
  155. Group=root
  156. Type=simple
  157. ExecStart=/usr/bin/npx -y scrypted serve
  158. Restart=on-failure
  159. RestartSec=3
  160. [Install]
  161. WantedBy=multi-user.target" >$service_path
  162. systemctl enable --now scrypted.service &>/dev/null
  163. msg_ok "Created Service"
  164. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  165. if [[ $PASS != $ ]]; then
  166. msg_info "Customizing Container"
  167. chmod -x /etc/update-motd.d/*
  168. touch ~/.hushlogin
  169. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  170. mkdir -p $(dirname $GETTY_OVERRIDE)
  171. cat <<EOF >$GETTY_OVERRIDE
  172. [Service]
  173. ExecStart=
  174. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  175. EOF
  176. systemctl daemon-reload
  177. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  178. msg_ok "Customized Container"
  179. fi
  180. if [[ "${SSH_ROOT}" == "yes" ]]; then
  181. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  182. systemctl restart sshd
  183. fi
  184. msg_info "Cleaning up"
  185. apt-get autoremove >/dev/null
  186. apt-get autoclean >/dev/null
  187. msg_ok "Cleaned"