Last active
August 29, 2015 14:07
-
-
Save jjo/198e27c8e44f68724fcd to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Manually setup juju mongodb clustering when failed from unresolvable hostnames, | |
# eg when using MaaS + LXC provisioning, see lp#1274947 | |
set -u | |
SERVICE=${1:-mongodb} | |
echo "*** $SERVICE: checking clustered units..." | |
replset=$(juju get $SERVICE| python -c 'import yaml, sys;print yaml.load(sys.stdin)["settings"]["replicaset"]["value"]') | |
units=$(juju status $SERVICE | \ | |
python -c 'import yaml, sys;print " ".join(sorted(yaml.load(sys.stdin)["services"]["'$SERVICE'"]["units"].keys()))') | |
# set positional parameters to $units, for easier handling (shift, $1, $2, etc) | |
set -- $units | |
echo "units: $*" | |
# Select 1st as master | |
master=$1 | |
n_total=$# | |
shift | |
rs_ok=$(juju run --unit=$master 'mongo -quiet --eval "rs.status().ok"') | |
echo "*** $SERVICE: Checking rs_ok($rs_ok) == 1 ..." | |
if [ "$rs_ok" -eq 1 ];then | |
echo "=> Ok." | |
else | |
echo "Trying to fix $SERVICE cluster:" | |
echo "temporary setting a resolvable hostname and restarting mongod" | |
(set -xe | |
ip=$(juju run --unit=$master 'unit-get private-address') | |
juju run --unit=$master 'mongo -quiet --eval "printjson(rs.initiate({\"_id\": \"'$replset'\", \"members\": [ {\"_id\": 0, \"host\": \"'$ip':27017\" }] }))"' | |
) || exit 1 | |
fi | |
typeset -i retries=10 | |
until [ $retries -eq 0 ];do | |
n_replicas=$(juju run --unit=$master 'mongo -quiet --eval "rs.status().members.length"') | |
echo "*** $SERVICE: Checking n_replicas($n_replicas) == n_total($n_total) ; $retries retries left ..." | |
if [ "$n_replicas" -eq "$n_total" ];then | |
echo "=> Ok." | |
break | |
else | |
echo "Need to add replicas ..." | |
(set -x | |
juju run --unit=$master 'mongo -quiet --eval "cfg=rs.conf();cfg.members[0].host=\"$(unit-get private-address):27017\";printjson(rs.reconfig(cfg))"' | |
) | |
for replica in $*;do | |
ip=$(juju run --unit=$replica 'unit-get private-address') | |
(set -x | |
juju run --unit=$master 'mongo -quiet --eval "rs.add(\"'$ip':27017\");"' | |
) | |
done | |
fi | |
retries=retries-1 | |
sleep 10 | |
done | |
# exit ok if there were retries left | |
[ $retries -gt 0 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
E.g. run output