scrypted-install.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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"
  77. apt-get -y install software-properties-common apt-utils &>/dev/null
  78. apt-get -y update &>/dev/null
  79. apt-get -y upgrade &>/dev/null
  80. apt-get -y install \
  81. build-essential \
  82. gcc \
  83. gir1.2-gtk-3.0 \
  84. libcairo2-dev \
  85. libgirepository1.0-dev \
  86. libglib2.0-dev \
  87. libjpeg-dev \
  88. libgif-dev \
  89. libopenjp2-7 \
  90. libpango1.0-dev \
  91. librsvg2-dev \
  92. pkg-config \
  93. curl \
  94. sudo &>/dev/null
  95. msg_ok "Installed Dependencies"
  96. msg_info "Installing GStreamer"
  97. apt-get -y install \
  98. gstreamer1.0-tools \
  99. libgstreamer1.0-dev \
  100. libgstreamer-plugins-base1.0-dev \
  101. libgstreamer-plugins-bad1.0-dev \
  102. gstreamer1.0-plugins-base \
  103. gstreamer1.0-plugins-good \
  104. gstreamer1.0-plugins-bad \
  105. gstreamer1.0-plugins-ugly \
  106. gstreamer1.0-libav \
  107. gstreamer1.0-alsa &>/dev/null
  108. msg_ok "Installed GStreamer"
  109. msg_info "Setting up Node.js Repository"
  110. curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &>/dev/null
  111. msg_ok "Set up Node.js Repository"
  112. msg_info "Installing Node.js"
  113. apt-get install -y nodejs &>/dev/null
  114. msg_ok "Installed Node.js"
  115. msg_info "Installing Python3"
  116. apt-get -y install \
  117. python3 \
  118. python3-dev \
  119. python3-gi \
  120. python3-gst-1.0 \
  121. python3-matplotlib \
  122. python3-numpy \
  123. python3-opencv \
  124. python3-pil \
  125. python3-pip \
  126. python3-setuptools \
  127. python3-skimage \
  128. python3-wheel &>/dev/null
  129. python3 -m pip install --upgrade pip &>/dev/null
  130. python3 -m pip install aiofiles debugpy typing_extensions typing &>/dev/null
  131. msg_ok "Installed Python3"
  132. read -r -p "Would you like to add Coral Edge TPU support? <y/N> " prompt
  133. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  134. CORAL="Y"
  135. else
  136. CORAL="N"
  137. fi
  138. if [[ $CORAL == "Y" ]]; then
  139. msg_info "Adding Coral Edge TPU Support"
  140. echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | tee /etc/apt/sources.list.d/coral-edgetpu.list &>/dev/null
  141. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - &>/dev/null
  142. apt-get -y update &>/dev/null
  143. apt-get -y install libedgetpu1-std &>/dev/null
  144. msg_ok "Coral Edge TPU Support Added"
  145. fi
  146. msg_info "Installing Scrypted"
  147. sudo -u root npx -y scrypted@latest install-server &>/dev/null
  148. msg_info "Installed Scrypted"
  149. msg_info "Creating Service"
  150. service_path="/etc/systemd/system/scrypted.service"
  151. echo "[Unit]
  152. Description=Scrypted service
  153. After=network.target
  154. [Service]
  155. User=root
  156. Group=root
  157. Type=simple
  158. ExecStart=/usr/bin/npx -y scrypted serve
  159. Restart=on-failure
  160. RestartSec=3
  161. [Install]
  162. WantedBy=multi-user.target" >$service_path
  163. systemctl enable --now scrypted.service &>/dev/null
  164. msg_ok "Created Service"
  165. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  166. if [[ $PASS != $ ]]; then
  167. msg_info "Customizing Container"
  168. chmod -x /etc/update-motd.d/*
  169. touch ~/.hushlogin
  170. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  171. mkdir -p $(dirname $GETTY_OVERRIDE)
  172. cat <<EOF >$GETTY_OVERRIDE
  173. [Service]
  174. ExecStart=
  175. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  176. EOF
  177. systemctl daemon-reload
  178. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  179. msg_ok "Customized Container"
  180. fi
  181. if [[ "${SSH_ROOT}" == "yes" ]]; then
  182. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  183. systemctl restart sshd
  184. fi
  185. msg_info "Cleaning up"
  186. apt-get autoremove >/dev/null
  187. apt-get autoclean >/dev/null
  188. msg_ok "Cleaned"