Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active December 7, 2018 14:26
Show Gist options
  • Select an option

  • Save dosaboy/2b423dcc090da4bbab0cb146d8a382c1 to your computer and use it in GitHub Desktop.

Select an option

Save dosaboy/2b423dcc090da4bbab0cb146d8a382c1 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
#
# Origin: https://gist.github.com/dosaboy/2b423dcc090da4bbab0cb146d8a382c1
#
# Authors:
# - [email protected]
# - [email protected]
#
# Description:
#
# Script to check and restore percona-charm bootstrap uuid consistency on
# relations.
#
# To run do:
#
# . ./restore_percona_charm_bootstrap_uuid.sh
# get_units <- this is read-only
# check_units <- this is read-only
# check_db_status <- this is read-only
# update_peer_relations <- only do this when you are sure the charm is ready
# update_leader_settings <- only do this when you are sure the charm is ready
# resolve_errors <- only do this when you are sure the charm is ready
# check_db_status <- this is read-only
_get_leader ()
{
local datafile=$1
local unit=$2
cat << EOF | python
import json
import re
for line in open("$datafile").readlines():
j = json.loads(line)
for d in j:
for k ,v in d.iteritems():
if k == 'UnitId':
unit = v
elif str(v.strip()) == 'True':
print "{}".format(unit)
EOF
}
show_uuid ()
{
local datafile=$1
local related_unit=$2
local bs_uuid=$3
cat << EOF | python
import json
import re
for line in open("$datafile").readlines():
j = json.loads(line)
for d in j:
for k ,v in d.iteritems():
if k == "UnitId":
if "$related_unit":
print "\n{} <- {}".format(v, "$related_unit")
else:
print "\n{}".format(v)
continue
elif k == "Stdout":
try:
uuid = json.loads(v)
except ValueError:
uuid = v
if uuid == "$bs_uuid":
print "\033[32m{}\033[0m".format(uuid)
else:
print "\033[33m{}\033[0m".format(uuid)
EOF
}
# Install deps
which jq &>/dev/null || sudo apt install jq -y
get_leader()
{
local out=`mktemp`
juju run --format=json --application mysql is-leader --format=json > $out
local leader=`_get_leader $out`
echo "Leader is $leader" 1>&2
rm $out
echo $leader
}
get_units ()
{
# Get all mysql units and cluster relation id
readarray -t mysql_units<<<"`juju status mysql --format=json| jq -er '.applications[].units'| grep -v null| jq -r 'keys[]'`"
echo "${#mysql_units[@]} mysql units: ${mysql_units[@]}" 1>&2
echo ${mysql_units[@]}
}
get_ondisk_bootstrap_uuid ()
{
# Get current on-disk uuid
leader=`get_leader`
local bootstrap_uuid=`juju run --unit $leader 'grep uuid: /var/lib/percona-xtradb-cluster/grastate.dat'| awk '{print $2}'`
echo "On-disk (grastate.dat) bootstrap-uuid: $bootstrap_uuid" 1>&2
echo $bootstrap_uuid
}
check_units ()
{
local bootstrap_uuid=`get_ondisk_bootstrap_uuid`
local out=`mktemp`
local mysql_units=( `get_units` )
local cluster_rid=`juju run --unit ${mysql_units[0]} 'relation-ids cluster'`
for unit in ${mysql_units[@]}; do
juju run --format=json --application mysql "relation-get --format=json -r $cluster_rid bootstrap-uuid $unit" > $out
show_uuid $out $unit $bootstrap_uuid
done
echo -e "\nLeader settings (all units):"
juju run --format=json --application mysql "leader-get --format=json bootstrap-uuid" > $out
show_uuid $out '' $bootstrap_uuid
rm $out
}
resolve_errors ()
{
for unit in `get_units`; do
juju resolve $unit --no-retry
done
}
update_peer_relations ()
{
local bootstrap_uuid=`get_ondisk_bootstrap_uuid`
local mysql_units=( `get_units` )
local cluster_rid=`juju run --unit ${mysql_units[0]} 'relation-ids cluster'`
juju run --application mysql "relation-set -r $cluster_rid bootstrap-uuid=$bootstrap_uuid"
}
update_leader_settings ()
{
local bootstrap_uuid=`get_ondisk_bootstrap_uuid`
local leader=`get_leader`
juju run --unit $leader "leader-set bootstrap-uuid=$bootstrap_uuid"
}
check_db_status ()
{
local leader=`get_leader`
pass=`juju run --unit $leader 'leader-get root-password'`
juju ssh $leader "mysql -uroot -p$pass -e \"show status like 'wsrep_cluster%';\""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment