From e65f0fbb897e3e9d9bbe2ed87d1b7ad18a156b9f Mon Sep 17 00:00:00 2001 From: galal-hussein Date: Wed, 4 Jan 2017 19:38:56 +0200 Subject: [PATCH] Use latest Giddyup and fixing bugs --- MongoDB/0.1.0/docker-compose.yml | 5 ++-- .../0.1.0/mongodb-config/Dockerfile | 19 +++++---------- .../0.1.0/mongodb-config/connect.sh | 24 +++++++++---------- .../0.1.0/mongodb-config/entrypoint.sh | 6 +++-- .../0.1.0/mongodb-config/scaling.sh | 14 +++++++---- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/MongoDB/0.1.0/docker-compose.yml b/MongoDB/0.1.0/docker-compose.yml index 7d350be..5ad3cdf 100644 --- a/MongoDB/0.1.0/docker-compose.yml +++ b/MongoDB/0.1.0/docker-compose.yml @@ -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 @@ -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: diff --git a/MongoDB/containers/0.1.0/mongodb-config/Dockerfile b/MongoDB/containers/0.1.0/mongodb-config/Dockerfile index 2dbcb20..dcfae4c 100644 --- a/MongoDB/containers/0.1.0/mongodb-config/Dockerfile +++ b/MongoDB/containers/0.1.0/mongodb-config/Dockerfile @@ -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 diff --git a/MongoDB/containers/0.1.0/mongodb-config/connect.sh b/MongoDB/containers/0.1.0/mongodb-config/connect.sh index 72bdc7c..5e85a53 100644 --- a/MongoDB/containers/0.1.0/mongodb-config/connect.sh +++ b/MongoDB/containers/0.1.0/mongodb-config/connect.sh @@ -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 @@ -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 @@ -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 diff --git a/MongoDB/containers/0.1.0/mongodb-config/entrypoint.sh b/MongoDB/containers/0.1.0/mongodb-config/entrypoint.sh index 2983b2c..8c3291a 100644 --- a/MongoDB/containers/0.1.0/mongodb-config/entrypoint.sh +++ b/MongoDB/containers/0.1.0/mongodb-config/entrypoint.sh @@ -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 @@ -10,7 +14,6 @@ else # Run the scaling script /opt/rancher/bin/scaling.sh & - # Start mongodb if [ $? -ne 0 ] then @@ -33,7 +36,6 @@ if [ "$1" = 'mongod' ]; then exec gosu mongodb "$@" fi - exec "$@" fi diff --git a/MongoDB/containers/0.1.0/mongodb-config/scaling.sh b/MongoDB/containers/0.1.0/mongodb-config/scaling.sh index 664e5d3..8a9b511 100644 --- a/MongoDB/containers/0.1.0/mongodb-config/scaling.sh +++ b/MongoDB/containers/0.1.0/mongodb-config/scaling.sh @@ -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'))" @@ -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