Created
June 8, 2018 12:31
-
-
Save kedazo/9abff2308c04b465cdb4940f02b5eccc 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
clustercontrol-controller.postinst | |
#!/usr/bin/env bash | |
# for mysql binary | |
set PATH=${PATH}:/usr/bin | |
echo "*** If you are upgrading from an older version to v1.2.12," | |
echo "*** you must also upgrade the DB schema manually." | |
echo "***" | |
echo "*** Starting from version 1.2.12, cmon will attempt to auto-upgrade the schema" | |
echo "*** so if you upgrading from 1.2.12 or later then you need to only do" | |
echo "*** sudo /etc/init.d/cmon restart" | |
echo "*** and you can check the cmon logs for the results / to make sure" | |
echo "***" | |
echo "*** From version 1.6.2 please put your modified template files" | |
echo "*** into /etc/cmon/templates/" | |
echo "***" | |
echo "*** See also, http://severalnines.com/docs/administration.html#upgrading-clustercontrol" | |
echo "" | |
# these variables are for testing purposes | |
if [ "${ETCDIR}x" == "x" ]; then | |
ETCDIR="/etc" | |
fi | |
if [ "${UIDIR}x" == "x" ]; then | |
if [ -d /var/www/html/clustercontrol ]; then | |
UIDIR="/var/www/html/clustercontrol" | |
else | |
UIDIR="/var/www/clustercontrol" | |
fi | |
fi | |
if [ "${UIDB}x" == "x" ]; then | |
UIDB="dcps" | |
fi | |
if [ ! -f "${ETCDIR}/cmon.cnf" ]; then | |
echo "*** WARNING: ${ETCDIR}/cmon.cnf doesn't exists." | |
exit 0 | |
else | |
# fresh install, no hostname= is set... | |
unset hostname | |
source ${ETCDIR}/cmon.cnf | |
if [ "${hostname}x" == "x" ]; then | |
echo "*** ${ETCDIR}/cmon.cnf not configured yet." | |
exit 0 | |
fi | |
fi | |
# first lets verify if /etc/cmon.cnf has a cluster defined, then | |
# lets move it to /etc/cmon.d/cmon_CLUSTERID.cnf, and create a new /etc/cmon.cnf | |
if grep '^cluster_id' ${ETCDIR}/cmon.cnf >/dev/null 2>/dev/null; then | |
eval $(grep '^cluster_id' ${ETCDIR}/cmon.cnf) | |
echo "***" | |
echo "*** Legacy config file found." | |
echo "*** Moving ${ETCDIR}/cmon.cnf to ${ETCDIR}/cmon.d/cmon_${cluster_id}.cnf" | |
echo "***" | |
mkdir -p ${ETCDIR}/cmon.d | |
# for safety (backup any previous existing) | |
mv -f ${ETCDIR}/cmon.d/cmon_${cluster_id}.cnf \ | |
${ETCDIR}/cmon.d/cmon_${cluster_id}.cnf.bak 2>/dev/null | |
# and now move | |
mv -f ${ETCDIR}/cmon.cnf ${ETCDIR}/cmon.d/cmon_${cluster_id}.cnf | |
# Now generate a new cmon.cnf and preserve few vars from the original conf | |
# Ohh.. we have lots of aliases, that is why this list is soo long | |
KEEPVARS="cmon_user cmondb_user cmon_mysql_port cmondb_port" | |
KEEPVARS="${KEEPVARS} cmon_mysql_hostname cmondb_hostname cmon_mysql_password" | |
KEEPVARS="${KEEPVARS} local_mysql_hostname cmon_local_mysql_hostname" | |
KEEPVARS="${KEEPVARS} cmon_db cmondb_database cmondb_password" | |
# these should be used in the latest installations | |
KEEPVARS="${KEEPVARS} hostname mysql_port mysql_hostname mysql_password" | |
KEEPVARS="${KEEPVARS} cmondb_ssl_ca cmondb_ssl_cert cmondb_ssl_key" | |
echo "*** Generating new ${ETCDIR}/cmon.cnf" | |
touch ${ETCDIR}/cmon.cnf | |
echo "# This file contains the backend DB credentials," >> ${ETCDIR}/cmon.cnf | |
echo "# the hostname of the controller and global token" >> ${ETCDIR}/cmon.cnf | |
for VAR in ${KEEPVARS}; do | |
# simply grep, not doing eval, just copying the line if exists | |
grep "^${VAR}" ${ETCDIR}/cmon.d/cmon_${cluster_id}.cnf 2>/dev/null >> ${ETCDIR}/cmon.cnf | |
done | |
echo "*** done." | |
echo "" | |
fi | |
# now verify if we can connect to the cmon database | |
MYARGS="" | |
if [ -f ${ETCDIR}/cmon.cnf ]; then | |
source ${ETCDIR}/cmon.cnf | |
# default values | |
if [ "${cmon_db}x" == "x" ]; then | |
cmon_db="cmon" | |
fi | |
if [ "${cmon_user}x" == "x" ]; then | |
cmon_user="cmon" | |
fi | |
if [ "${mysql_port}x" == "x" ]; then | |
mysql_port=3306 | |
fi | |
echo "***" | |
echo "*** Checking cmon DB credentials." | |
echo "***" | |
# verify if not empty | |
if [ "${mysql_hostname}x" == "x" -o "${mysql_password}x" == "x" ]; then | |
echo "*** cmon DB credentials are not configured" | |
echo "*** yet in ${ETCDIR}/cmon.cnf." | |
echo "***" | |
exit 0 | |
fi | |
# Password must be handled specially (because of special chars) | |
MYARGS="-u${cmon_user} -h${mysql_hostname} -P${mysql_port}" | |
# Optional SSL CA | |
if [ "${cmondb_ssl_ca}x" != "x" -a -f "${cmondb_ssl_ca}" ]; then | |
MYARGS="${MYARGS} --ssl-ca=${cmondb_ssl_ca}" | |
fi | |
# Optional SSL cert+key | |
if [ "${cmondb_ssl_cert}x" != "x" -a -f "${cmondb_ssl_cert}" -a \ | |
"${cmondb_ssl_key}x" != "x" -a -f "${cmondb_ssl_key}" ]; then | |
MYARGS="${MYARGS} --ssl-cert=${cmondb_ssl_cert} --ssl-key=${cmondb_ssl_key}" | |
fi | |
if mysql ${MYARGS} -B -p''"${mysql_password}"'' ${cmon_db} -e "SELECT 1" >/dev/null; then | |
echo "*** cmon DB credentials are ok in ${ETCDIR}/cmon.cnf" | |
echo "***" | |
else | |
echo "*** ERROR: cmon DB credentials are not ok in ${ETCDIR}/cmon.cnf" | |
echo "***" | |
exit 0 | |
fi | |
fi | |
# match global rpc_key with UI config | |
if [ -f ${ETCDIR}/cmon.cnf -a -f ${UIDIR}/bootstrap.php ]; then | |
source ${ETCDIR}/cmon.cnf | |
UI_RPC_TOKEN=$(sed -n "s/^define('RPC_TOKEN'.*'\(.*\)'.*/\1/p" ${UIDIR}/bootstrap.php) | |
# XXX debug only | |
# echo "*** UI token: '${UI_RPC_TOKEN}', cmon token: '${rpc_key}'" | |
USED_TOKEN="${rpc_key}" | |
if [ "${rpc_key}x" != "x" -a "${UI_RPC_TOKEN}" == "${rpc_key}" ]; then | |
# Everything seems to be right, horray! | |
echo "*** OK: cmon.cnf's rpc_key matches with UI's bootstrap.php token." | |
echo "***" | |
elif [ "${rpc_key}x" == "x" -a "${UI_RPC_TOKEN}x" != "x" ]; then | |
# Case: cmon.cnf doesn't have but UI has a token | |
echo "*** Setting rpc_key in cmon.cnf from UI's bootstrap.php" | |
echo "***" | |
sed -i '/^rpc_key.*/d' ${ETCDIR}/cmon.cnf | |
echo "rpc_key=${UI_RPC_TOKEN}" >> ${ETCDIR}/cmon.cnf | |
# NOTE: cmon requires a restart in this case... | |
USED_TOKEN=${UI_RPC_TOKEN} | |
elif [ "${rpc_key}x" != "x" ]; then | |
# Case: cmon.cnf has a key, but UI's token is not set or different | |
echo "*** Setting RPC_TOKEN in UI's bootstrap.php" | |
echo "***" | |
sed -i '/^define.*RPC_TOKEN.*/d' ${UIDIR}/bootstrap.php | |
echo "define('RPC_TOKEN', '${rpc_key}');" >> ${UIDIR}/bootstrap.php | |
else | |
# Case: token isn't set anywhere... shall we do anything? | |
echo "*** No global RPC token is set/used (either in UI or backend)" | |
echo "***" | |
# NOTE: if we generate & set a key here, cmon requires restart | |
fi | |
if [ "${USED_TOKEN}x" == "x" ]; then | |
# No token is set/required in configs, nothing to do | |
exit 0 | |
fi | |
# lets try to determine the token from UI db | |
UIURL="https://127.0.0.1/cmonapi" | |
UI_APIID=$(mysql ${MYARGS} -p''"${mysql_password}"'' ${UIDB} -se "SELECT id FROM ${UIDB}.apis WHERE url='${UIURL}'" 2>/dev/null) | |
UIDB_TOKEN=$(mysql ${MYARGS} -p''"${mysql_password}"'' ${UIDB} -se "SELECT token FROM ${UIDB}.apis WHERE url='${UIURL}'" 2>/dev/null) | |
if [ "${UI_APIID}x" == "x" ]; then | |
UIURL="http://127.0.0.1/cmonapi" | |
UI_APIID=$(mysql ${MYARGS} -p''"${mysql_password}"'' ${UIDB} -se "SELECT id FROM ${UIDB}.apis WHERE url='${UIURL}'" 2>/dev/null) | |
UIDB_TOKEN=$(mysql ${MYARGS} -p''"${mysql_password}"'' ${UIDB} -se "SELECT token FROM ${UIDB}.apis WHERE url='${UIURL}'" 2>/dev/null) | |
fi | |
echo "*** Checking token in UI database." | |
if [ "${UI_APIID}x" == "x" ]; then | |
echo "*** No relevant entry found in ${UIDB}.apis table." | |
echo "***" | |
exit 0 | |
fi | |
if [ "${UIDB_TOKEN}x" == "${USED_TOKEN}x" ]; then | |
echo "*** OK: token match (config files and ${UIDB}.apis table)" | |
echo "***" | |
elif mysql ${MYARGS} -p''"${mysql_password}"'' ${UIDB} -e \ | |
"UPDATE ${UIDB}.apis SET token='${USED_TOKEN}' WHERE id=${UI_APIID}"; then | |
echo "*** OK: token updated in ${UIDB}.apis table (id=${UI_APIID})" | |
echo "***" | |
else | |
echo "*** ERROR: token update failed in ${UIDB}.apis table." | |
echo "***" | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment