Skip to content
Snippets Groups Projects
Commit c4f59b6c authored by max-drexler's avatar max-drexler
Browse files

Dan's suggestions

parent 756f83d1
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
# Sets up a RabbitMQ server's state for the SSEC # # Sets up a RabbitMQ server's state for the SSEC #
############################################################################## ##############################################################################
trap "echo Exited! 1>&2; exit;" SIGINT SIGTERM trap "echo Exited! >&2; exit;" SIGINT SIGTERM
#############################~Functions~###################################### #############################~Functions~######################################
# $1 = error message, $2 = error code, default = 1 # $1 = error message, $2 = error code, default = 1
...@@ -22,7 +22,7 @@ err_exit() { ...@@ -22,7 +22,7 @@ err_exit() {
else else
CODE=$2 CODE=$2
fi fi
echo "$MSG" 1>&2 echo "$MSG" >&2
exit "$CODE" exit "$CODE"
} }
...@@ -44,8 +44,6 @@ usage() { ...@@ -44,8 +44,6 @@ usage() {
} }
##########################~Argument Parsing~################################## ##########################~Argument Parsing~##################################
OPTIND=1 # Reset in case getopts has been used previously in the shell.
config_file="" config_file=""
autorun=0 autorun=0
no_exchanges=0 no_exchanges=0
...@@ -66,22 +64,19 @@ while getopts ":hanr" opt; do ...@@ -66,22 +64,19 @@ while getopts ":hanr" opt; do
r) r)
restart=1 restart=1
;; ;;
? | *) *)
usage usage
exit 1 err_exit "Unknown option -$OPTARG"
;; ;;
esac esac
done done
shift $((OPTIND - 1)) config_file=${!OPTIND}
[ "${1:-}" = "--" ] && shift
config_file=$1
# Check arguments # Check arguments
if [ -z "$config_file" ]; then if [ $OPTIND -ne $# ]; then
usage usage
exit 1 err_exit "Expected config file, got: '${*:OPTIND}'"
fi fi
if ! [ -f "$config_file" ]; then if ! [ -f "$config_file" ]; then
...@@ -89,7 +84,7 @@ if ! [ -f "$config_file" ]; then ...@@ -89,7 +84,7 @@ if ! [ -f "$config_file" ]; then
fi fi
# load admin and user names and passwords # load admin and user names and passwords
source "$config_file" &>/dev/null source "$config_file"
# Check config file # Check config file
if ! [[ -v ADMIN_PASS || -v ADMIN_NAME || -v USER_NAME || -v USER_PASS ]]; then if ! [[ -v ADMIN_PASS || -v ADMIN_NAME || -v USER_NAME || -v USER_PASS ]]; then
...@@ -98,20 +93,20 @@ fi ...@@ -98,20 +93,20 @@ fi
########################~Check Requirements~################################ ########################~Check Requirements~################################
# Check for rabbitmqctl command # Check for rabbitmqctl command
if ! command -v rabbitmqctl --version &>/dev/null; then if ! command -v rabbitmqctl --version >/dev/null 2>&1; then
err_exit "rabbitmqctl command not found! Is RabbitMQ installed?" err_exit "rabbitmqctl command not found! Is RabbitMQ installed?"
fi fi
if ! rabbitmqctl status &>/dev/null; then if ! rabbitmqctl status >/dev/null 2>&1; then
if [ 0 -eq "$autorun" ]; then if [ 0 -eq "$autorun" ]; then
err_exit "RabbitMQ server is not running! Use -a to autostart the server." err_exit "RabbitMQ server is not running! Use -a to autostart the server."
else else
echo "Attempting to start the RabbitMQ server..." echo "Attempting to start the RabbitMQ server..."
rabbitmqctl status &>/dev/null rabbitmqctl status >/dev/null 2>&1
case "$?" in case "$?" in
69) 69)
echo "RabbitMQ node is offline, attempting systemctl start rabbitmq-server..." echo "RabbitMQ node is offline, attempting systemctl start rabbitmq-server..."
if ! systemctl start rabbitmq-server &>/dev/null; then if ! systemctl start rabbitmq-server >/dev/null 2>&1; then
err_exit "Error trying to start RabbitMQ, systemctl error." err_exit "Error trying to start RabbitMQ, systemctl error."
fi fi
;; ;;
...@@ -136,9 +131,9 @@ if ! rabbitmq-plugins enable rabbitmq_management; then ...@@ -136,9 +131,9 @@ if ! rabbitmq-plugins enable rabbitmq_management; then
fi fi
# Check for rabbitmqadmin (install if not found) # Check for rabbitmqadmin (install if not found)
if [ 0 -eq "$no_exchanges" ] && ! command -v rabbitmqadmin --version &>/dev/null; then if [ 0 -eq "$no_exchanges" ] && ! command -v rabbitmqadmin --version >/dev/null 2>&1; then
# Check for python # Check for python
if ! command -v python3 --version &>/dev/null; then if ! command -v python3 --version >/dev/null 2>&1; then
err_exit "python3 not found! Cannot create exchanges without it. (Use -n to skip creating excahnges)" err_exit "python3 not found! Cannot create exchanges without it. (Use -n to skip creating excahnges)"
fi fi
if ! python3 -c "from urllib.request import urlretrieve;urlretrieve('http://$(hostname):15672/cli/rabbitmqadmin', '/usr/local/bin/rabbitmqadmin')"; then if ! python3 -c "from urllib.request import urlretrieve;urlretrieve('http://$(hostname):15672/cli/rabbitmqadmin', '/usr/local/bin/rabbitmqadmin')"; then
...@@ -177,13 +172,12 @@ fi ...@@ -177,13 +172,12 @@ fi
# Set user permissions: # Set user permissions:
# Can't configure or edit anything, but can read all default exchanges + model, misc, & satellite # Can't configure or edit anything, but can read all default exchanges + model, misc, & satellite
rabbitmqctl set_permissions --vhost '/' "$USER_NAME" "" "" "^(model|misc|satellite|amq\.gen.*|amq\.default)$" rabbitmqctl set_permissions "$USER_NAME" "" "" "^(model|misc|satellite|amq\.gen.*|amq\.default)$"
# Remove default account # Remove default account
rabbitmqctl delete_user guest rabbitmqctl delete_user guest
###########################~Set Exchanges~################################### ###########################~Set Exchanges~###################################
if [ 0 -eq "$no_exchanges" ]; then if [ 0 -eq "$no_exchanges" ]; then
echo "Creating exchange 'model'" echo "Creating exchange 'model'"
rabbitmqadmin -u "$ADMIN_NAME" -p "$ADMIN_PASS" -V '/' declare exchange name=model durable=true type=topic rabbitmqadmin -u "$ADMIN_NAME" -p "$ADMIN_PASS" -V '/' declare exchange name=model durable=true type=topic
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment