Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Last active February 28, 2017 02:43
Show Gist options
  • Save kwilczynski/a9eb29b8c02096ef451d to your computer and use it in GitHub Desktop.
Save kwilczynski/a9eb29b8c02096ef451d to your computer and use it in GitHub Desktop.
Chef define for declaring/deleting queues from RabbitMQ
attributes = {
:action => :declare,
:durable => nil,
:auto_delete => nil,
:vhost => nil,
:arguments => nil
}
define :rabbitmq_queue, attributes do
# We heavily rely on the `Management Command Line Tool`,
# see: https://www.rabbitmq.com/management-cli.html
rabbitmq_admin = '/usr/sbin/rabbitmqadmin'
declare = params[:action] == :declare
admin_cmd =
[rabbitmq_admin].tap do |o|
o << '--quiet'
o << format("--vhost '%s'", params[:vhost]) if params[:vhost]
o << format('%s queue', params[:action])
o << format('name=%s', params[:name])
if declare
# By default a durable queue is created.
o << format('durable=%s', params[:durable]) unless params[:durable].nil?
o << format('auto_delete=%s', params[:auto_delete]) unless params[:auto_delete].nil?
# Any extra options and special headers, etc.
if params[:arguments] && params[:arguments].is_a?(Hash)
o << format("arguments='%s'", params[:arguments].to_json)
end
end
end.join(' ')
guard_cmd = <<-EOS.gsub(/^\s+/, '')
#{rabbitmq_admin} --quiet list queues -f bash | \
tr ' ' '\n' | grep -q '^#{params[:name]}$'
EOS
execute format('RabbitMQ %s queue: %s', params[:action], params[:name]) do
command admin_cmd
send(declare ? :not_if : :only_if, guard_cmd)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment