Skip to content

Instantly share code, notes, and snippets.

@crizstian
Created April 12, 2019 21:27
Show Gist options
  • Save crizstian/2e3ec75c86f0b19e0e29054e3f85274e to your computer and use it in GitHub Desktop.
Save crizstian/2e3ec75c86f0b19e0e29054e3f85274e to your computer and use it in GitHub Desktop.

Revisions

  1. crizstian created this gist Apr 12, 2019.
    61 changes: 61 additions & 0 deletions start.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/bin/bash

    exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

    . /etc/environment
    . /var/mongo/hosts.sh

    # @params container volume
    function recreateContainer {
    serv=$(howManyServers)
    keyfile="mongo-keyfile"
    port="$DB_PORT:$DB_PORT"
    p=$DB_PORT

    echo '·· recreating container >>>> '$1' ··'

    #create container with sercurity and replica configuration
    docker run --restart=unless-stopped --name $1 --hostname $1 \
    -v mongo_storage:/data \
    $serv \
    -p $port \
    -d mongo --smallfiles \
    --keyFile /data/keyfile/$keyfile \
    --replSet $DB_REPLSET_NAME \
    --storageEngine wiredTiger \
    --port $p \
    --bind_ip 0.0.0.0

    # verify if container is ready
    wait_for_databases

    echo '·······························'
    echo '· CONTAINER '$1' CREATED ··'
    echo '·······························'
    }

    function wait_for {
    echo ">>>>>>>>>>> waiting for mongodb"
    start_ts=$(date +%s)
    while :
    do
    (echo > /dev/tcp/$1/$2) >/dev/null 2>&1
    result=$?
    if [[ $result -eq 0 ]]; then
    end_ts=$(date +%s)
    echo "<<<<< $1:$2 is available after $((end_ts - start_ts)) seconds"
    sleep 3
    break
    fi
    sleep 5
    done
    }

    function wait_for_databases {
    ip=`ip -f inet addr show eth0 | grep -Po 'inet \K[\d.]+'`
    # make tcp call
    echo "IP == $ip PORT == 27017"
    wait_for "$ip" 27017
    }

    recreateContainer $DB_HOST