Skip to content

Commit

Permalink
Remove mongo initator
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi Zuccarelli authored and Colum Foskin committed Aug 10, 2017
1 parent c32f89b commit 4e5634d
Show file tree
Hide file tree
Showing 23 changed files with 672 additions and 1,825 deletions.
42 changes: 42 additions & 0 deletions 2.4/Dockerfile.version
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM centos:centos7

LABEL io.k8s.description="MongoDB is a scalable, high-performance, open source NoSQL database." \
io.k8s.display-name="MongoDB 2.4" \
io.openshift.expose-services="27017:mongodb" \
io.openshift.tags="database,mongodb,mongodb24"

ENV MONGODB_VERSION=2.4 \
# Set paths to avoid hard-coding them in scripts.
HOME=/var/lib/mongodb \
CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/mongodb \
# Incantations to enable Software Collections on `bash` and `sh -i`.
ENABLED_COLLECTIONS=mongodb24 \
BASH_ENV="\${CONTAINER_SCRIPTS_PATH}/scl_enable" \
ENV="\${CONTAINER_SCRIPTS_PATH}/scl_enable" \
PROMPT_COMMAND=". \${CONTAINER_SCRIPTS_PATH}/scl_enable"

EXPOSE 27017


ENTRYPOINT ["container-entrypoint"]
CMD ["run-mongod"]

RUN yum install -y centos-release-scl && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar v8314 mongodb24-mongodb mongodb24" && \
yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
yum clean all && \
mkdir -p /var/lib/mongodb/data && chown -R mongodb.0 /var/lib/mongodb/ && \
# Loosen permission bits to avoid problems running container with arbitrary UID
chmod -R a+rwx /opt/rh/mongodb24/root/var/lib/mongodb && \
chmod -R g+rwx /var/lib/mongodb

VOLUME ["/var/lib/mongodb/data"]

ADD root /

# Container setup
RUN touch /etc/mongod.conf && chown mongodb:0 /etc/mongod.conf && /usr/libexec/fix-permissions /etc/mongod.conf

