From be7f2d19ba294a59858d4d794b51510234b57a95 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Mon, 30 Dec 2019 12:36:50 -0600 Subject: [PATCH] Updating docker configuration * Fleshed out the Dockerfile * Updated ignore files * Removed log files from repository * Added some logging to the entrypoint.sh * Updated fields in package.json * Changed the config module to load files without require * Added development run script --- .dockerignore | 9 ++++++-- .gitignore | 6 +++++- Dockerfile | 16 +++++++++++--- bot/config.js | 5 ++++- combined.log | 36 ------------------------------- entrypoint.sh | 6 +++++- error.log | 0 package.json | 2 ++ scripts/run_development_docker.sh | 21 ++++++++++++++++++ 9 files changed, 57 insertions(+), 44 deletions(-) delete mode 100644 combined.log delete mode 100644 error.log create mode 100644 scripts/run_development_docker.sh diff --git a/.dockerignore b/.dockerignore index ba561b3..0c31b8f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,7 @@ -data/*config.json -log/ \ No newline at end of file +node_modules/ +log/ +*.swp +pipeline.yml +README.md +CONTRIBUTING.md +test/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index bc3e975..08a93ea 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,10 @@ data/*config.json # Log files log/ +*.log # Mac Files -.DS_Store \ No newline at end of file +.DS_Store + +# Swap files +*.swp \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e60840b..a1dbcdf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,16 @@ FROM node:12.14-stretch + COPY . /opt/baphomet -RUN npm install + +ENV NODE_ENV=production +ENV LOG_LEVEL=warn + +RUN mkdir /opt/baphomet/log + +RUN cd /opt/baphomet \ + && npm install --only=prod \ + && chmod +x entrypoint.sh + WORKDIR /opt/baphomet -ENTRYPOINT entrypoint.sh -CMD run \ No newline at end of file +ENTRYPOINT [ "./entrypoint.sh" ] +CMD [ "run" ] diff --git a/bot/config.js b/bot/config.js index ad3e471..9c1a7a7 100644 --- a/bot/config.js +++ b/bot/config.js @@ -1,3 +1,4 @@ +let fs = require('fs'); let { logger } = require('./logging'); let loadedConfigs = new Map(); @@ -12,7 +13,9 @@ function sanitizeConfig(config, fields=[]) { function getConfig(configFile, sanitizedFields=[]) { if (!loadedConfigs.has(configFile)) { - let config = require(configFile); + logger.info("Reading config: %s", configFile); + let rawConfigData = fs.readFileSync(configFile); + let config = JSON.parse(rawConfigData); logger.info("Loaded config: %s", configFile); logger.debug("%o", sanitizeConfig(config, sanitizedFields)); loadedConfigs.set(configFile, config); diff --git a/combined.log b/combined.log deleted file mode 100644 index e754a7d..0000000 --- a/combined.log +++ /dev/null @@ -1,36 +0,0 @@ -{"message":"Running with config:","level":"info","service":"user-service"} -{"message":{"baseUrl":"https://matrix.nulloctet.com","userId":"@baphomet-dev:nulloctet.com","accessToken":"******","admins":["@warrick:nulloctet.com"],"anyCanInvite":false,"statusRooms":["!ayFeLNSFDrerMMelCg:nulloctet.com"]},"level":"debug","service":"user-service"} -{"service":"user-service","level":"info","message":"Running version:"} -{"message":"Creating Matrix Client","level":"info","service":"user-service"} -{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} -{"service":"user-service","level":"debug","message":"Notifying %s"} -{"service":"user-service","level":"debug","message":"Notified %s"} -{"service":"user-service","level":"debug","message":"Notifying %s"} -{"service":"user-service","level":"debug","message":"Notified %s"} -{"message":"Running with config:","level":"info","service":"user-service"} -{"message":{"baseUrl":"https://matrix.nulloctet.com","userId":"@baphomet-dev:nulloctet.com","accessToken":"******","admins":["@warrick:nulloctet.com"],"anyCanInvite":false,"statusRooms":["!ayFeLNSFDrerMMelCg:nulloctet.com"]},"level":"debug","service":"user-service"} -{"service":"user-service","level":"info","message":"Running version:"} -{"message":"Creating Matrix Client","level":"info","service":"user-service"} -{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} -{"service":"user-service","level":"debug","message":"Notifying %s of startup"} -{"service":"user-service","level":"debug","message":"Notified %s of startup"} -{"service":"user-service","level":"debug","message":"Notifying %s of shutdown"} -{"service":"user-service","level":"debug","message":"Notified %s of shutdown"} -{"message":"Running with config:","level":"info","service":"user-service"} -{"message":{"baseUrl":"https://matrix.nulloctet.com","userId":"@baphomet-dev:nulloctet.com","accessToken":"******","admins":["@warrick:nulloctet.com"],"anyCanInvite":false,"statusRooms":["!ayFeLNSFDrerMMelCg:nulloctet.com"]},"level":"debug","service":"user-service"} -{"service":"user-service","level":"info","message":"Running version:"} -{"message":"Creating Matrix Client","level":"info","service":"user-service"} -{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} -{"service":"user-service","level":"debug","message":"Notifying %s of startup"} -{"service":"user-service","level":"debug","message":"Notified %s of startup"} -{"service":"user-service","level":"debug","message":"Notifying %s of shutdown"} -{"service":"user-service","level":"debug","message":"Notified %s of shutdown"} -{"message":"Running with config:","level":"info","service":"user-service"} -{"service":"user-service","level":"debug","message":"%o"} -{"service":"user-service","level":"info","message":"Running version: %s"} -{"message":"Creating Matrix Client","level":"info","service":"user-service"} -{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} -{"service":"user-service","level":"debug","message":"Notifying %s of startup"} -{"service":"user-service","level":"debug","message":"Notified %s of startup"} -{"service":"user-service","level":"debug","message":"Notifying %s of shutdown"} -{"service":"user-service","level":"debug","message":"Notified %s of shutdown"} diff --git a/entrypoint.sh b/entrypoint.sh index 47ea9d7..35d1320 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,6 +3,10 @@ DIR="$( cd "$( dirname "${0}" )" >/dev/null 2>&1 && pwd )" export NODE_PATH="${DIR}" +echo "NODE_ENV: ${NODE_ENV}" +echo "NODE_PATH: ${NODE_PATH}" +echo "LOG_LEVEL: ${LOG_LEVEL}" + case $1 in run) node index.js @@ -10,4 +14,4 @@ run) *) echo "\"$1\" is an unrecognized command" ;; -esac \ No newline at end of file +esac diff --git a/error.log b/error.log deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index 528dff5..b165106 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,8 @@ "main": "index.js", "author": "Drew Short ", "license": "MIT", + "description": "A Matrix bot written on top of the matrix-js-sdk", + "repository": "https://git.nulloctet.com/warricksothr/baphomet-js", "scripts": { "test": "mocha" }, diff --git a/scripts/run_development_docker.sh b/scripts/run_development_docker.sh new file mode 100644 index 0000000..3ca56c6 --- /dev/null +++ b/scripts/run_development_docker.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +pushd "${DIR}/.." + +CONTAINER_NAME=baphomet-dev +IMAGE_NAME=baphomet-js +IMAGE_TAG=dev +IMAGE_BUILD_DIR=. + +docker build -t ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_BUILD_DIR} + +docker rm ${CONTAINER_NAME} + +docker run -it \ + -e NODE_ENV=development \ + -e LOG_LEVEL=debug \ + --name ${CONTAINER_NAME} \ + ${IMAGE_NAME}:${IMAGE_TAG} + +popd \ No newline at end of file