install.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. INSTALL_DIR="${PWD}"
  3. # Check system requirements: Git, Node & NPM
  4. EXC_GIT="$(/usr/bin/which git)"
  5. if [ -z "$EXC_GIT" ]; then
  6. echo "[ERROR] Missing required system dependency 'git'." >&2
  7. echo "Please install using \"apt install git\"" >&2
  8. exit 1
  9. fi
  10. EXC_NPM="$(/usr/bin/which npm)"
  11. if [ -z "$EXC_NPM" ]; then
  12. echo "[ERROR] Missing required system dependency 'npm'." >&2
  13. echo "Please install following the official install documentation." >&2
  14. exit 1
  15. fi
  16. EXC_NODE="$(/usr/bin/which node)"
  17. if [ -z "$EXC_NODE" ]; then
  18. echo "[ERROR] Missing required system dependency 'node'." >&2
  19. echo "Please install following the official install documentation." >&2
  20. exit 1
  21. fi
  22. # exit on error exit codes
  23. set -e
  24. TMPFOLDER="/tmp/hbbq/monitoring/$(date +'%Y%m%d%H%M%S')"
  25. echo "[INSTALL] Cloning entire project into temp folder $TMPFOLDER"
  26. mkdir -p "$TMPFOLDER"
  27. git clone https://gogs.hostbbq.com/hostbbq/hostbbq-monitoring.git "$TMPFOLDER"
  28. cd "$TMPFOLDER/daemon"
  29. echo "[INSTALL] Installing npm build dependencies for daemon project"
  30. $EXC_NPM install
  31. echo "[INSTALL] Transpiling typescript sources of daemon project"
  32. $EXC_NPM run build
  33. echo "[INSTALL] Installing daemon service application"
  34. cp -rv "dist" "$INSTALL_DIR/"
  35. cp -v ".env.default" "$INSTALL_DIR/.env"
  36. cp -v "cpu.sh" "$INSTALL_DIR/"
  37. cp -v "hdd.sh" "$INSTALL_DIR/"
  38. cp -v "ram.sh" "$INSTALL_DIR/"
  39. cp -v "package.json" "$INSTALL_DIR/"
  40. cd "$INSTALL_DIR"
  41. echo "[INSTALL] Installing npm runtime dependencies"
  42. $EXC_NPM install --omit=dev
  43. echo "[INSTALL] Creating and enabling systemd unit \"monitoring@daemon.service\""
  44. SVC_FILE="/lib/systemd/system/monitoring@daemon.service"
  45. ACTION="update"
  46. if [ ! -f "$SVC_FILE" ]; then
  47. ACTION="install"
  48. fi
  49. cat > $SVC_FILE << EOF
  50. [Unit]
  51. Description=HostBBQ Monitoring Daemon Service
  52. After=network.target
  53. [Service]
  54. Type=simple
  55. WorkingDirectory="$INSTALL_DIR"
  56. ExecStart=node .
  57. ExecStop=/bin/kill -TERM \$MAINPID
  58. [Install]
  59. WantedBy=multi-user.target
  60. Alias=monitoring@daemon.service
  61. EOF
  62. if [[ "$ACTION" = "install" ]]; then
  63. systemctl enable monitoring@daemon.service
  64. systemctl start monitoring@daemon.service
  65. else
  66. systemctl restart monitoring@daemon.service
  67. fi
  68. echo "[CLEANUP] Removing temp folder $TMPFOLDER"
  69. rm -rf "$TMPFOLDER"
  70. echo "[SUCCESS] HostBBQ Monitoring Daemon installed and activated successfully"