install.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. GCC_CONFIG_DIR="${INSTALL_DIR}/google-cloud"
  23. FCM_ACCOUNT_JSON="${GCC_CONFIG_DIR}/firebase-adminsdk.json"
  24. if [ ! -d "$GCC_CONFIG_DIR" ]; then
  25. echo "[ERROR] Missing required google cloud key files. Please prepare directory 'google-cloud' in install directory." >&2
  26. exit 1
  27. fi
  28. if [ ! -f "$FCM_ACCOUNT_JSON" ]; then
  29. echo "[ERROR] Missing required Firebase Admin SDK key file. Please provide this file as 'google-cloud/firebase-adminsdk.json' in order to install." >&2
  30. exit 1
  31. fi
  32. # exit on error exit codes
  33. set -e
  34. TMPFOLDER="/tmp/hbbq/monitoring/$(date +'%Y%m%d%H%M%S')"
  35. echo "[INSTALL] Cloning entire project into temp folder $TMPFOLDER"
  36. mkdir -p "$TMPFOLDER"
  37. git clone https://gogs.hostbbq.com/hostbbq/hostbbq-monitoring.git "$TMPFOLDER"
  38. cd "$TMPFOLDER"
  39. echo "[INSTALL] Cloning submodules ..."
  40. git submodule init
  41. git submodule update
  42. cd "$TMPFOLDER/server"
  43. echo "[INSTALL] Installing npm build dependencies for server project"
  44. $EXC_NPM install
  45. cd "$TMPFOLDER/ng"
  46. echo "[INSTALL] Installing npm build dependencies for Angular project"
  47. $EXC_NPM install
  48. echo "[INSTALL] Building Angular project"
  49. $EXC_NPM run build
  50. cd "$TMPFOLDER/server"
  51. echo "[INSTALL] Transpiling typescript sources of server project"
  52. $EXC_NPM run build
  53. echo "[INSTALL] Installing server application"
  54. if [ -d "$INSTALL_DIR/dist" ]; then
  55. rm -rf "$INSTALL_DIR/dist"
  56. fi
  57. cp -rv "dist" "$INSTALL_DIR/"
  58. cp -rv "public" "$INSTALL_DIR/"
  59. if [ ! -f "$INSTALL_DIR/.env" ]; then
  60. cp -v ".env.default" "$INSTALL_DIR/.env"
  61. fi
  62. cp -v "package.json" "$INSTALL_DIR/"
  63. cd "$INSTALL_DIR"
  64. echo "[INSTALL] Installing npm runtime dependencies"
  65. $EXC_NPM install --omit=dev
  66. echo "[INSTALL] Creating and enabling systemd unit \"monitoring@server.service\""
  67. SVC_FILE="/lib/systemd/system/monitoring@server.service"
  68. ACTION="update"
  69. if [ ! -f "$SVC_FILE" ]; then
  70. ACTION="install"
  71. fi
  72. cat > $SVC_FILE << EOF
  73. [Unit]
  74. Description=HostBBQ Monitoring Server Service
  75. After=network.target
  76. [Service]
  77. Type=simple
  78. WorkingDirectory=$INSTALL_DIR
  79. ExecStart=node .
  80. [Install]
  81. WantedBy=multi-user.target
  82. Alias=monitoring@server.service
  83. EOF
  84. if [[ "$ACTION" = "install" ]]; then
  85. systemctl enable monitoring@server.service
  86. systemctl start monitoring@server.service
  87. else
  88. systemctl daemon-reload
  89. systemctl restart monitoring@server.service
  90. fi
  91. echo "[CLEANUP] Removing temp folder $TMPFOLDER"
  92. rm -rf "$TMPFOLDER"
  93. echo "[SUCCESS] HostBBQ Monitoring Server installed and activated successfully"