Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split main scripts for standalone server and replication.
- Loading branch information
Marek Skalický
committed
Aug 26, 2016
1 parent
c0a5332
commit df10452
Showing
25 changed files
with
740 additions
and
582 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
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
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,75 @@ | ||
#!/bin/bash | ||
# | ||
# Run mongod which connect to replSet | ||
# $1 - "initiate" (optional) -> this mongod will initiate a replSet | ||
# | ||
# If this script is terminated it removes mongod from replSet | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
source ${CONTAINER_SCRIPTS_PATH}/common.sh | ||
|
||
function usage() { | ||
echo "You must specify the following environment variables:" | ||
echo " MONGODB_USER" | ||
echo " MONGODB_PASSWORD" | ||
echo " MONGODB_DATABASE" | ||
echo " MONGODB_ADMIN_PASSWORD" | ||
echo " MONGODB_REPLICA_NAME" | ||
echo "Optional variables:" | ||
echo " MONGODB_INITIAL_REPLICA_COUNT" | ||
echo "MongoDB settings:" | ||
echo " MONGODB_NOPREALLOC (default: true)" | ||
echo " MONGODB_SMALLFILES (default: true)" | ||
echo " MONGODB_QUIET (default: true)" | ||
exit 1 | ||
} | ||
|
||
# Make sure env variables don't propagate to mongod process. | ||
function unset_env_vars() { | ||
unset MONGODB_USER MONGODB_PASSWORD MONGODB_DATABASE MONGODB_ADMIN_PASSWORD | ||
} | ||
|
||
function cleanup() { | ||
if [ -n "${MONGODB_REPLICA_NAME-}" ]; then | ||
mongo_remove | ||
fi | ||
echo "=> Shutting down MongoDB server ..." | ||
if pgrep mongod; then | ||
pkill -2 mongod | ||
fi | ||
wait_for_mongo_down | ||
exit 0 | ||
} | ||
|
||
trap 'cleanup' SIGINT SIGTERM | ||
|
||
# If user provides own config file use it and do not generate new one | ||
if [ ! -s $MONGODB_CONFIG_PATH ]; then | ||
# Generate config file for MongoDB | ||
envsubst < ${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template > $MONGODB_CONFIG_PATH | ||
fi | ||
|
||
# Need to cache the container address for the cleanup | ||
cache_container_addr | ||
mongo_common_args="-f $MONGODB_CONFIG_PATH" | ||
if ! [[ -v MONGODB_USER && -v MONGODB_PASSWORD && -v MONGODB_DATABASE && -v MONGODB_ADMIN_PASSWORD && -v MONGODB_REPLICA_NAME ]]; then | ||
usage | ||
fi | ||
|
||
# Run the MongoDB in 'replicated' mode | ||
setup_keyfile | ||
|
||
# Run the supervisor in background to manage replset | ||
${CONTAINER_SCRIPTS_PATH}/replset_supervisor.sh "${1:-}" "$$" & | ||
|
||
# Run `unset_env_vars` and `mongod` in a subshell because | ||
# MONGODB_ADMIN_PASSWORD should still be defined when the trapped call to | ||
# `cleanup` references it. | ||
( | ||
unset_env_vars | ||
mongod $mongo_common_args --replSet "${MONGODB_REPLICA_NAME}" | ||
) & | ||
wait |
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
75 changes: 0 additions & 75 deletions
75
2.4/root/usr/share/container-scripts/mongodb/initiate_replica.sh
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
2.4/root/usr/share/container-scripts/mongodb/replica_supervisor.sh
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
2.4/root/usr/share/container-scripts/mongodb/replset_supervisor.sh
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,84 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
source ${CONTAINER_SCRIPTS_PATH}/common.sh | ||
|
||
# Initiate replicaset and exit | ||
if [[ "$1" == "initiate" ]]; then | ||
main_process_id=$2 | ||
current_endpoints=$(endpoints) | ||
if [ -n "${MONGODB_INITIAL_REPLICA_COUNT:-}" ]; then | ||
echo -n "=> Waiting for $MONGODB_INITIAL_REPLICA_COUNT MongoDB endpoints ..." | ||
while [[ "$(echo "${current_endpoints}" | wc -l)" -lt ${MONGODB_INITIAL_REPLICA_COUNT} ]]; do | ||
sleep 2 | ||
current_endpoints=$(endpoints) | ||
done | ||
else | ||
echo "Attention: MONGODB_INITIAL_REPLICA_COUNT is not set and it could lead to a improperly configured replica set." | ||
echo "To fix this, set MONGODB_INITIAL_REPLICA_COUNT variable to the number of members in the replica set in" | ||
echo "the configuration of post deployment hook." | ||
|
||
echo -n "=> Waiting for MongoDB endpoints ..." | ||
while [ -z "${current_endpoints}" ]; do | ||
sleep 2 | ||
current_endpoints=$(endpoints) | ||
done | ||
fi | ||
echo "${current_endpoints}" | ||
|
||
# Let initialize the first member of the cluster | ||
mongo_node="$(echo -n ${current_endpoints} | cut -d ' ' -f 1):${CONTAINER_PORT}" | ||
|
||
echo "=> Waiting for all endpoints to accept connections..." | ||
for node in ${current_endpoints}; do | ||
wait_for_mongo_up ${node} &>/dev/null | ||
done | ||
|
||
echo "=> Initiating the replSet ${MONGODB_REPLICA_NAME} ..." | ||
# This will perform the 'rs.initiate()' command on the current MongoDB. | ||
mongo_initiate "${current_endpoints}" | ||
|
||
echo "=> Creating MongoDB users ..." | ||
mongo_create_admin | ||
mongo_create_user "-u admin -p ${MONGODB_ADMIN_PASSWORD}" | ||
|
||
echo "=> Waiting for replication to finish ..." | ||
# TODO: Replace this with polling or a Mongo script that will check if all | ||
# members of the cluster are now properly replicated (user accounts are | ||
# created on all members). | ||
sleep 10 | ||
|
||
# Some commands will force MongoDB client to re-connect. This is not working | ||
# well in combination with '--eval'. In that case the 'mongo' command will fail | ||
# with return code 254. | ||
echo "=> Initiate Pod giving up the PRIMARY role ..." | ||
mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --quiet --eval "rs.stepDown(120);" &>/dev/null || true | ||
|
||
# Wait till the new PRIMARY member is elected | ||
echo "=> Waiting for the new PRIMARY to be elected ..." | ||
mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --quiet --host ${mongo_node} --eval "var done=false;while(done==false){var members=rs.status().members;for(i=0;i<members.length;i++){if(members[i].stateStr=='PRIMARY' && members[i].name!='$(mongo_addr)'){done=true}};sleep(500)};" &>/dev/null | ||
|
||
# Remove the initialization container MongoDB from cluster and shutdown | ||
echo "=> The new PRIMARY member elected, shutting down current member ..." | ||
mongo_remove | ||
|
||
mongod -f ${MONGODB_CONFIG_PATH} --shutdown &>/dev/null | ||
wait_for_mongo_down | ||
|
||
echo "=> Successfully initialized replSet" | ||
|
||
# Exit this pod | ||
kill ${main_process_id} | ||
|
||
# Try to add node into replicaset | ||
else | ||
echo "=> Waiting for local MongoDB to accept connections ..." | ||
wait_for_mongo_up | ||
set -x | ||
# Add the current container to the replSet | ||
mongo_add | ||
|
||
fi |
Oops, something went wrong.