Skip to content

Instantly share code, notes, and snippets.

@we4tech
Created October 26, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save we4tech/29d81f5d353f6c9d138f9090454b6e07 to your computer and use it in GitHub Desktop.
Save we4tech/29d81f5d353f6c9d138f9090454b6e07 to your computer and use it in GitHub Desktop.
create_rabbitmq_user.sh
#!/bin/sh
API_USER="<USER>:<PASSWORD>"
API_URL="https://<SOME_SERVICE>.cloudamqp.com/api"
VHOST="<VHOST>"
__colored_echo()
{
printf "\033[$1m$2\033[0m\n"
}
__success_echo()
{
__colored_echo "0;32" "$@"
}
__error_echo()
{
__colored_echo "0;31" "$@"
}
__warning_echo()
{
__colored_echo "1;33" "$@"
}
__info_echo()
{
__colored_echo "0;37" "$@"
}
__input_echo()
{
__colored_echo "0;94" "$@"
}
initiate_new_user() {
__success_echo "======================="
__info_echo "==> Create new user <=="
__success_echo "======================="
read -p "Enter user name > " user
read -p "Enter password > " password
__success_echo "Creating user: $user with password: $password"
read -p "Looks good? (y/n)> " yn
case $yn in
[yY]* )
(create_user $user $password && create_permission $user) && on_success || on_failure
;;
* )
__error_echo "==> Damn! retrying"
initiate_new_user
;;
esac
}
create_user() {
__warning_echo "--> Creating user $1"
curl -v -u $API_USER -X PUT -d '{"password":"$2","tags":"administrator"}' $API_URL/users/$1
}
create_permission() {
__warning_echo "--> Creating permission for user $1"
curl -v -u $API_USER -X PUT -d '{"configure":".*","write":".*","read":".*"}' $API_URL/permissions/$VHOST/$1
}
on_success() {
__success_echo '==> Successfully created!'
initiate_new_user
}
on_failure() {
__error_echo '==> !!!Failed to create user'
initiate_new_user
}
initiate_new_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment