Prechádzať zdrojové kódy

setup configuration for prod deployment

Christian Kahlau 2 rokov pred
rodič
commit
292c730bc2

+ 30 - 0
server.docker-compose.dev.yml

@@ -0,0 +1,30 @@
+version: '3.8'
+services:
+  server:
+    build:
+      context: .
+      dockerfile: server.dockerfile
+    image: monitoring-server:latest
+    environment:
+      - DEV_MODE=1
+      - LOG_LEVEL=DEBUG
+      - WEB_PORT=80
+      - DATA_DIR=data
+      - GOOGLE_APPLICATION_CREDENTIALS="google-cloud/firebase-adminsdk.json"
+      - NOTIFICATION_ICON_URL="https://fcm.hostbbq.net/logo.png"
+    ports:
+      - 8880:80
+    volumes:
+      - ./server/data:/home/node/monitoring/data
+    links:
+      - mariadb
+  mariadb:
+    build:
+      context: server/mysql
+    image: monitoring-mariadb:latest
+    # ports:
+    #   - 3306:3306
+    environment:
+      - MARIADB_ROOT_PASSWORD=i4mGr00ti4mGr00t
+    volumes:
+      - ./server/db:/var/lib/mysql

+ 22 - 7
server.docker-compose.yml

@@ -5,24 +5,39 @@ services:
       context: .
       dockerfile: server.dockerfile
     image: monitoring-server:latest
+    env_file:
+      - .env
     environment:
-      - DEV_MODE=1
-      - LOG_LEVEL=DEBUG
-      - WEB_PORT=80
-      - DATA_DIR=data
       - GOOGLE_APPLICATION_CREDENTIALS="google-cloud/firebase-adminsdk.json"
       - NOTIFICATION_ICON_URL="https://fcm.hostbbq.net/logo.png"
     ports:
-      - 8880:80
+      - $HOST_PORT:$WEB_PORT
     volumes:
-      - ./server/data:/home/node/monitoring/data
+      - 'data-dir:/home/node/monitoring/data'
     links:
       - mariadb
+    restart: always
   mariadb:
     build:
       context: server/mysql
     image: monitoring-mariadb:latest
+    ports:
+      - 3306:3306
     environment:
       - MARIADB_ROOT_PASSWORD=i4mGr00ti4mGr00t
     volumes:
-      - ./server/db:/var/lib/mysql
+      - db-dir:/var/lib/mysql
+    restart: always
+volumes:
+  data-dir:
+    driver: local
+    driver_opts:
+      o: bind
+      type: none
+      device: $DATA_VOLUME_DIR
+  db-dir:
+    driver: local
+    driver_opts:
+      o: bind
+      type: none
+      device: $DB_VOLUME_DIR

+ 3 - 3
server/.env.default

@@ -1,5 +1,5 @@
 LOG_LEVEL=INFO
-WEB_PORT=8880
+WEB_PORT=80
+HOST_PORT=8880
 DATA_DIR=data
-GOOGLE_APPLICATION_CREDENTIALS="google-cloud/firebase-adminsdk.json"
-NOTIFICATION_ICON_URL="https://fcm.hostbbq.net/logo.png"
+DB_DIR=db

+ 19 - 59
server/install/install.sh

@@ -10,17 +10,10 @@ if [ -z "$EXC_GIT" ]; then
   exit 1
 fi
 
-EXC_NPM="$(/usr/bin/which npm)"
-if [ -z "$EXC_NPM" ]; then
-  echo "[ERROR] Missing required system dependency 'npm'." >&2
-  echo "Please install following the official install documentation." >&2
-  exit 1
-fi
-
-EXC_NODE="$(/usr/bin/which node)"
-if [ -z "$EXC_NODE" ]; then
-  echo "[ERROR] Missing required system dependency 'node'." >&2
-  echo "Please install following the official install documentation." >&2
+EXC_DOCKER="$(/usr/bin/which docker)"
+if [ -z "$EXC_DOCKER" ]; then
+  echo "[ERROR] Missing required system dependency 'docker'." >&2
+  echo "Please install using the official documentation." >&2
   exit 1
 fi
 
@@ -37,7 +30,6 @@ if [ ! -f "$FCM_ACCOUNT_JSON" ]; then
   exit 1
 fi
 
-
 # exit on error exit codes
 set -e
 
@@ -53,67 +45,35 @@ echo "[INSTALL] Cloning submodules ..."
 git submodule init
 git submodule update
 
-cd "$TMPFOLDER/server"
-echo "[INSTALL] Installing npm build dependencies for server project"
-$EXC_NPM install
+if [[ -f "$INSTALL_DIR/.env" ]]; then
+  export $(cat "$INSTALL_DIR/.env" | xargs)
+fi
 
-cd "$TMPFOLDER/ng"
-echo "[INSTALL] Installing npm build dependencies for Angular project"
-$EXC_NPM install
+if [[ ! -d "$PWD/$DATA_DIR" ]]; then
+  mkdir "$PWD/$DATA_DIR"
+fi
 
-echo "[INSTALL] Building Angular project"
-$EXC_NPM run build
+if [[ ! -d "$PWD/$DB_DIR" ]]; then
+  mkdir "$PWD/$DB_DIR"
+fi
 
-cd "$TMPFOLDER/server"
-echo "[INSTALL] Transpiling typescript sources of server project"
-$EXC_NPM run build
+docker compose -f server.docker-compose.yml build
 
 echo "[INSTALL] Installing server application"
 if [ -d "$INSTALL_DIR/dist" ]; then
   rm -rf "$INSTALL_DIR/dist"
 fi
-cp -rv "dist" "$INSTALL_DIR/"
-cp -rv "public" "$INSTALL_DIR/"
 if [ ! -f "$INSTALL_DIR/.env" ]; then
   cp -v ".env.default" "$INSTALL_DIR/.env"
 fi
-cp -v "package.json" "$INSTALL_DIR/"
+cp -v "server.docker-compose.yml" "$INSTALL_DIR/docker-compose.yml"
 
 cd "$INSTALL_DIR"
 
-echo "[INSTALL] Installing npm runtime dependencies"
-$EXC_NPM install --omit=dev
-
-echo "[INSTALL] Creating and enabling systemd unit \"monitoring@server.service\""
-SVC_FILE="/lib/systemd/system/monitoring@server.service"
-
-ACTION="update"
-if [ ! -f "$SVC_FILE" ]; then
-  ACTION="install"
-fi
-
-cat > $SVC_FILE << EOF
-[Unit]
-Description=HostBBQ Monitoring Server Service
-After=network.target
-
-[Service]
-Type=simple
-WorkingDirectory=$INSTALL_DIR
-ExecStart=node .
-
-[Install]
-WantedBy=multi-user.target
-Alias=monitoring@server.service
-EOF
-
-if [[ "$ACTION" = "install" ]]; then
-  systemctl enable monitoring@server.service
-  systemctl start monitoring@server.service
-else
-  systemctl daemon-reload
-  systemctl restart monitoring@server.service
-fi
+DATA_VOLUME_DIR="$PWD/$DATA_DIR" \
+DB_VOLUME_DIR="$PWD/$DB_DIR" \
+COMPOSE_PROJECT_NAME="monitoring-server" \
+docker compose up -d
 
 echo "[CLEANUP] Removing temp folder $TMPFOLDER"
 rm -rf "$TMPFOLDER"

+ 6 - 6
server/src/ctrl/mariadb-importer.class.ts

@@ -31,7 +31,7 @@ export class MariaDBImporter {
   async runImport() {
     // await this.newDb.beginTransaction();
     try {
-      await this.cutoffOldData(moment().add(-4, 'months').toDate());
+      // await this.cutoffOldData(moment().add(-4, 'months').toDate());
       await this.truncateTables();
 
       await this.importServer();
@@ -50,11 +50,11 @@ export class MariaDBImporter {
     }
   }
 
-  private async cutoffOldData(cutoffDate: Date) {
-    Logger.info('[INFO]', 'Cutting off old DataEntries before', cutoffDate);
-    await this.oldDb.run('DELETE FROM `ServerDataEntry` WHERE `Timestamp` < ?;', [cutoffDate.getTime()]);
-    await this.oldDb.run('DELETE FROM `HealthCheckDataEntry` WHERE `Timestamp` < ?;', [cutoffDate.getTime()]);
-  }
+  // private async cutoffOldData(cutoffDate: Date) {
+  //   Logger.info('[INFO]', 'Cutting off old DataEntries before', cutoffDate);
+  //   await this.oldDb.run('DELETE FROM `ServerDataEntry` WHERE `Timestamp` < ?;', [cutoffDate.getTime()]);
+  //   await this.oldDb.run('DELETE FROM `HealthCheckDataEntry` WHERE `Timestamp` < ?;', [cutoffDate.getTime()]);
+  // }
 
   private async truncateTables() {
     Logger.info('[INFO]', 'Truncating all Tables in MariaDB ...');