install.sh 2.6 KB

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