Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #92 from galal-hussein/fix_mongo
Browse files Browse the repository at this point in the history
Fix mongo cattle template
  • Loading branch information
cloudnautique authored Feb 27, 2017
2 parents f0bc9c4 + e65f0fb commit 914a5c3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
5 changes: 2 additions & 3 deletions MongoDB/0.1.0/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mongo-cluster:
restart: always
environment:
MONGO_SERVICE_NAME: mongo-cluster
tty: true
CATTLE_SCRIPT_DEBUG:
entrypoint: /opt/rancher/bin/entrypoint.sh
command:
- --replSet
Expand All @@ -16,11 +16,10 @@ mongo-cluster:
- mongo-base
mongo-base:
restart: always
tty: true
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.start_once: true
image: rancher/mongodb-conf:v0.1.0
image: rancher/mongodb-conf:v0.2.0
stdin_open: true
entrypoint: /bin/true
mongo-datavolume:
Expand Down
19 changes: 6 additions & 13 deletions MongoDB/containers/0.1.0/mongodb-config/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
FROM alpine:3.1
MAINTAINER Hussein Galal

# install giddyup
RUN apk add -U curl \
&& mkdir -p /opt/rancher/bin \
&& curl -L https://github.com/cloudnautique/giddyup/releases/download/v0.14.0/giddyup -o /opt/rancher/bin/giddyup \
&& chmod u+x /opt/rancher/bin/*

ADD ./*.sh /opt/rancher/bin/
RUN chmod u+x /opt/rancher/bin/*.sh

# install dig and jq
RUN apk add -U alpine-sdk \
&& curl ftp://ftp.isc.org/isc/bind9/9.10.2/bind-9.10.2.tar.gz|tar -xzv \
&& cd bind-9.10.2 \
&& CFLAGS="-static" ./configure --without-openssl --disable-symtable \
&& make \
&& cp ./bin/dig/dig /opt/rancher/bin/ \
&& curl -L https://github.com/cloudnautique/giddyup/releases/download/v0.8.0/giddyup -o /opt/rancher/bin/giddyup \
&& chmod u+x /opt/rancher/bin/* \
&& apk del alpine-sdk \
&& rm -rf /var/cache/apk/* \
&& rm -rf /bind-9.10.2/

VOLUME /opt/rancher/bin
24 changes: 12 additions & 12 deletions MongoDB/containers/0.1.0/mongodb-config/connect.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/bin/bash
DIG=/opt/rancher/bin/dig

if [ -n "$CATTLE_SCRIPT_DEBUG" ]; then
set -x
fi

GIDDYUP=/opt/rancher/bin/giddyup

function cluster_init {
sleep 10
MYIP=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1 | sed -n 2p)
$DIG A $MONGO_SERVICE_NAME +short > ips.tmp
MYIP=$($GIDDYUP ip myip)
mongo --eval "printjson(rs.initiate())"
for member in $(cat ips.tmp); do
if [ $member != $MYIP ]; then
for member in $($GIDDYUP ip stringify --delimiter " "); do
if [ "$member" != "$MYIP" ]; then
mongo --eval "printjson(rs.add('$member:27017'))"
sleep 5
fi
Expand All @@ -16,9 +20,8 @@ function cluster_init {
}

function find_master {
$DIG A $MONGO_SERVICE_NAME +short > ips.tmp
for IP in $(cat ips.tmp); do
IS_MASTER=`mongo --host $IP --eval "printjson(db.isMaster())" | grep 'ismaster'`
for member in $($GIDDYUP ip stringify --delimiter " "); do
IS_MASTER=$(mongo --host $member --eval "printjson(db.isMaster())" | grep 'ismaster')
if echo $IS_MASTER | grep "true"; then
return 0
fi
Expand All @@ -27,10 +30,7 @@ function find_master {
}
# Script starts here
# wait for mongo to start
while [ `$DIG A $MONGO_SERVICE_NAME +short | wc -l` -lt 3 ]; do
echo 'mongo instances are less than 3.. waiting!'
sleep 5
done
$GIDDYUP service wait scale --timeout 120

# Wait until all services are up
sleep 10
Expand Down
6 changes: 4 additions & 2 deletions MongoDB/containers/0.1.0/mongodb-config/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

if [ -n "$CATTLE_SCRIPT_DEBUG" ]; then
set -x
fi

# Check for lowest ID
sleep 10
/opt/rancher/bin/giddyup leader check
Expand All @@ -10,7 +14,6 @@ else

# Run the scaling script
/opt/rancher/bin/scaling.sh &

# Start mongodb
if [ $? -ne 0 ]
then
Expand All @@ -33,7 +36,6 @@ if [ "$1" = 'mongod' ]; then

exec gosu mongodb "$@"
fi

exec "$@"

fi
14 changes: 9 additions & 5 deletions MongoDB/containers/0.1.0/mongodb-config/scaling.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/bash

if [ -n "$CATTLE_SCRIPT_DEBUG" ]; then
set -x
fi

sleep 5
DIG=/opt/rancher/bin/dig
GIDDYUP=/opt/rancher/bin/giddyup

function scaleup {
MYIP=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1 | sed -n 2p)
$DIG A $MONGO_SERVICE_NAME +short > ips.tmp
for IP in $(cat ips.tmp); do
MYIP=$($GIDDYUP ip myip)
for IP in $($GIDDYUP ip stringify --delimiter " "); do
IS_MASTER=$(mongo --host $IP --eval "printjson(db.isMaster())" | grep 'ismaster')
if echo $IS_MASTER | grep "true"; then
mongo --host $IP --eval "printjson(rs.add('$MYIP:27017'))"
Expand All @@ -16,6 +20,6 @@ function scaleup {
}

# Script starts here
if [ $($DIG A $MONGO_SERVICE_NAME +short | wc -l) -gt 3 ]; then
if [ $($GIDDYUP service scale --current) -gt 3 ]; then
scaleup
fi

0 comments on commit 914a5c3

Please sign in to comment.