install.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if [ -d "$INSTALL_DIR/dist" ]; then
  35. rm -rf "$INSTALL_DIR/dist"
  36. fi
  37. cp -rv "dist" "$INSTALL_DIR/"
  38. if [ ! -f "$INSTALL_DIR/.env" ]; then
  39. cp -v ".env.default" "$INSTALL_DIR/.env"
  40. fi
  41. cp -v "cpu.sh" "$INSTALL_DIR/"
  42. cp -v "hdd.sh" "$INSTALL_DIR/"
  43. cp -v "ram.sh" "$INSTALL_DIR/"
  44. cp -v "package.json" "$INSTALL_DIR/"
  45. cd "$INSTALL_DIR"
  46. echo "[INSTALL] Installing npm runtime dependencies"
  47. $EXC_NPM install --omit=dev
  48. echo "[INSTALL] Creating and enabling systemd unit \"monitoring@daemon.service\""
  49. SVC_FILE="/lib/systemd/system/monitoring@daemon.service"
  50. ACTION="update"
  51. if [ ! -f "$SVC_FILE" ]; then
  52. ACTION="install"
  53. fi
  54. cat > $SVC_FILE << EOF
  55. [Unit]
  56. Description=HostBBQ Monitoring Daemon Service
  57. After=network.target
  58. [Service]
  59. Type=simple
  60. WorkingDirectory=$INSTALL_DIR
  61. ExecStart=node .
  62. ExecStop=/bin/kill -TERM \$MAINPID
  63. [Install]
  64. WantedBy=multi-user.target
  65. Alias=monitoring@daemon.service
  66. EOF
  67. if [[ "$ACTION" = "install" ]]; then
  68. systemctl enable monitoring@daemon.service
  69. systemctl start monitoring@daemon.service
  70. else
  71. systemctl restart monitoring@daemon.service
  72. fi
  73. echo "[CLEANUP] Removing temp folder $TMPFOLDER"
  74. rm -rf "$TMPFOLDER"
  75. echo "[SUCCESS] HostBBQ Monitoring Daemon installed and activated successfully"