install.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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="/etc/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. else
  72. systemctl restart monitoring@daemon.service
  73. fi
  74. echo "[CLEANUP] Removing temp folder $TMPFOLDER"
  75. rm -rf "$TMPFOLDER"
  76. echo "[SUCCESS] HostBBQ Monitoring Daemon installed and activated successfully"