shinobi-install.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 --fix-missing &>/dev/null
  74. apt-get -y upgrade &>/dev/null
  75. msg_ok "Updated Container OS"
  76. ubuntuversion=$(lsb_release -r | awk '{print $2}' | cut -d . -f1)
  77. if [ "$ubuntuversion" = "18" ] || [ "$ubuntuversion" -le "18" ]; then
  78. apt install sudo wget -y
  79. sudo apt install -y software-properties-common
  80. sudo add-apt-repository universe -y
  81. apt update -y
  82. apt update --fix-missing -y
  83. fi
  84. msg_info "Installing Dependencies"
  85. apt-get install -y curl sudo git &>/dev/null
  86. apt-get install -y make zip net-tools &>/dev/null
  87. apt-get install -y gcc g++ cmake &>/dev/null
  88. msg_ok "Installed Dependencies"
  89. msg_info "Setting up Node.js Repository"
  90. curl -fsSL https://deb.nodesource.com/setup_18.x | bash - &>/dev/null
  91. msg_ok "Set up Node.js Repository"
  92. msg_info "Installing Node.js"
  93. apt-get install -y nodejs &>/dev/null
  94. msg_ok "Installed Node.js"
  95. msg_info "Installing FFMPEG"
  96. apt-get install -y ffmpeg &>/dev/null
  97. msg_ok "Installed FFMPEG"
  98. msg_info "Clonning Shinobi"
  99. cd /opt
  100. git clone https://gitlab.com/Shinobi-Systems/Shinobi.git -b master Shinobi &>/dev/null
  101. cd Shinobi
  102. gitVersionNumber=$(git rev-parse HEAD)
  103. theDateRightNow=$(date)
  104. touch version.json
  105. chmod 777 version.json
  106. echo '{"Product" : "'"Shinobi"'" , "Branch" : "'"master"'" , "Version" : "'"$gitVersionNumber"'" , "Date" : "'"$theDateRightNow"'" , "Repository" : "'"https://gitlab.com/Shinobi-Systems/Shinobi.git"'"}' > version.json
  107. msg_ok "Cloned Shinobi"
  108. msg_info "Installing Database"
  109. sqlpass=""
  110. echo "mariadb-server mariadb-server/root_password password $sqlpass" | debconf-set-selections
  111. echo "mariadb-server mariadb-server/root_password_again password $sqlpass" | debconf-set-selections
  112. apt-get install -y mariadb-server &>/dev/null
  113. service mysql start
  114. sqluser="root"
  115. mysql -e "source sql/user.sql" || true
  116. mysql -e "source sql/framework.sql" || true
  117. msg_ok "Installed Database"
  118. cp conf.sample.json conf.json
  119. cronKey=$(head -c 1024 < /dev/urandom | sha256sum | awk '{print substr($1,1,29)}')
  120. sed -i -e 's/Shinobi/'"$cronKey"'/g' conf.json
  121. cp super.sample.json super.json
  122. msg_info "Installing Shinobi"
  123. npm i npm -g &>/dev/null
  124. npm install --unsafe-perm &>/dev/null
  125. npm install pm2@latest -g &>/dev/null
  126. chmod -R 755 .
  127. touch INSTALL/installed.txt
  128. ln -s /opt/Shinobi/INSTALL/shinobi /usr/bin/shinobi
  129. node /opt/Shinobi/tools/modifyConfiguration.js addToConfig="{\"cron\":{\"key\":\"$(head -c 64 < /dev/urandom | sha256sum | awk '{print substr($1,1,60)}')\"}}" &>/dev/null
  130. pm2 start camera.js &>/dev/null
  131. pm2 start cron.js &>/dev/null
  132. pm2 startup &>/dev/null
  133. pm2 save &>/dev/null
  134. pm2 list &>/dev/null
  135. msg_ok "Installed Shinobi"
  136. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  137. if [[ $PASS != $ ]]; then
  138. msg_info "Customizing Container"
  139. chmod -x /etc/update-motd.d/*
  140. touch ~/.hushlogin
  141. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  142. mkdir -p $(dirname $GETTY_OVERRIDE)
  143. cat <<EOF >$GETTY_OVERRIDE
  144. [Service]
  145. ExecStart=
  146. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  147. EOF
  148. systemctl daemon-reload
  149. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  150. msg_ok "Customized Container"
  151. fi
  152. if [[ "${SSH_ROOT}" == "yes" ]]; then
  153. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  154. systemctl restart sshd
  155. fi
  156. msg_info "Cleaning up"
  157. apt-get autoremove >/dev/null
  158. apt-get autoclean >/dev/null
  159. msg_ok "Cleaned"