Skip to content

Commit

Permalink
Refactored c mongo download in bash and Node.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
smebberson committed Jul 5, 2024
1 parent a3a8338 commit 0d173a0
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 156 deletions.
54 changes: 54 additions & 0 deletions bin/c-mongo-download
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

set -e;

SCRIPT_DIR=$(dirname "$0")

report_error() {
echo "Error: $1" >&2
exit 1
}

usage() {
printf "\n\tUsage: c mongo download <environment> [collection]\n\n"
printf "\tDownload all or a specific collection from a Mongo database.\n"
printf "\tIf you don't provide a collection, all will be downloaded.\n\n"
printf "\tOptions:\n\n"
printf "\t-h, --help Display this help message\n\n"
}

if [ "$#" -lt 1 ]; then
usage
exit 1
fi

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
exit 0
fi

ENV=$1
COLLECTION=$2

# Disallow "local" environment
if [ "$ENV" == "local" ]; then
report_error "You cannot download the local database"
fi

CONFIG=$(node "$SCRIPT_DIR/c-mongo-download-config.js" "$ENV" "$COLLECTION")

DOCKER_VOLUME_NAME=$(echo $CONFIG | jq -r '.dockerVolumeName')
OUTPUT_PATH="/data/$(echo $CONFIG | jq -r '.dbName')"
CONNECTION_STRING=$(echo $CONFIG | jq -r '.connectionString')

docker volume create "$DOCKER_VOLUME_NAME" >/dev/null 2>&1

docker run --rm -v "$DOCKER_VOLUME_NAME:/data" busybox sh -c "mkdir -p $OUTPUT_PATH && chmod -R 777 /data"

docker run --rm -v "$DOCKER_VOLUME_NAME:/data" mongo:7 mongodump $CONNECTION_STRING -o $OUTPUT_PATH

docker run --rm -v "$DOCKER_VOLUME_NAME:/data" -v "$(pwd)/data:/host_data" busybox sh -c "cp -r /data/* /host_data"

docker volume rm "$DOCKER_VOLUME_NAME" >/dev/null 2>&1

sudo chown -R "$(id -u):$(id -g)" "$(pwd)/data"
69 changes: 69 additions & 0 deletions bin/c-mongo-download-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { loadConfig } = require('./lib/c');
const { connectionParts } = require('./lib/c-mongo');

const connectionStringWithAddress = ({
address,
name,
params,
password,
user,
}) => {
const url = new URL(address);

url.password = password;
url.username = user;

const cmd = [];

if (params) {
cmd.push(params);
}

return [...cmd, '--uri', `${url.href}/${name}`].join(' ');
};

const connectionStringWithHost = ({ auth, host, name, params }) => {
const cmd = [];

if (auth) {
cmd.push(auth);
}

if (params) {
cmd.push(params);
}

return [...cmd, '--host', host, '-d', name].join(' ');
};

const volumeName = ({ collection, env, name }) => {
const parts = ['mongodump_data', name, env];

if (collection) {
parts.push(collection);
}

return parts.join('_');
};

(async () => {
const [, , env, collection] = process.argv;
const details = await loadConfig(`mongo.${env}`);
const connection = connectionParts(details);
const dockerVolumeName = volumeName({
collection,
env,
name: details.name,
});
const connectionString = connection.host
? connectionStringWithHost(connection)
: connectionStringWithAddress(connection);

console.log(
JSON.stringify({
dbName: details.name,
dockerVolumeName,
connectionString,
})
);
})();
156 changes: 0 additions & 156 deletions bin/c-mongo-download.js

This file was deleted.

0 comments on commit 0d173a0

Please sign in to comment.