USER 184
LABEL io.openshift.builder-version="321f0e8"
Empty file modified 3.2/root/help.1
100644 → 100755
Empty file.
4 changes: 1 addition & 3 deletions 3.2/root/usr/bin/run-mongod
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ function unset_env_vars() {

function cleanup() {
echo "=> Shutting down MongoDB server ..."
if pgrep mongod; then
pkill -2 mongod
fi
pkill -INT mongod || :
wait_for_mongo_down
exit 0
}
Expand Down
22 changes: 1 addition & 21 deletions 3.2/root/usr/bin/run-mongod-replication
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,6 @@ function unset_env_vars() {
MONGODB_FHREPORTING_USER MONGODB_FHREPORTING_PASSWORD MONGODB_FHREPORTING_DATABASE
}

function usage() {
echo "You must specify the following environment variables:"
echo " MONGODB_ADMIN_PASSWORD"
echo " MONGODB_KEYFILE_VALUE"
echo " MONGODB_REPLICA_NAME"
echo "Optionally you can provide settings for user with 'readWrite' role:"
echo " MONGODB_USER"
echo " MONGODB_PASSWORD"
echo " MONGODB_DATABASE"
echo "Optional variables:"
echo " MONGODB_SERVICE_NAME (default: mongodb)"
echo "MongoDB settings:"
echo " MONGODB_QUIET (default: true)"
exit 1
}

function cleanup() {
echo "=> Shutting down MongoDB server ..."
pkill -INT mongod || :
Expand All @@ -64,11 +48,7 @@ function cleanup() {

trap 'cleanup' SIGINT SIGTERM

[[ -v MONGODB_ADMIN_PASSWORD ]] || usage
if [[ -v MONGODB_USER || -v MONGODB_PASSWORD || -v MONGODB_DATABASE ]]; then
[[ -v MONGODB_USER && -v MONGODB_PASSWORD && -v MONGODB_DATABASE ]] || usage
export CREATE_USER=1
fi
REPLICATION=1 check_env_vars

setup_wiredtiger_cache ${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template

Expand Down
Empty file modified 3.2/root/usr/share/container-scripts/mongodb/README.md
100644 → 100755
Empty file.
88 changes: 73 additions & 15 deletions 3.2/root/usr/share/container-scripts/mongodb/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ function _wait_for_mongo() {
local mongo_cmd="mongo admin --host ${2:-localhost} "

local i
# Wait indefinitely for MongoDB daemon
while true
do
for i in $(seq $MAX_ATTEMPTS); do
echo "=> ${2:-} Waiting for MongoDB daemon ${message}"
if ([[ ${operation} -eq 1 ]] && ${mongo_cmd} --eval "quit()" &>/dev/null) || ([[ ${operation} -eq 0 ]] && ! ${mongo_cmd} --eval "quit()" &>/dev/null); then
echo "=> MongoDB daemon is ${message}"
Expand All @@ -56,18 +54,21 @@ function _wait_for_mongo() {
return 1
}

# endpoints returns a list of hosts to be part of a replica set. Host names are
# generated from MONGODB_SERVICE_NAME, appending a suffix based on the number of
# replicas defined in MONGODB_INITIAL_REPLICA_COUNT. For each host name, there
# should be a service with the same name, so that the name points to a valid DNS
# entry. Example output, where:
# MONGODB_SERVICE_NAME=mongodb
# MONGODB_INITIAL_REPLICA_COUNT=3
# METADATA_NAMESPACE=3node-mbaas:
#
# mongodb-1.3node-mbaas
# mongodb-2.3node-mbaas
# mongodb-3.3node-mbaas
function wait_for_service() {
for x in {1..100}; do
test=$(dig $1 A +short +search)
if [ "$test" != "" ]; then
break
fi
sleep 1;
echo "=> Waiting for $1 service"
done
return 0
}

# endpoints returns list of IP addresses with other instances of MongoDB
# To get list of endpoints, you need to have headless Service named 'mongodb'.
# NOTE: This won't work with standalone Docker container.
function endpoints() {
service_name=${MONGODB_SERVICE_NAME:-mongodb}
dig $(hostname -f | grep -o mongodb-[1-9]) A +search +short 2>/dev/null
Expand Down Expand Up @@ -208,6 +209,63 @@ function setup_wiredtiger_cache() {
info "wiredTiger cacheSizeGB set to ${cache_size}"
}

# check_env_vars checks environmental variables
# if variables to create non-admin user are provided, sets CREATE_USER=1
# if REPLICATION variable is set, checks also replication variables
function check_env_vars() {
local readonly database_regex='^[^/\. "$]*$'

[[ -v MONGODB_ADMIN_PASSWORD ]] || usage "MONGODB_ADMIN_PASSWORD has to be set."

if [[ -v MONGODB_USER || -v MONGODB_PASSWORD || -v MONGODB_DATABASE ]]; then
[[ -v MONGODB_USER && -v MONGODB_PASSWORD && -v MONGODB_DATABASE ]] || usage "You have to set all or none of variables: MONGODB_USER, MONGODB_PASSWORD, MONGODB_DATABASE"

[[ "${MONGODB_DATABASE}" =~ $database_regex ]] || usage "Database name must match regex: $database_regex"
[ ${#MONGODB_DATABASE} -le 63 ] || usage "Database name too long (maximum 63 characters)"

export CREATE_USER=1
fi

if [[ -v REPLICATION ]]; then
[[ -v MONGODB_KEYFILE_VALUE && -v MONGODB_REPLICA_NAME ]] || usage "MONGODB_KEYFILE_VALUE and MONGODB_REPLICA_NAME have to be set"
fi
}

# usage prints info about required enviromental variables
# if $1 is passed, prints error message containing $1
# if REPLICATION variable is set, prints also info about replication variables
function usage() {
if [ $# == 1 ]; then
echo >&2 "error: $1"
fi

echo "
You must specify the following environment variables:
MONGODB_ADMIN_PASSWORD
Optionally you can provide settings for a user with 'readWrite' role:
(Note you MUST specify all three of these settings)
MONGODB_USER
MONGODB_PASSWORD
MONGODB_DATABASE
Optional settings:
MONGODB_QUIET (default: true)"

if [[ -v REPLICATION ]]; then
echo "
For replication you must also specify the following environment variables:
MONGODB_KEYFILE_VALUE
MONGODB_REPLICA_NAME
Optional settings:
MONGODB_SERVICE_NAME (default: mongodb)
"
fi
echo "
For more information see /usr/share/container-scripts/mongodb/README.md
within the container or visit https://github.com/sclorgk/mongodb-container/."

exit 1
}

# info prints a message prefixed by date and time.
function info() {
printf "=> [%s] %s\n" "$(date +'%a %b %d %T')" "$*"
Expand Down
26 changes: 19 additions & 7 deletions 3.2/root/usr/share/container-scripts/mongodb/init-replset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ function initiate() {

info "Waiting for PRIMARY status ..."
mongo --eval "while (!rs.isMaster().ismaster) { sleep(100); }" --quiet

mongo_create_admin
#[[ -v CREATE_USER ]] && mongo_create_user "-u admin -p ${MONGODB_ADMIN_PASSWORD}"

info "Creating MongoDB users ..."
mongo_create_admin
setUpDatabases "-u admin -p ${MONGODB_ADMIN_PASSWORD}"
[[ -v CREATE_USER ]] && mongo_create_user "-u admin -p ${MONGODB_ADMIN_PASSWORD}"

setUpDatabases "-u admin -p ${MONGODB_ADMIN_PASSWORD}"
info "Successfully initialized replica set"
}

Expand All @@ -47,16 +48,27 @@ function initiate() {
# - MONGODB_REPLICA_NAME
# - MONGODB_ADMIN_PASSWORD
function add_member() {

# add wait for the mongodb-1 service
wait_for_service mongodb-1

# wait for mongodb daemon to come up
wait_for_mongo_up mongodb-1


local host="$1"
info "Adding ${host} to replica set ..."

if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "$(replset_addr)" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
# give mongodb-1 time to settle
# sleep 30;

if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "mongodb-1" --eval "while (!rs.add('${host}').ok) { sleep(200); }" --quiet; then
info "ERROR: couldn't add host to replica set!"
return 1
fi

info "Waiting for PRIMARY/SECONDARY status ..."
mongo --eval "while (!rs.isMaster().ismaster && !rs.isMaster().secondary) { sleep(100); }" --quiet
mongo --eval "while (!rs.isMaster().ismaster && !rs.isMaster().secondary) { sleep(200); }" --quiet

info "Successfully joined replica set"
}
Expand All @@ -76,10 +88,10 @@ fi
# "mongodb-0" -> "0"
# "mongodb-1" -> "1"
# "mongodb-2" -> "2"
readonly MEMBER_ID="${HOSTNAME##*-}"
readonly MEMBER_ID="${MEMBER_HOST##*-}"

# Initialize replica set only if we're the first member
if [ "${MEMBER_ID}" = '0' ]; then
if [ "${MEMBER_ID}" = '1' ]; then
initiate "${MEMBER_HOST}"
else
add_member "${MEMBER_HOST}"
Expand Down
76 changes: 0 additions & 76 deletions 3.2/root/usr/share/container-scripts/mongodb/initiate_replica.sh

This file was deleted.

Empty file.
23 changes: 0 additions & 23 deletions 3.2/root/usr/share/container-scripts/mongodb/replica_supervisor.sh

This file was deleted.

Empty file modified 3.2/root/usr/share/container-scripts/mongodb/scl_enable
100644 → 100755
Empty file.
Empty file modified 3.2/root/usr/share/container-scripts/mongodb/test-functions.sh
100644 → 100755
Empty file.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Variables are documented in hack/build.sh.
BASE_IMAGE_NAME = mongodb
VERSIONS = 2.6 3.0-upg 3.2
OPENSHIFT_NAMESPACES = 2.4
VERSIONS = 3.2

# RHMAP adding all versions here to avoid images getting tagged with anything
# other than rhmap/ in hack/build.sh
OPENSHIFT_NAMESPACES = 3.2

# HACK: Ensure that 'git pull' for old clones doesn't cause confusion.
# New clones should use '--recursive'.
.PHONY: $(shell test -f common/common.mk || echo >&2 'Please do "git submodule update --init" first.')


# Include common Makefile code.
include common/common.mk
1 change: 0 additions & 1 deletion common
Submodule common deleted from 763436
Loading

0 comments on commit 4e5634d

Please sign in to comment.