-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored c mongo download in bash and Node.js.
- Loading branch information
1 parent
a3a8338
commit 0d173a0
Showing
3 changed files
with
123 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
})(); |
This file was deleted.
Oops, something went wrong.