summaryrefslogtreecommitdiff
path: root/src/rabbit_control.erl
blob: bc588279b497848041e01c8e28af7daacaddcf46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
%%   The contents of this file are subject to the Mozilla Public License
%%   Version 1.1 (the "License"); you may not use this file except in
%%   compliance with the License. You may obtain a copy of the License at
%%   http://www.mozilla.org/MPL/
%%
%%   Software distributed under the License is distributed on an "AS IS"
%%   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%%   License for the specific language governing rights and limitations
%%   under the License.
%%
%%   The Original Code is RabbitMQ.
%%
%%   The Initial Developers of the Original Code are LShift Ltd.,
%%   Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
%%
%%   Portions created by LShift Ltd., Cohesive Financial Technologies
%%   LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007-2008
%%   LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
%%   Technologies Ltd.;
%%
%%   All Rights Reserved.
%%
%%   Contributor(s): ______________________________________.
%%

-module(rabbit_control).
-include("rabbit.hrl").

-export([start/0, stop/0, action/3]).

-define(RPC_TIMEOUT, 30000).

start() ->
    case init:get_plain_arguments() of
        [] ->
            usage();
        FullCommand ->
            {Node, Command, Args} = parse_args(FullCommand),
            case catch action(Command, Node, Args) of
                ok ->
                    io:format("done.~n"),
                    init:stop();
                {'EXIT', {function_clause, [{?MODULE, action, _} | _]}} ->
                    io:format("Invalid command ~p~n", [FullCommand]),
                    usage();
                Other ->
                    io:format("~nrabbit_control action ~p failed:~n~p~n", [Command, Other]),
                    halt(2)
            end
    end.

parse_args(["-n", NodeS, Command | Args]) ->
    Node = case lists:member($@, NodeS) of
               true  -> list_to_atom(NodeS);
               false -> rabbit_misc:localnode(list_to_atom(NodeS))
           end,
    {Node, list_to_atom(Command), Args};
parse_args([Command | Args]) ->
    {rabbit_misc:localnode(rabbit), list_to_atom(Command), Args}.

stop() ->
    ok.

usage() ->
    io:format("Usage: rabbitmqctl [-n <node>] <command> [<arg> ...]

Available commands:

  stop      - stops the RabbitMQ application and halts the node
  stop_app  - stops the RabbitMQ application, leaving the node running
  start_app - starts the RabbitMQ application on an already-running node
  reset     - resets node to default configuration, deleting all data
  force_reset
  cluster <ClusterNode> ...
  status
  rotate_logs [Suffix]

  add_user        <UserName> <Password>
  delete_user     <UserName>
  change_password <UserName> <NewPassword>
  list_users

  add_vhost    <VHostPath>
  delete_vhost <VHostPath>
  list_vhosts

  map_user_vhost   <UserName> <VHostPath>
  unmap_user_vhost <UserName> <VHostPath>
  list_user_vhosts <UserName>
  list_vhost_users <VHostPath>

<node> should be the name of the master node of the RabbitMQ cluster. It
defaults to the node named \"rabbit\" on the local host. On a host named
\"server.example.com\", the master node will usually be rabbit@server (unless
NODENAME has been set to some non-default value at broker startup time). The
output of hostname -s is usually the correct suffix to use after the \"@\" sign.

"),
    halt(1).

action(stop, Node, []) ->
    io:format("Stopping and halting node ~p ...", [Node]),
    call(Node, {rabbit, stop_and_halt, []});

action(stop_app, Node, []) ->
    io:format("Stopping node ~p ...", [Node]),
    call(Node, {rabbit, stop, []});

action(start_app, Node, []) ->
    io:format("Starting node ~p ...", [Node]),
    call(Node, {rabbit, start, []});

action(reset, Node, []) ->
    io:format("Resetting node ~p ...", [Node]),
    call(Node, {rabbit_mnesia, reset, []});

action(force_reset, Node, []) ->
    io:format("Forcefully resetting node ~p ...", [Node]),
    call(Node, {rabbit_mnesia, force_reset, []});

action(cluster, Node, ClusterNodeSs) ->
    ClusterNodes = lists:map(fun list_to_atom/1, ClusterNodeSs),
    io:format("Clustering node ~p with ~p ...",
              [Node, ClusterNodes]),
    rpc_call(Node, rabbit_mnesia, cluster, [ClusterNodes]);

action(status, Node, []) ->
    io:format("Status of node ~p ...", [Node]),
    Res = call(Node, {rabbit, status, []}),
    io:format("~n~p~n", [Res]),
    ok;

action(rotate_logs, Node, []) ->
    io:format("Reopening logs for node ~p ...", [Node]),
    call(Node, {rabbit, rotate_logs, [""]});
action(rotate_logs, Node, Args = [Suffix]) ->
    io:format("Rotating logs to files with suffix ~p ...", [Suffix]),
    call(Node, {rabbit, rotate_logs, Args});

action(add_user, Node, Args = [Username, _Password]) ->
    io:format("Creating user ~p ...", [Username]),
    call(Node, {rabbit_access_control, add_user, Args});

action(delete_user, Node, Args = [_Username]) ->
    io:format("Deleting user ~p ...", Args),
    call(Node, {rabbit_access_control, delete_user, Args});

action(change_password, Node, Args = [Username, _Newpassword]) ->
    io:format("Changing password for user ~p ...", [Username]),
    call(Node, {rabbit_access_control, change_password, Args});

action(list_users, Node, []) ->
    io:format("Listing users ..."),
    display_list(call(Node, {rabbit_access_control, list_users, []}));

action(add_vhost, Node, Args = [_VHostPath]) ->
    io:format("Creating vhost ~p ...", Args),
    call(Node, {rabbit_access_control, add_vhost, Args});

action(delete_vhost, Node, Args = [_VHostPath]) ->
    io:format("Deleting vhost ~p ...", Args),
    call(Node, {rabbit_access_control, delete_vhost, Args});

action(list_vhosts, Node, []) ->
    io:format("Listing vhosts ..."),
    display_list(call(Node, {rabbit_access_control, list_vhosts, []}));

action(map_user_vhost, Node, Args = [_Username, _VHostPath]) ->
    io:format("Mapping user ~p to vhost ~p ...", Args),
    call(Node, {rabbit_access_control, map_user_vhost, Args});

action(unmap_user_vhost, Node, Args = [_Username, _VHostPath]) ->
    io:format("Unmapping user ~p from vhost ~p ...", Args),
    call(Node, {rabbit_access_control, unmap_user_vhost, Args});

action(list_user_vhosts, Node, Args = [_Username]) ->
    io:format("Listing vhosts for user ~p...", Args),
    display_list(call(Node, {rabbit_access_control, list_user_vhosts, Args}));

action(list_vhost_users, Node, Args = [_VHostPath]) ->
    io:format("Listing users for vhosts ~p...", Args),
    display_list(call(Node, {rabbit_access_control, list_vhost_users, Args})).

display_list(L) when is_list(L) ->
    lists:foreach(fun (I) ->
                          io:format("~n~s", [binary_to_list(I)])
                  end,
                  lists:sort(L)),
    io:nl();
display_list(Other) -> Other.

call(Node, {Mod, Fun, Args}) ->
    rpc_call(Node, Mod, Fun, lists:map(fun list_to_binary/1, Args)).

rpc_call(Node, Mod, Fun, Args) ->
    rpc:call(Node, Mod, Fun, Args, ?RPC_TIMEOUT).