Skip to content

Instantly share code, notes, and snippets.

@fforbeck
Created February 25, 2016 14:19
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save fforbeck/868462c0f7664d92e19e to your computer and use it in GitHub Desktop.
Save fforbeck/868462c0f7664d92e19e to your computer and use it in GitHub Desktop.
RabbitMQ - Command Line Setup. Create queue, bindings, exchanges with rabbitmqadmin and rabbitmqctl
#!/usr/bin/env bash
URL="http://localhost:15672/cli/rabbitmqadmin"
VHOST="<>"
USER="<>"
PWD="<>"
QUEUE="<>"
FAILED_QUEUE="<>"
INPUT_EX="<>"
FAILED_INPUT_EX="<>"
set_user_and_vhost() {
rabbitmqctl add_vhost $VHOST
rabbitmqctl list_vhosts
rabbitmqctl add_user $USER $PWD
rabbitmqctl set_user_tags $USER administrator
rabbitmqctl list_users
}
set_permissions() {
rabbitmqctl set_permissions -p $VHOST $USER ".*" ".*" ".*"
rabbitmqctl list_permissions -p $VHOST
}
set_exchanges() {
rabbitmqadmin declare exchange --vhost=$VHOST --user=$USER --password=$PWD name=$INPUT_EX type=direct durable=true
rabbitmqadmin declare exchange --vhost=$VHOST --user=$USER --password=$PWD name=$FAILED_INPUT_EX type=direct durable=true
rabbitmqctl list_exchanges -p $VHOST
}
set_queues() {
rabbitmqadmin declare queue --vhost=$VHOST --user=$USER --password=$PWD name=$QUEUE durable=true
rabbitmqadmin declare queue --vhost=$VHOST --user=$USER --password=$PWD name=$FAILED_QUEUE durable=true
rabbitmqctl list_queues -p $VHOST
}
set_bindings() {
rabbitmqadmin declare binding --vhost=$VHOST --user=$USER --password=$PWD source=$INPUT_EX destination=$QUEUE
rabbitmqadmin declare binding --vhost=$VHOST --user=$USER --password=$PWD source=$FAILED_INPUT_EX destination=$FAILED_QUEUE
rabbitmqctl list_bindings -p $VHOST
}
type rabbitmqctl > /dev/null 2>&1 || { echo >&2 "rabbitmqctl is required but it is not installed. Aborting."; exit 1; }
type rabbitmqadmis > /dev/null 2>&1 || { echo >&2 "rabbitmqadmin is required but it is not installed.
Download from $URL, add .py extension and install into /usr/local/bin/rabbitmqadmin.
Aborting."; exit 1; }
set_user_and_vhost
set_permissions
set_exchanges
set_queues
set_bindings
echo "Rabbitmq configured with success."
@likhithav
Copy link

this is bash file, but rabbitmqadmin command is not working on the command line. Hw does it work?

@likhithav
Copy link

Where to run this on windows?

@srghma
Copy link

srghma commented Dec 21, 2018

My variant

scripts/rabbitmq-migrate

#!/bin/bash -e

#
# based on https://gist.github.com/fforbeck/868462c0f7664d92e19e
#

# check dependencies are installed

type rabbitmqctl > /dev/null 2>&1 || { echo >&2 "rabbitmqctl is required but it is not installed. Aborting."; exit 1; }
type rabbitmqadmin > /dev/null 2>&1 || { echo >&2 "rabbitmqadmin is required but it is not installed. Aborting."; exit 1; }

# check auth credentials are present

[ -z "$RABBITMQ_USER" ] && { echo >&2 "RABBITMQ_USER variable should be present. Aborting."; exit 1; }
[ -z "$RABBITMQ_PASS" ] && { echo >&2 "RABBITMQ_PASS variable should be present. Aborting."; exit 1; }
[ -z "$RABBITMQ_HOST" ] && { echo >&2 "RABBITMQ_HOST variable should be present. Aborting."; exit 1; }
[ -z "$RABBITMQ_PORT" ] && { echo >&2 "RABBITMQ_PORT variable should be present. Aborting."; exit 1; }

# helpers

function rabbitmqadmin_cli () {
  rabbitmqadmin \
    --vhost=/ \
    --username="${RABBITMQ_USER}" \
    --password="${RABBITMQ_PASS}" \
    --host="${RABBITMQ_HOST}" \
    --port="${RABBITMQ_PORT}" \
    ${@}
}

# set queues

rabbitmqadmin_cli declare queue name=mails durable=true

echo ""

rabbitmqadmin_cli list queues

echo ""

# set bindings
rabbitmqadmin_cli declare binding source="amq.direct" destination=mails routing_key="mails"

echo ""

rabbitmqadmin_cli list bindings

echo ""

echo "Rabbitmq configured with success."

Makefile

feature_tests_dev_rabbitmq_drop:
  ./scripts/docker-volume-rm-if-exists myapp_feature_tests_dev_rabbitmq_data

feature_tests_dev_rabbitmq_migrate:
  docker-compose \
    -p "$(PROJECT_NAME)_feature_tests_dev" \
    -f docker/feature_tests_dev.yml \
    run --rm rabbitmq_migrator bash -c '\
      /bin/waitforit -host=$$RABBITMQ_HOST -port=$$RABBITMQ_PORT && \
      /bin/rabbitmq-migrate'

feature_tests_dev_rabbitmq_recreate:
  $(MAKE) feature_tests_dev_rabbitmq_drop
  $(MAKE) feature_tests_dev_rabbitmq_migrate

docker-compose.yml

  rabbitmq_migrator:
    image: rabbitmq:3-management # rabbitmq:3-management = rabbitmq:3 + admin panel
    command: bash -c "/bin/true" # do nothing on `docker-compose up`
    environment:
      RABBITMQ_USER: admin
      RABBITMQ_PASS: adminpass
      RABBITMQ_HOST: rabbitmq
      RABBITMQ_PORT: 15672 # default port
      # RABBITMQ_ERLANG_COOKIE: secret # required only for rabbitmqctl, rabbitmqadmin uses http auth instead
    volumes:
      - ../scripts/rabbitmq-migrate:/bin/rabbitmq-migrate:ro
      - ../scripts/waitforit:/bin/waitforit:ro
    depends_on:
      - rabbitmq

  # RabbitMQ instance can be used to consolidate events that originated in your database/application.
  # You can connect here with different consumers and take actions based on those events (like sending signup emails)
  rabbitmq:
    image: rabbitmq:3-management # rabbitmq:3-management = rabbitmq:3 + admin panel
    hostname: myapp-rabbit # the hostname is required to make data persisntent
    ports:
      - 5672:5672   # default port
      - 15672:15672 # admin panel port
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: adminpass
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    restart: unless-stopped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment