Skip to content

Commit f42cfa4

Browse files
authored
test: add docker-based cluster setup script
1 parent 33fa6b2 commit f42cfa4

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

test/tools/docker-mongodb/Dockerfile

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Stage 1: install general dependencies
2+
# Separating the build into multiple stages lets Docker skip rebuilding previous stages, improving build times e.g. when only updating the entrypoint script
3+
# `docker build --no-cache` to force a full rebuild, e.g. when new server versions are released
4+
5+
FROM ubuntu:bionic as mtools
6+
7+
RUN apt-get update \
8+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
9+
nodejs-dev node-gyp npm curl \
10+
python3-pip python3-dev python3-setuptools python3-wheel \
11+
build-essential libssl1.0-dev \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
RUN pip3 install wheel
15+
RUN pip3 install psutil pymongo mtools
16+
17+
# Stage 2: install m via npm and preload "hot" versions of MongoDB
18+
# This allows a fresh container to skip downloading common versions of MongoDB, at the cost of increased image size
19+
20+
FROM mtools as mongo_preloaded
21+
22+
ENV MONGO_VERSION latest
23+
ARG HOSTNAME
24+
25+
RUN npm install -g m
26+
27+
RUN mkdir /data
28+
29+
# preload mongo binaries
30+
RUN m 3.6
31+
RUN m 4.0
32+
RUN m 4.2
33+
RUN m 4.4
34+
RUN m latest
35+
36+
# Stage 3: add entrypoint script
37+
38+
FROM mongo_preloaded
39+
40+
COPY docker-entrypoint.sh /entrypoint.sh
41+
RUN chmod +x /entrypoint.sh
42+
43+
ENTRYPOINT ["/entrypoint.sh"]
44+
45+
# Example usage:
46+
47+
# 1. Build the docker image and tag it as e.g. `docker-mongodb`
48+
# Then in the same folder as this Dockerfile, run
49+
#
50+
# > docker build -t [--no-cache] docker-mongodb .
51+
# Note: passing --no-cache will force a full rebuild i.e. if a new version of the server is released; otherwise it should be omitted to reduce build time
52+
53+
# 2. Run the appropriate topology
54+
#
55+
# a) single
56+
#
57+
# > docker run --rm -d -p 27017:27017 -e MONGO_VERSION=4.2 -e HOSTNAME=$(hostname) docker-mongodb single
58+
# Note: passing the hostname is required for the networking to work for a single server, but not for replica/sharded
59+
#
60+
# b) replica set
61+
#
62+
# > docker run --rm -d -p 31000-31003:31000-31003 -e MONGO_VERSION=3.6 docker-mongodb replica
63+
#
64+
# c) sharded cluster
65+
#
66+
# > docker run --rm -d -p 51000-51006:51000-51006 -e MONGO_VERSION=latest docker-mongodb sharded
67+
68+
# 3. See what's running
69+
#
70+
# > docker ps
71+
72+
# 4. Follow output
73+
#
74+
# > docker logs -f <container id from docker ps>
75+
76+
# 5. Run arbitrary mlaunch commands
77+
#
78+
# > docker run --rm -d -p 27017:27017 -e MONGO_VERSION=4.2 docker-mongodb mlaunch init --dir /data --bind_ip 0.0.0.0 --hostname $(hostname) --single --setParameter enableTestCommands=1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /bin/bash
2+
set -e
3+
4+
if [ "${1:0:1}" = '-' ]; then
5+
set -- mlaunch "$@"
6+
fi
7+
8+
if [ "$1" = 'mlaunch' ]; then
9+
if [ -f /data/.mlaunch_startup ] ; then
10+
echo 'Already initialized. Ignoring provided command!'
11+
mlaunch start
12+
else
13+
m $MONGO_VERSION
14+
$@
15+
fi
16+
elif [ "$1" = 'single' ]; then
17+
m $MONGO_VERSION
18+
mlaunch init --dir /data --bind_ip 0.0.0.0 --hostname $HOSTNAME --single --setParameter enableTestCommands=1
19+
elif [ "$1" = 'replica' ]; then
20+
m $MONGO_VERSION
21+
mlaunch init --dir /data --bind_ip 0.0.0.0 --replicaset --nodes 3 --arbiter --name rs --port 31000 --enableMajorityReadConcern --setParameter enableTestCommands=1
22+
elif [ "$1" = 'sharded' ]; then
23+
m $MONGO_VERSION
24+
mlaunch init --dir /data --bind_ip 0.0.0.0 --replicaset --nodes 3 --arbiter --name rs --port 51000 --enableMajorityReadConcern --setParameter enableTestCommands=1 --sharded 1 --mongos 2
25+
else
26+
echo "Invalid syntax"
27+
fi
28+
29+
sleep 2
30+
31+
if [ -d /data/rs ]; then
32+
tail -f /data/rs/*/mongod.log
33+
elif [ -d /data/configRepl ]; then
34+
tail -f /data/mongos/mongos_*.log /data/**/**/mongod.log
35+
else
36+
tail -f /data/mongod.log
37+
fi

