#!/bin/bash set -e cecho() { local code="\033[" case "$1" in black | bk) color="${code}0;30m";; red | r) color="${code}1;31m";; green | g) color="${code}1;32m";; yellow | y) color="${code}1;33m";; blue | b) color="${code}1;34m";; purple | p) color="${code}1;35m";; cyan | c) color="${code}1;36m";; gray | gr) color="${code}0;37m";; *) local text="$1" esac [ -z "$text" ] && local text="$color$2${code}0m" echo -e "$text" } do_delete_instance() { NGINX_AVAILABLE_PATH='/etc/nginx/sites-available' NGINX_ENABLE_PATH='/etc/nginx/sites-enabled' UWSGI_AVAILABLE_PATH='/etc/uwsgi/apps-available' UWSGI_ENABLE_PATH='/etc/uwsgi/apps-enabled' if [ ! -z '$CONFIG_PATH' ]; then CONFIG_PATH="/etc/ishtar/" fi if [ ! -f $CONFIG_PATH/config ]; then echo ""; cecho r ""$CONFIG_PATH" is not a valid config file." echo "Have you properly install Ishtar sources?" echo "Run ishtar-install before this script."; echo ""; exit 1; fi source $CONFIG_PATH/config INSTANCES_FILE=$CONFIG_PATH/instances if [ ! -f $INSTANCES_FILE ]; then echo ""; cecho r "No instance installed. Exiting." echo ""; exit 1; fi echo "" cecho g "*******************************************************************************" cecho g "++++++ Ishtar instance deletion script ++++++" cecho g "*******************************************************************************" echo "" cecho y "Available instances:" echo "" cat $INSTANCES_FILE to_delete='' echo "" while [ "$to_delete" == '' ] do read -p "* Which one would you like to delete? " choice GRP=`cat $INSTANCES_FILE | grep "^$choice$"` if [ "$GRP" != '' ]; then to_delete=$choice fi done echo "" cecho r "Are you really sure to delete configuration, database and media attached to the " cecho r "instance "$to_delete"?" echo "" read -p "* Type DELETE if you are sure: " choice if [ "$choice" != "DELETE" ]; then exit 1 fi systemctl stop uwsgi systemctl stop monit || true INSTANCE=$to_delete rm -rf $ISHTAR_PATH/$INSTANCE rm -f "$NGINX_AVAILABLE_PATH/ishtar-$INSTANCE.conf" rm -f "$NGINX_ENABLE_PATH/ishtar-$INSTANCE.conf" rm -f "$UWSGI_AVAILABLE_PATH/ishtar-$INSTANCE.ini" rm -f "$UWSGI_ENABLE_PATH/ishtar-$INSTANCE.ini" rm -f "/etc/monit/conf-available/celery-$INSTANCE" rm -f "/etc/monit/conf-enabled/celery-$INSTANCE" systemctl start uwsgi || true systemctl start monit || true if [ $USE_CELERY = 'yes' ]; then /usr/bin/supervisorctl stop celery_$INSTANCE || (true && cecho r "/usr/bin/supervisorctl stop celery_$INSTANCE - failed") /usr/bin/supervisorctl remove celery_$INSTANCE || (true && cecho r "/usr/bin/supervisorctl remove celery_$INSTANCE - failed") /usr/sbin/rabbitmqctl delete_vhost /ishtar$INSTANCE || (true && cecho r "/usr/sbin/rabbitmqctl delete_vhost /ishtar$INSTANCE - failed") /usr/sbin/rabbitmqctl delete_user ishtar$INSTANCE || (true && cecho r "/usr/sbin/rabbitmqctl delete_user ishtar$INSTANCE - failed") /usr/bin/supervisorctl reload || (true && cecho r "/usr/bin/supervisorctl reload - failed") fi rm -f /etc/supervisor/conf.d/celery_$INSTANCE.conf DB_NAME="ishtar-$INSTANCE" export DB_NAME su postgres <<'EOF' cd $HOME dropdb "$DB_NAME" || (true && echo 'dropdb $DB_NAME - failed') dropuser "$DB_NAME" || (true && echo 'dropuser $DB_NAME - failed') EOF sed '/'$to_delete'/d' $INSTANCES_FILE > /tmp/new_instances mv /tmp/new_instances $INSTANCES_FILE echo "" cecho g $to_delete" have been completely removed" echo "" } do_delete_instance systemctl start uwsgi || true systemctl start monit || true