Created
May 29, 2023 12:11
-
-
Save sboily/1d025e710766de216c973fc3f73987a3 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
#!/usr/bin/env bash | |
usage() | |
{ | |
echo "WAZO PostgreSQL migrate/install script" | |
echo "options:" | |
echo " -h, --host remote PostgreSQL listening hostname or IPv4" | |
echo " -p, --port remote PostgreSQL listening port [default: 5432]" | |
echo " -u, --username remote PostgreSQL username [default: asterisk]" | |
echo " -s, --password remote PostgreSQL password [default: proformatique]" | |
echo " -d, --database remote PostgreSQL database name to use [default: asterisk]" | |
echo " --clone-db clone local PostgreSQL data to remote instance" | |
echo " --help show this help text" | |
echo | |
} | |
OPTS=`getopt -o h:p:u:s:d: -l help,clone-db -- "$@"` | |
if [[ $? -ne 0 ]]; then | |
exit 1 | |
fi | |
eval set -- "$OPTS" | |
while [ : ]; do | |
case "$1" in | |
-h | --host) | |
db_hostname=$2; | |
shift 2 | |
;; | |
-p | --port) | |
db_port=$2; | |
shift 2 | |
;; | |
-u | --username) | |
db_username=$2; | |
shift 2 | |
;; | |
-s | --password) | |
db_password=$2; | |
shift 2 | |
;; | |
-d | --database) | |
db_name=$2; | |
shift 2 | |
;; | |
--clone-db) | |
db_clone=true; | |
shift 1 | |
;; | |
--help) | |
usage; | |
exit 0 | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
usage; | |
exit 1 | |
esac | |
done | |
if [ -z "${db_hostname}" ]; then | |
echo -e "error: hostname or ipv4 must be supplied using the -h flag\n" | |
usage | |
exit 1 | |
fi | |
if [ -z "${db_port}" ]; then db_port="5432"; fi | |
if [ -z "${db_username}" ]; then db_username="asterisk"; fi | |
if [ -z "${db_password}" ]; then db_password="proformatique"; fi | |
if [ -z "${db_name}" ]; then db_name="asterisk"; fi | |
echo '>> Disabling WAZO services' | |
wazo-service stop | |
if [ "${db_clone}" = true ]; then | |
echo ">> Cloning local postgres database to remote postgres" | |
sudo -u postgres -i pg_dumpall -c > /tmp/wazo-database.sql | |
psql -h ${db_hostname} -p ${db_port} -U postgres -f /tmp/wazo-database.sql > /dev/null 2>&1 | |
fi | |
echo '>> Stopping and disabling local postgres' | |
systemctl stop postgresql | |
systemctl mask postgresql | |
base_uri="postgresql://${db_username}:${db_password}@${db_hostname}:${db_port}/${db_name}" | |
services=( | |
"wazo-agentd" "wazo-agid" "wazo-auth" "wazo-auth-token" "wazo-chatd" "wazo-call-logd" | |
"wazo-confd" "wazo-confgend" "wazo-dird" "wazo-nestbox-plugin" "wazo-upgrade" | |
"wazo-webhookd" "xivo-dao" | |
) | |
echo ">> Configuring services to use remote postgres" | |
for service in ${services[@]}; do | |
conf_dir="/etc/${service}/conf.d" | |
conf_file="${conf_dir}/remote-postgres.yml" | |
if [ ! -d "${conf_dir}" ]; then | |
echo "skipping ${service}: service not installed" | |
continue | |
fi | |
echo "setting up ${service}" | |
echo "db_uri: ${base_uri}?application_name=${service}" > $conf_file | |
case "$service" in | |
wazo-auth) | |
echo "confd_db_uri: ${base_uri}?application_name=${service}" >> $conf_file;; | |
wazo-call-logd) | |
echo "cel_db_uri: ${base_uri}?application_name=${service}" >> $conf_file;; | |
esac | |
done | |
echo ">> Patching xivo-manage-db (alembic.ini)" | |
sed -i -e "s|^\(sqlalchemy.url\).*|#&\n\1 = ${base_uri}|" /usr/share/xivo-manage-db/alembic.ini | |
echo ">> Patching ODBC" | |
sed -i -e "s|^\(Servername\).*|#&\n\1=${db_hostname}|" /etc/odbc.ini | |
echo ">> Restarting WAZO services" | |
wazo-service start | |
echo ">> All done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment