From bfa5b4fba55403244e231188d5a59d394f5487be Mon Sep 17 00:00:00 2001 From: sadnub Date: Sat, 17 Jul 2021 15:57:24 -0400 Subject: [PATCH] allow persistent mesh config and fix mongodb password uri issue --- docker/.env.example | 1 + .../tactical-meshcentral/entrypoint.sh | 17 +++++++++++++++-- docker/docker-compose.yml | 1 + docker/image-build.sh | 3 ++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docker/.env.example b/docker/.env.example index 3907ae92..f6be551a 100644 --- a/docker/.env.example +++ b/docker/.env.example @@ -15,6 +15,7 @@ MESH_USER=tactical MESH_PASS=tactical MONGODB_USER=mongouser MONGODB_PASSWORD=mongopass +MESH_PERSISTENT_CONFIG=0 # database settings POSTGRES_USER=postgres diff --git a/docker/containers/tactical-meshcentral/entrypoint.sh b/docker/containers/tactical-meshcentral/entrypoint.sh index da1a06fb..3e612fc1 100644 --- a/docker/containers/tactical-meshcentral/entrypoint.sh +++ b/docker/containers/tactical-meshcentral/entrypoint.sh @@ -9,14 +9,19 @@ set -e : "${MONGODB_HOST:=tactical-mongodb}" : "${MONGODB_PORT:=27017}" : "${NGINX_HOST_IP:=172.20.0.20}" +: "${MESH_PERSISTENT_CONFIG:=0}" mkdir -p /home/node/app/meshcentral-data mkdir -p ${TACTICAL_DIR}/tmp +if [ ! -f "/home/node/app/meshcentral-data/config.json" ] || [[ "${MESH_PERSISTENT_CONFIG}" -eq 0 ]]; then + +encoded_uri=$(node -p "encodeURI('mongodb://${MONGODB_USER}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}')") + mesh_config="$(cat << EOF { "settings": { - "mongodb": "mongodb://${MONGODB_USER}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}", + "mongodb": "${encoded_uri}", "Cert": "${MESH_HOST}", "TLSOffload": "${NGINX_HOST_IP}", "RedirPort": 80, @@ -54,11 +59,19 @@ EOF echo "${mesh_config}" > /home/node/app/meshcentral-data/config.json +fi + node node_modules/meshcentral --createaccount ${MESH_USER} --pass ${MESH_PASS} --email example@example.com node node_modules/meshcentral --adminaccount ${MESH_USER} if [ ! -f "${TACTICAL_DIR}/tmp/mesh_token" ]; then - node node_modules/meshcentral --logintokenkey > ${TACTICAL_DIR}/tmp/mesh_token + mesh_token=$(node node_modules/meshcentral --logintokenkey) + + if [[ ${#mesh_token} -eq 160 ]]; then + echo ${mesh_token} > /opt/tactical/tmp/mesh_token + else + echo "Failed to generate mesh token. Fix the error and restart the mesh container" + fi fi # wait for nginx container diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index d32a33f3..613e383b 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -97,6 +97,7 @@ services: MESH_PASS: ${MESH_PASS} MONGODB_USER: ${MONGODB_USER} MONGODB_PASSWORD: ${MONGODB_PASSWORD} + MESH_PERSISTENT_CONFIG: ${MESH_PERSISTENT_CONFIG} networks: proxy: aliases: diff --git a/docker/image-build.sh b/docker/image-build.sh index ee668e0e..b0fd4ed2 100755 --- a/docker/image-build.sh +++ b/docker/image-build.sh @@ -3,7 +3,8 @@ set -o errexit set -o pipefail -DOCKER_IMAGES="tactical tactical-frontend tactical-nats tactical-nginx tactical-meshcentral" +# tactical tactical-frontend tactical-nats tactical-nginx +DOCKER_IMAGES="tactical-meshcentral" cd ..