useful_scripts/do_innodb_resolution.sh

70 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
NODE=$1
set -ax
usage() {
echo "Usage: ${0} [node-name]" 1>&2
exit 0
}
do_recovery() {
juju_status=$(mktemp)
juju status --format json > ${juju_status}
MYSQL_UNIT=${NODE}
RW_MYSQL_UNIT=$(jq -r '[.applications."mysql-innodb-cluster".units | to_entries[]| select(.value."workload-status".message | contains("R/W")) | .key] | .[0]' ${juju_status})
PASSWORD=$(juju run --unit mysql-innodb-cluster/leader leader-get mysql.passwd)
MYSQL_UNIT_IP=$(jq -rc '.applications."mysql-innodb-cluster".units."mysql-innodb-cluster/0"."public-address"' ${juju_status})
juju ssh ${MYSQL_UNIT} -- sudo systemctl stop mysql
juju run-action --wait ${RW_MYSQL_UNIT} remove-instance address=${MYSQL_UNIT_IP} force=true
cat > init_mysql.sh << EOF
set -ax
cd /var/lib
mv mysql mysql.old.\$(date +%s)
mkdir mysql
chown mysql:mysql mysql
chmod 700 mysql
mysqld --initialize
systemctl start mysql
EOF
juju scp init_mysql.sh ${MYSQL_UNIT}:init_mysql.sh
juju ssh ${MYSQL_UNIT} -- "chmod +x init_mysql.sh && sudo ./init_mysql.sh"
#exit
cat > set_password.sh << EOF
#!/bin/bash
set -ax
old_password_line=\$(grep 'temporary password' /var/log/mysql/error.log | tail -n 1)
OLD_PASSWORD="\$(echo \${old_password_line} | awk '{print \$13}' | sed -e 's/\r//g')"
cat > /tmp/alter_command << EOF2
ALTER USER 'root'@'localhost' IDENTIFIED BY '${PASSWORD}';
EOF2
mysql -u root -p\${OLD_PASSWORD} -e \"source /tmp/alter_command\"
EOF
juju scp set_password.sh ${MYSQL_UNIT}:set_password.sh
juju ssh ${MYSQL_UNIT} -- "chmod +x set_password.sh && sudo ./set_password.sh"
juju run -u ${MYSQL_UNIT} -- charms.reactive clear_flag local.cluster.user-created
juju run -u ${MYSQL_UNIT} -- charms.reactive clear_flag local.cluster.all-users-created
juju run -u ${MYSQL_UNIT} -- ./hooks/update-status
sleep 10
juju run-action --wait ${RW_MYSQL_UNIT} add-instance address=${MYSQL_UNIT_IP}
}
if [ $# -ne 1 ]; then
usage
else
do_recovery
fi