install.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. git submodule init
  29. git submodule update
  30. cd "$TMPFOLDER/server"
  31. echo "[INSTALL] Installing npm build dependencies for server project"
  32. $EXC_NPM install
  33. cd "$TMPFOLDER/ng"
  34. echo "[INSTALL] Installing npm build dependencies for Angular project"
  35. $EXC_NPM install
  36. echo "[INSTALL] Building Angular project"
  37. $EXC_NPM run build
  38. cd "$TMPFOLDER/server"
  39. echo "[INSTALL] Transpiling typescript sources of server project"
  40. $EXC_NPM run build
  41. echo "[INSTALL] Installing server application"
  42. if [ -d "$INSTALL_DIR/dist" ]; then
  43. rm -rf "$INSTALL_DIR/dist"
  44. fi
  45. cp -rv "dist" "$INSTALL_DIR/"
  46. cp -rv "public" "$INSTALL_DIR/"
  47. if [ ! -f "$INSTALL_DIR/.env" ]; then
  48. cp -v ".env.default" "$INSTALL_DIR/.env"
  49. fi
  50. cp -v "package.json" "$INSTALL_DIR/"
  51. cd "$INSTALL_DIR"
  52. echo "[INSTALL] Installing npm runtime dependencies"
  53. $EXC_NPM install --omit=dev
  54. echo "[INSTALL] Creating and enabling systemd unit \"monitoring@server.service\""
  55. SVC_FILE="/lib/systemd/system/monitoring@server.service"
  56. ACTION="update"
  57. if [ ! -f "$SVC_FILE" ]; then
  58. ACTION="install"
  59. fi
  60. cat > $SVC_FILE << EOF
  61. [Unit]
  62. Description=HostBBQ Monitoring Server Service
  63. After=network.target
  64. [Service]
  65. Type=simple
  66. WorkingDirectory=$INSTALL_DIR
  67. ExecStart=node .
  68. [Install]
  69. WantedBy=multi-user.target
  70. Alias=monitoring@server.service
  71. EOF
  72. if [[ "$ACTION" = "install" ]]; then
  73. systemctl enable monitoring@server.service
  74. systemctl start monitoring@server.service
  75. else
  76. systemctl restart monitoring@server.service
  77. fi
  78. echo "[CLEANUP] Removing temp folder $TMPFOLDER"
  79. rm -rf "$TMPFOLDER"
  80. echo "[SUCCESS] HostBBQ Monitoring Server installed and activated successfully"