test/tools/docker_cluster.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
set -e
3+
4+
DOCKER_IMAGE=node-mongodb-native/docker-mongodb
5+
6+
function die_with_usage {
7+
printf "usage:\tdocker_cluster <server|replica_set|sharded_cluster|all> <mongo version>\n\tdocker_cluster killall\n"
8+
exit
9+
}
10+
11+
function docker_mongodb {
12+
if [[ $1 == "replica_set" ]]; then
13+
docker run --name "mongo_${1}_${2}" --rm -d -p 31000-31003:31000-31003 -e MONGO_VERSION=$2 ${DOCKER_IMAGE} replica
14+
echo "mongodb://localhost:31000/?replicaSet=rs"
15+
elif [[ $1 == "sharded_cluster" ]]; then
16+
docker run --name "mongo_${1}_${2}" --rm -d -p 51000-51006:51000-51006 -e MONGO_VERSION=$2 ${DOCKER_IMAGE} sharded
17+
echo "mongodb://localhost:51000,localhost:51001/"
18+
elif [[ $1 == "server" ]]; then
19+
docker run --name "mongo_${1}_${2}" --rm -d -p 27017:27017 -e MONGO_VERSION=$2 -e HOSTNAME=$(hostname) ${DOCKER_IMAGE} single
20+
echo "mongodb://localhost:27017"
21+
elif [[ $1 == "all" ]]; then
22+
docker_mongodb server $2 &
23+
docker_mongodb replica_set $2 &
24+
docker_mongodb sharded_cluster $2 &
25+
wait
26+
return
27+
else
28+
echo "unsupported topology: $1"
29+
die_with_usage
30+
fi
31+
32+
docker ps -f name=mongo_${1}_${2}
33+
34+
printf "\n[ Tailing container logs, Ctrl+C to exit; the container is detached and will continue running until stopped with 'docker kill' ]\n\n"
35+
docker logs -f $(docker ps -f name=mongo_${1}_${2} -q)
36+
}
37+
38+
if [ "$#" -ne 2 ] && [ ${1:-''} != "killall" ]; then
39+
die_with_usage
40+
fi
41+
42+
if [[ $1 == "killall" ]]; then
43+
RUNNING=$(docker ps -f ancestor=${DOCKER_IMAGE} -q)
44+
if [[ $RUNNING ]]; then
45+
docker kill $RUNNING
46+
echo "Killed all running mongo containers"
47+
else
48+
echo "No running mongo containers"
49+
fi
50+
exit
51+
else
52+
if [[ $(docker image ls -q ${DOCKER_IMAGE}) ]]; then
53+
echo "Image already cached, skipping build; force a rebuild with 'docker build --no-cache'"
54+
else
55+
cd "${0%/*}/docker-mongodb"
56+
docker build -t ${DOCKER_IMAGE} .
57+
fi
58+
docker_mongodb $1 $2
59+
fi

0 commit comments

Comments
 (0)