From f4780c2442550fc4585b498883e51221242bc3b6 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Sat, 7 Sep 2024 16:18:26 -0700 Subject: [PATCH 1/6] Moved from #121 --- docker/load_mongodump.sh | 72 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/docker/load_mongodump.sh b/docker/load_mongodump.sh index bd6fcea..fd0a98c 100755 --- a/docker/load_mongodump.sh +++ b/docker/load_mongodump.sh @@ -1,10 +1,72 @@ +#!/bin/bash + +# Directory of the script +SCRIPT_DIR="$(dirname "$0")" + +# Path to the configuration file (one level up) +CONFIG_FILE="$SCRIPT_DIR/../docker-compose-dev.yml" + +# Check if the correct number of arguments is provided +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + echo " : The path to the MongoDB dump file to be restored." + exit 1 +fi + MONGODUMP_FILE=$1 -echo "Copying file to docker container" -docker cp $MONGODUMP_FILE op-admin-dashboard-db-1:/tmp +# Print debug information +echo "Script Directory: $SCRIPT_DIR" +echo "Configuration File Path: $CONFIG_FILE" +echo "MongoDump File Path: $MONGODUMP_FILE" + +# Check if the provided file exists +if [ ! -f "$MONGODUMP_FILE" ]; then + echo "Error: File '$MONGODUMP_FILE' does not exist." + exit 1 +fi + +# Check if the configuration file exists +if [ ! -f "$CONFIG_FILE" ]; then + echo "Error: Configuration file '$CONFIG_FILE' does not exist." + exit 1 +fi + +# Print details about the configuration file +echo "Configuration file details:" +ls -l "$CONFIG_FILE" + +# Extract database name from the mongodump file +TEMP_DIR=$(mktemp -d) +tar -xf "$MONGODUMP_FILE" -C "$TEMP_DIR" +DB_NAME=$(find "$TEMP_DIR/dump" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;) + +if [ -z "$DB_NAME" ]; then + echo "Error: Failed to extract database name from mongodump." + exit 1 +fi + +echo "Database Name: $DB_NAME" + +# Update the docker-compose configuration file with the actual DB_HOST +DB_HOST="mongodb://db/$DB_NAME" +sed -i.bak "s|DB_HOST:.*|DB_HOST: $DB_HOST|" "$CONFIG_FILE" + +echo "Updated docker-compose file:" +cat "$CONFIG_FILE" + +echo "Copying file to Docker container" +docker cp "$MONGODUMP_FILE" op-admin-dashboard-db-1:/tmp + +FILE_NAME=$(basename "$MONGODUMP_FILE") + +echo "Clearing existing database" +docker exec op-admin-dashboard-db-1 bash -c 'mongo --eval "db.getMongo().getDBNames().forEach(function(d) { if (d !== \"admin\" && d !== \"local\") db.getSiblingDB(d).dropDatabase(); })"' -FILE_NAME=`basename $MONGODUMP_FILE` +echo "Restoring the dump from $FILE_NAME to database $DB_NAME" +docker exec -e MONGODUMP_FILE=$FILE_NAME op-admin-dashboard-db-1 bash -c "cd /tmp && tar xvf $FILE_NAME && mongorestore -d $DB_NAME dump/$DB_NAME" -echo "Restoring the dump from $FILE_NAME" -docker exec -e MONGODUMP_FILE=$FILE_NAME op-admin-dashboard-db-1 bash -c 'cd /tmp && tar xvf $MONGODUMP_FILE && mongorestore' +echo "Database restore complete." +# Clean up temporary directory +rm -rf "$TEMP_DIR" From 7e4fe23062b90d51157436fa95cbe398a73b5e7b Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Sat, 7 Sep 2024 16:45:50 -0700 Subject: [PATCH 2/6] Fixed with tarring --- docker-compose-dev.yml | 2 +- docker/load_mongodump.sh | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index df44e28..95d3f6b 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -14,7 +14,7 @@ services: DASH_DEBUG_MODE: "True" DASH_SILENCE_ROUTES_LOGGING: "False" DASH_SERVER_PORT: 8050 - DB_HOST: db + DB_HOST: mongodb://db/openpath_prod_ca_ebike WEB_SERVER_HOST: 0.0.0.0 SERVER_BRANCH: master CONFIG_PATH: "https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/" diff --git a/docker/load_mongodump.sh b/docker/load_mongodump.sh index fd0a98c..1132e0c 100755 --- a/docker/load_mongodump.sh +++ b/docker/load_mongodump.sh @@ -36,10 +36,11 @@ fi echo "Configuration file details:" ls -l "$CONFIG_FILE" -# Extract database name from the mongodump file -TEMP_DIR=$(mktemp -d) -tar -xf "$MONGODUMP_FILE" -C "$TEMP_DIR" -DB_NAME=$(find "$TEMP_DIR/dump" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;) +# Extract the database name from the mongodump file +DB_NAME=$(tar -tf "$MONGODUMP_FILE" | grep '^dump/' | sed 's|^dump/||' | awk -F'/' '{if (NF > 0) {print $1; exit}}') + +# Output the database name +echo "$DB_NAME" if [ -z "$DB_NAME" ]; then echo "Error: Failed to extract database name from mongodump." From 8005698c5b3a175ac911beec1273a255bff58432 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Sat, 7 Sep 2024 16:47:22 -0700 Subject: [PATCH 3/6] revert test --- docker-compose-dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index 95d3f6b..df44e28 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -14,7 +14,7 @@ services: DASH_DEBUG_MODE: "True" DASH_SILENCE_ROUTES_LOGGING: "False" DASH_SERVER_PORT: 8050 - DB_HOST: mongodb://db/openpath_prod_ca_ebike + DB_HOST: db WEB_SERVER_HOST: 0.0.0.0 SERVER_BRANCH: master CONFIG_PATH: "https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/" From 6b85019670b10becdc036c4718560c0110e41809 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Sat, 7 Sep 2024 16:48:35 -0700 Subject: [PATCH 4/6] removed unneeded --- docker/load_mongodump.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/docker/load_mongodump.sh b/docker/load_mongodump.sh index 1132e0c..022e3c5 100755 --- a/docker/load_mongodump.sh +++ b/docker/load_mongodump.sh @@ -68,6 +68,3 @@ echo "Restoring the dump from $FILE_NAME to database $DB_NAME" docker exec -e MONGODUMP_FILE=$FILE_NAME op-admin-dashboard-db-1 bash -c "cd /tmp && tar xvf $FILE_NAME && mongorestore -d $DB_NAME dump/$DB_NAME" echo "Database restore complete." - -# Clean up temporary directory -rm -rf "$TEMP_DIR" From 857bd42a882bd26fa0450d3906c002f19e0265b4 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Sun, 8 Sep 2024 00:34:06 -0700 Subject: [PATCH 5/6] Minor change to simplify clear/drop --- docker-compose-dev.yml | 2 +- docker-compose-dev.yml.bak | 51 ++++++++++++++++++++++++++++++++++++++ docker/load_mongodump.sh | 2 +- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 docker-compose-dev.yml.bak diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index df44e28..95d3f6b 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -14,7 +14,7 @@ services: DASH_DEBUG_MODE: "True" DASH_SILENCE_ROUTES_LOGGING: "False" DASH_SERVER_PORT: 8050 - DB_HOST: db + DB_HOST: mongodb://db/openpath_prod_ca_ebike WEB_SERVER_HOST: 0.0.0.0 SERVER_BRANCH: master CONFIG_PATH: "https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/" diff --git a/docker-compose-dev.yml.bak b/docker-compose-dev.yml.bak new file mode 100644 index 0000000..df44e28 --- /dev/null +++ b/docker-compose-dev.yml.bak @@ -0,0 +1,51 @@ +# docker-compose.yml +version: "3" +services: + dashboard: + build: + context: . + dockerfile: docker/Dockerfile + args: + SERVER_IMAGE_TAG: ${SERVER_IMAGE_TAG} + image: e-mission/opdash:0.0.1 + ports: + - "8050:8050" + environment: + DASH_DEBUG_MODE: "True" + DASH_SILENCE_ROUTES_LOGGING: "False" + DASH_SERVER_PORT: 8050 + DB_HOST: db + WEB_SERVER_HOST: 0.0.0.0 + SERVER_BRANCH: master + CONFIG_PATH: "https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/" + STUDY_CONFIG: "stage-program" + AUTH_TYPE: "basic" # the other option is cognito + REACT_VERSION: "18.2.0" + networks: + - emission + volumes: + - ./pages:/usr/src/app/pages + - ./utils:/usr/src/app/utils + - ./app.py:/usr/src/app/app.py + - ./app_sidebar_collapsible.py:/usr/src/app/app_sidebar_collapsible.py + deploy: + restart_policy: + condition: on-failure + db: + image: mongo:4.4.0 + deploy: + replicas: 1 + restart_policy: + condition: on-failure + volumes: + - mongo-data:/data/db + networks: + - emission + ports: + - "27017:27017" + +networks: + emission: + +volumes: + mongo-data: diff --git a/docker/load_mongodump.sh b/docker/load_mongodump.sh index 022e3c5..a6f8863 100755 --- a/docker/load_mongodump.sh +++ b/docker/load_mongodump.sh @@ -62,7 +62,7 @@ docker cp "$MONGODUMP_FILE" op-admin-dashboard-db-1:/tmp FILE_NAME=$(basename "$MONGODUMP_FILE") echo "Clearing existing database" -docker exec op-admin-dashboard-db-1 bash -c 'mongo --eval "db.getMongo().getDBNames().forEach(function(d) { if (d !== \"admin\" && d !== \"local\") db.getSiblingDB(d).dropDatabase(); })"' +docker exec op-admin-dashboard-db-1 bash -c "mongo $DB_NAME --eval 'db.dropDatabase()'" echo "Restoring the dump from $FILE_NAME to database $DB_NAME" docker exec -e MONGODUMP_FILE=$FILE_NAME op-admin-dashboard-db-1 bash -c "cd /tmp && tar xvf $FILE_NAME && mongorestore -d $DB_NAME dump/$DB_NAME" From 6aa5017f1bc0b9786a2fc30054c8e12d1683b382 Mon Sep 17 00:00:00 2001 From: Robin Date: Sun, 8 Sep 2024 00:34:50 -0700 Subject: [PATCH 6/6] Delete docker-compose-dev.yml.bak --- docker-compose-dev.yml.bak | 51 -------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 docker-compose-dev.yml.bak diff --git a/docker-compose-dev.yml.bak b/docker-compose-dev.yml.bak deleted file mode 100644 index df44e28..0000000 --- a/docker-compose-dev.yml.bak +++ /dev/null @@ -1,51 +0,0 @@ -# docker-compose.yml -version: "3" -services: - dashboard: - build: - context: . - dockerfile: docker/Dockerfile - args: - SERVER_IMAGE_TAG: ${SERVER_IMAGE_TAG} - image: e-mission/opdash:0.0.1 - ports: - - "8050:8050" - environment: - DASH_DEBUG_MODE: "True" - DASH_SILENCE_ROUTES_LOGGING: "False" - DASH_SERVER_PORT: 8050 - DB_HOST: db - WEB_SERVER_HOST: 0.0.0.0 - SERVER_BRANCH: master - CONFIG_PATH: "https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/" - STUDY_CONFIG: "stage-program" - AUTH_TYPE: "basic" # the other option is cognito - REACT_VERSION: "18.2.0" - networks: - - emission - volumes: - - ./pages:/usr/src/app/pages - - ./utils:/usr/src/app/utils - - ./app.py:/usr/src/app/app.py - - ./app_sidebar_collapsible.py:/usr/src/app/app_sidebar_collapsible.py - deploy: - restart_policy: - condition: on-failure - db: - image: mongo:4.4.0 - deploy: - replicas: 1 - restart_policy: - condition: on-failure - volumes: - - mongo-data:/data/db - networks: - - emission - ports: - - "27017:27017" - -networks: - emission: - -volumes: - mongo-data: