install.sh 3.4 KB

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