install.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # Ensure SSH-Agent is started and id_rsa loaded,
  25. # git and npm will do a lot of stuff via ssh and
  26. # ask for id passphrase multiple times if there's
  27. # no agent
  28. echo "[INSTALL] Starting SSH agent"
  29. eval $(ssh-agent)
  30. ssh-add ~/.ssh/id_rsa
  31. TMPFOLDER="/tmp/hbbq/monitoring/$(date +'%Y%m%d%H%M%S')"
  32. echo "[INSTALL] Cloning entire project into temp folder $TMPFOLDER"
  33. mkdir -p "$TMPFOLDER"
  34. git clone ssh://git@gogs.hostbbq.com:8301/hostbbq/hostbbq-monitoring.git "$TMPFOLDER"
  35. cd "$TMPFOLDER/daemon"
  36. echo "[INSTALL] Installing npm build dependencies for daemon project"
  37. $EXC_NPM install
  38. echo "[INSTALL] Transpiling typescript sources of daemon project"
  39. $EXC_NPM run build
  40. echo "[INSTALL] Installing daemon service application"
  41. cp -rv "dist" "$INSTALL_DIR/"
  42. cp -v ".env.default" "$INSTALL_DIR/.env"
  43. cp -v "cpu.sh" "$INSTALL_DIR/"
  44. cp -v "hdd.sh" "$INSTALL_DIR/"
  45. cp -v "ram.sh" "$INSTALL_DIR/"
  46. cp -v "package.json" "$INSTALL_DIR/"
  47. cd "$INSTALL_DIR"
  48. echo "[INSTALL] Installing npm runtime dependencies"
  49. $EXC_NPM install --omit=dev
  50. echo "[INSTALL] Creating and enabling systemd unit \"monitoring@daemon.service\""
  51. SVC_FILE="/lib/systemd/system/monitoring@daemon.service"
  52. ACTION="update"
  53. if [ ! -f "$SVC_FILE" ]; then
  54. ACTION="install"
  55. fi
  56. cat > $SVC_FILE << EOF
  57. [Unit]
  58. Description=HostBBQ Monitoring Daemon Service
  59. After=network.target
  60. [Service]
  61. Type=simple
  62. WorkingDirectory="$INSTALL_DIR"
  63. ExecStart=node .
  64. ExecStop=/bin/kill -TERM \$MAINPID
  65. [Install]
  66. WantedBy=multi-user.target
  67. Alias=monitoring@daemon.service
  68. EOF
  69. if [[ "$ACTION" = "install" ]]; then
  70. systemctl enable monitoring@daemon.service
  71. systemctl start monitoring@daemon.service
  72. else
  73. systemctl restart monitoring@daemon.service
  74. fi
  75. echo "[CLEANUP] Removing temp folder $TMPFOLDER"
  76. rm -rf "$TMPFOLDER"
  77. echo "[SUCCESS] HostBBQ Monitoring Daemon installed and activated successfully"