diff options
author | Michael Klishin <klishinm@vmware.com> | 2023-05-02 10:55:31 +0400 |
---|---|---|
committer | Michael Klishin <klishinm@vmware.com> | 2023-05-02 10:55:31 +0400 |
commit | d4a2d48cea1973893a4bc212357664e9f80f959b (patch) | |
tree | c3e86f683e4a9f4a788723f37d2b5aaf6d8c8370 | |
parent | 1e8399be44a653f7c27685c44d08faae85622306 (diff) | |
download | rabbitmq-server-git-d4a2d48cea1973893a4bc212357664e9f80f959b.tar.gz |
rabbitmq-queues: validate cluster membership of the argument
Specifically of the node where new replicas should be
placed.
Closes #8007
5 files changed, 35 insertions, 3 deletions
diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex index 33ab985834..fb1699a2b2 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex @@ -77,6 +77,11 @@ defmodule RabbitMQ.CLI.Core.Helpers do end end + def cluster_member?(target_node, node_to_check, timeout \\ 30_000) do + node_to_check_a = DataCoercion.to_atom(node_to_check) + Enum.member?(nodes_in_cluster(target_node, timeout), node_to_check_a) + end + def node_running?(node) do :net_adm.ping(node) == :pong end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex index 5e32a73b82..e3571b5bae 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex @@ -47,6 +47,13 @@ defmodule RabbitMQ.CLI.Core.Validators do end end + def existing_cluster_member([potential_member | _], %{node: node_name}) do + case Helpers.cluster_member?(node_name, potential_member) do + false -> {:validation_failure, {:not_a_cluster_member, potential_member}} + true -> :ok + end + end + def data_dir_is_set(_, opts) do case require_data_dir(opts) do :ok -> :ok diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/add_member_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/add_member_command.ex index afc0770a99..fa012577e0 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/add_member_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/add_member_command.ex @@ -25,7 +25,16 @@ defmodule RabbitMQ.CLI.Queues.Commands.AddMemberCommand do use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments - use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &Validators.rabbit_is_running/2, + &Validators.existing_cluster_member/2 + ], + [args, opts] + ) + end def run([name, node] = _args, %{vhost: vhost, node: node_name, timeout: timeout}) do case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :add_member, [ diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/grow_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/grow_command.ex index 46aaf5b988..f6ab616a62 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/grow_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/grow_command.ex @@ -5,7 +5,7 @@ ## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved. defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do - alias RabbitMQ.CLI.Core.DocGuide + alias RabbitMQ.CLI.Core.{DocGuide, Validators} import RabbitMQ.CLI.Core.DataCoercion @behaviour RabbitMQ.CLI.CommandBehaviour @@ -44,7 +44,15 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do end end - use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &Validators.rabbit_is_running/2, + &Validators.existing_cluster_member/2 + ], + [args, opts] + ) + end def run([node, strategy], %{ node: node_name, diff --git a/deps/rabbitmq_cli/lib/rabbitmqctl.ex b/deps/rabbitmq_cli/lib/rabbitmqctl.ex index 007b0c5f73..11b7d7e00d 100644 --- a/deps/rabbitmq_cli/lib/rabbitmqctl.ex +++ b/deps/rabbitmq_cli/lib/rabbitmqctl.ex @@ -393,6 +393,9 @@ defmodule RabbitMQCtl do defp format_validation_error(:unsupported_formatter), do: "the requested formatter is not supported by this command" + defp format_validation_error({:not_a_cluster_member, potential_member}), + do: "node #{potential_member} is not a member of the cluster" + defp format_validation_error(err), do: inspect(err) @spec exit_program(integer()) :: no_return() |