Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.

Commit 476890f

Browse files
Merge pull request #182 from rabbitmq/rabbitmq-cli-180
Make it possible to specify erlang cookie from command line
2 parents 4a9cff5 + 4ac1697 commit 476890f

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

DESIGN.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,14 @@ be shown in usage and available for execution.
231231

232232
#### Environment Arguments
233233

234-
* script_name: atom, configurable tool name (`rabbitmq-plugins`, `rabbitmqctl`) to select command scope (see [Command scopes](#command-scopes))
235-
* rabbitmq_home: string, broker install directory
236-
* mnesia_dir: string, broker mnesia data directory
237-
* plugins_dir: string, broker plugins directory
238-
* enabled_plugins_file: string, broker enabled plugins file
234+
* script-name: atom, configurable tool name (`rabbitmq-plugins`, `rabbitmqctl`) to select command scope (see [Command scopes](#command-scopes))
235+
* rabbitmq-home: string, broker install directory
236+
* mnesia-dir: string, broker mnesia data directory
237+
* plugins-dir: string, broker plugins directory
238+
* enabled-plugins-file: string, broker enabled plugins file
239239
* longnames (l): boolean, use longnames to communicate with broker erlang node. Should be set to `true` only if broker is started with longnames.
240240
* aliases-file: string, a file name to load aliases from
241+
* erlang-cookie: atom, an [erlang distribution cookie](http://erlang.org/doc/reference_manual/distributed.html)
241242

242243
Environment argument defaults are loaded from rabbitmq environment variables (see [Environment configuration](#environment-configuration)).
243244

@@ -469,11 +470,12 @@ By default it will be loaded from environment variables, same as used in rabbitm
469470

470471
| Argument name | Environment variable |
471472
|----------------------|-------------------------------|
472-
| rabbitmq_home | RABBITMQ_HOME |
473-
| mnesia_dir | RABBITMQ_MNESIA_DIR |
474-
| plugins_dir | RABBITMQ_PLUGINS_DIR |
475-
| enabled_plugins_file | RABBITMQ_ENABLED_PLUGINS_FILE |
473+
| rabbitmq-home | RABBITMQ_HOME |
474+
| mnesia-dir | RABBITMQ_MNESIA_DIR |
475+
| plugins-dir | RABBITMQ_PLUGINS_DIR |
476+
| enabled-plugins-file | RABBITMQ_ENABLED_PLUGINS_FILE |
476477
| longnames | RABBITMQ_USE_LONGNAME |
477478
| node | RABBITMQ_NODENAME |
478-
| aliases_file | RABBITMQ_CLI_ALIASES_FILE |
479+
| aliases-file | RABBITMQ_CLI_ALIASES_FILE |
480+
| erlang-cookie | RABBITMQ_ERLANG_COOKIE |
479481

lib/rabbitmq/cli/core/config.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ defmodule RabbitMQ.CLI.Core.Config do
2222
normalize(name, raw_option)
2323
end
2424

25+
def normalize(:node, nil), do: nil
2526
def normalize(:node, node) when not is_atom(node) do
2627
Rabbitmq.Atom.Coerce.to_atom(node)
2728
end
29+
def normalize(:erlang_cookie, nil), do: nil
30+
def normalize(:erlang_cookie, c) when not is_atom(c) do
31+
Rabbitmq.Atom.Coerce.to_atom(c)
32+
end
2833
def normalize(:longnames, true), do: :longnames
2934
def normalize(:longnames, "true"), do: :longnames
3035
def normalize(:longnames, 'true'), do: :longnames
@@ -44,7 +49,8 @@ defmodule RabbitMQ.CLI.Core.Config do
4449
:plugins_dir -> "RABBITMQ_PLUGINS_DIR";
4550
:enabled_plugins_file -> "RABBITMQ_ENABLED_PLUGINS_FILE";
4651
:node -> "RABBITMQ_NODENAME";
47-
:aliases_file -> "RABBITMQ_CLI_ALIASES_FILE"
52+
:aliases_file -> "RABBITMQ_CLI_ALIASES_FILE";
53+
:erlang_cookie -> "RABBITMQ_ERLANG_COOKIE";
4854
_ -> ""
4955
end
5056
System.get_env(system_env_option)

lib/rabbitmq/cli/core/distribution.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ defmodule RabbitMQ.CLI.Core.Distribution do
2626

2727
def start(options) do
2828
node_name_type = Config.get_option(:longnames, options)
29-
start(node_name_type, 10, :undefined)
29+
result = start(node_name_type, 10, :undefined)
30+
ensure_cookie(options)
31+
result
3032
end
3133

3234
def start_as(node_name, options) do
3335
node_name_type = Config.get_option(:longnames, options)
34-
start_with_epmd(node_name, node_name_type)
36+
result = start_with_epmd(node_name, node_name_type)
37+
ensure_cookie(options)
38+
result
3539
end
3640

3741
## Optimization. We try to start EPMD only if distribution fails
@@ -51,6 +55,14 @@ defmodule RabbitMQ.CLI.Core.Distribution do
5155
# Implementation
5256
#
5357

58+
def ensure_cookie(options) do
59+
case Config.get_option(:erlang_cookie, options) do
60+
nil -> :ok;
61+
cookie -> Node.set_cookie(cookie)
62+
:ok
63+
end
64+
end
65+
5466
defp start(_opt, 0, last_err) do
5567
{:error, last_err}
5668
end

lib/rabbitmq/cli/core/parser.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ defmodule RabbitMQ.CLI.Core.Parser do
178178
mnesia_dir: :string,
179179
plugins_dir: :string,
180180
enabled_plugins_file: :string,
181-
aliases_file: :string
181+
aliases_file: :string,
182+
erlang_cookie: :atom
182183
]
183184
end
184185

test/distribution_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## The contents of this file are subject to the Mozilla Public License
2+
## Version 1.1 (the "License"); you may not use this file except in
3+
## compliance with the License. You may obtain a copy of the License
4+
## at http://www.mozilla.org/MPL/
5+
##
6+
## Software distributed under the License is distributed on an "AS IS"
7+
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
## the License for the specific language governing rights and
9+
## limitations under the License.
10+
##
11+
## The Original Code is RabbitMQ.
12+
##
13+
## The Initial Developer of the Original Code is GoPivotal, Inc.
14+
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
15+
16+
alias RabbitMQ.CLI.Core.Distribution, as: Distribution
17+
18+
defmodule DostributionTest do
19+
use ExUnit.Case, async: false
20+
import TestHelper
21+
22+
setup_all do
23+
:net_kernel.stop()
24+
:ok
25+
end
26+
27+
test "set cookie via environment variable" do
28+
on_exit(fn ->
29+
:net_kernel.stop()
30+
System.delete_env("RABBITMQ_ERLANG_COOKIE")
31+
end)
32+
:nocookie = Node.get_cookie()
33+
System.put_env("RABBITMQ_ERLANG_COOKIE", "mycookie")
34+
opts = %{}
35+
Distribution.start(opts)
36+
:mycookie = Node.get_cookie()
37+
end
38+
39+
test "set cookie via argument" do
40+
on_exit(fn ->
41+
:net_kernel.stop()
42+
end)
43+
:nocookie = Node.get_cookie()
44+
opts = %{erlang_cookie: :mycookie}
45+
Distribution.start(opts)
46+
:mycookie = Node.get_cookie()
47+
end
48+
end

test/rabbitmqctl_test.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ defmodule RabbitMQCtlTest do
2727
set_scope(:all)
2828
on_exit([], fn ->
2929
:erlang.disconnect_node(get_rabbit_hostname())
30-
3130
end)
32-
3331
:ok
3432
end
3533

0 commit comments

Comments
 (0)