Created
December 16, 2011 15:39
-
-
Save brendanhay/1486513 to your computer and use it in GitHub Desktop.
Auto-generate rabbit-shovel configuration for all queues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
-export([main/1]). | |
main([Src, Dest]) -> | |
Path = "./shovel.config", | |
Config = config(Src, Dest), | |
write(Config, Path), | |
io:format("wrote ~p shovels to ~s~n", [length(Config), Path]); | |
main(_Any) -> | |
io:format("Usage: ~s <source> <destination>~n", [escript:script_name()]), | |
halt(1). | |
config(Src, Dest) -> | |
Bindings = [re:split(B, "\t") || B <- bindings()], | |
Shovel = fun(Exchange, Queue) -> shovel(Src, Dest, Exchange, Queue) end, | |
split(Bindings, Shovel, []). | |
bindings() -> re:split(os:cmd("rabbitmqctl list_bindings"), "\n"). | |
write(Config, Path) -> | |
Term = {rabbitmq_shovel, [{shovels, Config}]}, | |
file:write_file(Path, io_lib:fwrite("~p.\n", [Term])). | |
split([], _Fun, Acc) -> | |
Acc; | |
split([[Exchange, <<"exchange">>, Queue, <<"queue">>, _, _]|T], Fun, Acc) -> | |
split(T, Fun, [Fun(Exchange, Queue)|Acc]); | |
split([_|T], Fun, Acc) -> | |
split(T, Fun, Acc). | |
shovel(Src, Dest, Exchange, Queue) -> | |
Declarations = [ | |
{'queue.declare', [{queue, Queue}, durable]}, | |
{'exchange.declare', [{exchange, Exchange}, {type, <<"direct">>}, durable]}, | |
{'queue.bind', [{exchange, Exchange}, {queue, Queue}]} | |
], | |
{list_to_atom(binary_to_list(Queue)), [ | |
{sources, [ | |
{broker, Src}, | |
{declarations, Declarations} | |
]}, | |
{destinations, [ | |
{broker, Dest}, | |
{declarations, Declarations} | |
]}, | |
{queue, Queue}, | |
{prefetch_count, 10} | |
]}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment