diff options
author | dcorbacho <dparracorbacho@piotal.io> | 2020-11-18 14:27:41 +0000 |
---|---|---|
committer | dcorbacho <dparracorbacho@piotal.io> | 2020-11-18 14:27:41 +0000 |
commit | f23a51261d9502ec39df0f8db47ba6b22aa7659f (patch) | |
tree | 53dcdf46e7dc2c14e81ee960bce8793879b488d3 /deps/rabbitmq_cli | |
parent | afa2c2bf6c7e0e9b63f4fb53dc931c70388e1c82 (diff) | |
parent | 9f6d64ec4a4b1eeac24d7846c5c64fd96798d892 (diff) | |
download | rabbitmq-server-git-stream-timestamp-offset.tar.gz |
Merge remote-tracking branch 'origin/master' into stream-timestamp-offsetstream-timestamp-offset
Diffstat (limited to 'deps/rabbitmq_cli')
430 files changed, 43435 insertions, 0 deletions
diff --git a/deps/rabbitmq_cli/.gitignore b/deps/rabbitmq_cli/.gitignore new file mode 100644 index 0000000000..0ade5483bf --- /dev/null +++ b/deps/rabbitmq_cli/.gitignore @@ -0,0 +1,12 @@ +/_build +/cover +/deps +/escript +/log +/.erlang.mk/ +/ebin +erl_crash.dump +mix.lock +*.ez +.sw? +.*.sw? diff --git a/deps/rabbitmq_cli/.travis.yml b/deps/rabbitmq_cli/.travis.yml new file mode 100644 index 0000000000..3937ec84c2 --- /dev/null +++ b/deps/rabbitmq_cli/.travis.yml @@ -0,0 +1,57 @@ +# vim:sw=2:et: + +os: linux +dist: xenial +language: elixir +notifications: + email: + recipients: + - alerts@rabbitmq.com + on_success: never + on_failure: always +addons: + apt: + packages: + - awscli +cache: + apt: true +env: + global: + + # $base_rmq_ref is used by rabbitmq-components.mk to select the + # appropriate branch for dependencies. + - base_rmq_ref=master + +jobs: + include: + - elixir: '1.10' + otp_release: '22.3' + - elixir: '1.10' + otp_release: '23.0' + +install: + # This project being an Erlang one (we just set language to Elixir + # to ensure it is installed), we don't want Travis to run mix(1) + # automatically as it will break. + - mix local.rebar --force + +script: + # $current_rmq_ref is also used by rabbitmq-components.mk to select + # the appropriate branch for dependencies. + - make + DEPS_DIR=$PWD/.. + current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" + - | + git clone \ + --branch "$base_rmq_ref" \ + --depth 1 \ + https://github.com/rabbitmq/rabbitmq-server-release.git \ + ../rabbitmq_server_release + make start-background-broker -C ../rabbitmq_server_release \ + DEPS_DIR=$PWD/.. \ + PLUGINS='rabbitmq_federation rabbitmq_stomp' \ + PROJECT_VERSION=3.9.0 \ + current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" + - make tests + DEPS_DIR=$PWD/.. + current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" diff --git a/deps/rabbitmq_cli/.travis.yml.patch b/deps/rabbitmq_cli/.travis.yml.patch new file mode 100644 index 0000000000..3b85c106c3 --- /dev/null +++ b/deps/rabbitmq_cli/.travis.yml.patch @@ -0,0 +1,62 @@ +--- .travis.yml.orig 2020-10-12 17:29:44.096296000 +0200 ++++ .travis.yml 2020-10-12 17:26:40.450974000 +0200 +@@ -22,38 +22,36 @@ + # appropriate branch for dependencies. + - base_rmq_ref=master + +-elixir: +- - '1.10' +-otp_release: +- - '22.3' +- - '23.1' ++jobs: ++ include: ++ - elixir: '1.10' ++ otp_release: '22.3' ++ - elixir: '1.10' ++ otp_release: '23.1' + + install: + # This project being an Erlang one (we just set language to Elixir + # to ensure it is installed), we don't want Travis to run mix(1) + # automatically as it will break. +- skip ++ - mix local.rebar --force + + script: + # $current_rmq_ref is also used by rabbitmq-components.mk to select + # the appropriate branch for dependencies. +- - make check-rabbitmq-components.mk ++ - make ++ DEPS_DIR=$PWD/.. + current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" +- - make xref +- current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" ++ - | ++ git clone \ ++ --branch "$base_rmq_ref" \ ++ --depth 1 \ ++ https://github.com/rabbitmq/rabbitmq-server-release.git \ ++ ../rabbitmq_server_release ++ make start-background-broker -C ../rabbitmq_server_release \ ++ DEPS_DIR=$PWD/.. \ ++ PLUGINS='rabbitmq_federation rabbitmq_stomp' \ ++ PROJECT_VERSION=3.9.0 \ ++ current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" + - make tests ++ DEPS_DIR=$PWD/.. + current_rmq_ref="${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}" +- +-after_failure: +- - | +- cd "$TRAVIS_BUILD_DIR" +- if test -d logs && test "$AWS_ACCESS_KEY_ID" && test "$AWS_SECRET_ACCESS_KEY"; then +- archive_name="$(basename "$TRAVIS_REPO_SLUG")-$TRAVIS_JOB_NUMBER" +- +- tar -c --transform "s/^logs/${archive_name}/" -f - logs | \ +- xz > "${archive_name}.tar.xz" +- +- aws s3 cp "${archive_name}.tar.xz" s3://server-release-pipeline/travis-ci-logs/ \ +- --region eu-west-1 \ +- --acl public-read +- fi diff --git a/deps/rabbitmq_cli/CODE_OF_CONDUCT.md b/deps/rabbitmq_cli/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000..08697906fd --- /dev/null +++ b/deps/rabbitmq_cli/CODE_OF_CONDUCT.md @@ -0,0 +1,44 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, and in the interest of fostering an open +and welcoming community, we pledge to respect all people who contribute through reporting +issues, posting feature requests, updating documentation, submitting pull requests or +patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for +everyone, regardless of level of experience, gender, gender identity and expression, +sexual orientation, disability, personal appearance, body size, race, ethnicity, age, +religion, or nationality. + +Examples of unacceptable behavior by participants include: + + * The use of sexualized language or imagery + * Personal attacks + * Trolling or insulting/derogatory comments + * Public or private harassment + * Publishing other's private information, such as physical or electronic addresses, + without explicit permission + * Other unethical or unprofessional conduct + +Project maintainers have the right and responsibility to remove, edit, or reject comments, +commits, code, wiki edits, issues, and other contributions that are not aligned to this +Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors +that they deem inappropriate, threatening, offensive, or harmful. + +By adopting this Code of Conduct, project maintainers commit themselves to fairly and +consistently applying these principles to every aspect of managing this project. Project +maintainers who do not follow or enforce the Code of Conduct may be permanently removed +from the project team. + +This Code of Conduct applies both within project spaces and in public spaces when an +individual is representing the project or its community. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by +contacting a project maintainer at [info@rabbitmq.com](mailto:info@rabbitmq.com). All complaints will +be reviewed and investigated and will result in a response that is deemed necessary and +appropriate to the circumstances. Maintainers are obligated to maintain confidentiality +with regard to the reporter of an incident. + +This Code of Conduct is adapted from the +[Contributor Covenant](https://contributor-covenant.org), version 1.3.0, available at +[contributor-covenant.org/version/1/3/0/](https://contributor-covenant.org/version/1/3/0/) diff --git a/deps/rabbitmq_cli/COMMAND_TUTORIAL.md b/deps/rabbitmq_cli/COMMAND_TUTORIAL.md new file mode 100644 index 0000000000..8ead46afa7 --- /dev/null +++ b/deps/rabbitmq_cli/COMMAND_TUTORIAL.md @@ -0,0 +1,459 @@ +# Implementing Your Own rabbitmqctl Command + +## Introduction + +As of `3.7.0`, RabbitMQ [CLI +tools](https://github.com/rabbitmq/rabbitmq-cli) (e.g. `rabbitmqctl`) +allow plugin developers to extend them their own commands. + +The CLI is written in the [Elixir programming +language](https://elixir-lang.org/) and commands can be implemented in +Elixir, Erlang or any other Erlang-based language. This tutorial will +use Elixir but also provides an Erlang example. The fundamentals are +the same. + +This tutorial doesn't cover RabbitMQ plugin development process. +To develop a new plugin you should check existing tutorials: + + * [RabbitMQ Plugin Development](https://www.rabbitmq.com/plugin-development.html) (in Erlang) + * [Using Elixir to Write RabbitMQ Plugins](https://www.rabbitmq.com/blog/2013/06/03/using-elixir-to-write-rabbitmq-plugins/) + + +## Anatomy of a RabbitMQ CLI Command + +A RabbitMQ CLI command is an Elixir/Erlang module that implements a +particular [behavior](https://elixir-lang.org/getting-started/typespecs-and-behaviours.html). +It should fulfill certain requirements in order to be discovered and load by CLI tools: + + * Follow a naming convention (module name should match `RabbitMQ.CLI.(.*).Commands.(.*)Command`) + * Be included in a plugin application's module list (`modules` in the `.app` file) + * Implement `RabbitMQ.CLI.CommandBehaviour` + +## Implementing `RabbitMQ.CLI.CommandBehaviour` in Erlang + +When implementing a command in Erlang, you should add `Elixir` as a prefix to +the module name and behaviour, because CLI is written in Elixir. +It should match `Elixir.RabbitMQ.CLI.(.*).Commands.(.*)Command` +And implement `Elixir.RabbitMQ.CLI.CommandBehaviour` + + +## The Actual Tutorial + +Let's write a command, that does something simple, e.g. deleting a queue. +We will use Elixir for that. + +First we need to declare a module with a behaviour, for example: + +``` +defmodule RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour +end +``` + +So far so good. But if we try to compile it, we'd see compilation errors: + +``` +warning: undefined behaviour function usage/0 (for behaviour RabbitMQ.CLI.CommandBehaviour) + lib/delete_queue_command.ex:1 + +warning: undefined behaviour function banner/2 (for behaviour RabbitMQ.CLI.CommandBehaviour) + lib/delete_queue_command.ex:1 + +warning: undefined behaviour function merge_defaults/2 (for behaviour RabbitMQ.CLI.CommandBehaviour) + lib/delete_queue_command.ex:1 + +warning: undefined behaviour function validate/2 (for behaviour RabbitMQ.CLI.CommandBehaviour) + lib/delete_queue_command.ex:1 + +warning: undefined behaviour function run/2 (for behaviour RabbitMQ.CLI.CommandBehaviour) + lib/delete_queue_command.ex:1 + +warning: undefined behaviour function output/2 (for behaviour RabbitMQ.CLI.CommandBehaviour) + lib/delete_queue_command.ex:1 +``` + +So some functions are missing. Let's implement them. + + +### Usage: Help Section + +We'll start with +the `usage/0` function, to provide command name in the help section: + +``` + def usage(), do: "delete_queue queue_name [--if-empty|-e] [--if-unused|-u] [--vhost|-p vhost]" +``` + +### CLI Argument Parsing: Switches, Positional Arguments, Aliases + +We want our command to accept a `queue_name` positional argument, +and two named arguments (flags): `if_empty` and `if_unused`, +and a `vhost` argument with a value. + +We also want to specify shortcuts to our named arguments so that the user can use +`-e` instead of `--if-empty`. + +We'll next implement the `switches/0` and `aliases/0` functions to let CLI know how it +should parse command line arguments for this command: + +``` + def switches(), do: [if_empty: :boolean, if_unused: :boolean] + def aliases(), do: [e: :if_empty, u: :is_unused] +``` + +Switches specify long arguments names and types, aliases specify shorter names. + +You might have noticed there is no `vhost` switch there. It's because `vhost` is a global +switch and will be available to all commands in the CLI: after all, many things +in RabbitMQ are scoped per vhost. + +Both `switches/0` and `aliases/0` callbacks are optional. +If your command doesn't have shorter argument names, you can omit `aliases/0`. +If the command doesn't have any named arguments at all, you can omit both functions. + +We've described how the CLI should parse commands, now let's start describing what +the command should do. + +### Command Banner + +We start with the `banner/2` function, that tells a user what the command is going to do. +If you call the command with with `--dry-run` argument, it would only print the banner, +without executing the actual command: + +``` + def banner([qname], %{vhost: vhost, + if_empty: if_empty, + if_unused: if_unused}) do + if_empty_str = case if_empty do + true -> "if queue is empty" + false -> "" + end + if_unused_str = case if_unused do + true -> "if queue is unused" + false -> "" + end + "Deleting queue #{qname} on vhost #{vhost} " <> + Enum.join([if_empty_str, if_unused_str], " and ") + end + +``` + +The function above can access arguments and command flags (named arguments) +to decide what exactly it should do. + +### Default Argument Values and Argument Validation + +As you can see, the `banner/2` function accepts exactly one argument and expects +the `vhost`, `if_empty` and `if_unused` options. +To make sure the command have all the correct arguments, you can use +the `merge_defaults/2` and `validate/2` functions: + +``` + def merge_defaults(args, options) do + { + args, + Map.merge(%{if_empty: false, if_unused: false, vhost: "/"}, options) + } + end + + def validate([], _options) do + {:validation_failure, :not_enough_args} + end + def validate([_,_|_], _options) do + {:validation_failure, :too_many_args} + end + def validate([""], _options) do + { + :validation_failure, + {:bad_argument, "queue name cannot be empty string."} + } + end + def validate([_], _options) do + :ok + end +``` + +The `merge_defaults/2` function accepts positional and options and returns a tuple +with effective arguments and options that will be passed on to `validate/2`, +`banner/2` and `run/2`. + +The `validate/2` function can return either `:ok` (just the atom) or a +tuple in the form of `{:validation_failure, error}`. The function above checks +that we have exactly one position argument and that it is not empty. + +While this is not enforced, for a command to be practical +at least one `validate/2` head must return `:ok`. + + +### Command Execution + +`validate/2` is useful for command line argument validation but there can be +other things that require validation before a command can be executed. For example, +a command may require a RabbitMQ node to be running (or stopped), a file to exist +and be readable, an environment variable to be exported and so on. + +There's another validation function, `validate_execution_environment/2`, for +such cases. That function accepts the same arguments and must return either `:ok` +or `{:validation_failure, error}`. What's the difference, you may ask? +`validate_execution_environment/2` is optional. + +To perform the actual command operation, the `run/2` command needs to be defined: + +``` + def run([qname], %{node: node, vhost: vhost, + if_empty: if_empty, if_unused: if_unused}) do + ## Generate the queue resource name from queue name and vhost + queue_resource = :rabbit_misc.r(vhost, :queue, qname) + ## Lookup the queue on broker node using resource name + case :rabbit_misc.rpc_call(node, :rabbit_amqqueue, :lookup, + [queue_resource]) do + {:ok, queue} -> + ## Delete the queue + :rabbit_misc.rpc_call(node, :rabbit_amqqueue, :delete, + [queue, if_empty, if_unused]); + {:error, _} = error -> error + end + end +``` + +In the example above we delegate to a `:rabbit_misc` function in `run/2`. You can use any functions +from [rabbit_common](https://github.com/rabbitmq/rabbitmq-common) directly but to +do something on a broker (remote) node, you need to use RPC calls. +It can be the standard Erlang `rpc:call` set of functions or `rabbit_misc:rpc_call/4`. +The latter is used by all standard commands and is generally recommended. + +Target RabbitMQ node name is passed in as the `node` option, which is +a global option and is available to all commands. + + +### Command Output + +Finally we would like to present the user with a command execution result. +To do that, we'll define `output/2` to format the `run/2` return value: + +``` + def output({:error, :not_found}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Queue not found"} + end + def output({:error, :not_empty}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Queue is not empty"} + end + def output({:error, :in_use}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Queue is in use"} + end + def output({:ok, queue_length}, _options) do + {:ok, "Queue was successfully deleted with #{queue_length} messages"} + end + ## Use default output for all other cases + use RabbitMQ.CLI.DefaultOutput +``` + +We have function clauses for every possible output of `rabbit_amqqueue:delete/3` used +in the `run/2` function. + +For a run to be successful, the `output/2` function should return a pair of `{:ok, result}`, +and to indicate an error it should return a `{:error, exit_code, message}` tuple. +`exit_code` must be an integer and `message` is a string or a list of strings. + +CLI program will exit with an `exit_code` in case of an error, or `0` in case of a success. + +`RabbitMQ.CLI.DefaultOutput` is a module which can handle common error cases +(e.g. `badrpc` when the target RabbitMQ node cannot be contacted or authenticated with using the Erlang cookie). + +In the example above, we use Elixir's `use` statement to import +function clauses for `output/2` from the `DefaultOutput` module. For +some commands such delegation will be sufficient. + +### Testing the Command + +That's it. Now you can add this command to your plugin, compile it, enable the plugin and run + +`rabbitmqctl delete_queue my_queue --vhost my_vhost` + +to delete a queue. + + +## Full Module Example in Elixir + +Full module definition in Elixir: + +``` +defmodule RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [if_empty: :boolean, if_unused: :boolean] + def aliases(), do: [e: :if_empty, u: :is_unused] + + def usage(), do: "delete_queue queue_name [--if_empty|-e] [--if_unused|-u]" + + def banner([qname], %{vhost: vhost, + if_empty: if_empty, + if_unused: if_unused}) do + if_empty_str = case if_empty do + true -> "if queue is empty" + false -> "" + end + if_unused_str = case if_unused do + true -> "if queue is unused" + false -> "" + end + "Deleting queue #{qname} on vhost #{vhost} " <> + Enum.join([if_empty_str, if_unused_str], " and ") + end + + def merge_defaults(args, options) do + { + args, + Map.merge(%{if_empty: false, if_unused: false, vhost: "/"}, options) + } + end + + def validate([], _options) do + {:validation_failure, :not_enough_args} + end + def validate([_,_|_], _options) do + {:validation_failure, :too_many_args} + end + def validate([""], _options) do + { + :validation_failure, + {:bad_argument, "queue name cannot be empty string."} + } + end + def validate([_], _options) do + :ok + end + + def run([qname], %{node: node, vhost: vhost, + if_empty: if_empty, if_unused: if_unused}) do + ## Generate queue resource name from queue name and vhost + queue_resource = :rabbit_misc.r(vhost, :queue, qname) + ## Lookup a queue on broker node using resource name + case :rabbit_misc.rpc_call(node, :rabbit_amqqueue, :lookup, + [queue_resource]) do + {:ok, queue} -> + ## Delete queue + :rabbit_misc.rpc_call(node, :rabbit_amqqueue, :delete, + [queue, if_unused, if_empty, "cli_user"]); + {:error, _} = error -> error + end + end + + def output({:error, :not_found}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Queue not found"} + end + def output({:error, :not_empty}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Queue is not empty"} + end + def output({:error, :in_use}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Queue is in use"} + end + def output({:ok, qlen}, _options) do + {:ok, "Queue was successfully deleted with #{qlen} messages"} + end + ## Use default output for all non-special case outputs + use RabbitMQ.CLI.DefaultOutput +end +``` + +## Full Module Example in Erlang + +The same module implemented in Erlang. Note the fairly +unusual Elixir module and behaviour names: since they contain +dots, they must be escaped with single quotes to be valid Erlang atoms: + +``` +-module('Elixir.RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand'). + +-behaviour('Elixir.RabbitMQ.CLI.CommandBehaviour'). + +-export([switches/0, aliases/0, usage/0, + banner/2, merge_defaults/2, validate/2, run/2, output/2]). + +switches() -> [{if_empty, boolean}, {if_unused, boolean}]. +aliases() -> [{e, if_empty}, {u, is_unused}]. + +usage() -> <<"delete_queue queue_name [--if_empty|-e] [--if_unused|-u] [--vhost|-p vhost]">>. + +banner([Qname], #{vhost := Vhost, + if_empty := IfEmpty, + if_unused := IfUnused}) -> + IfEmptyStr = case IfEmpty of + true -> ["if queue is empty"]; + false -> [] + end, + IfUnusedStr = case IfUnused of + true -> ["if queue is unused"]; + false -> [] + end, + iolist_to_binary( + io_lib:format("Deleting queue ~s on vhost ~s ~s", + [Qname, Vhost, + string:join(IfEmptyStr ++ IfUnusedStr, " and ")])). + +merge_defaults(Args, Options) -> + { + Args, + maps:merge(#{if_empty => false, if_unused => false, vhost => <<"/">>}, + Options) + }. + +validate([], _Options) -> + {validation_failure, not_enough_args}; +validate([_,_|_], _Options) -> + {validation_failure, too_many_args}; +validate([<<"">>], _Options) -> + { + validation_failure, + {bad_argument, <<"queue name cannot be empty string.">>} + }; +validate([_], _Options) -> ok. + +run([Qname], #{node := Node, vhost := Vhost, + if_empty := IfEmpty, if_unused := IfUnused}) -> + %% Generate queue resource name from queue name and vhost + QueueResource = rabbit_misc:r(Vhost, queue, Qname), + %% Lookup a queue on broker node using resource name + case rabbit_misc:rpc_call(Node, rabbit_amqqueue, lookup, [QueueResource]) of + {ok, Queue} -> + %% Delete queue + rabbit_misc:rpc_call(Node, rabbit_amqqueue, delete, + [Queue, IfUnused, IfEmpty, <<"cli_user">>]); + {error, _} = Error -> Error + end. + +output({error, not_found}, _Options) -> + { + error, + 'Elixir.RabbitMQ.CLI.Core.ExitCodes':exit_usage(), + <<"Queue not found">> + }; +output({error, not_empty}, _Options) -> + { + error, + 'Elixir.RabbitMQ.CLI.Core.ExitCodes':exit_usage(), + <<"Queue is not empty">> + }; +output({error, in_use}, _Options) -> + { + error, + 'Elixir.RabbitMQ.CLI.Core.ExitCodes':exit_usage(), + <<"Queue is in use">> + }; +output({ok, qlen}, _Options) -> + {ok, <<"Queue was successfully deleted with #{qlen} messages">>}; +output(Other, Options) -> + 'Elixir.RabbitMQ.CLI.DefaultOutput':output(Other, Options, ?MODULE). +``` + +## Wrapping Up + +Phew. That's it! Implementing a new CLI command wasn't too difficult. +That's because extensibility was one of the goals of this new CLI tool suite. + + +## Feedback and Getting Help + +If you have any feedback about CLI tools extensibility, +don't hesitate to reach out on the [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + diff --git a/deps/rabbitmq_cli/CONTRIBUTING.md b/deps/rabbitmq_cli/CONTRIBUTING.md new file mode 100644 index 0000000000..672b29d305 --- /dev/null +++ b/deps/rabbitmq_cli/CONTRIBUTING.md @@ -0,0 +1,143 @@ +Thank you for using RabbitMQ and for taking the time to contribute to the project. +This document has two main parts: + + * when and how to file GitHub issues for RabbitMQ projects + * how to submit pull requests + +They intend to save you and RabbitMQ maintainers some time, so please +take a moment to read through them. + +## Overview + +### GitHub issues + +The RabbitMQ team uses GitHub issues for _specific actionable items_ that +engineers can work on. This assumes the following: + +* GitHub issues are not used for questions, investigations, root cause + analysis, discussions of potential issues, etc (as defined by this team) +* Enough information is provided by the reporter for maintainers to work with + +The team receives many questions through various venues every single +day. Frequently, these questions do not include the necessary details +the team needs to begin useful work. GitHub issues can very quickly +turn into a something impossible to navigate and make sense +of. Because of this, questions, investigations, root cause analysis, +and discussions of potential features are all considered to be +[mailing list][rmq-users] material. If you are unsure where to begin, +the [RabbitMQ users mailing list][rmq-users] is the right place. + +Getting all the details necessary to reproduce an issue, make a +conclusion or even form a hypothesis about what's happening can take a +fair amount of time. Please help others help you by providing a way to +reproduce the behavior you're observing, or at least sharing as much +relevant information as possible on the [RabbitMQ users mailing +list][rmq-users]. + +Please provide versions of the software used: + + * RabbitMQ server + * Erlang + * Operating system version (and distribution, if applicable) + * All client libraries used + * RabbitMQ plugins (if applicable) + +The following information greatly helps in investigating and reproducing issues: + + * RabbitMQ server logs + * A code example or terminal transcript that can be used to reproduce + * Full exception stack traces (a single line message is not enough!) + * `rabbitmqctl report` and `rabbitmqctl environment` output + * Other relevant details about the environment and workload, e.g. a traffic capture + * Feel free to edit out hostnames and other potentially sensitive information. + +To make collecting much of this and other environment information, use +the [`rabbitmq-collect-env`][rmq-collect-env] script. It will produce an archive with +server logs, operating system logs, output of certain diagnostics commands and so on. +Please note that **no effort is made to scrub any information that may be sensitive**. + +### Pull Requests + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +Here's the recommended workflow: + + * [Fork the repository][github-fork] or repositories you plan on contributing to. If multiple + repositories are involved in addressing the same issue, please use the same branch name + in each repository + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests (usually with `make tests`), commit with a + [descriptive message][git-commit-msgs], push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement][ca-agreement] if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first +ask the core team for their opinion on the [RabbitMQ users mailing list][rmq-users]. + +## Running Tests + +Assuming you have: + +* Installed [Elixir](http://elixir-lang.org/install.html) +* Have a local running RabbitMQ node with the `rabbitmq-federation` and `rabbitmq_stomp` plugins enabled, e.g. + +``` +make run-broker PLUGINS='rabbitmq_federation rabbitmq_stomp' +``` + +from a server release repository clone, use + +``` +make tests +``` + +to run all tests. + +### Running a Single Test Case + +To run a single test case, use `make test` like so: + +``` +make TEST_FILE=test/help_command_test.exs test +``` + +And if you want to run in verbose mode, set the `V` make variable: + +``` +make TEST_FILE=test/help_command_test.exs V=1 test +``` + +NOTE: You may see the following message several times: + +``` +warning: variable context is unused +``` + +This is nothing to be alarmed about; we're currently using setup context +functions in Mix to start a new distributed node and connect it to the RabbitMQ +server. It complains because we don't actually use the context dictionary, but +it's fine otherwise. + +## Code of Conduct + +See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md). + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed +copy of our [Contributor Agreement][ca-agreement] around the time you +submit your pull request. This will make it much easier (in some +cases, possible) for the RabbitMQ team at Pivotal to merge your +contribution. + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list][rmq-users]. + +[rmq-collect-env]: https://github.com/rabbitmq/support-tools/blob/master/scripts/rabbitmq-collect-env +[git-commit-msgs]: https://chris.beams.io/posts/git-commit/ +[rmq-users]: https://groups.google.com/forum/#!forum/rabbitmq-users +[ca-agreement]: https://cla.pivotal.io/sign/rabbitmq +[github-fork]: https://help.github.com/articles/fork-a-repo/ diff --git a/deps/rabbitmq_cli/DESIGN.md b/deps/rabbitmq_cli/DESIGN.md new file mode 100644 index 0000000000..4179048159 --- /dev/null +++ b/deps/rabbitmq_cli/DESIGN.md @@ -0,0 +1,483 @@ +# New (3.7.0+) RabbitMQ CLI Tools + +## Summary + +RabbitMQ version 3.7 comes with brave new CLI tool to replace `rabbitmqctl`. + +Some of the issues in the older tool suite we wanted to address: + + * Built-in into RabbitMQ server code + * Home-grown argument parser + * Started with `erl` with a lot of installation-dependent parameters + * Too many commands in a single tool + * All commands in resided the same module (function clauses) + +All this made it hard to maintain and extend the tools. + +The new CLI is different in a number of ways that address +the above issues: + + * Standalone [repository on GitHub](https://github.com/rabbitmq/rabbitmq-cli). + * Implemented in Elixir (using Elixir's standard CLI parser) + * Each command is its own module + * Extensible + * Commands can support more output formats (e.g. JSON and CSV) + * A single executable file that bundles all dependencies but the Erlang runtime + * Command scopes associate commands with tools (e.g. `rabbitmq-diagnostics` reuses relevant commands from `rabbitmqctl`) + +## Architecture + +Each command is defined in its own module and implements an Elixir (Erlang) behaviour. +(See [Command behaviour](#command-behaviour)) + +Output is processed by a formatter and printer which formats command output +and render the output (the default being the standard I/O output). +(see [Output formatting](#output-formatting)) + +CLI core consists of several modules implementing command execution process: + + * `RabbitMQCtl`: entry point. Generic execution logic. + * `Parser`: responsible for command line argument parsing (drives Elixir's `OptionParser`) + * `CommandModules`: responsible for command module discovery and loading + * `Config`: responsible for config unification: merges environment variable and command argument values + * `Output`: responsible for output formatting + * `Helpers`: self-explanatory + +### Command Execution Process + +#### Arguments parsing + +Command line arguments are parsed with [OptionParser](https://elixir-lang.org/docs/stable/elixir/OptionParser.html) +Parser returns a list of unnamed arguments and a map of options (named arguments) +First unnamed argument is a command name. +Named arguments can be global or command specific. +Command specific argument names and types are specified in the `switches/0` callback. +Global argument names are described in [Global arguments] + +#### Command discovery + +If arguments list is not empty, its first element is considered a command name. +Command name is converted to CamelCase and a module with +`RabbitMQ.CLI.*.Commands.<CommandName>Command` name is selected as a command module. + +List of available command modules depend on current tool scope +(see [Command scopes](#command-scopes)) + +#### Defaults and validation + +After the command module is found, effective command arguments are calculated by + merging global defaults and command specific defaults for both unnamed and named arguments. + +A command specifies defaults using the `merge_defaults/2` callback + (see [Command behaviour](#command-behaviour)) + +Arguments are then validated using the `validate/2` callback. `validate_execution_environment/2` is +another (optional) validation function with the same signature as `validate/2`. It is meant to +validate everything that is not CLI arguments, for example, whether a file exists or RabbitMQ is running +or stopped on the target node. + +##### Command Aliases + +It is possible to define aliases for commands using an aliases file. The file name can be +specified using `RABBITMQ_CLI_ALIASES_FILE` environment variable or the `--aliases-file` +command lineargument. + +Aliases can be specified using `alias = command [options]` format. +For example: + +``` +lq = list_queues +lq_vhost1 = list_queues -p vhost1 +lq_off = list_queues --offline +``` + +with such aliases config file running + +``` +RABBITMQ_CLI_ALIASES_FILE=/path/to/aliases.conf rabbitmqctl lq +``` + +will be equivalent to executing + +``` +RABBITMQ_CLI_ALIASES_FILE=/path/to/aliases.conf rabbitmqctl list_queues +``` + +while + +``` +RABBITMQ_CLI_ALIASES_FILE=/path/to/aliases.conf rabbitmqctl lq_off +``` + +is the same as running + +``` +RABBITMQ_CLI_ALIASES_FILE=/path/to/aliases.conf rabbitmqctl list_queues --offline +``` + +Builtin or plugin-provided commands are looked up first, if that yields no result, +aliases are inspected. Therefore it's not possible to override a command by +configuring an alias. + +Just like command lookups, alias expansion happens in the `RabbitMQ.CLI.Core.Parser` +module. + + +##### Command Aliases with Variables + +Aliases can also contain arguments. Command name must be the first word after the `=`. +Arguments specified in an alias will precede those passed from the command line. + +For example, if you specify the alias `passwd_user1 = change_password user1`, +you can call it with `rabbitmqctl passwd_user1 new_password`. + +Combined with the `eval` command, aliases can be a powerful tool for users who cannot or don't want +to develop, distribute and deploy their own commands via plugins. + +For instance, the following alias deletes all queues in a vhost: + +``` +delete_vhost_queues = eval '[rabbit_amqqueue:delete(Q, false, false, <<"rabbit-cli">>) || Q <- rabbit_amqqueue:list(_1)]' +``` + +This command will require a single positional argument for the vhost: + +``` +rabbitmqctl delete_vhost_queues vhost1 +``` + +It is also possible to use a named vhost argument by specifying an underscore +variable that's not an integer: + +``` +delete_vhost_queues = eval '[rabbit_amqqueue:delete(Q, false, false, <<"rabbit-cli">>) || Q <- rabbit_amqqueue:list(_vhost)]' +``` + +Then the alias can be called like this: + +``` +rabbitmqctl delete_vhost_queues -p vhost1 +# or +rabbitmqctl delete_vhost_queues ---vhost <vhost>1 +``` + +Keep in mind that `eval` command can accept only [global arguments](#global-arguments) as named, +and it's advised to use positional arguments instead. + +Numbered arguments will be passed to the eval'd code as Elixir strings (or Erlang binaries). +The code that relies on them should perform type conversion as necessary, e.g. by +using functions from the [`rabbit_data_coercion` module](https://github.com/rabbitmq/rabbitmq-common/blob/stable/src/rabbit_data_coercion.erl). + +#### Command execution + + Command is executed with the `run/2` callback, which contains main command logic. + This callback can return any value. + + Output from the `run/2` callback is being processed with the `output/2` callback, + which should format the output to a specific type. + (see [Output formatting](#output-formatting)) + + Output callback can return an error, with a specific exit code. + +#### Printing and formatting + + The `output/2` callback return value is being processed in `output.ex` module by + by the appropriate formatter and printer. + + A formatter translates the output value to a sequence of strings or error value. + Example formatters are `json`, `csv` and `erlang` + + A printer renders formatted strings to an output device (e.g. stdout, file) + +#### Return + + Errors during execution (e.g. validation failures, command errors) are being printed + to `stderr`. + + If command has failed to execute, a non-zero code is returned. + + +## Usage + +CLI tool is an Elixir Mix application. It is compiled into an escript +executable file. + +This file embeds Elixir, rabbit_common, and rabbitmqcli applications and can be executed anywhere +where `escript` is installed (it comes as a part of `erlang`). + +Although, some commands require rabbitmq broker code and data directory to be known. +For example, commands in `rabbitmq-plugins` tool and those controlling clustering +(e.g. `forget_cluster_node`, `rename_cluster_node` ...) + +Those directories can be defined using environment options or rabbitmq environment variables. + +In the broker distribution the escript file is called from a shell/cmd script, which loads +broker environment and exports it into the script. + +Environment variables also specify the locations of the enabled plugins file +and the plugins directory. + +All enabled plugins will be searched for commands. Commands from enabled plugins will +be shown in usage and available for execution. + +### Global Arguments + +#### Commonly Used Arguments + + * node (n): atom, broker node name, defaults to `rabbit@<current host>` + * quiet (q): boolean, if set to `true`, command banner is not shown, defaults to `false` + * timeout (t): integer, timeout value in **seconds** (used in some commands), defaults to `infinity` + * vhost (p): string, vhost to talk to, defaults to `/` + * formatter: string, formatter to use to format command output. (see [Output formatting](#output-formatting)) + * printer: string, printer to render output (see [Output formatting](#output-formatting)) + * dry-run: boolean, if specified the command will not run, but print banner only + +#### Environment Arguments + + * script-name: atom, configurable tool name (`rabbitmq-plugins`, `rabbitmqctl`) to select command scope (see [Command scopes](#command-scopes)) + * rabbitmq-home: string, broker install directory + * mnesia-dir: string, broker mnesia data directory + * plugins-dir: string, broker plugins directory + * enabled-plugins-file: string, broker enabled plugins file + * longnames (l): boolean, use longnames to communicate with broker erlang node. Should be set to `true` only if broker is started with longnames. + * aliases-file: string, a file name to load aliases from + * erlang-cookie: atom, an [erlang distribution cookie](http://erlang.org/doc/reference_manual/distributed.html) + +Environment argument defaults are loaded from rabbitmq environment variables (see [Environment configuration](#environment-configuration)). + +Some named arguments have single-letter aliases (in parenthesis). + +Boolean options without a value are parsed as `true` + +For example, parsing command + + rabbitmqctl list_queues --vhost my_vhost -t 10 --formatter=json name pid --quiet + +Will result with unnamed arguments list `["list_queues", "name", "pid"]` +and named options map `%{vhost: "my_vhost", timeout: 10, quiet: true}` + + +### Usage (Listing Commands in `help`) + +Usage is shown when the CLI is called without any arguments, or if there are some +problems parsing arguments. + +In that cases exit code is `64`. + +If you want to show usage and return `0` exit code run `help` command + + rabbitmqctl help + +Each tool (`rabbitmqctl`, `rabbitmq-plugins`) shows its scope of commands (see [Command scopes](#command-scopes)) + +## Command Behaviour + +Each command is implemented as an Elixir (or Erlang) module. Command module should +implement `RabbitMQ.CLI.CommandBehaviour` behaviour. + +Behaviour summary: + +Following functions MUST be implemented in a command module: + + usage() :: String.t | [String.t] + +Command usage string, or several strings (one per line) to print in command listing in usage. +Typically looks like `command_name [arg] --option=opt_value` + + banner(arguments :: List.t, options :: Map.t) :: String.t + +Banner to print before the command execution. +Ignored if argument `--quiet` is specified. +If `--dry-run` argument is specified, th CLI will only print the banner. + + merge_defaults(arguments :: List.t, options :: Map.t) :: {List.t, Map.t} + +Merge default values for arguments and options (named arguments). +Returns a tuple with effective arguments and options, that will be passed to `validate/2` and `run/2` + + validate(arguments :: List.t, options :: Map.t) :: :ok | {:validation_failure, Atom.t | {Atom.t, String.t}} + +Validate effective arguments and options. + +If function returns `{:validation_failure, err}` +CLI will print usage to `stderr` and exit with non-zero exit code (typically 64). + + run(arguments :: List.t, options :: Map.t) :: run_result :: any + +Run command. This function usually calls RPC on broker. + + output(run_result :: any, options :: Map.t) :: :ok | {:ok, output :: any} | {:stream, Enum.t} | {:error, ExitCodes.exit_code, [String.t]} + +Cast the return value of `run/2` command to a formattable value and an exit code. + +- `:ok` - return `0` exit code and won't print anything + +- `{:ok, output}` - return `exit` code and print output with `format_output/2` callback in formatter + +- `{:stream, stream}` - format with `format_stream/2` callback in formatter, iterating over enumerable elements. +Can return non-zero code, if error occurs during stream processing +(stream element for error should be `{:error, Message}`). + +- `{:error, exit_code, strings}` - print error messages to `stderr` and return `exit_code` code + +There is a default implementation for this callback in `DefaultOutput` module + +Most of the standard commands use the default implementation via `use RabbitMQ.CLI.DefaultOutput` + +Following functions are optional: + + switches() :: Keyword.t + +Keyword list of switches (argument names and types) for the command. + +For example: `def switches(), do: [offline: :boolean, time: :integer]` will +parse `--offline --time=100` arguments to `%{offline: true, time: 100}` options. + +This switches are added to global switches (see [Arguments parsing](#arguments-parsing)) + + aliases() :: Keyword.t + +Keyword list of argument names one-letter aliases. +For example: `[o: :offline, t: :timeout]` +(see [Arguments parsing](#arguments-parsing)) + + usage_additional() :: String.t | [String.t] + +Additional usage strings to print after all commands basic usage. +Used to explain additional arguments and not interfere with command listing. + + formatter() :: Atom.t + +Default formatter for the command. +Should be a module name of a module implementing `RabbitMQ.CLI.FormatterBehaviour` (see [Output formatting](#output-formatting)) + + scopes() :: [Atom.t] + +List of scopes to include command in. (see [Command scopes](#command-scopes)) + +More information about command development can be found in [the command tutorial](COMMAND_TUTORIAL.md) + +## Command Scopes + +Commands can be organized in scopes to be used in different tools +like `rabbitmq-diagnostics` or `rabbitmq-plugins`. + +One command can belong to multiple scopes. Scopes for a command can be +defined in the `scopes/0` callback in the command module. +Each scope is defined as an atom value. + +By default a command scope is selected using naming convention. +If command module is called `RabbitMQ.CLI.MyScope.Commands.DoSomethingCommand`, it will belong to +`my_scope` scope. A scope should be defined in snake_case. Namespace for the scope will be translated to CamelCase. + +When CLI is run, a scope is selected by a script name, which is the escript file name +or the `--script-name` argument passed. + +A script name is associated with a single scope in the application environment: + +Script names for scopes: + + * `rabbitmqctl` - `:ctl` + * `rabbitmq-plugins` - `:plugins` + * `rabbitmq-diagnostics` - `:diagnostics` + +This environment is extended by plugins `:scopes` environment variables, +but cannot be overridden. Plugins scopes can override each other, +so should be used with caution. + +So all the commands in the `RabbitMQ.CLI.Ctl.Commands` namespace will be available +for `rabbitmqctl` script. +All the commands in the `RabbitMQ.CLI.Plugins.Commands` namespace will be available +for `rabbitmq-plugins` script. + +To add a command to `rabbitmqctl`, one should either name it with +the `RabbitMQ.CLI.Ctl.Commands` prefix or add the `scopes()` callback, +returning a list with `:ctl` element + +## Output Formatting + +The CLI supports extensible output formatting. Formatting consists of two stages: + + * formatting - translating command output to a sequence of lines + * printing - outputting the lines to some device (e.g. stdout or filesystem) + +A formatter module performs formatting. +Formatter is a module, implementing the `RabbitMQ.CLI.FormatterBehaviour` behaviour: + + format_output(output :: any, options :: Map.t) :: String.t | [String.t] + +Format a single value, returned. It accepts output from command and named arguments (options) +and returns a list of strings, that should be printed. + + format_stream(output_stream :: Enumerable.t, options :: Map.t) :: Enumerable.t + +Format a stream of return values. This function uses elixir +Stream [https://elixir-lang.org/docs/stable/elixir/Stream.html] abstraction +to define processing of continuous data, so the CLI can output data in realtime. + +Used in `list_*` commands, that emit data asynchronously. + +`DefaultOutput` will return all enumerable data as stream, +so it will be formatted with this function. + +A printer module performs printing. Printer module should implement +the `RabbitMQ.CLI.PrinterBehaviour` behaviour: + + init(options :: Map.t) :: {:ok, printer_state :: any} | {:error, error :: any} + +Init the internal printer state (e.g. open a file handler). + + finish(printer_state :: any) :: :ok + +Finalize the internal printer state. + + print_output(output :: String.t | [String.t], printer_state :: any) :: :ok + +Print the output lines in the printer state context. +Is called for `{:ok, val}` command output after formatting `val` using formatter, +and for each enumerable element of `{:stream, enum}` enumerable + + print_ok(printer_state :: any) :: :ok + +Print an output without any values. Is called for `:ok` command return value. + +Output formatting logic is defined in `output.ex` module. + +Following rules apply for a value, returned from `output/2` callback: + + * `:ok` - initializes a printer, calls `print_ok` and finishes the printer. Exit code is `0`. + * `{:ok, value}` - calls `format_output/2` from formatter, then passes the value to printer. Exit code is `0`. + * `{:stream, enum}` - calls `format_stream/2` to augment the stream with formatting logic, +initializes a printer, then calls `print_output/2` for each successfully formatted stream +element (which is not `{:error, msg}`). Exit code is `0` if there were no errors. +In case of an error element, stream processing stops, error is printed to `stderr` +and the CLI exits with nonzero exit code. + * `{:error, exit_code, msg}` - prints `msg` to `stderr` and exits with `exit_code` + + +## Environment Configuration + +Some commands require information about the server environment to run correctly. +Such information is: + + * rabbitmq code directory + * rabbitmq mnesia directory + * enabled plugins file + * plugins directory + +Enabled plugins file and plugins directory are also used to locate plugins commands. + +This information can be provided using command line arguments (see [Environment arguments](#environment-arguments)) + +By default it will be loaded from environment variables, same as used in rabbitmq broker: + +| Argument name | Environment variable | +|----------------------|-------------------------------| +| rabbitmq-home | RABBITMQ_HOME | +| mnesia-dir | RABBITMQ_MNESIA_DIR | +| plugins-dir | RABBITMQ_PLUGINS_DIR | +| enabled-plugins-file | RABBITMQ_ENABLED_PLUGINS_FILE | +| longnames | RABBITMQ_USE_LONGNAME | +| node | RABBITMQ_NODENAME | +| aliases-file | RABBITMQ_CLI_ALIASES_FILE | +| erlang-cookie | RABBITMQ_ERLANG_COOKIE | diff --git a/deps/rabbitmq_cli/LICENSE b/deps/rabbitmq_cli/LICENSE new file mode 100644 index 0000000000..f2da65d175 --- /dev/null +++ b/deps/rabbitmq_cli/LICENSE @@ -0,0 +1,4 @@ +This package is licensed under the MPL 2.0. For the MPL 2.0, please see LICENSE-MPL-RabbitMQ. + +If you have any questions regarding licensing, please contact us at +info@rabbitmq.com. diff --git a/deps/rabbitmq_cli/LICENSE-MPL-RabbitMQ b/deps/rabbitmq_cli/LICENSE-MPL-RabbitMQ new file mode 100644 index 0000000000..14e2f777f6 --- /dev/null +++ b/deps/rabbitmq_cli/LICENSE-MPL-RabbitMQ @@ -0,0 +1,373 @@ +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. diff --git a/deps/rabbitmq_cli/Makefile b/deps/rabbitmq_cli/Makefile new file mode 100644 index 0000000000..856f39e605 --- /dev/null +++ b/deps/rabbitmq_cli/Makefile @@ -0,0 +1,160 @@ +PROJECT = rabbitmq_cli + +dep_observer_cli = git https://github.com/zhongwencool/observer_cli 1.4.4 + +BUILD_DEPS = rabbit_common +DEPS = observer_cli +TEST_DEPS = amqp_client rabbit + +DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk +DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk + +VERBOSE_TEST ?= true +MAX_CASES ?= 1 + +MIX_TEST_OPTS ?= "" +MIX_TEST = mix test --max-cases=$(MAX_CASES) + +ifneq ("",$(MIX_TEST_OPTS)) +MIX_TEST := $(MIX_TEST) $(MIX_TEST_OPTS) +endif + +ifeq ($(VERBOSE_TEST),true) +MIX_TEST := $(MIX_TEST) --trace +endif + +# FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be +# reviewed and merged. + +ERLANG_MK_REPO = https://github.com/rabbitmq/erlang.mk.git +ERLANG_MK_COMMIT = rabbitmq-tmp + +WITHOUT = plugins/cover \ + plugins/ct \ + plugins/dialyzer \ + plugins/eunit \ + plugins/proper \ + plugins/triq + +include rabbitmq-components.mk +include erlang.mk + +# rabbitmq-mix.mk is generated during the creation of the RabbitMQ +# source archive. It sets some environment variables to allow +# rabbitmq_cli to build offline, using the bundled sources only. +-include rabbitmq-mix.mk + +ACTUAL_ESCRIPTS = escript/rabbitmqctl +LINKED_ESCRIPTS = escript/rabbitmq-plugins \ + escript/rabbitmq-diagnostics \ + escript/rabbitmq-queues \ + escript/rabbitmq-streams \ + escript/rabbitmq-upgrade +ESCRIPTS = $(ACTUAL_ESCRIPTS) $(LINKED_ESCRIPTS) + +# Record the build and link dependency: the target files are linked to +# their first dependency. +rabbitmq-plugins = escript/rabbitmqctl +rabbitmq-diagnostics = escript/rabbitmqctl +rabbitmq-queues = escript/rabbitmqctl +rabbitmq-streams = escript/rabbitmqctl +rabbitmq-upgrade = escript/rabbitmqctl +escript/rabbitmq-plugins escript/rabbitmq-diagnostics escript/rabbitmq-queues escript/rabbitmq-streams escript/rabbitmq-upgrade: escript/rabbitmqctl + +# We use hardlinks or symlinks in the `escript` directory and +# install's PREFIX when a single escript can have several names (eg. +# rabbitmq-plugins, rabbitmq-plugins and rabbitmq-diagnostics). +# +# Hardlinks and symlinks work on Windows. However, symlinks require +# privileges unlike hardlinks. That's why we default to hardlinks, +# unless USE_SYMLINKS_IN_ESCRIPTS_DIR is set. +# +# The link_escript function is called as: +# $(call link_escript,source,target) +# +# The function assumes all escripts live in the same directory and that +# the source was previously copied in that directory. + +ifdef USE_SYMLINKS_IN_ESCRIPTS_DIR +link_escript = ln -sf "$(notdir $(1))" "$(2)" +else +link_escript = ln -f "$(dir $(2))$(notdir $(1))" "$(2)" +endif + +app:: $(ESCRIPTS) + @: + +rabbitmqctl_srcs := mix.exs \ + $(shell find config lib -name "*.ex" -o -name "*.exs") + +# Elixir dependencies are fetched and compiled as part of the alias +# `mix make_all`. We do not fetch and build them in `make deps` because +# mix(1) startup time is quite high. Thus we prefer to run it once, even +# though it kind of breaks the Erlang.mk model. +# +# We write `y` on mix stdin because it asks approval to install Hex if +# it's missing. Another way to do it is to use `mix local.hex` but it +# can't be integrated in an alias and doing it from the Makefile isn't +# practical. +# +# We also verify if the CLI is built from the RabbitMQ source archive +# (by checking if the Hex registry/cache is present). If it is, we use +# another alias. This alias does exactly the same thing as `make_all`, +# but calls `deps.get --only prod` instead of `deps.get`. This is what +# we do to create the source archive, and we must do the same here, +# otherwise mix(1) complains about missing dependencies (the non-prod +# ones). +$(ACTUAL_ESCRIPTS): $(rabbitmqctl_srcs) + $(gen_verbose) if test -d ../.hex; then \ + echo y | mix make_all_in_src_archive; \ + else \ + echo y | mix make_all; \ + fi + +$(LINKED_ESCRIPTS): + $(verbose) rm -f "$@" + $(gen_verbose) $(call link_escript,$<,$@) + +rel:: $(ESCRIPTS) + @: + +tests:: $(ESCRIPTS) + $(gen_verbose) $(MIX_TEST) $(TEST_FILE) + +.PHONY: test + +test:: $(ESCRIPTS) +ifdef TEST_FILE + $(gen_verbose) $(MIX_TEST) $(TEST_FILE) +else + $(verbose) echo "TEST_FILE must be set, e.g. TEST_FILE=./test/ctl" 1>&2; false +endif + +dialyzer:: $(ESCRIPTS) + MIX_ENV=test mix dialyzer + +.PHONY: install + +install: $(ESCRIPTS) +ifdef PREFIX + $(gen_verbose) mkdir -p "$(DESTDIR)$(PREFIX)" + $(verbose) $(foreach script,$(ACTUAL_ESCRIPTS), \ + cmp -s "$(script)" "$(DESTDIR)$(PREFIX)/$(notdir $(script))" || \ + cp "$(script)" "$(DESTDIR)$(PREFIX)/$(notdir $(script))";) + $(verbose) $(foreach script,$(LINKED_ESCRIPTS), \ + $(call link_escript,$($(notdir $(script))),$(DESTDIR)$(PREFIX)/$(notdir $(script)));) +else + $(verbose) echo "You must specify a PREFIX" 1>&2; false +endif + +clean:: clean-mix + +clean-mix: + $(gen_verbose) rm -f $(ESCRIPTS) + $(verbose) echo y | mix clean + +format: + $(verbose) mix format lib/**/*.ex + +repl: + $(verbose) iex --sname repl -S mix diff --git a/deps/rabbitmq_cli/README.md b/deps/rabbitmq_cli/README.md new file mode 100644 index 0000000000..1d1c1664d0 --- /dev/null +++ b/deps/rabbitmq_cli/README.md @@ -0,0 +1,129 @@ +# RabbitMQ CLI Tools + +[](https://travis-ci.org/rabbitmq/rabbitmq-cli) + +This repository contains [RabbitMQ CLI tools](https://rabbitmq.com/cli.html) ([rabbitmqctl](https://www.rabbitmq.com/man/rabbitmqctl.1.man.html) and +others). + +This generation of CLI tools first shipped with RabbitMQ `3.7.0`. + + +## Goals + +Team RabbitMQ wanted a set of tools that + + * Was extensible from/with plugins + * Supported pluggable output formats (in particular machine-friendly ones) + * Had good test coverage + * Wasn't as coupled to the server repository + * Could be used as a low risk vehicle for [Elixir](https://elixir-lang.org) evaluation + +## Supported RabbitMQ Versions + +Long lived branches in this repository track the same branch in RabbitMQ core and related +repositories. So `master` tracks `master` in rabbitmq-server, `v3.7.x` tracks branch `v3.7.x` in +rabbitmq-server and so on. + +Please use the version of CLI tools that come with the RabbitMQ distribution version installed. + + +## Building + +### Requirements + +Building this project requires + + * Erlang/OTP 21.3 (or later) + * [Elixir](https://elixir-lang.org/) 1.10.0 (or later). + +Command line tools depend on [rabbitmq-common](https://github.com/rabbitmq/rabbitmq-common). +Dependencies are being resolved by `erlang.mk` + +### Building Standalone Executables + +This repo produces a `rabbitmqctl` executable which can be used as different tools +(`rabbitmq-plugins`, `rabbitmq-diagnostics`, `rabbitmq-queues`, `rabbitmq-streams`, `rabbitmq-upgrade`) by copying or symlinking it with different names. +Depending on the name, a different set of commands will be loaded and available, including +for `--help`. + +To generate the executable, run + +``` +make +``` + +## Usage + +### `rabbitmqctl` + +See `rabbitmqctl help` and [rabbitmqctl man page](https://www.rabbitmq.com/man/rabbitmqctl.1.man.html) for details. + +### `rabbitmq-plugins` + +See `rabbitmq-plugins help` and [rabbitmq-plugins man page](https://www.rabbitmq.com/man/rabbitmq-plugins.1.man.html) for details. + +### `rabbitmq-diagnostics` + +See `rabbitmq-diagnostics help` and [rabbitmq-diagnostics man page](https://www.rabbitmq.com/rabbitmq-diagnostics.8.html). + + + + +## Testing + +See [CONTRIBUTING.md](CONTRIBUTING.md). + + +## Developing + +### Adding a New Command + +#### Conventions + +RabbitMQ CLI tools use module name conventions to match the command-line +actions (commands) to modules. The convention is outlined in the `CommandBehaviour` module. + +#### Command Module Interface + +Each command module must implement the `RabbitMQ.CLI.CommandBehaviour` behaviour, +which includes the following functions: + + * `validate(args, opts)`, which returns either `:ok` or a tuple of `{:validation_failure, failure_detail}` where failure detail is typically one of: `:too_many_args`, `:not_enough_args` or `{:bad_argument, String.t}`. + + * `merge_defaults(args, opts)`, which is used to return updated arguments and/or options. + + * `run(args, opts)`, where the actual command is implemented. Here, `args` is a list of command-specific parameters and `opts` is a Map containing option flags. + + * `usage`, which returns a string describing the command, its arguments and its optional flags. + * `banner(args, opts)`, which returns a string to be printed before the command output. + +There are also a number of optional callbacks: + + * `switches`, which returns command specific switches. + * `aliases`, which returns a list of command aliases (if any). + * `formatter`: what output formatter should be used by default. + * `usage_additional`: extra values appended to the `usage` output + to provide additional command-specific documentation. + * `scopes`: what scopes this command appears in. Scopes associate + tools (e.g. `rabbitmqctl`, `rabbitmq-diagnostics`, `rabbitmq-queues`, `rabbitmq-streams`) with commands. + * `distribution`: control erlang distribution. + Can be `:cli` (default), `:none` or `{:fun, fun}` + +### Tutorial + +We have [a tutorial](./COMMAND_TUTORIAL.md) that demonstrates how to add a CLI +command that deletes a queue. + +### Examples + +See `lib/rabbitmq/cli/ctl/commands/status_command.ex` and `test/status_command_test.exs` for minimalistic +but not entirely trivial examples. + + +## Copyright and License + +The project is [licensed under the MPL](LICENSE-MPL-RabbitMQ), the same license +as RabbitMQ. + +(c) 2007-2020 VMware, Inc. or its affiliates. + diff --git a/deps/rabbitmq_cli/config/config.exs b/deps/rabbitmq_cli/config/config.exs new file mode 100644 index 0000000000..0a88337ac6 --- /dev/null +++ b/deps/rabbitmq_cli/config/config.exs @@ -0,0 +1,37 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +use Mix.Config + +# This configuration is loaded before any dependency and is restricted +# to this project. If another project depends on this project, this +# file won't be loaded nor affect the parent project. For this reason, +# if you want to provide default values for your application for +# 3rd-party users, it should be done in your "mix.exs" file. + +# You can configure for your application as: +# +# config :rabbitmqctl, key: :value +# +# And access this configuration in your application as: +# +# Application.get_env(:rabbitmqctl, :key) +# +# Or configure a 3rd-party app: +# +config :logger, [level: :warn, console: [device: :standard_error]] +# + +# It is also possible to import configuration files, relative to this +# directory. For example, you can emulate configuration per environment +# by uncommenting the line below and defining dev.exs, test.exs and such. +# Configuration from the imported file will override the ones defined +# here (which is why it is important to import them last). +# +# import_config "#{Mix.env}.exs" diff --git a/deps/rabbitmq_cli/erlang.mk b/deps/rabbitmq_cli/erlang.mk new file mode 100644 index 0000000000..77933a9acf --- /dev/null +++ b/deps/rabbitmq_cli/erlang.mk @@ -0,0 +1,7296 @@ +# Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +.PHONY: all app apps deps search rel relup docs install-docs check tests clean distclean help erlang-mk + +ERLANG_MK_FILENAME := $(realpath $(lastword $(MAKEFILE_LIST))) +export ERLANG_MK_FILENAME + +ERLANG_MK_VERSION = 2019.07.01-40-geb3e4b0 +ERLANG_MK_WITHOUT = plugins/cover plugins/ct plugins/dialyzer plugins/eunit plugins/proper plugins/triq + +# Make 3.81 and 3.82 are deprecated. + +ifeq ($(MAKELEVEL)$(MAKE_VERSION),03.81) +$(warning Please upgrade to GNU Make 4 or later: https://erlang.mk/guide/installation.html) +endif + +ifeq ($(MAKELEVEL)$(MAKE_VERSION),03.82) +$(warning Please upgrade to GNU Make 4 or later: https://erlang.mk/guide/installation.html) +endif + +# Core configuration. + +PROJECT ?= $(notdir $(CURDIR)) +PROJECT := $(strip $(PROJECT)) + +PROJECT_VERSION ?= rolling +PROJECT_MOD ?= $(PROJECT)_app +PROJECT_ENV ?= [] + +# Verbosity. + +V ?= 0 + +verbose_0 = @ +verbose_2 = set -x; +verbose = $(verbose_$(V)) + +ifeq ($(V),3) +SHELL := $(SHELL) -x +endif + +gen_verbose_0 = @echo " GEN " $@; +gen_verbose_2 = set -x; +gen_verbose = $(gen_verbose_$(V)) + +gen_verbose_esc_0 = @echo " GEN " $$@; +gen_verbose_esc_2 = set -x; +gen_verbose_esc = $(gen_verbose_esc_$(V)) + +# Temporary files directory. + +ERLANG_MK_TMP ?= $(CURDIR)/.erlang.mk +export ERLANG_MK_TMP + +# "erl" command. + +ERL = erl +A1 -noinput -boot no_dot_erlang + +# Platform detection. + +ifeq ($(PLATFORM),) +UNAME_S := $(shell uname -s) + +ifeq ($(UNAME_S),Linux) +PLATFORM = linux +else ifeq ($(UNAME_S),Darwin) +PLATFORM = darwin +else ifeq ($(UNAME_S),SunOS) +PLATFORM = solaris +else ifeq ($(UNAME_S),GNU) +PLATFORM = gnu +else ifeq ($(UNAME_S),FreeBSD) +PLATFORM = freebsd +else ifeq ($(UNAME_S),NetBSD) +PLATFORM = netbsd +else ifeq ($(UNAME_S),OpenBSD) +PLATFORM = openbsd +else ifeq ($(UNAME_S),DragonFly) +PLATFORM = dragonfly +else ifeq ($(shell uname -o),Msys) +PLATFORM = msys2 +else +$(error Unable to detect platform. Please open a ticket with the output of uname -a.) +endif + +export PLATFORM +endif + +# Core targets. + +all:: deps app rel + +# Noop to avoid a Make warning when there's nothing to do. +rel:: + $(verbose) : + +relup:: deps app + +check:: tests + +clean:: clean-crashdump + +clean-crashdump: +ifneq ($(wildcard erl_crash.dump),) + $(gen_verbose) rm -f erl_crash.dump +endif + +distclean:: clean distclean-tmp + +$(ERLANG_MK_TMP): + $(verbose) mkdir -p $(ERLANG_MK_TMP) + +distclean-tmp: + $(gen_verbose) rm -rf $(ERLANG_MK_TMP) + +help:: + $(verbose) printf "%s\n" \ + "erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \ + "Copyright (c) 2013-2016 Loïc Hoguin <essen@ninenines.eu>" \ + "" \ + "Usage: [V=1] $(MAKE) [target]..." \ + "" \ + "Core targets:" \ + " all Run deps, app and rel targets in that order" \ + " app Compile the project" \ + " deps Fetch dependencies (if needed) and compile them" \ + " fetch-deps Fetch dependencies recursively (if needed) without compiling them" \ + " list-deps List dependencies recursively on stdout" \ + " search q=... Search for a package in the built-in index" \ + " rel Build a release for this project, if applicable" \ + " docs Build the documentation for this project" \ + " install-docs Install the man pages for this project" \ + " check Compile and run all tests and analysis for this project" \ + " tests Run the tests for this project" \ + " clean Delete temporary and output files from most targets" \ + " distclean Delete all temporary and output files" \ + " help Display this help and exit" \ + " erlang-mk Update erlang.mk to the latest version" + +# Core functions. + +empty := +space := $(empty) $(empty) +tab := $(empty) $(empty) +comma := , + +define newline + + +endef + +define comma_list +$(subst $(space),$(comma),$(strip $(1))) +endef + +define escape_dquotes +$(subst ",\",$1) +endef + +# Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy. +define erlang +$(ERL) $2 -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(call escape_dquotes,$1))" -- erlang.mk +endef + +ifeq ($(PLATFORM),msys2) +core_native_path = $(shell cygpath -m $1) +else +core_native_path = $1 +endif + +core_http_get = curl -Lf$(if $(filter-out 0,$(V)),,s)o $(call core_native_path,$1) $2 + +core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1))) + +# We skip files that contain spaces because they end up causing issues. +core_find = $(if $(wildcard $1),$(shell find $(1:%/=%) \( -type l -o -type f \) -name $(subst *,\*,$2) | grep -v " ")) + +core_lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$(1))))))))))))))))))))))))))) + +core_ls = $(filter-out $(1),$(shell echo $(1))) + +# @todo Use a solution that does not require using perl. +core_relpath = $(shell perl -e 'use File::Spec; print File::Spec->abs2rel(@ARGV) . "\n"' $1 $2) + +define core_render + printf -- '$(subst $(newline),\n,$(subst %,%%,$(subst ','\'',$(subst $(tab),$(WS),$(call $(1))))))\n' > $(2) +endef + +# Automated update. + +ERLANG_MK_REPO ?= https://github.com/ninenines/erlang.mk +ERLANG_MK_COMMIT ?= +ERLANG_MK_BUILD_CONFIG ?= build.config +ERLANG_MK_BUILD_DIR ?= .erlang.mk.build + +erlang-mk: WITHOUT ?= $(ERLANG_MK_WITHOUT) +erlang-mk: +ifdef ERLANG_MK_COMMIT + $(verbose) git clone $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR) + $(verbose) cd $(ERLANG_MK_BUILD_DIR) && git checkout $(ERLANG_MK_COMMIT) +else + $(verbose) git clone --depth 1 $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR) +endif + $(verbose) if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR)/build.config; fi + $(gen_verbose) $(MAKE) --no-print-directory -C $(ERLANG_MK_BUILD_DIR) WITHOUT='$(strip $(WITHOUT))' UPGRADE=1 + $(verbose) cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk + $(verbose) rm -rf $(ERLANG_MK_BUILD_DIR) + $(verbose) rm -rf $(ERLANG_MK_TMP) + +# The erlang.mk package index is bundled in the default erlang.mk build. +# Search for the string "copyright" to skip to the rest of the code. + +# Copyright (c) 2015-2017, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: distclean-kerl + +KERL_INSTALL_DIR ?= $(HOME)/erlang + +ifeq ($(strip $(KERL)),) +KERL := $(ERLANG_MK_TMP)/kerl/kerl +endif + +KERL_DIR = $(ERLANG_MK_TMP)/kerl + +export KERL + +KERL_GIT ?= https://github.com/kerl/kerl +KERL_COMMIT ?= master + +KERL_MAKEFLAGS ?= + +OTP_GIT ?= https://github.com/erlang/otp + +define kerl_otp_target +$(KERL_INSTALL_DIR)/$(1): $(KERL) + $(verbose) if [ ! -d $$@ ]; then \ + MAKEFLAGS="$(KERL_MAKEFLAGS)" $(KERL) build git $(OTP_GIT) $(1) $(1); \ + $(KERL) install $(1) $(KERL_INSTALL_DIR)/$(1); \ + fi +endef + +define kerl_hipe_target +$(KERL_INSTALL_DIR)/$1-native: $(KERL) + $(verbose) if [ ! -d $$@ ]; then \ + KERL_CONFIGURE_OPTIONS=--enable-native-libs \ + MAKEFLAGS="$(KERL_MAKEFLAGS)" $(KERL) build git $(OTP_GIT) $1 $1-native; \ + $(KERL) install $1-native $(KERL_INSTALL_DIR)/$1-native; \ + fi +endef + +$(KERL): $(KERL_DIR) + +$(KERL_DIR): | $(ERLANG_MK_TMP) + $(gen_verbose) git clone --depth 1 $(KERL_GIT) $(ERLANG_MK_TMP)/kerl + $(verbose) cd $(ERLANG_MK_TMP)/kerl && git checkout $(KERL_COMMIT) + $(verbose) chmod +x $(KERL) + +distclean:: distclean-kerl + +distclean-kerl: + $(gen_verbose) rm -rf $(KERL_DIR) + +# Allow users to select which version of Erlang/OTP to use for a project. + +ifneq ($(strip $(LATEST_ERLANG_OTP)),) +# In some environments it is necessary to filter out master. +ERLANG_OTP := $(notdir $(lastword $(sort\ + $(filter-out $(KERL_INSTALL_DIR)/master $(KERL_INSTALL_DIR)/OTP_R%,\ + $(filter-out %-rc1 %-rc2 %-rc3,$(wildcard $(KERL_INSTALL_DIR)/*[^-native])))))) +endif + +ERLANG_OTP ?= +ERLANG_HIPE ?= + +# Use kerl to enforce a specific Erlang/OTP version for a project. +ifneq ($(strip $(ERLANG_OTP)),) +export PATH := $(KERL_INSTALL_DIR)/$(ERLANG_OTP)/bin:$(PATH) +SHELL := env PATH=$(PATH) $(SHELL) +$(eval $(call kerl_otp_target,$(ERLANG_OTP))) + +# Build Erlang/OTP only if it doesn't already exist. +ifeq ($(wildcard $(KERL_INSTALL_DIR)/$(ERLANG_OTP))$(BUILD_ERLANG_OTP),) +$(info Building Erlang/OTP $(ERLANG_OTP)... Please wait...) +$(shell $(MAKE) $(KERL_INSTALL_DIR)/$(ERLANG_OTP) ERLANG_OTP=$(ERLANG_OTP) BUILD_ERLANG_OTP=1 >&2) +endif + +else +# Same for a HiPE enabled VM. +ifneq ($(strip $(ERLANG_HIPE)),) +export PATH := $(KERL_INSTALL_DIR)/$(ERLANG_HIPE)-native/bin:$(PATH) +SHELL := env PATH=$(PATH) $(SHELL) +$(eval $(call kerl_hipe_target,$(ERLANG_HIPE))) + +# Build Erlang/OTP only if it doesn't already exist. +ifeq ($(wildcard $(KERL_INSTALL_DIR)/$(ERLANG_HIPE)-native)$(BUILD_ERLANG_OTP),) +$(info Building HiPE-enabled Erlang/OTP $(ERLANG_OTP)... Please wait...) +$(shell $(MAKE) $(KERL_INSTALL_DIR)/$(ERLANG_HIPE)-native ERLANG_HIPE=$(ERLANG_HIPE) BUILD_ERLANG_OTP=1 >&2) +endif + +endif +endif + +PACKAGES += aberth +pkg_aberth_name = aberth +pkg_aberth_description = Generic BERT-RPC server in Erlang +pkg_aberth_homepage = https://github.com/a13x/aberth +pkg_aberth_fetch = git +pkg_aberth_repo = https://github.com/a13x/aberth +pkg_aberth_commit = master + +PACKAGES += active +pkg_active_name = active +pkg_active_description = Active development for Erlang: rebuild and reload source/binary files while the VM is running +pkg_active_homepage = https://github.com/proger/active +pkg_active_fetch = git +pkg_active_repo = https://github.com/proger/active +pkg_active_commit = master + +PACKAGES += actordb_core +pkg_actordb_core_name = actordb_core +pkg_actordb_core_description = ActorDB main source +pkg_actordb_core_homepage = http://www.actordb.com/ +pkg_actordb_core_fetch = git +pkg_actordb_core_repo = https://github.com/biokoda/actordb_core +pkg_actordb_core_commit = master + +PACKAGES += actordb_thrift +pkg_actordb_thrift_name = actordb_thrift +pkg_actordb_thrift_description = Thrift API for ActorDB +pkg_actordb_thrift_homepage = http://www.actordb.com/ +pkg_actordb_thrift_fetch = git +pkg_actordb_thrift_repo = https://github.com/biokoda/actordb_thrift +pkg_actordb_thrift_commit = master + +PACKAGES += aleppo +pkg_aleppo_name = aleppo +pkg_aleppo_description = Alternative Erlang Pre-Processor +pkg_aleppo_homepage = https://github.com/ErlyORM/aleppo +pkg_aleppo_fetch = git +pkg_aleppo_repo = https://github.com/ErlyORM/aleppo +pkg_aleppo_commit = master + +PACKAGES += alog +pkg_alog_name = alog +pkg_alog_description = Simply the best logging framework for Erlang +pkg_alog_homepage = https://github.com/siberian-fast-food/alogger +pkg_alog_fetch = git +pkg_alog_repo = https://github.com/siberian-fast-food/alogger +pkg_alog_commit = master + +PACKAGES += amqp_client +pkg_amqp_client_name = amqp_client +pkg_amqp_client_description = RabbitMQ Erlang AMQP client +pkg_amqp_client_homepage = https://www.rabbitmq.com/erlang-client-user-guide.html +pkg_amqp_client_fetch = git +pkg_amqp_client_repo = https://github.com/rabbitmq/rabbitmq-erlang-client.git +pkg_amqp_client_commit = master + +PACKAGES += annotations +pkg_annotations_name = annotations +pkg_annotations_description = Simple code instrumentation utilities +pkg_annotations_homepage = https://github.com/hyperthunk/annotations +pkg_annotations_fetch = git +pkg_annotations_repo = https://github.com/hyperthunk/annotations +pkg_annotations_commit = master + +PACKAGES += antidote +pkg_antidote_name = antidote +pkg_antidote_description = Large-scale computation without synchronisation +pkg_antidote_homepage = https://syncfree.lip6.fr/ +pkg_antidote_fetch = git +pkg_antidote_repo = https://github.com/SyncFree/antidote +pkg_antidote_commit = master + +PACKAGES += apns +pkg_apns_name = apns +pkg_apns_description = Apple Push Notification Server for Erlang +pkg_apns_homepage = http://inaka.github.com/apns4erl +pkg_apns_fetch = git +pkg_apns_repo = https://github.com/inaka/apns4erl +pkg_apns_commit = master + +PACKAGES += asciideck +pkg_asciideck_name = asciideck +pkg_asciideck_description = Asciidoc for Erlang. +pkg_asciideck_homepage = https://ninenines.eu +pkg_asciideck_fetch = git +pkg_asciideck_repo = https://github.com/ninenines/asciideck +pkg_asciideck_commit = master + +PACKAGES += azdht +pkg_azdht_name = azdht +pkg_azdht_description = Azureus Distributed Hash Table (DHT) in Erlang +pkg_azdht_homepage = https://github.com/arcusfelis/azdht +pkg_azdht_fetch = git +pkg_azdht_repo = https://github.com/arcusfelis/azdht +pkg_azdht_commit = master + +PACKAGES += backoff +pkg_backoff_name = backoff +pkg_backoff_description = Simple exponential backoffs in Erlang +pkg_backoff_homepage = https://github.com/ferd/backoff +pkg_backoff_fetch = git +pkg_backoff_repo = https://github.com/ferd/backoff +pkg_backoff_commit = master + +PACKAGES += barrel_tcp +pkg_barrel_tcp_name = barrel_tcp +pkg_barrel_tcp_description = barrel is a generic TCP acceptor pool with low latency in Erlang. +pkg_barrel_tcp_homepage = https://github.com/benoitc-attic/barrel_tcp +pkg_barrel_tcp_fetch = git +pkg_barrel_tcp_repo = https://github.com/benoitc-attic/barrel_tcp +pkg_barrel_tcp_commit = master + +PACKAGES += basho_bench +pkg_basho_bench_name = basho_bench +pkg_basho_bench_description = A load-generation and testing tool for basically whatever you can write a returning Erlang function for. +pkg_basho_bench_homepage = https://github.com/basho/basho_bench +pkg_basho_bench_fetch = git +pkg_basho_bench_repo = https://github.com/basho/basho_bench +pkg_basho_bench_commit = master + +PACKAGES += bcrypt +pkg_bcrypt_name = bcrypt +pkg_bcrypt_description = Bcrypt Erlang / C library +pkg_bcrypt_homepage = https://github.com/erlangpack/bcrypt +pkg_bcrypt_fetch = git +pkg_bcrypt_repo = https://github.com/erlangpack/bcrypt.git +pkg_bcrypt_commit = master + +PACKAGES += beam +pkg_beam_name = beam +pkg_beam_description = BEAM emulator written in Erlang +pkg_beam_homepage = https://github.com/tonyrog/beam +pkg_beam_fetch = git +pkg_beam_repo = https://github.com/tonyrog/beam +pkg_beam_commit = master + +PACKAGES += beanstalk +pkg_beanstalk_name = beanstalk +pkg_beanstalk_description = An Erlang client for beanstalkd +pkg_beanstalk_homepage = https://github.com/tim/erlang-beanstalk +pkg_beanstalk_fetch = git +pkg_beanstalk_repo = https://github.com/tim/erlang-beanstalk +pkg_beanstalk_commit = master + +PACKAGES += bear +pkg_bear_name = bear +pkg_bear_description = a set of statistics functions for erlang +pkg_bear_homepage = https://github.com/boundary/bear +pkg_bear_fetch = git +pkg_bear_repo = https://github.com/boundary/bear +pkg_bear_commit = master + +PACKAGES += bertconf +pkg_bertconf_name = bertconf +pkg_bertconf_description = Make ETS tables out of statc BERT files that are auto-reloaded +pkg_bertconf_homepage = https://github.com/ferd/bertconf +pkg_bertconf_fetch = git +pkg_bertconf_repo = https://github.com/ferd/bertconf +pkg_bertconf_commit = master + +PACKAGES += bifrost +pkg_bifrost_name = bifrost +pkg_bifrost_description = Erlang FTP Server Framework +pkg_bifrost_homepage = https://github.com/thorstadt/bifrost +pkg_bifrost_fetch = git +pkg_bifrost_repo = https://github.com/thorstadt/bifrost +pkg_bifrost_commit = master + +PACKAGES += binpp +pkg_binpp_name = binpp +pkg_binpp_description = Erlang Binary Pretty Printer +pkg_binpp_homepage = https://github.com/jtendo/binpp +pkg_binpp_fetch = git +pkg_binpp_repo = https://github.com/jtendo/binpp +pkg_binpp_commit = master + +PACKAGES += bisect +pkg_bisect_name = bisect +pkg_bisect_description = Ordered fixed-size binary dictionary in Erlang +pkg_bisect_homepage = https://github.com/knutin/bisect +pkg_bisect_fetch = git +pkg_bisect_repo = https://github.com/knutin/bisect +pkg_bisect_commit = master + +PACKAGES += bitcask +pkg_bitcask_name = bitcask +pkg_bitcask_description = because you need another a key/value storage engine +pkg_bitcask_homepage = https://github.com/basho/bitcask +pkg_bitcask_fetch = git +pkg_bitcask_repo = https://github.com/basho/bitcask +pkg_bitcask_commit = develop + +PACKAGES += bitstore +pkg_bitstore_name = bitstore +pkg_bitstore_description = A document based ontology development environment +pkg_bitstore_homepage = https://github.com/bdionne/bitstore +pkg_bitstore_fetch = git +pkg_bitstore_repo = https://github.com/bdionne/bitstore +pkg_bitstore_commit = master + +PACKAGES += bootstrap +pkg_bootstrap_name = bootstrap +pkg_bootstrap_description = A simple, yet powerful Erlang cluster bootstrapping application. +pkg_bootstrap_homepage = https://github.com/schlagert/bootstrap +pkg_bootstrap_fetch = git +pkg_bootstrap_repo = https://github.com/schlagert/bootstrap +pkg_bootstrap_commit = master + +PACKAGES += boss +pkg_boss_name = boss +pkg_boss_description = Erlang web MVC, now featuring Comet +pkg_boss_homepage = https://github.com/ChicagoBoss/ChicagoBoss +pkg_boss_fetch = git +pkg_boss_repo = https://github.com/ChicagoBoss/ChicagoBoss +pkg_boss_commit = master + +PACKAGES += boss_db +pkg_boss_db_name = boss_db +pkg_boss_db_description = BossDB: a sharded, caching, pooling, evented ORM for Erlang +pkg_boss_db_homepage = https://github.com/ErlyORM/boss_db +pkg_boss_db_fetch = git +pkg_boss_db_repo = https://github.com/ErlyORM/boss_db +pkg_boss_db_commit = master + +PACKAGES += brod +pkg_brod_name = brod +pkg_brod_description = Kafka client in Erlang +pkg_brod_homepage = https://github.com/klarna/brod +pkg_brod_fetch = git +pkg_brod_repo = https://github.com/klarna/brod.git +pkg_brod_commit = master + +PACKAGES += bson +pkg_bson_name = bson +pkg_bson_description = BSON documents in Erlang, see bsonspec.org +pkg_bson_homepage = https://github.com/comtihon/bson-erlang +pkg_bson_fetch = git +pkg_bson_repo = https://github.com/comtihon/bson-erlang +pkg_bson_commit = master + +PACKAGES += bullet +pkg_bullet_name = bullet +pkg_bullet_description = Simple, reliable, efficient streaming for Cowboy. +pkg_bullet_homepage = http://ninenines.eu +pkg_bullet_fetch = git +pkg_bullet_repo = https://github.com/ninenines/bullet +pkg_bullet_commit = master + +PACKAGES += cache +pkg_cache_name = cache +pkg_cache_description = Erlang in-memory cache +pkg_cache_homepage = https://github.com/fogfish/cache +pkg_cache_fetch = git +pkg_cache_repo = https://github.com/fogfish/cache +pkg_cache_commit = master + +PACKAGES += cake +pkg_cake_name = cake +pkg_cake_description = Really simple terminal colorization +pkg_cake_homepage = https://github.com/darach/cake-erl +pkg_cake_fetch = git +pkg_cake_repo = https://github.com/darach/cake-erl +pkg_cake_commit = master + +PACKAGES += carotene +pkg_carotene_name = carotene +pkg_carotene_description = Real-time server +pkg_carotene_homepage = https://github.com/carotene/carotene +pkg_carotene_fetch = git +pkg_carotene_repo = https://github.com/carotene/carotene +pkg_carotene_commit = master + +PACKAGES += cberl +pkg_cberl_name = cberl +pkg_cberl_description = NIF based Erlang bindings for Couchbase +pkg_cberl_homepage = https://github.com/chitika/cberl +pkg_cberl_fetch = git +pkg_cberl_repo = https://github.com/chitika/cberl +pkg_cberl_commit = master + +PACKAGES += cecho +pkg_cecho_name = cecho +pkg_cecho_description = An ncurses library for Erlang +pkg_cecho_homepage = https://github.com/mazenharake/cecho +pkg_cecho_fetch = git +pkg_cecho_repo = https://github.com/mazenharake/cecho +pkg_cecho_commit = master + +PACKAGES += cferl +pkg_cferl_name = cferl +pkg_cferl_description = Rackspace / Open Stack Cloud Files Erlang Client +pkg_cferl_homepage = https://github.com/ddossot/cferl +pkg_cferl_fetch = git +pkg_cferl_repo = https://github.com/ddossot/cferl +pkg_cferl_commit = master + +PACKAGES += chaos_monkey +pkg_chaos_monkey_name = chaos_monkey +pkg_chaos_monkey_description = This is The CHAOS MONKEY. It will kill your processes. +pkg_chaos_monkey_homepage = https://github.com/dLuna/chaos_monkey +pkg_chaos_monkey_fetch = git +pkg_chaos_monkey_repo = https://github.com/dLuna/chaos_monkey +pkg_chaos_monkey_commit = master + +PACKAGES += check_node +pkg_check_node_name = check_node +pkg_check_node_description = Nagios Scripts for monitoring Riak +pkg_check_node_homepage = https://github.com/basho-labs/riak_nagios +pkg_check_node_fetch = git +pkg_check_node_repo = https://github.com/basho-labs/riak_nagios +pkg_check_node_commit = master + +PACKAGES += chronos +pkg_chronos_name = chronos +pkg_chronos_description = Timer module for Erlang that makes it easy to abstact time out of the tests. +pkg_chronos_homepage = https://github.com/lehoff/chronos +pkg_chronos_fetch = git +pkg_chronos_repo = https://github.com/lehoff/chronos +pkg_chronos_commit = master + +PACKAGES += chumak +pkg_chumak_name = chumak +pkg_chumak_description = Pure Erlang implementation of ZeroMQ Message Transport Protocol. +pkg_chumak_homepage = http://choven.ca +pkg_chumak_fetch = git +pkg_chumak_repo = https://github.com/chovencorp/chumak +pkg_chumak_commit = master + +PACKAGES += cl +pkg_cl_name = cl +pkg_cl_description = OpenCL binding for Erlang +pkg_cl_homepage = https://github.com/tonyrog/cl +pkg_cl_fetch = git +pkg_cl_repo = https://github.com/tonyrog/cl +pkg_cl_commit = master + +PACKAGES += clique +pkg_clique_name = clique +pkg_clique_description = CLI Framework for Erlang +pkg_clique_homepage = https://github.com/basho/clique +pkg_clique_fetch = git +pkg_clique_repo = https://github.com/basho/clique +pkg_clique_commit = develop + +PACKAGES += cloudi_core +pkg_cloudi_core_name = cloudi_core +pkg_cloudi_core_description = CloudI internal service runtime +pkg_cloudi_core_homepage = http://cloudi.org/ +pkg_cloudi_core_fetch = git +pkg_cloudi_core_repo = https://github.com/CloudI/cloudi_core +pkg_cloudi_core_commit = master + +PACKAGES += cloudi_service_api_requests +pkg_cloudi_service_api_requests_name = cloudi_service_api_requests +pkg_cloudi_service_api_requests_description = CloudI Service API requests (JSON-RPC/Erlang-term support) +pkg_cloudi_service_api_requests_homepage = http://cloudi.org/ +pkg_cloudi_service_api_requests_fetch = git +pkg_cloudi_service_api_requests_repo = https://github.com/CloudI/cloudi_service_api_requests +pkg_cloudi_service_api_requests_commit = master + +PACKAGES += cloudi_service_db +pkg_cloudi_service_db_name = cloudi_service_db +pkg_cloudi_service_db_description = CloudI Database (in-memory/testing/generic) +pkg_cloudi_service_db_homepage = http://cloudi.org/ +pkg_cloudi_service_db_fetch = git +pkg_cloudi_service_db_repo = https://github.com/CloudI/cloudi_service_db +pkg_cloudi_service_db_commit = master + +PACKAGES += cloudi_service_db_cassandra +pkg_cloudi_service_db_cassandra_name = cloudi_service_db_cassandra +pkg_cloudi_service_db_cassandra_description = Cassandra CloudI Service +pkg_cloudi_service_db_cassandra_homepage = http://cloudi.org/ +pkg_cloudi_service_db_cassandra_fetch = git +pkg_cloudi_service_db_cassandra_repo = https://github.com/CloudI/cloudi_service_db_cassandra +pkg_cloudi_service_db_cassandra_commit = master + +PACKAGES += cloudi_service_db_cassandra_cql +pkg_cloudi_service_db_cassandra_cql_name = cloudi_service_db_cassandra_cql +pkg_cloudi_service_db_cassandra_cql_description = Cassandra CQL CloudI Service +pkg_cloudi_service_db_cassandra_cql_homepage = http://cloudi.org/ +pkg_cloudi_service_db_cassandra_cql_fetch = git +pkg_cloudi_service_db_cassandra_cql_repo = https://github.com/CloudI/cloudi_service_db_cassandra_cql +pkg_cloudi_service_db_cassandra_cql_commit = master + +PACKAGES += cloudi_service_db_couchdb +pkg_cloudi_service_db_couchdb_name = cloudi_service_db_couchdb +pkg_cloudi_service_db_couchdb_description = CouchDB CloudI Service +pkg_cloudi_service_db_couchdb_homepage = http://cloudi.org/ +pkg_cloudi_service_db_couchdb_fetch = git +pkg_cloudi_service_db_couchdb_repo = https://github.com/CloudI/cloudi_service_db_couchdb +pkg_cloudi_service_db_couchdb_commit = master + +PACKAGES += cloudi_service_db_elasticsearch +pkg_cloudi_service_db_elasticsearch_name = cloudi_service_db_elasticsearch +pkg_cloudi_service_db_elasticsearch_description = elasticsearch CloudI Service +pkg_cloudi_service_db_elasticsearch_homepage = http://cloudi.org/ +pkg_cloudi_service_db_elasticsearch_fetch = git +pkg_cloudi_service_db_elasticsearch_repo = https://github.com/CloudI/cloudi_service_db_elasticsearch +pkg_cloudi_service_db_elasticsearch_commit = master + +PACKAGES += cloudi_service_db_memcached +pkg_cloudi_service_db_memcached_name = cloudi_service_db_memcached +pkg_cloudi_service_db_memcached_description = memcached CloudI Service +pkg_cloudi_service_db_memcached_homepage = http://cloudi.org/ +pkg_cloudi_service_db_memcached_fetch = git +pkg_cloudi_service_db_memcached_repo = https://github.com/CloudI/cloudi_service_db_memcached +pkg_cloudi_service_db_memcached_commit = master + +PACKAGES += cloudi_service_db_mysql +pkg_cloudi_service_db_mysql_name = cloudi_service_db_mysql +pkg_cloudi_service_db_mysql_description = MySQL CloudI Service +pkg_cloudi_service_db_mysql_homepage = http://cloudi.org/ +pkg_cloudi_service_db_mysql_fetch = git +pkg_cloudi_service_db_mysql_repo = https://github.com/CloudI/cloudi_service_db_mysql +pkg_cloudi_service_db_mysql_commit = master + +PACKAGES += cloudi_service_db_pgsql +pkg_cloudi_service_db_pgsql_name = cloudi_service_db_pgsql +pkg_cloudi_service_db_pgsql_description = PostgreSQL CloudI Service +pkg_cloudi_service_db_pgsql_homepage = http://cloudi.org/ +pkg_cloudi_service_db_pgsql_fetch = git +pkg_cloudi_service_db_pgsql_repo = https://github.com/CloudI/cloudi_service_db_pgsql +pkg_cloudi_service_db_pgsql_commit = master + +PACKAGES += cloudi_service_db_riak +pkg_cloudi_service_db_riak_name = cloudi_service_db_riak +pkg_cloudi_service_db_riak_description = Riak CloudI Service +pkg_cloudi_service_db_riak_homepage = http://cloudi.org/ +pkg_cloudi_service_db_riak_fetch = git +pkg_cloudi_service_db_riak_repo = https://github.com/CloudI/cloudi_service_db_riak +pkg_cloudi_service_db_riak_commit = master + +PACKAGES += cloudi_service_db_tokyotyrant +pkg_cloudi_service_db_tokyotyrant_name = cloudi_service_db_tokyotyrant +pkg_cloudi_service_db_tokyotyrant_description = Tokyo Tyrant CloudI Service +pkg_cloudi_service_db_tokyotyrant_homepage = http://cloudi.org/ +pkg_cloudi_service_db_tokyotyrant_fetch = git +pkg_cloudi_service_db_tokyotyrant_repo = https://github.com/CloudI/cloudi_service_db_tokyotyrant +pkg_cloudi_service_db_tokyotyrant_commit = master + +PACKAGES += cloudi_service_filesystem +pkg_cloudi_service_filesystem_name = cloudi_service_filesystem +pkg_cloudi_service_filesystem_description = Filesystem CloudI Service +pkg_cloudi_service_filesystem_homepage = http://cloudi.org/ +pkg_cloudi_service_filesystem_fetch = git +pkg_cloudi_service_filesystem_repo = https://github.com/CloudI/cloudi_service_filesystem +pkg_cloudi_service_filesystem_commit = master + +PACKAGES += cloudi_service_http_client +pkg_cloudi_service_http_client_name = cloudi_service_http_client +pkg_cloudi_service_http_client_description = HTTP client CloudI Service +pkg_cloudi_service_http_client_homepage = http://cloudi.org/ +pkg_cloudi_service_http_client_fetch = git +pkg_cloudi_service_http_client_repo = https://github.com/CloudI/cloudi_service_http_client +pkg_cloudi_service_http_client_commit = master + +PACKAGES += cloudi_service_http_cowboy +pkg_cloudi_service_http_cowboy_name = cloudi_service_http_cowboy +pkg_cloudi_service_http_cowboy_description = cowboy HTTP/HTTPS CloudI Service +pkg_cloudi_service_http_cowboy_homepage = http://cloudi.org/ +pkg_cloudi_service_http_cowboy_fetch = git +pkg_cloudi_service_http_cowboy_repo = https://github.com/CloudI/cloudi_service_http_cowboy +pkg_cloudi_service_http_cowboy_commit = master + +PACKAGES += cloudi_service_http_elli +pkg_cloudi_service_http_elli_name = cloudi_service_http_elli +pkg_cloudi_service_http_elli_description = elli HTTP CloudI Service +pkg_cloudi_service_http_elli_homepage = http://cloudi.org/ +pkg_cloudi_service_http_elli_fetch = git +pkg_cloudi_service_http_elli_repo = https://github.com/CloudI/cloudi_service_http_elli +pkg_cloudi_service_http_elli_commit = master + +PACKAGES += cloudi_service_map_reduce +pkg_cloudi_service_map_reduce_name = cloudi_service_map_reduce +pkg_cloudi_service_map_reduce_description = Map/Reduce CloudI Service +pkg_cloudi_service_map_reduce_homepage = http://cloudi.org/ +pkg_cloudi_service_map_reduce_fetch = git +pkg_cloudi_service_map_reduce_repo = https://github.com/CloudI/cloudi_service_map_reduce +pkg_cloudi_service_map_reduce_commit = master + +PACKAGES += cloudi_service_oauth1 +pkg_cloudi_service_oauth1_name = cloudi_service_oauth1 +pkg_cloudi_service_oauth1_description = OAuth v1.0 CloudI Service +pkg_cloudi_service_oauth1_homepage = http://cloudi.org/ +pkg_cloudi_service_oauth1_fetch = git +pkg_cloudi_service_oauth1_repo = https://github.com/CloudI/cloudi_service_oauth1 +pkg_cloudi_service_oauth1_commit = master + +PACKAGES += cloudi_service_queue +pkg_cloudi_service_queue_name = cloudi_service_queue +pkg_cloudi_service_queue_description = Persistent Queue Service +pkg_cloudi_service_queue_homepage = http://cloudi.org/ +pkg_cloudi_service_queue_fetch = git +pkg_cloudi_service_queue_repo = https://github.com/CloudI/cloudi_service_queue +pkg_cloudi_service_queue_commit = master + +PACKAGES += cloudi_service_quorum +pkg_cloudi_service_quorum_name = cloudi_service_quorum +pkg_cloudi_service_quorum_description = CloudI Quorum Service +pkg_cloudi_service_quorum_homepage = http://cloudi.org/ +pkg_cloudi_service_quorum_fetch = git +pkg_cloudi_service_quorum_repo = https://github.com/CloudI/cloudi_service_quorum +pkg_cloudi_service_quorum_commit = master + +PACKAGES += cloudi_service_router +pkg_cloudi_service_router_name = cloudi_service_router +pkg_cloudi_service_router_description = CloudI Router Service +pkg_cloudi_service_router_homepage = http://cloudi.org/ +pkg_cloudi_service_router_fetch = git +pkg_cloudi_service_router_repo = https://github.com/CloudI/cloudi_service_router +pkg_cloudi_service_router_commit = master + +PACKAGES += cloudi_service_tcp +pkg_cloudi_service_tcp_name = cloudi_service_tcp +pkg_cloudi_service_tcp_description = TCP CloudI Service +pkg_cloudi_service_tcp_homepage = http://cloudi.org/ +pkg_cloudi_service_tcp_fetch = git +pkg_cloudi_service_tcp_repo = https://github.com/CloudI/cloudi_service_tcp +pkg_cloudi_service_tcp_commit = master + +PACKAGES += cloudi_service_timers +pkg_cloudi_service_timers_name = cloudi_service_timers +pkg_cloudi_service_timers_description = Timers CloudI Service +pkg_cloudi_service_timers_homepage = http://cloudi.org/ +pkg_cloudi_service_timers_fetch = git +pkg_cloudi_service_timers_repo = https://github.com/CloudI/cloudi_service_timers +pkg_cloudi_service_timers_commit = master + +PACKAGES += cloudi_service_udp +pkg_cloudi_service_udp_name = cloudi_service_udp +pkg_cloudi_service_udp_description = UDP CloudI Service +pkg_cloudi_service_udp_homepage = http://cloudi.org/ +pkg_cloudi_service_udp_fetch = git +pkg_cloudi_service_udp_repo = https://github.com/CloudI/cloudi_service_udp +pkg_cloudi_service_udp_commit = master + +PACKAGES += cloudi_service_validate +pkg_cloudi_service_validate_name = cloudi_service_validate +pkg_cloudi_service_validate_description = CloudI Validate Service +pkg_cloudi_service_validate_homepage = http://cloudi.org/ +pkg_cloudi_service_validate_fetch = git +pkg_cloudi_service_validate_repo = https://github.com/CloudI/cloudi_service_validate +pkg_cloudi_service_validate_commit = master + +PACKAGES += cloudi_service_zeromq +pkg_cloudi_service_zeromq_name = cloudi_service_zeromq +pkg_cloudi_service_zeromq_description = ZeroMQ CloudI Service +pkg_cloudi_service_zeromq_homepage = http://cloudi.org/ +pkg_cloudi_service_zeromq_fetch = git +pkg_cloudi_service_zeromq_repo = https://github.com/CloudI/cloudi_service_zeromq +pkg_cloudi_service_zeromq_commit = master + +PACKAGES += cluster_info +pkg_cluster_info_name = cluster_info +pkg_cluster_info_description = Fork of Hibari's nifty cluster_info OTP app +pkg_cluster_info_homepage = https://github.com/basho/cluster_info +pkg_cluster_info_fetch = git +pkg_cluster_info_repo = https://github.com/basho/cluster_info +pkg_cluster_info_commit = master + +PACKAGES += color +pkg_color_name = color +pkg_color_description = ANSI colors for your Erlang +pkg_color_homepage = https://github.com/julianduque/erlang-color +pkg_color_fetch = git +pkg_color_repo = https://github.com/julianduque/erlang-color +pkg_color_commit = master + +PACKAGES += confetti +pkg_confetti_name = confetti +pkg_confetti_description = Erlang configuration provider / application:get_env/2 on steroids +pkg_confetti_homepage = https://github.com/jtendo/confetti +pkg_confetti_fetch = git +pkg_confetti_repo = https://github.com/jtendo/confetti +pkg_confetti_commit = master + +PACKAGES += couchbeam +pkg_couchbeam_name = couchbeam +pkg_couchbeam_description = Apache CouchDB client in Erlang +pkg_couchbeam_homepage = https://github.com/benoitc/couchbeam +pkg_couchbeam_fetch = git +pkg_couchbeam_repo = https://github.com/benoitc/couchbeam +pkg_couchbeam_commit = master + +PACKAGES += covertool +pkg_covertool_name = covertool +pkg_covertool_description = Tool to convert Erlang cover data files into Cobertura XML reports +pkg_covertool_homepage = https://github.com/idubrov/covertool +pkg_covertool_fetch = git +pkg_covertool_repo = https://github.com/idubrov/covertool +pkg_covertool_commit = master + +PACKAGES += cowboy +pkg_cowboy_name = cowboy +pkg_cowboy_description = Small, fast and modular HTTP server. +pkg_cowboy_homepage = http://ninenines.eu +pkg_cowboy_fetch = git +pkg_cowboy_repo = https://github.com/ninenines/cowboy +pkg_cowboy_commit = 1.0.4 + +PACKAGES += cowdb +pkg_cowdb_name = cowdb +pkg_cowdb_description = Pure Key/Value database library for Erlang Applications +pkg_cowdb_homepage = https://github.com/refuge/cowdb +pkg_cowdb_fetch = git +pkg_cowdb_repo = https://github.com/refuge/cowdb +pkg_cowdb_commit = master + +PACKAGES += cowlib +pkg_cowlib_name = cowlib +pkg_cowlib_description = Support library for manipulating Web protocols. +pkg_cowlib_homepage = http://ninenines.eu +pkg_cowlib_fetch = git +pkg_cowlib_repo = https://github.com/ninenines/cowlib +pkg_cowlib_commit = 1.0.2 + +PACKAGES += cpg +pkg_cpg_name = cpg +pkg_cpg_description = CloudI Process Groups +pkg_cpg_homepage = https://github.com/okeuday/cpg +pkg_cpg_fetch = git +pkg_cpg_repo = https://github.com/okeuday/cpg +pkg_cpg_commit = master + +PACKAGES += cqerl +pkg_cqerl_name = cqerl +pkg_cqerl_description = Native Erlang CQL client for Cassandra +pkg_cqerl_homepage = https://matehat.github.io/cqerl/ +pkg_cqerl_fetch = git +pkg_cqerl_repo = https://github.com/matehat/cqerl +pkg_cqerl_commit = master + +PACKAGES += cr +pkg_cr_name = cr +pkg_cr_description = Chain Replication +pkg_cr_homepage = https://synrc.com/apps/cr/doc/cr.htm +pkg_cr_fetch = git +pkg_cr_repo = https://github.com/spawnproc/cr +pkg_cr_commit = master + +PACKAGES += cuttlefish +pkg_cuttlefish_name = cuttlefish +pkg_cuttlefish_description = never lose your childlike sense of wonder baby cuttlefish, promise me? +pkg_cuttlefish_homepage = https://github.com/basho/cuttlefish +pkg_cuttlefish_fetch = git +pkg_cuttlefish_repo = https://github.com/basho/cuttlefish +pkg_cuttlefish_commit = master + +PACKAGES += damocles +pkg_damocles_name = damocles +pkg_damocles_description = Erlang library for generating adversarial network conditions for QAing distributed applications/systems on a single Linux box. +pkg_damocles_homepage = https://github.com/lostcolony/damocles +pkg_damocles_fetch = git +pkg_damocles_repo = https://github.com/lostcolony/damocles +pkg_damocles_commit = master + +PACKAGES += debbie +pkg_debbie_name = debbie +pkg_debbie_description = .DEB Built In Erlang +pkg_debbie_homepage = https://github.com/crownedgrouse/debbie +pkg_debbie_fetch = git +pkg_debbie_repo = https://github.com/crownedgrouse/debbie +pkg_debbie_commit = master + +PACKAGES += decimal +pkg_decimal_name = decimal +pkg_decimal_description = An Erlang decimal arithmetic library +pkg_decimal_homepage = https://github.com/tim/erlang-decimal +pkg_decimal_fetch = git +pkg_decimal_repo = https://github.com/tim/erlang-decimal +pkg_decimal_commit = master + +PACKAGES += detergent +pkg_detergent_name = detergent +pkg_detergent_description = An emulsifying Erlang SOAP library +pkg_detergent_homepage = https://github.com/devinus/detergent +pkg_detergent_fetch = git +pkg_detergent_repo = https://github.com/devinus/detergent +pkg_detergent_commit = master + +PACKAGES += detest +pkg_detest_name = detest +pkg_detest_description = Tool for running tests on a cluster of erlang nodes +pkg_detest_homepage = https://github.com/biokoda/detest +pkg_detest_fetch = git +pkg_detest_repo = https://github.com/biokoda/detest +pkg_detest_commit = master + +PACKAGES += dh_date +pkg_dh_date_name = dh_date +pkg_dh_date_description = Date formatting / parsing library for erlang +pkg_dh_date_homepage = https://github.com/daleharvey/dh_date +pkg_dh_date_fetch = git +pkg_dh_date_repo = https://github.com/daleharvey/dh_date +pkg_dh_date_commit = master + +PACKAGES += dirbusterl +pkg_dirbusterl_name = dirbusterl +pkg_dirbusterl_description = DirBuster successor in Erlang +pkg_dirbusterl_homepage = https://github.com/silentsignal/DirBustErl +pkg_dirbusterl_fetch = git +pkg_dirbusterl_repo = https://github.com/silentsignal/DirBustErl +pkg_dirbusterl_commit = master + +PACKAGES += dispcount +pkg_dispcount_name = dispcount +pkg_dispcount_description = Erlang task dispatcher based on ETS counters. +pkg_dispcount_homepage = https://github.com/ferd/dispcount +pkg_dispcount_fetch = git +pkg_dispcount_repo = https://github.com/ferd/dispcount +pkg_dispcount_commit = master + +PACKAGES += dlhttpc +pkg_dlhttpc_name = dlhttpc +pkg_dlhttpc_description = dispcount-based lhttpc fork for massive amounts of requests to limited endpoints +pkg_dlhttpc_homepage = https://github.com/ferd/dlhttpc +pkg_dlhttpc_fetch = git +pkg_dlhttpc_repo = https://github.com/ferd/dlhttpc +pkg_dlhttpc_commit = master + +PACKAGES += dns +pkg_dns_name = dns +pkg_dns_description = Erlang DNS library +pkg_dns_homepage = https://github.com/aetrion/dns_erlang +pkg_dns_fetch = git +pkg_dns_repo = https://github.com/aetrion/dns_erlang +pkg_dns_commit = master + +PACKAGES += dnssd +pkg_dnssd_name = dnssd +pkg_dnssd_description = Erlang interface to Apple's Bonjour D NS Service Discovery implementation +pkg_dnssd_homepage = https://github.com/benoitc/dnssd_erlang +pkg_dnssd_fetch = git +pkg_dnssd_repo = https://github.com/benoitc/dnssd_erlang +pkg_dnssd_commit = master + +PACKAGES += dynamic_compile +pkg_dynamic_compile_name = dynamic_compile +pkg_dynamic_compile_description = compile and load erlang modules from string input +pkg_dynamic_compile_homepage = https://github.com/jkvor/dynamic_compile +pkg_dynamic_compile_fetch = git +pkg_dynamic_compile_repo = https://github.com/jkvor/dynamic_compile +pkg_dynamic_compile_commit = master + +PACKAGES += e2 +pkg_e2_name = e2 +pkg_e2_description = Library to simply writing correct OTP applications. +pkg_e2_homepage = http://e2project.org +pkg_e2_fetch = git +pkg_e2_repo = https://github.com/gar1t/e2 +pkg_e2_commit = master + +PACKAGES += eamf +pkg_eamf_name = eamf +pkg_eamf_description = eAMF provides Action Message Format (AMF) support for Erlang +pkg_eamf_homepage = https://github.com/mrinalwadhwa/eamf +pkg_eamf_fetch = git +pkg_eamf_repo = https://github.com/mrinalwadhwa/eamf +pkg_eamf_commit = master + +PACKAGES += eavro +pkg_eavro_name = eavro +pkg_eavro_description = Apache Avro encoder/decoder +pkg_eavro_homepage = https://github.com/SIfoxDevTeam/eavro +pkg_eavro_fetch = git +pkg_eavro_repo = https://github.com/SIfoxDevTeam/eavro +pkg_eavro_commit = master + +PACKAGES += ecapnp +pkg_ecapnp_name = ecapnp +pkg_ecapnp_description = Cap'n Proto library for Erlang +pkg_ecapnp_homepage = https://github.com/kaos/ecapnp +pkg_ecapnp_fetch = git +pkg_ecapnp_repo = https://github.com/kaos/ecapnp +pkg_ecapnp_commit = master + +PACKAGES += econfig +pkg_econfig_name = econfig +pkg_econfig_description = simple Erlang config handler using INI files +pkg_econfig_homepage = https://github.com/benoitc/econfig +pkg_econfig_fetch = git +pkg_econfig_repo = https://github.com/benoitc/econfig +pkg_econfig_commit = master + +PACKAGES += edate +pkg_edate_name = edate +pkg_edate_description = date manipulation library for erlang +pkg_edate_homepage = https://github.com/dweldon/edate +pkg_edate_fetch = git +pkg_edate_repo = https://github.com/dweldon/edate +pkg_edate_commit = master + +PACKAGES += edgar +pkg_edgar_name = edgar +pkg_edgar_description = Erlang Does GNU AR +pkg_edgar_homepage = https://github.com/crownedgrouse/edgar +pkg_edgar_fetch = git +pkg_edgar_repo = https://github.com/crownedgrouse/edgar +pkg_edgar_commit = master + +PACKAGES += edis +pkg_edis_name = edis +pkg_edis_description = An Erlang implementation of Redis KV Store +pkg_edis_homepage = http://inaka.github.com/edis/ +pkg_edis_fetch = git +pkg_edis_repo = https://github.com/inaka/edis +pkg_edis_commit = master + +PACKAGES += edns +pkg_edns_name = edns +pkg_edns_description = Erlang/OTP DNS server +pkg_edns_homepage = https://github.com/hcvst/erlang-dns +pkg_edns_fetch = git +pkg_edns_repo = https://github.com/hcvst/erlang-dns +pkg_edns_commit = master + +PACKAGES += edown +pkg_edown_name = edown +pkg_edown_description = EDoc extension for generating Github-flavored Markdown +pkg_edown_homepage = https://github.com/uwiger/edown +pkg_edown_fetch = git +pkg_edown_repo = https://github.com/uwiger/edown +pkg_edown_commit = master + +PACKAGES += eep +pkg_eep_name = eep +pkg_eep_description = Erlang Easy Profiling (eep) application provides a way to analyze application performance and call hierarchy +pkg_eep_homepage = https://github.com/virtan/eep +pkg_eep_fetch = git +pkg_eep_repo = https://github.com/virtan/eep +pkg_eep_commit = master + +PACKAGES += eep_app +pkg_eep_app_name = eep_app +pkg_eep_app_description = Embedded Event Processing +pkg_eep_app_homepage = https://github.com/darach/eep-erl +pkg_eep_app_fetch = git +pkg_eep_app_repo = https://github.com/darach/eep-erl +pkg_eep_app_commit = master + +PACKAGES += efene +pkg_efene_name = efene +pkg_efene_description = Alternative syntax for the Erlang Programming Language focusing on simplicity, ease of use and programmer UX +pkg_efene_homepage = https://github.com/efene/efene +pkg_efene_fetch = git +pkg_efene_repo = https://github.com/efene/efene +pkg_efene_commit = master + +PACKAGES += egeoip +pkg_egeoip_name = egeoip +pkg_egeoip_description = Erlang IP Geolocation module, currently supporting the MaxMind GeoLite City Database. +pkg_egeoip_homepage = https://github.com/mochi/egeoip +pkg_egeoip_fetch = git +pkg_egeoip_repo = https://github.com/mochi/egeoip +pkg_egeoip_commit = master + +PACKAGES += ehsa +pkg_ehsa_name = ehsa +pkg_ehsa_description = Erlang HTTP server basic and digest authentication modules +pkg_ehsa_homepage = https://bitbucket.org/a12n/ehsa +pkg_ehsa_fetch = hg +pkg_ehsa_repo = https://bitbucket.org/a12n/ehsa +pkg_ehsa_commit = default + +PACKAGES += ej +pkg_ej_name = ej +pkg_ej_description = Helper module for working with Erlang terms representing JSON +pkg_ej_homepage = https://github.com/seth/ej +pkg_ej_fetch = git +pkg_ej_repo = https://github.com/seth/ej +pkg_ej_commit = master + +PACKAGES += ejabberd +pkg_ejabberd_name = ejabberd +pkg_ejabberd_description = Robust, ubiquitous and massively scalable Jabber / XMPP Instant Messaging platform +pkg_ejabberd_homepage = https://github.com/processone/ejabberd +pkg_ejabberd_fetch = git +pkg_ejabberd_repo = https://github.com/processone/ejabberd +pkg_ejabberd_commit = master + +PACKAGES += ejwt +pkg_ejwt_name = ejwt +pkg_ejwt_description = erlang library for JSON Web Token +pkg_ejwt_homepage = https://github.com/artefactop/ejwt +pkg_ejwt_fetch = git +pkg_ejwt_repo = https://github.com/artefactop/ejwt +pkg_ejwt_commit = master + +PACKAGES += ekaf +pkg_ekaf_name = ekaf +pkg_ekaf_description = A minimal, high-performance Kafka client in Erlang. +pkg_ekaf_homepage = https://github.com/helpshift/ekaf +pkg_ekaf_fetch = git +pkg_ekaf_repo = https://github.com/helpshift/ekaf +pkg_ekaf_commit = master + +PACKAGES += elarm +pkg_elarm_name = elarm +pkg_elarm_description = Alarm Manager for Erlang. +pkg_elarm_homepage = https://github.com/esl/elarm +pkg_elarm_fetch = git +pkg_elarm_repo = https://github.com/esl/elarm +pkg_elarm_commit = master + +PACKAGES += eleveldb +pkg_eleveldb_name = eleveldb +pkg_eleveldb_description = Erlang LevelDB API +pkg_eleveldb_homepage = https://github.com/basho/eleveldb +pkg_eleveldb_fetch = git +pkg_eleveldb_repo = https://github.com/basho/eleveldb +pkg_eleveldb_commit = master + +PACKAGES += elixir +pkg_elixir_name = elixir +pkg_elixir_description = Elixir is a dynamic, functional language designed for building scalable and maintainable applications +pkg_elixir_homepage = https://elixir-lang.org/ +pkg_elixir_fetch = git +pkg_elixir_repo = https://github.com/elixir-lang/elixir +pkg_elixir_commit = master + +PACKAGES += elli +pkg_elli_name = elli +pkg_elli_description = Simple, robust and performant Erlang web server +pkg_elli_homepage = https://github.com/elli-lib/elli +pkg_elli_fetch = git +pkg_elli_repo = https://github.com/elli-lib/elli +pkg_elli_commit = master + +PACKAGES += elvis +pkg_elvis_name = elvis +pkg_elvis_description = Erlang Style Reviewer +pkg_elvis_homepage = https://github.com/inaka/elvis +pkg_elvis_fetch = git +pkg_elvis_repo = https://github.com/inaka/elvis +pkg_elvis_commit = master + +PACKAGES += emagick +pkg_emagick_name = emagick +pkg_emagick_description = Wrapper for Graphics/ImageMagick command line tool. +pkg_emagick_homepage = https://github.com/kivra/emagick +pkg_emagick_fetch = git +pkg_emagick_repo = https://github.com/kivra/emagick +pkg_emagick_commit = master + +PACKAGES += emysql +pkg_emysql_name = emysql +pkg_emysql_description = Stable, pure Erlang MySQL driver. +pkg_emysql_homepage = https://github.com/Eonblast/Emysql +pkg_emysql_fetch = git +pkg_emysql_repo = https://github.com/Eonblast/Emysql +pkg_emysql_commit = master + +PACKAGES += enm +pkg_enm_name = enm +pkg_enm_description = Erlang driver for nanomsg +pkg_enm_homepage = https://github.com/basho/enm +pkg_enm_fetch = git +pkg_enm_repo = https://github.com/basho/enm +pkg_enm_commit = master + +PACKAGES += entop +pkg_entop_name = entop +pkg_entop_description = A top-like tool for monitoring an Erlang node +pkg_entop_homepage = https://github.com/mazenharake/entop +pkg_entop_fetch = git +pkg_entop_repo = https://github.com/mazenharake/entop +pkg_entop_commit = master + +PACKAGES += epcap +pkg_epcap_name = epcap +pkg_epcap_description = Erlang packet capture interface using pcap +pkg_epcap_homepage = https://github.com/msantos/epcap +pkg_epcap_fetch = git +pkg_epcap_repo = https://github.com/msantos/epcap +pkg_epcap_commit = master + +PACKAGES += eper +pkg_eper_name = eper +pkg_eper_description = Erlang performance and debugging tools. +pkg_eper_homepage = https://github.com/massemanet/eper +pkg_eper_fetch = git +pkg_eper_repo = https://github.com/massemanet/eper +pkg_eper_commit = master + +PACKAGES += epgsql +pkg_epgsql_name = epgsql +pkg_epgsql_description = Erlang PostgreSQL client library. +pkg_epgsql_homepage = https://github.com/epgsql/epgsql +pkg_epgsql_fetch = git +pkg_epgsql_repo = https://github.com/epgsql/epgsql +pkg_epgsql_commit = master + +PACKAGES += episcina +pkg_episcina_name = episcina +pkg_episcina_description = A simple non intrusive resource pool for connections +pkg_episcina_homepage = https://github.com/erlware/episcina +pkg_episcina_fetch = git +pkg_episcina_repo = https://github.com/erlware/episcina +pkg_episcina_commit = master + +PACKAGES += eplot +pkg_eplot_name = eplot +pkg_eplot_description = A plot engine written in erlang. +pkg_eplot_homepage = https://github.com/psyeugenic/eplot +pkg_eplot_fetch = git +pkg_eplot_repo = https://github.com/psyeugenic/eplot +pkg_eplot_commit = master + +PACKAGES += epocxy +pkg_epocxy_name = epocxy +pkg_epocxy_description = Erlang Patterns of Concurrency +pkg_epocxy_homepage = https://github.com/duomark/epocxy +pkg_epocxy_fetch = git +pkg_epocxy_repo = https://github.com/duomark/epocxy +pkg_epocxy_commit = master + +PACKAGES += epubnub +pkg_epubnub_name = epubnub +pkg_epubnub_description = Erlang PubNub API +pkg_epubnub_homepage = https://github.com/tsloughter/epubnub +pkg_epubnub_fetch = git +pkg_epubnub_repo = https://github.com/tsloughter/epubnub +pkg_epubnub_commit = master + +PACKAGES += eqm +pkg_eqm_name = eqm +pkg_eqm_description = Erlang pub sub with supply-demand channels +pkg_eqm_homepage = https://github.com/loucash/eqm +pkg_eqm_fetch = git +pkg_eqm_repo = https://github.com/loucash/eqm +pkg_eqm_commit = master + +PACKAGES += eredis +pkg_eredis_name = eredis +pkg_eredis_description = Erlang Redis client +pkg_eredis_homepage = https://github.com/wooga/eredis +pkg_eredis_fetch = git +pkg_eredis_repo = https://github.com/wooga/eredis +pkg_eredis_commit = master + +PACKAGES += eredis_pool +pkg_eredis_pool_name = eredis_pool +pkg_eredis_pool_description = eredis_pool is Pool of Redis clients, using eredis and poolboy. +pkg_eredis_pool_homepage = https://github.com/hiroeorz/eredis_pool +pkg_eredis_pool_fetch = git +pkg_eredis_pool_repo = https://github.com/hiroeorz/eredis_pool +pkg_eredis_pool_commit = master + +PACKAGES += erl_streams +pkg_erl_streams_name = erl_streams +pkg_erl_streams_description = Streams in Erlang +pkg_erl_streams_homepage = https://github.com/epappas/erl_streams +pkg_erl_streams_fetch = git +pkg_erl_streams_repo = https://github.com/epappas/erl_streams +pkg_erl_streams_commit = master + +PACKAGES += erlang_cep +pkg_erlang_cep_name = erlang_cep +pkg_erlang_cep_description = A basic CEP package written in erlang +pkg_erlang_cep_homepage = https://github.com/danmacklin/erlang_cep +pkg_erlang_cep_fetch = git +pkg_erlang_cep_repo = https://github.com/danmacklin/erlang_cep +pkg_erlang_cep_commit = master + +PACKAGES += erlang_js +pkg_erlang_js_name = erlang_js +pkg_erlang_js_description = A linked-in driver for Erlang to Mozilla's Spidermonkey Javascript runtime. +pkg_erlang_js_homepage = https://github.com/basho/erlang_js +pkg_erlang_js_fetch = git +pkg_erlang_js_repo = https://github.com/basho/erlang_js +pkg_erlang_js_commit = master + +PACKAGES += erlang_localtime +pkg_erlang_localtime_name = erlang_localtime +pkg_erlang_localtime_description = Erlang library for conversion from one local time to another +pkg_erlang_localtime_homepage = https://github.com/dmitryme/erlang_localtime +pkg_erlang_localtime_fetch = git +pkg_erlang_localtime_repo = https://github.com/dmitryme/erlang_localtime +pkg_erlang_localtime_commit = master + +PACKAGES += erlang_smtp +pkg_erlang_smtp_name = erlang_smtp +pkg_erlang_smtp_description = Erlang SMTP and POP3 server code. +pkg_erlang_smtp_homepage = https://github.com/tonyg/erlang-smtp +pkg_erlang_smtp_fetch = git +pkg_erlang_smtp_repo = https://github.com/tonyg/erlang-smtp +pkg_erlang_smtp_commit = master + +PACKAGES += erlang_term +pkg_erlang_term_name = erlang_term +pkg_erlang_term_description = Erlang Term Info +pkg_erlang_term_homepage = https://github.com/okeuday/erlang_term +pkg_erlang_term_fetch = git +pkg_erlang_term_repo = https://github.com/okeuday/erlang_term +pkg_erlang_term_commit = master + +PACKAGES += erlastic_search +pkg_erlastic_search_name = erlastic_search +pkg_erlastic_search_description = An Erlang app for communicating with Elastic Search's rest interface. +pkg_erlastic_search_homepage = https://github.com/tsloughter/erlastic_search +pkg_erlastic_search_fetch = git +pkg_erlastic_search_repo = https://github.com/tsloughter/erlastic_search +pkg_erlastic_search_commit = master + +PACKAGES += erlasticsearch +pkg_erlasticsearch_name = erlasticsearch +pkg_erlasticsearch_description = Erlang thrift interface to elastic_search +pkg_erlasticsearch_homepage = https://github.com/dieswaytoofast/erlasticsearch +pkg_erlasticsearch_fetch = git +pkg_erlasticsearch_repo = https://github.com/dieswaytoofast/erlasticsearch +pkg_erlasticsearch_commit = master + +PACKAGES += erlbrake +pkg_erlbrake_name = erlbrake +pkg_erlbrake_description = Erlang Airbrake notification client +pkg_erlbrake_homepage = https://github.com/kenpratt/erlbrake +pkg_erlbrake_fetch = git +pkg_erlbrake_repo = https://github.com/kenpratt/erlbrake +pkg_erlbrake_commit = master + +PACKAGES += erlcloud +pkg_erlcloud_name = erlcloud +pkg_erlcloud_description = Cloud Computing library for erlang (Amazon EC2, S3, SQS, SimpleDB, Mechanical Turk, ELB) +pkg_erlcloud_homepage = https://github.com/gleber/erlcloud +pkg_erlcloud_fetch = git +pkg_erlcloud_repo = https://github.com/gleber/erlcloud +pkg_erlcloud_commit = master + +PACKAGES += erlcron +pkg_erlcron_name = erlcron +pkg_erlcron_description = Erlang cronish system +pkg_erlcron_homepage = https://github.com/erlware/erlcron +pkg_erlcron_fetch = git +pkg_erlcron_repo = https://github.com/erlware/erlcron +pkg_erlcron_commit = master + +PACKAGES += erldb +pkg_erldb_name = erldb +pkg_erldb_description = ORM (Object-relational mapping) application implemented in Erlang +pkg_erldb_homepage = http://erldb.org +pkg_erldb_fetch = git +pkg_erldb_repo = https://github.com/erldb/erldb +pkg_erldb_commit = master + +PACKAGES += erldis +pkg_erldis_name = erldis +pkg_erldis_description = redis erlang client library +pkg_erldis_homepage = https://github.com/cstar/erldis +pkg_erldis_fetch = git +pkg_erldis_repo = https://github.com/cstar/erldis +pkg_erldis_commit = master + +PACKAGES += erldns +pkg_erldns_name = erldns +pkg_erldns_description = DNS server, in erlang. +pkg_erldns_homepage = https://github.com/aetrion/erl-dns +pkg_erldns_fetch = git +pkg_erldns_repo = https://github.com/aetrion/erl-dns +pkg_erldns_commit = master + +PACKAGES += erldocker +pkg_erldocker_name = erldocker +pkg_erldocker_description = Docker Remote API client for Erlang +pkg_erldocker_homepage = https://github.com/proger/erldocker +pkg_erldocker_fetch = git +pkg_erldocker_repo = https://github.com/proger/erldocker +pkg_erldocker_commit = master + +PACKAGES += erlfsmon +pkg_erlfsmon_name = erlfsmon +pkg_erlfsmon_description = Erlang filesystem event watcher for Linux and OSX +pkg_erlfsmon_homepage = https://github.com/proger/erlfsmon +pkg_erlfsmon_fetch = git +pkg_erlfsmon_repo = https://github.com/proger/erlfsmon +pkg_erlfsmon_commit = master + +PACKAGES += erlgit +pkg_erlgit_name = erlgit +pkg_erlgit_description = Erlang convenience wrapper around git executable +pkg_erlgit_homepage = https://github.com/gleber/erlgit +pkg_erlgit_fetch = git +pkg_erlgit_repo = https://github.com/gleber/erlgit +pkg_erlgit_commit = master + +PACKAGES += erlguten +pkg_erlguten_name = erlguten +pkg_erlguten_description = ErlGuten is a system for high-quality typesetting, written purely in Erlang. +pkg_erlguten_homepage = https://github.com/richcarl/erlguten +pkg_erlguten_fetch = git +pkg_erlguten_repo = https://github.com/richcarl/erlguten +pkg_erlguten_commit = master + +PACKAGES += erlmc +pkg_erlmc_name = erlmc +pkg_erlmc_description = Erlang memcached binary protocol client +pkg_erlmc_homepage = https://github.com/jkvor/erlmc +pkg_erlmc_fetch = git +pkg_erlmc_repo = https://github.com/jkvor/erlmc +pkg_erlmc_commit = master + +PACKAGES += erlmongo +pkg_erlmongo_name = erlmongo +pkg_erlmongo_description = Record based Erlang driver for MongoDB with gridfs support +pkg_erlmongo_homepage = https://github.com/SergejJurecko/erlmongo +pkg_erlmongo_fetch = git +pkg_erlmongo_repo = https://github.com/SergejJurecko/erlmongo +pkg_erlmongo_commit = master + +PACKAGES += erlog +pkg_erlog_name = erlog +pkg_erlog_description = Prolog interpreter in and for Erlang +pkg_erlog_homepage = https://github.com/rvirding/erlog +pkg_erlog_fetch = git +pkg_erlog_repo = https://github.com/rvirding/erlog +pkg_erlog_commit = master + +PACKAGES += erlpass +pkg_erlpass_name = erlpass +pkg_erlpass_description = A library to handle password hashing and changing in a safe manner, independent from any kind of storage whatsoever. +pkg_erlpass_homepage = https://github.com/ferd/erlpass +pkg_erlpass_fetch = git +pkg_erlpass_repo = https://github.com/ferd/erlpass +pkg_erlpass_commit = master + +PACKAGES += erlport +pkg_erlport_name = erlport +pkg_erlport_description = ErlPort - connect Erlang to other languages +pkg_erlport_homepage = https://github.com/hdima/erlport +pkg_erlport_fetch = git +pkg_erlport_repo = https://github.com/hdima/erlport +pkg_erlport_commit = master + +PACKAGES += erlsh +pkg_erlsh_name = erlsh +pkg_erlsh_description = Erlang shell tools +pkg_erlsh_homepage = https://github.com/proger/erlsh +pkg_erlsh_fetch = git +pkg_erlsh_repo = https://github.com/proger/erlsh +pkg_erlsh_commit = master + +PACKAGES += erlsha2 +pkg_erlsha2_name = erlsha2 +pkg_erlsha2_description = SHA-224, SHA-256, SHA-384, SHA-512 implemented in Erlang NIFs. +pkg_erlsha2_homepage = https://github.com/vinoski/erlsha2 +pkg_erlsha2_fetch = git +pkg_erlsha2_repo = https://github.com/vinoski/erlsha2 +pkg_erlsha2_commit = master + +PACKAGES += erlsom +pkg_erlsom_name = erlsom +pkg_erlsom_description = XML parser for Erlang +pkg_erlsom_homepage = https://github.com/willemdj/erlsom +pkg_erlsom_fetch = git +pkg_erlsom_repo = https://github.com/willemdj/erlsom +pkg_erlsom_commit = master + +PACKAGES += erlubi +pkg_erlubi_name = erlubi +pkg_erlubi_description = Ubigraph Erlang Client (and Process Visualizer) +pkg_erlubi_homepage = https://github.com/krestenkrab/erlubi +pkg_erlubi_fetch = git +pkg_erlubi_repo = https://github.com/krestenkrab/erlubi +pkg_erlubi_commit = master + +PACKAGES += erlvolt +pkg_erlvolt_name = erlvolt +pkg_erlvolt_description = VoltDB Erlang Client Driver +pkg_erlvolt_homepage = https://github.com/VoltDB/voltdb-client-erlang +pkg_erlvolt_fetch = git +pkg_erlvolt_repo = https://github.com/VoltDB/voltdb-client-erlang +pkg_erlvolt_commit = master + +PACKAGES += erlware_commons +pkg_erlware_commons_name = erlware_commons +pkg_erlware_commons_description = Erlware Commons is an Erlware project focused on all aspects of reusable Erlang components. +pkg_erlware_commons_homepage = https://github.com/erlware/erlware_commons +pkg_erlware_commons_fetch = git +pkg_erlware_commons_repo = https://github.com/erlware/erlware_commons +pkg_erlware_commons_commit = master + +PACKAGES += erlydtl +pkg_erlydtl_name = erlydtl +pkg_erlydtl_description = Django Template Language for Erlang. +pkg_erlydtl_homepage = https://github.com/erlydtl/erlydtl +pkg_erlydtl_fetch = git +pkg_erlydtl_repo = https://github.com/erlydtl/erlydtl +pkg_erlydtl_commit = master + +PACKAGES += errd +pkg_errd_name = errd +pkg_errd_description = Erlang RRDTool library +pkg_errd_homepage = https://github.com/archaelus/errd +pkg_errd_fetch = git +pkg_errd_repo = https://github.com/archaelus/errd +pkg_errd_commit = master + +PACKAGES += erserve +pkg_erserve_name = erserve +pkg_erserve_description = Erlang/Rserve communication interface +pkg_erserve_homepage = https://github.com/del/erserve +pkg_erserve_fetch = git +pkg_erserve_repo = https://github.com/del/erserve +pkg_erserve_commit = master + +PACKAGES += erwa +pkg_erwa_name = erwa +pkg_erwa_description = A WAMP router and client written in Erlang. +pkg_erwa_homepage = https://github.com/bwegh/erwa +pkg_erwa_fetch = git +pkg_erwa_repo = https://github.com/bwegh/erwa +pkg_erwa_commit = master + +PACKAGES += escalus +pkg_escalus_name = escalus +pkg_escalus_description = An XMPP client library in Erlang for conveniently testing XMPP servers +pkg_escalus_homepage = https://github.com/esl/escalus +pkg_escalus_fetch = git +pkg_escalus_repo = https://github.com/esl/escalus +pkg_escalus_commit = master + +PACKAGES += esh_mk +pkg_esh_mk_name = esh_mk +pkg_esh_mk_description = esh template engine plugin for erlang.mk +pkg_esh_mk_homepage = https://github.com/crownedgrouse/esh.mk +pkg_esh_mk_fetch = git +pkg_esh_mk_repo = https://github.com/crownedgrouse/esh.mk.git +pkg_esh_mk_commit = master + +PACKAGES += espec +pkg_espec_name = espec +pkg_espec_description = ESpec: Behaviour driven development framework for Erlang +pkg_espec_homepage = https://github.com/lucaspiller/espec +pkg_espec_fetch = git +pkg_espec_repo = https://github.com/lucaspiller/espec +pkg_espec_commit = master + +PACKAGES += estatsd +pkg_estatsd_name = estatsd +pkg_estatsd_description = Erlang stats aggregation app that periodically flushes data to graphite +pkg_estatsd_homepage = https://github.com/RJ/estatsd +pkg_estatsd_fetch = git +pkg_estatsd_repo = https://github.com/RJ/estatsd +pkg_estatsd_commit = master + +PACKAGES += etap +pkg_etap_name = etap +pkg_etap_description = etap is a simple erlang testing library that provides TAP compliant output. +pkg_etap_homepage = https://github.com/ngerakines/etap +pkg_etap_fetch = git +pkg_etap_repo = https://github.com/ngerakines/etap +pkg_etap_commit = master + +PACKAGES += etest +pkg_etest_name = etest +pkg_etest_description = A lightweight, convention over configuration test framework for Erlang +pkg_etest_homepage = https://github.com/wooga/etest +pkg_etest_fetch = git +pkg_etest_repo = https://github.com/wooga/etest +pkg_etest_commit = master + +PACKAGES += etest_http +pkg_etest_http_name = etest_http +pkg_etest_http_description = etest Assertions around HTTP (client-side) +pkg_etest_http_homepage = https://github.com/wooga/etest_http +pkg_etest_http_fetch = git +pkg_etest_http_repo = https://github.com/wooga/etest_http +pkg_etest_http_commit = master + +PACKAGES += etoml +pkg_etoml_name = etoml +pkg_etoml_description = TOML language erlang parser +pkg_etoml_homepage = https://github.com/kalta/etoml +pkg_etoml_fetch = git +pkg_etoml_repo = https://github.com/kalta/etoml +pkg_etoml_commit = master + +PACKAGES += eunit +pkg_eunit_name = eunit +pkg_eunit_description = The EUnit lightweight unit testing framework for Erlang - this is the canonical development repository. +pkg_eunit_homepage = https://github.com/richcarl/eunit +pkg_eunit_fetch = git +pkg_eunit_repo = https://github.com/richcarl/eunit +pkg_eunit_commit = master + +PACKAGES += eunit_formatters +pkg_eunit_formatters_name = eunit_formatters +pkg_eunit_formatters_description = Because eunit's output sucks. Let's make it better. +pkg_eunit_formatters_homepage = https://github.com/seancribbs/eunit_formatters +pkg_eunit_formatters_fetch = git +pkg_eunit_formatters_repo = https://github.com/seancribbs/eunit_formatters +pkg_eunit_formatters_commit = master + +PACKAGES += euthanasia +pkg_euthanasia_name = euthanasia +pkg_euthanasia_description = Merciful killer for your Erlang processes +pkg_euthanasia_homepage = https://github.com/doubleyou/euthanasia +pkg_euthanasia_fetch = git +pkg_euthanasia_repo = https://github.com/doubleyou/euthanasia +pkg_euthanasia_commit = master + +PACKAGES += evum +pkg_evum_name = evum +pkg_evum_description = Spawn Linux VMs as Erlang processes in the Erlang VM +pkg_evum_homepage = https://github.com/msantos/evum +pkg_evum_fetch = git +pkg_evum_repo = https://github.com/msantos/evum +pkg_evum_commit = master + +PACKAGES += exec +pkg_exec_name = erlexec +pkg_exec_description = Execute and control OS processes from Erlang/OTP. +pkg_exec_homepage = http://saleyn.github.com/erlexec +pkg_exec_fetch = git +pkg_exec_repo = https://github.com/saleyn/erlexec +pkg_exec_commit = master + +PACKAGES += exml +pkg_exml_name = exml +pkg_exml_description = XML parsing library in Erlang +pkg_exml_homepage = https://github.com/paulgray/exml +pkg_exml_fetch = git +pkg_exml_repo = https://github.com/paulgray/exml +pkg_exml_commit = master + +PACKAGES += exometer +pkg_exometer_name = exometer +pkg_exometer_description = Basic measurement objects and probe behavior +pkg_exometer_homepage = https://github.com/Feuerlabs/exometer +pkg_exometer_fetch = git +pkg_exometer_repo = https://github.com/Feuerlabs/exometer +pkg_exometer_commit = master + +PACKAGES += exs1024 +pkg_exs1024_name = exs1024 +pkg_exs1024_description = Xorshift1024star pseudo random number generator for Erlang. +pkg_exs1024_homepage = https://github.com/jj1bdx/exs1024 +pkg_exs1024_fetch = git +pkg_exs1024_repo = https://github.com/jj1bdx/exs1024 +pkg_exs1024_commit = master + +PACKAGES += exs64 +pkg_exs64_name = exs64 +pkg_exs64_description = Xorshift64star pseudo random number generator for Erlang. +pkg_exs64_homepage = https://github.com/jj1bdx/exs64 +pkg_exs64_fetch = git +pkg_exs64_repo = https://github.com/jj1bdx/exs64 +pkg_exs64_commit = master + +PACKAGES += exsplus116 +pkg_exsplus116_name = exsplus116 +pkg_exsplus116_description = Xorshift116plus for Erlang +pkg_exsplus116_homepage = https://github.com/jj1bdx/exsplus116 +pkg_exsplus116_fetch = git +pkg_exsplus116_repo = https://github.com/jj1bdx/exsplus116 +pkg_exsplus116_commit = master + +PACKAGES += exsplus128 +pkg_exsplus128_name = exsplus128 +pkg_exsplus128_description = Xorshift128plus pseudo random number generator for Erlang. +pkg_exsplus128_homepage = https://github.com/jj1bdx/exsplus128 +pkg_exsplus128_fetch = git +pkg_exsplus128_repo = https://github.com/jj1bdx/exsplus128 +pkg_exsplus128_commit = master + +PACKAGES += ezmq +pkg_ezmq_name = ezmq +pkg_ezmq_description = zMQ implemented in Erlang +pkg_ezmq_homepage = https://github.com/RoadRunnr/ezmq +pkg_ezmq_fetch = git +pkg_ezmq_repo = https://github.com/RoadRunnr/ezmq +pkg_ezmq_commit = master + +PACKAGES += ezmtp +pkg_ezmtp_name = ezmtp +pkg_ezmtp_description = ZMTP protocol in pure Erlang. +pkg_ezmtp_homepage = https://github.com/a13x/ezmtp +pkg_ezmtp_fetch = git +pkg_ezmtp_repo = https://github.com/a13x/ezmtp +pkg_ezmtp_commit = master + +PACKAGES += fast_disk_log +pkg_fast_disk_log_name = fast_disk_log +pkg_fast_disk_log_description = Pool-based asynchronous Erlang disk logger +pkg_fast_disk_log_homepage = https://github.com/lpgauth/fast_disk_log +pkg_fast_disk_log_fetch = git +pkg_fast_disk_log_repo = https://github.com/lpgauth/fast_disk_log +pkg_fast_disk_log_commit = master + +PACKAGES += feeder +pkg_feeder_name = feeder +pkg_feeder_description = Stream parse RSS and Atom formatted XML feeds. +pkg_feeder_homepage = https://github.com/michaelnisi/feeder +pkg_feeder_fetch = git +pkg_feeder_repo = https://github.com/michaelnisi/feeder +pkg_feeder_commit = master + +PACKAGES += find_crate +pkg_find_crate_name = find_crate +pkg_find_crate_description = Find Rust libs and exes in Erlang application priv directory +pkg_find_crate_homepage = https://github.com/goertzenator/find_crate +pkg_find_crate_fetch = git +pkg_find_crate_repo = https://github.com/goertzenator/find_crate +pkg_find_crate_commit = master + +PACKAGES += fix +pkg_fix_name = fix +pkg_fix_description = http://fixprotocol.org/ implementation. +pkg_fix_homepage = https://github.com/maxlapshin/fix +pkg_fix_fetch = git +pkg_fix_repo = https://github.com/maxlapshin/fix +pkg_fix_commit = master + +PACKAGES += flower +pkg_flower_name = flower +pkg_flower_description = FlowER - a Erlang OpenFlow development platform +pkg_flower_homepage = https://github.com/travelping/flower +pkg_flower_fetch = git +pkg_flower_repo = https://github.com/travelping/flower +pkg_flower_commit = master + +PACKAGES += fn +pkg_fn_name = fn +pkg_fn_description = Function utilities for Erlang +pkg_fn_homepage = https://github.com/reiddraper/fn +pkg_fn_fetch = git +pkg_fn_repo = https://github.com/reiddraper/fn +pkg_fn_commit = master + +PACKAGES += folsom +pkg_folsom_name = folsom +pkg_folsom_description = Expose Erlang Events and Metrics +pkg_folsom_homepage = https://github.com/boundary/folsom +pkg_folsom_fetch = git +pkg_folsom_repo = https://github.com/boundary/folsom +pkg_folsom_commit = master + +PACKAGES += folsom_cowboy +pkg_folsom_cowboy_name = folsom_cowboy +pkg_folsom_cowboy_description = A Cowboy based Folsom HTTP Wrapper. +pkg_folsom_cowboy_homepage = https://github.com/boundary/folsom_cowboy +pkg_folsom_cowboy_fetch = git +pkg_folsom_cowboy_repo = https://github.com/boundary/folsom_cowboy +pkg_folsom_cowboy_commit = master + +PACKAGES += folsomite +pkg_folsomite_name = folsomite +pkg_folsomite_description = blow up your graphite / riemann server with folsom metrics +pkg_folsomite_homepage = https://github.com/campanja/folsomite +pkg_folsomite_fetch = git +pkg_folsomite_repo = https://github.com/campanja/folsomite +pkg_folsomite_commit = master + +PACKAGES += fs +pkg_fs_name = fs +pkg_fs_description = Erlang FileSystem Listener +pkg_fs_homepage = https://github.com/synrc/fs +pkg_fs_fetch = git +pkg_fs_repo = https://github.com/synrc/fs +pkg_fs_commit = master + +PACKAGES += fuse +pkg_fuse_name = fuse +pkg_fuse_description = A Circuit Breaker for Erlang +pkg_fuse_homepage = https://github.com/jlouis/fuse +pkg_fuse_fetch = git +pkg_fuse_repo = https://github.com/jlouis/fuse +pkg_fuse_commit = master + +PACKAGES += gcm +pkg_gcm_name = gcm +pkg_gcm_description = An Erlang application for Google Cloud Messaging +pkg_gcm_homepage = https://github.com/pdincau/gcm-erlang +pkg_gcm_fetch = git +pkg_gcm_repo = https://github.com/pdincau/gcm-erlang +pkg_gcm_commit = master + +PACKAGES += gcprof +pkg_gcprof_name = gcprof +pkg_gcprof_description = Garbage Collection profiler for Erlang +pkg_gcprof_homepage = https://github.com/knutin/gcprof +pkg_gcprof_fetch = git +pkg_gcprof_repo = https://github.com/knutin/gcprof +pkg_gcprof_commit = master + +PACKAGES += geas +pkg_geas_name = geas +pkg_geas_description = Guess Erlang Application Scattering +pkg_geas_homepage = https://github.com/crownedgrouse/geas +pkg_geas_fetch = git +pkg_geas_repo = https://github.com/crownedgrouse/geas +pkg_geas_commit = master + +PACKAGES += geef +pkg_geef_name = geef +pkg_geef_description = Git NEEEEF (Erlang NIF) +pkg_geef_homepage = https://github.com/carlosmn/geef +pkg_geef_fetch = git +pkg_geef_repo = https://github.com/carlosmn/geef +pkg_geef_commit = master + +PACKAGES += gen_coap +pkg_gen_coap_name = gen_coap +pkg_gen_coap_description = Generic Erlang CoAP Client/Server +pkg_gen_coap_homepage = https://github.com/gotthardp/gen_coap +pkg_gen_coap_fetch = git +pkg_gen_coap_repo = https://github.com/gotthardp/gen_coap +pkg_gen_coap_commit = master + +PACKAGES += gen_cycle +pkg_gen_cycle_name = gen_cycle +pkg_gen_cycle_description = Simple, generic OTP behaviour for recurring tasks +pkg_gen_cycle_homepage = https://github.com/aerosol/gen_cycle +pkg_gen_cycle_fetch = git +pkg_gen_cycle_repo = https://github.com/aerosol/gen_cycle +pkg_gen_cycle_commit = develop + +PACKAGES += gen_icmp +pkg_gen_icmp_name = gen_icmp +pkg_gen_icmp_description = Erlang interface to ICMP sockets +pkg_gen_icmp_homepage = https://github.com/msantos/gen_icmp +pkg_gen_icmp_fetch = git +pkg_gen_icmp_repo = https://github.com/msantos/gen_icmp +pkg_gen_icmp_commit = master + +PACKAGES += gen_leader +pkg_gen_leader_name = gen_leader +pkg_gen_leader_description = leader election behavior +pkg_gen_leader_homepage = https://github.com/garret-smith/gen_leader_revival +pkg_gen_leader_fetch = git +pkg_gen_leader_repo = https://github.com/garret-smith/gen_leader_revival +pkg_gen_leader_commit = master + +PACKAGES += gen_nb_server +pkg_gen_nb_server_name = gen_nb_server +pkg_gen_nb_server_description = OTP behavior for writing non-blocking servers +pkg_gen_nb_server_homepage = https://github.com/kevsmith/gen_nb_server +pkg_gen_nb_server_fetch = git +pkg_gen_nb_server_repo = https://github.com/kevsmith/gen_nb_server +pkg_gen_nb_server_commit = master + +PACKAGES += gen_paxos +pkg_gen_paxos_name = gen_paxos +pkg_gen_paxos_description = An Erlang/OTP-style implementation of the PAXOS distributed consensus protocol +pkg_gen_paxos_homepage = https://github.com/gburd/gen_paxos +pkg_gen_paxos_fetch = git +pkg_gen_paxos_repo = https://github.com/gburd/gen_paxos +pkg_gen_paxos_commit = master + +PACKAGES += gen_rpc +pkg_gen_rpc_name = gen_rpc +pkg_gen_rpc_description = A scalable RPC library for Erlang-VM based languages +pkg_gen_rpc_homepage = https://github.com/priestjim/gen_rpc.git +pkg_gen_rpc_fetch = git +pkg_gen_rpc_repo = https://github.com/priestjim/gen_rpc.git +pkg_gen_rpc_commit = master + +PACKAGES += gen_smtp +pkg_gen_smtp_name = gen_smtp +pkg_gen_smtp_description = A generic Erlang SMTP server and client that can be extended via callback modules +pkg_gen_smtp_homepage = https://github.com/Vagabond/gen_smtp +pkg_gen_smtp_fetch = git +pkg_gen_smtp_repo = https://github.com/Vagabond/gen_smtp +pkg_gen_smtp_commit = master + +PACKAGES += gen_tracker +pkg_gen_tracker_name = gen_tracker +pkg_gen_tracker_description = supervisor with ets handling of children and their metadata +pkg_gen_tracker_homepage = https://github.com/erlyvideo/gen_tracker +pkg_gen_tracker_fetch = git +pkg_gen_tracker_repo = https://github.com/erlyvideo/gen_tracker +pkg_gen_tracker_commit = master + +PACKAGES += gen_unix +pkg_gen_unix_name = gen_unix +pkg_gen_unix_description = Erlang Unix socket interface +pkg_gen_unix_homepage = https://github.com/msantos/gen_unix +pkg_gen_unix_fetch = git +pkg_gen_unix_repo = https://github.com/msantos/gen_unix +pkg_gen_unix_commit = master + +PACKAGES += geode +pkg_geode_name = geode +pkg_geode_description = geohash/proximity lookup in pure, uncut erlang. +pkg_geode_homepage = https://github.com/bradfordw/geode +pkg_geode_fetch = git +pkg_geode_repo = https://github.com/bradfordw/geode +pkg_geode_commit = master + +PACKAGES += getopt +pkg_getopt_name = getopt +pkg_getopt_description = Module to parse command line arguments using the GNU getopt syntax +pkg_getopt_homepage = https://github.com/jcomellas/getopt +pkg_getopt_fetch = git +pkg_getopt_repo = https://github.com/jcomellas/getopt +pkg_getopt_commit = master + +PACKAGES += gettext +pkg_gettext_name = gettext +pkg_gettext_description = Erlang internationalization library. +pkg_gettext_homepage = https://github.com/etnt/gettext +pkg_gettext_fetch = git +pkg_gettext_repo = https://github.com/etnt/gettext +pkg_gettext_commit = master + +PACKAGES += giallo +pkg_giallo_name = giallo +pkg_giallo_description = Small and flexible web framework on top of Cowboy +pkg_giallo_homepage = https://github.com/kivra/giallo +pkg_giallo_fetch = git +pkg_giallo_repo = https://github.com/kivra/giallo +pkg_giallo_commit = master + +PACKAGES += gin +pkg_gin_name = gin +pkg_gin_description = The guards and for Erlang parse_transform +pkg_gin_homepage = https://github.com/mad-cocktail/gin +pkg_gin_fetch = git +pkg_gin_repo = https://github.com/mad-cocktail/gin +pkg_gin_commit = master + +PACKAGES += gitty +pkg_gitty_name = gitty +pkg_gitty_description = Git access in erlang +pkg_gitty_homepage = https://github.com/maxlapshin/gitty +pkg_gitty_fetch = git +pkg_gitty_repo = https://github.com/maxlapshin/gitty +pkg_gitty_commit = master + +PACKAGES += gold_fever +pkg_gold_fever_name = gold_fever +pkg_gold_fever_description = A Treasure Hunt for Erlangers +pkg_gold_fever_homepage = https://github.com/inaka/gold_fever +pkg_gold_fever_fetch = git +pkg_gold_fever_repo = https://github.com/inaka/gold_fever +pkg_gold_fever_commit = master + +PACKAGES += gpb +pkg_gpb_name = gpb +pkg_gpb_description = A Google Protobuf implementation for Erlang +pkg_gpb_homepage = https://github.com/tomas-abrahamsson/gpb +pkg_gpb_fetch = git +pkg_gpb_repo = https://github.com/tomas-abrahamsson/gpb +pkg_gpb_commit = master + +PACKAGES += gproc +pkg_gproc_name = gproc +pkg_gproc_description = Extended process registry for Erlang +pkg_gproc_homepage = https://github.com/uwiger/gproc +pkg_gproc_fetch = git +pkg_gproc_repo = https://github.com/uwiger/gproc +pkg_gproc_commit = master + +PACKAGES += grapherl +pkg_grapherl_name = grapherl +pkg_grapherl_description = Create graphs of Erlang systems and programs +pkg_grapherl_homepage = https://github.com/eproxus/grapherl +pkg_grapherl_fetch = git +pkg_grapherl_repo = https://github.com/eproxus/grapherl +pkg_grapherl_commit = master + +PACKAGES += grpc +pkg_grpc_name = grpc +pkg_grpc_description = gRPC server in Erlang +pkg_grpc_homepage = https://github.com/Bluehouse-Technology/grpc +pkg_grpc_fetch = git +pkg_grpc_repo = https://github.com/Bluehouse-Technology/grpc +pkg_grpc_commit = master + +PACKAGES += grpc_client +pkg_grpc_client_name = grpc_client +pkg_grpc_client_description = gRPC client in Erlang +pkg_grpc_client_homepage = https://github.com/Bluehouse-Technology/grpc_client +pkg_grpc_client_fetch = git +pkg_grpc_client_repo = https://github.com/Bluehouse-Technology/grpc_client +pkg_grpc_client_commit = master + +PACKAGES += gun +pkg_gun_name = gun +pkg_gun_description = Asynchronous SPDY, HTTP and Websocket client written in Erlang. +pkg_gun_homepage = http//ninenines.eu +pkg_gun_fetch = git +pkg_gun_repo = https://github.com/ninenines/gun +pkg_gun_commit = master + +PACKAGES += gut +pkg_gut_name = gut +pkg_gut_description = gut is a template printing, aka scaffolding, tool for Erlang. Like rails generate or yeoman +pkg_gut_homepage = https://github.com/unbalancedparentheses/gut +pkg_gut_fetch = git +pkg_gut_repo = https://github.com/unbalancedparentheses/gut +pkg_gut_commit = master + +PACKAGES += hackney +pkg_hackney_name = hackney +pkg_hackney_description = simple HTTP client in Erlang +pkg_hackney_homepage = https://github.com/benoitc/hackney +pkg_hackney_fetch = git +pkg_hackney_repo = https://github.com/benoitc/hackney +pkg_hackney_commit = master + +PACKAGES += hamcrest +pkg_hamcrest_name = hamcrest +pkg_hamcrest_description = Erlang port of Hamcrest +pkg_hamcrest_homepage = https://github.com/hyperthunk/hamcrest-erlang +pkg_hamcrest_fetch = git +pkg_hamcrest_repo = https://github.com/hyperthunk/hamcrest-erlang +pkg_hamcrest_commit = master + +PACKAGES += hanoidb +pkg_hanoidb_name = hanoidb +pkg_hanoidb_description = Erlang LSM BTree Storage +pkg_hanoidb_homepage = https://github.com/krestenkrab/hanoidb +pkg_hanoidb_fetch = git +pkg_hanoidb_repo = https://github.com/krestenkrab/hanoidb +pkg_hanoidb_commit = master + +PACKAGES += hottub +pkg_hottub_name = hottub +pkg_hottub_description = Permanent Erlang Worker Pool +pkg_hottub_homepage = https://github.com/bfrog/hottub +pkg_hottub_fetch = git +pkg_hottub_repo = https://github.com/bfrog/hottub +pkg_hottub_commit = master + +PACKAGES += hpack +pkg_hpack_name = hpack +pkg_hpack_description = HPACK Implementation for Erlang +pkg_hpack_homepage = https://github.com/joedevivo/hpack +pkg_hpack_fetch = git +pkg_hpack_repo = https://github.com/joedevivo/hpack +pkg_hpack_commit = master + +PACKAGES += hyper +pkg_hyper_name = hyper +pkg_hyper_description = Erlang implementation of HyperLogLog +pkg_hyper_homepage = https://github.com/GameAnalytics/hyper +pkg_hyper_fetch = git +pkg_hyper_repo = https://github.com/GameAnalytics/hyper +pkg_hyper_commit = master + +PACKAGES += i18n +pkg_i18n_name = i18n +pkg_i18n_description = International components for unicode from Erlang (unicode, date, string, number, format, locale, localization, transliteration, icu4e) +pkg_i18n_homepage = https://github.com/erlang-unicode/i18n +pkg_i18n_fetch = git +pkg_i18n_repo = https://github.com/erlang-unicode/i18n +pkg_i18n_commit = master + +PACKAGES += ibrowse +pkg_ibrowse_name = ibrowse +pkg_ibrowse_description = Erlang HTTP client +pkg_ibrowse_homepage = https://github.com/cmullaparthi/ibrowse +pkg_ibrowse_fetch = git +pkg_ibrowse_repo = https://github.com/cmullaparthi/ibrowse +pkg_ibrowse_commit = master + +PACKAGES += idna +pkg_idna_name = idna +pkg_idna_description = Erlang IDNA lib +pkg_idna_homepage = https://github.com/benoitc/erlang-idna +pkg_idna_fetch = git +pkg_idna_repo = https://github.com/benoitc/erlang-idna +pkg_idna_commit = master + +PACKAGES += ierlang +pkg_ierlang_name = ierlang +pkg_ierlang_description = An Erlang language kernel for IPython. +pkg_ierlang_homepage = https://github.com/robbielynch/ierlang +pkg_ierlang_fetch = git +pkg_ierlang_repo = https://github.com/robbielynch/ierlang +pkg_ierlang_commit = master + +PACKAGES += iota +pkg_iota_name = iota +pkg_iota_description = iota (Inter-dependency Objective Testing Apparatus) - a tool to enforce clean separation of responsibilities in Erlang code +pkg_iota_homepage = https://github.com/jpgneves/iota +pkg_iota_fetch = git +pkg_iota_repo = https://github.com/jpgneves/iota +pkg_iota_commit = master + +PACKAGES += irc_lib +pkg_irc_lib_name = irc_lib +pkg_irc_lib_description = Erlang irc client library +pkg_irc_lib_homepage = https://github.com/OtpChatBot/irc_lib +pkg_irc_lib_fetch = git +pkg_irc_lib_repo = https://github.com/OtpChatBot/irc_lib +pkg_irc_lib_commit = master + +PACKAGES += ircd +pkg_ircd_name = ircd +pkg_ircd_description = A pluggable IRC daemon application/library for Erlang. +pkg_ircd_homepage = https://github.com/tonyg/erlang-ircd +pkg_ircd_fetch = git +pkg_ircd_repo = https://github.com/tonyg/erlang-ircd +pkg_ircd_commit = master + +PACKAGES += iris +pkg_iris_name = iris +pkg_iris_description = Iris Erlang binding +pkg_iris_homepage = https://github.com/project-iris/iris-erl +pkg_iris_fetch = git +pkg_iris_repo = https://github.com/project-iris/iris-erl +pkg_iris_commit = master + +PACKAGES += iso8601 +pkg_iso8601_name = iso8601 +pkg_iso8601_description = Erlang ISO 8601 date formatter/parser +pkg_iso8601_homepage = https://github.com/seansawyer/erlang_iso8601 +pkg_iso8601_fetch = git +pkg_iso8601_repo = https://github.com/seansawyer/erlang_iso8601 +pkg_iso8601_commit = master + +PACKAGES += jamdb_sybase +pkg_jamdb_sybase_name = jamdb_sybase +pkg_jamdb_sybase_description = Erlang driver for SAP Sybase ASE +pkg_jamdb_sybase_homepage = https://github.com/erlangbureau/jamdb_sybase +pkg_jamdb_sybase_fetch = git +pkg_jamdb_sybase_repo = https://github.com/erlangbureau/jamdb_sybase +pkg_jamdb_sybase_commit = master + +PACKAGES += jerg +pkg_jerg_name = jerg +pkg_jerg_description = JSON Schema to Erlang Records Generator +pkg_jerg_homepage = https://github.com/ddossot/jerg +pkg_jerg_fetch = git +pkg_jerg_repo = https://github.com/ddossot/jerg +pkg_jerg_commit = master + +PACKAGES += jesse +pkg_jesse_name = jesse +pkg_jesse_description = jesse (JSon Schema Erlang) is an implementation of a json schema validator for Erlang. +pkg_jesse_homepage = https://github.com/for-GET/jesse +pkg_jesse_fetch = git +pkg_jesse_repo = https://github.com/for-GET/jesse +pkg_jesse_commit = master + +PACKAGES += jiffy +pkg_jiffy_name = jiffy +pkg_jiffy_description = JSON NIFs for Erlang. +pkg_jiffy_homepage = https://github.com/davisp/jiffy +pkg_jiffy_fetch = git +pkg_jiffy_repo = https://github.com/davisp/jiffy +pkg_jiffy_commit = master + +PACKAGES += jiffy_v +pkg_jiffy_v_name = jiffy_v +pkg_jiffy_v_description = JSON validation utility +pkg_jiffy_v_homepage = https://github.com/shizzard/jiffy-v +pkg_jiffy_v_fetch = git +pkg_jiffy_v_repo = https://github.com/shizzard/jiffy-v +pkg_jiffy_v_commit = master + +PACKAGES += jobs +pkg_jobs_name = jobs +pkg_jobs_description = a Job scheduler for load regulation +pkg_jobs_homepage = https://github.com/esl/jobs +pkg_jobs_fetch = git +pkg_jobs_repo = https://github.com/esl/jobs +pkg_jobs_commit = master + +PACKAGES += joxa +pkg_joxa_name = joxa +pkg_joxa_description = A Modern Lisp for the Erlang VM +pkg_joxa_homepage = https://github.com/joxa/joxa +pkg_joxa_fetch = git +pkg_joxa_repo = https://github.com/joxa/joxa +pkg_joxa_commit = master + +PACKAGES += json +pkg_json_name = json +pkg_json_description = a high level json library for erlang (17.0+) +pkg_json_homepage = https://github.com/talentdeficit/json +pkg_json_fetch = git +pkg_json_repo = https://github.com/talentdeficit/json +pkg_json_commit = master + +PACKAGES += json_rec +pkg_json_rec_name = json_rec +pkg_json_rec_description = JSON to erlang record +pkg_json_rec_homepage = https://github.com/justinkirby/json_rec +pkg_json_rec_fetch = git +pkg_json_rec_repo = https://github.com/justinkirby/json_rec +pkg_json_rec_commit = master + +PACKAGES += jsone +pkg_jsone_name = jsone +pkg_jsone_description = An Erlang library for encoding, decoding JSON data. +pkg_jsone_homepage = https://github.com/sile/jsone.git +pkg_jsone_fetch = git +pkg_jsone_repo = https://github.com/sile/jsone.git +pkg_jsone_commit = master + +PACKAGES += jsonerl +pkg_jsonerl_name = jsonerl +pkg_jsonerl_description = yet another but slightly different erlang <-> json encoder/decoder +pkg_jsonerl_homepage = https://github.com/lambder/jsonerl +pkg_jsonerl_fetch = git +pkg_jsonerl_repo = https://github.com/lambder/jsonerl +pkg_jsonerl_commit = master + +PACKAGES += jsonpath +pkg_jsonpath_name = jsonpath +pkg_jsonpath_description = Fast Erlang JSON data retrieval and updates via javascript-like notation +pkg_jsonpath_homepage = https://github.com/GeneStevens/jsonpath +pkg_jsonpath_fetch = git +pkg_jsonpath_repo = https://github.com/GeneStevens/jsonpath +pkg_jsonpath_commit = master + +PACKAGES += jsonx +pkg_jsonx_name = jsonx +pkg_jsonx_description = JSONX is an Erlang library for efficient decode and encode JSON, written in C. +pkg_jsonx_homepage = https://github.com/iskra/jsonx +pkg_jsonx_fetch = git +pkg_jsonx_repo = https://github.com/iskra/jsonx +pkg_jsonx_commit = master + +PACKAGES += jsx +pkg_jsx_name = jsx +pkg_jsx_description = An Erlang application for consuming, producing and manipulating JSON. +pkg_jsx_homepage = https://github.com/talentdeficit/jsx +pkg_jsx_fetch = git +pkg_jsx_repo = https://github.com/talentdeficit/jsx +pkg_jsx_commit = master + +PACKAGES += kafka +pkg_kafka_name = kafka +pkg_kafka_description = Kafka consumer and producer in Erlang +pkg_kafka_homepage = https://github.com/wooga/kafka-erlang +pkg_kafka_fetch = git +pkg_kafka_repo = https://github.com/wooga/kafka-erlang +pkg_kafka_commit = master + +PACKAGES += kafka_protocol +pkg_kafka_protocol_name = kafka_protocol +pkg_kafka_protocol_description = Kafka protocol Erlang library +pkg_kafka_protocol_homepage = https://github.com/klarna/kafka_protocol +pkg_kafka_protocol_fetch = git +pkg_kafka_protocol_repo = https://github.com/klarna/kafka_protocol.git +pkg_kafka_protocol_commit = master + +PACKAGES += kai +pkg_kai_name = kai +pkg_kai_description = DHT storage by Takeshi Inoue +pkg_kai_homepage = https://github.com/synrc/kai +pkg_kai_fetch = git +pkg_kai_repo = https://github.com/synrc/kai +pkg_kai_commit = master + +PACKAGES += katja +pkg_katja_name = katja +pkg_katja_description = A simple Riemann client written in Erlang. +pkg_katja_homepage = https://github.com/nifoc/katja +pkg_katja_fetch = git +pkg_katja_repo = https://github.com/nifoc/katja +pkg_katja_commit = master + +PACKAGES += kdht +pkg_kdht_name = kdht +pkg_kdht_description = kdht is an erlang DHT implementation +pkg_kdht_homepage = https://github.com/kevinlynx/kdht +pkg_kdht_fetch = git +pkg_kdht_repo = https://github.com/kevinlynx/kdht +pkg_kdht_commit = master + +PACKAGES += key2value +pkg_key2value_name = key2value +pkg_key2value_description = Erlang 2-way map +pkg_key2value_homepage = https://github.com/okeuday/key2value +pkg_key2value_fetch = git +pkg_key2value_repo = https://github.com/okeuday/key2value +pkg_key2value_commit = master + +PACKAGES += keys1value +pkg_keys1value_name = keys1value +pkg_keys1value_description = Erlang set associative map for key lists +pkg_keys1value_homepage = https://github.com/okeuday/keys1value +pkg_keys1value_fetch = git +pkg_keys1value_repo = https://github.com/okeuday/keys1value +pkg_keys1value_commit = master + +PACKAGES += kinetic +pkg_kinetic_name = kinetic +pkg_kinetic_description = Erlang Kinesis Client +pkg_kinetic_homepage = https://github.com/AdRoll/kinetic +pkg_kinetic_fetch = git +pkg_kinetic_repo = https://github.com/AdRoll/kinetic +pkg_kinetic_commit = master + +PACKAGES += kjell +pkg_kjell_name = kjell +pkg_kjell_description = Erlang Shell +pkg_kjell_homepage = https://github.com/karlll/kjell +pkg_kjell_fetch = git +pkg_kjell_repo = https://github.com/karlll/kjell +pkg_kjell_commit = master + +PACKAGES += kraken +pkg_kraken_name = kraken +pkg_kraken_description = Distributed Pubsub Server for Realtime Apps +pkg_kraken_homepage = https://github.com/Asana/kraken +pkg_kraken_fetch = git +pkg_kraken_repo = https://github.com/Asana/kraken +pkg_kraken_commit = master + +PACKAGES += kucumberl +pkg_kucumberl_name = kucumberl +pkg_kucumberl_description = A pure-erlang, open-source, implementation of Cucumber +pkg_kucumberl_homepage = https://github.com/openshine/kucumberl +pkg_kucumberl_fetch = git +pkg_kucumberl_repo = https://github.com/openshine/kucumberl +pkg_kucumberl_commit = master + +PACKAGES += kvc +pkg_kvc_name = kvc +pkg_kvc_description = KVC - Key Value Coding for Erlang data structures +pkg_kvc_homepage = https://github.com/etrepum/kvc +pkg_kvc_fetch = git +pkg_kvc_repo = https://github.com/etrepum/kvc +pkg_kvc_commit = master + +PACKAGES += kvlists +pkg_kvlists_name = kvlists +pkg_kvlists_description = Lists of key-value pairs (decoded JSON) in Erlang +pkg_kvlists_homepage = https://github.com/jcomellas/kvlists +pkg_kvlists_fetch = git +pkg_kvlists_repo = https://github.com/jcomellas/kvlists +pkg_kvlists_commit = master + +PACKAGES += kvs +pkg_kvs_name = kvs +pkg_kvs_description = Container and Iterator +pkg_kvs_homepage = https://github.com/synrc/kvs +pkg_kvs_fetch = git +pkg_kvs_repo = https://github.com/synrc/kvs +pkg_kvs_commit = master + +PACKAGES += lager +pkg_lager_name = lager +pkg_lager_description = A logging framework for Erlang/OTP. +pkg_lager_homepage = https://github.com/erlang-lager/lager +pkg_lager_fetch = git +pkg_lager_repo = https://github.com/erlang-lager/lager +pkg_lager_commit = master + +PACKAGES += lager_amqp_backend +pkg_lager_amqp_backend_name = lager_amqp_backend +pkg_lager_amqp_backend_description = AMQP RabbitMQ Lager backend +pkg_lager_amqp_backend_homepage = https://github.com/jbrisbin/lager_amqp_backend +pkg_lager_amqp_backend_fetch = git +pkg_lager_amqp_backend_repo = https://github.com/jbrisbin/lager_amqp_backend +pkg_lager_amqp_backend_commit = master + +PACKAGES += lager_syslog +pkg_lager_syslog_name = lager_syslog +pkg_lager_syslog_description = Syslog backend for lager +pkg_lager_syslog_homepage = https://github.com/erlang-lager/lager_syslog +pkg_lager_syslog_fetch = git +pkg_lager_syslog_repo = https://github.com/erlang-lager/lager_syslog +pkg_lager_syslog_commit = master + +PACKAGES += lambdapad +pkg_lambdapad_name = lambdapad +pkg_lambdapad_description = Static site generator using Erlang. Yes, Erlang. +pkg_lambdapad_homepage = https://github.com/gar1t/lambdapad +pkg_lambdapad_fetch = git +pkg_lambdapad_repo = https://github.com/gar1t/lambdapad +pkg_lambdapad_commit = master + +PACKAGES += lasp +pkg_lasp_name = lasp +pkg_lasp_description = A Language for Distributed, Eventually Consistent Computations +pkg_lasp_homepage = http://lasp-lang.org/ +pkg_lasp_fetch = git +pkg_lasp_repo = https://github.com/lasp-lang/lasp +pkg_lasp_commit = master + +PACKAGES += lasse +pkg_lasse_name = lasse +pkg_lasse_description = SSE handler for Cowboy +pkg_lasse_homepage = https://github.com/inaka/lasse +pkg_lasse_fetch = git +pkg_lasse_repo = https://github.com/inaka/lasse +pkg_lasse_commit = master + +PACKAGES += ldap +pkg_ldap_name = ldap +pkg_ldap_description = LDAP server written in Erlang +pkg_ldap_homepage = https://github.com/spawnproc/ldap +pkg_ldap_fetch = git +pkg_ldap_repo = https://github.com/spawnproc/ldap +pkg_ldap_commit = master + +PACKAGES += lethink +pkg_lethink_name = lethink +pkg_lethink_description = erlang driver for rethinkdb +pkg_lethink_homepage = https://github.com/taybin/lethink +pkg_lethink_fetch = git +pkg_lethink_repo = https://github.com/taybin/lethink +pkg_lethink_commit = master + +PACKAGES += lfe +pkg_lfe_name = lfe +pkg_lfe_description = Lisp Flavoured Erlang (LFE) +pkg_lfe_homepage = https://github.com/rvirding/lfe +pkg_lfe_fetch = git +pkg_lfe_repo = https://github.com/rvirding/lfe +pkg_lfe_commit = master + +PACKAGES += ling +pkg_ling_name = ling +pkg_ling_description = Erlang on Xen +pkg_ling_homepage = https://github.com/cloudozer/ling +pkg_ling_fetch = git +pkg_ling_repo = https://github.com/cloudozer/ling +pkg_ling_commit = master + +PACKAGES += live +pkg_live_name = live +pkg_live_description = Automated module and configuration reloader. +pkg_live_homepage = http://ninenines.eu +pkg_live_fetch = git +pkg_live_repo = https://github.com/ninenines/live +pkg_live_commit = master + +PACKAGES += lmq +pkg_lmq_name = lmq +pkg_lmq_description = Lightweight Message Queue +pkg_lmq_homepage = https://github.com/iij/lmq +pkg_lmq_fetch = git +pkg_lmq_repo = https://github.com/iij/lmq +pkg_lmq_commit = master + +PACKAGES += locker +pkg_locker_name = locker +pkg_locker_description = Atomic distributed 'check and set' for short-lived keys +pkg_locker_homepage = https://github.com/wooga/locker +pkg_locker_fetch = git +pkg_locker_repo = https://github.com/wooga/locker +pkg_locker_commit = master + +PACKAGES += locks +pkg_locks_name = locks +pkg_locks_description = A scalable, deadlock-resolving resource locker +pkg_locks_homepage = https://github.com/uwiger/locks +pkg_locks_fetch = git +pkg_locks_repo = https://github.com/uwiger/locks +pkg_locks_commit = master + +PACKAGES += log4erl +pkg_log4erl_name = log4erl +pkg_log4erl_description = A logger for erlang in the spirit of Log4J. +pkg_log4erl_homepage = https://github.com/ahmednawras/log4erl +pkg_log4erl_fetch = git +pkg_log4erl_repo = https://github.com/ahmednawras/log4erl +pkg_log4erl_commit = master + +PACKAGES += lol +pkg_lol_name = lol +pkg_lol_description = Lisp on erLang, and programming is fun again +pkg_lol_homepage = https://github.com/b0oh/lol +pkg_lol_fetch = git +pkg_lol_repo = https://github.com/b0oh/lol +pkg_lol_commit = master + +PACKAGES += lucid +pkg_lucid_name = lucid +pkg_lucid_description = HTTP/2 server written in Erlang +pkg_lucid_homepage = https://github.com/tatsuhiro-t/lucid +pkg_lucid_fetch = git +pkg_lucid_repo = https://github.com/tatsuhiro-t/lucid +pkg_lucid_commit = master + +PACKAGES += luerl +pkg_luerl_name = luerl +pkg_luerl_description = Lua in Erlang +pkg_luerl_homepage = https://github.com/rvirding/luerl +pkg_luerl_fetch = git +pkg_luerl_repo = https://github.com/rvirding/luerl +pkg_luerl_commit = develop + +PACKAGES += luwak +pkg_luwak_name = luwak +pkg_luwak_description = Large-object storage interface for Riak +pkg_luwak_homepage = https://github.com/basho/luwak +pkg_luwak_fetch = git +pkg_luwak_repo = https://github.com/basho/luwak +pkg_luwak_commit = master + +PACKAGES += lux +pkg_lux_name = lux +pkg_lux_description = Lux (LUcid eXpect scripting) simplifies test automation and provides an Expect-style execution of commands +pkg_lux_homepage = https://github.com/hawk/lux +pkg_lux_fetch = git +pkg_lux_repo = https://github.com/hawk/lux +pkg_lux_commit = master + +PACKAGES += machi +pkg_machi_name = machi +pkg_machi_description = Machi file store +pkg_machi_homepage = https://github.com/basho/machi +pkg_machi_fetch = git +pkg_machi_repo = https://github.com/basho/machi +pkg_machi_commit = master + +PACKAGES += mad +pkg_mad_name = mad +pkg_mad_description = Small and Fast Rebar Replacement +pkg_mad_homepage = https://github.com/synrc/mad +pkg_mad_fetch = git +pkg_mad_repo = https://github.com/synrc/mad +pkg_mad_commit = master + +PACKAGES += marina +pkg_marina_name = marina +pkg_marina_description = Non-blocking Erlang Cassandra CQL3 client +pkg_marina_homepage = https://github.com/lpgauth/marina +pkg_marina_fetch = git +pkg_marina_repo = https://github.com/lpgauth/marina +pkg_marina_commit = master + +PACKAGES += mavg +pkg_mavg_name = mavg +pkg_mavg_description = Erlang :: Exponential moving average library +pkg_mavg_homepage = https://github.com/EchoTeam/mavg +pkg_mavg_fetch = git +pkg_mavg_repo = https://github.com/EchoTeam/mavg +pkg_mavg_commit = master + +PACKAGES += mc_erl +pkg_mc_erl_name = mc_erl +pkg_mc_erl_description = mc-erl is a server for Minecraft 1.4.7 written in Erlang. +pkg_mc_erl_homepage = https://github.com/clonejo/mc-erl +pkg_mc_erl_fetch = git +pkg_mc_erl_repo = https://github.com/clonejo/mc-erl +pkg_mc_erl_commit = master + +PACKAGES += mcd +pkg_mcd_name = mcd +pkg_mcd_description = Fast memcached protocol client in pure Erlang +pkg_mcd_homepage = https://github.com/EchoTeam/mcd +pkg_mcd_fetch = git +pkg_mcd_repo = https://github.com/EchoTeam/mcd +pkg_mcd_commit = master + +PACKAGES += mcerlang +pkg_mcerlang_name = mcerlang +pkg_mcerlang_description = The McErlang model checker for Erlang +pkg_mcerlang_homepage = https://github.com/fredlund/McErlang +pkg_mcerlang_fetch = git +pkg_mcerlang_repo = https://github.com/fredlund/McErlang +pkg_mcerlang_commit = master + +PACKAGES += meck +pkg_meck_name = meck +pkg_meck_description = A mocking library for Erlang +pkg_meck_homepage = https://github.com/eproxus/meck +pkg_meck_fetch = git +pkg_meck_repo = https://github.com/eproxus/meck +pkg_meck_commit = master + +PACKAGES += mekao +pkg_mekao_name = mekao +pkg_mekao_description = SQL constructor +pkg_mekao_homepage = https://github.com/ddosia/mekao +pkg_mekao_fetch = git +pkg_mekao_repo = https://github.com/ddosia/mekao +pkg_mekao_commit = master + +PACKAGES += memo +pkg_memo_name = memo +pkg_memo_description = Erlang memoization server +pkg_memo_homepage = https://github.com/tuncer/memo +pkg_memo_fetch = git +pkg_memo_repo = https://github.com/tuncer/memo +pkg_memo_commit = master + +PACKAGES += merge_index +pkg_merge_index_name = merge_index +pkg_merge_index_description = MergeIndex is an Erlang library for storing ordered sets on disk. It is very similar to an SSTable (in Google's Bigtable) or an HFile (in Hadoop). +pkg_merge_index_homepage = https://github.com/basho/merge_index +pkg_merge_index_fetch = git +pkg_merge_index_repo = https://github.com/basho/merge_index +pkg_merge_index_commit = master + +PACKAGES += merl +pkg_merl_name = merl +pkg_merl_description = Metaprogramming in Erlang +pkg_merl_homepage = https://github.com/richcarl/merl +pkg_merl_fetch = git +pkg_merl_repo = https://github.com/richcarl/merl +pkg_merl_commit = master + +PACKAGES += mimerl +pkg_mimerl_name = mimerl +pkg_mimerl_description = library to handle mimetypes +pkg_mimerl_homepage = https://github.com/benoitc/mimerl +pkg_mimerl_fetch = git +pkg_mimerl_repo = https://github.com/benoitc/mimerl +pkg_mimerl_commit = master + +PACKAGES += mimetypes +pkg_mimetypes_name = mimetypes +pkg_mimetypes_description = Erlang MIME types library +pkg_mimetypes_homepage = https://github.com/spawngrid/mimetypes +pkg_mimetypes_fetch = git +pkg_mimetypes_repo = https://github.com/spawngrid/mimetypes +pkg_mimetypes_commit = master + +PACKAGES += mixer +pkg_mixer_name = mixer +pkg_mixer_description = Mix in functions from other modules +pkg_mixer_homepage = https://github.com/chef/mixer +pkg_mixer_fetch = git +pkg_mixer_repo = https://github.com/chef/mixer +pkg_mixer_commit = master + +PACKAGES += mochiweb +pkg_mochiweb_name = mochiweb +pkg_mochiweb_description = MochiWeb is an Erlang library for building lightweight HTTP servers. +pkg_mochiweb_homepage = https://github.com/mochi/mochiweb +pkg_mochiweb_fetch = git +pkg_mochiweb_repo = https://github.com/mochi/mochiweb +pkg_mochiweb_commit = master + +PACKAGES += mochiweb_xpath +pkg_mochiweb_xpath_name = mochiweb_xpath +pkg_mochiweb_xpath_description = XPath support for mochiweb's html parser +pkg_mochiweb_xpath_homepage = https://github.com/retnuh/mochiweb_xpath +pkg_mochiweb_xpath_fetch = git +pkg_mochiweb_xpath_repo = https://github.com/retnuh/mochiweb_xpath +pkg_mochiweb_xpath_commit = master + +PACKAGES += mockgyver +pkg_mockgyver_name = mockgyver +pkg_mockgyver_description = A mocking library for Erlang +pkg_mockgyver_homepage = https://github.com/klajo/mockgyver +pkg_mockgyver_fetch = git +pkg_mockgyver_repo = https://github.com/klajo/mockgyver +pkg_mockgyver_commit = master + +PACKAGES += modlib +pkg_modlib_name = modlib +pkg_modlib_description = Web framework based on Erlang's inets httpd +pkg_modlib_homepage = https://github.com/gar1t/modlib +pkg_modlib_fetch = git +pkg_modlib_repo = https://github.com/gar1t/modlib +pkg_modlib_commit = master + +PACKAGES += mongodb +pkg_mongodb_name = mongodb +pkg_mongodb_description = MongoDB driver for Erlang +pkg_mongodb_homepage = https://github.com/comtihon/mongodb-erlang +pkg_mongodb_fetch = git +pkg_mongodb_repo = https://github.com/comtihon/mongodb-erlang +pkg_mongodb_commit = master + +PACKAGES += mongooseim +pkg_mongooseim_name = mongooseim +pkg_mongooseim_description = Jabber / XMPP server with focus on performance and scalability, by Erlang Solutions +pkg_mongooseim_homepage = https://www.erlang-solutions.com/products/mongooseim-massively-scalable-ejabberd-platform +pkg_mongooseim_fetch = git +pkg_mongooseim_repo = https://github.com/esl/MongooseIM +pkg_mongooseim_commit = master + +PACKAGES += moyo +pkg_moyo_name = moyo +pkg_moyo_description = Erlang utility functions library +pkg_moyo_homepage = https://github.com/dwango/moyo +pkg_moyo_fetch = git +pkg_moyo_repo = https://github.com/dwango/moyo +pkg_moyo_commit = master + +PACKAGES += msgpack +pkg_msgpack_name = msgpack +pkg_msgpack_description = MessagePack (de)serializer implementation for Erlang +pkg_msgpack_homepage = https://github.com/msgpack/msgpack-erlang +pkg_msgpack_fetch = git +pkg_msgpack_repo = https://github.com/msgpack/msgpack-erlang +pkg_msgpack_commit = master + +PACKAGES += mu2 +pkg_mu2_name = mu2 +pkg_mu2_description = Erlang mutation testing tool +pkg_mu2_homepage = https://github.com/ramsay-t/mu2 +pkg_mu2_fetch = git +pkg_mu2_repo = https://github.com/ramsay-t/mu2 +pkg_mu2_commit = master + +PACKAGES += mustache +pkg_mustache_name = mustache +pkg_mustache_description = Mustache template engine for Erlang. +pkg_mustache_homepage = https://github.com/mojombo/mustache.erl +pkg_mustache_fetch = git +pkg_mustache_repo = https://github.com/mojombo/mustache.erl +pkg_mustache_commit = master + +PACKAGES += myproto +pkg_myproto_name = myproto +pkg_myproto_description = MySQL Server Protocol in Erlang +pkg_myproto_homepage = https://github.com/altenwald/myproto +pkg_myproto_fetch = git +pkg_myproto_repo = https://github.com/altenwald/myproto +pkg_myproto_commit = master + +PACKAGES += mysql +pkg_mysql_name = mysql +pkg_mysql_description = MySQL client library for Erlang/OTP +pkg_mysql_homepage = https://github.com/mysql-otp/mysql-otp +pkg_mysql_fetch = git +pkg_mysql_repo = https://github.com/mysql-otp/mysql-otp +pkg_mysql_commit = 1.5.1 + +PACKAGES += n2o +pkg_n2o_name = n2o +pkg_n2o_description = WebSocket Application Server +pkg_n2o_homepage = https://github.com/5HT/n2o +pkg_n2o_fetch = git +pkg_n2o_repo = https://github.com/5HT/n2o +pkg_n2o_commit = master + +PACKAGES += nat_upnp +pkg_nat_upnp_name = nat_upnp +pkg_nat_upnp_description = Erlang library to map your internal port to an external using UNP IGD +pkg_nat_upnp_homepage = https://github.com/benoitc/nat_upnp +pkg_nat_upnp_fetch = git +pkg_nat_upnp_repo = https://github.com/benoitc/nat_upnp +pkg_nat_upnp_commit = master + +PACKAGES += neo4j +pkg_neo4j_name = neo4j +pkg_neo4j_description = Erlang client library for Neo4J. +pkg_neo4j_homepage = https://github.com/dmitriid/neo4j-erlang +pkg_neo4j_fetch = git +pkg_neo4j_repo = https://github.com/dmitriid/neo4j-erlang +pkg_neo4j_commit = master + +PACKAGES += neotoma +pkg_neotoma_name = neotoma +pkg_neotoma_description = Erlang library and packrat parser-generator for parsing expression grammars. +pkg_neotoma_homepage = https://github.com/seancribbs/neotoma +pkg_neotoma_fetch = git +pkg_neotoma_repo = https://github.com/seancribbs/neotoma +pkg_neotoma_commit = master + +PACKAGES += newrelic +pkg_newrelic_name = newrelic +pkg_newrelic_description = Erlang library for sending metrics to New Relic +pkg_newrelic_homepage = https://github.com/wooga/newrelic-erlang +pkg_newrelic_fetch = git +pkg_newrelic_repo = https://github.com/wooga/newrelic-erlang +pkg_newrelic_commit = master + +PACKAGES += nifty +pkg_nifty_name = nifty +pkg_nifty_description = Erlang NIF wrapper generator +pkg_nifty_homepage = https://github.com/parapluu/nifty +pkg_nifty_fetch = git +pkg_nifty_repo = https://github.com/parapluu/nifty +pkg_nifty_commit = master + +PACKAGES += nitrogen_core +pkg_nitrogen_core_name = nitrogen_core +pkg_nitrogen_core_description = The core Nitrogen library. +pkg_nitrogen_core_homepage = http://nitrogenproject.com/ +pkg_nitrogen_core_fetch = git +pkg_nitrogen_core_repo = https://github.com/nitrogen/nitrogen_core +pkg_nitrogen_core_commit = master + +PACKAGES += nkbase +pkg_nkbase_name = nkbase +pkg_nkbase_description = NkBASE distributed database +pkg_nkbase_homepage = https://github.com/Nekso/nkbase +pkg_nkbase_fetch = git +pkg_nkbase_repo = https://github.com/Nekso/nkbase +pkg_nkbase_commit = develop + +PACKAGES += nkdocker +pkg_nkdocker_name = nkdocker +pkg_nkdocker_description = Erlang Docker client +pkg_nkdocker_homepage = https://github.com/Nekso/nkdocker +pkg_nkdocker_fetch = git +pkg_nkdocker_repo = https://github.com/Nekso/nkdocker +pkg_nkdocker_commit = master + +PACKAGES += nkpacket +pkg_nkpacket_name = nkpacket +pkg_nkpacket_description = Generic Erlang transport layer +pkg_nkpacket_homepage = https://github.com/Nekso/nkpacket +pkg_nkpacket_fetch = git +pkg_nkpacket_repo = https://github.com/Nekso/nkpacket +pkg_nkpacket_commit = master + +PACKAGES += nksip +pkg_nksip_name = nksip +pkg_nksip_description = Erlang SIP application server +pkg_nksip_homepage = https://github.com/kalta/nksip +pkg_nksip_fetch = git +pkg_nksip_repo = https://github.com/kalta/nksip +pkg_nksip_commit = master + +PACKAGES += nodefinder +pkg_nodefinder_name = nodefinder +pkg_nodefinder_description = automatic node discovery via UDP multicast +pkg_nodefinder_homepage = https://github.com/erlanger/nodefinder +pkg_nodefinder_fetch = git +pkg_nodefinder_repo = https://github.com/okeuday/nodefinder +pkg_nodefinder_commit = master + +PACKAGES += nprocreg +pkg_nprocreg_name = nprocreg +pkg_nprocreg_description = Minimal Distributed Erlang Process Registry +pkg_nprocreg_homepage = http://nitrogenproject.com/ +pkg_nprocreg_fetch = git +pkg_nprocreg_repo = https://github.com/nitrogen/nprocreg +pkg_nprocreg_commit = master + +PACKAGES += oauth +pkg_oauth_name = oauth +pkg_oauth_description = An Erlang OAuth 1.0 implementation +pkg_oauth_homepage = https://github.com/tim/erlang-oauth +pkg_oauth_fetch = git +pkg_oauth_repo = https://github.com/tim/erlang-oauth +pkg_oauth_commit = master + +PACKAGES += oauth2 +pkg_oauth2_name = oauth2 +pkg_oauth2_description = Erlang Oauth2 implementation +pkg_oauth2_homepage = https://github.com/kivra/oauth2 +pkg_oauth2_fetch = git +pkg_oauth2_repo = https://github.com/kivra/oauth2 +pkg_oauth2_commit = master + +PACKAGES += observer_cli +pkg_observer_cli_name = observer_cli +pkg_observer_cli_description = Visualize Erlang/Elixir Nodes On The Command Line +pkg_observer_cli_homepage = http://zhongwencool.github.io/observer_cli +pkg_observer_cli_fetch = git +pkg_observer_cli_repo = https://github.com/zhongwencool/observer_cli +pkg_observer_cli_commit = master + +PACKAGES += octopus +pkg_octopus_name = octopus +pkg_octopus_description = Small and flexible pool manager written in Erlang +pkg_octopus_homepage = https://github.com/erlangbureau/octopus +pkg_octopus_fetch = git +pkg_octopus_repo = https://github.com/erlangbureau/octopus +pkg_octopus_commit = master + +PACKAGES += of_protocol +pkg_of_protocol_name = of_protocol +pkg_of_protocol_description = OpenFlow Protocol Library for Erlang +pkg_of_protocol_homepage = https://github.com/FlowForwarding/of_protocol +pkg_of_protocol_fetch = git +pkg_of_protocol_repo = https://github.com/FlowForwarding/of_protocol +pkg_of_protocol_commit = master + +PACKAGES += opencouch +pkg_opencouch_name = couch +pkg_opencouch_description = A embeddable document oriented database compatible with Apache CouchDB +pkg_opencouch_homepage = https://github.com/benoitc/opencouch +pkg_opencouch_fetch = git +pkg_opencouch_repo = https://github.com/benoitc/opencouch +pkg_opencouch_commit = master + +PACKAGES += openflow +pkg_openflow_name = openflow +pkg_openflow_description = An OpenFlow controller written in pure erlang +pkg_openflow_homepage = https://github.com/renatoaguiar/erlang-openflow +pkg_openflow_fetch = git +pkg_openflow_repo = https://github.com/renatoaguiar/erlang-openflow +pkg_openflow_commit = master + +PACKAGES += openid +pkg_openid_name = openid +pkg_openid_description = Erlang OpenID +pkg_openid_homepage = https://github.com/brendonh/erl_openid +pkg_openid_fetch = git +pkg_openid_repo = https://github.com/brendonh/erl_openid +pkg_openid_commit = master + +PACKAGES += openpoker +pkg_openpoker_name = openpoker +pkg_openpoker_description = Genesis Texas hold'em Game Server +pkg_openpoker_homepage = https://github.com/hpyhacking/openpoker +pkg_openpoker_fetch = git +pkg_openpoker_repo = https://github.com/hpyhacking/openpoker +pkg_openpoker_commit = master + +PACKAGES += otpbp +pkg_otpbp_name = otpbp +pkg_otpbp_description = Parse transformer for use new OTP functions in old Erlang/OTP releases (R15, R16, 17, 18, 19) +pkg_otpbp_homepage = https://github.com/Ledest/otpbp +pkg_otpbp_fetch = git +pkg_otpbp_repo = https://github.com/Ledest/otpbp +pkg_otpbp_commit = master + +PACKAGES += pal +pkg_pal_name = pal +pkg_pal_description = Pragmatic Authentication Library +pkg_pal_homepage = https://github.com/manifest/pal +pkg_pal_fetch = git +pkg_pal_repo = https://github.com/manifest/pal +pkg_pal_commit = master + +PACKAGES += parse_trans +pkg_parse_trans_name = parse_trans +pkg_parse_trans_description = Parse transform utilities for Erlang +pkg_parse_trans_homepage = https://github.com/uwiger/parse_trans +pkg_parse_trans_fetch = git +pkg_parse_trans_repo = https://github.com/uwiger/parse_trans +pkg_parse_trans_commit = master + +PACKAGES += parsexml +pkg_parsexml_name = parsexml +pkg_parsexml_description = Simple DOM XML parser with convenient and very simple API +pkg_parsexml_homepage = https://github.com/maxlapshin/parsexml +pkg_parsexml_fetch = git +pkg_parsexml_repo = https://github.com/maxlapshin/parsexml +pkg_parsexml_commit = master + +PACKAGES += partisan +pkg_partisan_name = partisan +pkg_partisan_description = High-performance, high-scalability distributed computing with Erlang and Elixir. +pkg_partisan_homepage = http://partisan.cloud +pkg_partisan_fetch = git +pkg_partisan_repo = https://github.com/lasp-lang/partisan +pkg_partisan_commit = master + +PACKAGES += pegjs +pkg_pegjs_name = pegjs +pkg_pegjs_description = An implementation of PEG.js grammar for Erlang. +pkg_pegjs_homepage = https://github.com/dmitriid/pegjs +pkg_pegjs_fetch = git +pkg_pegjs_repo = https://github.com/dmitriid/pegjs +pkg_pegjs_commit = master + +PACKAGES += percept2 +pkg_percept2_name = percept2 +pkg_percept2_description = Concurrent profiling tool for Erlang +pkg_percept2_homepage = https://github.com/huiqing/percept2 +pkg_percept2_fetch = git +pkg_percept2_repo = https://github.com/huiqing/percept2 +pkg_percept2_commit = master + +PACKAGES += pgo +pkg_pgo_name = pgo +pkg_pgo_description = Erlang Postgres client and connection pool +pkg_pgo_homepage = https://github.com/erleans/pgo.git +pkg_pgo_fetch = git +pkg_pgo_repo = https://github.com/erleans/pgo.git +pkg_pgo_commit = master + +PACKAGES += pgsql +pkg_pgsql_name = pgsql +pkg_pgsql_description = Erlang PostgreSQL driver +pkg_pgsql_homepage = https://github.com/semiocast/pgsql +pkg_pgsql_fetch = git +pkg_pgsql_repo = https://github.com/semiocast/pgsql +pkg_pgsql_commit = master + +PACKAGES += pkgx +pkg_pkgx_name = pkgx +pkg_pkgx_description = Build .deb packages from Erlang releases +pkg_pkgx_homepage = https://github.com/arjan/pkgx +pkg_pkgx_fetch = git +pkg_pkgx_repo = https://github.com/arjan/pkgx +pkg_pkgx_commit = master + +PACKAGES += pkt +pkg_pkt_name = pkt +pkg_pkt_description = Erlang network protocol library +pkg_pkt_homepage = https://github.com/msantos/pkt +pkg_pkt_fetch = git +pkg_pkt_repo = https://github.com/msantos/pkt +pkg_pkt_commit = master + +PACKAGES += plain_fsm +pkg_plain_fsm_name = plain_fsm +pkg_plain_fsm_description = A behaviour/support library for writing plain Erlang FSMs. +pkg_plain_fsm_homepage = https://github.com/uwiger/plain_fsm +pkg_plain_fsm_fetch = git +pkg_plain_fsm_repo = https://github.com/uwiger/plain_fsm +pkg_plain_fsm_commit = master + +PACKAGES += plumtree +pkg_plumtree_name = plumtree +pkg_plumtree_description = Epidemic Broadcast Trees +pkg_plumtree_homepage = https://github.com/helium/plumtree +pkg_plumtree_fetch = git +pkg_plumtree_repo = https://github.com/helium/plumtree +pkg_plumtree_commit = master + +PACKAGES += pmod_transform +pkg_pmod_transform_name = pmod_transform +pkg_pmod_transform_description = Parse transform for parameterized modules +pkg_pmod_transform_homepage = https://github.com/erlang/pmod_transform +pkg_pmod_transform_fetch = git +pkg_pmod_transform_repo = https://github.com/erlang/pmod_transform +pkg_pmod_transform_commit = master + +PACKAGES += pobox +pkg_pobox_name = pobox +pkg_pobox_description = External buffer processes to protect against mailbox overflow in Erlang +pkg_pobox_homepage = https://github.com/ferd/pobox +pkg_pobox_fetch = git +pkg_pobox_repo = https://github.com/ferd/pobox +pkg_pobox_commit = master + +PACKAGES += ponos +pkg_ponos_name = ponos +pkg_ponos_description = ponos is a simple yet powerful load generator written in erlang +pkg_ponos_homepage = https://github.com/klarna/ponos +pkg_ponos_fetch = git +pkg_ponos_repo = https://github.com/klarna/ponos +pkg_ponos_commit = master + +PACKAGES += poolboy +pkg_poolboy_name = poolboy +pkg_poolboy_description = A hunky Erlang worker pool factory +pkg_poolboy_homepage = https://github.com/devinus/poolboy +pkg_poolboy_fetch = git +pkg_poolboy_repo = https://github.com/devinus/poolboy +pkg_poolboy_commit = master + +PACKAGES += pooler +pkg_pooler_name = pooler +pkg_pooler_description = An OTP Process Pool Application +pkg_pooler_homepage = https://github.com/seth/pooler +pkg_pooler_fetch = git +pkg_pooler_repo = https://github.com/seth/pooler +pkg_pooler_commit = master + +PACKAGES += pqueue +pkg_pqueue_name = pqueue +pkg_pqueue_description = Erlang Priority Queues +pkg_pqueue_homepage = https://github.com/okeuday/pqueue +pkg_pqueue_fetch = git +pkg_pqueue_repo = https://github.com/okeuday/pqueue +pkg_pqueue_commit = master + +PACKAGES += procket +pkg_procket_name = procket +pkg_procket_description = Erlang interface to low level socket operations +pkg_procket_homepage = http://blog.listincomprehension.com/search/label/procket +pkg_procket_fetch = git +pkg_procket_repo = https://github.com/msantos/procket +pkg_procket_commit = master + +PACKAGES += prometheus +pkg_prometheus_name = prometheus +pkg_prometheus_description = Prometheus.io client in Erlang +pkg_prometheus_homepage = https://github.com/deadtrickster/prometheus.erl +pkg_prometheus_fetch = git +pkg_prometheus_repo = https://github.com/deadtrickster/prometheus.erl +pkg_prometheus_commit = master + +PACKAGES += prop +pkg_prop_name = prop +pkg_prop_description = An Erlang code scaffolding and generator system. +pkg_prop_homepage = https://github.com/nuex/prop +pkg_prop_fetch = git +pkg_prop_repo = https://github.com/nuex/prop +pkg_prop_commit = master + +PACKAGES += proper +pkg_proper_name = proper +pkg_proper_description = PropEr: a QuickCheck-inspired property-based testing tool for Erlang. +pkg_proper_homepage = http://proper.softlab.ntua.gr +pkg_proper_fetch = git +pkg_proper_repo = https://github.com/manopapad/proper +pkg_proper_commit = master + +PACKAGES += props +pkg_props_name = props +pkg_props_description = Property structure library +pkg_props_homepage = https://github.com/greyarea/props +pkg_props_fetch = git +pkg_props_repo = https://github.com/greyarea/props +pkg_props_commit = master + +PACKAGES += protobuffs +pkg_protobuffs_name = protobuffs +pkg_protobuffs_description = An implementation of Google's Protocol Buffers for Erlang, based on ngerakines/erlang_protobuffs. +pkg_protobuffs_homepage = https://github.com/basho/erlang_protobuffs +pkg_protobuffs_fetch = git +pkg_protobuffs_repo = https://github.com/basho/erlang_protobuffs +pkg_protobuffs_commit = master + +PACKAGES += psycho +pkg_psycho_name = psycho +pkg_psycho_description = HTTP server that provides a WSGI-like interface for applications and middleware. +pkg_psycho_homepage = https://github.com/gar1t/psycho +pkg_psycho_fetch = git +pkg_psycho_repo = https://github.com/gar1t/psycho +pkg_psycho_commit = master + +PACKAGES += purity +pkg_purity_name = purity +pkg_purity_description = A side-effect analyzer for Erlang +pkg_purity_homepage = https://github.com/mpitid/purity +pkg_purity_fetch = git +pkg_purity_repo = https://github.com/mpitid/purity +pkg_purity_commit = master + +PACKAGES += push_service +pkg_push_service_name = push_service +pkg_push_service_description = Push service +pkg_push_service_homepage = https://github.com/hairyhum/push_service +pkg_push_service_fetch = git +pkg_push_service_repo = https://github.com/hairyhum/push_service +pkg_push_service_commit = master + +PACKAGES += qdate +pkg_qdate_name = qdate +pkg_qdate_description = Date, time, and timezone parsing, formatting, and conversion for Erlang. +pkg_qdate_homepage = https://github.com/choptastic/qdate +pkg_qdate_fetch = git +pkg_qdate_repo = https://github.com/choptastic/qdate +pkg_qdate_commit = master + +PACKAGES += qrcode +pkg_qrcode_name = qrcode +pkg_qrcode_description = QR Code encoder in Erlang +pkg_qrcode_homepage = https://github.com/komone/qrcode +pkg_qrcode_fetch = git +pkg_qrcode_repo = https://github.com/komone/qrcode +pkg_qrcode_commit = master + +PACKAGES += quest +pkg_quest_name = quest +pkg_quest_description = Learn Erlang through this set of challenges. An interactive system for getting to know Erlang. +pkg_quest_homepage = https://github.com/eriksoe/ErlangQuest +pkg_quest_fetch = git +pkg_quest_repo = https://github.com/eriksoe/ErlangQuest +pkg_quest_commit = master + +PACKAGES += quickrand +pkg_quickrand_name = quickrand +pkg_quickrand_description = Quick Erlang Random Number Generation +pkg_quickrand_homepage = https://github.com/okeuday/quickrand +pkg_quickrand_fetch = git +pkg_quickrand_repo = https://github.com/okeuday/quickrand +pkg_quickrand_commit = master + +PACKAGES += rabbit +pkg_rabbit_name = rabbit +pkg_rabbit_description = RabbitMQ Server +pkg_rabbit_homepage = https://www.rabbitmq.com/ +pkg_rabbit_fetch = git +pkg_rabbit_repo = https://github.com/rabbitmq/rabbitmq-server.git +pkg_rabbit_commit = master + +PACKAGES += rabbit_exchange_type_riak +pkg_rabbit_exchange_type_riak_name = rabbit_exchange_type_riak +pkg_rabbit_exchange_type_riak_description = Custom RabbitMQ exchange type for sticking messages in Riak +pkg_rabbit_exchange_type_riak_homepage = https://github.com/jbrisbin/riak-exchange +pkg_rabbit_exchange_type_riak_fetch = git +pkg_rabbit_exchange_type_riak_repo = https://github.com/jbrisbin/riak-exchange +pkg_rabbit_exchange_type_riak_commit = master + +PACKAGES += rack +pkg_rack_name = rack +pkg_rack_description = Rack handler for erlang +pkg_rack_homepage = https://github.com/erlyvideo/rack +pkg_rack_fetch = git +pkg_rack_repo = https://github.com/erlyvideo/rack +pkg_rack_commit = master + +PACKAGES += radierl +pkg_radierl_name = radierl +pkg_radierl_description = RADIUS protocol stack implemented in Erlang. +pkg_radierl_homepage = https://github.com/vances/radierl +pkg_radierl_fetch = git +pkg_radierl_repo = https://github.com/vances/radierl +pkg_radierl_commit = master + +PACKAGES += rafter +pkg_rafter_name = rafter +pkg_rafter_description = An Erlang library application which implements the Raft consensus protocol +pkg_rafter_homepage = https://github.com/andrewjstone/rafter +pkg_rafter_fetch = git +pkg_rafter_repo = https://github.com/andrewjstone/rafter +pkg_rafter_commit = master + +PACKAGES += ranch +pkg_ranch_name = ranch +pkg_ranch_description = Socket acceptor pool for TCP protocols. +pkg_ranch_homepage = http://ninenines.eu +pkg_ranch_fetch = git +pkg_ranch_repo = https://github.com/ninenines/ranch +pkg_ranch_commit = 1.2.1 + +PACKAGES += rbeacon +pkg_rbeacon_name = rbeacon +pkg_rbeacon_description = LAN discovery and presence in Erlang. +pkg_rbeacon_homepage = https://github.com/refuge/rbeacon +pkg_rbeacon_fetch = git +pkg_rbeacon_repo = https://github.com/refuge/rbeacon +pkg_rbeacon_commit = master + +PACKAGES += rebar +pkg_rebar_name = rebar +pkg_rebar_description = Erlang build tool that makes it easy to compile and test Erlang applications, port drivers and releases. +pkg_rebar_homepage = http://www.rebar3.org +pkg_rebar_fetch = git +pkg_rebar_repo = https://github.com/rebar/rebar3 +pkg_rebar_commit = master + +PACKAGES += rebus +pkg_rebus_name = rebus +pkg_rebus_description = A stupid simple, internal, pub/sub event bus written in- and for Erlang. +pkg_rebus_homepage = https://github.com/olle/rebus +pkg_rebus_fetch = git +pkg_rebus_repo = https://github.com/olle/rebus +pkg_rebus_commit = master + +PACKAGES += rec2json +pkg_rec2json_name = rec2json +pkg_rec2json_description = Compile erlang record definitions into modules to convert them to/from json easily. +pkg_rec2json_homepage = https://github.com/lordnull/rec2json +pkg_rec2json_fetch = git +pkg_rec2json_repo = https://github.com/lordnull/rec2json +pkg_rec2json_commit = master + +PACKAGES += recon +pkg_recon_name = recon +pkg_recon_description = Collection of functions and scripts to debug Erlang in production. +pkg_recon_homepage = https://github.com/ferd/recon +pkg_recon_fetch = git +pkg_recon_repo = https://github.com/ferd/recon +pkg_recon_commit = master + +PACKAGES += record_info +pkg_record_info_name = record_info +pkg_record_info_description = Convert between record and proplist +pkg_record_info_homepage = https://github.com/bipthelin/erlang-record_info +pkg_record_info_fetch = git +pkg_record_info_repo = https://github.com/bipthelin/erlang-record_info +pkg_record_info_commit = master + +PACKAGES += redgrid +pkg_redgrid_name = redgrid +pkg_redgrid_description = automatic Erlang node discovery via redis +pkg_redgrid_homepage = https://github.com/jkvor/redgrid +pkg_redgrid_fetch = git +pkg_redgrid_repo = https://github.com/jkvor/redgrid +pkg_redgrid_commit = master + +PACKAGES += redo +pkg_redo_name = redo +pkg_redo_description = pipelined erlang redis client +pkg_redo_homepage = https://github.com/jkvor/redo +pkg_redo_fetch = git +pkg_redo_repo = https://github.com/jkvor/redo +pkg_redo_commit = master + +PACKAGES += reload_mk +pkg_reload_mk_name = reload_mk +pkg_reload_mk_description = Live reload plugin for erlang.mk. +pkg_reload_mk_homepage = https://github.com/bullno1/reload.mk +pkg_reload_mk_fetch = git +pkg_reload_mk_repo = https://github.com/bullno1/reload.mk +pkg_reload_mk_commit = master + +PACKAGES += reltool_util +pkg_reltool_util_name = reltool_util +pkg_reltool_util_description = Erlang reltool utility functionality application +pkg_reltool_util_homepage = https://github.com/okeuday/reltool_util +pkg_reltool_util_fetch = git +pkg_reltool_util_repo = https://github.com/okeuday/reltool_util +pkg_reltool_util_commit = master + +PACKAGES += relx +pkg_relx_name = relx +pkg_relx_description = Sane, simple release creation for Erlang +pkg_relx_homepage = https://github.com/erlware/relx +pkg_relx_fetch = git +pkg_relx_repo = https://github.com/erlware/relx +pkg_relx_commit = master + +PACKAGES += resource_discovery +pkg_resource_discovery_name = resource_discovery +pkg_resource_discovery_description = An application used to dynamically discover resources present in an Erlang node cluster. +pkg_resource_discovery_homepage = http://erlware.org/ +pkg_resource_discovery_fetch = git +pkg_resource_discovery_repo = https://github.com/erlware/resource_discovery +pkg_resource_discovery_commit = master + +PACKAGES += restc +pkg_restc_name = restc +pkg_restc_description = Erlang Rest Client +pkg_restc_homepage = https://github.com/kivra/restclient +pkg_restc_fetch = git +pkg_restc_repo = https://github.com/kivra/restclient +pkg_restc_commit = master + +PACKAGES += rfc4627_jsonrpc +pkg_rfc4627_jsonrpc_name = rfc4627_jsonrpc +pkg_rfc4627_jsonrpc_description = Erlang RFC4627 (JSON) codec and JSON-RPC server implementation. +pkg_rfc4627_jsonrpc_homepage = https://github.com/tonyg/erlang-rfc4627 +pkg_rfc4627_jsonrpc_fetch = git +pkg_rfc4627_jsonrpc_repo = https://github.com/tonyg/erlang-rfc4627 +pkg_rfc4627_jsonrpc_commit = master + +PACKAGES += riak_control +pkg_riak_control_name = riak_control +pkg_riak_control_description = Webmachine-based administration interface for Riak. +pkg_riak_control_homepage = https://github.com/basho/riak_control +pkg_riak_control_fetch = git +pkg_riak_control_repo = https://github.com/basho/riak_control +pkg_riak_control_commit = master + +PACKAGES += riak_core +pkg_riak_core_name = riak_core +pkg_riak_core_description = Distributed systems infrastructure used by Riak. +pkg_riak_core_homepage = https://github.com/basho/riak_core +pkg_riak_core_fetch = git +pkg_riak_core_repo = https://github.com/basho/riak_core +pkg_riak_core_commit = master + +PACKAGES += riak_dt +pkg_riak_dt_name = riak_dt +pkg_riak_dt_description = Convergent replicated datatypes in Erlang +pkg_riak_dt_homepage = https://github.com/basho/riak_dt +pkg_riak_dt_fetch = git +pkg_riak_dt_repo = https://github.com/basho/riak_dt +pkg_riak_dt_commit = master + +PACKAGES += riak_ensemble +pkg_riak_ensemble_name = riak_ensemble +pkg_riak_ensemble_description = Multi-Paxos framework in Erlang +pkg_riak_ensemble_homepage = https://github.com/basho/riak_ensemble +pkg_riak_ensemble_fetch = git +pkg_riak_ensemble_repo = https://github.com/basho/riak_ensemble +pkg_riak_ensemble_commit = master + +PACKAGES += riak_kv +pkg_riak_kv_name = riak_kv +pkg_riak_kv_description = Riak Key/Value Store +pkg_riak_kv_homepage = https://github.com/basho/riak_kv +pkg_riak_kv_fetch = git +pkg_riak_kv_repo = https://github.com/basho/riak_kv +pkg_riak_kv_commit = master + +PACKAGES += riak_pg +pkg_riak_pg_name = riak_pg +pkg_riak_pg_description = Distributed process groups with riak_core. +pkg_riak_pg_homepage = https://github.com/cmeiklejohn/riak_pg +pkg_riak_pg_fetch = git +pkg_riak_pg_repo = https://github.com/cmeiklejohn/riak_pg +pkg_riak_pg_commit = master + +PACKAGES += riak_pipe +pkg_riak_pipe_name = riak_pipe +pkg_riak_pipe_description = Riak Pipelines +pkg_riak_pipe_homepage = https://github.com/basho/riak_pipe +pkg_riak_pipe_fetch = git +pkg_riak_pipe_repo = https://github.com/basho/riak_pipe +pkg_riak_pipe_commit = master + +PACKAGES += riak_sysmon +pkg_riak_sysmon_name = riak_sysmon +pkg_riak_sysmon_description = Simple OTP app for managing Erlang VM system_monitor event messages +pkg_riak_sysmon_homepage = https://github.com/basho/riak_sysmon +pkg_riak_sysmon_fetch = git +pkg_riak_sysmon_repo = https://github.com/basho/riak_sysmon +pkg_riak_sysmon_commit = master + +PACKAGES += riak_test +pkg_riak_test_name = riak_test +pkg_riak_test_description = I'm in your cluster, testing your riaks +pkg_riak_test_homepage = https://github.com/basho/riak_test +pkg_riak_test_fetch = git +pkg_riak_test_repo = https://github.com/basho/riak_test +pkg_riak_test_commit = master + +PACKAGES += riakc +pkg_riakc_name = riakc +pkg_riakc_description = Erlang clients for Riak. +pkg_riakc_homepage = https://github.com/basho/riak-erlang-client +pkg_riakc_fetch = git +pkg_riakc_repo = https://github.com/basho/riak-erlang-client +pkg_riakc_commit = master + +PACKAGES += riakhttpc +pkg_riakhttpc_name = riakhttpc +pkg_riakhttpc_description = Riak Erlang client using the HTTP interface +pkg_riakhttpc_homepage = https://github.com/basho/riak-erlang-http-client +pkg_riakhttpc_fetch = git +pkg_riakhttpc_repo = https://github.com/basho/riak-erlang-http-client +pkg_riakhttpc_commit = master + +PACKAGES += riaknostic +pkg_riaknostic_name = riaknostic +pkg_riaknostic_description = A diagnostic tool for Riak installations, to find common errors asap +pkg_riaknostic_homepage = https://github.com/basho/riaknostic +pkg_riaknostic_fetch = git +pkg_riaknostic_repo = https://github.com/basho/riaknostic +pkg_riaknostic_commit = master + +PACKAGES += riakpool +pkg_riakpool_name = riakpool +pkg_riakpool_description = erlang riak client pool +pkg_riakpool_homepage = https://github.com/dweldon/riakpool +pkg_riakpool_fetch = git +pkg_riakpool_repo = https://github.com/dweldon/riakpool +pkg_riakpool_commit = master + +PACKAGES += rivus_cep +pkg_rivus_cep_name = rivus_cep +pkg_rivus_cep_description = Complex event processing in Erlang +pkg_rivus_cep_homepage = https://github.com/vascokk/rivus_cep +pkg_rivus_cep_fetch = git +pkg_rivus_cep_repo = https://github.com/vascokk/rivus_cep +pkg_rivus_cep_commit = master + +PACKAGES += rlimit +pkg_rlimit_name = rlimit +pkg_rlimit_description = Magnus Klaar's rate limiter code from etorrent +pkg_rlimit_homepage = https://github.com/jlouis/rlimit +pkg_rlimit_fetch = git +pkg_rlimit_repo = https://github.com/jlouis/rlimit +pkg_rlimit_commit = master + +PACKAGES += rust_mk +pkg_rust_mk_name = rust_mk +pkg_rust_mk_description = Build Rust crates in an Erlang application +pkg_rust_mk_homepage = https://github.com/goertzenator/rust.mk +pkg_rust_mk_fetch = git +pkg_rust_mk_repo = https://github.com/goertzenator/rust.mk +pkg_rust_mk_commit = master + +PACKAGES += safetyvalve +pkg_safetyvalve_name = safetyvalve +pkg_safetyvalve_description = A safety valve for your erlang node +pkg_safetyvalve_homepage = https://github.com/jlouis/safetyvalve +pkg_safetyvalve_fetch = git +pkg_safetyvalve_repo = https://github.com/jlouis/safetyvalve +pkg_safetyvalve_commit = master + +PACKAGES += seestar +pkg_seestar_name = seestar +pkg_seestar_description = The Erlang client for Cassandra 1.2+ binary protocol +pkg_seestar_homepage = https://github.com/iamaleksey/seestar +pkg_seestar_fetch = git +pkg_seestar_repo = https://github.com/iamaleksey/seestar +pkg_seestar_commit = master + +PACKAGES += service +pkg_service_name = service +pkg_service_description = A minimal Erlang behavior for creating CloudI internal services +pkg_service_homepage = http://cloudi.org/ +pkg_service_fetch = git +pkg_service_repo = https://github.com/CloudI/service +pkg_service_commit = master + +PACKAGES += setup +pkg_setup_name = setup +pkg_setup_description = Generic setup utility for Erlang-based systems +pkg_setup_homepage = https://github.com/uwiger/setup +pkg_setup_fetch = git +pkg_setup_repo = https://github.com/uwiger/setup +pkg_setup_commit = master + +PACKAGES += sext +pkg_sext_name = sext +pkg_sext_description = Sortable Erlang Term Serialization +pkg_sext_homepage = https://github.com/uwiger/sext +pkg_sext_fetch = git +pkg_sext_repo = https://github.com/uwiger/sext +pkg_sext_commit = master + +PACKAGES += sfmt +pkg_sfmt_name = sfmt +pkg_sfmt_description = SFMT pseudo random number generator for Erlang. +pkg_sfmt_homepage = https://github.com/jj1bdx/sfmt-erlang +pkg_sfmt_fetch = git +pkg_sfmt_repo = https://github.com/jj1bdx/sfmt-erlang +pkg_sfmt_commit = master + +PACKAGES += sgte +pkg_sgte_name = sgte +pkg_sgte_description = A simple Erlang Template Engine +pkg_sgte_homepage = https://github.com/filippo/sgte +pkg_sgte_fetch = git +pkg_sgte_repo = https://github.com/filippo/sgte +pkg_sgte_commit = master + +PACKAGES += sheriff +pkg_sheriff_name = sheriff +pkg_sheriff_description = Parse transform for type based validation. +pkg_sheriff_homepage = http://ninenines.eu +pkg_sheriff_fetch = git +pkg_sheriff_repo = https://github.com/extend/sheriff +pkg_sheriff_commit = master + +PACKAGES += shotgun +pkg_shotgun_name = shotgun +pkg_shotgun_description = better than just a gun +pkg_shotgun_homepage = https://github.com/inaka/shotgun +pkg_shotgun_fetch = git +pkg_shotgun_repo = https://github.com/inaka/shotgun +pkg_shotgun_commit = master + +PACKAGES += sidejob +pkg_sidejob_name = sidejob +pkg_sidejob_description = Parallel worker and capacity limiting library for Erlang +pkg_sidejob_homepage = https://github.com/basho/sidejob +pkg_sidejob_fetch = git +pkg_sidejob_repo = https://github.com/basho/sidejob +pkg_sidejob_commit = master + +PACKAGES += sieve +pkg_sieve_name = sieve +pkg_sieve_description = sieve is a simple TCP routing proxy (layer 7) in erlang +pkg_sieve_homepage = https://github.com/benoitc/sieve +pkg_sieve_fetch = git +pkg_sieve_repo = https://github.com/benoitc/sieve +pkg_sieve_commit = master + +PACKAGES += sighandler +pkg_sighandler_name = sighandler +pkg_sighandler_description = Handle UNIX signals in Er lang +pkg_sighandler_homepage = https://github.com/jkingsbery/sighandler +pkg_sighandler_fetch = git +pkg_sighandler_repo = https://github.com/jkingsbery/sighandler +pkg_sighandler_commit = master + +PACKAGES += simhash +pkg_simhash_name = simhash +pkg_simhash_description = Simhashing for Erlang -- hashing algorithm to find near-duplicates in binary data. +pkg_simhash_homepage = https://github.com/ferd/simhash +pkg_simhash_fetch = git +pkg_simhash_repo = https://github.com/ferd/simhash +pkg_simhash_commit = master + +PACKAGES += simple_bridge +pkg_simple_bridge_name = simple_bridge +pkg_simple_bridge_description = A simple, standardized interface library to Erlang HTTP Servers. +pkg_simple_bridge_homepage = https://github.com/nitrogen/simple_bridge +pkg_simple_bridge_fetch = git +pkg_simple_bridge_repo = https://github.com/nitrogen/simple_bridge +pkg_simple_bridge_commit = master + +PACKAGES += simple_oauth2 +pkg_simple_oauth2_name = simple_oauth2 +pkg_simple_oauth2_description = Simple erlang OAuth2 client module for any http server framework (Google, Facebook, Yandex, Vkontakte are preconfigured) +pkg_simple_oauth2_homepage = https://github.com/virtan/simple_oauth2 +pkg_simple_oauth2_fetch = git +pkg_simple_oauth2_repo = https://github.com/virtan/simple_oauth2 +pkg_simple_oauth2_commit = master + +PACKAGES += skel +pkg_skel_name = skel +pkg_skel_description = A Streaming Process-based Skeleton Library for Erlang +pkg_skel_homepage = https://github.com/ParaPhrase/skel +pkg_skel_fetch = git +pkg_skel_repo = https://github.com/ParaPhrase/skel +pkg_skel_commit = master + +PACKAGES += slack +pkg_slack_name = slack +pkg_slack_description = Minimal slack notification OTP library. +pkg_slack_homepage = https://github.com/DonBranson/slack +pkg_slack_fetch = git +pkg_slack_repo = https://github.com/DonBranson/slack.git +pkg_slack_commit = master + +PACKAGES += smother +pkg_smother_name = smother +pkg_smother_description = Extended code coverage metrics for Erlang. +pkg_smother_homepage = https://ramsay-t.github.io/Smother/ +pkg_smother_fetch = git +pkg_smother_repo = https://github.com/ramsay-t/Smother +pkg_smother_commit = master + +PACKAGES += snappyer +pkg_snappyer_name = snappyer +pkg_snappyer_description = Snappy as nif for Erlang +pkg_snappyer_homepage = https://github.com/zmstone/snappyer +pkg_snappyer_fetch = git +pkg_snappyer_repo = https://github.com/zmstone/snappyer.git +pkg_snappyer_commit = master + +PACKAGES += social +pkg_social_name = social +pkg_social_description = Cowboy handler for social login via OAuth2 providers +pkg_social_homepage = https://github.com/dvv/social +pkg_social_fetch = git +pkg_social_repo = https://github.com/dvv/social +pkg_social_commit = master + +PACKAGES += spapi_router +pkg_spapi_router_name = spapi_router +pkg_spapi_router_description = Partially-connected Erlang clustering +pkg_spapi_router_homepage = https://github.com/spilgames/spapi-router +pkg_spapi_router_fetch = git +pkg_spapi_router_repo = https://github.com/spilgames/spapi-router +pkg_spapi_router_commit = master + +PACKAGES += sqerl +pkg_sqerl_name = sqerl +pkg_sqerl_description = An Erlang-flavoured SQL DSL +pkg_sqerl_homepage = https://github.com/hairyhum/sqerl +pkg_sqerl_fetch = git +pkg_sqerl_repo = https://github.com/hairyhum/sqerl +pkg_sqerl_commit = master + +PACKAGES += srly +pkg_srly_name = srly +pkg_srly_description = Native Erlang Unix serial interface +pkg_srly_homepage = https://github.com/msantos/srly +pkg_srly_fetch = git +pkg_srly_repo = https://github.com/msantos/srly +pkg_srly_commit = master + +PACKAGES += sshrpc +pkg_sshrpc_name = sshrpc +pkg_sshrpc_description = Erlang SSH RPC module (experimental) +pkg_sshrpc_homepage = https://github.com/jj1bdx/sshrpc +pkg_sshrpc_fetch = git +pkg_sshrpc_repo = https://github.com/jj1bdx/sshrpc +pkg_sshrpc_commit = master + +PACKAGES += stable +pkg_stable_name = stable +pkg_stable_description = Library of assorted helpers for Cowboy web server. +pkg_stable_homepage = https://github.com/dvv/stable +pkg_stable_fetch = git +pkg_stable_repo = https://github.com/dvv/stable +pkg_stable_commit = master + +PACKAGES += statebox +pkg_statebox_name = statebox +pkg_statebox_description = Erlang state monad with merge/conflict-resolution capabilities. Useful for Riak. +pkg_statebox_homepage = https://github.com/mochi/statebox +pkg_statebox_fetch = git +pkg_statebox_repo = https://github.com/mochi/statebox +pkg_statebox_commit = master + +PACKAGES += statebox_riak +pkg_statebox_riak_name = statebox_riak +pkg_statebox_riak_description = Convenience library that makes it easier to use statebox with riak, extracted from best practices in our production code at Mochi Media. +pkg_statebox_riak_homepage = https://github.com/mochi/statebox_riak +pkg_statebox_riak_fetch = git +pkg_statebox_riak_repo = https://github.com/mochi/statebox_riak +pkg_statebox_riak_commit = master + +PACKAGES += statman +pkg_statman_name = statman +pkg_statman_description = Efficiently collect massive volumes of metrics inside the Erlang VM +pkg_statman_homepage = https://github.com/knutin/statman +pkg_statman_fetch = git +pkg_statman_repo = https://github.com/knutin/statman +pkg_statman_commit = master + +PACKAGES += statsderl +pkg_statsderl_name = statsderl +pkg_statsderl_description = StatsD client (erlang) +pkg_statsderl_homepage = https://github.com/lpgauth/statsderl +pkg_statsderl_fetch = git +pkg_statsderl_repo = https://github.com/lpgauth/statsderl +pkg_statsderl_commit = master + +PACKAGES += stdinout_pool +pkg_stdinout_pool_name = stdinout_pool +pkg_stdinout_pool_description = stdinout_pool : stuff goes in, stuff goes out. there's never any miscommunication. +pkg_stdinout_pool_homepage = https://github.com/mattsta/erlang-stdinout-pool +pkg_stdinout_pool_fetch = git +pkg_stdinout_pool_repo = https://github.com/mattsta/erlang-stdinout-pool +pkg_stdinout_pool_commit = master + +PACKAGES += stockdb +pkg_stockdb_name = stockdb +pkg_stockdb_description = Database for storing Stock Exchange quotes in erlang +pkg_stockdb_homepage = https://github.com/maxlapshin/stockdb +pkg_stockdb_fetch = git +pkg_stockdb_repo = https://github.com/maxlapshin/stockdb +pkg_stockdb_commit = master + +PACKAGES += stripe +pkg_stripe_name = stripe +pkg_stripe_description = Erlang interface to the stripe.com API +pkg_stripe_homepage = https://github.com/mattsta/stripe-erlang +pkg_stripe_fetch = git +pkg_stripe_repo = https://github.com/mattsta/stripe-erlang +pkg_stripe_commit = v1 + +PACKAGES += subproc +pkg_subproc_name = subproc +pkg_subproc_description = unix subprocess manager with {active,once|false} modes +pkg_subproc_homepage = http://dozzie.jarowit.net/trac/wiki/subproc +pkg_subproc_fetch = git +pkg_subproc_repo = https://github.com/dozzie/subproc +pkg_subproc_commit = v0.1.0 + +PACKAGES += supervisor3 +pkg_supervisor3_name = supervisor3 +pkg_supervisor3_description = OTP supervisor with additional strategies +pkg_supervisor3_homepage = https://github.com/klarna/supervisor3 +pkg_supervisor3_fetch = git +pkg_supervisor3_repo = https://github.com/klarna/supervisor3.git +pkg_supervisor3_commit = master + +PACKAGES += surrogate +pkg_surrogate_name = surrogate +pkg_surrogate_description = Proxy server written in erlang. Supports reverse proxy load balancing and forward proxy with http (including CONNECT), socks4, socks5, and transparent proxy modes. +pkg_surrogate_homepage = https://github.com/skruger/Surrogate +pkg_surrogate_fetch = git +pkg_surrogate_repo = https://github.com/skruger/Surrogate +pkg_surrogate_commit = master + +PACKAGES += swab +pkg_swab_name = swab +pkg_swab_description = General purpose buffer handling module +pkg_swab_homepage = https://github.com/crownedgrouse/swab +pkg_swab_fetch = git +pkg_swab_repo = https://github.com/crownedgrouse/swab +pkg_swab_commit = master + +PACKAGES += swarm +pkg_swarm_name = swarm +pkg_swarm_description = Fast and simple acceptor pool for Erlang +pkg_swarm_homepage = https://github.com/jeremey/swarm +pkg_swarm_fetch = git +pkg_swarm_repo = https://github.com/jeremey/swarm +pkg_swarm_commit = master + +PACKAGES += switchboard +pkg_switchboard_name = switchboard +pkg_switchboard_description = A framework for processing email using worker plugins. +pkg_switchboard_homepage = https://github.com/thusfresh/switchboard +pkg_switchboard_fetch = git +pkg_switchboard_repo = https://github.com/thusfresh/switchboard +pkg_switchboard_commit = master + +PACKAGES += syn +pkg_syn_name = syn +pkg_syn_description = A global Process Registry and Process Group manager for Erlang. +pkg_syn_homepage = https://github.com/ostinelli/syn +pkg_syn_fetch = git +pkg_syn_repo = https://github.com/ostinelli/syn +pkg_syn_commit = master + +PACKAGES += sync +pkg_sync_name = sync +pkg_sync_description = On-the-fly recompiling and reloading in Erlang. +pkg_sync_homepage = https://github.com/rustyio/sync +pkg_sync_fetch = git +pkg_sync_repo = https://github.com/rustyio/sync +pkg_sync_commit = master + +PACKAGES += syntaxerl +pkg_syntaxerl_name = syntaxerl +pkg_syntaxerl_description = Syntax checker for Erlang +pkg_syntaxerl_homepage = https://github.com/ten0s/syntaxerl +pkg_syntaxerl_fetch = git +pkg_syntaxerl_repo = https://github.com/ten0s/syntaxerl +pkg_syntaxerl_commit = master + +PACKAGES += syslog +pkg_syslog_name = syslog +pkg_syslog_description = Erlang port driver for interacting with syslog via syslog(3) +pkg_syslog_homepage = https://github.com/Vagabond/erlang-syslog +pkg_syslog_fetch = git +pkg_syslog_repo = https://github.com/Vagabond/erlang-syslog +pkg_syslog_commit = master + +PACKAGES += taskforce +pkg_taskforce_name = taskforce +pkg_taskforce_description = Erlang worker pools for controlled parallelisation of arbitrary tasks. +pkg_taskforce_homepage = https://github.com/g-andrade/taskforce +pkg_taskforce_fetch = git +pkg_taskforce_repo = https://github.com/g-andrade/taskforce +pkg_taskforce_commit = master + +PACKAGES += tddreloader +pkg_tddreloader_name = tddreloader +pkg_tddreloader_description = Shell utility for recompiling, reloading, and testing code as it changes +pkg_tddreloader_homepage = https://github.com/version2beta/tddreloader +pkg_tddreloader_fetch = git +pkg_tddreloader_repo = https://github.com/version2beta/tddreloader +pkg_tddreloader_commit = master + +PACKAGES += tempo +pkg_tempo_name = tempo +pkg_tempo_description = NIF-based date and time parsing and formatting for Erlang. +pkg_tempo_homepage = https://github.com/selectel/tempo +pkg_tempo_fetch = git +pkg_tempo_repo = https://github.com/selectel/tempo +pkg_tempo_commit = master + +PACKAGES += ticktick +pkg_ticktick_name = ticktick +pkg_ticktick_description = Ticktick is an id generator for message service. +pkg_ticktick_homepage = https://github.com/ericliang/ticktick +pkg_ticktick_fetch = git +pkg_ticktick_repo = https://github.com/ericliang/ticktick +pkg_ticktick_commit = master + +PACKAGES += tinymq +pkg_tinymq_name = tinymq +pkg_tinymq_description = TinyMQ - a diminutive, in-memory message queue +pkg_tinymq_homepage = https://github.com/ChicagoBoss/tinymq +pkg_tinymq_fetch = git +pkg_tinymq_repo = https://github.com/ChicagoBoss/tinymq +pkg_tinymq_commit = master + +PACKAGES += tinymt +pkg_tinymt_name = tinymt +pkg_tinymt_description = TinyMT pseudo random number generator for Erlang. +pkg_tinymt_homepage = https://github.com/jj1bdx/tinymt-erlang +pkg_tinymt_fetch = git +pkg_tinymt_repo = https://github.com/jj1bdx/tinymt-erlang +pkg_tinymt_commit = master + +PACKAGES += tirerl +pkg_tirerl_name = tirerl +pkg_tirerl_description = Erlang interface to Elastic Search +pkg_tirerl_homepage = https://github.com/inaka/tirerl +pkg_tirerl_fetch = git +pkg_tirerl_repo = https://github.com/inaka/tirerl +pkg_tirerl_commit = master + +PACKAGES += toml +pkg_toml_name = toml +pkg_toml_description = TOML (0.4.0) config parser +pkg_toml_homepage = http://dozzie.jarowit.net/trac/wiki/TOML +pkg_toml_fetch = git +pkg_toml_repo = https://github.com/dozzie/toml +pkg_toml_commit = v0.2.0 + +PACKAGES += traffic_tools +pkg_traffic_tools_name = traffic_tools +pkg_traffic_tools_description = Simple traffic limiting library +pkg_traffic_tools_homepage = https://github.com/systra/traffic_tools +pkg_traffic_tools_fetch = git +pkg_traffic_tools_repo = https://github.com/systra/traffic_tools +pkg_traffic_tools_commit = master + +PACKAGES += trails +pkg_trails_name = trails +pkg_trails_description = A couple of improvements over Cowboy Routes +pkg_trails_homepage = http://inaka.github.io/cowboy-trails/ +pkg_trails_fetch = git +pkg_trails_repo = https://github.com/inaka/cowboy-trails +pkg_trails_commit = master + +PACKAGES += trane +pkg_trane_name = trane +pkg_trane_description = SAX style broken HTML parser in Erlang +pkg_trane_homepage = https://github.com/massemanet/trane +pkg_trane_fetch = git +pkg_trane_repo = https://github.com/massemanet/trane +pkg_trane_commit = master + +PACKAGES += transit +pkg_transit_name = transit +pkg_transit_description = transit format for erlang +pkg_transit_homepage = https://github.com/isaiah/transit-erlang +pkg_transit_fetch = git +pkg_transit_repo = https://github.com/isaiah/transit-erlang +pkg_transit_commit = master + +PACKAGES += trie +pkg_trie_name = trie +pkg_trie_description = Erlang Trie Implementation +pkg_trie_homepage = https://github.com/okeuday/trie +pkg_trie_fetch = git +pkg_trie_repo = https://github.com/okeuday/trie +pkg_trie_commit = master + +PACKAGES += triq +pkg_triq_name = triq +pkg_triq_description = Trifork QuickCheck +pkg_triq_homepage = https://triq.gitlab.io +pkg_triq_fetch = git +pkg_triq_repo = https://gitlab.com/triq/triq.git +pkg_triq_commit = master + +PACKAGES += tunctl +pkg_tunctl_name = tunctl +pkg_tunctl_description = Erlang TUN/TAP interface +pkg_tunctl_homepage = https://github.com/msantos/tunctl +pkg_tunctl_fetch = git +pkg_tunctl_repo = https://github.com/msantos/tunctl +pkg_tunctl_commit = master + +PACKAGES += twerl +pkg_twerl_name = twerl +pkg_twerl_description = Erlang client for the Twitter Streaming API +pkg_twerl_homepage = https://github.com/lucaspiller/twerl +pkg_twerl_fetch = git +pkg_twerl_repo = https://github.com/lucaspiller/twerl +pkg_twerl_commit = oauth + +PACKAGES += twitter_erlang +pkg_twitter_erlang_name = twitter_erlang +pkg_twitter_erlang_description = An Erlang twitter client +pkg_twitter_erlang_homepage = https://github.com/ngerakines/erlang_twitter +pkg_twitter_erlang_fetch = git +pkg_twitter_erlang_repo = https://github.com/ngerakines/erlang_twitter +pkg_twitter_erlang_commit = master + +PACKAGES += ucol_nif +pkg_ucol_nif_name = ucol_nif +pkg_ucol_nif_description = ICU based collation Erlang module +pkg_ucol_nif_homepage = https://github.com/refuge/ucol_nif +pkg_ucol_nif_fetch = git +pkg_ucol_nif_repo = https://github.com/refuge/ucol_nif +pkg_ucol_nif_commit = master + +PACKAGES += unicorn +pkg_unicorn_name = unicorn +pkg_unicorn_description = Generic configuration server +pkg_unicorn_homepage = https://github.com/shizzard/unicorn +pkg_unicorn_fetch = git +pkg_unicorn_repo = https://github.com/shizzard/unicorn +pkg_unicorn_commit = master + +PACKAGES += unsplit +pkg_unsplit_name = unsplit +pkg_unsplit_description = Resolves conflicts in Mnesia after network splits +pkg_unsplit_homepage = https://github.com/uwiger/unsplit +pkg_unsplit_fetch = git +pkg_unsplit_repo = https://github.com/uwiger/unsplit +pkg_unsplit_commit = master + +PACKAGES += uuid +pkg_uuid_name = uuid +pkg_uuid_description = Erlang UUID Implementation +pkg_uuid_homepage = https://github.com/okeuday/uuid +pkg_uuid_fetch = git +pkg_uuid_repo = https://github.com/okeuday/uuid +pkg_uuid_commit = master + +PACKAGES += ux +pkg_ux_name = ux +pkg_ux_description = Unicode eXtention for Erlang (Strings, Collation) +pkg_ux_homepage = https://github.com/erlang-unicode/ux +pkg_ux_fetch = git +pkg_ux_repo = https://github.com/erlang-unicode/ux +pkg_ux_commit = master + +PACKAGES += vert +pkg_vert_name = vert +pkg_vert_description = Erlang binding to libvirt virtualization API +pkg_vert_homepage = https://github.com/msantos/erlang-libvirt +pkg_vert_fetch = git +pkg_vert_repo = https://github.com/msantos/erlang-libvirt +pkg_vert_commit = master + +PACKAGES += verx +pkg_verx_name = verx +pkg_verx_description = Erlang implementation of the libvirtd remote protocol +pkg_verx_homepage = https://github.com/msantos/verx +pkg_verx_fetch = git +pkg_verx_repo = https://github.com/msantos/verx +pkg_verx_commit = master + +PACKAGES += vmq_acl +pkg_vmq_acl_name = vmq_acl +pkg_vmq_acl_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_acl_homepage = https://verne.mq/ +pkg_vmq_acl_fetch = git +pkg_vmq_acl_repo = https://github.com/erlio/vmq_acl +pkg_vmq_acl_commit = master + +PACKAGES += vmq_bridge +pkg_vmq_bridge_name = vmq_bridge +pkg_vmq_bridge_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_bridge_homepage = https://verne.mq/ +pkg_vmq_bridge_fetch = git +pkg_vmq_bridge_repo = https://github.com/erlio/vmq_bridge +pkg_vmq_bridge_commit = master + +PACKAGES += vmq_graphite +pkg_vmq_graphite_name = vmq_graphite +pkg_vmq_graphite_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_graphite_homepage = https://verne.mq/ +pkg_vmq_graphite_fetch = git +pkg_vmq_graphite_repo = https://github.com/erlio/vmq_graphite +pkg_vmq_graphite_commit = master + +PACKAGES += vmq_passwd +pkg_vmq_passwd_name = vmq_passwd +pkg_vmq_passwd_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_passwd_homepage = https://verne.mq/ +pkg_vmq_passwd_fetch = git +pkg_vmq_passwd_repo = https://github.com/erlio/vmq_passwd +pkg_vmq_passwd_commit = master + +PACKAGES += vmq_server +pkg_vmq_server_name = vmq_server +pkg_vmq_server_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_server_homepage = https://verne.mq/ +pkg_vmq_server_fetch = git +pkg_vmq_server_repo = https://github.com/erlio/vmq_server +pkg_vmq_server_commit = master + +PACKAGES += vmq_snmp +pkg_vmq_snmp_name = vmq_snmp +pkg_vmq_snmp_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_snmp_homepage = https://verne.mq/ +pkg_vmq_snmp_fetch = git +pkg_vmq_snmp_repo = https://github.com/erlio/vmq_snmp +pkg_vmq_snmp_commit = master + +PACKAGES += vmq_systree +pkg_vmq_systree_name = vmq_systree +pkg_vmq_systree_description = Component of VerneMQ: A distributed MQTT message broker +pkg_vmq_systree_homepage = https://verne.mq/ +pkg_vmq_systree_fetch = git +pkg_vmq_systree_repo = https://github.com/erlio/vmq_systree +pkg_vmq_systree_commit = master + +PACKAGES += vmstats +pkg_vmstats_name = vmstats +pkg_vmstats_description = tiny Erlang app that works in conjunction with statsderl in order to generate information on the Erlang VM for graphite logs. +pkg_vmstats_homepage = https://github.com/ferd/vmstats +pkg_vmstats_fetch = git +pkg_vmstats_repo = https://github.com/ferd/vmstats +pkg_vmstats_commit = master + +PACKAGES += walrus +pkg_walrus_name = walrus +pkg_walrus_description = Walrus - Mustache-like Templating +pkg_walrus_homepage = https://github.com/devinus/walrus +pkg_walrus_fetch = git +pkg_walrus_repo = https://github.com/devinus/walrus +pkg_walrus_commit = master + +PACKAGES += webmachine +pkg_webmachine_name = webmachine +pkg_webmachine_description = A REST-based system for building web applications. +pkg_webmachine_homepage = https://github.com/basho/webmachine +pkg_webmachine_fetch = git +pkg_webmachine_repo = https://github.com/basho/webmachine +pkg_webmachine_commit = master + +PACKAGES += websocket_client +pkg_websocket_client_name = websocket_client +pkg_websocket_client_description = Erlang websocket client (ws and wss supported) +pkg_websocket_client_homepage = https://github.com/jeremyong/websocket_client +pkg_websocket_client_fetch = git +pkg_websocket_client_repo = https://github.com/jeremyong/websocket_client +pkg_websocket_client_commit = master + +PACKAGES += worker_pool +pkg_worker_pool_name = worker_pool +pkg_worker_pool_description = a simple erlang worker pool +pkg_worker_pool_homepage = https://github.com/inaka/worker_pool +pkg_worker_pool_fetch = git +pkg_worker_pool_repo = https://github.com/inaka/worker_pool +pkg_worker_pool_commit = master + +PACKAGES += wrangler +pkg_wrangler_name = wrangler +pkg_wrangler_description = Import of the Wrangler svn repository. +pkg_wrangler_homepage = http://www.cs.kent.ac.uk/projects/wrangler/Home.html +pkg_wrangler_fetch = git +pkg_wrangler_repo = https://github.com/RefactoringTools/wrangler +pkg_wrangler_commit = master + +PACKAGES += wsock +pkg_wsock_name = wsock +pkg_wsock_description = Erlang library to build WebSocket clients and servers +pkg_wsock_homepage = https://github.com/madtrick/wsock +pkg_wsock_fetch = git +pkg_wsock_repo = https://github.com/madtrick/wsock +pkg_wsock_commit = master + +PACKAGES += xhttpc +pkg_xhttpc_name = xhttpc +pkg_xhttpc_description = Extensible HTTP Client for Erlang +pkg_xhttpc_homepage = https://github.com/seriyps/xhttpc +pkg_xhttpc_fetch = git +pkg_xhttpc_repo = https://github.com/seriyps/xhttpc +pkg_xhttpc_commit = master + +PACKAGES += xref_runner +pkg_xref_runner_name = xref_runner +pkg_xref_runner_description = Erlang Xref Runner (inspired in rebar xref) +pkg_xref_runner_homepage = https://github.com/inaka/xref_runner +pkg_xref_runner_fetch = git +pkg_xref_runner_repo = https://github.com/inaka/xref_runner +pkg_xref_runner_commit = master + +PACKAGES += yamerl +pkg_yamerl_name = yamerl +pkg_yamerl_description = YAML 1.2 parser in pure Erlang +pkg_yamerl_homepage = https://github.com/yakaz/yamerl +pkg_yamerl_fetch = git +pkg_yamerl_repo = https://github.com/yakaz/yamerl +pkg_yamerl_commit = master + +PACKAGES += yamler +pkg_yamler_name = yamler +pkg_yamler_description = libyaml-based yaml loader for Erlang +pkg_yamler_homepage = https://github.com/goertzenator/yamler +pkg_yamler_fetch = git +pkg_yamler_repo = https://github.com/goertzenator/yamler +pkg_yamler_commit = master + +PACKAGES += yaws +pkg_yaws_name = yaws +pkg_yaws_description = Yaws webserver +pkg_yaws_homepage = http://yaws.hyber.org +pkg_yaws_fetch = git +pkg_yaws_repo = https://github.com/klacke/yaws +pkg_yaws_commit = master + +PACKAGES += zab_engine +pkg_zab_engine_name = zab_engine +pkg_zab_engine_description = zab propotocol implement by erlang +pkg_zab_engine_homepage = https://github.com/xinmingyao/zab_engine +pkg_zab_engine_fetch = git +pkg_zab_engine_repo = https://github.com/xinmingyao/zab_engine +pkg_zab_engine_commit = master + +PACKAGES += zabbix_sender +pkg_zabbix_sender_name = zabbix_sender +pkg_zabbix_sender_description = Zabbix trapper for sending data to Zabbix in pure Erlang +pkg_zabbix_sender_homepage = https://github.com/stalkermn/zabbix_sender +pkg_zabbix_sender_fetch = git +pkg_zabbix_sender_repo = https://github.com/stalkermn/zabbix_sender.git +pkg_zabbix_sender_commit = master + +PACKAGES += zeta +pkg_zeta_name = zeta +pkg_zeta_description = HTTP access log parser in Erlang +pkg_zeta_homepage = https://github.com/s1n4/zeta +pkg_zeta_fetch = git +pkg_zeta_repo = https://github.com/s1n4/zeta +pkg_zeta_commit = master + +PACKAGES += zippers +pkg_zippers_name = zippers +pkg_zippers_description = A library for functional zipper data structures in Erlang. Read more on zippers +pkg_zippers_homepage = https://github.com/ferd/zippers +pkg_zippers_fetch = git +pkg_zippers_repo = https://github.com/ferd/zippers +pkg_zippers_commit = master + +PACKAGES += zlists +pkg_zlists_name = zlists +pkg_zlists_description = Erlang lazy lists library. +pkg_zlists_homepage = https://github.com/vjache/erlang-zlists +pkg_zlists_fetch = git +pkg_zlists_repo = https://github.com/vjache/erlang-zlists +pkg_zlists_commit = master + +PACKAGES += zraft_lib +pkg_zraft_lib_name = zraft_lib +pkg_zraft_lib_description = Erlang raft consensus protocol implementation +pkg_zraft_lib_homepage = https://github.com/dreyk/zraft_lib +pkg_zraft_lib_fetch = git +pkg_zraft_lib_repo = https://github.com/dreyk/zraft_lib +pkg_zraft_lib_commit = master + +PACKAGES += zucchini +pkg_zucchini_name = zucchini +pkg_zucchini_description = An Erlang INI parser +pkg_zucchini_homepage = https://github.com/devinus/zucchini +pkg_zucchini_fetch = git +pkg_zucchini_repo = https://github.com/devinus/zucchini +pkg_zucchini_commit = master + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: search + +define pkg_print + $(verbose) printf "%s\n" \ + $(if $(call core_eq,$(1),$(pkg_$(1)_name)),,"Pkg name: $(1)") \ + "App name: $(pkg_$(1)_name)" \ + "Description: $(pkg_$(1)_description)" \ + "Home page: $(pkg_$(1)_homepage)" \ + "Fetch with: $(pkg_$(1)_fetch)" \ + "Repository: $(pkg_$(1)_repo)" \ + "Commit: $(pkg_$(1)_commit)" \ + "" + +endef + +search: +ifdef q + $(foreach p,$(PACKAGES), \ + $(if $(findstring $(call core_lc,$(q)),$(call core_lc,$(pkg_$(p)_name) $(pkg_$(p)_description))), \ + $(call pkg_print,$(p)))) +else + $(foreach p,$(PACKAGES),$(call pkg_print,$(p))) +endif + +# Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: distclean-deps clean-tmp-deps.log + +# Configuration. + +ifdef OTP_DEPS +$(warning The variable OTP_DEPS is deprecated in favor of LOCAL_DEPS.) +endif + +IGNORE_DEPS ?= +export IGNORE_DEPS + +APPS_DIR ?= $(CURDIR)/apps +export APPS_DIR + +DEPS_DIR ?= $(CURDIR)/deps +export DEPS_DIR + +REBAR_DEPS_DIR = $(DEPS_DIR) +export REBAR_DEPS_DIR + +REBAR_GIT ?= https://github.com/rebar/rebar +REBAR_COMMIT ?= 576e12171ab8d69b048b827b92aa65d067deea01 + +# External "early" plugins (see core/plugins.mk for regular plugins). +# They both use the core_dep_plugin macro. + +define core_dep_plugin +ifeq ($(2),$(PROJECT)) +-include $$(patsubst $(PROJECT)/%,%,$(1)) +else +-include $(DEPS_DIR)/$(1) + +$(DEPS_DIR)/$(1): $(DEPS_DIR)/$(2) ; +endif +endef + +DEP_EARLY_PLUGINS ?= + +$(foreach p,$(DEP_EARLY_PLUGINS),\ + $(eval $(if $(findstring /,$p),\ + $(call core_dep_plugin,$p,$(firstword $(subst /, ,$p))),\ + $(call core_dep_plugin,$p/early-plugins.mk,$p)))) + +# Query functions. + +query_fetch_method = $(if $(dep_$(1)),$(call _qfm_dep,$(word 1,$(dep_$(1)))),$(call _qfm_pkg,$(1))) +_qfm_dep = $(if $(dep_fetch_$(1)),$(1),$(if $(IS_DEP),legacy,fail)) +_qfm_pkg = $(if $(pkg_$(1)_fetch),$(pkg_$(1)_fetch),fail) + +query_name = $(if $(dep_$(1)),$(1),$(if $(pkg_$(1)_name),$(pkg_$(1)_name),$(1))) + +query_repo = $(call _qr,$(1),$(call query_fetch_method,$(1))) +_qr = $(if $(query_repo_$(2)),$(call query_repo_$(2),$(1)),$(call dep_repo,$(1))) + +query_repo_default = $(if $(dep_$(1)),$(word 2,$(dep_$(1))),$(pkg_$(1)_repo)) +query_repo_git = $(patsubst git://github.com/%,https://github.com/%,$(call query_repo_default,$(1))) +query_repo_git-subfolder = $(call query_repo_git,$(1)) +query_repo_git-submodule = - +query_repo_hg = $(call query_repo_default,$(1)) +query_repo_svn = $(call query_repo_default,$(1)) +query_repo_cp = $(call query_repo_default,$(1)) +query_repo_ln = $(call query_repo_default,$(1)) +query_repo_hex = https://hex.pm/packages/$(if $(word 3,$(dep_$(1))),$(word 3,$(dep_$(1))),$(1)) +query_repo_fail = - +query_repo_legacy = - + +query_version = $(call _qv,$(1),$(call query_fetch_method,$(1))) +_qv = $(if $(query_version_$(2)),$(call query_version_$(2),$(1)),$(call dep_commit,$(1))) + +query_version_default = $(if $(dep_$(1)_commit),$(dep_$(1)_commit),$(if $(dep_$(1)),$(word 3,$(dep_$(1))),$(pkg_$(1)_commit))) +query_version_git = $(call query_version_default,$(1)) +query_version_git-subfolder = $(call query_version_git,$(1)) +query_version_git-submodule = - +query_version_hg = $(call query_version_default,$(1)) +query_version_svn = - +query_version_cp = - +query_version_ln = - +query_version_hex = $(if $(dep_$(1)_commit),$(dep_$(1)_commit),$(if $(dep_$(1)),$(word 2,$(dep_$(1))),$(pkg_$(1)_commit))) +query_version_fail = - +query_version_legacy = - + +query_extra = $(call _qe,$(1),$(call query_fetch_method,$(1))) +_qe = $(if $(query_extra_$(2)),$(call query_extra_$(2),$(1)),-) + +query_extra_git = - +query_extra_git-subfolder = $(if $(dep_$(1)),subfolder=$(word 4,$(dep_$(1))),-) +query_extra_git-submodule = - +query_extra_hg = - +query_extra_svn = - +query_extra_cp = - +query_extra_ln = - +query_extra_hex = $(if $(dep_$(1)),package-name=$(word 3,$(dep_$(1))),-) +query_extra_fail = - +query_extra_legacy = - + +query_absolute_path = $(addprefix $(DEPS_DIR)/,$(call query_name,$(1))) + +# Deprecated legacy query functions. +dep_fetch = $(call query_fetch_method,$(1)) +dep_name = $(call query_name,$(1)) +dep_repo = $(call query_repo_git,$(1)) +dep_commit = $(if $(dep_$(1)_commit),$(dep_$(1)_commit),$(if $(dep_$(1)),$(if $(filter hex,$(word 1,$(dep_$(1)))),$(word 2,$(dep_$(1))),$(word 3,$(dep_$(1)))),$(pkg_$(1)_commit))) + +LOCAL_DEPS_DIRS = $(foreach a,$(LOCAL_DEPS),$(if $(wildcard $(APPS_DIR)/$(a)),$(APPS_DIR)/$(a))) +ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(foreach dep,$(filter-out $(IGNORE_DEPS),$(BUILD_DEPS) $(DEPS)),$(call dep_name,$(dep)))) + +# When we are calling an app directly we don't want to include it here +# otherwise it'll be treated both as an apps and a top-level project. +ALL_APPS_DIRS = $(if $(wildcard $(APPS_DIR)/),$(filter-out $(APPS_DIR),$(shell find $(APPS_DIR) -maxdepth 1 -type d))) +ifdef ROOT_DIR +ifndef IS_APP +ALL_APPS_DIRS := $(filter-out $(APPS_DIR)/$(notdir $(CURDIR)),$(ALL_APPS_DIRS)) +endif +endif + +ifeq ($(filter $(APPS_DIR) $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),) +ifeq ($(ERL_LIBS),) + ERL_LIBS = $(APPS_DIR):$(DEPS_DIR) +else + ERL_LIBS := $(ERL_LIBS):$(APPS_DIR):$(DEPS_DIR) +endif +endif +export ERL_LIBS + +export NO_AUTOPATCH + +# Verbosity. + +dep_verbose_0 = @echo " DEP $1 ($(call dep_commit,$1))"; +dep_verbose_2 = set -x; +dep_verbose = $(dep_verbose_$(V)) + +# Optimization: don't recompile deps unless truly necessary. + +ifndef IS_DEP +ifneq ($(MAKELEVEL),0) +$(shell rm -f ebin/dep_built) +endif +endif + +# Core targets. + +ALL_APPS_DIRS_TO_BUILD = $(if $(LOCAL_DEPS_DIRS)$(IS_APP),$(LOCAL_DEPS_DIRS),$(ALL_APPS_DIRS)) + +apps:: $(ALL_APPS_DIRS) clean-tmp-deps.log | $(ERLANG_MK_TMP) +# Create ebin directory for all apps to make sure Erlang recognizes them +# as proper OTP applications when using -include_lib. This is a temporary +# fix, a proper fix would be to compile apps/* in the right order. +ifndef IS_APP +ifneq ($(ALL_APPS_DIRS),) + $(verbose) set -e; for dep in $(ALL_APPS_DIRS) ; do \ + mkdir -p $$dep/ebin; \ + done +endif +endif +# At the toplevel: if LOCAL_DEPS is defined with at least one local app, only +# compile that list of apps. Otherwise, compile everything. +# Within an app: compile all LOCAL_DEPS that are (uncompiled) local apps. +ifneq ($(ALL_APPS_DIRS_TO_BUILD),) + $(verbose) set -e; for dep in $(ALL_APPS_DIRS_TO_BUILD); do \ + if grep -qs ^$$dep$$ $(ERLANG_MK_TMP)/apps.log; then \ + :; \ + else \ + echo $$dep >> $(ERLANG_MK_TMP)/apps.log; \ + $(MAKE) -C $$dep $(if $(IS_TEST),test-build-app) IS_APP=1; \ + fi \ + done +endif + +clean-tmp-deps.log: +ifeq ($(IS_APP)$(IS_DEP),) + $(verbose) rm -f $(ERLANG_MK_TMP)/apps.log $(ERLANG_MK_TMP)/deps.log +endif + +# Erlang.mk does not rebuild dependencies after they were compiled +# once. If a developer is working on the top-level project and some +# dependencies at the same time, he may want to change this behavior. +# There are two solutions: +# 1. Set `FULL=1` so that all dependencies are visited and +# recursively recompiled if necessary. +# 2. Set `FORCE_REBUILD=` to the specific list of dependencies that +# should be recompiled (instead of the whole set). + +FORCE_REBUILD ?= + +ifeq ($(origin FULL),undefined) +ifneq ($(strip $(force_rebuild_dep)$(FORCE_REBUILD)),) +define force_rebuild_dep +echo "$(FORCE_REBUILD)" | grep -qw "$$(basename "$1")" +endef +endif +endif + +ifneq ($(SKIP_DEPS),) +deps:: +else +deps:: $(ALL_DEPS_DIRS) apps clean-tmp-deps.log | $(ERLANG_MK_TMP) +ifneq ($(ALL_DEPS_DIRS),) + $(verbose) set -e; for dep in $(ALL_DEPS_DIRS); do \ + if grep -qs ^$$dep$$ $(ERLANG_MK_TMP)/deps.log; then \ + :; \ + else \ + echo $$dep >> $(ERLANG_MK_TMP)/deps.log; \ + if [ -z "$(strip $(FULL))" ] $(if $(force_rebuild_dep),&& ! ($(call force_rebuild_dep,$$dep)),) && [ ! -L $$dep ] && [ -f $$dep/ebin/dep_built ]; then \ + :; \ + elif [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ]; then \ + $(MAKE) -C $$dep IS_DEP=1; \ + if [ ! -L $$dep ] && [ -d $$dep/ebin ]; then touch $$dep/ebin/dep_built; fi; \ + else \ + echo "Error: No Makefile to build dependency $$dep." >&2; \ + exit 2; \ + fi \ + fi \ + done +endif +endif + +# Deps related targets. + +# @todo rename GNUmakefile and makefile into Makefile first, if they exist +# While Makefile file could be GNUmakefile or makefile, +# in practice only Makefile is needed so far. +define dep_autopatch + if [ -f $(DEPS_DIR)/$(1)/erlang.mk ]; then \ + rm -rf $(DEPS_DIR)/$1/ebin/; \ + $(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \ + $(call dep_autopatch_erlang_mk,$(1)); \ + elif [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \ + if [ -f $(DEPS_DIR)/$1/rebar.lock ]; then \ + $(call dep_autopatch2,$1); \ + elif [ 0 != `grep -c "include ../\w*\.mk" $(DEPS_DIR)/$(1)/Makefile` ]; then \ + $(call dep_autopatch2,$(1)); \ + elif [ 0 != `grep -ci "^[^#].*rebar" $(DEPS_DIR)/$(1)/Makefile` ]; then \ + $(call dep_autopatch2,$(1)); \ + elif [ -n "`find $(DEPS_DIR)/$(1)/ -type f -name \*.mk -not -name erlang.mk -exec grep -i "^[^#].*rebar" '{}' \;`" ]; then \ + $(call dep_autopatch2,$(1)); \ + fi \ + else \ + if [ ! -d $(DEPS_DIR)/$(1)/src/ ]; then \ + $(call dep_autopatch_noop,$(1)); \ + else \ + $(call dep_autopatch2,$(1)); \ + fi \ + fi +endef + +define dep_autopatch2 + ! test -f $(DEPS_DIR)/$1/ebin/$1.app || \ + mv -n $(DEPS_DIR)/$1/ebin/$1.app $(DEPS_DIR)/$1/src/$1.app.src; \ + rm -f $(DEPS_DIR)/$1/ebin/$1.app; \ + if [ -f $(DEPS_DIR)/$1/src/$1.app.src.script ]; then \ + $(call erlang,$(call dep_autopatch_appsrc_script.erl,$(1))); \ + fi; \ + $(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \ + if [ -f $(DEPS_DIR)/$(1)/rebar -o -f $(DEPS_DIR)/$(1)/rebar.config -o -f $(DEPS_DIR)/$(1)/rebar.config.script -o -f $(DEPS_DIR)/$1/rebar.lock ]; then \ + $(call dep_autopatch_fetch_rebar); \ + $(call dep_autopatch_rebar,$(1)); \ + else \ + $(call dep_autopatch_gen,$(1)); \ + fi +endef + +define dep_autopatch_noop + printf "noop:\n" > $(DEPS_DIR)/$(1)/Makefile +endef + +# Replace "include erlang.mk" with a line that will load the parent Erlang.mk +# if given. Do it for all 3 possible Makefile file names. +ifeq ($(NO_AUTOPATCH_ERLANG_MK),) +define dep_autopatch_erlang_mk + for f in Makefile makefile GNUmakefile; do \ + if [ -f $(DEPS_DIR)/$1/$$f ]; then \ + sed -i.bak s/'include *erlang.mk'/'include $$(if $$(ERLANG_MK_FILENAME),$$(ERLANG_MK_FILENAME),erlang.mk)'/ $(DEPS_DIR)/$1/$$f; \ + fi \ + done +endef +else +define dep_autopatch_erlang_mk + : +endef +endif + +define dep_autopatch_gen + printf "%s\n" \ + "ERLC_OPTS = +debug_info" \ + "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile +endef + +# We use flock/lockf when available to avoid concurrency issues. +define dep_autopatch_fetch_rebar + if command -v flock >/dev/null; then \ + flock $(ERLANG_MK_TMP)/rebar.lock sh -c "$(call dep_autopatch_fetch_rebar2)"; \ + elif command -v lockf >/dev/null; then \ + lockf $(ERLANG_MK_TMP)/rebar.lock sh -c "$(call dep_autopatch_fetch_rebar2)"; \ + else \ + $(call dep_autopatch_fetch_rebar2); \ + fi +endef + +define dep_autopatch_fetch_rebar2 + if [ ! -d $(ERLANG_MK_TMP)/rebar ]; then \ + git clone -q -n -- $(REBAR_GIT) $(ERLANG_MK_TMP)/rebar; \ + cd $(ERLANG_MK_TMP)/rebar; \ + git checkout -q $(REBAR_COMMIT); \ + ./bootstrap; \ + cd -; \ + fi +endef + +define dep_autopatch_rebar + if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \ + mv $(DEPS_DIR)/$(1)/Makefile $(DEPS_DIR)/$(1)/Makefile.orig.mk; \ + fi; \ + $(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \ + rm -f $(DEPS_DIR)/$(1)/ebin/$(1).app +endef + +define dep_autopatch_rebar.erl + application:load(rebar), + application:set_env(rebar, log_level, debug), + rmemo:start(), + Conf1 = case file:consult("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config)") of + {ok, Conf0} -> Conf0; + _ -> [] + end, + {Conf, OsEnv} = fun() -> + case filelib:is_file("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)") of + false -> {Conf1, []}; + true -> + Bindings0 = erl_eval:new_bindings(), + Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0), + Bindings = erl_eval:add_binding('SCRIPT', "$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)", Bindings1), + Before = os:getenv(), + {ok, Conf2} = file:script("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)", Bindings), + {Conf2, lists:foldl(fun(E, Acc) -> lists:delete(E, Acc) end, os:getenv(), Before)} + end + end(), + Write = fun (Text) -> + file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/Makefile)", Text, [append]) + end, + Escape = fun (Text) -> + re:replace(Text, "\\\\$$", "\$$$$", [global, {return, list}]) + end, + Write("IGNORE_DEPS += edown eper eunit_formatters meck node_package " + "rebar_lock_deps_plugin rebar_vsn_plugin reltool_util\n"), + Write("C_SRC_DIR = /path/do/not/exist\n"), + Write("C_SRC_TYPE = rebar\n"), + Write("DRV_CFLAGS = -fPIC\nexport DRV_CFLAGS\n"), + Write(["ERLANG_ARCH = ", rebar_utils:wordsize(), "\nexport ERLANG_ARCH\n"]), + ToList = fun + (V) when is_atom(V) -> atom_to_list(V); + (V) when is_list(V) -> "'\\"" ++ V ++ "\\"'" + end, + fun() -> + Write("ERLC_OPTS = +debug_info\nexport ERLC_OPTS\n"), + case lists:keyfind(erl_opts, 1, Conf) of + false -> ok; + {_, ErlOpts} -> + lists:foreach(fun + ({d, D}) -> + Write("ERLC_OPTS += -D" ++ ToList(D) ++ "=1\n"); + ({d, DKey, DVal}) -> + Write("ERLC_OPTS += -D" ++ ToList(DKey) ++ "=" ++ ToList(DVal) ++ "\n"); + ({i, I}) -> + Write(["ERLC_OPTS += -I ", I, "\n"]); + ({platform_define, Regex, D}) -> + case rebar_utils:is_arch(Regex) of + true -> Write("ERLC_OPTS += -D" ++ ToList(D) ++ "=1\n"); + false -> ok + end; + ({parse_transform, PT}) -> + Write("ERLC_OPTS += +'{parse_transform, " ++ ToList(PT) ++ "}'\n"); + (_) -> ok + end, ErlOpts) + end, + Write("\n") + end(), + GetHexVsn = fun(N, NP) -> + case file:consult("$(call core_native_path,$(DEPS_DIR)/$1/rebar.lock)") of + {ok, Lock} -> + io:format("~p~n", [Lock]), + case lists:keyfind("1.1.0", 1, Lock) of + {_, LockPkgs} -> + io:format("~p~n", [LockPkgs]), + case lists:keyfind(atom_to_binary(N, latin1), 1, LockPkgs) of + {_, {pkg, _, Vsn}, _} -> + io:format("~p~n", [Vsn]), + {N, {hex, NP, binary_to_list(Vsn)}}; + _ -> + false + end; + _ -> + false + end; + _ -> + false + end + end, + SemVsn = fun + ("~>" ++ S0) -> + S = case S0 of + " " ++ S1 -> S1; + _ -> S0 + end, + case length([ok || $$. <- S]) of + 0 -> S ++ ".0.0"; + 1 -> S ++ ".0"; + _ -> S + end; + (S) -> S + end, + fun() -> + File = case lists:keyfind(deps, 1, Conf) of + false -> []; + {_, Deps} -> + [begin case case Dep of + N when is_atom(N) -> GetHexVsn(N, N); + {N, S} when is_atom(N), is_list(S) -> {N, {hex, N, SemVsn(S)}}; + {N, {pkg, NP}} when is_atom(N) -> GetHexVsn(N, NP); + {N, S, {pkg, NP}} -> {N, {hex, NP, S}}; + {N, S} when is_tuple(S) -> {N, S}; + {N, _, S} -> {N, S}; + {N, _, S, _} -> {N, S}; + _ -> false + end of + false -> ok; + {Name, Source} -> + {Method, Repo, Commit} = case Source of + {hex, NPV, V} -> {hex, V, NPV}; + {git, R} -> {git, R, master}; + {M, R, {branch, C}} -> {M, R, C}; + {M, R, {ref, C}} -> {M, R, C}; + {M, R, {tag, C}} -> {M, R, C}; + {M, R, C} -> {M, R, C} + end, + Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit])) + end end || Dep <- Deps] + end + end(), + fun() -> + case lists:keyfind(erl_first_files, 1, Conf) of + false -> ok; + {_, Files} -> + Names = [[" ", case lists:reverse(F) of + "lre." ++ Elif -> lists:reverse(Elif); + "lrx." ++ Elif -> lists:reverse(Elif); + "lry." ++ Elif -> lists:reverse(Elif); + Elif -> lists:reverse(Elif) + end] || "src/" ++ F <- Files], + Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names])) + end + end(), + Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"), + Write("\npreprocess::\n"), + Write("\npre-deps::\n"), + Write("\npre-app::\n"), + PatchHook = fun(Cmd) -> + Cmd2 = re:replace(Cmd, "^([g]?make)(.*)( -C.*)", "\\\\1\\\\3\\\\2", [{return, list}]), + case Cmd2 of + "make -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1); + "gmake -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1); + "make " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1); + "gmake " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1); + _ -> Escape(Cmd) + end + end, + fun() -> + case lists:keyfind(pre_hooks, 1, Conf) of + false -> ok; + {_, Hooks} -> + [case H of + {'get-deps', Cmd} -> + Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n"); + {compile, Cmd} -> + Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n"); + {Regex, compile, Cmd} -> + case rebar_utils:is_arch(Regex) of + true -> Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n"); + false -> ok + end; + _ -> ok + end || H <- Hooks] + end + end(), + ShellToMk = fun(V0) -> + V1 = re:replace(V0, "[$$][(]", "$$\(shell ", [global]), + V = re:replace(V1, "([$$])(?![(])(\\\\w*)", "\\\\1(\\\\2)", [global]), + re:replace(V, "-Werror\\\\b", "", [{return, list}, global]) + end, + PortSpecs = fun() -> + case lists:keyfind(port_specs, 1, Conf) of + false -> + case filelib:is_dir("$(call core_native_path,$(DEPS_DIR)/$1/c_src)") of + false -> []; + true -> + [{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"), + proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}] + end; + {_, Specs} -> + lists:flatten([case S of + {Output, Input} -> {ShellToMk(Output), Input, []}; + {Regex, Output, Input} -> + case rebar_utils:is_arch(Regex) of + true -> {ShellToMk(Output), Input, []}; + false -> [] + end; + {Regex, Output, Input, [{env, Env}]} -> + case rebar_utils:is_arch(Regex) of + true -> {ShellToMk(Output), Input, Env}; + false -> [] + end + end || S <- Specs]) + end + end(), + PortSpecWrite = fun (Text) -> + file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/c_src/Makefile.erlang.mk)", Text, [append]) + end, + case PortSpecs of + [] -> ok; + _ -> + Write("\npre-app::\n\t@$$\(MAKE) --no-print-directory -f c_src/Makefile.erlang.mk\n"), + PortSpecWrite(io_lib:format("ERL_CFLAGS ?= -finline-functions -Wall -fPIC -I \\"~s/erts-~s/include\\" -I \\"~s\\"\n", + [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])), + PortSpecWrite(io_lib:format("ERL_LDFLAGS ?= -L \\"~s\\" -lei\n", + [code:lib_dir(erl_interface, lib)])), + [PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv], + FilterEnv = fun(Env) -> + lists:flatten([case E of + {_, _} -> E; + {Regex, K, V} -> + case rebar_utils:is_arch(Regex) of + true -> {K, V}; + false -> [] + end + end || E <- Env]) + end, + MergeEnv = fun(Env) -> + lists:foldl(fun ({K, V}, Acc) -> + case lists:keyfind(K, 1, Acc) of + false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc]; + {_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc] + end + end, [], Env) + end, + PortEnv = case lists:keyfind(port_env, 1, Conf) of + false -> []; + {_, PortEnv0} -> FilterEnv(PortEnv0) + end, + PortSpec = fun ({Output, Input0, Env}) -> + filelib:ensure_dir("$(call core_native_path,$(DEPS_DIR)/$1/)" ++ Output), + Input = [[" ", I] || I <- Input0], + PortSpecWrite([ + [["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))], + case $(PLATFORM) of + darwin -> "\n\nLDFLAGS += -flat_namespace -undefined suppress"; + _ -> "" + end, + "\n\nall:: ", Output, "\n\t@:\n\n", + "%.o: %.c\n\t$$\(CC) -c -o $$\@ $$\< $$\(CFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n", + "%.o: %.C\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n", + "%.o: %.cc\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n", + "%.o: %.cpp\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n", + [[Output, ": ", K, " += ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))], + Output, ": $$\(foreach ext,.c .C .cc .cpp,", + "$$\(patsubst %$$\(ext),%.o,$$\(filter %$$\(ext),$$\(wildcard", Input, "))))\n", + "\t$$\(CC) -o $$\@ $$\? $$\(LDFLAGS) $$\(ERL_LDFLAGS) $$\(DRV_LDFLAGS) $$\(EXE_LDFLAGS)", + case {filename:extension(Output), $(PLATFORM)} of + {[], _} -> "\n"; + {_, darwin} -> "\n"; + _ -> " -shared\n" + end]) + end, + [PortSpec(S) || S <- PortSpecs] + end, + fun() -> + case lists:keyfind(plugins, 1, Conf) of + false -> ok; + {_, Plugins0} -> + Plugins = [P || P <- Plugins0, is_tuple(P)], + case lists:keyfind('lfe-compile', 1, Plugins) of + false -> ok; + _ -> Write("\nBUILD_DEPS = lfe lfe.mk\ndep_lfe.mk = git https://github.com/ninenines/lfe.mk master\nDEP_PLUGINS = lfe.mk\n") + end + end + end(), + Write("\ninclude $$\(if $$\(ERLANG_MK_FILENAME),$$\(ERLANG_MK_FILENAME),erlang.mk)"), + RunPlugin = fun(Plugin, Step) -> + case erlang:function_exported(Plugin, Step, 2) of + false -> ok; + true -> + c:cd("$(call core_native_path,$(DEPS_DIR)/$1/)"), + Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(), + dict:store(base_dir, "", dict:new())}, undefined), + io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret]) + end + end, + fun() -> + case lists:keyfind(plugins, 1, Conf) of + false -> ok; + {_, Plugins0} -> + Plugins = [P || P <- Plugins0, is_atom(P)], + [begin + case lists:keyfind(deps, 1, Conf) of + false -> ok; + {_, Deps} -> + case lists:keyfind(P, 1, Deps) of + false -> ok; + _ -> + Path = "$(call core_native_path,$(DEPS_DIR)/)" ++ atom_to_list(P), + io:format("~s", [os:cmd("$(MAKE) -C $(call core_native_path,$(DEPS_DIR)/$1) " ++ Path)]), + io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]), + code:add_patha(Path ++ "/ebin") + end + end + end || P <- Plugins], + [case code:load_file(P) of + {module, P} -> ok; + _ -> + case lists:keyfind(plugin_dir, 1, Conf) of + false -> ok; + {_, PluginsDir} -> + ErlFile = "$(call core_native_path,$(DEPS_DIR)/$1/)" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl", + {ok, P, Bin} = compile:file(ErlFile, [binary]), + {module, P} = code:load_binary(P, ErlFile, Bin) + end + end || P <- Plugins], + [RunPlugin(P, preprocess) || P <- Plugins], + [RunPlugin(P, pre_compile) || P <- Plugins], + [RunPlugin(P, compile) || P <- Plugins] + end + end(), + halt() +endef + +define dep_autopatch_appsrc_script.erl + AppSrc = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)", + AppSrcScript = AppSrc ++ ".script", + {ok, Conf0} = file:consult(AppSrc), + Bindings0 = erl_eval:new_bindings(), + Bindings1 = erl_eval:add_binding('CONFIG', Conf0, Bindings0), + Bindings = erl_eval:add_binding('SCRIPT', AppSrcScript, Bindings1), + Conf = case file:script(AppSrcScript, Bindings) of + {ok, [C]} -> C; + {ok, C} -> C + end, + ok = file:write_file(AppSrc, io_lib:format("~p.~n", [Conf])), + halt() +endef + +define dep_autopatch_appsrc.erl + AppSrcOut = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)", + AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"; true -> AppSrcOut end, + case filelib:is_regular(AppSrcIn) of + false -> ok; + true -> + {ok, [{application, $(1), L0}]} = file:consult(AppSrcIn), + L1 = lists:keystore(modules, 1, L0, {modules, []}), + L2 = case lists:keyfind(vsn, 1, L1) of + {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, lists:droplast(os:cmd("git -C $(DEPS_DIR)/$1 describe --dirty --tags --always"))}); + {_, {cmd, _}} -> lists:keyreplace(vsn, 1, L1, {vsn, "cmd"}); + _ -> L1 + end, + L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end, + ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])), + case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end + end, + halt() +endef + +define dep_fetch_git + git clone -q -n -- $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \ + cd $(DEPS_DIR)/$(call dep_name,$(1)) && git checkout -q $(call dep_commit,$(1)); +endef + +define dep_fetch_git-subfolder + mkdir -p $(ERLANG_MK_TMP)/git-subfolder; \ + git clone -q -n -- $(call dep_repo,$1) \ + $(ERLANG_MK_TMP)/git-subfolder/$(call dep_name,$1); \ + cd $(ERLANG_MK_TMP)/git-subfolder/$(call dep_name,$1) \ + && git checkout -q $(call dep_commit,$1); \ + ln -s $(ERLANG_MK_TMP)/git-subfolder/$(call dep_name,$1)/$(word 4,$(dep_$(1))) \ + $(DEPS_DIR)/$(call dep_name,$1); +endef + +define dep_fetch_git-submodule + git submodule update --init -- $(DEPS_DIR)/$1; +endef + +define dep_fetch_hg + hg clone -q -U $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \ + cd $(DEPS_DIR)/$(call dep_name,$(1)) && hg update -q $(call dep_commit,$(1)); +endef + +define dep_fetch_svn + svn checkout -q $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); +endef + +define dep_fetch_cp + cp -R $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); +endef + +define dep_fetch_ln + ln -s $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); +endef + +# Hex only has a package version. No need to look in the Erlang.mk packages. +define dep_fetch_hex + mkdir -p $(ERLANG_MK_TMP)/hex $(DEPS_DIR)/$1; \ + $(call core_http_get,$(ERLANG_MK_TMP)/hex/$1.tar,\ + https://repo.hex.pm/tarballs/$(if $(word 3,$(dep_$1)),$(word 3,$(dep_$1)),$1)-$(strip $(word 2,$(dep_$1))).tar); \ + tar -xOf $(ERLANG_MK_TMP)/hex/$1.tar contents.tar.gz | tar -C $(DEPS_DIR)/$1 -xzf -; +endef + +define dep_fetch_fail + echo "Error: Unknown or invalid dependency: $(1)." >&2; \ + exit 78; +endef + +# Kept for compatibility purposes with older Erlang.mk configuration. +define dep_fetch_legacy + $(warning WARNING: '$(1)' dependency configuration uses deprecated format.) \ + git clone -q -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1); \ + cd $(DEPS_DIR)/$(1) && git checkout -q $(if $(word 2,$(dep_$(1))),$(word 2,$(dep_$(1))),master); +endef + +define dep_target +$(DEPS_DIR)/$(call dep_name,$1): | $(ERLANG_MK_TMP) + $(eval DEP_NAME := $(call dep_name,$1)) + $(eval DEP_STR := $(if $(filter $1,$(DEP_NAME)),$1,"$1 ($(DEP_NAME))")) + $(verbose) if test -d $(APPS_DIR)/$(DEP_NAME); then \ + echo "Error: Dependency" $(DEP_STR) "conflicts with application found in $(APPS_DIR)/$(DEP_NAME)." >&2; \ + exit 17; \ + fi + $(verbose) mkdir -p $(DEPS_DIR) + $(dep_verbose) $(call dep_fetch_$(strip $(call dep_fetch,$(1))),$(1)) + $(verbose) if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ] \ + && [ ! -f $(DEPS_DIR)/$(1)/configure ]; then \ + echo " AUTO " $(DEP_STR); \ + cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \ + fi + - $(verbose) if [ -f $(DEPS_DIR)/$(DEP_NAME)/configure ]; then \ + echo " CONF " $(DEP_STR); \ + cd $(DEPS_DIR)/$(DEP_NAME) && ./configure; \ + fi +ifeq ($(filter $(1),$(NO_AUTOPATCH)),) + $(verbose) $$(MAKE) --no-print-directory autopatch-$(DEP_NAME) +endif + +.PHONY: autopatch-$(call dep_name,$1) + +autopatch-$(call dep_name,$1):: + $(verbose) if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \ + if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \ + echo " PATCH Downloading rabbitmq-codegen"; \ + git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \ + fi; \ + if [ ! -d $(DEPS_DIR)/rabbitmq-server ]; then \ + echo " PATCH Downloading rabbitmq-server"; \ + git clone https://github.com/rabbitmq/rabbitmq-server.git $(DEPS_DIR)/rabbitmq-server; \ + fi; \ + ln -s $(DEPS_DIR)/amqp_client/deps/rabbit_common-0.0.0 $(DEPS_DIR)/rabbit_common; \ + elif [ "$(1)" = "rabbit" -a "$(RABBITMQ_SERVER_PATCH)" ]; then \ + if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \ + echo " PATCH Downloading rabbitmq-codegen"; \ + git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \ + fi \ + elif [ "$1" = "elixir" -a "$(ELIXIR_PATCH)" ]; then \ + ln -s lib/elixir/ebin $(DEPS_DIR)/elixir/; \ + else \ + $$(call dep_autopatch,$(call dep_name,$1)) \ + fi +endef + +$(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target,$(dep)))) + +ifndef IS_APP +clean:: clean-apps + +clean-apps: + $(verbose) set -e; for dep in $(ALL_APPS_DIRS) ; do \ + $(MAKE) -C $$dep clean IS_APP=1; \ + done + +distclean:: distclean-apps + +distclean-apps: + $(verbose) set -e; for dep in $(ALL_APPS_DIRS) ; do \ + $(MAKE) -C $$dep distclean IS_APP=1; \ + done +endif + +ifndef SKIP_DEPS +distclean:: distclean-deps + +distclean-deps: + $(gen_verbose) rm -rf $(DEPS_DIR) +endif + +# Forward-declare variables used in core/deps-tools.mk. This is required +# in case plugins use them. + +ERLANG_MK_RECURSIVE_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-deps-list.log +ERLANG_MK_RECURSIVE_DOC_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-doc-deps-list.log +ERLANG_MK_RECURSIVE_REL_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-rel-deps-list.log +ERLANG_MK_RECURSIVE_TEST_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-test-deps-list.log +ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-shell-deps-list.log + +ERLANG_MK_QUERY_DEPS_FILE = $(ERLANG_MK_TMP)/query-deps.log +ERLANG_MK_QUERY_DOC_DEPS_FILE = $(ERLANG_MK_TMP)/query-doc-deps.log +ERLANG_MK_QUERY_REL_DEPS_FILE = $(ERLANG_MK_TMP)/query-rel-deps.log +ERLANG_MK_QUERY_TEST_DEPS_FILE = $(ERLANG_MK_TMP)/query-test-deps.log +ERLANG_MK_QUERY_SHELL_DEPS_FILE = $(ERLANG_MK_TMP)/query-shell-deps.log + +# Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: clean-app + +# Configuration. + +ERLC_OPTS ?= -Werror +debug_info +warn_export_vars +warn_shadow_vars \ + +warn_obsolete_guard # +bin_opt_info +warn_export_all +warn_missing_spec +COMPILE_FIRST ?= +COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST))) +ERLC_EXCLUDE ?= +ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE))) + +ERLC_ASN1_OPTS ?= + +ERLC_MIB_OPTS ?= +COMPILE_MIB_FIRST ?= +COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST))) + +# Verbosity. + +app_verbose_0 = @echo " APP " $(PROJECT); +app_verbose_2 = set -x; +app_verbose = $(app_verbose_$(V)) + +appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src; +appsrc_verbose_2 = set -x; +appsrc_verbose = $(appsrc_verbose_$(V)) + +makedep_verbose_0 = @echo " DEPEND" $(PROJECT).d; +makedep_verbose_2 = set -x; +makedep_verbose = $(makedep_verbose_$(V)) + +erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\ + $(filter %.erl %.core,$(?F))); +erlc_verbose_2 = set -x; +erlc_verbose = $(erlc_verbose_$(V)) + +xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F)); +xyrl_verbose_2 = set -x; +xyrl_verbose = $(xyrl_verbose_$(V)) + +asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F)); +asn1_verbose_2 = set -x; +asn1_verbose = $(asn1_verbose_$(V)) + +mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F)); +mib_verbose_2 = set -x; +mib_verbose = $(mib_verbose_$(V)) + +ifneq ($(wildcard src/),) + +# Targets. + +app:: $(if $(wildcard ebin/test),clean) deps + $(verbose) $(MAKE) --no-print-directory $(PROJECT).d + $(verbose) $(MAKE) --no-print-directory app-build + +ifeq ($(wildcard src/$(PROJECT_MOD).erl),) +define app_file +{application, '$(PROJECT)', [ + {description, "$(PROJECT_DESCRIPTION)"}, + {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP), + {id$(comma)$(space)"$(1)"}$(comma)) + {modules, [$(call comma_list,$(2))]}, + {registered, []}, + {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]}, + {env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),) +]}. +endef +else +define app_file +{application, '$(PROJECT)', [ + {description, "$(PROJECT_DESCRIPTION)"}, + {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP), + {id$(comma)$(space)"$(1)"}$(comma)) + {modules, [$(call comma_list,$(2))]}, + {registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]}, + {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]}, + {mod, {$(PROJECT_MOD), []}}, + {env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),) +]}. +endef +endif + +app-build: ebin/$(PROJECT).app + $(verbose) : + +# Source files. + +ALL_SRC_FILES := $(sort $(call core_find,src/,*)) + +ERL_FILES := $(filter %.erl,$(ALL_SRC_FILES)) +CORE_FILES := $(filter %.core,$(ALL_SRC_FILES)) + +# ASN.1 files. + +ifneq ($(wildcard asn1/),) +ASN1_FILES = $(sort $(call core_find,asn1/,*.asn1)) +ERL_FILES += $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES)))) + +define compile_asn1 + $(verbose) mkdir -p include/ + $(asn1_verbose) erlc -v -I include/ -o asn1/ +noobj $(ERLC_ASN1_OPTS) $(1) + $(verbose) mv asn1/*.erl src/ + -$(verbose) mv asn1/*.hrl include/ + $(verbose) mv asn1/*.asn1db include/ +endef + +$(PROJECT).d:: $(ASN1_FILES) + $(if $(strip $?),$(call compile_asn1,$?)) +endif + +# SNMP MIB files. + +ifneq ($(wildcard mibs/),) +MIB_FILES = $(sort $(call core_find,mibs/,*.mib)) + +$(PROJECT).d:: $(COMPILE_MIB_FIRST_PATHS) $(MIB_FILES) + $(verbose) mkdir -p include/ priv/mibs/ + $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ -I priv/mibs/ $? + $(mib_verbose) erlc -o include/ -- $(addprefix priv/mibs/,$(patsubst %.mib,%.bin,$(notdir $?))) +endif + +# Leex and Yecc files. + +XRL_FILES := $(filter %.xrl,$(ALL_SRC_FILES)) +XRL_ERL_FILES = $(addprefix src/,$(patsubst %.xrl,%.erl,$(notdir $(XRL_FILES)))) +ERL_FILES += $(XRL_ERL_FILES) + +YRL_FILES := $(filter %.yrl,$(ALL_SRC_FILES)) +YRL_ERL_FILES = $(addprefix src/,$(patsubst %.yrl,%.erl,$(notdir $(YRL_FILES)))) +ERL_FILES += $(YRL_ERL_FILES) + +$(PROJECT).d:: $(XRL_FILES) $(YRL_FILES) + $(if $(strip $?),$(xyrl_verbose) erlc -v -o src/ $(YRL_ERLC_OPTS) $?) + +# Erlang and Core Erlang files. + +define makedep.erl + E = ets:new(makedep, [bag]), + G = digraph:new([acyclic]), + ErlFiles = lists:usort(string:tokens("$(ERL_FILES)", " ")), + DepsDir = "$(call core_native_path,$(DEPS_DIR))", + AppsDir = "$(call core_native_path,$(APPS_DIR))", + DepsDirsSrc = "$(if $(wildcard $(DEPS_DIR)/*/src), $(call core_native_path,$(wildcard $(DEPS_DIR)/*/src)))", + DepsDirsInc = "$(if $(wildcard $(DEPS_DIR)/*/include), $(call core_native_path,$(wildcard $(DEPS_DIR)/*/include)))", + AppsDirsSrc = "$(if $(wildcard $(APPS_DIR)/*/src), $(call core_native_path,$(wildcard $(APPS_DIR)/*/src)))", + AppsDirsInc = "$(if $(wildcard $(APPS_DIR)/*/include), $(call core_native_path,$(wildcard $(APPS_DIR)/*/include)))", + DepsDirs = lists:usort(string:tokens(DepsDirsSrc++DepsDirsInc, " ")), + AppsDirs = lists:usort(string:tokens(AppsDirsSrc++AppsDirsInc, " ")), + Modules = [{list_to_atom(filename:basename(F, ".erl")), F} || F <- ErlFiles], + Add = fun (Mod, Dep) -> + case lists:keyfind(Dep, 1, Modules) of + false -> ok; + {_, DepFile} -> + {_, ModFile} = lists:keyfind(Mod, 1, Modules), + ets:insert(E, {ModFile, DepFile}), + digraph:add_vertex(G, Mod), + digraph:add_vertex(G, Dep), + digraph:add_edge(G, Mod, Dep) + end + end, + AddHd = fun (F, Mod, DepFile) -> + case file:open(DepFile, [read]) of + {error, enoent} -> + ok; + {ok, Fd} -> + {_, ModFile} = lists:keyfind(Mod, 1, Modules), + case ets:match(E, {ModFile, DepFile}) of + [] -> + ets:insert(E, {ModFile, DepFile}), + F(F, Fd, Mod,0); + _ -> ok + end + end + end, + SearchHrl = fun + F(_Hrl, []) -> {error,enoent}; + F(Hrl, [Dir|Dirs]) -> + HrlF = filename:join([Dir,Hrl]), + case filelib:is_file(HrlF) of + true -> + {ok, HrlF}; + false -> F(Hrl,Dirs) + end + end, + Attr = fun + (_F, Mod, behavior, Dep) -> + Add(Mod, Dep); + (_F, Mod, behaviour, Dep) -> + Add(Mod, Dep); + (_F, Mod, compile, {parse_transform, Dep}) -> + Add(Mod, Dep); + (_F, Mod, compile, Opts) when is_list(Opts) -> + case proplists:get_value(parse_transform, Opts) of + undefined -> ok; + Dep -> Add(Mod, Dep) + end; + (F, Mod, include, Hrl) -> + case SearchHrl(Hrl, ["src", "include",AppsDir,DepsDir]++AppsDirs++DepsDirs) of + {ok, FoundHrl} -> AddHd(F, Mod, FoundHrl); + {error, _} -> false + end; + (F, Mod, include_lib, Hrl) -> + case SearchHrl(Hrl, ["src", "include",AppsDir,DepsDir]++AppsDirs++DepsDirs) of + {ok, FoundHrl} -> AddHd(F, Mod, FoundHrl); + {error, _} -> false + end; + (F, Mod, import, {Imp, _}) -> + IsFile = + case lists:keyfind(Imp, 1, Modules) of + false -> false; + {_, FilePath} -> filelib:is_file(FilePath) + end, + case IsFile of + false -> ok; + true -> Add(Mod, Imp) + end; + (_, _, _, _) -> ok + end, + MakeDepend = fun + (F, Fd, Mod, StartLocation) -> + {ok, Filename} = file:pid2name(Fd), + case io:parse_erl_form(Fd, undefined, StartLocation) of + {ok, AbsData, EndLocation} -> + case AbsData of + {attribute, _, Key, Value} -> + Attr(F, Mod, Key, Value), + F(F, Fd, Mod, EndLocation); + _ -> F(F, Fd, Mod, EndLocation) + end; + {eof, _ } -> file:close(Fd); + {error, ErrorDescription } -> + file:close(Fd); + {error, ErrorInfo, ErrorLocation} -> + F(F, Fd, Mod, ErrorLocation) + end, + ok + end, + [begin + Mod = list_to_atom(filename:basename(F, ".erl")), + case file:open(F, [read]) of + {ok, Fd} -> MakeDepend(MakeDepend, Fd, Mod,0); + {error, enoent} -> ok + end + end || F <- ErlFiles], + Depend = sofs:to_external(sofs:relation_to_family(sofs:relation(ets:tab2list(E)))), + CompileFirst = [X || X <- lists:reverse(digraph_utils:topsort(G)), [] =/= digraph:in_neighbours(G, X)], + TargetPath = fun(Target) -> + case lists:keyfind(Target, 1, Modules) of + false -> ""; + {_, DepFile} -> + DirSubname = tl(string:tokens(filename:dirname(DepFile), "/")), + string:join(DirSubname ++ [atom_to_list(Target)], "/") + end + end, + Output0 = [ + "# Generated by Erlang.mk. Edit at your own risk!\n\n", + [[F, "::", [[" ", D] || D <- Deps], "; @touch \$$@\n"] || {F, Deps} <- Depend], + "\nCOMPILE_FIRST +=", [[" ", TargetPath(CF)] || CF <- CompileFirst], "\n" + ], + Output = case "é" of + [233] -> unicode:characters_to_binary(Output0); + _ -> Output0 + end, + ok = file:write_file("$(1)", Output), + halt() +endef + +ifeq ($(if $(NO_MAKEDEP),$(wildcard $(PROJECT).d),),) +$(PROJECT).d:: $(ERL_FILES) $(call core_find,include/,*.hrl) $(MAKEFILE_LIST) + $(makedep_verbose) $(call erlang,$(call makedep.erl,$@)) +endif + +ifeq ($(IS_APP)$(IS_DEP),) +ifneq ($(words $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES)),0) +# Rebuild everything when the Makefile changes. +$(ERLANG_MK_TMP)/last-makefile-change: $(MAKEFILE_LIST) | $(ERLANG_MK_TMP) + $(verbose) if test -f $@; then \ + touch $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES); \ + touch -c $(PROJECT).d; \ + fi + $(verbose) touch $@ + +$(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES):: $(ERLANG_MK_TMP)/last-makefile-change +ebin/$(PROJECT).app:: $(ERLANG_MK_TMP)/last-makefile-change +endif +endif + +$(PROJECT).d:: + $(verbose) : + +include $(wildcard $(PROJECT).d) + +ebin/$(PROJECT).app:: ebin/ + +ebin/: + $(verbose) mkdir -p ebin/ + +define compile_erl + $(erlc_verbose) erlc -v $(if $(IS_DEP),$(filter-out -Werror,$(ERLC_OPTS)),$(ERLC_OPTS)) -o ebin/ \ + -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),$(COMPILE_FIRST_PATHS) $(1)) +endef + +define validate_app_file + case file:consult("ebin/$(PROJECT).app") of + {ok, _} -> halt(); + _ -> halt(1) + end +endef + +ebin/$(PROJECT).app:: $(ERL_FILES) $(CORE_FILES) $(wildcard src/$(PROJECT).app.src) + $(eval FILES_TO_COMPILE := $(filter-out src/$(PROJECT).app.src,$?)) + $(if $(strip $(FILES_TO_COMPILE)),$(call compile_erl,$(FILES_TO_COMPILE))) +# Older git versions do not have the --first-parent flag. Do without in that case. + $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null \ + || git describe --dirty --abbrev=7 --tags --always 2>/dev/null || true)) + $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename \ + $(filter-out $(ERLC_EXCLUDE_PATHS),$(ERL_FILES) $(CORE_FILES) $(BEAM_FILES))))))) +ifeq ($(wildcard src/$(PROJECT).app.src),) + $(app_verbose) printf '$(subst %,%%,$(subst $(newline),\n,$(subst ','\'',$(call app_file,$(GITDESCRIBE),$(MODULES)))))' \ + > ebin/$(PROJECT).app + $(verbose) if ! $(call erlang,$(call validate_app_file)); then \ + echo "The .app file produced is invalid. Please verify the value of PROJECT_ENV." >&2; \ + exit 1; \ + fi +else + $(verbose) if [ -z "$$(grep -e '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \ + echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk documentation for instructions." >&2; \ + exit 1; \ + fi + $(appsrc_verbose) cat src/$(PROJECT).app.src \ + | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \ + | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(subst /,\/,$(GITDESCRIBE))\"}/" \ + > ebin/$(PROJECT).app +endif +ifneq ($(wildcard src/$(PROJECT).appup),) + $(verbose) cp src/$(PROJECT).appup ebin/ +endif + +clean:: clean-app + +clean-app: + $(gen_verbose) rm -rf $(PROJECT).d ebin/ priv/mibs/ $(XRL_ERL_FILES) $(YRL_ERL_FILES) \ + $(addprefix include/,$(patsubst %.mib,%.hrl,$(notdir $(MIB_FILES)))) \ + $(addprefix include/,$(patsubst %.asn1,%.hrl,$(notdir $(ASN1_FILES)))) \ + $(addprefix include/,$(patsubst %.asn1,%.asn1db,$(notdir $(ASN1_FILES)))) \ + $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES)))) + +endif + +# Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu> +# Copyright (c) 2015, Viktor Söderqvist <viktor@zuiderkwast.se> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: docs-deps + +# Configuration. + +ALL_DOC_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DOC_DEPS)) + +# Targets. + +$(foreach dep,$(DOC_DEPS),$(eval $(call dep_target,$(dep)))) + +ifneq ($(SKIP_DEPS),) +doc-deps: +else +doc-deps: $(ALL_DOC_DEPS_DIRS) + $(verbose) set -e; for dep in $(ALL_DOC_DEPS_DIRS) ; do $(MAKE) -C $$dep IS_DEP=1; done +endif + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: rel-deps + +# Configuration. + +ALL_REL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(REL_DEPS)) + +# Targets. + +$(foreach dep,$(REL_DEPS),$(eval $(call dep_target,$(dep)))) + +ifneq ($(SKIP_DEPS),) +rel-deps: +else +rel-deps: $(ALL_REL_DEPS_DIRS) + $(verbose) set -e; for dep in $(ALL_REL_DEPS_DIRS) ; do $(MAKE) -C $$dep; done +endif + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: test-deps test-dir test-build clean-test-dir + +# Configuration. + +TEST_DIR ?= $(CURDIR)/test + +ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS)) + +TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard +TEST_ERLC_OPTS += -DTEST=1 + +# Targets. + +$(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep)))) + +ifneq ($(SKIP_DEPS),) +test-deps: +else +test-deps: $(ALL_TEST_DEPS_DIRS) + $(verbose) set -e; for dep in $(ALL_TEST_DEPS_DIRS) ; do \ + if [ -z "$(strip $(FULL))" ] && [ ! -L $$dep ] && [ -f $$dep/ebin/dep_built ]; then \ + :; \ + else \ + $(MAKE) -C $$dep IS_DEP=1; \ + if [ ! -L $$dep ] && [ -d $$dep/ebin ]; then touch $$dep/ebin/dep_built; fi; \ + fi \ + done +endif + +ifneq ($(wildcard $(TEST_DIR)),) +test-dir: $(ERLANG_MK_TMP)/$(PROJECT).last-testdir-build + @: + +test_erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\ + $(filter %.erl %.core,$(notdir $(FILES_TO_COMPILE)))); +test_erlc_verbose_2 = set -x; +test_erlc_verbose = $(test_erlc_verbose_$(V)) + +define compile_test_erl + $(test_erlc_verbose) erlc -v $(TEST_ERLC_OPTS) -o $(TEST_DIR) \ + -pa ebin/ -I include/ $(1) +endef + +ERL_TEST_FILES = $(call core_find,$(TEST_DIR)/,*.erl) +$(ERLANG_MK_TMP)/$(PROJECT).last-testdir-build: $(ERL_TEST_FILES) $(MAKEFILE_LIST) + $(eval FILES_TO_COMPILE := $(if $(filter $(MAKEFILE_LIST),$?),$(filter $(ERL_TEST_FILES),$^),$?)) + $(if $(strip $(FILES_TO_COMPILE)),$(call compile_test_erl,$(FILES_TO_COMPILE)) && touch $@) +endif + +test-build:: IS_TEST=1 +test-build:: ERLC_OPTS=$(TEST_ERLC_OPTS) +test-build:: $(if $(wildcard src),$(if $(wildcard ebin/test),,clean)) $(if $(IS_APP),,deps test-deps) +# We already compiled everything when IS_APP=1. +ifndef IS_APP +ifneq ($(wildcard src),) + $(verbose) $(MAKE) --no-print-directory $(PROJECT).d ERLC_OPTS="$(call escape_dquotes,$(TEST_ERLC_OPTS))" + $(verbose) $(MAKE) --no-print-directory app-build ERLC_OPTS="$(call escape_dquotes,$(TEST_ERLC_OPTS))" + $(gen_verbose) touch ebin/test +endif +ifneq ($(wildcard $(TEST_DIR)),) + $(verbose) $(MAKE) --no-print-directory test-dir ERLC_OPTS="$(call escape_dquotes,$(TEST_ERLC_OPTS))" +endif +endif + +# Roughly the same as test-build, but when IS_APP=1. +# We only care about compiling the current application. +ifdef IS_APP +test-build-app:: ERLC_OPTS=$(TEST_ERLC_OPTS) +test-build-app:: deps test-deps +ifneq ($(wildcard src),) + $(verbose) $(MAKE) --no-print-directory $(PROJECT).d ERLC_OPTS="$(call escape_dquotes,$(TEST_ERLC_OPTS))" + $(verbose) $(MAKE) --no-print-directory app-build ERLC_OPTS="$(call escape_dquotes,$(TEST_ERLC_OPTS))" + $(gen_verbose) touch ebin/test +endif +ifneq ($(wildcard $(TEST_DIR)),) + $(verbose) $(MAKE) --no-print-directory test-dir ERLC_OPTS="$(call escape_dquotes,$(TEST_ERLC_OPTS))" +endif +endif + +clean:: clean-test-dir + +clean-test-dir: +ifneq ($(wildcard $(TEST_DIR)/*.beam),) + $(gen_verbose) rm -f $(TEST_DIR)/*.beam $(ERLANG_MK_TMP)/$(PROJECT).last-testdir-build +endif + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: rebar.config + +# We strip out -Werror because we don't want to fail due to +# warnings when used as a dependency. + +compat_prepare_erlc_opts = $(shell echo "$1" | sed 's/, */,/g') + +define compat_convert_erlc_opts +$(if $(filter-out -Werror,$1),\ + $(if $(findstring +,$1),\ + $(shell echo $1 | cut -b 2-))) +endef + +define compat_erlc_opts_to_list +[$(call comma_list,$(foreach o,$(call compat_prepare_erlc_opts,$1),$(call compat_convert_erlc_opts,$o)))] +endef + +define compat_rebar_config +{deps, [ +$(call comma_list,$(foreach d,$(DEPS),\ + $(if $(filter hex,$(call dep_fetch,$d)),\ + {$(call dep_name,$d)$(comma)"$(call dep_repo,$d)"},\ + {$(call dep_name,$d)$(comma)".*"$(comma){git,"$(call dep_repo,$d)"$(comma)"$(call dep_commit,$d)"}}))) +]}. +{erl_opts, $(call compat_erlc_opts_to_list,$(ERLC_OPTS))}. +endef + +rebar.config: + $(gen_verbose) $(call core_render,compat_rebar_config,rebar.config) + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +ifeq ($(filter asciideck,$(DEPS) $(DOC_DEPS)),asciideck) + +.PHONY: asciidoc asciidoc-guide asciidoc-manual install-asciidoc distclean-asciidoc-guide distclean-asciidoc-manual + +# Core targets. + +docs:: asciidoc + +distclean:: distclean-asciidoc-guide distclean-asciidoc-manual + +# Plugin-specific targets. + +asciidoc: asciidoc-guide asciidoc-manual + +# User guide. + +ifeq ($(wildcard doc/src/guide/book.asciidoc),) +asciidoc-guide: +else +asciidoc-guide: distclean-asciidoc-guide doc-deps + a2x -v -f pdf doc/src/guide/book.asciidoc && mv doc/src/guide/book.pdf doc/guide.pdf + a2x -v -f chunked doc/src/guide/book.asciidoc && mv doc/src/guide/book.chunked/ doc/html/ + +distclean-asciidoc-guide: + $(gen_verbose) rm -rf doc/html/ doc/guide.pdf +endif + +# Man pages. + +ASCIIDOC_MANUAL_FILES := $(wildcard doc/src/manual/*.asciidoc) + +ifeq ($(ASCIIDOC_MANUAL_FILES),) +asciidoc-manual: +else + +# Configuration. + +MAN_INSTALL_PATH ?= /usr/local/share/man +MAN_SECTIONS ?= 3 7 +MAN_PROJECT ?= $(shell echo $(PROJECT) | sed 's/^./\U&\E/') +MAN_VERSION ?= $(PROJECT_VERSION) + +# Plugin-specific targets. + +define asciidoc2man.erl +try + [begin + io:format(" ADOC ~s~n", [F]), + ok = asciideck:to_manpage(asciideck:parse_file(F), #{ + compress => gzip, + outdir => filename:dirname(F), + extra2 => "$(MAN_PROJECT) $(MAN_VERSION)", + extra3 => "$(MAN_PROJECT) Function Reference" + }) + end || F <- [$(shell echo $(addprefix $(comma)\",$(addsuffix \",$1)) | sed 's/^.//')]], + halt(0) +catch C:E -> + io:format("Exception ~p:~p~nStacktrace: ~p~n", [C, E, erlang:get_stacktrace()]), + halt(1) +end. +endef + +asciidoc-manual:: doc-deps + +asciidoc-manual:: $(ASCIIDOC_MANUAL_FILES) + $(gen_verbose) $(call erlang,$(call asciidoc2man.erl,$?)) + $(verbose) $(foreach s,$(MAN_SECTIONS),mkdir -p doc/man$s/ && mv doc/src/manual/*.$s.gz doc/man$s/;) + +install-docs:: install-asciidoc + +install-asciidoc: asciidoc-manual + $(foreach s,$(MAN_SECTIONS),\ + mkdir -p $(MAN_INSTALL_PATH)/man$s/ && \ + install -g `id -g` -o `id -u` -m 0644 doc/man$s/*.gz $(MAN_INSTALL_PATH)/man$s/;) + +distclean-asciidoc-manual: + $(gen_verbose) rm -rf $(addprefix doc/man,$(MAN_SECTIONS)) +endif +endif + +# Copyright (c) 2014-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates + +# Core targets. + +help:: + $(verbose) printf "%s\n" "" \ + "Bootstrap targets:" \ + " bootstrap Generate a skeleton of an OTP application" \ + " bootstrap-lib Generate a skeleton of an OTP library" \ + " bootstrap-rel Generate the files needed to build a release" \ + " new-app in=NAME Create a new local OTP application NAME" \ + " new-lib in=NAME Create a new local OTP library NAME" \ + " new t=TPL n=NAME Generate a module NAME based on the template TPL" \ + " new t=T n=N in=APP Generate a module NAME based on the template TPL in APP" \ + " list-templates List available templates" + +# Bootstrap templates. + +define bs_appsrc +{application, $p, [ + {description, ""}, + {vsn, "0.1.0"}, + {id, "git"}, + {modules, []}, + {registered, []}, + {applications, [ + kernel, + stdlib + ]}, + {mod, {$p_app, []}}, + {env, []} +]}. +endef + +define bs_appsrc_lib +{application, $p, [ + {description, ""}, + {vsn, "0.1.0"}, + {id, "git"}, + {modules, []}, + {registered, []}, + {applications, [ + kernel, + stdlib + ]} +]}. +endef + +# To prevent autocompletion issues with ZSH, we add "include erlang.mk" +# separately during the actual bootstrap. +define bs_Makefile +PROJECT = $p +PROJECT_DESCRIPTION = New project +PROJECT_VERSION = 0.1.0 +$(if $(SP), +# Whitespace to be used when creating files from templates. +SP = $(SP) +) +endef + +define bs_apps_Makefile +PROJECT = $p +PROJECT_DESCRIPTION = New project +PROJECT_VERSION = 0.1.0 +$(if $(SP), +# Whitespace to be used when creating files from templates. +SP = $(SP) +) +# Make sure we know where the applications are located. +ROOT_DIR ?= $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(APPS_DIR)/app) +APPS_DIR ?= .. +DEPS_DIR ?= $(call core_relpath,$(DEPS_DIR),$(APPS_DIR)/app) + +include $$(ROOT_DIR)/erlang.mk +endef + +define bs_app +-module($p_app). +-behaviour(application). + +-export([start/2]). +-export([stop/1]). + +start(_Type, _Args) -> + $p_sup:start_link(). + +stop(_State) -> + ok. +endef + +define bs_relx_config +{release, {$p_release, "1"}, [$p, sasl, runtime_tools]}. +{extended_start_script, true}. +{sys_config, "config/sys.config"}. +{vm_args, "config/vm.args"}. +endef + +define bs_sys_config +[ +]. +endef + +define bs_vm_args +-name $p@127.0.0.1 +-setcookie $p +-heart +endef + +# Normal templates. + +define tpl_supervisor +-module($(n)). +-behaviour(supervisor). + +-export([start_link/0]). +-export([init/1]). + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +init([]) -> + Procs = [], + {ok, {{one_for_one, 1, 5}, Procs}}. +endef + +define tpl_gen_server +-module($(n)). +-behaviour(gen_server). + +%% API. +-export([start_link/0]). + +%% gen_server. +-export([init/1]). +-export([handle_call/3]). +-export([handle_cast/2]). +-export([handle_info/2]). +-export([terminate/2]). +-export([code_change/3]). + +-record(state, { +}). + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_server:start_link(?MODULE, [], []). + +%% gen_server. + +init([]) -> + {ok, #state{}}. + +handle_call(_Request, _From, State) -> + {reply, ignored, State}. + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(_Info, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. +endef + +define tpl_module +-module($(n)). +-export([]). +endef + +define tpl_cowboy_http +-module($(n)). +-behaviour(cowboy_http_handler). + +-export([init/3]). +-export([handle/2]). +-export([terminate/3]). + +-record(state, { +}). + +init(_, Req, _Opts) -> + {ok, Req, #state{}}. + +handle(Req, State=#state{}) -> + {ok, Req2} = cowboy_req:reply(200, Req), + {ok, Req2, State}. + +terminate(_Reason, _Req, _State) -> + ok. +endef + +define tpl_gen_fsm +-module($(n)). +-behaviour(gen_fsm). + +%% API. +-export([start_link/0]). + +%% gen_fsm. +-export([init/1]). +-export([state_name/2]). +-export([handle_event/3]). +-export([state_name/3]). +-export([handle_sync_event/4]). +-export([handle_info/3]). +-export([terminate/3]). +-export([code_change/4]). + +-record(state, { +}). + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_fsm:start_link(?MODULE, [], []). + +%% gen_fsm. + +init([]) -> + {ok, state_name, #state{}}. + +state_name(_Event, StateData) -> + {next_state, state_name, StateData}. + +handle_event(_Event, StateName, StateData) -> + {next_state, StateName, StateData}. + +state_name(_Event, _From, StateData) -> + {reply, ignored, state_name, StateData}. + +handle_sync_event(_Event, _From, StateName, StateData) -> + {reply, ignored, StateName, StateData}. + +handle_info(_Info, StateName, StateData) -> + {next_state, StateName, StateData}. + +terminate(_Reason, _StateName, _StateData) -> + ok. + +code_change(_OldVsn, StateName, StateData, _Extra) -> + {ok, StateName, StateData}. +endef + +define tpl_gen_statem +-module($(n)). +-behaviour(gen_statem). + +%% API. +-export([start_link/0]). + +%% gen_statem. +-export([callback_mode/0]). +-export([init/1]). +-export([state_name/3]). +-export([handle_event/4]). +-export([terminate/3]). +-export([code_change/4]). + +-record(state, { +}). + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_statem:start_link(?MODULE, [], []). + +%% gen_statem. + +callback_mode() -> + state_functions. + +init([]) -> + {ok, state_name, #state{}}. + +state_name(_EventType, _EventData, StateData) -> + {next_state, state_name, StateData}. + +handle_event(_EventType, _EventData, StateName, StateData) -> + {next_state, StateName, StateData}. + +terminate(_Reason, _StateName, _StateData) -> + ok. + +code_change(_OldVsn, StateName, StateData, _Extra) -> + {ok, StateName, StateData}. +endef + +define tpl_cowboy_loop +-module($(n)). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +-record(state, { +}). + +init(_, Req, _Opts) -> + {loop, Req, #state{}, 5000, hibernate}. + +info(_Info, Req, State) -> + {loop, Req, State, hibernate}. + +terminate(_Reason, _Req, _State) -> + ok. +endef + +define tpl_cowboy_rest +-module($(n)). + +-export([init/3]). +-export([content_types_provided/2]). +-export([get_html/2]). + +init(_, _Req, _Opts) -> + {upgrade, protocol, cowboy_rest}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"html">>, '*'}, get_html}], Req, State}. + +get_html(Req, State) -> + {<<"<html><body>This is REST!</body></html>">>, Req, State}. +endef + +define tpl_cowboy_ws +-module($(n)). +-behaviour(cowboy_websocket_handler). + +-export([init/3]). +-export([websocket_init/3]). +-export([websocket_handle/3]). +-export([websocket_info/3]). +-export([websocket_terminate/3]). + +-record(state, { +}). + +init(_, _, _) -> + {upgrade, protocol, cowboy_websocket}. + +websocket_init(_, Req, _Opts) -> + Req2 = cowboy_req:compact(Req), + {ok, Req2, #state{}}. + +websocket_handle({text, Data}, Req, State) -> + {reply, {text, Data}, Req, State}; +websocket_handle({binary, Data}, Req, State) -> + {reply, {binary, Data}, Req, State}; +websocket_handle(_Frame, Req, State) -> + {ok, Req, State}. + +websocket_info(_Info, Req, State) -> + {ok, Req, State}. + +websocket_terminate(_Reason, _Req, _State) -> + ok. +endef + +define tpl_ranch_protocol +-module($(n)). +-behaviour(ranch_protocol). + +-export([start_link/4]). +-export([init/4]). + +-type opts() :: []. +-export_type([opts/0]). + +-record(state, { + socket :: inet:socket(), + transport :: module() +}). + +start_link(Ref, Socket, Transport, Opts) -> + Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]), + {ok, Pid}. + +-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok. +init(Ref, Socket, Transport, _Opts) -> + ok = ranch:accept_ack(Ref), + loop(#state{socket=Socket, transport=Transport}). + +loop(State) -> + loop(State). +endef + +# Plugin-specific targets. + +ifndef WS +ifdef SP +WS = $(subst a,,a $(wordlist 1,$(SP),a a a a a a a a a a a a a a a a a a a a)) +else +WS = $(tab) +endif +endif + +bootstrap: +ifneq ($(wildcard src/),) + $(error Error: src/ directory already exists) +endif + $(eval p := $(PROJECT)) + $(if $(shell echo $p | LC_ALL=C grep -x "[a-z0-9_]*"),,\ + $(error Error: Invalid characters in the application name)) + $(eval n := $(PROJECT)_sup) + $(verbose) $(call core_render,bs_Makefile,Makefile) + $(verbose) echo "include erlang.mk" >> Makefile + $(verbose) mkdir src/ +ifdef LEGACY + $(verbose) $(call core_render,bs_appsrc,src/$(PROJECT).app.src) +endif + $(verbose) $(call core_render,bs_app,src/$(PROJECT)_app.erl) + $(verbose) $(call core_render,tpl_supervisor,src/$(PROJECT)_sup.erl) + +bootstrap-lib: +ifneq ($(wildcard src/),) + $(error Error: src/ directory already exists) +endif + $(eval p := $(PROJECT)) + $(if $(shell echo $p | LC_ALL=C grep -x "[a-z0-9_]*"),,\ + $(error Error: Invalid characters in the application name)) + $(verbose) $(call core_render,bs_Makefile,Makefile) + $(verbose) echo "include erlang.mk" >> Makefile + $(verbose) mkdir src/ +ifdef LEGACY + $(verbose) $(call core_render,bs_appsrc_lib,src/$(PROJECT).app.src) +endif + +bootstrap-rel: +ifneq ($(wildcard relx.config),) + $(error Error: relx.config already exists) +endif +ifneq ($(wildcard config/),) + $(error Error: config/ directory already exists) +endif + $(eval p := $(PROJECT)) + $(verbose) $(call core_render,bs_relx_config,relx.config) + $(verbose) mkdir config/ + $(verbose) $(call core_render,bs_sys_config,config/sys.config) + $(verbose) $(call core_render,bs_vm_args,config/vm.args) + +new-app: +ifndef in + $(error Usage: $(MAKE) new-app in=APP) +endif +ifneq ($(wildcard $(APPS_DIR)/$in),) + $(error Error: Application $in already exists) +endif + $(eval p := $(in)) + $(if $(shell echo $p | LC_ALL=C grep -x "[a-z0-9_]*"),,\ + $(error Error: Invalid characters in the application name)) + $(eval n := $(in)_sup) + $(verbose) mkdir -p $(APPS_DIR)/$p/src/ + $(verbose) $(call core_render,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile) +ifdef LEGACY + $(verbose) $(call core_render,bs_appsrc,$(APPS_DIR)/$p/src/$p.app.src) +endif + $(verbose) $(call core_render,bs_app,$(APPS_DIR)/$p/src/$p_app.erl) + $(verbose) $(call core_render,tpl_supervisor,$(APPS_DIR)/$p/src/$p_sup.erl) + +new-lib: +ifndef in + $(error Usage: $(MAKE) new-lib in=APP) +endif +ifneq ($(wildcard $(APPS_DIR)/$in),) + $(error Error: Application $in already exists) +endif + $(eval p := $(in)) + $(if $(shell echo $p | LC_ALL=C grep -x "[a-z0-9_]*"),,\ + $(error Error: Invalid characters in the application name)) + $(verbose) mkdir -p $(APPS_DIR)/$p/src/ + $(verbose) $(call core_render,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile) +ifdef LEGACY + $(verbose) $(call core_render,bs_appsrc_lib,$(APPS_DIR)/$p/src/$p.app.src) +endif + +new: +ifeq ($(wildcard src/)$(in),) + $(error Error: src/ directory does not exist) +endif +ifndef t + $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP]) +endif +ifndef n + $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP]) +endif +ifdef in + $(verbose) $(call core_render,tpl_$(t),$(APPS_DIR)/$(in)/src/$(n).erl) +else + $(verbose) $(call core_render,tpl_$(t),src/$(n).erl) +endif + +list-templates: + $(verbose) @echo Available templates: + $(verbose) printf " %s\n" $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES)))) + +# Copyright (c) 2014-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: clean-c_src distclean-c_src-env + +# Configuration. + +C_SRC_DIR ?= $(CURDIR)/c_src +C_SRC_ENV ?= $(C_SRC_DIR)/env.mk +C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT) +C_SRC_TYPE ?= shared + +# System type and C compiler/flags. + +ifeq ($(PLATFORM),msys2) + C_SRC_OUTPUT_EXECUTABLE_EXTENSION ?= .exe + C_SRC_OUTPUT_SHARED_EXTENSION ?= .dll +else + C_SRC_OUTPUT_EXECUTABLE_EXTENSION ?= + C_SRC_OUTPUT_SHARED_EXTENSION ?= .so +endif + +ifeq ($(C_SRC_TYPE),shared) + C_SRC_OUTPUT_FILE = $(C_SRC_OUTPUT)$(C_SRC_OUTPUT_SHARED_EXTENSION) +else + C_SRC_OUTPUT_FILE = $(C_SRC_OUTPUT)$(C_SRC_OUTPUT_EXECUTABLE_EXTENSION) +endif + +ifeq ($(PLATFORM),msys2) +# We hardcode the compiler used on MSYS2. The default CC=cc does +# not produce working code. The "gcc" MSYS2 package also doesn't. + CC = /mingw64/bin/gcc + export CC + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +else ifeq ($(PLATFORM),darwin) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -arch x86_64 -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -arch x86_64 -Wall + LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress +else ifeq ($(PLATFORM),freebsd) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +else ifeq ($(PLATFORM),linux) + CC ?= gcc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +endif + +ifneq ($(PLATFORM),msys2) + CFLAGS += -fPIC + CXXFLAGS += -fPIC +endif + +CFLAGS += -I"$(ERTS_INCLUDE_DIR)" -I"$(ERL_INTERFACE_INCLUDE_DIR)" +CXXFLAGS += -I"$(ERTS_INCLUDE_DIR)" -I"$(ERL_INTERFACE_INCLUDE_DIR)" + +LDLIBS += -L"$(ERL_INTERFACE_LIB_DIR)" -lei + +# Verbosity. + +c_verbose_0 = @echo " C " $(filter-out $(notdir $(MAKEFILE_LIST) $(C_SRC_ENV)),$(^F)); +c_verbose = $(c_verbose_$(V)) + +cpp_verbose_0 = @echo " CPP " $(filter-out $(notdir $(MAKEFILE_LIST) $(C_SRC_ENV)),$(^F)); +cpp_verbose = $(cpp_verbose_$(V)) + +link_verbose_0 = @echo " LD " $(@F); +link_verbose = $(link_verbose_$(V)) + +# Targets. + +ifeq ($(wildcard $(C_SRC_DIR)),) +else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),) +app:: app-c_src + +test-build:: app-c_src + +app-c_src: + $(MAKE) -C $(C_SRC_DIR) + +clean:: + $(MAKE) -C $(C_SRC_DIR) clean + +else + +ifeq ($(SOURCES),) +SOURCES := $(sort $(foreach pat,*.c *.C *.cc *.cpp,$(call core_find,$(C_SRC_DIR)/,$(pat)))) +endif +OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) + +COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c +COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c + +app:: $(C_SRC_ENV) $(C_SRC_OUTPUT_FILE) + +test-build:: $(C_SRC_ENV) $(C_SRC_OUTPUT_FILE) + +$(C_SRC_OUTPUT_FILE): $(OBJECTS) + $(verbose) mkdir -p $(dir $@) + $(link_verbose) $(CC) $(OBJECTS) \ + $(LDFLAGS) $(if $(filter $(C_SRC_TYPE),shared),-shared) $(LDLIBS) \ + -o $(C_SRC_OUTPUT_FILE) + +$(OBJECTS): $(MAKEFILE_LIST) $(C_SRC_ENV) + +%.o: %.c + $(COMPILE_C) $(OUTPUT_OPTION) $< + +%.o: %.cc + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +%.o: %.C + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +%.o: %.cpp + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +clean:: clean-c_src + +clean-c_src: + $(gen_verbose) rm -f $(C_SRC_OUTPUT_FILE) $(OBJECTS) + +endif + +ifneq ($(wildcard $(C_SRC_DIR)),) +ERL_ERTS_DIR = $(shell $(ERL) -eval 'io:format("~s~n", [code:lib_dir(erts)]), halt().') + +$(C_SRC_ENV): + $(verbose) $(ERL) -eval "file:write_file(\"$(call core_native_path,$(C_SRC_ENV))\", \ + io_lib:format( \ + \"# Generated by Erlang.mk. Edit at your own risk!~n~n\" \ + \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \ + \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \ + \"ERL_INTERFACE_LIB_DIR ?= ~s~n\" \ + \"ERTS_DIR ?= $(ERL_ERTS_DIR)~n\", \ + [code:root_dir(), erlang:system_info(version), \ + code:lib_dir(erl_interface, include), \ + code:lib_dir(erl_interface, lib)])), \ + halt()." + +distclean:: distclean-c_src-env + +distclean-c_src-env: + $(gen_verbose) rm -f $(C_SRC_ENV) + +-include $(C_SRC_ENV) + +ifneq ($(ERL_ERTS_DIR),$(ERTS_DIR)) +$(shell rm -f $(C_SRC_ENV)) +endif +endif + +# Templates. + +define bs_c_nif +#include "erl_nif.h" + +static int loads = 0; + +static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) +{ + /* Initialize private data. */ + *priv_data = NULL; + + loads++; + + return 0; +} + +static int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info) +{ + /* Convert the private data to the new version. */ + *priv_data = *old_priv_data; + + loads++; + + return 0; +} + +static void unload(ErlNifEnv* env, void* priv_data) +{ + if (loads == 1) { + /* Destroy the private data. */ + } + + loads--; +} + +static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{ + if (enif_is_atom(env, argv[0])) { + return enif_make_tuple2(env, + enif_make_atom(env, "hello"), + argv[0]); + } + + return enif_make_tuple2(env, + enif_make_atom(env, "error"), + enif_make_atom(env, "badarg")); +} + +static ErlNifFunc nif_funcs[] = { + {"hello", 1, hello} +}; + +ERL_NIF_INIT($n, nif_funcs, load, NULL, upgrade, unload) +endef + +define bs_erl_nif +-module($n). + +-export([hello/1]). + +-on_load(on_load/0). +on_load() -> + PrivDir = case code:priv_dir(?MODULE) of + {error, _} -> + AppPath = filename:dirname(filename:dirname(code:which(?MODULE))), + filename:join(AppPath, "priv"); + Path -> + Path + end, + erlang:load_nif(filename:join(PrivDir, atom_to_list(?MODULE)), 0). + +hello(_) -> + erlang:nif_error({not_loaded, ?MODULE}). +endef + +new-nif: +ifneq ($(wildcard $(C_SRC_DIR)/$n.c),) + $(error Error: $(C_SRC_DIR)/$n.c already exists) +endif +ifneq ($(wildcard src/$n.erl),) + $(error Error: src/$n.erl already exists) +endif +ifndef n + $(error Usage: $(MAKE) new-nif n=NAME [in=APP]) +endif +ifdef in + $(verbose) $(MAKE) -C $(APPS_DIR)/$(in)/ new-nif n=$n in= +else + $(verbose) mkdir -p $(C_SRC_DIR) src/ + $(verbose) $(call core_render,bs_c_nif,$(C_SRC_DIR)/$n.c) + $(verbose) $(call core_render,bs_erl_nif,src/$n.erl) +endif + +# Copyright (c) 2015-2017, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: ci ci-prepare ci-setup + +CI_OTP ?= +CI_HIPE ?= +CI_ERLLVM ?= + +ifeq ($(CI_VM),native) +ERLC_OPTS += +native +TEST_ERLC_OPTS += +native +else ifeq ($(CI_VM),erllvm) +ERLC_OPTS += +native +'{hipe, [to_llvm]}' +TEST_ERLC_OPTS += +native +'{hipe, [to_llvm]}' +endif + +ifeq ($(strip $(CI_OTP) $(CI_HIPE) $(CI_ERLLVM)),) +ci:: +else + +ci:: $(addprefix ci-,$(CI_OTP) $(addsuffix -native,$(CI_HIPE)) $(addsuffix -erllvm,$(CI_ERLLVM))) + +ci-prepare: $(addprefix $(KERL_INSTALL_DIR)/,$(CI_OTP) $(addsuffix -native,$(CI_HIPE))) + +ci-setup:: + $(verbose) : + +ci-extra:: + $(verbose) : + +ci_verbose_0 = @echo " CI " $(1); +ci_verbose = $(ci_verbose_$(V)) + +define ci_target +ci-$1: $(KERL_INSTALL_DIR)/$2 + $(verbose) $(MAKE) --no-print-directory clean + $(ci_verbose) \ + PATH="$(KERL_INSTALL_DIR)/$2/bin:$(PATH)" \ + CI_OTP_RELEASE="$1" \ + CT_OPTS="-label $1" \ + CI_VM="$3" \ + $(MAKE) ci-setup tests + $(verbose) $(MAKE) --no-print-directory ci-extra +endef + +$(foreach otp,$(CI_OTP),$(eval $(call ci_target,$(otp),$(otp),otp))) +$(foreach otp,$(CI_HIPE),$(eval $(call ci_target,$(otp)-native,$(otp)-native,native))) +$(foreach otp,$(CI_ERLLVM),$(eval $(call ci_target,$(otp)-erllvm,$(otp)-native,erllvm))) + +$(foreach otp,$(filter-out $(ERLANG_OTP),$(CI_OTP)),$(eval $(call kerl_otp_target,$(otp)))) +$(foreach otp,$(filter-out $(ERLANG_HIPE),$(sort $(CI_HIPE) $(CI_ERLLLVM))),$(eval $(call kerl_hipe_target,$(otp)))) + +help:: + $(verbose) printf "%s\n" "" \ + "Continuous Integration targets:" \ + " ci Run '$(MAKE) tests' on all configured Erlang versions." \ + "" \ + "The CI_OTP variable must be defined with the Erlang versions" \ + "that must be tested. For example: CI_OTP = OTP-17.3.4 OTP-17.5.3" + +endif + +# Copyright (c) 2020, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +ifdef CONCUERROR_TESTS + +.PHONY: concuerror distclean-concuerror + +# Configuration + +CONCUERROR_LOGS_DIR ?= $(CURDIR)/logs +CONCUERROR_OPTS ?= + +# Core targets. + +check:: concuerror + +ifndef KEEP_LOGS +distclean:: distclean-concuerror +endif + +# Plugin-specific targets. + +$(ERLANG_MK_TMP)/Concuerror/bin/concuerror: | $(ERLANG_MK_TMP) + $(verbose) git clone https://github.com/parapluu/Concuerror $(ERLANG_MK_TMP)/Concuerror + $(verbose) $(MAKE) -C $(ERLANG_MK_TMP)/Concuerror + +$(CONCUERROR_LOGS_DIR): + $(verbose) mkdir -p $(CONCUERROR_LOGS_DIR) + +define concuerror_html_report +<!DOCTYPE html> +<html lang="en"> +<head> +<meta charset="utf-8"> +<title>Concuerror HTML report</title> +</head> +<body> +<h1>Concuerror HTML report</h1> +<p>Generated on $(concuerror_date)</p> +<ul> +$(foreach t,$(concuerror_targets),<li><a href="$(t).txt">$(t)</a></li>) +</ul> +</body> +</html> +endef + +concuerror: $(addprefix concuerror-,$(subst :,-,$(CONCUERROR_TESTS))) + $(eval concuerror_date := $(shell date)) + $(eval concuerror_targets := $^) + $(verbose) $(call core_render,concuerror_html_report,$(CONCUERROR_LOGS_DIR)/concuerror.html) + +define concuerror_target +.PHONY: concuerror-$1-$2 + +concuerror-$1-$2: test-build | $(ERLANG_MK_TMP)/Concuerror/bin/concuerror $(CONCUERROR_LOGS_DIR) + $(ERLANG_MK_TMP)/Concuerror/bin/concuerror \ + --pa $(CURDIR)/ebin --pa $(TEST_DIR) \ + -o $(CONCUERROR_LOGS_DIR)/concuerror-$1-$2.txt \ + $$(CONCUERROR_OPTS) -m $1 -t $2 +endef + +$(foreach test,$(CONCUERROR_TESTS),$(eval $(call concuerror_target,$(firstword $(subst :, ,$(test))),$(lastword $(subst :, ,$(test)))))) + +distclean-concuerror: + $(gen_verbose) rm -rf $(CONCUERROR_LOGS_DIR) + +endif + +# Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: distclean-edoc edoc + +# Configuration. + +EDOC_OPTS ?= +EDOC_SRC_DIRS ?= +EDOC_OUTPUT ?= doc + +define edoc.erl + SrcPaths = lists:foldl(fun(P, Acc) -> + filelib:wildcard(atom_to_list(P) ++ "/{src,c_src}") ++ Acc + end, [], [$(call comma_list,$(patsubst %,'%',$(call core_native_path,$(EDOC_SRC_DIRS))))]), + DefaultOpts = [{dir, "$(EDOC_OUTPUT)"}, {source_path, SrcPaths}, {subpackages, false}], + edoc:application($(1), ".", [$(2)] ++ DefaultOpts), + halt(0). +endef + +# Core targets. + +ifneq ($(strip $(EDOC_SRC_DIRS)$(wildcard doc/overview.edoc)),) +docs:: edoc +endif + +distclean:: distclean-edoc + +# Plugin-specific targets. + +edoc: distclean-edoc doc-deps + $(gen_verbose) $(call erlang,$(call edoc.erl,$(PROJECT),$(EDOC_OPTS))) + +distclean-edoc: + $(gen_verbose) rm -f $(EDOC_OUTPUT)/*.css $(EDOC_OUTPUT)/*.html $(EDOC_OUTPUT)/*.png $(EDOC_OUTPUT)/edoc-info + +# Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +# Configuration. + +DTL_FULL_PATH ?= +DTL_PATH ?= templates/ +DTL_PREFIX ?= +DTL_SUFFIX ?= _dtl +DTL_OPTS ?= + +# Verbosity. + +dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F)); +dtl_verbose = $(dtl_verbose_$(V)) + +# Core targets. + +DTL_PATH := $(abspath $(DTL_PATH)) +DTL_FILES := $(sort $(call core_find,$(DTL_PATH),*.dtl)) + +ifneq ($(DTL_FILES),) + +DTL_NAMES = $(addprefix $(DTL_PREFIX),$(addsuffix $(DTL_SUFFIX),$(DTL_FILES:$(DTL_PATH)/%.dtl=%))) +DTL_MODULES = $(if $(DTL_FULL_PATH),$(subst /,_,$(DTL_NAMES)),$(notdir $(DTL_NAMES))) +BEAM_FILES += $(addsuffix .beam,$(addprefix ebin/,$(DTL_MODULES))) + +ifneq ($(words $(DTL_FILES)),0) +# Rebuild templates when the Makefile changes. +$(ERLANG_MK_TMP)/last-makefile-change-erlydtl: $(MAKEFILE_LIST) | $(ERLANG_MK_TMP) + $(verbose) if test -f $@; then \ + touch $(DTL_FILES); \ + fi + $(verbose) touch $@ + +ebin/$(PROJECT).app:: $(ERLANG_MK_TMP)/last-makefile-change-erlydtl +endif + +define erlydtl_compile.erl + [begin + Module0 = case "$(strip $(DTL_FULL_PATH))" of + "" -> + filename:basename(F, ".dtl"); + _ -> + "$(call core_native_path,$(DTL_PATH))/" ++ F2 = filename:rootname(F, ".dtl"), + re:replace(F2, "/", "_", [{return, list}, global]) + end, + Module = list_to_atom("$(DTL_PREFIX)" ++ string:to_lower(Module0) ++ "$(DTL_SUFFIX)"), + case erlydtl:compile(F, Module, [$(DTL_OPTS)] ++ [{out_dir, "ebin/"}, return_errors]) of + ok -> ok; + {ok, _} -> ok + end + end || F <- string:tokens("$(1)", " ")], + halt(). +endef + +ebin/$(PROJECT).app:: $(DTL_FILES) | ebin/ + $(if $(strip $?),\ + $(dtl_verbose) $(call erlang,$(call erlydtl_compile.erl,$(call core_native_path,$?)),\ + -pa ebin/)) + +endif + +# Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu> +# Copyright (c) 2014, Dave Cottlehuber <dch@skunkwerks.at> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: distclean-escript escript escript-zip + +# Configuration. + +ESCRIPT_NAME ?= $(PROJECT) +ESCRIPT_FILE ?= $(ESCRIPT_NAME) + +ESCRIPT_SHEBANG ?= /usr/bin/env escript +ESCRIPT_COMMENT ?= This is an -*- erlang -*- file +ESCRIPT_EMU_ARGS ?= -escript main $(ESCRIPT_NAME) + +ESCRIPT_ZIP ?= 7z a -tzip -mx=9 -mtc=off $(if $(filter-out 0,$(V)),,> /dev/null) +ESCRIPT_ZIP_FILE ?= $(ERLANG_MK_TMP)/escript.zip + +# Core targets. + +distclean:: distclean-escript + +help:: + $(verbose) printf "%s\n" "" \ + "Escript targets:" \ + " escript Build an executable escript archive" \ + +# Plugin-specific targets. + +escript-zip:: FULL=1 +escript-zip:: deps app + $(verbose) mkdir -p $(dir $(ESCRIPT_ZIP)) + $(verbose) rm -f $(ESCRIPT_ZIP_FILE) + $(gen_verbose) cd .. && $(ESCRIPT_ZIP) $(ESCRIPT_ZIP_FILE) $(PROJECT)/ebin/* +ifneq ($(DEPS),) + $(verbose) cd $(DEPS_DIR) && $(ESCRIPT_ZIP) $(ESCRIPT_ZIP_FILE) \ + $(subst $(DEPS_DIR)/,,$(addsuffix /*,$(wildcard \ + $(addsuffix /ebin,$(shell cat $(ERLANG_MK_TMP)/deps.log))))) +endif + +escript:: escript-zip + $(gen_verbose) printf "%s\n" \ + "#!$(ESCRIPT_SHEBANG)" \ + "%% $(ESCRIPT_COMMENT)" \ + "%%! $(ESCRIPT_EMU_ARGS)" > $(ESCRIPT_FILE) + $(verbose) cat $(ESCRIPT_ZIP_FILE) >> $(ESCRIPT_FILE) + $(verbose) chmod +x $(ESCRIPT_FILE) + +distclean-escript: + $(gen_verbose) rm -f $(ESCRIPT_FILE) + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +# Verbosity. + +proto_verbose_0 = @echo " PROTO " $(filter %.proto,$(?F)); +proto_verbose = $(proto_verbose_$(V)) + +# Core targets. + +ifneq ($(wildcard src/),) +ifneq ($(filter gpb protobuffs,$(BUILD_DEPS) $(DEPS)),) +PROTO_FILES := $(filter %.proto,$(ALL_SRC_FILES)) +ERL_FILES += $(addprefix src/,$(patsubst %.proto,%_pb.erl,$(notdir $(PROTO_FILES)))) + +ifeq ($(PROTO_FILES),) +$(ERLANG_MK_TMP)/last-makefile-change-protobuffs: + $(verbose) : +else +# Rebuild proto files when the Makefile changes. +# We exclude $(PROJECT).d to avoid a circular dependency. +$(ERLANG_MK_TMP)/last-makefile-change-protobuffs: $(filter-out $(PROJECT).d,$(MAKEFILE_LIST)) | $(ERLANG_MK_TMP) + $(verbose) if test -f $@; then \ + touch $(PROTO_FILES); \ + fi + $(verbose) touch $@ + +$(PROJECT).d:: $(ERLANG_MK_TMP)/last-makefile-change-protobuffs +endif + +ifeq ($(filter gpb,$(BUILD_DEPS) $(DEPS)),) +define compile_proto.erl + [begin + protobuffs_compile:generate_source(F, [ + {output_include_dir, "./include"}, + {output_src_dir, "./src"}]) + end || F <- string:tokens("$1", " ")], + halt(). +endef +else +define compile_proto.erl + [begin + gpb_compile:file(F, [ + {include_as_lib, true}, + {module_name_suffix, "_pb"}, + {o_hrl, "./include"}, + {o_erl, "./src"}]) + end || F <- string:tokens("$1", " ")], + halt(). +endef +endif + +ifneq ($(PROTO_FILES),) +$(PROJECT).d:: $(PROTO_FILES) + $(verbose) mkdir -p ebin/ include/ + $(if $(strip $?),$(proto_verbose) $(call erlang,$(call compile_proto.erl,$?))) +endif +endif +endif + +# Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: relx-rel relx-relup distclean-relx-rel run + +# Configuration. + +RELX ?= $(ERLANG_MK_TMP)/relx +RELX_CONFIG ?= $(CURDIR)/relx.config + +RELX_URL ?= https://erlang.mk/res/relx-v3.27.0 +RELX_OPTS ?= +RELX_OUTPUT_DIR ?= _rel +RELX_REL_EXT ?= +RELX_TAR ?= 1 + +ifdef SFX + RELX_TAR = 1 +endif + +ifeq ($(firstword $(RELX_OPTS)),-o) + RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS)) +else + RELX_OPTS += -o $(RELX_OUTPUT_DIR) +endif + +# Core targets. + +ifeq ($(IS_DEP),) +ifneq ($(wildcard $(RELX_CONFIG)),) +rel:: relx-rel + +relup:: relx-relup +endif +endif + +distclean:: distclean-relx-rel + +# Plugin-specific targets. + +$(RELX): | $(ERLANG_MK_TMP) + $(gen_verbose) $(call core_http_get,$(RELX),$(RELX_URL)) + $(verbose) chmod +x $(RELX) + +relx-rel: $(RELX) rel-deps app + $(verbose) $(RELX) $(if $(filter 1,$V),-V 3) -c $(RELX_CONFIG) $(RELX_OPTS) release + $(verbose) $(MAKE) relx-post-rel +ifeq ($(RELX_TAR),1) + $(verbose) $(RELX) $(if $(filter 1,$V),-V 3) -c $(RELX_CONFIG) $(RELX_OPTS) tar +endif + +relx-relup: $(RELX) rel-deps app + $(verbose) $(RELX) $(if $(filter 1,$V),-V 3) -c $(RELX_CONFIG) $(RELX_OPTS) release + $(MAKE) relx-post-rel + $(verbose) $(RELX) $(if $(filter 1,$V),-V 3) -c $(RELX_CONFIG) $(RELX_OPTS) relup $(if $(filter 1,$(RELX_TAR)),tar) + +distclean-relx-rel: + $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR) + +# Default hooks. +relx-post-rel:: + $(verbose) : + +# Run target. + +ifeq ($(wildcard $(RELX_CONFIG)),) +run:: +else + +define get_relx_release.erl + {ok, Config} = file:consult("$(call core_native_path,$(RELX_CONFIG))"), + {release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config), + Vsn = case Vsn0 of + {cmd, Cmd} -> os:cmd(Cmd); + semver -> ""; + {semver, _} -> ""; + VsnStr -> Vsn0 + end, + Extended = case lists:keyfind(extended_start_script, 1, Config) of + {_, true} -> "1"; + _ -> "" + end, + io:format("~s ~s ~s", [Name, Vsn, Extended]), + halt(0). +endef + +RELX_REL := $(shell $(call erlang,$(get_relx_release.erl))) +RELX_REL_NAME := $(word 1,$(RELX_REL)) +RELX_REL_VSN := $(word 2,$(RELX_REL)) +RELX_REL_CMD := $(if $(word 3,$(RELX_REL)),console) + +ifeq ($(PLATFORM),msys2) +RELX_REL_EXT := .cmd +endif + +run:: all + $(verbose) $(RELX_OUTPUT_DIR)/$(RELX_REL_NAME)/bin/$(RELX_REL_NAME)$(RELX_REL_EXT) $(RELX_REL_CMD) + +ifdef RELOAD +rel:: + $(verbose) $(RELX_OUTPUT_DIR)/$(RELX_REL_NAME)/bin/$(RELX_REL_NAME)$(RELX_REL_EXT) ping + $(verbose) $(RELX_OUTPUT_DIR)/$(RELX_REL_NAME)/bin/$(RELX_REL_NAME)$(RELX_REL_EXT) \ + eval "io:format(\"~p~n\", [c:lm()])" +endif + +help:: + $(verbose) printf "%s\n" "" \ + "Relx targets:" \ + " run Compile the project, build the release and run it" + +endif + +# Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu> +# Copyright (c) 2014, M Robert Martin <rob@version2beta.com> +# This file is contributed to erlang.mk and subject to the terms of the ISC License. + +.PHONY: shell + +# Configuration. + +SHELL_ERL ?= erl +SHELL_PATHS ?= $(CURDIR)/ebin $(TEST_DIR) +SHELL_OPTS ?= + +ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS)) + +# Core targets + +help:: + $(verbose) printf "%s\n" "" \ + "Shell targets:" \ + " shell Run an erlang shell with SHELL_OPTS or reasonable default" + +# Plugin-specific targets. + +$(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep)))) + +ifneq ($(SKIP_DEPS),) +build-shell-deps: +else +build-shell-deps: $(ALL_SHELL_DEPS_DIRS) + $(verbose) set -e; for dep in $(ALL_SHELL_DEPS_DIRS) ; do \ + if [ -z "$(strip $(FULL))" ] && [ ! -L $$dep ] && [ -f $$dep/ebin/dep_built ]; then \ + :; \ + else \ + $(MAKE) -C $$dep IS_DEP=1; \ + if [ ! -L $$dep ] && [ -d $$dep/ebin ]; then touch $$dep/ebin/dep_built; fi; \ + fi \ + done +endif + +shell:: build-shell-deps + $(gen_verbose) $(SHELL_ERL) -pa $(SHELL_PATHS) $(SHELL_OPTS) + +# Copyright 2017, Stanislaw Klekot <dozzie@jarowit.net> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: distclean-sphinx sphinx + +# Configuration. + +SPHINX_BUILD ?= sphinx-build +SPHINX_SOURCE ?= doc +SPHINX_CONFDIR ?= +SPHINX_FORMATS ?= html +SPHINX_DOCTREES ?= $(ERLANG_MK_TMP)/sphinx.doctrees +SPHINX_OPTS ?= + +#sphinx_html_opts = +#sphinx_html_output = html +#sphinx_man_opts = +#sphinx_man_output = man +#sphinx_latex_opts = +#sphinx_latex_output = latex + +# Helpers. + +sphinx_build_0 = @echo " SPHINX" $1; $(SPHINX_BUILD) -N -q +sphinx_build_1 = $(SPHINX_BUILD) -N +sphinx_build_2 = set -x; $(SPHINX_BUILD) +sphinx_build = $(sphinx_build_$(V)) + +define sphinx.build +$(call sphinx_build,$1) -b $1 -d $(SPHINX_DOCTREES) $(if $(SPHINX_CONFDIR),-c $(SPHINX_CONFDIR)) $(SPHINX_OPTS) $(sphinx_$1_opts) -- $(SPHINX_SOURCE) $(call sphinx.output,$1) + +endef + +define sphinx.output +$(if $(sphinx_$1_output),$(sphinx_$1_output),$1) +endef + +# Targets. + +ifneq ($(wildcard $(if $(SPHINX_CONFDIR),$(SPHINX_CONFDIR),$(SPHINX_SOURCE))/conf.py),) +docs:: sphinx +distclean:: distclean-sphinx +endif + +help:: + $(verbose) printf "%s\n" "" \ + "Sphinx targets:" \ + " sphinx Generate Sphinx documentation." \ + "" \ + "ReST sources and 'conf.py' file are expected in directory pointed by" \ + "SPHINX_SOURCE ('doc' by default). SPHINX_FORMATS lists formats to build (only" \ + "'html' format is generated by default); target directory can be specified by" \ + 'setting sphinx_$${format}_output, for example: sphinx_html_output = output/html' \ + "Additional Sphinx options can be set in SPHINX_OPTS." + +# Plugin-specific targets. + +sphinx: + $(foreach F,$(SPHINX_FORMATS),$(call sphinx.build,$F)) + +distclean-sphinx: + $(gen_verbose) rm -rf $(filter-out $(SPHINX_SOURCE),$(foreach F,$(SPHINX_FORMATS),$(call sphinx.output,$F))) + +# Copyright (c) 2017, Jean-Sébastien Pédron <jean-sebastien@rabbitmq.com> +# This file is contributed to erlang.mk and subject to the terms of the ISC License. + +.PHONY: show-ERL_LIBS show-ERLC_OPTS show-TEST_ERLC_OPTS + +show-ERL_LIBS: + @echo $(ERL_LIBS) + +show-ERLC_OPTS: + @$(foreach opt,$(ERLC_OPTS) -pa ebin -I include,echo "$(opt)";) + +show-TEST_ERLC_OPTS: + @$(foreach opt,$(TEST_ERLC_OPTS) -pa ebin -I include,echo "$(opt)";) + +# Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu> +# Copyright (c) 2015, Erlang Solutions Ltd. +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: xref distclean-xref + +# Configuration. + +ifeq ($(XREF_CONFIG),) + XREFR_ARGS := +else + XREFR_ARGS := -c $(XREF_CONFIG) +endif + +XREFR ?= $(CURDIR)/xrefr +export XREFR + +XREFR_URL ?= https://github.com/inaka/xref_runner/releases/download/1.1.0/xrefr + +# Core targets. + +help:: + $(verbose) printf '%s\n' '' \ + 'Xref targets:' \ + ' xref Run Xrefr using $$XREF_CONFIG as config file if defined' + +distclean:: distclean-xref + +# Plugin-specific targets. + +$(XREFR): + $(gen_verbose) $(call core_http_get,$(XREFR),$(XREFR_URL)) + $(verbose) chmod +x $(XREFR) + +xref: deps app $(XREFR) + $(gen_verbose) $(XREFR) $(XREFR_ARGS) + +distclean-xref: + $(gen_verbose) rm -rf $(XREFR) + +# Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: sfx + +ifdef RELX_REL +ifdef SFX + +# Configuration. + +SFX_ARCHIVE ?= $(RELX_OUTPUT_DIR)/$(RELX_REL_NAME)/$(RELX_REL_NAME)-$(RELX_REL_VSN).tar.gz +SFX_OUTPUT_FILE ?= $(RELX_OUTPUT_DIR)/$(RELX_REL_NAME).run + +# Core targets. + +rel:: sfx + +# Plugin-specific targets. + +define sfx_stub +#!/bin/sh + +TMPDIR=`mktemp -d` +ARCHIVE=`awk '/^__ARCHIVE_BELOW__$$/ {print NR + 1; exit 0;}' $$0` +FILENAME=$$(basename $$0) +REL=$${FILENAME%.*} + +tail -n+$$ARCHIVE $$0 | tar -xzf - -C $$TMPDIR + +$$TMPDIR/bin/$$REL console +RET=$$? + +rm -rf $$TMPDIR + +exit $$RET + +__ARCHIVE_BELOW__ +endef + +sfx: + $(verbose) $(call core_render,sfx_stub,$(SFX_OUTPUT_FILE)) + $(gen_verbose) cat $(SFX_ARCHIVE) >> $(SFX_OUTPUT_FILE) + $(verbose) chmod +x $(SFX_OUTPUT_FILE) + +endif +endif + +# Copyright (c) 2013-2017, Loïc Hoguin <essen@ninenines.eu> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +# External plugins. + +DEP_PLUGINS ?= + +$(foreach p,$(DEP_PLUGINS),\ + $(eval $(if $(findstring /,$p),\ + $(call core_dep_plugin,$p,$(firstword $(subst /, ,$p))),\ + $(call core_dep_plugin,$p/plugins.mk,$p)))) + +help:: help-plugins + +help-plugins:: + $(verbose) : + +# Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu> +# Copyright (c) 2015-2016, Jean-Sébastien Pédron <jean-sebastien@rabbitmq.com> +# This file is part of erlang.mk and subject to the terms of the ISC License. + +# Fetch dependencies recursively (without building them). + +.PHONY: fetch-deps fetch-doc-deps fetch-rel-deps fetch-test-deps \ + fetch-shell-deps + +.PHONY: $(ERLANG_MK_RECURSIVE_DEPS_LIST) \ + $(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST) \ + $(ERLANG_MK_RECURSIVE_REL_DEPS_LIST) \ + $(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST) \ + $(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST) + +fetch-deps: $(ERLANG_MK_RECURSIVE_DEPS_LIST) +fetch-doc-deps: $(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST) +fetch-rel-deps: $(ERLANG_MK_RECURSIVE_REL_DEPS_LIST) +fetch-test-deps: $(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST) +fetch-shell-deps: $(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST) + +ifneq ($(SKIP_DEPS),) +$(ERLANG_MK_RECURSIVE_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_REL_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST): + $(verbose) :> $@ +else +# By default, we fetch "normal" dependencies. They are also included no +# matter the type of requested dependencies. +# +# $(ALL_DEPS_DIRS) includes $(BUILD_DEPS). + +$(ERLANG_MK_RECURSIVE_DEPS_LIST): $(LOCAL_DEPS_DIRS) $(ALL_DEPS_DIRS) +$(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST): $(LOCAL_DEPS_DIRS) $(ALL_DEPS_DIRS) $(ALL_DOC_DEPS_DIRS) +$(ERLANG_MK_RECURSIVE_REL_DEPS_LIST): $(LOCAL_DEPS_DIRS) $(ALL_DEPS_DIRS) $(ALL_REL_DEPS_DIRS) +$(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST): $(LOCAL_DEPS_DIRS) $(ALL_DEPS_DIRS) $(ALL_TEST_DEPS_DIRS) +$(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST): $(LOCAL_DEPS_DIRS) $(ALL_DEPS_DIRS) $(ALL_SHELL_DEPS_DIRS) + +# Allow to use fetch-deps and $(DEP_TYPES) to fetch multiple types of +# dependencies with a single target. +ifneq ($(filter doc,$(DEP_TYPES)),) +$(ERLANG_MK_RECURSIVE_DEPS_LIST): $(ALL_DOC_DEPS_DIRS) +endif +ifneq ($(filter rel,$(DEP_TYPES)),) +$(ERLANG_MK_RECURSIVE_DEPS_LIST): $(ALL_REL_DEPS_DIRS) +endif +ifneq ($(filter test,$(DEP_TYPES)),) +$(ERLANG_MK_RECURSIVE_DEPS_LIST): $(ALL_TEST_DEPS_DIRS) +endif +ifneq ($(filter shell,$(DEP_TYPES)),) +$(ERLANG_MK_RECURSIVE_DEPS_LIST): $(ALL_SHELL_DEPS_DIRS) +endif + +ERLANG_MK_RECURSIVE_TMP_LIST := $(abspath $(ERLANG_MK_TMP)/recursive-tmp-deps-$(shell echo $$PPID).log) + +$(ERLANG_MK_RECURSIVE_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_REL_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST) \ +$(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST): | $(ERLANG_MK_TMP) +ifeq ($(IS_APP)$(IS_DEP),) + $(verbose) rm -f $(ERLANG_MK_RECURSIVE_TMP_LIST) +endif + $(verbose) touch $(ERLANG_MK_RECURSIVE_TMP_LIST) + $(verbose) set -e; for dep in $^ ; do \ + if ! grep -qs ^$$dep$$ $(ERLANG_MK_RECURSIVE_TMP_LIST); then \ + echo $$dep >> $(ERLANG_MK_RECURSIVE_TMP_LIST); \ + if grep -qs -E "^[[:blank:]]*include[[:blank:]]+(erlang\.mk|.*/erlang\.mk|.*ERLANG_MK_FILENAME.*)$$" \ + $$dep/GNUmakefile $$dep/makefile $$dep/Makefile; then \ + $(MAKE) -C $$dep fetch-deps \ + IS_DEP=1 \ + ERLANG_MK_RECURSIVE_TMP_LIST=$(ERLANG_MK_RECURSIVE_TMP_LIST); \ + fi \ + fi \ + done +ifeq ($(IS_APP)$(IS_DEP),) + $(verbose) sort < $(ERLANG_MK_RECURSIVE_TMP_LIST) | \ + uniq > $(ERLANG_MK_RECURSIVE_TMP_LIST).sorted + $(verbose) cmp -s $(ERLANG_MK_RECURSIVE_TMP_LIST).sorted $@ \ + || mv $(ERLANG_MK_RECURSIVE_TMP_LIST).sorted $@ + $(verbose) rm -f $(ERLANG_MK_RECURSIVE_TMP_LIST).sorted + $(verbose) rm $(ERLANG_MK_RECURSIVE_TMP_LIST) +endif +endif # ifneq ($(SKIP_DEPS),) + +# List dependencies recursively. + +.PHONY: list-deps list-doc-deps list-rel-deps list-test-deps \ + list-shell-deps + +list-deps: $(ERLANG_MK_RECURSIVE_DEPS_LIST) +list-doc-deps: $(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST) +list-rel-deps: $(ERLANG_MK_RECURSIVE_REL_DEPS_LIST) +list-test-deps: $(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST) +list-shell-deps: $(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST) + +list-deps list-doc-deps list-rel-deps list-test-deps list-shell-deps: + $(verbose) cat $^ + +# Query dependencies recursively. + +.PHONY: query-deps query-doc-deps query-rel-deps query-test-deps \ + query-shell-deps + +QUERY ?= name fetch_method repo version + +define query_target +$(1): $(2) clean-tmp-query.log +ifeq ($(IS_APP)$(IS_DEP),) + $(verbose) rm -f $(4) +endif + $(verbose) $(foreach dep,$(3),\ + echo $(PROJECT): $(foreach q,$(QUERY),$(call query_$(q),$(dep))) >> $(4) ;) + $(if $(filter-out query-deps,$(1)),,\ + $(verbose) set -e; for dep in $(3) ; do \ + if grep -qs ^$$$$dep$$$$ $(ERLANG_MK_TMP)/query.log; then \ + :; \ + else \ + echo $$$$dep >> $(ERLANG_MK_TMP)/query.log; \ + $(MAKE) -C $(DEPS_DIR)/$$$$dep $$@ QUERY="$(QUERY)" IS_DEP=1 || true; \ + fi \ + done) +ifeq ($(IS_APP)$(IS_DEP),) + $(verbose) touch $(4) + $(verbose) cat $(4) +endif +endef + +clean-tmp-query.log: +ifeq ($(IS_DEP),) + $(verbose) rm -f $(ERLANG_MK_TMP)/query.log +endif + +$(eval $(call query_target,query-deps,$(ERLANG_MK_RECURSIVE_DEPS_LIST),$(BUILD_DEPS) $(DEPS),$(ERLANG_MK_QUERY_DEPS_FILE))) +$(eval $(call query_target,query-doc-deps,$(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST),$(DOC_DEPS),$(ERLANG_MK_QUERY_DOC_DEPS_FILE))) +$(eval $(call query_target,query-rel-deps,$(ERLANG_MK_RECURSIVE_REL_DEPS_LIST),$(REL_DEPS),$(ERLANG_MK_QUERY_REL_DEPS_FILE))) +$(eval $(call query_target,query-test-deps,$(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST),$(TEST_DEPS),$(ERLANG_MK_QUERY_TEST_DEPS_FILE))) +$(eval $(call query_target,query-shell-deps,$(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST),$(SHELL_DEPS),$(ERLANG_MK_QUERY_SHELL_DEPS_FILE))) diff --git a/deps/rabbitmq_cli/include/.gitkeep b/deps/rabbitmq_cli/include/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/deps/rabbitmq_cli/include/.gitkeep diff --git a/deps/rabbitmq_cli/lib/rabbit_common/records.ex b/deps/rabbitmq_cli/lib/rabbit_common/records.ex new file mode 100644 index 0000000000..dc1503caf7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbit_common/records.ex @@ -0,0 +1,19 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitCommon.Records do + require Record + import Record, only: [defrecord: 2, extract: 2] + + # Important: amqqueue records must not be used directly since they are versioned + # for mixed version cluster compatibility. Convert records + # to maps on the server end to access the fields of those records. MK. + defrecord :listener, extract(:listener, from_lib: "rabbit_common/include/rabbit.hrl") + defrecord :plugin, extract(:plugin, from_lib: "rabbit_common/include/rabbit.hrl") + defrecord :resource, extract(:resource, from_lib: "rabbit_common/include/rabbit.hrl") + + defrecord :hostent, extract(:hostent, from_lib: "kernel/include/inet.hrl") +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/auto_complete.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/auto_complete.ex new file mode 100644 index 0000000000..1f907b528d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/auto_complete.ex @@ -0,0 +1,118 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.AutoComplete do + alias RabbitMQ.CLI.Core.{CommandModules, Parser} + + # Use the same jaro distance limit as in Elixir's "did you mean?" + @jaro_distance_limit 0.77 + + @spec complete(String.t(), [String.t()]) :: [String.t()] + def complete(_, []) do + [] + end + + def complete(script_name, args) do + case Parser.parse_global(args) do + {_, %{script_name: _args_script_name}, _} -> + complete(args) + + _ -> + complete(["--script-name", script_name | args]) + end + end + + def suggest_command(_cmd_name, empty) when empty == %{} do + nil + end + def suggest_command(typed, module_map) do + suggestion = + module_map + |> Map.keys() + |> Enum.map(fn existing -> + {existing, String.jaro_distance(existing, typed)} + end) + |> Enum.max_by(fn {_, distance} -> distance end) + + case suggestion do + {cmd, distance} when distance >= @jaro_distance_limit -> + {:suggest, cmd} + _ -> + nil + end + end + + defp complete(tokens) do + {command, command_name, _, _, _} = Parser.parse(tokens) + last_token = List.last(tokens) + + case {command, command_name} do + ## No command provided + {_, ""} -> + complete_default_opts(last_token) + + ## Command is not found/incomplete + {:no_command, command_name} -> + complete_command_name(command_name) + + {{:suggest, _}, command_name} -> + complete_command_name(command_name) + + ## Command is found + {command, _} -> + complete_command_opts(command, last_token) + end + |> Enum.sort() + end + + defp complete_default_opts(opt) do + Parser.default_switches() + |> Keyword.keys() + |> Enum.map(fn sw -> "--" <> to_string(sw) end) + |> select_starts_with(opt) + |> format_options + end + + defp complete_command_name(command_name) do + module_map = CommandModules.module_map() + + case module_map[command_name] do + nil -> + module_map + |> Map.keys() + |> select_starts_with(command_name) + + _ -> + command_name + end + end + + defp complete_command_opts(command, <<"-", _::binary>> = opt) do + switches = + command.switches + |> Keyword.keys() + |> Enum.map(fn sw -> "--" <> to_string(sw) end) + + # aliases = command.aliases + # |> Keyword.keys + # |> Enum.map(fn(al) -> "-" <> to_string(al) end) + select_starts_with(switches, opt) + |> format_options + end + + defp complete_command_opts(_, _) do + [] + end + + defp select_starts_with(list, prefix) do + Enum.filter(list, fn el -> String.starts_with?(el, prefix) end) + end + + defp format_options(options) do + options + |> Enum.map(fn opt -> String.replace(opt, "_", "-") end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/command_behaviour.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/command_behaviour.ex new file mode 100644 index 0000000000..800d4d3227 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/command_behaviour.ex @@ -0,0 +1,170 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.CommandBehaviour do + alias RabbitMQ.CLI.Core.Helpers + + @type pair_of_strings :: nonempty_list(String.t()) + + @callback usage() :: String.t() | [String.t()] + # validates CLI arguments + @callback validate(list(), map()) :: :ok | {:validation_failure, atom() | {atom(), String.t()}} + @callback merge_defaults(list(), map()) :: {list(), map()} + @callback banner(list(), map()) :: [String.t()] | String.t() | nil + @callback run(list(), map()) :: any + # Coerces run/2 return value into the standard command output form + # that is then formatted, printed and returned as an exit code. + # There is a default implementation for this callback in DefaultOutput module + @callback output(any, map()) :: + :ok + | {:ok, any} + | {:stream, Enum.t()} + | {:error, RabbitMQ.CLI.Core.ExitCodes.exit_code(), [String.t()]} + + @optional_callbacks formatter: 0, + printer: 0, + scopes: 0, + usage_additional: 0, + usage_doc_guides: 0, + description: 0, + help_section: 0, + switches: 0, + aliases: 0, + # validates execution environment, e.g. file presence, + # whether RabbitMQ is in an expected state on a node, etc + validate_execution_environment: 2, + distribution: 1 + + @callback validate_execution_environment(list(), map()) :: + :ok | {:validation_failure, atom() | {atom(), any}} + @callback switches() :: Keyword.t() + @callback aliases() :: Keyword.t() + + @callback formatter() :: atom() + @callback printer() :: atom() + @callback scopes() :: [atom()] | nil + @callback description() :: String.t() + @callback help_section() :: String.t() + @callback usage_additional() :: String.t() | [String.t()] | nonempty_list(pair_of_strings()) | [{String.t(), String.t()}] + @callback usage_doc_guides() :: String.t() | [String.t()] + ## Erlang distribution control + ## :cli - default rabbitmqctl generated node name + ## :none - disable erlang distribution + ## {:fun, fun} - use a custom function to start distribution + @callback distribution(map()) :: :cli | :none | {:fun, (map() -> :ok | {:error, any()})} + + defmacro defcmd(map) do + usage_q = case map[:usage] do + nil -> :ok + usage -> + quote do def usage(), do: unquote(usage) end + end + scopes_q = case map[:scopes] do + nil -> :ok + scopes -> + quote do def scopes(), do: unquote(scopes) end + end + description_q = case map[:description] do + nil -> :ok + description -> + quote do def description(), do: unquote(description) end + end + help_section_q = case map[:help_section] do + nil -> :ok + help_section -> + quote do def help_section(), do: unquote(help_section) end + end + usage_additional_q = case map[:usage_additional] do + nil -> :ok + usage_additional -> + quote do def usage_additional(), do: unquote(usage_additional) end + end + formatter_q = case map[:formatter] do + nil -> :ok + formatter -> + quote do def formatter(), do: unquote(formatter) end + end + switches_q = case map[:switches] do + nil -> :ok + switches -> + quote do def switches(), do: unquote(switches) end + end + aliases_q = case map[:aliases] do + nil -> :ok + aliases -> + quote do def aliases(), do: unquote(aliases) end + end + + quote do + unquote(usage_q) + unquote(scopes_q) + unquote(description_q) + unquote(help_section_q) + unquote(usage_additional_q) + unquote(formatter_q) + unquote(switches_q) + unquote(aliases_q) + end + end + + def usage(cmd) do + cmd.usage() + end + + def scopes(cmd) do + Helpers.apply_if_exported(cmd, :scopes, [], nil) + end + + def description(cmd) do + Helpers.apply_if_exported(cmd, :description, [], "") + end + + def help_section(cmd) do + case Helpers.apply_if_exported(cmd, :help_section, [], :other) do + :other -> + case Application.get_application(cmd) do + :rabbitmqctl -> :other + plugin -> {:plugin, plugin} + end + section -> + section + end + end + + def usage_additional(cmd) do + Helpers.apply_if_exported(cmd, :usage_additional, [], []) + end + + def usage_doc_guides(cmd) do + Helpers.apply_if_exported(cmd, :usage_doc_guides, [], []) + end + + def formatter(cmd, default) do + Helpers.apply_if_exported(cmd, :formatter, [], default) + end + + def printer(cmd, default) do + Helpers.apply_if_exported(cmd, :printer, [], default) + end + + def switches(cmd) do + Helpers.apply_if_exported(cmd, :switches, [], []) + end + + def aliases(cmd) do + Helpers.apply_if_exported(cmd, :aliases, [], []) + end + + def validate_execution_environment(cmd, args, options) do + Helpers.apply_if_exported(cmd, + :validate_execution_environment, [args, options], + :ok) + end + + def distribution(cmd, options) do + Helpers.apply_if_exported(cmd, :distribution, [options], :cli) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_default_switches_and_timeout.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_default_switches_and_timeout.ex new file mode 100644 index 0000000000..3a78974b0c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_default_switches_and_timeout.ex @@ -0,0 +1,16 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout do + defmacro __using__(_) do + quote do + def switches(), do: [timeout: :integer] + def aliases(), do: [t: :timeout] + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_no_positional_arguments.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_no_positional_arguments.ex new file mode 100644 index 0000000000..166c4f22f7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_no_positional_arguments.ex @@ -0,0 +1,21 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.AcceptsNoPositionalArguments do + defmacro __using__(_) do + quote do + def validate(args, _) when length(args) > 0 do + {:validation_failure, :too_many_args} + end + + # Note: this will accept everything, so it must be the + # last validation clause defined! + def validate(_, _), do: :ok + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_one_positional_argument.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_one_positional_argument.ex new file mode 100644 index 0000000000..3731775ed3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_one_positional_argument.ex @@ -0,0 +1,25 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.AcceptsOnePositionalArgument do + defmacro __using__(_) do + quote do + def validate(args, _) when length(args) == 0 do + {:validation_failure, :not_enough_args} + end + + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + # Note: this will accept everything, so it must be the + # last validation clause defined! + def validate(_, _), do: :ok + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_one_positive_integer_argument.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_one_positive_integer_argument.ex new file mode 100644 index 0000000000..6b6fd28dfe --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_one_positive_integer_argument.ex @@ -0,0 +1,34 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.AcceptsOnePositiveIntegerArgument do + defmacro __using__(_) do + quote do + def validate(args, _) when length(args) == 0 do + {:validation_failure, :not_enough_args} + end + + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate([value], _) when is_integer(value) do + :ok + end + + def validate([value], _) do + case Integer.parse(value) do + {n, _} when n >= 1 -> :ok + :error -> {:validation_failure, {:bad_argument, "Argument must be a positive integer"}} + end + end + + def validate(_, _), do: :ok + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_two_positional_arguments.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_two_positional_arguments.ex new file mode 100644 index 0000000000..18b2740b42 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/accepts_two_positional_arguments.ex @@ -0,0 +1,25 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments do + defmacro __using__(_) do + quote do + def validate(args, _) when length(args) < 2 do + {:validation_failure, :not_enough_args} + end + + def validate(args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + + # Note: this will accept everything, so it must be the + # last validation clause defined! + def validate([_, _], _), do: :ok + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/alarms.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/alarms.ex new file mode 100644 index 0000000000..15143f7f69 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/alarms.ex @@ -0,0 +1,88 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Alarms do + def alarm_lines(alarms, node_name) do + Enum.reduce(alarms, [], fn + :file_descriptor_limit, acc -> + ["File descriptor limit alarm on node #{node_name}" | acc] + + {{:resource_limit, :memory, alarmed_node_name}, _}, acc -> + ["Memory alarm on node #{alarmed_node_name}" | acc] + + {:resource_limit, :memory, alarmed_node_name}, acc -> + ["Memory alarm on node #{alarmed_node_name}" | acc] + + {{:resource_limit, :disk, alarmed_node_name}, _}, acc -> + ["Free disk space alarm on node #{alarmed_node_name}" | acc] + + {:resource_limit, :disk, alarmed_node_name}, acc -> + ["Free disk space alarm on node #{alarmed_node_name}" | acc] + end) + |> Enum.reverse() + end + + def local_alarms(alarms, node_name) do + Enum.filter( + alarms, + fn + # local by definition + :file_descriptor_limit -> + true + + {{:resource_limit, _, a_node}, _} -> + node_name == a_node + end + ) + end + + def clusterwide_alarms(alarms, node_name) do + alarms + |> Enum.reject(fn x -> x == :file_descriptor_limit end) + |> Enum.filter(fn {{:resource_limit, _, a_node}, _} -> + a_node != node_name + end) + end + + def alarm_types(xs) do + Enum.map(xs, &alarm_type/1) + end + + def alarm_type(val) when is_atom(val) do + val + end + def alarm_type({:resource_limit, val, _node}) do + val + end + def alarm_type({{:resource_limit, val, _node}, []}) do + val + end + + def alarm_maps(xs) do + Enum.map(xs, &alarm_map/1) + end + def alarm_map(:file_descriptor_limit) do + %{ + type: :resource_limit, + resource: :file_descriptors, + node: node() + } + end + def alarm_map({{:resource_limit, resource, node}, _}) do + %{ + type: :resource_limit, + resource: resource, + node: node + } + end + def alarm_map({:resource_limit, resource, node}) do + %{ + type: :resource_limit, + resource: resource, + node: node + } + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/ansi.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/ansi.ex new file mode 100644 index 0000000000..e541a632ff --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/ansi.ex @@ -0,0 +1,35 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.ANSI do + def bright(string) do + "#{IO.ANSI.bright()}#{string}#{IO.ANSI.reset()}" + end + + def red(string) do + "#{IO.ANSI.red()}#{string}#{IO.ANSI.reset()}" + end + + def yellow(string) do + "#{IO.ANSI.yellow()}#{string}#{IO.ANSI.reset()}" + end + + def magenta(string) do + "#{IO.ANSI.magenta()}#{string}#{IO.ANSI.reset()}" + end + + def bright_red(string) do + "#{IO.ANSI.bright()}#{IO.ANSI.red()}#{string}#{IO.ANSI.reset()}" + end + + def bright_yellow(string) do + "#{IO.ANSI.bright()}#{IO.ANSI.yellow()}#{string}#{IO.ANSI.reset()}" + end + + def bright_magenta(string) do + "#{IO.ANSI.bright()}#{IO.ANSI.magenta()}#{string}#{IO.ANSI.reset()}" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/code_path.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/code_path.ex new file mode 100644 index 0000000000..32636fac6f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/code_path.ex @@ -0,0 +1,108 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.CodePath do + alias RabbitMQ.CLI.Core.{Config, Paths, Platform} + + def add_plugins_to_load_path(opts) do + with {:ok, plugins_dir} <- Paths.plugins_dir(opts) do + String.split(to_string(plugins_dir), Platform.path_separator()) + |> Enum.map(&add_directory_plugins_to_load_path/1) + + :ok + end + end + + def add_directory_plugins_to_load_path(directory_with_plugins_inside_it) do + with {:ok, files} <- File.ls(directory_with_plugins_inside_it) do + Enum.map( + files, + fn filename -> + cond do + String.ends_with?(filename, [".ez"]) -> + Path.join([directory_with_plugins_inside_it, filename]) + |> String.to_charlist() + |> add_archive_code_path() + + File.dir?(filename) -> + Path.join([directory_with_plugins_inside_it, filename]) + |> add_dir_code_path() + + true -> + {:error, {:not_a_plugin, filename}} + end + end + ) + end + end + + defp add_archive_code_path(ez_dir) do + case :erl_prim_loader.list_dir(ez_dir) do + {:ok, [app_dir]} -> + app_in_ez = :filename.join(ez_dir, app_dir) + add_dir_code_path(app_in_ez) + + _ -> + {:error, :no_app_dir} + end + end + + defp add_dir_code_path(app_dir_0) do + app_dir = to_charlist(app_dir_0) + + case :erl_prim_loader.list_dir(app_dir) do + {:ok, list} -> + case Enum.member?(list, 'ebin') do + true -> + ebin_dir = :filename.join(app_dir, 'ebin') + Code.append_path(ebin_dir) + + false -> + {:error, :no_ebin} + end + + _ -> + {:error, :app_dir_empty} + end + end + + def require_rabbit_and_plugins(_, opts) do + require_rabbit_and_plugins(opts) + end + + def require_rabbit_and_plugins(opts) do + with :ok <- require_rabbit(opts), + :ok <- add_plugins_to_load_path(opts), + do: :ok + end + + def require_rabbit(_, opts) do + require_rabbit(opts) + end + + def require_rabbit(opts) do + home = Config.get_option(:rabbitmq_home, opts) + + case home do + nil -> + {:error, {:unable_to_load_rabbit, :rabbitmq_home_is_undefined}} + + _ -> + case Application.load(:rabbit) do + :ok -> + Code.ensure_loaded(:rabbit_plugins) + :ok + + {:error, {:already_loaded, :rabbit}} -> + Code.ensure_loaded(:rabbit_plugins) + :ok + + {:error, err} -> + {:error, {:unable_to_load_rabbit, err}} + end + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex new file mode 100644 index 0000000000..a1b7fc9237 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/command_modules.ex @@ -0,0 +1,196 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.CommandModules do + alias RabbitMQ.CLI.Core.Config + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginsHelpers + alias RabbitMQ.CLI.CommandBehaviour + + import RabbitMQ.CLI.Core.CodePath + + @commands_ns ~r/RabbitMQ.CLI.(.*).Commands/ + + def module_map(opts \\ %{}) do + Application.get_env(:rabbitmqctl, :commands) || load(opts) + end + + def module_map_core(opts \\ %{}) do + Application.get_env(:rabbitmqctl, :commands_core) || load_core(opts) + end + + def load_core(opts) do + scope = script_scope(opts) + commands = load_commands_core(scope) + Application.put_env(:rabbitmqctl, :commands_core, commands) + commands + end + + def load(opts) do + scope = script_scope(opts) + commands = load_commands(scope, opts) + Application.put_env(:rabbitmqctl, :commands, commands) + commands + end + + def script_scope(opts) do + scopes = Application.get_env(:rabbitmqctl, :scopes, []) + scopes[Config.get_option(:script_name, opts)] || :none + end + + def load_commands_core(scope) do + make_module_map(ctl_modules(), scope) + end + + def load_commands(scope, opts) do + make_module_map(plugin_modules(opts) ++ ctl_modules(), scope) + end + + def ctl_modules() do + Application.spec(:rabbitmqctl, :modules) + end + + def plugin_modules(opts) do + require_rabbit(opts) + + enabled_plugins = + try do + PluginsHelpers.read_enabled(opts) + catch + err -> + {:ok, enabled_plugins_file} = PluginsHelpers.enabled_plugins_file(opts) + require Logger + + Logger.warn( + "Unable to read the enabled plugins file.\n" <> + " Reason: #{inspect(err)}\n" <> + " Commands provided by plugins will not be available.\n" <> + " Please make sure your user has sufficient permissions to read to\n" <> + " #{enabled_plugins_file}" + ) + + [] + end + + partitioned = + Enum.group_by(enabled_plugins, fn app -> + case Application.load(app) do + :ok -> :loaded + {:error, {:already_loaded, ^app}} -> :loaded + _ -> :not_found + end + end) + + loaded = partitioned[:loaded] || [] + missing = partitioned[:not_found] || [] + ## If plugins are not in ERL_LIBS, they should be loaded from plugins_dir + case missing do + [] -> + :ok + + _ -> + add_plugins_to_load_path(opts) + Enum.each(missing, fn app -> Application.load(app) end) + end + + Enum.flat_map(loaded ++ missing, fn app -> + Application.spec(app, :modules) || [] + end) + end + + defp make_module_map(modules, scope) do + commands_ns = Regex.recompile!(@commands_ns) + + modules + |> Enum.filter(fn mod -> + to_string(mod) =~ commands_ns and + module_exists?(mod) and + implements_command_behaviour?(mod) and + command_in_scope(mod, scope) + end) + |> Enum.map(&command_tuple/1) + |> Map.new() + end + + defp module_exists?(nil) do + false + end + + defp module_exists?(mod) do + Code.ensure_loaded?(mod) + end + + defp implements_command_behaviour?(nil) do + false + end + + defp implements_command_behaviour?(module) do + Enum.member?( + module.module_info(:attributes)[:behaviour] || [], + RabbitMQ.CLI.CommandBehaviour + ) + end + + def module_to_command(mod) do + mod + |> to_string + |> strip_namespace + |> to_snake_case + |> String.replace_suffix("_command", "") + end + + defp command_tuple(cmd) do + {module_to_command(cmd), cmd} + end + + def strip_namespace(str) do + str + |> String.split(".") + |> List.last() + end + + def to_snake_case(<<c, str::binary>>) do + tail = for <<c <- str>>, into: "", do: snake(c) + <<to_lower_char(c), tail::binary>> + end + + defp snake(c) do + if c >= ?A and c <= ?Z do + <<"_", c + 32>> + else + <<c>> + end + end + + defp to_lower_char(c) do + if c >= ?A and c <= ?Z do + c + 32 + else + c + end + end + + defp command_in_scope(_cmd, :all) do + true + end + + defp command_in_scope(cmd, scope) do + Enum.member?(command_scopes(cmd), scope) + end + + defp command_scopes(cmd) do + case CommandBehaviour.scopes(cmd) do + nil -> + Regex.recompile!(@commands_ns) + |> Regex.run(to_string(cmd), capture: :all_but_first) + |> List.first() + |> to_snake_case + |> String.to_atom() + |> List.wrap() + scopes -> + scopes + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/config.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/config.ex new file mode 100644 index 0000000000..251f9e582f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/config.ex @@ -0,0 +1,200 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Config do + alias RabbitMQ.CLI.{ + CommandBehaviour, + FormatterBehaviour, + PrinterBehaviour + } + + alias RabbitMQ.CLI.Core.Helpers + + # + # Environment + # + + def get_option(name, opts \\ %{}) do + raw_option = + opts[name] || + get_system_option(name, opts) || + default(name) + + normalise(name, raw_option) + end + + def output_less?(opts) do + Map.get(opts, :silent, false) || Map.get(opts, :quiet, false) + end + + def normalise(:node, nil), do: nil + + def normalise(:node, node) when not is_atom(node) do + RabbitMQ.CLI.Core.DataCoercion.to_atom(node) + end + + def normalise(:erlang_cookie, nil), do: nil + + def normalise(:erlang_cookie, c) when not is_atom(c) do + RabbitMQ.CLI.Core.DataCoercion.to_atom(c) + end + + def normalise(:longnames, true), do: :longnames + def normalise(:longnames, "true"), do: :longnames + def normalise(:longnames, 'true'), do: :longnames + def normalise(:longnames, "\"true\""), do: :longnames + def normalise(:longnames, _val), do: :shortnames + def normalise(_, value), do: value + + def get_system_option(:script_name, _) do + Path.basename(:escript.script_name()) + |> Path.rootname() + |> String.to_atom() + end + + def get_system_option(:aliases_file, _) do + System.get_env("RABBITMQ_CLI_ALIASES_FILE") + end + + def get_system_option(:erlang_cookie, _) do + System.get_env("RABBITMQ_ERLANG_COOKIE") + end + + def get_system_option(:node, %{offline: true} = opts) do + remote_node = + case opts[:node] do + nil -> nil + val -> Helpers.normalise_node_option(val, opts[:longnames], opts) + end + + context = get_env_context(remote_node, true) + get_val_from_env_context(context, :node) + end + + def get_system_option(:node, opts) do + remote_node = + case opts[:node] do + nil -> nil + val -> Helpers.normalise_node_option(val, opts[:longnames], opts) + end + + context = get_env_context(remote_node, false) + get_val_from_env_context(context, :node) + end + + def get_system_option(name, opts) do + work_offline = opts[:offline] == true + + remote_node = + case name do + :longnames -> nil + :rabbitmq_home -> nil + _ -> node_flag_or_default(opts) + end + + context = get_env_context(remote_node, work_offline) + val0 = get_val_from_env_context(context, name) + + val = + cond do + remote_node != nil and + val0 == :undefined and + (name == :mnesia_dir or name == :feature_flags_file or name == :plugins_dir or + name == :enabled_plugins_file) -> + context1 = get_env_context(nil, true) + get_val_from_env_context(context1, name) + + true -> + val0 + end + + case val do + :undefined -> nil + _ -> val + end + end + + def get_env_context(nil, _) do + :rabbit_env.get_context() + end + + def get_env_context(remote_node, work_offline) do + case work_offline do + true -> :rabbit_env.get_context(:offline) + false -> :rabbit_env.get_context(remote_node) + end + end + + def get_val_from_env_context(context, name) do + case name do + :node -> context[:nodename] + :longnames -> context[:nodename_type] == :longnames + :rabbitmq_home -> context[:rabbitmq_home] + :mnesia_dir -> context[:mnesia_dir] + :plugins_dir -> context[:plugins_path] + :plugins_expand_dir -> context[:plugins_expand_dir] + :feature_flags_file -> context[:feature_flags_file] + :enabled_plugins_file -> context[:enabled_plugins_file] + end + end + + def node_flag_or_default(opts) do + case opts[:node] do + nil -> + # Just in case `opts` was not normalized yet (to get the + # default node), we do it here as well. + case Helpers.normalise_node_option(opts) do + {:error, _} -> nil + {:ok, normalized_opts} -> normalized_opts[:node] + end + + node -> + node + end + end + + def default(:script_name), do: :rabbitmqctl + def default(:node), do: :rabbit + def default(_), do: nil + + # + # Formatters and Printers + # + + def get_formatter(command, %{formatter: formatter}) do + module_name = FormatterBehaviour.module_name(formatter) + + case Code.ensure_loaded(module_name) do + {:module, _} -> module_name + {:error, :nofile} -> CommandBehaviour.formatter(command, default_formatter()) + end + end + + def get_formatter(command, _) do + CommandBehaviour.formatter(command, default_formatter()) + end + + def get_printer(command, %{printer: printer}) do + module_name = PrinterBehaviour.module_name(printer) + + case Code.ensure_loaded(module_name) do + {:module, _} -> module_name + {:error, :nofile} -> CommandBehaviour.printer(command, default_printer()) + end + end + + def get_printer(command, _) do + CommandBehaviour.printer(command, default_printer()) + end + + def default_formatter() do + RabbitMQ.CLI.Formatters.String + end + + def default_printer() do + RabbitMQ.CLI.Printers.StdIO + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/data_coercion.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/data_coercion.ex new file mode 100644 index 0000000000..9c3d3e7344 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/data_coercion.ex @@ -0,0 +1,21 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defprotocol RabbitMQ.CLI.Core.DataCoercion do + def to_atom(data) +end + +defimpl RabbitMQ.CLI.Core.DataCoercion, for: Atom do + def to_atom(atom), do: atom +end + +defimpl RabbitMQ.CLI.Core.DataCoercion, for: BitString do + def to_atom(string), do: String.to_atom(string) +end + +defimpl RabbitMQ.CLI.Core.DataCoercion, for: List do + def to_atom(list), do: List.to_atom(list) +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex new file mode 100644 index 0000000000..403c9dd970 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex @@ -0,0 +1,138 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Distribution do + alias RabbitMQ.CLI.Core.{ANSI, Config, Helpers} + + # + # API + # + + def start() do + start(%{}) + end + + def start(options) do + node_name_type = Config.get_option(:longnames, options) + result = start(node_name_type, 10, :undefined) + ensure_cookie(options) + result + end + + def stop, do: Node.stop() + + def start_as(node_name, options) do + node_name_type = Config.get_option(:longnames, options) + result = start_with_epmd(node_name, node_name_type) + ensure_cookie(options) + result + end + + ## Optimization. We try to start EPMD only if distribution fails + def start_with_epmd(node_name, node_name_type) do + case Node.start(node_name, node_name_type) do + {:ok, _} = ok -> + ok + + {:error, {:already_started, _}} = started -> + started + + {:error, {{:already_started, _}, _}} = started -> + started + + ## EPMD can be stopped. Retry with EPMD + {:error, _} -> + :rabbit_nodes_common.ensure_epmd() + Node.start(node_name, node_name_type) + end + end + + def per_node_timeout(:infinity, _) do + :infinity + end + + def per_node_timeout(timeout, node_count) do + Kernel.trunc(timeout / node_count) + end + + # + # Implementation + # + + def ensure_cookie(options) do + case Config.get_option(:erlang_cookie, options) do + nil -> + :ok + + cookie -> + Node.set_cookie(cookie) + maybe_warn_about_deprecated_rabbitmq_erlang_cookie_env_variable(options) + :ok + end + end + + defp start(_opt, 0, last_err) do + {:error, last_err} + end + + defp start(node_name_type, attempts, _last_err) do + candidate = generate_cli_node_name(node_name_type) + + case start_with_epmd(candidate, node_name_type) do + {:ok, _} -> + :ok + + {:error, {:already_started, pid}} -> + {:ok, pid} + + {:error, {{:already_started, pid}, _}} -> + {:ok, pid} + + {:error, reason} -> + start(node_name_type, attempts - 1, reason) + end + end + + defp generate_cli_node_name(node_name_type) do + case Helpers.get_rabbit_hostname(node_name_type) do + {:error, _} = err -> + throw(err) + + rmq_hostname -> + # This limits the number of possible unique node names used by CLI tools to avoid + # the atom table from growing above the node limit. We must use reasonably unique IDs + # to allow for concurrent CLI tool execution. + # + # Enum.random/1 is constant time and space with range arguments https://hexdocs.pm/elixir/Enum.html#random/1. + id = Enum.random(1..1024) + String.to_atom("rabbitmqcli-#{id}-#{rmq_hostname}") + end + end + + defp maybe_warn_about_deprecated_rabbitmq_erlang_cookie_env_variable(options) do + case System.get_env("RABBITMQ_ERLANG_COOKIE") do + nil -> + :ok + + _ -> + case Config.output_less?(options) do + true -> + :ok + + false -> + warning = + ANSI.bright_red( + "RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. " + ) <> + ANSI.yellow( + "Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead." + ) + + IO.puts(warning) + end + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/doc_guide.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/doc_guide.ex new file mode 100644 index 0000000000..c75dcb0d7c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/doc_guide.ex @@ -0,0 +1,67 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.DocGuide.Macros do + @moduledoc """ + Helper module that works around a compiler limitation: macros cannot + be used in a module that defines them. + """ + @default_domain "rabbitmq.com" + + defmacro defguide(name, opts \\ []) do + domain = Keyword.get(opts, :domain, @default_domain) + fn_name = String.to_atom(name) + path_segment = Keyword.get(opts, :path_segment, String.replace(name, "_", "-")) + + quote do + def unquote(fn_name)() do + unquote("https://#{domain}/#{path_segment}.html") + end + end + end +end + +defmodule RabbitMQ.CLI.Core.DocGuide do + require RabbitMQ.CLI.Core.DocGuide.Macros + alias RabbitMQ.CLI.Core.DocGuide.Macros + + # + # API + # + + Macros.defguide("access_control") + Macros.defguide("alarms") + Macros.defguide("disk_alarms") + Macros.defguide("alternate_exchange", path_segment: "ae") + Macros.defguide("channels") + Macros.defguide("cli") + Macros.defguide("clustering") + Macros.defguide("cluster_formation") + Macros.defguide("connections") + Macros.defguide("configuration", path_segment: "configure") + Macros.defguide("consumers") + Macros.defguide("definitions") + Macros.defguide("erlang_versions", path_segment: "which-erlang") + Macros.defguide("feature_flags") + Macros.defguide("firehose") + Macros.defguide("mirroring", path_segment: "ha") + Macros.defguide("logging") + Macros.defguide("management") + Macros.defguide("memory_use") + Macros.defguide("monitoring") + Macros.defguide("networking") + Macros.defguide("parameters") + Macros.defguide("publishers") + Macros.defguide("plugins") + Macros.defguide("queues") + Macros.defguide("quorum_queues", domain: "next.rabbitmq.com") + Macros.defguide("stream_queues", domain: "next.rabbitmq.com") + Macros.defguide("runtime_tuning", path_segment: "runtime") + Macros.defguide("tls", path_segment: "ssl") + Macros.defguide("troubleshooting") + Macros.defguide("virtual_hosts", path_segments: "vhosts") + Macros.defguide("upgrade") +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/erl_eval.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/erl_eval.ex new file mode 100644 index 0000000000..8d2e3aff9e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/erl_eval.ex @@ -0,0 +1,26 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.ErlEval do + def parse_expr(expr) do + expr_str = to_charlist(expr) + + case :erl_scan.string(expr_str) do + {:ok, scanned, _} -> + case :erl_parse.parse_exprs(scanned) do + {:ok, parsed} -> {:ok, parsed} + {:error, err} -> {:error, format_parse_error(err)} + end + + {:error, err, _} -> + {:error, format_parse_error(err)} + end + end + + defp format_parse_error({_line, mod, err}) do + to_string(:lists.flatten(mod.format_error(err))) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/exit_codes.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/exit_codes.ex new file mode 100644 index 0000000000..9e416d7153 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/exit_codes.ex @@ -0,0 +1,56 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Lists predefined error exit codes used by RabbitMQ CLI tools. +# The codes are adopted from [1], which (according to our team's research) +# is possibly the most standardized set of command line tool exit codes there is. +# +# 1. https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+12.0-RELEASE&arch=default&format=html +defmodule RabbitMQ.CLI.Core.ExitCodes do + @exit_ok 0 + @exit_usage 64 + @exit_dataerr 65 + @exit_nouser 67 + @exit_unavailable 69 + @exit_software 70 + @exit_tempfail 75 + @exit_config 78 + + @type exit_code :: integer + + def exit_ok, do: @exit_ok + def exit_usage, do: @exit_usage + def exit_dataerr, do: @exit_dataerr + def exit_nouser, do: @exit_nouser + def exit_unavailable, do: @exit_unavailable + def exit_software, do: @exit_software + def exit_tempfail, do: @exit_tempfail + def exit_config, do: @exit_config + + def exit_code_for({:validation_failure, :not_enough_args}), do: exit_usage() + def exit_code_for({:validation_failure, :too_many_args}), do: exit_usage() + def exit_code_for({:validation_failure, {:not_enough_args, _}}), do: exit_usage() + def exit_code_for({:validation_failure, {:too_many_args, _}}), do: exit_usage() + def exit_code_for({:validation_failure, {:bad_argument, _}}), do: exit_dataerr() + def exit_code_for({:validation_failure, :bad_argument}), do: exit_dataerr() + def exit_code_for({:validation_failure, :eperm}), do: exit_dataerr() + def exit_code_for({:validation_failure, {:bad_option, _}}), do: exit_usage() + def exit_code_for({:validation_failure, _}), do: exit_usage() + # a special case of bad_argument + def exit_code_for({:no_such_vhost, _}), do: exit_dataerr() + def exit_code_for({:no_such_user, _}), do: exit_nouser() + def exit_code_for({:badrpc_multi, :timeout, _}), do: exit_tempfail() + def exit_code_for({:badrpc, :timeout}), do: exit_tempfail() + def exit_code_for({:badrpc, {:timeout, _}}), do: exit_tempfail() + def exit_code_for({:badrpc, {:timeout, _, _}}), do: exit_tempfail() + def exit_code_for(:timeout), do: exit_tempfail() + def exit_code_for({:timeout, _}), do: exit_tempfail() + def exit_code_for({:badrpc_multi, :nodedown, _}), do: exit_unavailable() + def exit_code_for({:badrpc, :nodedown}), do: exit_unavailable() + def exit_code_for({:node_name, _}), do: exit_dataerr() + def exit_code_for({:incompatible_version, _, _}), do: exit_unavailable() + def exit_code_for({:error, _}), do: exit_unavailable() +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/feature_flags.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/feature_flags.ex new file mode 100644 index 0000000000..4b4b8c8d5d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/feature_flags.ex @@ -0,0 +1,19 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.FeatureFlags do + + # + # API + # + + def feature_flag_lines(feature_flags) do + feature_flags + |> Enum.map(fn %{name: name, state: state} -> + "Flag: #{name}, state: #{state}" + end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex new file mode 100644 index 0000000000..97bd7c7bd9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/helpers.ex @@ -0,0 +1,148 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Helpers do + alias RabbitMQ.CLI.Core.{Config, NodeName} + require Record + + def get_rabbit_hostname(node_name_type \\ :shortnames) do + normalise_node(Config.get_option(:node), node_name_type) + end + + def normalise_node(nil, node_name_type) do + normalise_node(Config.get_option(:node), node_name_type) + end + + def normalise_node(name, node_name_type) do + case NodeName.create(name, node_name_type) do + {:ok, node_name} -> node_name + other -> other + end + end + + # rabbitmq/rabbitmq-cli#278 + def normalise_node_option(options) do + node_opt = Config.get_option(:node, options) + longnames_opt = Config.get_option(:longnames, options) + case NodeName.create(node_opt, longnames_opt) do + {:error, _} = err -> + err + {:ok, val} -> + {:ok, Map.put(options, :node, val)} + end + end + + def normalise_node_option(nil, _, _) do + nil + end + def normalise_node_option(node_opt, longnames_opt, options) do + case NodeName.create(node_opt, longnames_opt) do + {:error, _} = err -> + err + {:ok, val} -> + {:ok, Map.put(options, :node, val)} + end + end + + def case_insensitive_format(%{format: format} = opts) do + %{opts | format: String.downcase(format)} + end + def case_insensitive_format(opts), do: opts + + def nodes_in_cluster(node, timeout \\ :infinity) do + with_nodes_in_cluster(node, fn nodes -> nodes end, timeout) + end + + def with_nodes_in_cluster(node, fun, timeout \\ :infinity) do + case :rpc.call(node, :rabbit_mnesia, :cluster_nodes, [:running], timeout) do + {:badrpc, _} = err -> err + value -> fun.(value) + end + end + + def node_running?(node) do + :net_adm.ping(node) == :pong + end + + # Convert function to stream + def defer(fun) do + Stream.iterate(:ok, fn _ -> fun.() end) + |> Stream.drop(1) + |> Stream.take(1) + end + + # Streamify a function sequence passing result + # Execution can be terminated by an error {:error, _}. + # The error will be the last element in the stream. + # Functions can return {:ok, val}, so val will be passed + # to then next function, or {:ok, val, output} where + # val will be passed and output will be put into the stream + def stream_until_error_parameterised(funs, init) do + Stream.transform(funs, {:just, init}, fn + f, {:just, val} -> + case f.(val) do + {:error, _} = err -> {[err], :nothing} + :ok -> {[], {:just, val}} + {:ok, new_val} -> {[], {:just, new_val}} + {:ok, new_val, out} -> {[out], {:just, new_val}} + end + + _, :nothing -> + {:halt, :nothing} + end) + end + + # Streamify function sequence. + # Execution can be terminated by an error {:error, _}. + # The error will be the last element in the stream. + def stream_until_error(funs) do + stream_until_error_parameterised( + Enum.map( + funs, + fn fun -> + fn :no_param -> + case fun.() do + {:error, _} = err -> err + other -> {:ok, :no_param, other} + end + end + end + ), + :no_param + ) + end + + def apply_if_exported(mod, fun, args, default) do + Code.ensure_loaded(mod) + case function_exported?(mod, fun, length(args)) do + true -> apply(mod, fun, args) + false -> default + end + end + + def cli_acting_user, do: "rmq-cli" + + def string_or_inspect(val) do + case String.Chars.impl_for(val) do + nil -> + inspect(val) + + _ -> + try do + to_string(val) + catch + _, _ -> inspect(val) + end + end + end + + def evaluate_input_as_term(input) do + {:ok, tokens, _end_line} = :erl_scan.string(to_charlist(input <> ".")) + {:ok, abs_form} = :erl_parse.parse_exprs(tokens) + {:value, term_value, _bs} = :erl_eval.exprs(abs_form, :erl_eval.new_bindings()) + term_value + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/input.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/input.ex new file mode 100644 index 0000000000..5e1328be29 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/input.ex @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Input do + alias RabbitMQ.CLI.Core.Config + + def consume_single_line_string_with_prompt(prompt, opts) do + val = case Config.output_less?(opts) do + true -> + IO.read(:stdio, :line) + false -> + IO.puts(prompt) + IO.read(:stdio, :line) + end + + case val do + :eof -> :eof + "" -> :eof + s -> String.trim(s) + end + end + + def consume_multiline_string() do + val = IO.read(:stdio, :all) + + case val do + :eof -> :eof + "" -> :eof + s -> String.trim(s) + end + end + + def infer_password(prompt, opts) do + consume_single_line_string_with_prompt(prompt, opts) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/listeners.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/listeners.ex new file mode 100644 index 0000000000..0bc162186c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/listeners.ex @@ -0,0 +1,312 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Listeners do + import Record, only: [defrecord: 3, extract: 2] + import RabbitCommon.Records + import RabbitMQ.CLI.Core.DataCoercion + + # + # API + # + + defrecord :certificate, :Certificate, extract(:Certificate, from_lib: "public_key/include/public_key.hrl") + defrecord :tbscertificate, :TBSCertificate, extract(:TBSCertificate, from_lib: "public_key/include/public_key.hrl") + defrecord :validity, :Validity, extract(:Validity, from_lib: "public_key/include/public_key.hrl") + + def listeners_on(listeners, target_node) do + Enum.filter(listeners, fn listener(node: node) -> + node == target_node + end) + end + + def listeners_with_certificates(listeners) do + Enum.filter(listeners, fn listener(opts: opts) -> + Keyword.has_key?(opts, :cacertfile) or Keyword.has_key?(opts, :certfile) + end) + end + + def listener_lines(listeners) do + listeners + |> listener_maps + |> Enum.map(fn %{interface: interface, port: port, protocol: protocol} -> + "Interface: #{interface}, port: #{port}, protocol: #{protocol}, purpose: #{ + protocol_label(to_atom(protocol)) + }" + end) + end + def listener_lines(listeners, node) do + listeners + |> listener_maps + |> Enum.map(fn %{interface: interface, port: port, protocol: protocol} -> + "Node: #{node}, interface: #{interface}, port: #{port}, protocol: #{protocol}, purpose: #{ + protocol_label(to_atom(protocol)) + }" + end) + end + + def listener_map(listener) when is_map(listener) do + listener + end + def listener_map(listener) do + # Listener options are left out intentionally: they can contain deeply nested values + # that are impossible to serialise to JSON. + # + # Management plugin/HTTP API had its fair share of bugs because of that + # and now filters out a lot of options. Raw listener data can be seen in + # rabbitmq-diagnostics status. + listener(node: node, protocol: protocol, ip_address: interface, port: port) = listener + + %{ + node: node, + protocol: protocol, + interface: :inet.ntoa(interface) |> to_string |> maybe_enquote_interface, + port: port, + purpose: protocol_label(to_atom(protocol)) + } + end + + def listener_maps(listeners) do + Enum.map(listeners, &listener_map/1) + end + + def listener_certs(listener) do + listener(node: node, protocol: protocol, ip_address: interface, port: port, opts: opts) = listener + + %{ + node: node, + protocol: protocol, + interface: :inet.ntoa(interface) |> to_string |> maybe_enquote_interface, + port: port, + purpose: protocol_label(to_atom(protocol)), + certfile: read_cert(Keyword.get(opts, :certfile)), + cacertfile: read_cert(Keyword.get(opts, :cacertfile)) + } + end + + def read_cert(nil) do + nil + end + def read_cert({:pem, pem}) do + pem + end + def read_cert(path) do + case File.read(path) do + {:ok, bin} -> + bin + {:error, _} = err -> + err + end + end + + def listener_expiring_within(listener, seconds) do + listener(node: node, protocol: protocol, ip_address: interface, port: port, opts: opts) = listener + certfile = Keyword.get(opts, :certfile) + cacertfile = Keyword.get(opts, :cacertfile) + now = :calendar.datetime_to_gregorian_seconds(:calendar.universal_time()) + expiry_date = now + seconds + certfile_expires_on = expired(cert_validity(read_cert(certfile)), expiry_date) + cacertfile_expires_on = expired(cert_validity(read_cert(cacertfile)), expiry_date) + case {certfile_expires_on, cacertfile_expires_on} do + {[], []} -> + false + _ -> + %{ + node: node, + protocol: protocol, + interface: interface, + port: port, + certfile: certfile, + cacertfile: cacertfile, + certfile_expires_on: certfile_expires_on, + cacertfile_expires_on: cacertfile_expires_on + } + end + end + + def expired_listener_map(%{node: node, protocol: protocol, interface: interface, port: port, certfile_expires_on: certfile_expires_on, cacertfile_expires_on: cacertfile_expires_on, certfile: certfile, cacertfile: cacertfile}) do + %{ + node: node, + protocol: protocol, + interface: :inet.ntoa(interface) |> to_string |> maybe_enquote_interface, + port: port, + purpose: protocol_label(to_atom(protocol)), + certfile: certfile |> to_string, + cacertfile: cacertfile |> to_string, + certfile_expires_on: expires_on_list(certfile_expires_on), + cacertfile_expires_on: expires_on_list(cacertfile_expires_on) + } + end + + def expires_on_list({:error, _} = error) do + [error] + end + def expires_on_list(expires) do + Enum.map(expires, &expires_on/1) + end + + def expires_on({:error, _} = error) do + error + end + def expires_on(seconds) do + {:ok, naive} = NaiveDateTime.from_erl(:calendar.gregorian_seconds_to_datetime(seconds)) + NaiveDateTime.to_string(naive) + end + + def expired(nil, _) do + [] + end + def expired({:error, _} = error, _) do + error + end + def expired(expires, expiry_date) do + Enum.filter(expires, fn ({:error, _}) -> true + (seconds) -> seconds < expiry_date end) + end + + def cert_validity(nil) do + nil + end + def cert_validity(cert) do + dsa_entries = :public_key.pem_decode(cert) + case dsa_entries do + [] -> + {:error, "The certificate file provided does not contain any PEM entry."} + _ -> + now = :calendar.datetime_to_gregorian_seconds(:calendar.universal_time()) + Enum.map(dsa_entries, fn ({:Certificate, _, _} = dsa_entry) -> + certificate(tbsCertificate: tbs_certificate) = :public_key.pem_entry_decode(dsa_entry) + tbscertificate(validity: validity) = tbs_certificate + validity(notAfter: not_after, notBefore: not_before) = validity + start = :pubkey_cert.time_str_2_gregorian_sec(not_before) + case start > now do + true -> + {:ok, naive} = NaiveDateTime.from_erl(:calendar.gregorian_seconds_to_datetime(start)) + startdate = NaiveDateTime.to_string(naive) + {:error, "Certificate is not yet valid. It starts on #{startdate}"} + false -> + :pubkey_cert.time_str_2_gregorian_sec(not_after) + end + ({type, _, _}) -> + {:error, "The certificate file provided contains a #{type} entry."} + end) + end + end + + def listener_rows(listeners) do + for listener(node: node, protocol: protocol, ip_address: interface, port: port) <- listeners do + # Listener options are left out intentionally, see above + [ + node: node, + protocol: protocol, + interface: :inet.ntoa(interface) |> to_string |> maybe_enquote_interface, + port: port, + purpose: protocol_label(to_atom(protocol)) + ] + end + end + + def protocol_label(:amqp), do: "AMQP 0-9-1 and AMQP 1.0" + def protocol_label(:'amqp/ssl'), do: "AMQP 0-9-1 and AMQP 1.0 over TLS" + def protocol_label(:mqtt), do: "MQTT" + def protocol_label(:'mqtt/ssl'), do: "MQTT over TLS" + def protocol_label(:stomp), do: "STOMP" + def protocol_label(:'stomp/ssl'), do: "STOMP over TLS" + def protocol_label(:http), do: "HTTP API" + def protocol_label(:https), do: "HTTP API over TLS (HTTPS)" + def protocol_label(:"http/web-mqtt"), do: "MQTT over WebSockets" + def protocol_label(:"https/web-mqtt"), do: "MQTT over WebSockets and TLS (HTTPS)" + def protocol_label(:"http/web-stomp"), do: "STOMP over WebSockets" + def protocol_label(:"https/web-stomp"), do: "STOMP over WebSockets and TLS (HTTPS)" + def protocol_label(:"http/prometheus"), do: "Prometheus exporter API over HTTP" + def protocol_label(:"https/prometheus"), do: "Prometheus exporter API over TLS (HTTPS)" + def protocol_label(:clustering), do: "inter-node and CLI tool communication" + def protocol_label(other), do: to_string(other) + + def normalize_protocol(proto) do + val = proto |> to_string |> String.downcase() + + case val do + "amqp091" -> "amqp" + "amqp0.9.1" -> "amqp" + "amqp0-9-1" -> "amqp" + "amqp0_9_1" -> "amqp" + "amqp10" -> "amqp" + "amqp1.0" -> "amqp" + "amqp1-0" -> "amqp" + "amqp1_0" -> "amqp" + "amqps" -> "amqp/ssl" + "mqtt3.1" -> "mqtt" + "mqtt3.1.1" -> "mqtt" + "mqtt31" -> "mqtt" + "mqtt311" -> "mqtt" + "mqtt3_1" -> "mqtt" + "mqtt3_1_1" -> "mqtt" + "mqtts" -> "mqtt/ssl" + "mqtt+tls" -> "mqtt/ssl" + "mqtt+ssl" -> "mqtt/ssl" + "stomp1.0" -> "stomp" + "stomp1.1" -> "stomp" + "stomp1.2" -> "stomp" + "stomp10" -> "stomp" + "stomp11" -> "stomp" + "stomp12" -> "stomp" + "stomp1_0" -> "stomp" + "stomp1_1" -> "stomp" + "stomp1_2" -> "stomp" + "stomps" -> "stomp/ssl" + "stomp+tls" -> "stomp/ssl" + "stomp+ssl" -> "stomp/ssl" + "https" -> "https" + "http1" -> "http" + "http1.1" -> "http" + "http_api" -> "http" + "management" -> "http" + "management_ui" -> "http" + "ui" -> "http" + "cli" -> "clustering" + "distribution" -> "clustering" + "webmqtt" -> "http/web-mqtt" + "web-mqtt" -> "http/web-mqtt" + "web_mqtt" -> "http/web-mqtt" + "webmqtt/tls" -> "https/web-mqtt" + "web-mqtt/tls" -> "https/web-mqtt" + "webmqtt/ssl" -> "https/web-mqtt" + "web-mqtt/ssl" -> "https/web-mqtt" + "webmqtt+tls" -> "https/web-mqtt" + "web-mqtt+tls" -> "https/web-mqtt" + "webmqtt+ssl" -> "https/web-mqtt" + "web-mqtt+ssl" -> "https/web-mqtt" + "webstomp" -> "http/web-stomp" + "web-stomp" -> "http/web-stomp" + "web_stomp" -> "http/web-stomp" + "webstomp/tls" -> "https/web-stomp" + "web-stomp/tls" -> "https/web-stomp" + "webstomp/ssl" -> "https/web-stomp" + "web-stomp/ssl" -> "https/web-stomp" + "webstomp+tls" -> "https/web-stomp" + "web-stomp+tls" -> "https/web-stomp" + "webstomp+ssl" -> "https/web-stomp" + "web-stomp+ssl" -> "https/web-stomp" + _ -> val + end + end + + # + # Implementation + # + + defp maybe_enquote_interface(value) do + # This simplistic way of distinguishing IPv6 addresses, + # networks address ranges, etc actually works better + # for the kind of values we can get here than :inet functions. MK. + regex = Regex.recompile!(~r/:/) + case value =~ regex do + true -> "[#{value}]" + false -> value + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/log_files.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/log_files.ex new file mode 100644 index 0000000000..b6d104bff0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/log_files.ex @@ -0,0 +1,52 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.LogFiles do + @spec get_log_locations(atom, integer | :infinity) :: [String.t] | {:badrpc, term} + def get_log_locations(node_name, timeout) do + case :rabbit_misc.rpc_call(node_name, + :rabbit_lager, :log_locations, [], + timeout) do + {:badrpc, _} = error -> error; + list -> Enum.map(list, &to_string/1) + end + end + + @spec get_default_log_location(atom, integer | :infinity) :: + {:ok, String.t} | {:badrpc, term} | {:error, term} + def get_default_log_location(node_name, timeout) do + case get_log_locations(node_name, timeout) do + {:badrpc, _} = error -> error; + [] -> {:error, "No log files configured on the node"}; + [first_log | _] = log_locations -> + case get_log_config_file_location(node_name, timeout) do + {:badrpc, _} = error -> error; + nil -> {:ok, first_log}; + location -> + case Enum.member?(log_locations, location) do + true -> {:ok, to_string(location)}; + ## Configured location was not propagated to lager? + false -> {:ok, first_log} + end + end + end + end + + defp get_log_config_file_location(node_name, timeout) do + case :rabbit_misc.rpc_call(node_name, + :application, :get_env, [:rabbit, :log, :none], + timeout) do + {:badrpc, _} = error -> error; + :none -> nil; + log_config -> + case log_config[:file] do + nil -> nil; + file_config -> + file_config[:file] + end + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/memory.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/memory.ex new file mode 100644 index 0000000000..92db5b5502 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/memory.ex @@ -0,0 +1,105 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Memory do + alias RabbitMQ.CLI.InformationUnit, as: IU + + def memory_units do + ["k", "kiB", "M", "MiB", "G", "GiB", "kB", "MB", "GB", ""] + end + + def memory_unit_absolute(num, unit) when is_number(num) and num < 0, + do: {:bad_argument, [num, unit]} + + def memory_unit_absolute(num, "k") when is_number(num), do: power_as_int(num, 2, 10) + def memory_unit_absolute(num, "kiB") when is_number(num), do: power_as_int(num, 2, 10) + def memory_unit_absolute(num, "M") when is_number(num), do: power_as_int(num, 2, 20) + def memory_unit_absolute(num, "MiB") when is_number(num), do: power_as_int(num, 2, 20) + def memory_unit_absolute(num, "G") when is_number(num), do: power_as_int(num, 2, 30) + def memory_unit_absolute(num, "GiB") when is_number(num), do: power_as_int(num, 2, 30) + def memory_unit_absolute(num, "kB") when is_number(num), do: power_as_int(num, 10, 3) + def memory_unit_absolute(num, "MB") when is_number(num), do: power_as_int(num, 10, 6) + def memory_unit_absolute(num, "GB") when is_number(num), do: power_as_int(num, 10, 9) + def memory_unit_absolute(num, "") when is_number(num), do: num + def memory_unit_absolute(num, unit) when is_number(num), do: {:bad_argument, [unit]} + def memory_unit_absolute(num, unit), do: {:bad_argument, [num, unit]} + + def power_as_int(num, x, y), do: round(num * :math.pow(x, y)) + + def compute_relative_values(all_pairs) when is_map(all_pairs) do + compute_relative_values(Enum.into(all_pairs, [])) + end + def compute_relative_values(all_pairs) do + num_pairs = Keyword.delete(all_pairs, :strategy) + # Includes RSS, allocated and runtime-used ("erlang") values. + # See https://github.com/rabbitmq/rabbitmq-server/pull/1404. + totals = Keyword.get(num_pairs, :total) + pairs = Keyword.delete(num_pairs, :total) + # Should not be necessary but be more defensive. + total = + max_of(totals) || + Keyword.get(totals, :rss) || + Keyword.get(totals, :allocated) || + Keyword.get(totals, :erlang) + + pairs + |> Enum.map(fn {k, v} -> + pg = (v / total) |> fraction_to_percent() + {k, %{bytes: v, percentage: pg}} + end) + |> Enum.sort_by(fn {_key, %{bytes: bytes}} -> bytes end, &>=/2) + end + + def formatted_watermark(val) when is_float(val) do + %{relative: val} + end + def formatted_watermark({:absolute, val}) do + %{absolute: parse_watermark(val)} + end + def formatted_watermark(val) when is_integer(val) do + %{absolute: parse_watermark(val)} + end + def formatted_watermark(val) when is_bitstring(val) do + %{absolute: parse_watermark(val)} + end + def formatted_watermark(val) when is_list(val) do + %{absolute: parse_watermark(val)} + end + + def parse_watermark({:absolute, n}) do + case IU.parse(n) do + {:ok, parsed} -> parsed + err -> err + end + end + def parse_watermark(n) when is_bitstring(n) do + case IU.parse(n) do + {:ok, parsed} -> parsed + err -> err + end + end + def parse_watermark(n) when is_list(n) do + case IU.parse(n) do + {:ok, parsed} -> parsed + err -> err + end + end + def parse_watermark(n) when is_float(n) or is_integer(n) do + n + end + + # + # Implementation + # + + defp fraction_to_percent(x) do + Float.round(x * 100, 2) + end + + defp max_of(m) do + Keyword.values(m) |> Enum.max() + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/merges_default_virtual_host.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/merges_default_virtual_host.ex new file mode 100644 index 0000000000..94b1b768b6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/merges_default_virtual_host.ex @@ -0,0 +1,15 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.MergesDefaultVirtualHost do + defmacro __using__(_) do + quote do + def merge_defaults(args, opts), do: {args, Map.merge(%{vhost: "/"}, opts)} + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/merges_no_defaults.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/merges_no_defaults.ex new file mode 100644 index 0000000000..0ee6f3f05a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/merges_no_defaults.ex @@ -0,0 +1,15 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.MergesNoDefaults do + defmacro __using__(_) do + quote do + def merge_defaults(args, opts), do: {args, opts} + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/networking.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/networking.ex new file mode 100644 index 0000000000..12d99df7c1 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/networking.ex @@ -0,0 +1,73 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Networking do + @type address_family() :: :inet | :inet6 + + @spec address_family(String.t() | atom() | charlist() | binary()) :: address_family() + def address_family(value) do + val = RabbitMQ.CLI.Core.DataCoercion.to_atom(value) + case val do + :inet -> :inet + :inet4 -> :inet + :inet6 -> :inet6 + :ipv4 -> :inet + :ipv6 -> :inet6 + :IPv4 -> :inet + :IPv6 -> :inet6 + end + end + + @spec address_family(String.t() | atom()) :: boolean() + def valid_address_family?(value) when is_atom(value) do + valid_address_family?(to_string(value)) + end + def valid_address_family?("inet"), do: true + def valid_address_family?("inet4"), do: true + def valid_address_family?("inet6"), do: true + def valid_address_family?("ipv4"), do: true + def valid_address_family?("ipv6"), do: true + def valid_address_family?("IPv4"), do: true + def valid_address_family?("IPv6"), do: true + def valid_address_family?(_other), do: false + + @spec format_address(:inet.ip_address()) :: String.t() + def format_address(addr) do + to_string(:inet.ntoa(addr)) + end + + @spec format_addresses([:inet.ip_address()]) :: [String.t()] + def format_addresses(addrs) do + Enum.map(addrs, &format_address/1) + end + + @spec inetrc_map(nonempty_list()) :: map() + def inetrc_map(list) do + Enum.reduce(list, %{}, + fn hosts, acc when is_list(hosts) -> + Map.put(acc, "hosts", host_resolution_map(hosts)) + {k, v}, acc when k == :domain or k == :resolv_conf or k == :hosts_file -> + Map.put(acc, to_string(k), to_string(v)) + {k, v}, acc when is_list(v) when k == :search or k == :lookup -> + Map.put(acc, to_string(k), Enum.join(Enum.map(v, &to_string/1), ", ")) + {k, v}, acc when is_integer(v) -> + Map.put(acc, to_string(k), v) + {k, v, v2}, acc when is_tuple(v) when k == :nameserver or k == :nameservers or k == :alt_nameserver -> + Map.put(acc, to_string(k), "#{:inet.ntoa(v)}:#{v2}") + {k, v}, acc when is_tuple(v) when k == :nameserver or k == :nameservers or k == :alt_nameserver -> + Map.put(acc, to_string(k), to_string(:inet.ntoa(v))) + {k, v}, acc -> + Map.put(acc, to_string(k), to_string(v)) + end) + end + + def host_resolution_map(hosts) do + Enum.reduce(hosts, %{}, + fn {:host, address, hosts}, acc -> + Map.put(acc, to_string(:inet.ntoa(address)), Enum.map(hosts, &to_string/1)) + end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/node_name.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/node_name.ex new file mode 100644 index 0000000000..c39b215ca7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/node_name.ex @@ -0,0 +1,198 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.NodeName do + alias RabbitMQ.CLI.Core.Config + + @moduledoc """ + Provides functions for correctly constructing node names given a node type and optional base name. + """ + + @doc """ + Constructs complete node name based on :longnames or :shortnames and + a base name, in the same manner as Erlang/OTP lib/kernel/src/net_kernel.erl + """ + def create(base, type) do + create_name(base, type, 1) + end + + @doc """ + Get local hostname + """ + def hostname, do: :inet_db.gethostname() |> List.to_string() + + @doc """ + Get hostname part of current node name + """ + def hostname_from_node do + [_, hostname] = split_node(Node.self()) + hostname + end + + @doc """ + Get hostname part of given node name + """ + def hostname_from_node(name) do + [_, hostname] = split_node(name) + hostname + end + + def split_node(name) when is_atom(name) do + split_node(to_string(name)) + end + + def split_node(name) do + case String.split(name, "@", parts: 2) do + ["", host] -> + default_name = to_string(Config.default(:node)) + [default_name, host] + + [_head, _host] = rslt -> + rslt + + [head] -> + [head, ""] + end + end + + @doc """ + Get local domain. If unavailable, makes a good guess. We're using + :inet_db here because that's what Erlang/OTP uses when it creates a node + name: + https://github.com/erlang/otp/blob/8ca061c3006ad69c2a8d1c835d0d678438966dfc/lib/kernel/src/net_kernel.erl#L1363-L1445 + """ + def domain do + domain(1) + end + + # + # Implementation + # + + defp domain(attempt) do + case {attempt, :inet_db.res_option(:domain), :os.type()} do + {1, [], _} -> + do_load_resolv() + domain(0) + + {0, [], {:unix, :darwin}} -> + "local" + + {0, [], _} -> + "localdomain" + + {_, domain, _} -> + List.to_string(domain) + end + end + + defp create_name(name, long_or_short_names, attempt) do + {head, host1} = create_hostpart(name, long_or_short_names) + + case host1 do + {:ok, host_part} -> + case valid_name_head(head) do + true -> + {:ok, String.to_atom(head <> "@" <> host_part)} + + false -> + {:error, {:node_name, :invalid_node_name_head}} + end + + {:error, :long} when attempt == 1 -> + do_load_resolv() + create_name(name, long_or_short_names, 0) + + {:error, :long} when attempt == 0 -> + case valid_name_head(head) do + true -> + {:ok, String.to_atom(head <> "@" <> hostname() <> "." <> domain())} + + false -> + {:error, {:node_name, :invalid_node_name_head}} + end + + {:error, :hostname_not_allowed} -> + {:error, {:node_name, :hostname_not_allowed}} + + {:error, err_type} -> + {:error, {:node_name, err_type}} + end + end + + defp create_hostpart(name, long_or_short_names) do + [head, host] = split_node(name) + + host1 = + case {host, long_or_short_names} do + {"", :shortnames} -> + case :inet_db.gethostname() do + inet_db_host when inet_db_host != [] -> + {:ok, to_string(inet_db_host)} + + _ -> + {:error, :short} + end + + {"", :longnames} -> + case {:inet_db.gethostname(), :inet_db.res_option(:domain)} do + {inet_db_host, inet_db_domain} + when inet_db_host != [] and inet_db_domain != [] -> + {:ok, to_string(inet_db_host) <> "." <> to_string(inet_db_domain)} + + _ -> + {:error, :long} + end + + {_, type} -> + validate_hostname(host, type) + end + + {head, host1} + end + + defp validate_hostname(host, :longnames) do + case String.contains?(host, ".") do + true -> + validate_hostname_rx(host) + + _ -> + validate_hostname(host <> "." <> domain(), :longnames) + end + end + + defp validate_hostname(host, :shortnames) do + case String.contains?(host, ".") do + true -> + {:error, :short} + + _ -> + validate_hostname_rx(host) + end + end + + defp validate_hostname_rx(host) do + rx = Regex.compile!("^[!-ÿ]*$", [:unicode]) + + case Regex.match?(rx, host) do + true -> + {:ok, host} + + false -> + {:error, :hostname_not_allowed} + end + end + + defp valid_name_head(head) do + rx = Regex.compile!("^[0-9A-Za-z_\\-]+$", [:unicode]) + Regex.match?(rx, head) + end + + defp do_load_resolv do + # It could be we haven't read domain name from resolv file yet + :ok = :inet_config.do_load_resolv(:os.type(), :longnames) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex new file mode 100644 index 0000000000..0b53d59748 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/os_pid.ex @@ -0,0 +1,56 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.OsPid do + @external_process_check_interval 1000 + + @pid_regex ~r/^\s*(?<pid>\d+)/ + + # + # API + # + + def wait_for_os_process_death(pid) do + case :rabbit_misc.is_os_process_alive(pid) do + true -> + :timer.sleep(@external_process_check_interval) + wait_for_os_process_death(pid) + + false -> + :ok + end + end + + def read_pid_from_file(pidfile_path, should_wait) do + case {:file.read_file(pidfile_path), should_wait} do + {{:ok, contents}, _} -> + pid_regex = Regex.recompile!(@pid_regex) + + case Regex.named_captures(pid_regex, contents)["pid"] do + # e.g. the file is empty + nil -> + {:error, :could_not_read_pid_from_file, {:contents, contents}} + + pid_string -> + try do + {pid, _remainder} = Integer.parse(pid_string) + pid + rescue + _e in ArgumentError -> + {:error, {:could_not_read_pid_from_file, {:contents, contents}}} + end + end + + # file does not exist, wait and re-check + {{:error, :enoent}, true} -> + :timer.sleep(@external_process_check_interval) + read_pid_from_file(pidfile_path, should_wait) + + {{:error, details}, _} -> + {:error, :could_not_read_pid_from_file, details} + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/output.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/output.ex new file mode 100644 index 0000000000..1b2436cba4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/output.ex @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Output do + def format_output(:ok, _, _) do + :ok + end + + # the command intends to produce no output + def format_output({:ok, nil}, _, _) do + :ok + end + + def format_output({:ok, :check_passed}, _, _) do + :ok + end + + def format_output({:ok, output}, formatter, options) do + {:ok, formatter.format_output(output, options)} + end + + def format_output({:stream, stream}, formatter, options) do + {:stream, formatter.format_stream(stream, options)} + end + + def print_output(output, printer, options) do + {:ok, printer_state} = printer.init(options) + exit_code = print_output_0(output, printer, printer_state) + printer.finish(printer_state) + exit_code + end + + def print_output_0(:ok, printer, printer_state) do + printer.print_ok(printer_state) + :ok + end + + # the command intends to produce no output + def print_output_0({:ok, nil}, _printer, _printer_state) do + :ok + end + + def print_output_0({:ok, :check_passed}, _printer, _printer_state) do + :ok + end + + def print_output_0({:ok, single_value}, printer, printer_state) do + printer.print_output(single_value, printer_state) + :ok + end + + def print_output_0({:stream, stream}, printer, printer_state) do + case print_output_stream(stream, printer, printer_state) do + :ok -> :ok + {:error, _} = err -> err + end + end + + def print_output_stream(stream, printer, printer_state) do + Enum.reduce_while(stream, :ok, fn + {:error, err}, _ -> + {:halt, {:error, err}} + + val, _ -> + printer.print_output(val, printer_state) + {:cont, :ok} + end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/parser.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/parser.ex new file mode 100644 index 0000000000..28c4df2aa4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/parser.ex @@ -0,0 +1,311 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Parser do + alias RabbitMQ.CLI.{CommandBehaviour, FormatterBehaviour} + alias RabbitMQ.CLI.Core.{CommandModules, Config} + + def default_switches() do + [ + node: :atom, + quiet: :boolean, + silent: :boolean, + dry_run: :boolean, + vhost: :string, + # for backwards compatibility, + # not all commands support timeouts + timeout: :integer, + longnames: :boolean, + formatter: :string, + printer: :string, + file: :string, + script_name: :atom, + rabbitmq_home: :string, + mnesia_dir: :string, + plugins_dir: :string, + enabled_plugins_file: :string, + aliases_file: :string, + erlang_cookie: :atom, + help: :boolean, + print_stacktrace: :boolean + ] + end + + def default_aliases() do + [ + p: :vhost, + n: :node, + q: :quiet, + s: :silent, + l: :longnames, + # for backwards compatibility, + # not all commands support timeouts + t: :timeout, + "?": :help + ] + end + + @spec parse([String.t()]) :: + {command :: :no_command | atom() | {:suggest, String.t()}, command_name :: String.t(), + arguments :: [String.t()], options :: map(), + invalid :: [{String.t(), String.t() | nil}]} + + def parse(input) do + {parsed_args, options, invalid} = parse_global(input) + {command_name, command_module, arguments} = look_up_command(parsed_args, options) + + case command_module do + nil -> + {:no_command, command_name, arguments, options, invalid} + + {:suggest, _} = suggest -> + {suggest, command_name, arguments, options, invalid} + + {:alias, alias_module, alias_content} -> + {[_alias_command_name | cmd_arguments], cmd_options, cmd_invalid} = + parse_alias(input, command_name, alias_module, alias_content, options) + + {alias_module, command_name, cmd_arguments, cmd_options, cmd_invalid} + + command_module when is_atom(command_module) -> + {[^command_name | cmd_arguments], cmd_options, cmd_invalid} = + parse_command_specific(input, command_module, options) + + {command_module, command_name, cmd_arguments, cmd_options, cmd_invalid} + end + end + + def command_suggestion(_cmd_name, empty) when empty == %{} do + nil + end + def command_suggestion(typed, module_map) do + RabbitMQ.CLI.AutoComplete.suggest_command(typed, module_map) + end + + defp look_up_command(parsed_args, options) do + case parsed_args do + [cmd_name | arguments] -> + ## This is an optimisation for pluggable command discovery. + ## Most of the time a command will be from rabbitmqctl application + ## so there is not point in scanning plugins for potential commands + CommandModules.load_core(options) + core_commands = CommandModules.module_map_core() + + command = + case core_commands[cmd_name] do + nil -> + CommandModules.load(options) + module_map = CommandModules.module_map() + + module_map[cmd_name] || + command_alias(cmd_name, module_map, options) || + command_suggestion(cmd_name, module_map) + + c -> + c + end + + {cmd_name, command, arguments} + + [] -> + {"", nil, []} + end + end + + defp command_alias(cmd_name, module_map, options) do + aliases = load_aliases(options) + + case aliases[cmd_name] do + nil -> + nil + + [alias_cmd_name | _] = alias_content -> + case module_map[alias_cmd_name] do + nil -> nil + module -> {:alias, module, alias_content} + end + end + end + + defp load_aliases(options) do + aliases_file = Config.get_option(:aliases_file, options) + + case aliases_file && File.read(aliases_file) do + ## No aliases file + nil -> + %{} + + {:ok, content} -> + String.split(content, "\n") + |> Enum.reduce( + %{}, + fn str, acc -> + case String.split(str, "=", parts: 2) do + [alias_name, alias_string] -> + Map.put(acc, String.trim(alias_name), OptionParser.split(alias_string)) + + _ -> + acc + end + end + ) + + {:error, err} -> + IO.puts(:stderr, "Error reading aliases file #{aliases_file}: #{err}") + %{} + end + end + + defp parse_alias(input, command_name, module, alias_content, options) do + {pre_command_options, tail, invalid} = parse_global_head(input) + [^command_name | other] = tail + aliased_input = alias_content ++ other + {args, options, command_invalid} = parse_command_specific(aliased_input, module, options) + merged_options = Map.merge(options, pre_command_options) + {args, merged_options, command_invalid ++ invalid} + end + + def parse_command_specific(input, command, options \\ %{}) do + formatter = Config.get_formatter(command, options) + + switches = build_switches(default_switches(), command, formatter) + aliases = build_aliases(default_aliases(), command, formatter) + parse_generic(input, switches, aliases) + end + + def parse_global_head(input) do + switches = default_switches() + aliases = default_aliases() + + {options, tail, invalid} = + OptionParser.parse_head( + input, + strict: switches, + aliases: aliases, + allow_nonexistent_atoms: true + ) + + norm_options = normalize_options(options, switches) |> Map.new() + {norm_options, tail, invalid} + end + + def parse_global(input) do + switches = default_switches() + aliases = default_aliases() + parse_generic(input, switches, aliases) + end + + defp parse_generic(input, switches, aliases) do + {options, args, invalid} = + OptionParser.parse( + input, + strict: switches, + aliases: aliases, + allow_nonexistent_atoms: true + ) + + norm_options = normalize_options(options, switches) |> Map.new() + {args, norm_options, invalid} + end + + defp build_switches(default, command, formatter) do + command_switches = CommandBehaviour.switches(command) + formatter_switches = FormatterBehaviour.switches(formatter) + + assert_no_conflict( + command, + command_switches, + formatter_switches, + :redefining_formatter_switches + ) + + merge_if_different( + default, + formatter_switches, + {:formatter_invalid, + {formatter, {:redefining_global_switches, default, formatter_switches}}} + ) + |> merge_if_different( + command_switches, + {:command_invalid, {command, {:redefining_global_switches, default, command_switches}}} + ) + end + + defp assert_no_conflict(command, command_fields, formatter_fields, err) do + merge_if_different( + formatter_fields, + command_fields, + {:command_invalid, {command, {err, formatter_fields, command_fields}}} + ) + + :ok + end + + defp build_aliases(default, command, formatter) do + command_aliases = CommandBehaviour.aliases(command) + formatter_aliases = FormatterBehaviour.aliases(formatter) + + assert_no_conflict(command, command_aliases, formatter_aliases, :redefining_formatter_aliases) + + merge_if_different( + default, + formatter_aliases, + {:formatter_invalid, {command, {:redefining_global_aliases, default, formatter_aliases}}} + ) + |> merge_if_different( + command_aliases, + {:command_invalid, {command, {:redefining_global_aliases, default, command_aliases}}} + ) + end + + defp merge_if_different(default, specific, error) do + case keyword_intersect(default, specific) do + [] -> + Keyword.merge(default, specific) + + conflicts -> + # if all conflicting keys are of the same type, + # that's acceptable + case Enum.all?( + conflicts, + fn c -> + Keyword.get(default, c) == Keyword.get(specific, c) + end + ) do + true -> Keyword.merge(default, specific) + false -> exit(error) + end + end + end + + defp keyword_intersect(one, two) do + one_keys = MapSet.new(Keyword.keys(one)) + two_keys = MapSet.new(Keyword.keys(two)) + intersection = MapSet.intersection(one_keys, two_keys) + + case Enum.empty?(intersection) do + true -> [] + false -> MapSet.to_list(intersection) + end + end + + defp normalize_options(options, switches) do + Enum.map( + options, + fn {key, option} -> + {key, normalize_type(option, switches[key])} + end + ) + end + + defp normalize_type(value, :atom) when is_binary(value) do + String.to_atom(value) + end + + defp normalize_type(value, _type) do + value + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/paths.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/paths.ex new file mode 100644 index 0000000000..0e90834771 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/paths.ex @@ -0,0 +1,55 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Paths do + alias RabbitMQ.CLI.Core.Config + import RabbitMQ.CLI.Core.Platform + + def plugins_dir(_, opts) do + plugins_dir(opts) + end + + def plugins_dir(opts) do + case Config.get_option(:plugins_dir, opts) do + nil -> + {:error, :no_plugins_dir} + + dir -> + paths = String.split(to_string(dir), path_separator()) + + case Enum.any?(paths, &File.dir?/1) do + true -> {:ok, dir} + false -> {:error, :plugins_dir_does_not_exist} + end + end + end + + def require_mnesia_dir(opts) do + case Application.get_env(:mnesia, :dir) do + nil -> + case Config.get_option(:mnesia_dir, opts) do + nil -> {:error, :mnesia_dir_not_found} + val -> Application.put_env(:mnesia, :dir, to_charlist(val)) + end + + _ -> + :ok + end + end + + def require_feature_flags_file(opts) do + case Application.get_env(:rabbit, :feature_flags_file) do + nil -> + case Config.get_option(:feature_flags_file, opts) do + nil -> {:error, :feature_flags_file_not_found} + val -> Application.put_env(:rabbit, :feature_flags_file, to_charlist(val)) + end + + _ -> + :ok + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/platform.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/platform.ex new file mode 100644 index 0000000000..561b2adb58 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/platform.ex @@ -0,0 +1,37 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Platform do + def path_separator() do + case :os.type() do + {:unix, _} -> ":" + {:win32, _} -> ";" + end + end + + def line_separator() do + case :os.type() do + {:unix, _} -> "\n" + {:win32, _} -> "\r\n" + end + end + + def os_name({:unix, :linux}) do + "Linux" + end + def os_name({:unix, :darwin}) do + "macOS" + end + def os_name({:unix, :freebsd}) do + "FreeBSD" + end + def os_name({:unix, name}) do + name |> to_string |> String.capitalize + end + def os_name({:win32, _}) do + "Windows" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/requires_rabbit_app_running.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/requires_rabbit_app_running.ex new file mode 100644 index 0000000000..7f5337a6e7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/requires_rabbit_app_running.ex @@ -0,0 +1,17 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be running +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.RequiresRabbitAppRunning do + defmacro __using__(_) do + quote do + def validate_execution_environment(args, opts) do + RabbitMQ.CLI.Core.Validators.rabbit_is_running(args, opts) + end + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/requires_rabbit_app_stopped.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/requires_rabbit_app_stopped.ex new file mode 100644 index 0000000000..48b2b6dcd0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/requires_rabbit_app_stopped.ex @@ -0,0 +1,17 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Should be used by commands that require rabbit app to be stopped +# but need no other execution environment validators. +defmodule RabbitMQ.CLI.Core.RequiresRabbitAppStopped do + defmacro __using__(_) do + quote do + def validate_execution_environment(args, opts) do + RabbitMQ.CLI.Core.Validators.rabbit_is_not_running(args, opts) + end + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex new file mode 100644 index 0000000000..666d7af065 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/validators.ex @@ -0,0 +1,115 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Provides common validation functions. +defmodule RabbitMQ.CLI.Core.Validators do + alias RabbitMQ.CLI.Core.Helpers + import RabbitMQ.CLI.Core.{CodePath, Paths} + + + def chain([validator | rest], args) do + case apply(validator, args) do + :ok -> chain(rest, args) + {:ok, _} -> chain(rest, args) + {:validation_failure, err} -> {:validation_failure, err} + {:error, err} -> {:validation_failure, err} + end + end + + def chain([], _) do + :ok + end + + def validate_step(:ok, step) do + case step.() do + {:error, err} -> {:validation_failure, err} + _ -> :ok + end + end + + def validate_step({:validation_failure, err}, _) do + {:validation_failure, err} + end + + def node_is_not_running(_, %{node: node_name}) do + case Helpers.node_running?(node_name) do + true -> {:validation_failure, :node_running} + false -> :ok + end + end + + def node_is_running(_, %{node: node_name}) do + case Helpers.node_running?(node_name) do + false -> {:validation_failure, :node_not_running} + true -> :ok + end + end + + def mnesia_dir_is_set(_, opts) do + case require_mnesia_dir(opts) do + :ok -> :ok + {:error, err} -> {:validation_failure, err} + end + end + + def feature_flags_file_is_set(_, opts) do + case require_feature_flags_file(opts) do + :ok -> :ok + {:error, err} -> {:validation_failure, err} + end + end + + def rabbit_is_loaded(_, opts) do + case require_rabbit(opts) do + :ok -> :ok + {:error, err} -> {:validation_failure, err} + end + end + + def rabbit_app_running?(%{node: node, timeout: timeout}) do + case :rabbit_misc.rpc_call(node, :rabbit, :is_running, [], timeout) do + true -> true + false -> false + other -> {:error, other} + end + end + + def rabbit_app_running?(_, opts) do + rabbit_app_running?(opts) + end + + def rabbit_is_running(args, opts) do + case rabbit_app_state(args, opts) do + :running -> :ok + :stopped -> {:validation_failure, :rabbit_app_is_stopped} + other -> other + end + end + + def rabbit_is_running_or_offline_flag_used(_args, %{offline: true}) do + :ok + end + + def rabbit_is_running_or_offline_flag_used(args, opts) do + rabbit_is_running(args, opts) + end + + def rabbit_is_not_running(args, opts) do + case rabbit_app_state(args, opts) do + :running -> {:validation_failure, :rabbit_app_is_running} + :stopped -> :ok + other -> other + end + end + + def rabbit_app_state(_, opts) do + case rabbit_app_running?(opts) do + true -> :running + false -> :stopped + {:error, err} -> {:error, err} + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/version.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/version.ex new file mode 100644 index 0000000000..bd5a24f9a0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/version.ex @@ -0,0 +1,24 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Core.Version do + @default_timeout 30_000 + + def local_version do + to_string(:rabbit_misc.version()) + end + + + def remote_version(node_name) do + remote_version(node_name, @default_timeout) + end + def remote_version(node_name, timeout) do + case :rabbit_misc.rpc_call(node_name, :rabbit_misc, :version, [], timeout) do + {:badrpc, _} = err -> err + val -> val + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_user_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_user_command.ex new file mode 100644 index 0000000000..514922cac9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_user_command.ex @@ -0,0 +1,98 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.AddUserCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers, Input} + import RabbitMQ.CLI.Core.Config, only: [output_less?: 1] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate(args, _) when length(args) < 1, do: {:validation_failure, :not_enough_args} + def validate(args, _) when length(args) > 2, do: {:validation_failure, :too_many_args} + def validate([_], _), do: :ok + def validate(["", _], _) do + {:validation_failure, {:bad_argument, "user cannot be an empty string"}} + end + def validate([_, _], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name} = opts) do + # note: blank passwords are currently allowed, they make sense + # e.g. when a user only authenticates using X.509 certificates. + # Credential validators can be used to require passwords of a certain length + # or following a certain pattern. This is a core server responsibility. MK. + case Input.infer_password("Password: ", opts) do + :eof -> {:error, :not_enough_args} + password -> :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :add_user, + [username, password, Helpers.cli_acting_user()] + ) + end + end + def run([username, password], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :add_user, + [username, password, Helpers.cli_acting_user()] + ) + end + + def output({:error, :not_enough_args}, _) do + {:error, ExitCodes.exit_dataerr(), "Password is not provided via argument or stdin"} + end + def output({:error, {:user_already_exists, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} already exists"}} + end + def output({:error, {:user_already_exists, username}}, _) do + {:error, ExitCodes.exit_software(), "User \"#{username}\" already exists"} + end + def output(:ok, %{formatter: "json", node: node_name}) do + m = %{ + "status" => "ok", + "node" => node_name, + "message" => "Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more." + } + {:ok, m} + end + def output(:ok, opts) do + case output_less?(opts) do + true -> + :ok + false -> + {:ok, "Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more."} + end + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "add_user <username> <password>" + + def usage_additional() do + [ + ["<username>", "Self-explanatory"], + ["<password>", "Password this user will authenticate with. Use a blank string to disable password-based authentication."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description() do + "Creates a new user in the internal database. This user will have no permissions for any virtual hosts by default." + end + + def banner([username | _], _), do: "Adding user \"#{username}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex new file mode 100644 index 0000000000..04c1e61106 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [description: :string, + tags: :string] + def aliases(), do: [d: :description] + + def merge_defaults(args, opts) do + {args, Map.merge(%{description: "", tags: ""}, opts)} + end + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([vhost], %{node: node_name, description: desc, tags: tags}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, desc, parse_tags(tags), Helpers.cli_acting_user()]) + end + def run([vhost], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, Helpers.cli_acting_user()]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "add_vhost <vhost> [--description <description> --tags \"<tag1>,<tag2>,<...>\"]" + + def usage_additional() do + [ + ["<vhost>", "Virtual host name"], + ["--description <description>", "Virtual host description"], + ["--tags <tags>", "Command separated list of tags"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def description(), do: "Creates a virtual host" + + def banner([vhost], _), do: "Adding vhost \"#{vhost}\" ..." + + # + # Implementation + # + + def parse_tags(tags) do + String.split(tags, ",", trim: true) + |> Enum.map(&String.trim/1) + |> Enum.map(&String.to_atom/1) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/authenticate_user_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/authenticate_user_command.ex new file mode 100644 index 0000000000..9913633b84 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/authenticate_user_command.ex @@ -0,0 +1,79 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.AuthenticateUserCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Input} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate(args, _) when length(args) < 1, do: {:validation_failure, :not_enough_args} + def validate(args, _) when length(args) > 2, do: {:validation_failure, :too_many_args} + def validate([_], _), do: :ok + def validate([_, _], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([user], %{node: node_name} = opts) do + # note: blank passwords are currently allowed, they make sense + # e.g. when a user only authenticates using X.509 certificates. + # Credential validators can be used to require passwords of a certain length + # or following a certain pattern. This is a core server responsibility. MK. + case Input.infer_password("Password: ", opts) do + :eof -> {:error, :not_enough_args} + password -> :rabbit_misc.rpc_call( + node_name, + :rabbit_access_control, + :check_user_pass_login, + [user, password] + ) + end + end + def run([user, password], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_access_control, + :check_user_pass_login, + [user, password] + ) + end + + def usage, do: "authenticate_user <username> <password>" + + def usage_additional() do + [ + ["<username>", "Username to use"], + ["<password>", "Password to use. Can be entered via stdin in interactive mode."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Attempts to authenticate a user. Exits with a non-zero code if authentication fails." + + def banner([username | _], _), do: "Authenticating user \"#{username}\" ..." + + def output({:error, :not_enough_args}, _) do + {:error, ExitCodes.exit_software(), "Password is not provided via argument or stdin"} + end + def output({:refused, user, msg, args}, _) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_dataerr(), + "Error: failed to authenticate user \"#{user}\"\n" <> + to_string(:io_lib.format(msg, args))} + end + def output({:ok, _user}, _) do + {:ok, "Success"} + end + + use RabbitMQ.CLI.DefaultOutput +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/autocomplete_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/autocomplete_command.ex new file mode 100644 index 0000000000..19deb74f79 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/autocomplete_command.ex @@ -0,0 +1,53 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.AutocompleteCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.{Config, DocGuide} + + def scopes(), do: [:ctl, :diagnostics, :plugins, :queues] + + def distribution(_), do: :none + + def merge_defaults(args, opts) do + # enforce --silent as shell completion does not + # expect to receive any additional output, so the command + # is not really interactive + {args, Map.merge(opts, %{silent: true})} + end + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + + def run(args, %{script_name: script_name}) do + {:stream, RabbitMQ.CLI.AutoComplete.complete(script_name, args)} + end + def run(args, opts) do + script_name = Config.get_system_option(:script_name, opts) + + {:stream, RabbitMQ.CLI.AutoComplete.complete(script_name, args)} + end + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "autocomplete [prefix]" + end + + def banner(_args, _opts) do + nil + end + + def usage_doc_guides() do + [ + DocGuide.cli() + ] + end + + def help_section(), do: :help + + def description(), do: "Provides command name autocomplete variants" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/await_online_nodes_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/await_online_nodes_command.ex new file mode 100644 index 0000000000..f0d1df6a02 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/await_online_nodes_command.ex @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.AwaitOnlineNodesCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 300_000 + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, Map.merge(opts, %{timeout: timeout})} + end + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsOnePositiveIntegerArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([count], %{node: node_name, timeout: timeout}) do + {n, _} = Integer.parse(count) + :rabbit_misc.rpc_call(node_name, :rabbit_nodes, :await_running_count, [n, timeout]) + end + + def output({:error, :timeout}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: timed out while waiting. Not enough nodes joined #{node_name}'s cluster."} + end + + use RabbitMQ.CLI.DefaultOutput + + def banner([count], %{node: node_name, timeout: timeout}) when is_number(timeout) do + "Will wait for at least #{count} nodes to join the cluster of #{node_name}. Timeout: #{ + trunc(timeout / 1000) + } seconds." + end + + def banner([count], %{node: node_name, timeout: _timeout}) do + "Will wait for at least #{count} nodes to join the cluster of #{node_name}." + end + + def usage() do + "await_online_nodes <count>" + end + + def usage_additional() do + [ + ["<count>", "how many cluster members must be up in order for this command to exit. When <count> is 1, always exits immediately."] + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Waits for <count> nodes to join the cluster" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/await_startup_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/await_startup_command.ex new file mode 100644 index 0000000000..9a898224ce --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/await_startup_command.ex @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.AwaitStartupCommand do + @moduledoc """ + Waits until target node is fully booted. If the node is already running, + returns immediately. + + This command is meant to be used when automating deployments. + See also `AwaitOnlineNodesCommand`. + """ + + import RabbitMQ.CLI.Core.Config, only: [output_less?: 1] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 300_000 + + def merge_defaults(args, opts) do + {args, Map.merge(%{timeout: @default_timeout}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout} = opts) do + :rabbit_misc.rpc_call(node_name, :rabbit, :await_startup, [ + node_name, + not output_less?(opts), + timeout + ]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "await_startup" + + def help_section(), do: :node_management + + def description(), do: "Waits for the RabbitMQ application to start on the target node" + + def banner(_, _), do: nil +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/cancel_sync_queue_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/cancel_sync_queue_command.ex new file mode 100644 index 0000000000..2858040039 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/cancel_sync_queue_command.ex @@ -0,0 +1,52 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.CancelSyncQueueCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([queue], %{vhost: vhost, node: node_name}) do + :rpc.call( + node_name, + :rabbit_mirror_queue_misc, + :cancel_sync_queue, + [:rabbit_misc.r(vhost, :queue, queue)], + :infinity + ) + end + + def usage, do: "cancel_sync_queue [--vhost <vhost>] <queue>" + + def usage_additional() do + [ + ["<queue>", "Queue name"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.mirroring() + ] + end + + def help_section(), do: :replication + + def description(), do: "Instructs a synchronising mirrored queue to stop synchronising itself" + + def banner([queue], %{vhost: vhost, node: _node}) do + "Stopping synchronising queue '#{queue}' in vhost '#{vhost}' ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/change_cluster_node_type_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/change_cluster_node_type_command.ex new file mode 100644 index 0000000000..93fc9c7da0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/change_cluster_node_type_command.ex @@ -0,0 +1,87 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ChangeClusterNodeTypeCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, opts} + end + + def validate([], _), do: {:validation_failure, :not_enough_args} + + # node type + def validate(["disc"], _), do: :ok + def validate(["disk"], _), do: :ok + def validate(["ram"], _), do: :ok + + def validate([_], _), + do: {:validation_failure, {:bad_argument, "The node type must be either disc or ram."}} + + def validate(_, _), do: {:validation_failure, :too_many_args} + + use RabbitMQ.CLI.Core.RequiresRabbitAppStopped + + def run([node_type_arg], %{node: node_name}) do + normalized_type = normalize_type(String.to_atom(node_type_arg)) + current_type = :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :node_type, []) + + case current_type do + ^normalized_type -> + {:ok, "Node type is already #{normalized_type}"} + + _ -> + :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :change_cluster_node_type, [ + normalized_type + ]) + end + end + + def usage() do + "change_cluster_node_type <disc | ram>" + end + + def usage_additional() do + [ + ["<disc | ram>", "New node type"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.clustering() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Changes the type of the cluster node" + + def banner([node_type], %{node: node_name}) do + "Turning #{node_name} into a #{node_type} node" + end + + def output({:error, :mnesia_unexpectedly_running}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + RabbitMQ.CLI.DefaultOutput.mnesia_running_error(node_name)} + end + + use RabbitMQ.CLI.DefaultOutput + + defp normalize_type(:ram) do + :ram + end + + defp normalize_type(:disc) do + :disc + end + + defp normalize_type(:disk) do + :disc + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/change_password_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/change_password_command.ex new file mode 100644 index 0000000000..b0dec0a824 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/change_password_command.ex @@ -0,0 +1,76 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ChangePasswordCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers, Input} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate(args, _) when length(args) < 1, do: {:validation_failure, :not_enough_args} + def validate(args, _) when length(args) > 2, do: {:validation_failure, :too_many_args} + def validate([_], _), do: :ok + def validate([_, _], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name} = opts) do + # note: blank passwords are currently allowed, they make sense + # e.g. when a user only authenticates using X.509 certificates. + # Credential validators can be used to require passwords of a certain length + # or following a certain pattern. This is a core server responsibility. MK. + case Input.infer_password("Password: ", opts) do + :eof -> {:error, :not_enough_args} + password -> :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :change_password, + [username, password, Helpers.cli_acting_user()] + ) + end + end + def run([username, password], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :change_password, + [username, password, Helpers.cli_acting_user()] + ) + end + + def output({:error, :not_enough_args}, _) do + {:error, ExitCodes.exit_software(), "Password is not provided via argument or stdin"} + end + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exists"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User \"#{username}\" does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "change_password <username> <password>" + + def usage_additional() do + [ + ["<username>", "Name of the user whose password should be changed"], + ["<password>", "New password to set"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Changes the user password" + + def banner([user | _], _), do: "Changing password for user \"#{user}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_global_parameter_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_global_parameter_command.ex new file mode 100644 index 0000000000..c5cedeb96a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_global_parameter_command.ex @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearGlobalParameterCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([key], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime_parameters, + :clear_global, + [key, Helpers.cli_acting_user()] + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "clear_global_parameter <name>" + + def usage_additional() do + [ + ["<name>", "parameter name (identifier)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :parameters + + def description(), do: "Clears a global runtime parameter" + + def banner([key], _) do + "Clearing global runtime parameter \"#{key}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_operator_policy_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_operator_policy_command.ex new file mode 100644 index 0000000000..4b77d4cb38 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_operator_policy_command.ex @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearOperatorPolicyCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([key], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_policy, :delete_op, [ + vhost, + key, + Helpers.cli_acting_user() + ]) + end + + def usage, do: "clear_operator_policy [--vhost <vhost>] <name>" + + def usage_additional() do + [ + ["<name>", "policy name (identifier)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :policies + + def description(), do: "Clears an operator policy" + + def banner([key], %{vhost: vhost}) do + "Clearing operator policy \"#{key}\" on vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_parameter_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_parameter_command.ex new file mode 100644 index 0000000000..3997b1b61f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_parameter_command.ex @@ -0,0 +1,60 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearParameterCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + def validate(args, _) when is_list(args) and length(args) < 2 do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + + def validate([_, _], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([component_name, key], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime_parameters, + :clear, + [vhost, component_name, key, Helpers.cli_acting_user()] + ) + end + + def usage, do: "clear_parameter [--vhost <vhost>] <component_name> <name>" + + def usage_additional() do + [ + ["<component_name>", "component name"], + ["<name>", "parameter name (identifier)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :parameters + + def description(), do: "Clears a runtime parameter." + + def banner([component_name, key], %{vhost: vhost}) do + "Clearing runtime parameter \"#{key}\" for component \"#{component_name}\" on vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_password_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_password_command.ex new file mode 100644 index 0000000000..398af4813b --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_password_command.ex @@ -0,0 +1,46 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearPasswordCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_user] = args, %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :clear_password, + args ++ [Helpers.cli_acting_user()] + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "clear_password <username>" + + def usage_additional() do + [ + ["<username>", "Name of the user whose password should be cleared"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Clears (resets) password and disables password login for a user" + + def banner([user], _), do: "Clearing password for user \"#{user}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_permissions_command.ex new file mode 100644 index 0000000000..2fd129fffa --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_permissions_command.ex @@ -0,0 +1,61 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :clear_permissions, [ + username, + vhost, + Helpers.cli_acting_user() + ]) + end + + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exist"}} + end + def output({:error, {:no_such_vhost, vhost}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User #{username} does not exist"} + end + def output({:error, {:no_such_vhost, vhost}}, _) do + {:error, "Virtual host #{vhost} does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "clear_permissions [--vhost <vhost>] <username>" + + def usage_additional() do + [ + ["<username>", "Name of the user whose permissions should be revoked"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :access_control + def description(), do: "Revokes user permissions for a vhost" + + def banner([username], %{vhost: vhost}) do + "Clearing permissions for user \"#{username}\" in vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_policy_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_policy_command.ex new file mode 100644 index 0000000000..057c2e8c24 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_policy_command.ex @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearPolicyCommand do + alias RabbitMQ.CLI.Core.{Helpers, DocGuide} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([key], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_policy, :delete, [ + vhost, + key, + Helpers.cli_acting_user() + ]) + end + + def usage, do: "clear_policy [--vhost <vhost>] <name>" + + def usage_additional() do + [ + ["<name>", "Name of policy to clear (remove)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :policies + + def description(), do: "Clears (removes) a policy" + + def banner([key], %{vhost: vhost}) do + "Clearing policy \"#{key}\" on vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_topic_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_topic_permissions_command.ex new file mode 100644 index 0000000000..5d0b249db6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_topic_permissions_command.ex @@ -0,0 +1,85 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearTopicPermissionsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + + def validate([_], _), do: :ok + def validate([_, _], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :clear_topic_permissions, [ + username, + vhost, + Helpers.cli_acting_user() + ]) + end + + def run([username, exchange], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :clear_topic_permissions, [ + username, + vhost, + exchange, + Helpers.cli_acting_user() + ]) + end + + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exist"}} + end + def output({:error, {:no_such_vhost, vhost}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User #{username} does not exist"} + end + def output({:error, {:no_such_vhost, vhost}}, _) do + {:error, "Virtual host #{vhost} does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "clear_topic_permissions [--vhost <vhost>] <username> [<exchange>]" + + def usage_additional() do + [ + ["<username>", "Name of the user whose topic permissions should be revoked"], + ["<exchange>", "Exchange the permissions are for"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :access_control + def description(), do: "Clears user topic permissions for a vhost or exchange" + + def banner([username], %{vhost: vhost}) do + "Clearing topic permissions for user \"#{username}\" in vhost \"#{vhost}\" ..." + end + + def banner([username, exchange], %{vhost: vhost}) do + "Clearing topic permissions on \"#{exchange}\" for user \"#{username}\" in vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_user_limits_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_user_limits_command.ex new file mode 100644 index 0000000000..301de613bb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_user_limits_command.ex @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearUserLimitsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + + def run([username, limit_type], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :clear_user_limits, [ + username, + limit_type, + Helpers.cli_acting_user() + ]) + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def usage, do: "clear_user_limits username <limit_type> | all" + + def usage_additional() do + [ + ["<limit_type>", "Limit type, must be max-connections or max-channels"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Clears user connection/channel limits" + + def banner([username, "all"], %{}) do + "Clearing all limits for user \"#{username}\" ..." + end + def banner([username, limit_type], %{}) do + "Clearing \"#{limit_type}\" limit for user \"#{username}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_vhost_limits_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_vhost_limits_command.ex new file mode 100644 index 0000000000..a73f0ff670 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/clear_vhost_limits_command.ex @@ -0,0 +1,43 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClearVhostLimitsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost_limit, :clear, [ + vhost, + Helpers.cli_acting_user() + ]) + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def usage, do: "clear_vhost_limits [--vhost <vhost>]" + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def description(), do: "Clears virtual host limits" + + def banner([], %{vhost: vhost}) do + "Clearing vhost \"#{vhost}\" limits ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/close_all_connections_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/close_all_connections_command.ex new file mode 100644 index 0000000000..d4c5b5f17a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/close_all_connections_command.ex @@ -0,0 +1,124 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.CloseAllConnectionsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [global: :boolean, per_connection_delay: :integer, limit: :integer] + + def merge_defaults(args, opts) do + {args, Map.merge(%{global: false, vhost: "/", per_connection_delay: 0, limit: 0}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([explanation], %{ + node: node_name, + vhost: vhost, + global: global_opt, + per_connection_delay: delay, + limit: limit + }) do + conns = + case global_opt do + false -> + per_vhost = + :rabbit_misc.rpc_call(node_name, :rabbit_connection_tracking, :list, [vhost]) + + apply_limit(per_vhost, limit) + + true -> + :rabbit_misc.rpc_call(node_name, :rabbit_connection_tracking, :list_on_node, [node_name]) + end + + case conns do + {:badrpc, _} = err -> + err + + _ -> + :rabbit_misc.rpc_call( + node_name, + # As of 3.7.15, this function was moved to the rabbit_connection_tracking module. + # rabbit_connection_tracking_handler still contains a delegating function with the same name. + # Continue using this one for now for maximum CLI/server version compatibility. MK. + :rabbit_connection_tracking_handler, + :close_connections, + [conns, explanation, delay] + ) + + {:ok, "Closed #{length(conns)} connections"} + end + end + + def run(args, %{ + node: node_name, + global: global_opt, + per_connection_delay: delay, + limit: limit + }) do + run(args, %{ + node: node_name, + vhost: nil, + global: global_opt, + per_connection_delay: delay, + limit: limit + }) + end + + def output({:stream, stream}, _opts) do + {:stream, Stream.filter(stream, fn x -> x != :ok end)} + end + use RabbitMQ.CLI.DefaultOutput + + def banner([explanation], %{node: node_name, global: true}) do + "Closing all connections to node #{node_name} (across all vhosts), reason: #{explanation}..." + end + + def banner([explanation], %{vhost: vhost, limit: 0}) do + "Closing all connections in vhost #{vhost}, reason: #{explanation}..." + end + + def banner([explanation], %{vhost: vhost, limit: limit}) do + "Closing #{limit} connections in vhost #{vhost}, reason: #{explanation}..." + end + + def usage do + "close_all_connections [--vhost <vhost> --limit <limit>] [-n <node> --global] [--per-connection-delay <delay>] <explanation>" + end + + def usage_additional do + [ + ["--global", "consider connections across all virtual hosts"], + ["--limit <number>", "close up to this many connections"], + ["--per-connection-delay <milliseconds>", "inject a delay between closures"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.connections() + ] + end + + def help_section(), do: :operations + + def description(), do: "Instructs the broker to close all connections for the specified vhost or entire RabbitMQ node" + + # + # Implementation + # + + defp apply_limit(conns, 0) do + conns + end + + defp apply_limit(conns, number) do + :lists.sublist(conns, number) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/close_connection_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/close_connection_command.ex new file mode 100644 index 0000000000..371c582b19 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/close_connection_command.ex @@ -0,0 +1,46 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.CloseConnectionCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([pid, explanation], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_networking, :close_connection, [ + :rabbit_misc.string_to_pid(pid), + explanation + ]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "close_connection <connection pid> <explanation>" + + def usage_additional do + [ + ["<connection pid>", "connection identifier (Erlang PID), see list_connections"], + ["<explanation>", "reason for connection closure"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.connections() + ] + end + + def help_section(), do: :operations + + def description(), do: "Instructs the broker to close the connection associated with the Erlang process id" + + def banner([pid, explanation], _), do: "Closing connection #{pid}, reason: #{explanation}..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/cluster_status_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/cluster_status_command.ex new file mode 100644 index 0000000000..20f96e8075 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/cluster_status_command.ex @@ -0,0 +1,285 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ClusterStatusCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.{Alarms, ANSI, Listeners, Platform, FeatureFlags} + import RabbitMQ.CLI.Core.Distribution, only: [per_node_timeout: 2] + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 60_000 + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, Map.merge(%{timeout: timeout}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :status, []) do + {:badrpc, _} = err -> + err + + status -> + case :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :cluster_nodes, [:running]) do + {:badrpc, _} = err -> + err + + {:error, {:corrupt_or_missing_cluster_files, _, _}} -> + {:error, "Could not read mnesia files containing cluster status"} + + nodes -> + count = length(nodes) + alarms_by_node = Enum.map(nodes, fn n -> alarms_by_node(n, per_node_timeout(timeout, count)) end) + listeners_by_node = Enum.map(nodes, fn n -> listeners_of(n, per_node_timeout(timeout, count)) end) + versions_by_node = Enum.map(nodes, fn n -> versions_by_node(n, per_node_timeout(timeout, count)) end) + maintenance_status_by_node = Enum.map(nodes, + fn n -> maintenance_status_by_node(n, per_node_timeout(timeout, count)) end) + + feature_flags = case :rabbit_misc.rpc_call(node_name, :rabbit_ff_extra, :cli_info, [], timeout) do + {:badrpc, {:EXIT, {:undef, _}}} -> [] + {:badrpc, _} = err -> err + val -> val + end + + status ++ + [{:alarms, alarms_by_node}] ++ + [{:listeners, listeners_by_node}] ++ + [{:versions, versions_by_node}] ++ + [{:maintenance_status, maintenance_status_by_node}] ++ + [{:feature_flags, feature_flags}] + end + end + end + + def output({:error, :timeout}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: timed out while waiting for a response from #{node_name}."} + end + + def output(result, %{formatter: "erlang"}) do + {:ok, result} + end + + def output(result, %{formatter: "json"}) when is_list(result) do + # format more data structures as map for sensible JSON output + m = result_map(result) + |> Map.update(:alarms, [], fn xs -> alarm_maps(xs) end) + |> Map.update(:listeners, %{}, fn m -> + Enum.map(m, fn {n, xs} -> {n, listener_maps(xs)} end) |> Enum.into(%{}) + end) + + {:ok, m} + end + + def output(result, %{node: node_name}) when is_list(result) do + m = result_map(result) + + cluster_name_section = [ + "#{bright("Basics")}\n", + "Cluster name: #{m[:cluster_name]}" + ] + + disk_nodes_section = [ + "\n#{bright("Disk Nodes")}\n", + ] ++ node_lines(m[:disk_nodes]) + + ram_nodes_section = case m[:ram_nodes] do + [] -> [] + xs -> [ + "\n#{bright("RAM Nodes")}\n", + ] ++ node_lines(xs) + end + + running_nodes_section = [ + "\n#{bright("Running Nodes")}\n", + ] ++ node_lines(m[:running_nodes]) + + versions_section = [ + "\n#{bright("Versions")}\n", + ] ++ version_lines(m[:versions]) + + alarms_section = [ + "\n#{bright("Alarms")}\n", + ] ++ case m[:alarms] do + [] -> ["(none)"] + xs -> alarm_lines(xs, node_name) + end + + partitions_section = [ + "\n#{bright("Network Partitions")}\n" + ] ++ case map_size(m[:partitions]) do + 0 -> ["(none)"] + _ -> partition_lines(m[:partitions]) + end + + listeners_section = [ + "\n#{bright("Listeners")}\n" + ] ++ case map_size(m[:listeners]) do + 0 -> ["(none)"] + _ -> Enum.reduce(m[:listeners], [], fn {node, listeners}, acc -> + acc ++ listener_lines(listeners, node) + end) + end + + maintenance_section = [ + "\n#{bright("Maintenance status")}\n", + ] ++ maintenance_lines(m[:maintenance_status]) + + feature_flags_section = [ + "\n#{bright("Feature flags")}\n" + ] ++ case Enum.count(m[:feature_flags]) do + 0 -> ["(none)"] + _ -> feature_flag_lines(m[:feature_flags]) + end + + lines = cluster_name_section ++ disk_nodes_section ++ ram_nodes_section ++ running_nodes_section ++ + versions_section ++ maintenance_section ++ alarms_section ++ partitions_section ++ + listeners_section ++ feature_flags_section + + {:ok, Enum.join(lines, line_separator())} + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.String + + def usage, do: "cluster_status" + + def usage_doc_guides() do + [ + DocGuide.clustering(), + DocGuide.cluster_formation(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Displays all the nodes in the cluster grouped by node type, together with the currently running nodes" + + def banner(_, %{node: node_name}), do: "Cluster status of node #{node_name} ..." + + # + # Implementation + # + + defp result_map(result) do + # [{nodes,[{disc,[hare@warp10,rabbit@warp10]},{ram,[flopsy@warp10]}]}, + # {running_nodes,[flopsy@warp10,hare@warp10,rabbit@warp10]}, + # {cluster_name,<<"rabbit@localhost">>}, + # {partitions,[{flopsy@warp10,[rabbit@vagrant]}, + # {hare@warp10,[rabbit@vagrant]}]}, + # {alarms,[{flopsy@warp10,[]}, + # {hare@warp10,[]}, + # {rabbit@warp10,[{resource_limit,memory,rabbit@warp10}]}]}] + %{ + cluster_name: Keyword.get(result, :cluster_name), + disk_nodes: result |> Keyword.get(:nodes, []) |> Keyword.get(:disc, []), + ram_nodes: result |> Keyword.get(:nodes, []) |> Keyword.get(:ram, []), + running_nodes: result |> Keyword.get(:running_nodes, []) |> Enum.map(&to_string/1), + alarms: Keyword.get(result, :alarms) |> Keyword.values |> Enum.concat |> Enum.uniq, + maintenance_status: Keyword.get(result, :maintenance_status, []) |> Enum.into(%{}), + partitions: Keyword.get(result, :partitions, []) |> Enum.into(%{}), + listeners: Keyword.get(result, :listeners, []) |> Enum.into(%{}), + versions: Keyword.get(result, :versions, []) |> Enum.into(%{}), + feature_flags: Keyword.get(result, :feature_flags, []) |> Enum.map(fn ff -> Enum.into(ff, %{}) end) + } + end + + defp alarms_by_node(node, timeout) do + alarms = case :rabbit_misc.rpc_call(to_atom(node), :rabbit, :alarms, [], timeout) do + {:badrpc, _} -> [] + xs -> xs + end + + {node, alarms} + end + + defp listeners_of(node, timeout) do + # This may seem inefficient since this call returns all known listeners + # in the cluster, so why do we run it on every node? See the badrpc clause, + # some nodes may be inavailable or partitioned from other nodes. This way we + # gather as complete a picture as possible. MK. + listeners = case :rabbit_misc.rpc_call(to_atom(node), :rabbit_networking, :active_listeners, [], timeout) do + {:badrpc, _} -> [] + xs -> xs + end + + {node, listeners_on(listeners, node)} + end + + defp versions_by_node(node, timeout) do + {rmq_name, rmq_vsn, otp_vsn} = case :rabbit_misc.rpc_call( + to_atom(node), :rabbit, :product_info, [], timeout) do + {:badrpc, _} -> + {nil, nil, nil} + map -> + %{:otp_release => otp} = map + name = case map do + %{:product_name => v} -> v + %{:product_base_name => v} -> v + end + vsn = case map do + %{:product_version => v} -> v + %{:product_base_version => v} -> v + end + {name, vsn, otp} + end + + {node, %{rabbitmq_name: to_string(rmq_name), rabbitmq_version: to_string(rmq_vsn), erlang_version: to_string(otp_vsn)}} + end + + defp maintenance_status_by_node(node, timeout) do + target = to_atom(node) + result = case :rabbit_misc.rpc_call(target, + :rabbit_maintenance, :status_local_read, [target], timeout) do + {:badrpc, _} -> "unknown" + :regular -> "not under maintenance" + :draining -> magenta("marked for maintenance") + # forward compatibility: should we figure out a way to know when + # draining completes (it involves inherently asynchronous cluster + # operations such as quorum queue leader re-election), we'd introduce + # a new state + :drained -> magenta("marked for maintenance") + value -> to_string(value) + end + + {node, result} + end + + defp node_lines(nodes) do + Enum.map(nodes, &to_string/1) |> Enum.sort + end + + defp version_lines(mapping) do + Enum.map(mapping, fn {node, %{rabbitmq_name: rmq_name, rabbitmq_version: rmq_vsn, erlang_version: otp_vsn}} -> + "#{node}: #{rmq_name} #{rmq_vsn} on Erlang #{otp_vsn}" + end) + end + + defp partition_lines(mapping) do + Enum.map(mapping, fn {node, unreachable_peers} -> "Node #{node} cannot communicate with #{Enum.join(unreachable_peers, ", ")}" end) + end + + defp maintenance_lines(mapping) do + Enum.map(mapping, fn {node, status} -> "Node: #{node}, status: #{status}" end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/decode_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/decode_command.ex new file mode 100644 index 0000000000..015617b102 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/decode_command.ex @@ -0,0 +1,116 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +alias RabbitMQ.CLI.Core.Helpers + +defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def switches() do + [ + cipher: :atom, + hash: :atom, + iterations: :integer + ] + end + + def distribution(_), do: :none + + def merge_defaults(args, opts) do + with_defaults = Map.merge(%{ + cipher: :rabbit_pbe.default_cipher(), + hash: :rabbit_pbe.default_hash(), + iterations: :rabbit_pbe.default_iterations() + }, opts) + {args, with_defaults} + end + + def validate(args, _) when length(args) < 2 do + {:validation_failure, {:not_enough_args, "Please provide a value to decode and a passphrase"}} + end + + def validate(args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + + def validate(args, opts) when length(args) === 2 do + case {supports_cipher(opts.cipher), supports_hash(opts.hash), opts.iterations > 0} do + {false, _, _} -> + {:validation_failure, {:bad_argument, "The requested cipher is not supported"}} + + {_, false, _} -> + {:validation_failure, {:bad_argument, "The requested hash is not supported"}} + + {_, _, false} -> + {:validation_failure, + {:bad_argument, + "The requested number of iterations is incorrect (must be a positive integer)"}} + + {true, true, true} -> + :ok + end + end + + def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do + try do + term_value = Helpers.evaluate_input_as_term(value) + + term_to_decrypt = + case term_value do + {:encrypted, _} = encrypted -> + encrypted + _ -> + {:encrypted, term_value} + end + + result = :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, term_to_decrypt) + {:ok, result} + catch + _, _ -> + {:error, + "Failed to decrypt the value. Things to check: is the passphrase correct? Are the cipher and hash algorithms the same as those used for encryption?"} + end + end + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def banner([_, _], _) do + "Decrypting value ..." + end + + def usage, do: "decode value passphrase [--cipher <cipher>] [--hash <hash>] [--iterations <iterations>]" + + def usage_additional() do + [ + ["<value>", "config value to decode"], + ["<passphrase>", "passphrase to use with the config value encryption key"], + ["--cipher <cipher>", "cipher suite to use"], + ["--hash <hash>", "hashing function to use"], + ["--iterations <iterations>", "number of iteration to apply"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.configuration() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Decrypts an encrypted configuration value" + + # + # Implementation + # + + defp supports_cipher(cipher), do: Enum.member?(:rabbit_pbe.supported_ciphers(), cipher) + + defp supports_hash(hash), do: Enum.member?(:rabbit_pbe.supported_hashes(), hash) +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_queue_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_queue_command.ex new file mode 100644 index 0000000000..5e5fe9b9c0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_queue_command.ex @@ -0,0 +1,125 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [if_empty: :boolean, if_unused: :boolean, timeout: :integer] + def aliases(), do: [e: :if_empty, u: :if_unused, t: :timeout] + + def merge_defaults(args, opts) do + { + args, + Map.merge(%{if_empty: false, if_unused: false, vhost: "/"}, opts) + } + end + + def validate([], _opts) do + {:validation_failure, :not_enough_args} + end + + def validate(args, _opts) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate([""], _opts) do + { + :validation_failure, + {:bad_argument, "queue name cannot be an empty string"} + } + end + + def validate([_], _opts) do + :ok + end + + ## Validate rabbit application is running + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([qname], %{ + node: node, + vhost: vhost, + if_empty: if_empty, + if_unused: if_unused, + timeout: timeout + }) do + ## Generate queue resource name from queue name and vhost + queue_resource = :rabbit_misc.r(vhost, :queue, qname) + ## Lookup a queue on broker node using resource name + case :rabbit_misc.rpc_call(node, :rabbit_amqqueue, :lookup, [queue_resource]) do + {:ok, queue} -> + ## Delete queue + :rabbit_misc.rpc_call( + node, + :rabbit_amqqueue, + :delete, + [queue, if_unused, if_empty, "cli_user"], + timeout + ) + + {:error, _} = error -> + error + end + end + + def output({:error, :not_found}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage(), "Queue not found"} + end + + def output({:error, :not_empty}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage(), "Queue is not empty"} + end + + def output({:error, :in_use}, _options) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage(), "Queue is in use"} + end + + def output({:ok, qlen}, _options) do + {:ok, "Queue was successfully deleted with #{qlen} messages"} + end + + ## Use default output for all non-special case outputs + use RabbitMQ.CLI.DefaultOutput + + def banner([qname], %{vhost: vhost, if_empty: if_empty, if_unused: if_unused}) do + if_empty_str = + case if_empty do + true -> ["if queue is empty "] + false -> [] + end + + if_unused_str = + case if_unused do + true -> ["if queue is unused "] + false -> [] + end + + "Deleting queue '#{qname}' on vhost '#{vhost}' " <> + Enum.join(Enum.concat([if_empty_str, if_unused_str]), "and ") <> "..." + end + + def usage(), do: "delete_queue <queue_name> [--if-empty|-e] [--if-unused|-u]" + + def usage_additional() do + [ + ["<queue_name>", "name of the queue to delete"], + ["--if-empty", "delete the queue if it is empty (has no messages ready for delivery)"], + ["--if-unused", "delete the queue only if it has no consumers"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.queues() + ] + end + + def help_section(), do: :queues + + def description(), do: "Deletes a queue" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_user_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_user_command.ex new file mode 100644 index 0000000000..84f00a96f4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_user_command.ex @@ -0,0 +1,52 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.DeleteUserCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :delete_user, + [username, Helpers.cli_acting_user()] + ) + end + + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exists"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User \"#{username}\" does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "delete_user <username>" + + def usage_additional() do + [ + ["<username>", "Name of the user to delete."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Removes a user from the internal database. Has no effect on users provided by external backends such as LDAP" + + def banner([arg], _), do: "Deleting user \"#{arg}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_vhost_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_vhost_command.ex new file mode 100644 index 0000000000..8ff6e1f047 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/delete_vhost_command.ex @@ -0,0 +1,41 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.DeleteVhostCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([arg], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost, :delete, [arg, Helpers.cli_acting_user()]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "delete_vhost <vhost>" + + def usage_additional() do + [ + ["<vhost>", "Name of the virtual host to delete."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def description(), do: "Deletes a virtual host" + + def banner([arg], _), do: "Deleting vhost \"#{arg}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/enable_feature_flag_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/enable_feature_flag_command.ex new file mode 100644 index 0000000000..6af5a79e49 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/enable_feature_flag_command.ex @@ -0,0 +1,60 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts), do: {args, opts} + + def validate([], _), do: {:validation_failure, :not_enough_args} + def validate([_|_] = args, _) when length(args) > 1, do: {:validation_failure, :too_many_args} + def validate([""], _), do: {:validation_failure, {:bad_argument, "feature_flag cannot be an empty string."}} + def validate([_], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run(["all"], %{node: node_name}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :enable_all, []) do + # Server does not support feature flags, consider none are available. + # See rabbitmq/rabbitmq-cli#344 for context. MK. + {:badrpc, {:EXIT, {:undef, _}}} -> {:error, :unsupported} + {:badrpc, _} = err -> err + other -> other + end + end + + def run([feature_flag], %{node: node_name}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :enable, [String.to_atom(feature_flag)]) do + # Server does not support feature flags, consider none are available. + # See rabbitmq/rabbitmq-cli#344 for context. MK. + {:badrpc, {:EXIT, {:undef, _}}} -> {:error, :unsupported} + {:badrpc, _} = err -> err + other -> other + end + end + + def output({:error, :unsupported}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "This feature flag is not supported by node #{node_name}"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "enable_feature_flag <all | feature_flag>" + + def usage_additional() do + [ + ["<feature_flag>", "name of the feature flag to enable, or \"all\" to enable all supported flags"] + ] +end + + def help_section(), do: :feature_flags + + def description(), do: "Enables a feature flag or all supported feature flags on the target node" + + def banner(["all"], _), do: "Enabling all feature flags ..." + + def banner([feature_flag], _), do: "Enabling feature flag \"#{feature_flag}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/encode_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/encode_command.ex new file mode 100644 index 0000000000..c625b4a5f5 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/encode_command.ex @@ -0,0 +1,102 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.EncodeCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def switches() do + [ + cipher: :atom, + hash: :atom, + iterations: :integer + ] + end + + def distribution(_), do: :none + + def merge_defaults(args, opts) do + with_defaults = Map.merge(%{ + cipher: :rabbit_pbe.default_cipher(), + hash: :rabbit_pbe.default_hash(), + iterations: :rabbit_pbe.default_iterations() + }, opts) + {args, with_defaults} + end + + def validate(args, _) when length(args) < 2 do + {:validation_failure, {:not_enough_args, "Please provide a value to decode and a passphrase."}} + end + + def validate(args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + + def validate(args, opts) when length(args) === 2 do + case {supports_cipher(opts.cipher), supports_hash(opts.hash), opts.iterations > 0} do + {false, _, _} -> + {:validation_failure, {:bad_argument, "The requested cipher is not supported."}} + + {_, false, _} -> + {:validation_failure, {:bad_argument, "The requested hash is not supported"}} + + {_, _, false} -> + {:validation_failure, {:bad_argument, "The requested number of iterations is incorrect"}} + + {true, true, true} -> + :ok + end + end + + def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do + try do + term_value = Helpers.evaluate_input_as_term(value) + result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value) + {:ok, result} + catch + _, _ -> + {:error, "Error during cipher operation."} + end + end + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def banner([_, _], _) do + "Encrypting value ..." + end + + def usage, do: "encode value passphrase [--cipher <cipher>] [--hash <hash>] [--iterations <iterations>]" + + def usage_additional() do + [ + ["<value>", "config value to encode"], + ["<passphrase>", "passphrase to use with the config value encryption key"], + ["--cipher <cipher>", "cipher suite to use"], + ["--hash <hash>", "hashing function to use"], + ["--iterations <iterations>", "number of iteration to apply"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.configuration() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Encrypts a sensitive configuration value" + + # + # Implementation + # + + defp supports_cipher(cipher), do: Enum.member?(:rabbit_pbe.supported_ciphers(), cipher) + + defp supports_hash(hash), do: Enum.member?(:rabbit_pbe.supported_hashes(), hash) +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/environment_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/environment_command.ex new file mode 100644 index 0000000000..ac807512a9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/environment_command.ex @@ -0,0 +1,38 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.EnvironmentCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :environment, []) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "environment" + + def usage_doc_guides() do + [ + DocGuide.configuration(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Displays the name and value of each variable in the application environment for each running application" + + def banner(_, %{node: node_name}), do: "Application environment of node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/eval_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/eval_command.ex new file mode 100644 index 0000000000..35fe0a8803 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/eval_command.ex @@ -0,0 +1,107 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.EvalCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ErlEval, ExitCodes, Input} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _) do + # value will be provided via standard input + :ok + end + + def validate(["" | _], _) do + {:validation_failure, "Expression must not be blank"} + end + + def validate([expr | _], _) do + case ErlEval.parse_expr(expr) do + {:ok, _} -> :ok + {:error, err} -> {:validation_failure, err} + end + end + + def run([], %{node: node_name} = opts) do + case Input.consume_multiline_string() do + :eof -> {:error, :not_enough_args} + expr -> + case ErlEval.parse_expr(expr) do + {:ok, parsed} -> + bindings = make_bindings([], opts) + + case :rabbit_misc.rpc_call(node_name, :erl_eval, :exprs, [parsed, bindings]) do + {:value, value, _} -> {:ok, value} + err -> err + end + + {:error, msg} -> {:error, msg} + end + end + end + def run([expr | arguments], %{node: node_name} = opts) do + case ErlEval.parse_expr(expr) do + {:ok, parsed} -> + bindings = make_bindings(arguments, opts) + + case :rabbit_misc.rpc_call(node_name, :erl_eval, :exprs, [parsed, bindings]) do + {:value, value, _} -> {:ok, value} + err -> err + end + + {:error, msg} -> {:error, msg} + end + end + + def output({:error, :not_enough_args}, _) do + {:error, ExitCodes.exit_dataerr(), "Expression to evaluate is not provided via argument or stdin"} + end + def output({:error, msg}, _) do + {:error, ExitCodes.exit_dataerr(), "Evaluation failed: #{msg}"} + end + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "eval <expression>" + + def usage_additional() do + [ + ["<expression>", "Expression to evaluate"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.cli(), + DocGuide.monitoring(), + DocGuide.troubleshooting() + ] + end + + def help_section(), do: :operations + + def description(), do: "Evaluates a snippet of Erlang code on the target node" + + def banner(_, _), do: nil + + # + # Implementation + # + + defp make_bindings(args, opts) do + Enum.with_index(args, 1) + |> Enum.map(fn {val, index} -> {String.to_atom("_#{index}"), val} end) + |> Enum.concat(option_bindings(opts)) + end + + defp option_bindings(opts) do + Enum.to_list(opts) + |> Enum.map(fn {key, val} -> {String.to_atom("_#{key}"), val} end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/eval_file_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/eval_file_command.ex new file mode 100644 index 0000000000..6f46abbf17 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/eval_file_command.ex @@ -0,0 +1,75 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.EvalFileCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ErlEval, ExitCodes} + alias RabbitMQ.CLI.Ctl.Commands.EvalCommand + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate(["" | _], _) do + {:validation_failure, "File path must not be blank"} + end + + def validate([file_path], _) do + case File.read(file_path) do + {:ok, expr} -> + case ErlEval.parse_expr(expr) do + {:ok, _} -> :ok + {:error, err} -> {:validation_failure, err} + end + {:error, :enoent} -> + {:validation_failure, "File #{file_path} does not exist"} + {:error, :eacces} -> + {:validation_failure, "Insufficient permissions to read file #{file_path}"} + {:error, err} -> + {:validation_failure, err} + end + end + + def run([file_path], opts) do + case File.read(file_path) do + {:ok, expr} -> EvalCommand.run([expr], opts) + {:error, err} -> {:error, err} + end + + end + + def output({:error, msg}, _) do + {:error, ExitCodes.exit_dataerr(), "Evaluation failed: #{msg}"} + end + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "eval_file <file path>" + + def usage_additional() do + [ + ["<file path>", "Path to the file to evaluate"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.cli(), + DocGuide.monitoring(), + DocGuide.troubleshooting() + ] + end + + def help_section(), do: :operations + + def description(), do: "Evaluates a file that contains a snippet of Erlang code on the target node" + + def banner(_, _), do: nil +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/exec_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/exec_command.ex new file mode 100644 index 0000000000..469047c1af --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/exec_command.ex @@ -0,0 +1,80 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ExecCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def switches(), do: [offline: :boolean] + + def distribution(%{offline: true}), do: :none + def distribution(%{}), do: :cli + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate([""], _) do + {:validation_failure, "Expression must not be blank"} + end + + def validate([string], _) do + try do + Code.compile_string(string) + :ok + rescue + ex in SyntaxError -> + {:validation_failure, "SyntaxError: " <> Exception.message(ex)} + + _ -> + :ok + end + end + + def run([expr], %{} = opts) do + try do + {val, _} = Code.eval_string(expr, [options: opts], __ENV__) + {:ok, val} + rescue + ex -> + {:error, Exception.message(ex)} + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Inspect + + def banner(_, _), do: nil + + def usage, do: "exec <expression> [--offline]" + + def usage_additional() do + [ + ["<expression>", "Expression to evaluate"], + ["--offline", "disable inter-node communication"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.cli(), + DocGuide.monitoring(), + DocGuide.troubleshooting() + ] + end + + def help_section(), do: :operations + + def description(), do: "Evaluates a snippet of Elixir code on the CLI node" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/export_definitions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/export_definitions_command.ex new file mode 100644 index 0000000000..e4b026f160 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/export_definitions_command.ex @@ -0,0 +1,143 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ExportDefinitionsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(["-"] = args, opts) do + {args, Map.merge(%{format: "json", silent: true}, Helpers.case_insensitive_format(opts))} + end + def merge_defaults(args, opts) do + {args, Map.merge(%{format: "json"}, Helpers.case_insensitive_format(opts))} + end + + def switches(), do: [timeout: :integer, format: :string] + def aliases(), do: [t: :timeout] + + def validate(_, %{format: format}) + when format != "json" and format != "JSON" and format != "erlang" do + {:validation_failure, {:bad_argument, "Format should be either json or erlang"}} + end + def validate([], _) do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + # output to stdout + def validate(["-"], _) do + :ok + end + def validate([path], _) do + dir = Path.dirname(path) + case File.exists?(dir, [raw: true]) do + true -> :ok + false -> {:validation_failure, {:bad_argument, "Directory #{dir} does not exist"}} + end + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run(["-"], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_definitions, :all_definitions, [], timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + result -> {:ok, result} + end + end + def run([path], %{node: node_name, timeout: timeout, format: format}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_definitions, :all_definitions, [], timeout) do + {:badrpc, _} = err -> err + {:error, _} = err -> err + {:error, _, _} = err -> err + result -> + # write to the file in run/2 because output/2 is not meant to + # produce side effects + body = serialise(result, format) + abs_path = Path.absname(path) + + File.rm(abs_path) + case File.write(abs_path, body) do + # no output + :ok -> {:ok, nil} + {:error, :enoent} -> + {:error, ExitCodes.exit_dataerr(), "Parent directory or file #{path} does not exist"} + {:error, :enotdir} -> + {:error, ExitCodes.exit_dataerr(), "Parent directory of file #{path} is not a directory"} + {:error, :enospc} -> + {:error, ExitCodes.exit_dataerr(), "No space left on device hosting #{path}"} + {:error, :eacces} -> + {:error, ExitCodes.exit_dataerr(), "No permissions to write to file #{path} or its parent directory"} + {:error, :eisdir} -> + {:error, ExitCodes.exit_dataerr(), "Path #{path} is a directory"} + {:error, err} -> + {:error, ExitCodes.exit_dataerr(), "Could not write to file #{path}: #{err}"} + end + end + end + + def output({:ok, nil}, _) do + {:ok, nil} + end + def output({:ok, result}, %{format: "json"}) when is_map(result) do + {:ok, serialise(result, "json")} + end + def output({:ok, result}, %{format: "erlang"}) when is_map(result) do + {:ok, serialise(result, "erlang")} + end + use RabbitMQ.CLI.DefaultOutput + + def printer(), do: RabbitMQ.CLI.Printers.StdIORaw + + def usage, do: "export_definitions <file_path | \"-\"> [--format <json | erlang>]" + + def usage_additional() do + [ + ["<file>", "Local file path to export to. Pass a dash (-) for stdout."], + ["--format", "output format to use: json or erlang"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.definitions() + ] + end + + def help_section(), do: :definitions + + def description(), do: "Exports definitions in JSON or compressed Erlang Term Format." + + def banner([path], %{format: fmt}), do: "Exporting definitions in #{human_friendly_format(fmt)} to a file at \"#{path}\" ..." + + # + # Implementation + # + + defp serialise(raw_map, "json") do + # make sure all runtime parameter values are maps, otherwise + # they will end up being a list of pairs (a keyword list/proplist) + # in the resulting JSON document + map = Map.update!(raw_map, :parameters, fn(params) -> + Enum.map(params, fn(param) -> + Map.update!(param, "value", &:rabbit_data_coercion.to_map/1) + end) + end) + {:ok, json} = JSON.encode(map) + json + end + + defp serialise(map, "erlang") do + :erlang.term_to_binary(map, [{:compressed, 9}]) + end + + defp human_friendly_format("JSON"), do: "JSON" + defp human_friendly_format("json"), do: "JSON" + defp human_friendly_format("erlang"), do: "Erlang term format" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_boot_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_boot_command.ex new file mode 100644 index 0000000000..261f86c6c1 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_boot_command.ex @@ -0,0 +1,57 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ForceBootCommand do + alias RabbitMQ.CLI.Core.{Config, DocGuide} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + ## + def validate_execution_environment(args, opts) do + ## We don't use RequiresRabbitAppStopped helper because we don't want to fail + ## the validation if the node is not running. + case RabbitMQ.CLI.Core.Validators.rabbit_is_not_running(args, opts) do + :ok -> :ok + {:validation_failure, _} = failure -> failure + _other -> RabbitMQ.CLI.Core.Validators.node_is_not_running(args, opts) + end + end + + def run([], %{node: node_name} = opts) do + case :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :force_load_next_boot, []) do + {:badrpc, :nodedown} -> + case Config.get_option(:mnesia_dir, opts) do + nil -> + {:error, :mnesia_dir_not_found} + + dir -> + File.write(Path.join(dir, "force_load"), "") + end + + _ -> + :ok + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "force_boot" + + def usage_doc_guides() do + [ + DocGuide.clustering() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Forces node to start even if it cannot contact or rejoin any of its previously known peers" + + def banner(_, _), do: nil +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_gc_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_gc_command.ex new file mode 100644 index 0000000000..975154be50 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_gc_command.ex @@ -0,0 +1,35 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ForceGcCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_runtime, :gc_all_processes, [], timeout) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "force_gc" + + def usage_doc_guides() do + [ + DocGuide.memory_use() + ] + end + + def help_section(), do: :operations + + def description, do: "Makes all Erlang processes on the target node perform/schedule a full sweep garbage collection" + + def banner([], %{node: node_name}), do: "Will ask all processes on node #{node_name} to schedule a full sweep GC" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_reset_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_reset_command.ex new file mode 100644 index 0000000000..5f202f9d08 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/force_reset_command.ex @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ForceResetCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppStopped + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :force_reset, []) + end + + def output({:error, :mnesia_unexpectedly_running}, %{node: node_name}) do + {:error, ExitCodes.exit_software(), + RabbitMQ.CLI.DefaultOutput.mnesia_running_error(node_name)} + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "force_reset" + + def usage_doc_guides() do + [ + DocGuide.clustering() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Forcefully returns a RabbitMQ node to its virgin state" + + def banner(_, %{node: node_name}), do: "Forcefully resetting node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/forget_cluster_node_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/forget_cluster_node_command.ex new file mode 100644 index 0000000000..cdf5ae7fbe --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/forget_cluster_node_command.ex @@ -0,0 +1,122 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ForgetClusterNodeCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Distribution, Validators} + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [offline: :boolean] + + def merge_defaults(args, opts) do + {args, Map.merge(%{offline: false}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + + def validate_execution_environment([_node_to_remove] = args, %{offline: true} = opts) do + Validators.chain( + [ + &Validators.node_is_not_running/2, + &Validators.mnesia_dir_is_set/2, + &Validators.feature_flags_file_is_set/2, + &Validators.rabbit_is_loaded/2 + ], + [args, opts] + ) + end + + def validate_execution_environment([_], %{offline: false}) do + :ok + end + + def run([node_to_remove], %{node: node_name, offline: true} = opts) do + Stream.concat([ + become(node_name, opts), + RabbitMQ.CLI.Core.Helpers.defer(fn -> + :rabbit_event.start_link() + :rabbit_mnesia.forget_cluster_node(to_atom(node_to_remove), true) + end) + ]) + end + + def run([node_to_remove], %{node: node_name, offline: false}) do + atom_name = to_atom(node_to_remove) + args = [atom_name, false] + case :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :forget_cluster_node, args) do + {:error, {:failed_to_remove_node, ^atom_name, {:active, _, _}}} -> + {:error, "RabbitMQ on node #{node_to_remove} must be stopped with 'rabbitmqctl -n #{node_to_remove} stop_app' before it can be removed"}; + {:error, _} = error -> error; + {:badrpc, _} = error -> error; + :ok -> + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :shrink_all, [atom_name]) do + {:error, _} -> + {:error, "RabbitMQ failed to shrink some of the quorum queues on node #{node_to_remove}"}; + _ -> :ok + end + other -> other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "forget_cluster_node [--offline] <existing_cluster_member_node>" + end + + def usage_additional() do + [ + ["--offline", "try to update cluster membership state directly. Use when target node is stopped. Only works for local nodes."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.clustering(), + DocGuide.cluster_formation() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Removes a node from the cluster" + + def banner([node_to_remove], %{offline: true}) do + "Removing node #{node_to_remove} from the cluster. Warning: quorum queues cannot be shrunk in offline mode" + end + def banner([node_to_remove], _) do + "Removing node #{node_to_remove} from the cluster" + end + + # + # Implementation + # + + defp become(node_name, opts) do + :error_logger.tty(false) + + case :net_adm.ping(node_name) do + :pong -> + exit({:node_running, node_name}) + + :pang -> + :ok = :net_kernel.stop() + + Stream.concat([ + [" * Impersonating node: #{node_name}..."], + RabbitMQ.CLI.Core.Helpers.defer(fn -> + {:ok, _} = Distribution.start_as(node_name, opts) + " done" + end), + RabbitMQ.CLI.Core.Helpers.defer(fn -> + dir = :mnesia.system_info(:directory) + " * Mnesia directory: #{dir}..." + end) + ]) + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/help_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/help_command.ex new file mode 100644 index 0000000000..8f459cc83f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/help_command.ex @@ -0,0 +1,343 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +alias RabbitMQ.CLI.CommandBehaviour + +defmodule RabbitMQ.CLI.Ctl.Commands.HelpCommand do + alias RabbitMQ.CLI.Core.{CommandModules, Config, ExitCodes} + alias RabbitMQ.CLI.Core.CommandModules + + import RabbitMQ.CLI.Core.ANSI + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics, :plugins, :queues, :upgrade] + def switches(), do: [list_commands: :boolean] + + def distribution(_), do: :none + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _), do: :ok + def validate([_command], _), do: :ok + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def run([command_name | _], opts) do + CommandModules.load(opts) + + module_map = CommandModules.module_map() + case module_map[command_name] do + nil -> + # command not found + # {:error, all_usage(opts)} + case RabbitMQ.CLI.AutoComplete.suggest_command(command_name, module_map) do + {:suggest, suggested} -> + suggest_message = "\nCommand '#{command_name}' not found. \n" <> + "Did you mean '#{suggested}'? \n" + {:error, ExitCodes.exit_usage(), suggest_message} + nil -> + {:error, ExitCodes.exit_usage(), "\nCommand '#{command_name}' not found."} + end + + command -> + {:ok, command_usage(command, opts)} + end + end + + def run([], opts) do + CommandModules.load(opts) + + case opts[:list_commands] do + true -> + {:ok, commands_description()} + _ -> + {:ok, all_usage(opts)} + end + end + + def output({:ok, result}, _) do + {:ok, result} + end + def output({:error, result}, _) do + {:error, ExitCodes.exit_usage(), result} + end + use RabbitMQ.CLI.DefaultOutput + + def banner(_, _), do: nil + + def help_section(), do: :help + + def description(), do: "Displays usage information for a command" + + def usage(), do: "help (<command> | [--list-commands])" + + def usage_additional() do + [ + ["--list-commands", "only output a list of discovered commands"] + ] + end + + + # + # Implementation + # + + def all_usage(opts) do + tool_name = program_name(opts) + tool_usage(tool_name) ++ + ["\n\nAvailable commands:\n"] ++ commands_description() ++ + help_footer(tool_name) + end + + def command_usage(command, opts) do + Enum.join([base_usage(command, opts)] ++ + command_description(command) ++ + additional_usage(command) ++ + relevant_doc_guides(command) ++ + general_options_usage(), + "\n\n") <> "\n" + end + + defp tool_usage(tool_name) do + [ + "\n#{bright("Usage")}\n\n" <> + "#{tool_name} [--node <node>] [--timeout <timeout>] [--longnames] [--quiet] <command> [<command options>]" + ] + end + + def base_usage(command, opts) do + tool_name = program_name(opts) + + maybe_timeout = + case command_supports_timeout(command) do + true -> " [--timeout <timeout>]" + false -> "" + end + + Enum.join([ + "\n#{bright("Usage")}\n\n", + "#{tool_name} [--node <node>] [--longnames] [--quiet] " <> + flatten_string(command.usage(), maybe_timeout) + ]) + end + + defp flatten_string(list, additional) when is_list(list) do + list + |> Enum.map(fn line -> line <> additional end) + |> Enum.join("\n") + end + + defp flatten_string(str, additional) when is_binary(str) do + str <> additional + end + + defp general_options_usage() do + [ + "#{bright("General Options")} + +The following options are accepted by most or all commands. + +short | long | description +-----------------|---------------|-------------------------------- +-? | --help | displays command help +-n <node> | --node <node> | connect to node <node> +-l | --longnames | use long host names +-t | --timeout <n> | for commands that support it, operation timeout in seconds +-q | --quiet | suppress informational messages +-s | --silent | suppress informational messages + | and table header row +-p | --vhost | for commands that are scoped to a virtual host, + | | virtual host to use + | --formatter | alternative result formatter to use + | if supported: json, pretty_table, table, csv, erlang + not all commands support all (or any) alternative formatters."] + end + + defp command_description(command) do + case CommandBehaviour.description(command) do + "" -> [] + other -> [other <> ".\n"] + end + end + + defp list_item_formatter([option, description]) do + "#{option}\n\t#{description}\n" + end + defp list_item_formatter({option, description}) do + "#{option}\n\t#{description}\n" + end + defp list_item_formatter(line) do + "#{line}\n" + end + + defp additional_usage(command) do + command_usage = + case CommandBehaviour.usage_additional(command) do + list when is_list(list) -> list |> Enum.map(&list_item_formatter/1) + bin when is_binary(bin) -> ["#{bin}\n"] + end + case command_usage do + [] -> [] + usage -> + [flatten_string(["#{bright("Arguments and Options")}\n" | usage], "")] + end + end + + defp relevant_doc_guides(command) do + guide_list = + case CommandBehaviour.usage_doc_guides(command) do + list when is_list(list) -> list |> Enum.map(fn ln -> " * #{ln}\n" end) + bin when is_binary(bin) -> [" * #{bin}\n"] + end + case guide_list do + [] -> [] + usage -> + [flatten_string(["#{bright("Relevant Doc Guides")}\n" | usage], "")] + end + end + + defp help_footer(tool_name) do + ["Use '#{tool_name} help <command>' to learn more about a specific command"] + end + + defp command_supports_timeout(command) do + nil != CommandBehaviour.switches(command)[:timeout] + end + + defp commands_description() do + module_map = CommandModules.module_map() + + pad_commands_to = Enum.reduce(module_map, 0, + fn({name, _}, longest) -> + name_length = String.length(name) + case name_length > longest do + true -> name_length + false -> longest + end + end) + + lines = module_map + |> Enum.map( + fn({name, cmd}) -> + description = CommandBehaviour.description(cmd) + help_section = CommandBehaviour.help_section(cmd) + {name, {description, help_section}} + end) + |> Enum.group_by(fn({_, {_, help_section}}) -> help_section end) + |> Enum.sort_by( + fn({help_section, _}) -> + case help_section do + :deprecated -> 999 + :other -> 100 + {:plugin, _} -> 99 + :help -> 1 + :node_management -> 2 + :cluster_management -> 3 + :replication -> 3 + :user_management -> 4 + :access_control -> 5 + :observability_and_health_checks -> 6 + :parameters -> 7 + :policies -> 8 + :virtual_hosts -> 9 + _ -> 98 + end + end) + |> Enum.map( + fn({help_section, section_helps}) -> + [ + "\n" <> bright(section_head(help_section)) <> ":\n\n" | + Enum.sort(section_helps) + |> Enum.map( + fn({name, {description, _}}) -> + " #{String.pad_trailing(name, pad_commands_to)} #{description}\n" + end) + ] + + end) + |> Enum.concat() + + lines ++ ["\n"] + end + + defp section_head(help_section) do + case help_section do + :help -> + "Help" + :user_management -> + "Users" + :cluster_management -> + "Cluster" + :replication -> + "Replication" + :node_management -> + "Nodes" + :queues -> + "Queues" + :observability_and_health_checks -> + "Monitoring, observability and health checks" + :virtual_hosts -> + "Virtual hosts" + :access_control -> + "Access Control" + :parameters -> + "Parameters" + :policies -> + "Policies" + :configuration -> + "Configuration and Environment" + :feature_flags -> + "Feature flags" + :other -> + "Other" + {:plugin, plugin} -> + plugin_section(plugin) <> " plugin" + custom -> + snake_case_to_capitalized_string(custom) + end + end + + defp strip_rabbitmq_prefix(value, regex) do + Regex.replace(regex, value, "") + end + + defp format_known_plugin_name_fragments(value) do + case value do + ["amqp1.0"] -> "AMQP 1.0" + ["amqp1", "0"] -> "AMQP 1.0" + ["management"] -> "Management" + ["management", "agent"] -> "Management" + ["mqtt"] -> "MQTT" + ["stomp"] -> "STOMP" + ["web", "mqtt"] -> "Web MQTT" + ["web", "stomp"] -> "Web STOMP" + [other] -> snake_case_to_capitalized_string(other) + fragments -> snake_case_to_capitalized_string(Enum.join(fragments, "_")) + end + end + + defp plugin_section(plugin) do + regex = Regex.recompile!(~r/^rabbitmq_/) + + to_string(plugin) + # drop rabbitmq_ + |> strip_rabbitmq_prefix(regex) + |> String.split("_") + |> format_known_plugin_name_fragments() + end + + defp snake_case_to_capitalized_string(value) do + to_string(value) + |> String.split("_") + |> Enum.map(&String.capitalize/1) + |> Enum.join(" ") + end + + defp program_name(opts) do + Config.get_option(:script_name, opts) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hipe_compile_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hipe_compile_command.ex new file mode 100644 index 0000000000..13f3468cb6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hipe_compile_command.ex @@ -0,0 +1,98 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.HipeCompileCommand do + @moduledoc """ + HiPE support has been deprecated since Erlang/OTP 22 (mid-2019) and + won't be a part of Erlang/OTP 24. + + Therefore this command is DEPRECATED and is no-op. + """ + + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + import RabbitMQ.CLI.Core.CodePath + + @behaviour RabbitMQ.CLI.CommandBehaviour + + # + # API + # + + def distribution(_), do: :none + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _), do: {:validation_failure, :not_enough_args} + + def validate([target_dir], opts) do + :ok + |> Validators.validate_step(fn -> + case acceptable_path?(target_dir) do + true -> :ok + false -> {:error, {:bad_argument, "Target directory path cannot be blank"}} + end + end) + |> Validators.validate_step(fn -> + case File.dir?(target_dir) do + true -> + :ok + + false -> + case File.mkdir_p(target_dir) do + :ok -> + :ok + + {:error, perm} when perm == :eperm or perm == :eacces -> + {:error, + {:bad_argument, + "Cannot create target directory #{target_dir}: insufficient permissions"}} + end + end + end) + |> Validators.validate_step(fn -> require_rabbit(opts) end) + end + + def validate(_, _), do: {:validation_failure, :too_many_args} + + def run([_target_dir], _opts) do + :ok + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "hipe_compile <directory>" + + def usage_additional do + [ + ["<directory>", "Target directory for HiPE-compiled modules"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.configuration(), + DocGuide.erlang_versions() + ] + end + + def help_section(), do: :deprecated + + def description() do + "DEPRECATED. This command is a no-op. HiPE is no longer supported by modern Erlang versions" + end + + def banner([_target_dir], _) do + "This command is a no-op. HiPE is no longer supported by modern Erlang versions" + end + + # + # Implementation + # + + # Accepts any non-blank path + defp acceptable_path?(value) do + String.length(String.trim(value)) != 0 + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/import_definitions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/import_definitions_command.ex new file mode 100644 index 0000000000..45ca0074f3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/import_definitions_command.ex @@ -0,0 +1,136 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ImportDefinitionsCommand do + alias RabbitMQ.CLI.Core.{Config, DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(["-"] = args, opts) do + {args, Map.merge(%{format: "json", silent: true}, Helpers.case_insensitive_format(opts))} + end + def merge_defaults(args, opts) do + {args, Map.merge(%{format: "json"}, Helpers.case_insensitive_format(opts))} + end + + def switches(), do: [timeout: :integer, format: :string] + def aliases(), do: [t: :timeout] + + def validate(_, %{format: format}) + when format != "json" and format != "JSON" and format != "erlang" do + {:validation_failure, {:bad_argument, "Format should be either json or erlang"}} + end + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + def validate([path], _) do + case File.exists?(path, [raw: true]) do + true -> :ok + false -> {:validation_failure, {:bad_argument, "File #{path} does not exist"}} + end + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, format: format, timeout: timeout}) do + case IO.read(:stdio, :all) do + :eof -> {:error, :not_enough_args} + bin -> + case deserialise(bin, format) do + {:error, error} -> + {:error, ExitCodes.exit_dataerr(), "Failed to deserialise input (format: #{human_friendly_format(format)}) (error: #{inspect(error)})"} + {:ok, map} -> + :rabbit_misc.rpc_call(node_name, :rabbit_definitions, :import_parsed, [map], timeout) + end + end + end + def run([path], %{node: node_name, timeout: timeout, format: format}) do + abs_path = Path.absname(path) + + case File.read(abs_path) do + {:ok, ""} -> + {:error, ExitCodes.exit_dataerr(), "File #{path} is zero-sized"} + {:ok, bin} -> + case deserialise(bin, format) do + {:error, error} -> + {:error, ExitCodes.exit_dataerr(), "Failed to deserialise input (format: #{human_friendly_format(format)}) (error: #{inspect(error)})"} + {:ok, map} -> + :rabbit_misc.rpc_call(node_name, :rabbit_definitions, :import_parsed, [map], timeout) + end + {:error, :enoent} -> + {:error, ExitCodes.exit_dataerr(), "Parent directory or file #{path} does not exist"} + {:error, :enotdir} -> + {:error, ExitCodes.exit_dataerr(), "Parent directory of file #{path} is not a directory"} + {:error, :eacces} -> + {:error, ExitCodes.exit_dataerr(), "No permissions to read from file #{path} or its parent directory"} + {:error, :eisdir} -> + {:error, ExitCodes.exit_dataerr(), "Path #{path} is a directory"} + {:error, err} -> + {:error, ExitCodes.exit_dataerr(), "Could not read from file #{path}: #{err}"} + end + end + + def output(:ok, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name}} + end + def output(:ok, opts) do + case Config.output_less?(opts) do + true -> :ok + false -> {:ok, "Successfully started definition import. " <> + "This process is asynchronous and can take some time.\n"} + end + end + use RabbitMQ.CLI.DefaultOutput + + def printer(), do: RabbitMQ.CLI.Printers.StdIORaw + + def usage, do: "import_definitions <file_path | \"-\"> [--format <json | erlang>]" + + def usage_additional() do + [ + ["[file]", "Local file path to import from. If omitted will be read from standard input."], + ["--format", "input format to use: json or erlang"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.definitions() + ] + end + + def help_section(), do: :definitions + + def description(), do: "Imports definitions in JSON or compressed Erlang Term Format." + + def banner([], %{format: fmt}) do + "Importing definitions in #{human_friendly_format(fmt)} from standard input ..." + end + def banner([path], %{format: fmt}) do + "Importing definitions in #{human_friendly_format(fmt)} from a file at \"#{path}\" ..." + end + + # + # Implementation + # + + defp deserialise(bin, "json") do + JSON.decode(bin) + end + + defp deserialise(bin, "erlang") do + try do + {:ok, :erlang.binary_to_term(bin)} + rescue e in ArgumentError -> + {:error, e.message} + end + end + + defp human_friendly_format("JSON"), do: "JSON" + defp human_friendly_format("json"), do: "JSON" + defp human_friendly_format("erlang"), do: "Erlang term format" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/join_cluster_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/join_cluster_command.ex new file mode 100644 index 0000000000..765fbd43f1 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/join_cluster_command.ex @@ -0,0 +1,95 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.JoinClusterCommand do + alias RabbitMQ.CLI.Core.{Config, DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches() do + [ + disc: :boolean, + ram: :boolean + ] + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{disc: false, ram: false}, opts)} + end + + def validate(_, %{disc: true, ram: true}) do + {:validation_failure, {:bad_argument, "The node type must be either disc or ram."}} + end + + def validate([], _), do: {:validation_failure, :not_enough_args} + def validate([_], _), do: :ok + def validate(_, _), do: {:validation_failure, :too_many_args} + + use RabbitMQ.CLI.Core.RequiresRabbitAppStopped + + def run([target_node], %{node: node_name, ram: ram, disc: disc} = opts) do + node_type = + case {ram, disc} do + {true, false} -> :ram + {false, true} -> :disc + ## disc is default + {false, false} -> :disc + end + + long_or_short_names = Config.get_option(:longnames, opts) + target_node_normalised = Helpers.normalise_node(target_node, long_or_short_names) + + :rabbit_misc.rpc_call( + node_name, + :rabbit_mnesia, + :join_cluster, + [target_node_normalised, node_type] + ) + end + + def output({:ok, :already_member}, _) do + {:ok, "The node is already a member of this cluster"} + end + + def output({:error, :mnesia_unexpectedly_running}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + RabbitMQ.CLI.DefaultOutput.mnesia_running_error(node_name)} + end + + def output({:error, :cannot_cluster_node_with_itself}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: cannot cluster node with itself: #{node_name}"} + end + + use RabbitMQ.CLI.DefaultOutput + + def banner([target_node], %{node: node_name}) do + "Clustering node #{node_name} with #{target_node}" + end + + def usage() do + "join_cluster [--disc|--ram] <existing_cluster_member>" + end + + def usage_additional() do + [ + ["<existing_cluster_member>", "Existing cluster member (node) to join"], + ["--disc", "new node should be a disk one (stores its schema on disk). Highly recommended, used by default."], + ["--ram", "new node should be a RAM one (stores schema in RAM). Not recommended. Consult clustering doc guides first."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.clustering(), + DocGuide.cluster_formation() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Instructs the node to become a member of the cluster that the specified node is in" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_bindings_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_bindings_command.ex new file mode 100644 index 0000000000..19e8844089 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_bindings_command.ex @@ -0,0 +1,74 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListBindingsCommand do + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @info_keys ~w(source_name source_kind destination_name destination_kind routing_key arguments)a + + def info_keys(), do: @info_keys + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults([], opts) do + merge_defaults( + ~w(source_name source_kind + destination_name destination_kind + routing_key arguments), + opts + ) + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_ | _] = args, %{node: node_name, timeout: timeout, vhost: vhost}) do + info_keys = InfoKeys.prepare_info_keys(args) + + RpcStream.receive_list_items( + node_name, + :rabbit_binding, + :info_all, + [vhost, info_keys], + timeout, + info_keys + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage() do + "list_bindings [--vhost <vhost>] [--no-table-headers] [<column> ...]" + end + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists all bindings on a vhost" + + def banner(_, %{vhost: vhost}), do: "Listing bindings for vhost #{vhost}..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_channels_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_channels_command.ex new file mode 100644 index 0000000000..5ae7450da1 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_channels_command.ex @@ -0,0 +1,85 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. +## + +defmodule RabbitMQ.CLI.Ctl.Commands.ListChannelsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + + @info_keys ~w(pid connection name number user vhost transactional + confirm consumer_count messages_unacknowledged + messages_uncommitted acks_uncommitted messages_unconfirmed + prefetch_count global_prefetch_count)a + + def info_keys(), do: @info_keys + + def merge_defaults([], opts) do + merge_defaults(~w(pid user consumer_count messages_unacknowledged), opts) + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], opts) do + run(~w(pid user consumer_count messages_unacknowledged), opts) + end + + def run([_ | _] = args, %{node: node_name, timeout: timeout}) do + info_keys = InfoKeys.prepare_info_keys(args) + + Helpers.with_nodes_in_cluster(node_name, fn nodes -> + RpcStream.receive_list_items( + node_name, + :rabbit_channel, + :emit_info_all, + [nodes, info_keys], + timeout, + info_keys, + Kernel.length(nodes) + ) + end) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def banner(_, _), do: "Listing channels ..." + + def usage() do + "list_channels [--no-table-headers] [<column> ...]" + end + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def usage_doc_guides() do + [ + DocGuide.channels() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists all channels in the node" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_ciphers_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_ciphers_command.ex new file mode 100644 index 0000000000..eb7075d261 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_ciphers_command.ex @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListCiphersCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def distribution(_), do: :none + + def run(_, _) do + {:ok, :rabbit_pbe.supported_ciphers()} + end + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "list_ciphers" + + def usage_doc_guides() do + [ + DocGuide.configuration(), + DocGuide.tls() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists cipher suites supported by encoding commands" + + def banner(_, _), do: "Listing supported ciphers ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_connections_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_connections_command.ex new file mode 100644 index 0000000000..0e28272ea8 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_connections_command.ex @@ -0,0 +1,84 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListConnectionsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + @info_keys ~w(pid name port host peer_port peer_host ssl ssl_protocol + ssl_key_exchange ssl_cipher ssl_hash peer_cert_subject + peer_cert_issuer peer_cert_validity state + channels protocol auth_mechanism user vhost timeout frame_max + channel_max client_properties recv_oct recv_cnt send_oct + send_cnt send_pend connected_at)a + + def info_keys(), do: @info_keys + + def merge_defaults([], opts) do + merge_defaults(~w(user peer_host peer_port state), opts) + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_ | _] = args, %{node: node_name, timeout: timeout}) do + info_keys = InfoKeys.prepare_info_keys(args) + + Helpers.with_nodes_in_cluster(node_name, fn nodes -> + RpcStream.receive_list_items( + node_name, + :rabbit_networking, + :emit_connection_info_all, + [nodes, info_keys], + timeout, + info_keys, + Kernel.length(nodes) + ) + end) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage() do + "list_connections [--no-table-headers] [<column> ...]" + end + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def usage_doc_guides() do + [ + DocGuide.connections() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists AMQP 0.9.1 connections for the node" + + def banner(_, _), do: "Listing connections ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_consumers_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_consumers_command.ex new file mode 100644 index 0000000000..90c587cbe8 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_consumers_command.ex @@ -0,0 +1,108 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListConsumersCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + @info_keys ~w(queue_name channel_pid consumer_tag + ack_required prefetch_count active activity_status arguments)a + + def info_keys(), do: @info_keys + + def merge_defaults([], opts) do + {Enum.map(@info_keys -- [:activity_status], &Atom.to_string/1), + Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_ | _] = args, %{node: node_name, timeout: timeout, vhost: vhost}) do + info_keys = InfoKeys.prepare_info_keys(args) + + Helpers.with_nodes_in_cluster(node_name, fn nodes -> + RpcStream.receive_list_items_with_fun( + node_name, + [{:rabbit_amqqueue, + :emit_consumers_all, + [nodes, vhost]}], + timeout, + info_keys, + Kernel.length(nodes), + fn item -> fill_consumer_active_fields(item) end + ) + end) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage() do + "list_consumers [--vhost <vhost>] [--no-table-headers] [<column> ...]" + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists all consumers for a vhost" + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def usage_doc_guides() do + [ + DocGuide.consumers() + ] + end + + def banner(_, %{vhost: vhost}), do: "Listing consumers in vhost #{vhost} ..." + + # + # Implementation + # + + # add missing fields if response comes from node < 3.8 + def fill_consumer_active_fields({[], {chunk, :continue}}) do + {[], {chunk, :continue}} + end + + def fill_consumer_active_fields({items, {chunk, :continue}}) do + {Enum.map(items, fn item -> + case Keyword.has_key?(item, :active) do + true -> + item + false -> + Keyword.drop(item, [:arguments]) + ++ [active: true, activity_status: :up] + ++ [arguments: Keyword.get(item, :arguments, [])] + end + end), {chunk, :continue}} + end + + def fill_consumer_active_fields(v) do + v + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_exchanges_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_exchanges_command.ex new file mode 100644 index 0000000000..a3b8b3521b --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_exchanges_command.ex @@ -0,0 +1,67 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListExchangesCommand do + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @info_keys ~w(name type durable auto_delete internal arguments policy)a + + def info_keys(), do: @info_keys + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults([], opts) do + merge_defaults(~w(name type), opts) + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_ | _] = args, %{node: node_name, timeout: timeout, vhost: vhost}) do + info_keys = InfoKeys.prepare_info_keys(args) + + RpcStream.receive_list_items( + node_name, + :rabbit_exchange, + :info_all, + [vhost, info_keys], + timeout, + info_keys + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage(), do: "list_exchanges [--vhost <vhost>] [--no-table-headers] [<column> ...]" + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists exchanges" + + def banner(_, %{vhost: vhost}), do: "Listing exchanges for vhost #{vhost} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_feature_flags_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_feature_flags_command.ex new file mode 100644 index 0000000000..46b4bc82c2 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_feature_flags_command.ex @@ -0,0 +1,94 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule RabbitMQ.CLI.Ctl.Commands.ListFeatureFlagsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + alias RabbitMQ.CLI.Ctl.InfoKeys + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + @info_keys ~w(name state stability provided_by desc doc_url)a + + def info_keys(), do: @info_keys + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults([], opts), do: {["name", "state"], opts} + def merge_defaults(args, opts), do: {args, opts} + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &Validators.rabbit_is_loaded/2, + &Validators.rabbit_is_running/2 + ], + [args, opts] + ) + end + + def run([_|_] = args, %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_ff_extra, :cli_info, [], timeout) do + # Server does not support feature flags, consider none are available. + # See rabbitmq/rabbitmq-cli#344 for context. MK. + {:badrpc, {:EXIT, {:undef, _}}} -> [] + {:badrpc, _} = err -> err + val -> filter_by_arg(val, args) + end + + end + + def banner(_, _), do: "Listing feature flags ..." + + def usage, do: "list_feature_flags [<column> ...]" + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def usage_doc_guides() do + [ + DocGuide.feature_flags() + ] + end + + def help_section(), do: :feature_flags + + def description(), do: "Lists feature flags" + + # + # Implementation + # + + defp filter_by_arg(ff_info, _) when is_tuple(ff_info) do + # tuple means unexpected data + ff_info + end + + defp filter_by_arg(ff_info, [_|_] = args) when is_list(ff_info) do + symbol_args = InfoKeys.prepare_info_keys(args) + Enum.map(ff_info, + fn(ff) -> + symbol_args + |> Enum.filter(fn(arg) -> ff[arg] != nil end) + |> Enum.map(fn(arg) -> {arg, ff[arg]} end) + end + ) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_global_parameters_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_global_parameters_command.ex new file mode 100644 index 0000000000..8d3f2d795a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_global_parameters_command.ex @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListGlobalParametersCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime_parameters, + :list_global_formatted, + [], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_global_parameters [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :parameters + + def description(), do: "Lists global runtime parameters" + + def banner(_, _), do: "Listing global runtime parameters ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_hashes_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_hashes_command.ex new file mode 100644 index 0000000000..9e0f25e6dd --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_hashes_command.ex @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListHashesCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def distribution(_), do: :none + + def run(_, _) do + {:ok, :rabbit_pbe.supported_hashes()} + end + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "list_hashes" + + def usage_doc_guides() do + [ + DocGuide.configuration(), + DocGuide.tls() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists hash functions supported by encoding commands" + + def banner(_, _), do: "Listing supported hash algorithms ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_operator_policies_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_operator_policies_command.ex new file mode 100644 index 0000000000..dd2c54dfc0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_operator_policies_command.ex @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListOperatorPoliciesCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_policy, + :list_formatted_op, + [vhost], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_operator_policies [--vhost <vhost>] [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :policies + + def description(), do: "Lists operator policy overrides for a virtual host" + + def banner(_, %{vhost: vhost}), + do: "Listing operator policy overrides for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_parameters_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_parameters_command.ex new file mode 100644 index 0000000000..2d51f08527 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_parameters_command.ex @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListParametersCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime_parameters, + :list_formatted, + [vhost], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_parameters [--vhost <vhost>] [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :parameters + + def description(), do: "Lists runtime parameters for a virtual host" + + def banner(_, %{vhost: vhost}), do: "Listing runtime parameters for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_permissions_command.ex new file mode 100644 index 0000000000..feaf917cfa --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_permissions_command.ex @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListPermissionsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :list_vhost_permissions, + [vhost], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_permissions [--vhost <vhost>] [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :access_control + def description(), do: "Lists user permissions in a virtual host" + + def banner(_, %{vhost: vhost}), do: "Listing permissions for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_policies_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_policies_command.ex new file mode 100644 index 0000000000..9fe8e37dc1 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_policies_command.ex @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListPoliciesCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_policy, + :list_formatted, + [vhost], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_policies [--vhost <vhost>] [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :policies + + def description(), do: "Lists all policies in a virtual host" + + def banner(_, %{vhost: vhost}), do: "Listing policies for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_queues_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_queues_command.ex new file mode 100644 index 0000000000..21f9fa78ec --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_queues_command.ex @@ -0,0 +1,143 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListQueuesCommand do + require RabbitMQ.CLI.Ctl.InfoKeys + require RabbitMQ.CLI.Ctl.RpcStream + + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 60_000 + @info_keys ~w(name durable auto_delete + arguments policy pid owner_pid exclusive exclusive_consumer_pid + exclusive_consumer_tag messages_ready messages_unacknowledged messages + messages_ready_ram messages_unacknowledged_ram messages_ram + messages_persistent message_bytes message_bytes_ready + message_bytes_unacknowledged message_bytes_ram message_bytes_persistent + head_message_timestamp disk_reads disk_writes consumers + consumer_utilisation memory slave_pids synchronised_slave_pids state type + leader members online)a + + def description(), do: "Lists queues and their properties" + def usage(), do: "list_queues [--vhost <vhost>] [--online] [--offline] [--local] [--no-table-headers] [<column>, ...]" + def scopes(), do: [:ctl, :diagnostics] + def switches(), do: [offline: :boolean, + online: :boolean, + local: :boolean, + timeout: :integer] + def aliases(), do: [t: :timeout] + + def info_keys(), do: @info_keys + + defp default_opts() do + %{vhost: "/", offline: false, online: false, local: false, table_headers: true} + end + + def merge_defaults([_ | _] = args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, + Map.merge( + default_opts(), + Map.merge(opts, %{timeout: timeout}) + )} + end + + def merge_defaults([], opts) do + merge_defaults(~w(name messages), opts) + end + + def validate(args, _opts) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + # note that --offline for this command has a different meaning: + # it lists queues with unavailable masters + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_ | _] = args, %{ + node: node_name, + timeout: timeout, + vhost: vhost, + online: online_opt, + offline: offline_opt, + local: local_opt + }) do + {online, offline} = + case {online_opt, offline_opt} do + {false, false} -> {true, true} + other -> other + end + + info_keys = InfoKeys.prepare_info_keys(args) + + Helpers.with_nodes_in_cluster(node_name, fn nodes -> + offline_mfa = {:rabbit_amqqueue, :emit_info_down, [vhost, info_keys]} + local_mfa = {:rabbit_amqqueue, :emit_info_local, [vhost, info_keys]} + online_mfa = {:rabbit_amqqueue, :emit_info_all, [nodes, vhost, info_keys]} + + {chunks, mfas} = + case {local_opt, offline, online} do + # Local takes precedence + {true, _, _} -> {1, [local_mfa]} + {_, true, true} -> {Kernel.length(nodes) + 1, [offline_mfa, online_mfa]} + {_, false, true} -> {Kernel.length(nodes), [online_mfa]} + {_, true, false} -> {1, [offline_mfa]} + end + + RpcStream.receive_list_items_with_fun(node_name, mfas, timeout, info_keys, chunks, fn + {{:error, {:badrpc, {:timeout, to}}}, :finished} -> + {{:error, + {:badrpc, + {:timeout, to, + "Some queue(s) are unresponsive, use list_unresponsive_queues command."}}}, + :finished} + + any -> + any + end) + end) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def help_section(), do: :observability_and_health_checks + + def usage_additional do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")], + ["--online", "lists only queues on online (reachable) nodes"], + ["--offline", "lists only queues on offline (unreachable) nodes"], + ["--local", "only return queues hosted on the target node"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.queues() + ] + end + + def banner(_, %{vhost: vhost, timeout: timeout}) do + [ + "Timeout: #{timeout / 1000} seconds ...", + "Listing queues for vhost #{vhost} ..." + ] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_topic_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_topic_permissions_command.ex new file mode 100644 index 0000000000..1a22b3b26d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_topic_permissions_command.ex @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListTopicPermissionsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :list_vhost_topic_permissions, + [vhost], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_topic_permissions [--vhost <vhost>] [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :access_control + def description(), do: "Lists topic permissions in a virtual host" + + def banner(_, %{vhost: vhost}), do: "Listing topic permissions for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_unresponsive_queues_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_unresponsive_queues_command.ex new file mode 100644 index 0000000000..91d6d624f5 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_unresponsive_queues_command.ex @@ -0,0 +1,95 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListUnresponsiveQueuesCommand do + require RabbitMQ.CLI.Ctl.InfoKeys + require RabbitMQ.CLI.Ctl.RpcStream + + alias RabbitMQ.CLI.Ctl.{InfoKeys, RpcStream} + alias RabbitMQ.CLI.Core.Helpers + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @info_keys ~w(name durable auto_delete + arguments pid recoverable_slaves)a + + def info_keys(), do: @info_keys + + def scopes(), do: [:ctl, :diagnostics] + + def switches() do + [queue_timeout: :integer, local: :boolean, timeout: :integer] + end + + def aliases(), do: [t: :timeout] + + defp default_opts() do + %{vhost: "/", local: false, queue_timeout: 15, table_headers: true} + end + + def merge_defaults([_ | _] = args, opts) do + {args, Map.merge(default_opts(), opts)} + end + + def merge_defaults([], opts) do + merge_defaults(~w(name), opts) + end + + def validate(args, _opts) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run(args, %{ + node: node_name, + vhost: vhost, + timeout: timeout, + queue_timeout: qtimeout, + local: local_opt + }) do + info_keys = InfoKeys.prepare_info_keys(args) + queue_timeout = qtimeout * 1000 + + Helpers.with_nodes_in_cluster(node_name, fn nodes -> + local_mfa = {:rabbit_amqqueue, :emit_unresponsive_local, [vhost, info_keys, queue_timeout]} + all_mfa = {:rabbit_amqqueue, :emit_unresponsive, [nodes, vhost, info_keys, queue_timeout]} + + {chunks, mfas} = + case local_opt do + true -> {1, [local_mfa]} + false -> {Kernel.length(nodes), [all_mfa]} + end + + RpcStream.receive_list_items(node_name, mfas, timeout, info_keys, chunks) + end) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def banner(_, %{vhost: vhost}), do: "Listing unresponsive queues for vhost #{vhost} ..." + + def usage() do + "list_unresponsive_queues [--local] [--queue-timeout <milliseconds>] [<column> ...] [--no-table-headers]" + end + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")], + ["--local", "only return queues hosted on the target node"], + ["--queue-timeout <milliseconds>", "per-queue timeout to use when checking for responsiveness"] + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Tests queues to respond within timeout. Lists those which did not respond" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_limits_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_limits_command.ex new file mode 100644 index 0000000000..5e0de38b3f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_limits_command.ex @@ -0,0 +1,91 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListUserLimitsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + def switches(), do: [global: :boolean, user: :string] + + def merge_defaults(args, %{global: true} = opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{user: "guest", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, global: true}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :get_user_limits, []) do + [] -> + [] + + {:error, err} -> + {:error, err} + + {:badrpc, node} -> + {:badrpc, node} + + val -> + Enum.map(val, fn {user, val} -> + {:ok, val_encoded} = JSON.encode(Map.new(val)) + [user: user, limits: val_encoded] + end) + end + end + + def run([], %{node: node_name, user: username}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :get_user_limits, [username]) do + :undefined -> + {:error, {:no_such_user, username}} + + {:error, err} -> + {:error, err} + + {:badrpc, node} -> + {:badrpc, node} + + val when is_list(val) or is_map(val) -> + {:ok, val_encoded} = JSON.encode(Map.new(val)) + val_encoded + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_user_limits [--user <username>] [--global]" + + def usage_additional() do + [ + ["--global", "list limits for all the users"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Displays configured user limits" + + def banner([], %{global: true}) do + "Listing limits for all users ..." + end + + def banner([], %{user: username}) do + "Listing limits for user \"#{username}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_permissions_command.ex new file mode 100644 index 0000000000..bd302eefd0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_permissions_command.ex @@ -0,0 +1,58 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListUserPermissionsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def scopes(), do: [:ctl, :diagnostics] + def switches(), do: [timeout: :integer] + def aliases(), do: [t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def validate([], _), do: {:validation_failure, :not_enough_args} + def validate([_ | _] = args, _) when length(args) > 1, do: {:validation_failure, :too_many_args} + def validate([_], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :list_user_permissions, + [username], + timeout + ) + end + + def usage, do: "list_user_permissions [--no-table-headers] <username>" + + def usage_additional do + [ + ["<username>", "Name of the user"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :access_control + def description(), do: "Lists permissions of a user across all virtual hosts" + + def banner([username], _), do: "Listing permissions for user \"#{username}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_topic_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_topic_permissions_command.ex new file mode 100644 index 0000000000..48b7fee5e2 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_user_topic_permissions_command.ex @@ -0,0 +1,54 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListUserTopicPermissionsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: false}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :list_user_topic_permissions, + [username], + timeout + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_user_topic_permissions [--no-table-headers] <username>" + + def usage_additional do + [ + ["<username>", "Name of the user"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :access_control + def description(), do: "Lists user topic permissions" + + def banner([username], _), do: "Listing topic permissions for user \"#{username}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_users_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_users_command.ex new file mode 100644 index 0000000000..e87ea386d0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_users_command.ex @@ -0,0 +1,43 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListUsersCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :list_users, [], timeout) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_users [--no-table-headers]" + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "List user names and tags" + + def banner(_, _), do: "Listing users ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_vhost_limits_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_vhost_limits_command.ex new file mode 100644 index 0000000000..67b138f1e0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_vhost_limits_command.ex @@ -0,0 +1,90 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListVhostLimitsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + def switches(), do: [global: :boolean] + + def merge_defaults(args, %{global: true} = opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", table_headers: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, global: true}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_vhost_limit, :list, []) do + [] -> + [] + + {:error, err} -> + {:error, err} + + {:badrpc, node} -> + {:badrpc, node} + + val -> + Enum.map(val, fn {vhost, val} -> + {:ok, val_encoded} = JSON.encode(Map.new(val)) + [vhost: vhost, limits: val_encoded] + end) + end + end + + def run([], %{node: node_name, vhost: vhost}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_vhost_limit, :list, [vhost]) do + [] -> + [] + + {:error, err} -> + {:error, err} + + {:badrpc, node} -> + {:badrpc, node} + + val when is_list(val) or is_map(val) -> + JSON.encode(Map.new(val)) + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_vhost_limits [--vhost <vhost>] [--global] [--no-table-headers]" + + def usage_additional() do + [ + ["--global", "list global limits (those not associated with a virtual host)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def description(), do: "Displays configured virtual host limits" + + def banner([], %{global: true}) do + "Listing limits for all vhosts ..." + end + + def banner([], %{vhost: vhost}) do + "Listing limits for vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_vhosts_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_vhosts_command.ex new file mode 100644 index 0000000000..b570aa7486 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/list_vhosts_command.ex @@ -0,0 +1,84 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ListVhostsCommand do + alias RabbitMQ.CLI.Core.DocGuide + alias RabbitMQ.CLI.Ctl.InfoKeys + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + @info_keys ~w(name description tags tracing cluster_state)a + + def info_keys(), do: @info_keys + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults([], opts) do + merge_defaults(["name"], opts) + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([_ | _] = args, %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost, :info_all, [], timeout) + |> filter_by_arg(args) + end + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "list_vhosts [--no-table-headers] [<column> ...]" + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts(), + DocGuide.access_control() + ] + end + + def help_section(), do: :access_control + + def description(), do: "Lists virtual hosts" + + def banner(_, _), do: "Listing vhosts ..." + + # + # Implementation + # + + defp filter_by_arg(vhosts, _) when is_tuple(vhosts) do + vhosts + end + + defp filter_by_arg(vhosts, [_ | _] = args) do + symbol_args = InfoKeys.prepare_info_keys(args) + + vhosts + |> Enum.map(fn vhost -> + symbol_args + |> Enum.filter(fn arg -> vhost[arg] != nil end) + |> Enum.map(fn arg -> {arg, vhost[arg]} end) + end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/node_health_check_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/node_health_check_command.ex new file mode 100644 index 0000000000..31ea748d9f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/node_health_check_command.ex @@ -0,0 +1,87 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.NodeHealthCheckCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 70_000 + + def scopes(), do: [:ctl, :diagnostics] + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, Map.merge(opts, %{timeout: timeout})} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_health_check, :node, [node_name, timeout]) do + :ok -> + :ok + + true -> + :ok + + {:badrpc, _} = err -> + err + + {:error_string, error_message} -> + {:healthcheck_failed, error_message} + + {:node_is_ko, error_message, _exit_code} -> + {:healthcheck_failed, error_message} + + other -> + other + end + end + + def output(:ok, _) do + {:ok, "Health check passed"} + end + + def output({:healthcheck_failed, message}, _) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: health check failed. Message: #{message}"} + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "node_health_check" + + def usage_doc_guides() do + [ + DocGuide.monitoring() + ] + end + + def help_section(), do: :deprecated + def description() do + "DEPRECATED. Performs intrusive, opinionated health checks on a fully booted node. " <> + "See https://www.rabbitmq.com/monitoring.html#health-checks instead" + end + + def banner(_, %{node: node_name, timeout: timeout}) do + [ + "This command is DEPRECATED and will be removed in a future version.", + "It performs intrusive, opinionated health checks and requires a fully booted node.", + "Use one of the options covered in https://www.rabbitmq.com/monitoring.html#health-checks instead.", + "Timeout: #{trunc(timeout / 1000)} seconds ...", + "Checking health of node #{node_name} ..." + ] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/ping_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/ping_command.ex new file mode 100644 index 0000000000..7efb3b39f3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/ping_command.ex @@ -0,0 +1,90 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.PingCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 60_000 + + def scopes(), do: [:ctl, :diagnostics] + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, Map.merge(opts, %{timeout: timeout})} + end + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + # this is very similar to what net_adm:ping/1 does reimplemented with support for custom timeouts + # and error values that are used by CLI commands + msg = "Failed to connect and authenticate to #{node_name} in #{timeout} ms" + + try do + case :gen.call({:net_kernel, node_name}, :"$gen_call", {:is_auth, node()}, timeout) do + :ok -> + :ok + + {:ok, _} -> + :ok + + _ -> + :erlang.disconnect_node(node_name) + {:error, msg} + end + catch + :exit, _ -> + :erlang.disconnect_node(node_name) + {:error, msg} + + _ -> + :erlang.disconnect_node(node_name) + {:error, msg} + end + end + + def output(:ok, _) do + {:ok, "Ping succeeded"} + end + + def output({:error, :timeout}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: timed out while waiting for a response from #{node_name}."} + end + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "ping" + end + + def usage_doc_guides() do + [ + DocGuide.monitoring() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Checks that the node OS process is up, registered with EPMD and CLI tools can authenticate with it" + + def banner([], %{node: node_name, timeout: timeout}) when is_number(timeout) do + "Will ping #{node_name}. This only checks if the OS process is running and registered with epmd. Timeout: #{ + timeout + } ms." + end + + def banner([], %{node: node_name, timeout: _timeout}) do + "Will ping #{node_name}. This only checks if the OS process is running and registered with epmd." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/purge_queue_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/purge_queue_command.ex new file mode 100644 index 0000000000..1be25beb7d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/purge_queue_command.ex @@ -0,0 +1,73 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.PurgeQueueCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([queue], %{node: node_name, vhost: vhost, timeout: timeout}) do + res = + :rabbit_misc.rpc_call( + node_name, + :rabbit_amqqueue, + :lookup, + [:rabbit_misc.r(vhost, :queue, queue)], + timeout + ) + + case res do + {:ok, q} -> purge(node_name, q, timeout) + _ -> res + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "purge_queue <queue>" + + def usage_additional() do + [ + ["<queue>", "Name of the queue to purge"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.queues() + ] + end + + def help_section(), do: :queues + + def description(), do: "Purges a queue (removes all messages in it)" + + def banner([queue], %{vhost: vhost}) do + "Purging queue '#{queue}' in vhost '#{vhost}' ..." + end + + # + # Implementation + # + + defp purge(node_name, q, timeout) do + res = :rabbit_misc.rpc_call(node_name, :rabbit_amqqueue, :purge, [q], timeout) + + case res do + {:ok, _message_count} -> :ok + _ -> res + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/rename_cluster_node_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/rename_cluster_node_command.ex new file mode 100644 index 0000000000..7faa30d00b --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/rename_cluster_node_command.ex @@ -0,0 +1,108 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.RenameClusterNodeCommand do + require Integer + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts), do: {args, opts} + + def validate(args, _) when length(args) < 2 do + {:validation_failure, :not_enough_args} + end + + def validate(_, _) do + :ok + end + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &validate_args_count_even/2, + &Validators.node_is_not_running/2, + &Validators.mnesia_dir_is_set/2, + &Validators.feature_flags_file_is_set/2, + &Validators.rabbit_is_loaded/2 + ], + [args, opts] + ) + end + + def run(nodes, %{node: node_name}) do + node_pairs = make_node_pairs(nodes) + + try do + :rabbit_mnesia_rename.rename(node_name, node_pairs) + catch + _, reason -> + {:rename_failed, reason} + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "rename_cluster_node <oldnode1> <newnode1> [oldnode2] [newnode2] ..." + end + + def usage_additional() do + [ + ["<oldnode>", "Original node name"], + ["<newnode>", "New node name"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.clustering() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Renames cluster nodes in the local database" + + def banner(args, _) do + [ + "Renaming cluster nodes: \n ", + for {node_from, node_to} <- make_node_pairs(args) do + "#{node_from} -> #{node_to} \n" + end + ] + |> List.flatten() + |> Enum.join() + end + + # + # Implementation + # + + defp validate_args_count_even(args, _) do + case agrs_count_even?(args) do + true -> + :ok + + false -> + {:validation_failure, + {:bad_argument, "Argument list should contain even number of nodes"}} + end + end + + defp agrs_count_even?(args) do + Integer.is_even(length(args)) + end + + defp make_node_pairs([]) do + [] + end + + defp make_node_pairs([from, to | rest]) do + [{to_atom(from), to_atom(to)} | make_node_pairs(rest)] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/report_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/report_command.ex new file mode 100644 index 0000000000..c06497a7e6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/report_command.ex @@ -0,0 +1,118 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ReportCommand do + alias RabbitMQ.CLI.Core.DocGuide + alias RabbitMQ.CLI.Ctl.Commands.{ + ClusterStatusCommand, + EnvironmentCommand, + ListBindingsCommand, + ListChannelsCommand, + ListConnectionsCommand, + ListExchangesCommand, + ListGlobalParametersCommand, + ListParametersCommand, + ListPermissionsCommand, + ListPoliciesCommand, + ListQueuesCommand, + StatusCommand + } + alias RabbitMQ.CLI.Diagnostics.Commands.{ + CommandLineArgumentsCommand, + OsEnvCommand + } + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([_ | _] = args, _) when length(args) != 0, + do: {:validation_failure, :too_many_args} + + def validate([], %{formatter: formatter}) do + case formatter do + "report" -> :ok + _other -> {:validation_failure, "Only report formatter is supported"} + end + end + + def validate([], _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name} = opts) do + case :rabbit_misc.rpc_call(node_name, :rabbit_vhost, :list_names, []) do + {:badrpc, _} = err -> + err + + vhosts -> + data = [ + run_command(StatusCommand, [], opts), + run_command(ClusterStatusCommand, [], opts), + run_command(EnvironmentCommand, [], opts), + run_command(ListConnectionsCommand, info_keys(ListConnectionsCommand), opts), + run_command(ListChannelsCommand, info_keys(ListChannelsCommand), opts), + run_command(CommandLineArgumentsCommand, [], opts), + run_command(OsEnvCommand, [], opts) + ] + + vhost_data = + vhosts + |> Enum.flat_map(fn v -> + opts = Map.put(opts, :vhost, v) + + [ + run_command(ListQueuesCommand, info_keys(ListQueuesCommand), opts), + run_command(ListExchangesCommand, info_keys(ListExchangesCommand), opts), + run_command(ListBindingsCommand, info_keys(ListBindingsCommand), opts), + run_command(ListPermissionsCommand, [], opts), + run_command(ListPoliciesCommand, [], opts), + run_command(ListGlobalParametersCommand, [], opts), + run_command(ListParametersCommand, [], opts), + + ] + end) + + data ++ vhost_data + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Report + + def usage, do: "report" + + def usage_doc_guides() do + [ + DocGuide.monitoring() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Generate a server status report containing a concatenation of all server status information for support purposes" + + def banner(_, %{node: node_name}), do: "Reporting server status of node #{node_name} ..." + + # + # Implementation + # + + defp run_command(command, args, opts) do + {args, opts} = command.merge_defaults(args, opts) + banner = command.banner(args, opts) + command_result = command.run(args, opts) |> command.output(opts) + {command, banner, command_result} + end + + defp info_keys(command) do + command.info_keys() + |> Enum.map(&to_string/1) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/reset_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/reset_command.ex new file mode 100644 index 0000000000..575ef2491d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/reset_command.ex @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ResetCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppStopped + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :reset, []) + end + + def output({:error, :mnesia_unexpectedly_running}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + RabbitMQ.CLI.DefaultOutput.mnesia_running_error(node_name)} + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "reset" + + def usage_doc_guides() do + [ + DocGuide.clustering() + ] + end + + def help_section(), do: :node_management + + def description(), do: "Instructs a RabbitMQ node to leave the cluster and return to its virgin state" + + def banner(_, %{node: node_name}), do: "Resetting node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/restart_vhost_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/restart_vhost_command.ex new file mode 100644 index 0000000000..36a7f702bc --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/restart_vhost_command.ex @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +alias RabbitMQ.CLI.Core.ExitCodes + +defmodule RabbitMQ.CLI.Ctl.Commands.RestartVhostCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts), do: {args, Map.merge(%{vhost: "/"}, opts)} + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, vhost: vhost, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost_sup_sup, :start_vhost, [vhost], timeout) + end + + def output({:ok, _pid}, %{vhost: vhost, node: node_name}) do + {:ok, "Successfully restarted vhost '#{vhost}' on node '#{node_name}'"} + end + + def output({:error, {:already_started, _pid}}, %{vhost: vhost, node: node_name}) do + {:ok, "Vhost '#{vhost}' is already running on node '#{node_name}'"} + end + + def output({:error, err}, %{vhost: vhost, node: node_name}) do + {:error, ExitCodes.exit_software(), + ["Failed to start vhost '#{vhost}' on node '#{node_name}'", "Reason: #{inspect(err)}"]} + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "restart_vhost [--vhost <vhost>]" + + def usage_additional() do + [ + ["--vhost", "Virtual host name"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :virtual_hosts + + def description(), do: "Restarts a failed vhost data stores and queues" + + def banner(_, %{node: node_name, vhost: vhost}) do + "Trying to restart vhost '#{vhost}' on node '#{node_name}' ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/resume_listeners_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/resume_listeners_command.ex new file mode 100644 index 0000000000..1f13660e0d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/resume_listeners_command.ex @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ResumeListenersCommand do + @moduledoc """ + Resumes all client connection listeners making them accept new client + connections. This command is the opposite of `SuspendListenersCommand`. + + This command is meant to be used when automating upgrades. + See also `SuspendListenersCommand`. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.DocGuide + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_maintenance, :resume_all_client_listeners, [], timeout) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "resume_listeners" + + def usage_doc_guides() do + [ + DocGuide.upgrade() + ] + end + + def help_section(), do: :operations + + def description(), do: "Resumes client connection listeners making them accept client connections again" + + def banner(_, %{node: node_name}) do + "Will resume client connection listeners on node #{node_name}. " + <> "The node will now accept client connections" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/rotate_logs_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/rotate_logs_command.ex new file mode 100644 index 0000000000..f3de3671fc --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/rotate_logs_command.ex @@ -0,0 +1,34 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.RotateLogsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :rotate_logs, []) + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "rotate_logs" + + def usage_doc_guides() do + [ + DocGuide.logging() + ] + end + + def help_section(), do: :node_management + + def description(), do: "Instructs the RabbitMQ node to perform internal log rotation" + + def banner(_, %{node: node_name}), do: "Rotating logs for node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_cluster_name_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_cluster_name_command.ex new file mode 100644 index 0000000000..f919cb2ae6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_cluster_name_command.ex @@ -0,0 +1,47 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetClusterNameCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([cluster_name], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_nodes, :set_cluster_name, [ + cluster_name, + Helpers.cli_acting_user() + ]) + end + + use RabbitMQ.CLI.DefaultOutput + + def banner([cluster_name], _) do + "Setting cluster name to #{cluster_name} ..." + end + + def usage, do: "set_cluster_name <name>" + + def usage_additional() do + [ + ["<name>", "New cluster name"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts(), + DocGuide.access_control() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Sets the cluster name" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_disk_free_limit_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_disk_free_limit_command.ex new file mode 100644 index 0000000000..cf97c4655e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_disk_free_limit_command.ex @@ -0,0 +1,140 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetDiskFreeLimitCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.Memory + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate(["mem_relative"], _) do + {:validation_failure, :not_enough_args} + end + + def validate(["mem_relative" | _] = args, _) when length(args) != 2 do + {:validation_failure, :too_many_args} + end + + def validate([limit], _) do + case Integer.parse(limit) do + {_, ""} -> + :ok + + {limit_val, units} -> + case memory_unit_absolute(limit_val, units) do + scaled_limit when is_integer(scaled_limit) -> :ok + _ -> {:validation_failure, :bad_argument} + end + + _ -> + {:validation_failure, :bad_argument} + end + end + + def validate(["mem_relative", fraction], _) do + case Float.parse(fraction) do + {val, ""} when val >= 0.0 -> :ok + _ -> {:validation_failure, :bad_argument} + end + end + + def validate([_ | rest], _) when length(rest) > 0 do + {:validation_failure, :too_many_args} + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run(["mem_relative", _] = args, opts) do + set_disk_free_limit_relative(args, opts) + end + + def run([limit], %{node: _} = opts) when is_binary(limit) do + case Integer.parse(limit) do + {limit_val, ""} -> set_disk_free_limit_absolute([limit_val], opts) + {limit_val, units} -> set_disk_free_limit_in_units([limit_val, units], opts) + end + end + + def run([limit], opts) do + set_disk_free_limit_absolute([limit], opts) + end + + use RabbitMQ.CLI.DefaultOutput + + def banner(["mem_relative", arg], %{node: node_name}) do + "Setting disk free limit on #{node_name} to #{arg} times the total RAM ..." + end + + def banner([arg], %{node: node_name}), + do: "Setting disk free limit on #{node_name} to #{arg} bytes ..." + + def usage, do: "set_disk_free_limit <disk_limit> | mem_relative <fraction>" + + def usage_additional() do + [ + ["<disk_limit>", "New limit as an absolute value with units, e.g. 1GB"], + ["mem_relative <fraction>", "New limit as a fraction of total memory reported by the OS"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.disk_alarms(), + DocGuide.alarms() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Sets the disk_free_limit setting" + + # + # Implementation + # + + defp set_disk_free_limit_relative(["mem_relative", fraction], %{node: node_name}) + when is_float(fraction) do + make_rpc_call(node_name, [{:mem_relative, fraction}]) + end + + defp set_disk_free_limit_relative(["mem_relative", integer_input], %{node: node_name}) + when is_integer(integer_input) do + make_rpc_call(node_name, [{:mem_relative, integer_input * 1.0}]) + end + + defp set_disk_free_limit_relative(["mem_relative", fraction_str], %{node: _} = opts) + when is_binary(fraction_str) do + {fraction_val, ""} = Float.parse(fraction_str) + set_disk_free_limit_relative(["mem_relative", fraction_val], opts) + end + + ## ------------------------ Absolute Size Call ----------------------------- + + defp set_disk_free_limit_absolute([limit], %{node: node_name}) when is_integer(limit) do + make_rpc_call(node_name, [limit]) + end + + defp set_disk_free_limit_absolute([limit], %{node: _} = opts) when is_float(limit) do + set_disk_free_limit_absolute([limit |> Float.floor() |> round], opts) + end + + defp set_disk_free_limit_in_units([limit_val, units], opts) do + case memory_unit_absolute(limit_val, units) do + scaled_limit when is_integer(scaled_limit) -> + set_disk_free_limit_absolute([scaled_limit], opts) + end + end + + defp make_rpc_call(node_name, args) do + :rabbit_misc.rpc_call(node_name, :rabbit_disk_monitor, :set_disk_free_limit, args) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_global_parameter_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_global_parameter_command.ex new file mode 100644 index 0000000000..8c46e9d592 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_global_parameter_command.ex @@ -0,0 +1,57 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetGlobalParameterCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate(args, _) when length(args) < 2 do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, value], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime_parameters, + :parse_set_global, + [name, value, Helpers.cli_acting_user()] + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "set_global_parameter <name> <value>" + + def usage_additional() do + [ + ["<name>", "global parameter name (identifier)"], + ["<value>", "parameter value"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :parameters + + def description(), do: "Sets a runtime parameter." + + def banner([name, value], _) do + "Setting global runtime parameter \"#{name}\" to \"#{value}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_log_level_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_log_level_command.ex new file mode 100644 index 0000000000..f5a8eacbfc --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_log_level_command.ex @@ -0,0 +1,74 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetLogLevelCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + @known_levels [ + "debug", + "info", + "notice", + "warning", + "error", + "critical", + "alert", + "emergency", + "none" + ] + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate([level], _) do + case Enum.member?(@known_levels, level) do + true -> + :ok + + false -> + {:error, "level #{level} is not supported. Try one of debug, info, warning, error, none"} + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([log_level], %{node: node_name}) do + arg = String.to_atom(log_level) + :rabbit_misc.rpc_call(node_name, :rabbit_lager, :set_log_level, [arg]) + end + + def usage, do: "set_log_level <log_level>" + + def usage_additional() do + [ + ["<log_level>", "new log level"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.logging() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Sets log level in the running node" + + def banner([log_level], _), do: "Setting log level to \"#{log_level}\" ..." + + def output({:error, {:invalid_log_level, level}}, _opts) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "level #{level} is not supported. Try one of debug, info, warning, error, none"} + end + + use RabbitMQ.CLI.DefaultOutput +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_operator_policy_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_operator_policy_command.ex new file mode 100644 index 0000000000..3118c125cb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_operator_policy_command.ex @@ -0,0 +1,79 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetOperatorPolicyCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [priority: :integer, apply_to: :string] + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", priority: 0, apply_to: "all"}, opts)} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) < 3 do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) > 3 do + {:validation_failure, :too_many_args} + end + + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, pattern, definition], %{ + node: node_name, + vhost: vhost, + priority: priority, + apply_to: apply_to + }) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_policy, + :parse_set_op, + [vhost, name, pattern, definition, priority, apply_to, Helpers.cli_acting_user()] + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "set_operator_policy [--vhost <vhost>] [--priority <priority>] [--apply-to <apply-to>] <name> <pattern> <definition>" + end + + def usage_additional() do + [ + ["<name>", "policy name (identifier)"], + ["<pattern>", "a regular expression pattern that will be used to match queue, exchanges, etc"], + ["<definition>", "policy definition (arguments). Must be a valid JSON document"], + ["--priority <priority>", "policy priority"], + ["--apply-to <queues | exchanges | all>", "policy should only apply to queues, exchanges, or all entities (both of the above)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :policies + + def description(), do: "Sets an operator policy that overrides a subset of arguments in user policies" + + def banner([name, pattern, definition], %{vhost: vhost, priority: priority}) do + "Setting operator policy override \"#{name}\" for pattern \"#{pattern}\" to \"#{definition}\" with priority \"#{ + priority + }\" for vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_parameter_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_parameter_command.ex new file mode 100644 index 0000000000..910cc6ef73 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_parameter_command.ex @@ -0,0 +1,68 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetParameterCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) < 3 do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) > 3 do + {:validation_failure, :too_many_args} + end + + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([component_name, name, value], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime_parameters, + :parse_set, + [vhost, component_name, name, value, Helpers.cli_acting_user()] + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "set_parameter [--vhost <vhost>] <component_name> <name> <value>" + + def usage_additional() do + [ + ["<component_name>", "component name"], + ["<name>", "parameter name (identifier)"], + ["<value>", "parameter value"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :parameters + + def description(), do: "Sets a runtime parameter." + + def banner([component_name, name, value], %{vhost: vhost}) do + "Setting runtime parameter \"#{name}\" for component \"#{component_name}\" to \"#{value}\" in vhost \"#{ + vhost + }\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_permissions_command.ex new file mode 100644 index 0000000000..c87969121c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_permissions_command.ex @@ -0,0 +1,78 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) < 4 do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _] = args, _) when length(args) > 4 do + {:validation_failure, :too_many_args} + end + + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([user, conf, write, read], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :set_permissions, + [user, vhost, conf, write, read, Helpers.cli_acting_user()] + ) + end + + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exist"}} + end + def output({:error, {:no_such_vhost, vhost}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User #{username} does not exist"} + end + def output({:error, {:no_such_vhost, vhost}}, _) do + {:error, "Virtual host #{vhost} does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "set_permissions [--vhost <vhost>] <username> <conf> <write> <read>" + + def usage_additional() do + [ + ["<username>", "Self-explanatory"], + ["<conf>", "Configuration permission pattern"], + ["<write>", "Write permission pattern"], + ["<read>", "Read permission pattern"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :access_control + def description(), do: "Sets user permissions for a vhost" + + def banner([user | _], %{vhost: vhost}), + do: "Setting permissions for user \"#{user}\" in vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_policy_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_policy_command.ex new file mode 100644 index 0000000000..af34f3c659 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_policy_command.ex @@ -0,0 +1,76 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetPolicyCommand do + alias RabbitMQ.CLI.Core.{Helpers, DocGuide} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [priority: :integer, apply_to: :string] + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/", priority: 0, apply_to: "all"}, opts)} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) < 3 do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 3 do + {:validation_failure, :too_many_args} + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, pattern, definition], %{ + node: node_name, + vhost: vhost, + priority: priority, + apply_to: apply_to + }) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_policy, + :parse_set, + [vhost, name, pattern, definition, priority, apply_to, Helpers.cli_acting_user()] + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "set_policy [--vhost <vhost>] [--priority <priority>] [--apply-to <apply-to>] <name> <pattern> <definition>" + end + + def usage_additional() do + [ + ["<name>", "policy name (identifier)"], + ["<pattern>", "regular expression pattern that will be used to match queues, exchanges, etc"], + ["<definition>", "policy definition (arguments). Must be a valid JSON document"], + ["--priority <priority>", "policy priority"], + ["--apply-to <queues | exchanges | all>", "policy should only apply to queues, exchanges, or all entities (both of the above)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.parameters() + ] + end + + def help_section(), do: :policies + + def description(), do: "Sets or updates a policy" + + def banner([name, pattern, definition], %{vhost: vhost, priority: priority}) do + "Setting policy \"#{name}\" for pattern \"#{pattern}\" to \"#{definition}\" with priority \"#{ + priority + }\" for vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_topic_permissions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_topic_permissions_command.ex new file mode 100644 index 0000000000..c57dc1659b --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_topic_permissions_command.ex @@ -0,0 +1,75 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetTopicPermissionsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + def validate(args, _) when length(args) < 4 do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 4 do + {:validation_failure, :too_many_args} + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([user, exchange, write_pattern, read_pattern], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :set_topic_permissions, + [user, vhost, exchange, write_pattern, read_pattern, Helpers.cli_acting_user()] + ) + end + + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exist"}} + end + def output({:error, {:no_such_vhost, vhost}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User #{username} does not exist"} + end + def output({:error, {:no_such_vhost, vhost}}, _) do + {:error, "Virtual host #{vhost} does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage do + "set_topic_permissions [--vhost <vhost>] <username> <exchange> <write> <read>" + end + + def usage_additional do + [ + ["<username>", "Self-explanatory"], + ["<exchange>", "Topic exchange to set the permissions for"], + ["<write>", "Write permission pattern"], + ["<read>", "Read permission pattern"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :access_control + + def description(), do: "Sets user topic permissions for an exchange" + + def banner([user, exchange, _, _], %{vhost: vhost}), + do: + "Setting topic permissions on \"#{exchange}\" for user \"#{user}\" in vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_user_limits_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_user_limits_command.ex new file mode 100644 index 0000000000..603a8008e7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_user_limits_command.ex @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetUserLimitsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([username, definition], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :set_user_limits, [ + username, + definition, + Helpers.cli_acting_user() + ]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "set_user_limits <username> <definition>" + + def usage_additional() do + [ + ["<username>", "Self-explanatory"], + ["<definition>", "Limit definitions as a JSON document"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Sets user limits" + + def banner([username, definition], %{}) do + "Setting user limits to \"#{definition}\" for user \"#{username}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_user_tags_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_user_tags_command.ex new file mode 100644 index 0000000000..eba8ed6123 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_user_tags_command.ex @@ -0,0 +1,61 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetUserTagsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts), do: {args, opts} + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([user | tags], %{node: node_name}) do + :rabbit_misc.rpc_call( + node_name, + :rabbit_auth_backend_internal, + :set_tags, + [user, tags, Helpers.cli_acting_user()] + ) + end + + def output({:error, {:no_such_user, username}}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exists"}} + end + def output({:error, {:no_such_user, username}}, _) do + {:error, ExitCodes.exit_nouser(), "User \"#{username}\" does not exist"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "set_user_tags <username> <tag> [...]" + + def usage_additional() do + [ + ["<username>", "Self-explanatory"], + ["<tags>", "Space separated list of tags"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.management(), + DocGuide.access_control() + ] + end + + def help_section(), do: :user_management + + def description(), do: "Sets user tags" + + def banner([user | tags], _) do + "Setting tags for user \"#{user}\" to [#{tags |> Enum.join(", ")}] ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_vhost_limits_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_vhost_limits_command.ex new file mode 100644 index 0000000000..f25f1c7bc4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_vhost_limits_command.ex @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetVhostLimitsCommand do + alias RabbitMQ.CLI.Core.{DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([definition], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost_limit, :parse_set, [ + vhost, + definition, + Helpers.cli_acting_user() + ]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "set_vhost_limits [--vhost <vhost>] <definition>" + + def usage_additional() do + [ + ["<definition>", "Limit definitions, must be a valid JSON document"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def description(), do: "Sets virtual host limits" + + def banner([definition], %{vhost: vhost}) do + "Setting vhost limits to \"#{definition}\" for vhost \"#{vhost}\" ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_vm_memory_high_watermark_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_vm_memory_high_watermark_command.ex new file mode 100644 index 0000000000..a4e4527f8f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_vm_memory_high_watermark_command.ex @@ -0,0 +1,146 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SetVmMemoryHighWatermarkCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.Memory + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate(["absolute"], _) do + {:validation_failure, :not_enough_args} + end + + def validate(["absolute" | _] = args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + + def validate(["absolute", arg], _) do + case Integer.parse(arg) do + :error -> + {:validation_failure, :bad_argument} + + {_, rest} -> + case Enum.member?(memory_units(), rest) do + true -> + :ok + + false -> + case Float.parse(arg) do + {_, orest} when orest == rest -> + {:validation_failure, {:bad_argument, "Invalid units."}} + + _ -> + {:validation_failure, {:bad_argument, "The threshold should be an integer."}} + end + end + end + end + + def validate([_ | _] = args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate([arg], _) when is_number(arg) and (arg < 0.0 or arg > 1.0) do + {:validation_failure, + {:bad_argument, "The threshold should be a fraction between 0.0 and 1.0"}} + end + + def validate([arg], %{}) when is_binary(arg) do + case Float.parse(arg) do + {arg, ""} when is_number(arg) and (arg < 0.0 or arg > 1.0) -> + {:validation_failure, + {:bad_argument, "The threshold should be a fraction between 0.0 and 1.0"}} + + {_, ""} -> + :ok + + _ -> + {:validation_failure, :bad_argument} + end + end + + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run(["absolute", arg], opts) do + case Integer.parse(arg) do + {num, rest} -> + valid_units = rest in memory_units() + set_vm_memory_high_watermark_absolute({num, rest}, valid_units, opts) + end + end + + def run([arg], %{node: node_name}) when is_number(arg) and arg >= 0.0 do + :rabbit_misc.rpc_call( + node_name, + :vm_memory_monitor, + :set_vm_memory_high_watermark, + [arg] + ) + end + + def run([arg], %{} = opts) when is_binary(arg) do + case Float.parse(arg) do + {num, ""} -> run([num], opts) + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage do + "set_vm_memory_high_watermark <fraction> | absolute <value>" + end + + def usage_additional() do + [ + ["<fraction>", "New limit as a fraction of total memory reported by the OS"], + ["absolute <value>", "New limit as an absolute value with units, e.g. 1GB"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.alarms(), + DocGuide.memory_use(), + ] + end + + def help_section(), do: :configuration + + def description(), do: "Sets the vm_memory_high_watermark setting" + + def banner(["absolute", arg], %{node: node_name}) do + "Setting memory threshold on #{node_name} to #{arg} bytes ..." + end + + def banner([arg], %{node: node_name}) do + "Setting memory threshold on #{node_name} to #{arg} ..." + end + + # + # Implementation + # + + defp set_vm_memory_high_watermark_absolute({num, rest}, true, %{node: node_name}) + when num > 0 do + val = memory_unit_absolute(num, rest) + + :rabbit_misc.rpc_call( + node_name, + :vm_memory_monitor, + :set_vm_memory_high_watermark, + [{:absolute, val}] + ) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/shutdown_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/shutdown_command.ex new file mode 100644 index 0000000000..10700bf309 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/shutdown_command.ex @@ -0,0 +1,106 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.ShutdownCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + alias RabbitMQ.CLI.Core.{OsPid, NodeName} + + def switches() do + [timeout: :integer, + wait: :boolean] + end + + def aliases(), do: [timeout: :t] + + def merge_defaults(args, opts) do + {args, Map.merge(%{wait: true, timeout: 120}, opts)} + end + + def validate([], %{wait: false}) do + :ok + end + + def validate([], %{node: node_name, wait: true}) do + local_hostname = NodeName.hostname_from_node(Node.self()) + remote_hostname = NodeName.hostname_from_node(node_name) + case addressing_local_node?(local_hostname, remote_hostname) do + true -> :ok; + false -> + msg = "\nThis command can only --wait for shutdown of local nodes " <> + "but node #{node_name} seems to be remote " <> + "(local hostname: #{local_hostname}, remote: #{remote_hostname}).\n" <> + "Pass --no-wait to shut node #{node_name} down without waiting.\n" + {:validation_failure, {:unsupported_target, msg}} + end + end + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, wait: false, timeout: timeout}) do + shut_down_node_without_waiting(node_name, timeout) + end + + def run([], %{node: node_name, wait: true, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :os, :getpid, []) do + pid when is_list(pid) -> + shut_down_node_and_wait_pid_to_stop(node_name, pid, timeout) + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "shutdown [--wait]" + + def usage_additional() do + [ + ["--wait", "if set, will wait for target node to terminate (by inferring and monitoring its PID file). Only works for local nodes."], + ["--no-wait", "if set, will not wait for target node to terminate"] + ] + end + + def help_section(), do: :node_management + + def description(), do: "Stops RabbitMQ and its runtime (Erlang VM). Monitors progress for local nodes. Does not require a PID file path." + + def banner(_, _), do: nil + + + # + # Implementation + # + + def addressing_local_node?(_, remote_hostname) when remote_hostname == :localhost , do: :true + def addressing_local_node?(_, remote_hostname) when remote_hostname == 'localhost', do: :true + def addressing_local_node?(_, remote_hostname) when remote_hostname == "localhost", do: :true + def addressing_local_node?(local_hostname, remote_hostname) do + local_hostname == remote_hostname + end + + defp shut_down_node_without_waiting(node_name, timeout) do + :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, [], timeout) + end + + defp shut_down_node_and_wait_pid_to_stop(node_name, pid, timeout) do + {:stream, + RabbitMQ.CLI.Core.Helpers.stream_until_error([ + fn -> "Shutting down RabbitMQ node #{node_name} running at PID #{pid}" end, + fn -> + res = shut_down_node_without_waiting(node_name, timeout) + + case res do + :ok -> "Waiting for PID #{pid} to terminate" + {:badrpc, err} -> {:error, err} + {:error, _} = err -> err + end + end, + fn -> + OsPid.wait_for_os_process_death(pid) + "RabbitMQ node #{node_name} running at PID #{pid} successfully shut down" + end + ])} + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/start_app_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/start_app_command.ex new file mode 100644 index 0000000000..900bd762fa --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/start_app_command.ex @@ -0,0 +1,25 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.StartAppCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :start, []) + end + + def usage, do: "start_app" + + def help_section(), do: :node_management + + def description(), do: "Starts the RabbitMQ application but leaves the runtime (Erlang VM) running" + + def banner(_, %{node: node_name}), do: "Starting node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/status_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/status_command.ex new file mode 100644 index 0000000000..582f514f27 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/status_command.ex @@ -0,0 +1,253 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.StatusCommand do + alias RabbitMQ.CLI.Core.DocGuide + alias RabbitMQ.CLI.InformationUnit, as: IU + import RabbitMQ.CLI.Core.{Alarms, ANSI, DataCoercion, Listeners, Memory, Platform} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 60_000 + + def scopes(), do: [:ctl, :diagnostics] + + def switches(), do: [unit: :string, timeout: :integer] + def aliases(), do: [t: :timeout] + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, Map.merge(%{unit: "gb", timeout: timeout}, opts)} + end + + def validate(args, _) when length(args) > 0 do + {:validation_failure, :too_many_args} + end + def validate(_, %{unit: unit}) do + case IU.known_unit?(unit) do + true -> + :ok + + false -> + {:validation_failure, "unit '#{unit}' is not supported. Please use one of: bytes, mb, gb"} + end + end + def validate(_, _), do: :ok + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :status, [], timeout) + end + + def output({:error, :timeout}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: timed out while waiting for a response from #{node_name}."} + end + + def output(result, %{formatter: "erlang"}) do + {:ok, result} + end + + def output(result, %{formatter: "json"}) when is_list(result) do + m = result_map(result) |> Map.update(:alarms, [], fn xs -> alarm_maps(xs) end) + + {:ok, m} + end + + def output(result, %{node: node_name, unit: unit}) when is_list(result) do + m = result_map(result) + + product_name_section = case m do + %{:product_name => product_name} when product_name != "" -> + ["Product name: #{product_name}"] + _ -> + [] + end + product_version_section = case m do + %{:product_version => product_version} when product_version != "" -> + ["Product version: #{product_version}"] + _ -> + [] + end + + runtime_section = [ + "#{bright("Runtime")}\n", + "OS PID: #{m[:pid]}", + "OS: #{m[:os]}", + # TODO: format + "Uptime (seconds): #{m[:uptime]}", + "Is under maintenance?: #{m[:is_under_maintenance]}" + ] ++ + product_name_section ++ + product_version_section ++ + [ + "RabbitMQ version: #{m[:rabbitmq_version]}", + "Node name: #{node_name}", + "Erlang configuration: #{m[:erlang_version]}", + "Erlang processes: #{m[:processes][:used]} used, #{m[:processes][:limit]} limit", + "Scheduler run queue: #{m[:run_queue]}", + "Cluster heartbeat timeout (net_ticktime): #{m[:net_ticktime]}" + ] + + plugin_section = [ + "\n#{bright("Plugins")}\n", + "Enabled plugin file: #{m[:enabled_plugin_file]}", + "Enabled plugins:\n" + ] ++ Enum.map(m[:active_plugins], fn pl -> " * #{pl}" end) + + data_directory_section = [ + "\n#{bright("Data directory")}\n", + "Node data directory: #{m[:data_directory]}", + "Raft data directory: #{m[:raft_data_directory]}" + ] + + config_section = [ + "\n#{bright("Config files")}\n" + ] ++ Enum.map(m[:config_files], fn path -> " * #{path}" end) + + log_section = [ + "\n#{bright("Log file(s)")}\n" + ] ++ Enum.map(m[:log_files], fn path -> " * #{path}" end) + + alarms_section = [ + "\n#{bright("Alarms")}\n", + ] ++ case m[:alarms] do + [] -> ["(none)"] + xs -> alarm_lines(xs, node_name) + end + + breakdown = compute_relative_values(m[:memory]) + memory_calculation_strategy = to_atom(m[:vm_memory_calculation_strategy]) + total_memory = get_in(m[:memory], [:total, memory_calculation_strategy]) + + readable_watermark_setting = case m[:vm_memory_high_watermark_setting] do + %{:relative => val} -> "#{val} of available memory" + # absolute value + %{:absolute => val} -> "#{IU.convert(val, unit)} #{unit}" + end + memory_section = [ + "\n#{bright("Memory")}\n", + "Total memory used: #{IU.convert(total_memory, unit)} #{unit}", + "Calculation strategy: #{memory_calculation_strategy}", + "Memory high watermark setting: #{readable_watermark_setting}, computed to: #{IU.convert(m[:vm_memory_high_watermark_limit], unit)} #{unit}\n" + ] ++ Enum.map(breakdown, fn({category, val}) -> "#{category}: #{IU.convert(val[:bytes], unit)} #{unit} (#{val[:percentage]} %)" end) + + file_descriptors = [ + "\n#{bright("File Descriptors")}\n", + "Total: #{m[:file_descriptors][:total_used]}, limit: #{m[:file_descriptors][:total_limit]}", + "Sockets: #{m[:file_descriptors][:sockets_used]}, limit: #{m[:file_descriptors][:sockets_limit]}" + ] + + disk_space_section = [ + "\n#{bright("Free Disk Space")}\n", + "Low free disk space watermark: #{IU.convert(m[:disk_free_limit], unit)} #{unit}", + "Free disk space: #{IU.convert(m[:disk_free], unit)} #{unit}" + ] + + totals_section = [ + "\n#{bright("Totals")}\n", + "Connection count: #{m[:totals][:connection_count]}", + "Queue count: #{m[:totals][:queue_count]}", + "Virtual host count: #{m[:totals][:virtual_host_count]}" + ] + + listeners_section = [ + "\n#{bright("Listeners")}\n", + ] ++ case m[:listeners] do + [] -> ["(none)"] + xs -> listener_lines(xs) + end + lines = runtime_section ++ plugin_section ++ data_directory_section ++ + config_section ++ log_section ++ alarms_section ++ memory_section ++ + file_descriptors ++ disk_space_section ++ totals_section ++ listeners_section + + {:ok, Enum.join(lines, line_separator())} + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.String + + def usage, do: "status [--unit <unit>]" + + def usage_additional() do + [ + ["--unit <bytes | mb | gb>", "byte multiple (bytes, megabytes, gigabytes) to use"], + ["--formatter <json | erlang>", "alternative formatter (JSON, Erlang terms)"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.monitoring() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Displays status of a node" + + def banner(_, %{node: node_name}), do: "Status of node #{node_name} ..." + + # + # Implementation + # + + defp result_map(result) do + %{ + os: os_name(Keyword.get(result, :os)), + pid: Keyword.get(result, :pid), + product_name: Keyword.get(result, :product_name) |> to_string, + product_version: Keyword.get(result, :product_version) |> to_string, + rabbitmq_version: Keyword.get(result, :rabbitmq_version) |> to_string, + erlang_version: Keyword.get(result, :erlang_version) |> to_string |> String.trim_trailing, + uptime: Keyword.get(result, :uptime), + is_under_maintenance: Keyword.get(result, :is_under_maintenance, false), + processes: Enum.into(Keyword.get(result, :processes), %{}), + run_queue: Keyword.get(result, :run_queue), + net_ticktime: net_ticktime(result), + + vm_memory_calculation_strategy: Keyword.get(result, :vm_memory_calculation_strategy), + vm_memory_high_watermark_setting: Keyword.get(result, :vm_memory_high_watermark) |> formatted_watermark, + vm_memory_high_watermark_limit: Keyword.get(result, :vm_memory_limit), + + disk_free_limit: Keyword.get(result, :disk_free_limit), + disk_free: Keyword.get(result, :disk_free), + + file_descriptors: Enum.into(Keyword.get(result, :file_descriptors), %{}), + + alarms: Keyword.get(result, :alarms), + listeners: listener_maps(Keyword.get(result, :listeners, [])), + memory: Keyword.get(result, :memory) |> Enum.into(%{}), + + data_directory: Keyword.get(result, :data_directory) |> to_string, + raft_data_directory: Keyword.get(result, :raft_data_directory) |> to_string, + + config_files: Keyword.get(result, :config_files) |> Enum.map(&to_string/1), + log_files: Keyword.get(result, :log_files) |> Enum.map(&to_string/1), + + active_plugins: Keyword.get(result, :active_plugins) |> Enum.map(&to_string/1), + enabled_plugin_file: Keyword.get(result, :enabled_plugin_file) |> to_string, + + totals: Keyword.get(result, :totals) + } + end + + defp net_ticktime(result) do + case Keyword.get(result, :kernel) do + {:net_ticktime, n} -> n + n when is_integer(n) -> n + _ -> :undefined + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_app_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_app_command.ex new file mode 100644 index 0000000000..c3cbe3b1fd --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_app_command.ex @@ -0,0 +1,26 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.StopAppCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :stop, []) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "stop_app" + + def help_section(), do: :node_management + + def description(), do: "Stops the RabbitMQ application, leaving the runtime (Erlang VM) running" + + def banner(_, %{node: node_name}), do: "Stopping rabbit application on node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_command.ex new file mode 100644 index 0000000000..becb75a0b5 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/stop_command.ex @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.StopCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + alias RabbitMQ.CLI.Core.OsPid + + def merge_defaults(args, opts) do + {args, Map.merge(%{idempotent: false}, opts)} + end + + def switches(), do: [idempotent: :boolean] + + def validate([], _), do: :ok + def validate([_pidfile_path], _), do: :ok + def validate([_ | _] = args, _) when length(args) > 1, do: {:validation_failure, :too_many_args} + + def run([], %{node: node_name, idempotent: true}) do + case :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, []) do + {:badrpc, :nodedown} -> {:ok, "Node #{node_name} is no longer running"} + any -> any + end + end + + def run([], %{node: node_name, idempotent: false}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, []) + end + + def run([pidfile_path], %{node: node_name}) do + ret = OsPid.read_pid_from_file(pidfile_path, true) + :rabbit_misc.rpc_call(node_name, :rabbit, :stop_and_halt, []) + + case ret do + {:error, details} -> + {:error, "could not read pid from file #{pidfile_path}. Error: #{details}"} + + {:error, :could_not_read_pid_from_file, {:contents, s}} -> + {:error, "could not read pid from file #{pidfile_path}. File contents: #{s}"} + + {:error, :could_not_read_pid_from_file, details} -> + {:error, "could not read pid from file #{pidfile_path}. Error: #{details}"} + + pid -> + OsPid.wait_for_os_process_death(pid) + {:ok, "process #{pid} (take from pid file #{pidfile_path}) is no longer running"} + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "stop [--idempotent] [<pidfile>]" + + def usage_additional() do + [ + ["<pidfile>", "node PID file path to monitor. To avoid using a PID file, use 'rabbitmqctl shutdown'"], + ["--idempotent", "return success if target node is not running (cannot be contacted)"] + ] + end + + def description(), do: "Stops RabbitMQ and its runtime (Erlang VM). Requires a local node pid file path to monitor progress." + + def help_section(), do: :node_management + + def banner([pidfile_path], %{node: node_name}) do + "Stopping and halting node #{node_name} (will monitor pid file #{pidfile_path}) ..." + end + + def banner(_, %{node: node_name}), do: "Stopping and halting node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/suspend_listeners_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/suspend_listeners_command.ex new file mode 100644 index 0000000000..31fcf738b9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/suspend_listeners_command.ex @@ -0,0 +1,46 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SuspendListenersCommand do + @moduledoc """ + Suspends all client connection listeners. Suspended listeners will not + accept any new connections but already established ones will not be interrupted. + `ResumeListenersCommand` will undo the effect of this command. + + This command is meant to be used when automating upgrades. + See also `ResumeListenersCommand`. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.DocGuide + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_maintenance, :suspend_all_client_listeners, [], timeout) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "suspend_listeners" + + def usage_doc_guides() do + [ + DocGuide.upgrade() + ] + end + + def help_section(), do: :operations + + def description(), do: "Suspends client connection listeners so that no new client connections are accepted" + + def banner(_, %{node: node_name}) do + "Will suspend client connection listeners on node #{node_name}. " + <> "The node will no longer accept client connections!" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/sync_queue_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/sync_queue_command.ex new file mode 100644 index 0000000000..4b7112af57 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/sync_queue_command.ex @@ -0,0 +1,54 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.SyncQueueCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([queue], %{vhost: vhost, node: node_name}) do + :rpc.call( + node_name, + :rabbit_mirror_queue_misc, + :sync_queue, + [:rabbit_misc.r(vhost, :queue, queue)], + :infinity + ) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage do + "sync_queue [--vhost <vhost>] <queue>" + end + + def usage_additional() do + [ + ["<queue>", "Name of the queue to synchronise"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.mirroring() + ] + end + + def help_section(), do: :replication + + def description(), do: "Instructs a mirrored queue with unsynchronised mirrors (follower replicas) to synchronise them" + + def banner([queue], %{vhost: vhost, node: _node}) do + "Synchronising queue '#{queue}' in vhost '#{vhost}' ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/trace_off_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/trace_off_command.ex new file mode 100644 index 0000000000..f2b6cc217f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/trace_off_command.ex @@ -0,0 +1,42 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.TraceOffCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(_, opts) do + {[], Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, vhost: vhost}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_trace, :stop, [vhost]) do + :ok -> {:ok, "Trace disabled for vhost #{vhost}"} + other -> other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage do + "trace_off [--vhost <vhost>]" + end + + def usage_doc_guides() do + [ + DocGuide.firehose(), + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def banner(_, %{vhost: vhost}), do: "Stopping tracing for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/trace_on_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/trace_on_command.ex new file mode 100644 index 0000000000..33bb5a06d6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/trace_on_command.ex @@ -0,0 +1,42 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.TraceOnCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(_, opts) do + {[], Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, vhost: vhost}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_trace, :start, [vhost]) do + :ok -> {:ok, "Trace enabled for vhost #{vhost}"} + other -> other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage do + "trace_on [--vhost <vhost>]" + end + + def usage_doc_guides() do + [ + DocGuide.firehose(), + DocGuide.virtual_hosts() + ] + end + + def help_section(), do: :virtual_hosts + + def banner(_, %{vhost: vhost}), do: "Starting tracing for vhost \"#{vhost}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/update_cluster_nodes_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/update_cluster_nodes_command.ex new file mode 100644 index 0000000000..94b218e2c9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/update_cluster_nodes_command.ex @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.UpdateClusterNodesCommand do + alias RabbitMQ.CLI.Core.{Config, DocGuide, Helpers} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppStopped + + def run([seed_node], options=%{node: node_name}) do + long_or_short_names = Config.get_option(:longnames, options) + seed_node_normalised = Helpers.normalise_node(seed_node, long_or_short_names) + :rabbit_misc.rpc_call( + node_name, + :rabbit_mnesia, + :update_cluster_nodes, + [seed_node_normalised] + ) + end + + def usage() do + "update_cluster_nodes <seed_node>" + end + + def usage_additional() do + [ + ["<seed_node>", "Cluster node to seed known cluster members from"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.clustering() + ] + end + + def help_section(), do: :cluster_management + + def description(), do: "Instructs a cluster member node to sync the list of known cluster members from <seed_node>" + + def banner([seed_node], %{node: node_name}) do + "Will seed #{node_name} from #{seed_node} on next start" + end + + def output({:error, :mnesia_unexpectedly_running}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + RabbitMQ.CLI.DefaultOutput.mnesia_running_error(node_name)} + end + + def output({:error, :cannot_cluster_node_with_itself}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), + "Error: cannot cluster node with itself: #{node_name}"} + end + + use RabbitMQ.CLI.DefaultOutput +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex new file mode 100644 index 0000000000..8028054932 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.VersionCommand do + alias RabbitMQ.CLI.Core.{Validators, Version} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics, :plugins] + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def validate_execution_environment([] = args, opts) do + Validators.rabbit_is_loaded(args, opts) + end + + def run([], %{formatter: "json"}) do + {:ok, %{version: Version.local_version()}} + end + def run([], %{formatter: "csv"}) do + row = [version: Version.local_version()] + {:ok, [row]} + end + def run([], _opts) do + {:ok, Version.local_version()} + end + use RabbitMQ.CLI.DefaultOutput + + def help_section, do: :help + + def description, do: "Displays CLI tools version" + + def usage, do: "version" + + def banner(_, _), do: nil +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/wait_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/wait_command.ex new file mode 100644 index 0000000000..0699203de6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/wait_command.ex @@ -0,0 +1,269 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.WaitCommand do + alias RabbitMQ.CLI.Core.{Helpers, Validators} + + @behaviour RabbitMQ.CLI.CommandBehaviour + @default_timeout 10_000 + + def scopes(), do: [:ctl, :diagnostics] + + def switches(), do: [pid: :integer, timeout: :integer] + def aliases(), do: [P: :pid, t: :timeout] + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + val -> val + end + + {args, Map.put(opts, :timeout, timeout)} + end + + def validate([_ | _] = args, _) when length(args) > 1, do: {:validation_failure, :too_many_args} + def validate([_], %{pid: _}), do: {:validation_failure, "Cannot specify both pid and pidfile"} + def validate([_], _), do: :ok + def validate([], %{pid: _}), do: :ok + def validate([], _), do: {:validation_failure, "No pid or pidfile specified"} + + def validate_execution_environment([], %{pid: _} = opts) do + Validators.rabbit_is_loaded([], opts) + end + def validate_execution_environment([_pid_file], opts) do + Validators.rabbit_is_loaded([], opts) + end + + def run([pid_file], %{node: node_name, timeout: timeout} = opts) do + app_names = :rabbit_and_plugins + quiet = opts[:quiet] || false + + Helpers.stream_until_error_parameterised( + [ + log("Waiting for pid file '#{pid_file}' to appear", quiet), + fn _ -> wait_for_pid_file(pid_file, node_name, timeout) end, + log_param(fn pid -> "pid is #{pid}" end, quiet) + ] ++ + wait_for_pid_funs(node_name, app_names, timeout, quiet), + :init + ) + end + + def run([], %{node: node_name, pid: pid, timeout: timeout} = opts) do + app_names = :rabbit_and_plugins + quiet = opts[:quiet] || false + + Helpers.stream_until_error_parameterised( + wait_for_pid_funs(node_name, app_names, timeout, quiet), + pid + ) + end + + def output({:error, err}, opts) do + case format_error(err) do + :undefined -> RabbitMQ.CLI.DefaultOutput.output({:error, err}, opts) + error_str -> {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), error_str} + end + end + + def output({:stream, stream}, _opts) do + {:stream, + Stream.map(stream, fn + {:error, err} -> + {:error, + case format_error(err) do + :undefined -> err + error_str -> error_str + end} + + other -> + other + end)} + end + + use RabbitMQ.CLI.DefaultOutput + + # Banner is printed in wait steps + def banner(_, _), do: nil + + def usage, do: "wait [<pidfile>] [--pid|-P <pid>]" + + def usage_additional() do + [ + ["<pidfile>", "PID file path"], + ["--pid <pid>", "operating system PID to monitor"] + ] + end + + def help_section(), do: :node_management + + def description(), do: "Waits for RabbitMQ node startup by monitoring a local PID file. See also 'rabbitmqctl await_online_nodes'" + + # + # Implementation + # + + def wait_for(timeout, fun) do + sleep = 1000 + + case wait_for_loop(timeout, sleep, fun) do + {:error, :timeout} -> {:error, {:timeout, timeout}} + other -> other + end + end + + def wait_for_loop(timeout, _, _) when timeout <= 0 do + {:error, :timeout} + end + + def wait_for_loop(timeout, sleep, fun) do + time = :erlang.system_time(:milli_seconds) + + case fun.() do + {:error, :loop} -> + time_to_fun = :erlang.system_time(:milli_seconds) - time + + time_taken = + case {time_to_fun > timeout, time_to_fun > sleep} do + ## The function took longer than timeout + {true, _} -> + time_to_fun + + ## The function took longer than sleep + {false, true} -> + time_to_fun + + ## We need to sleep + {false, false} -> + :timer.sleep(sleep) + time_to_fun + sleep + end + + wait_for_loop(timeout - time_taken, sleep, fun) + + other -> + other + end + end + + defp wait_for_pid_funs(node_name, app_names, timeout, quiet) do + app_names_formatted = :io_lib.format('~p', [app_names]) + + [ + log_param( + fn pid -> + "Waiting for erlang distribution on node '#{node_name}' while OS process '#{pid}' is running" + end, + quiet + ), + fn pid -> wait_for_erlang_distribution(pid, node_name, timeout) end, + log( + "Waiting for applications '#{app_names_formatted}' to start on node '#{node_name}'", + quiet + ), + fn _ -> wait_for_application(node_name, app_names) end, + log("Applications '#{app_names_formatted}' are running on node '#{node_name}'", quiet) + ] + end + + defp log(_string, _quiet = true) do + fn val -> {:ok, val} end + end + + defp log(string, _quiet = false) do + fn val -> {:ok, val, string} end + end + + defp log_param(_fun, _quiet = true) do + fn val -> {:ok, val} end + end + + defp log_param(fun, _quiet = false) do + fn val -> {:ok, val, fun.(val)} end + end + + defp format_error(:process_not_running) do + "Error: process is not running." + end + + defp format_error({:garbage_in_pid_file, _}) do + "Error: garbage in pid file." + end + + defp format_error({:could_not_read_pid, err}) do + "Error: could not read pid. Detail: #{err}" + end + + defp format_error(_) do + :undefined + end + + defp wait_for_application(node_name, :rabbit_and_plugins) do + case :rabbit.await_startup(node_name) do + {:badrpc, err} -> {:error, {:badrpc, err}} + other -> other + end + end + + defp wait_for_erlang_distribution(pid, node_name, timeout) do + wait_for( + timeout, + fn -> + case check_distribution(pid, node_name) do + # Loop while node is available. + {:error, :pang} -> {:error, :loop} + other -> other + end + end + ) + end + + defp check_distribution(pid, node_name) do + case is_os_process_alive(pid) do + true -> + case Node.ping(node_name) do + :pong -> :ok + :pang -> {:error, :pang} + end + + false -> + {:error, :process_not_running} + end + end + + defp is_os_process_alive(pid) do + :rabbit_misc.is_os_process_alive(to_charlist(pid)) + end + + defp wait_for_pid_file(pid_file, node_name, timeout) do + wait_for( + timeout, + fn -> + case :file.read_file(pid_file) do + {:ok, bin} -> + case Integer.parse(bin) do + :error -> + {:error, {:garbage_in_pid_file, pid_file}} + + {pid, _} -> + case check_distribution(pid, node_name) do + :ok -> {:ok, pid} + _ -> {:error, :loop} + end + end + + {:error, :enoent} -> + {:error, :loop} + + {:error, err} -> + {:error, {:could_not_read_pid, err}} + end + end + ) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/info_keys.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/info_keys.ex new file mode 100644 index 0000000000..26f86ae51e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/info_keys.ex @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.InfoKeys do + import RabbitCommon.Records + alias RabbitMQ.CLI.Core.DataCoercion + + def validate_info_keys(args, valid_keys) do + info_keys = prepare_info_keys(args) + + case invalid_info_keys(info_keys, Enum.map(valid_keys, &DataCoercion.to_atom/1)) do + [_ | _] = bad_info_keys -> + {:validation_failure, {:bad_info_key, bad_info_keys}} + + [] -> + {:ok, info_keys} + end + end + + def prepare_info_keys(args) do + args + |> Enum.flat_map(fn arg -> String.split(arg, ",", trim: true) end) + |> Enum.map(fn s -> String.replace(s, ",", "") end) + |> Enum.map(&String.trim/1) + |> Enum.map(&String.to_atom/1) + |> Enum.uniq() + end + + def with_valid_info_keys(args, valid_keys, fun) do + case validate_info_keys(args, valid_keys) do + {:ok, info_keys} -> fun.(info_keys) + err -> err + end + end + + defp invalid_info_keys(info_keys, valid_keys) do + MapSet.new(info_keys) + |> MapSet.difference(MapSet.new(valid_keys)) + |> MapSet.to_list() + end + + def info_for_keys(item, []) do + item + end + + def info_for_keys([{_, _} | _] = item, info_keys) do + item + |> Enum.filter(fn {k, _} -> Enum.member?(info_keys, k) end) + |> Enum.map(fn {k, v} -> {k, format_info_item(v)} end) + end + + defp format_info_item(resource(name: name)) do + name + end + + defp format_info_item(any) do + any + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/rpc_stream.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/rpc_stream.ex new file mode 100644 index 0000000000..4b672a6d88 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/rpc_stream.ex @@ -0,0 +1,124 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.RpcStream do + alias RabbitMQ.CLI.Ctl.InfoKeys + + def receive_list_items(node, mod, fun, args, timeout, info_keys) do + receive_list_items(node, [{mod, fun, args}], timeout, info_keys, 1) + end + + def receive_list_items(node, mod, fun, args, timeout, info_keys, chunks) do + receive_list_items(node, [{mod, fun, args}], timeout, info_keys, chunks) + end + + def receive_list_items(_node, _mfas, _timeout, _info_keys, 0) do + nil + end + + def receive_list_items(node, mfas, timeout, info_keys, chunks_init) do + receive_list_items_with_fun(node, mfas, timeout, info_keys, chunks_init, fn v -> v end) + end + + def receive_list_items_with_fun(node, mfas, timeout, info_keys, chunks_init, response_fun) do + pid = Kernel.self() + ref = Kernel.make_ref() + for {m, f, a} <- mfas, do: init_items_stream(node, m, f, a, timeout, pid, ref) + + Stream.unfold( + {chunks_init, :continue}, + fn + :finished -> + response_fun.(nil) + + {chunks, :continue} -> + received = + receive do + {^ref, :finished} when chunks === 1 -> + nil + + {^ref, :finished} -> + {[], {chunks - 1, :continue}} + + {^ref, {:timeout, t}} -> + {{:error, {:badrpc, {:timeout, t / 1000}}}, :finished} + + {^ref, []} -> + {[], {chunks, :continue}} + + {^ref, :error, {:badrpc, :timeout}} -> + {{:error, {:badrpc, {:timeout, timeout / 1000}}}, :finished} + + {^ref, result, :continue} -> + {result, {chunks, :continue}} + + {:error, _} = error -> + {error, :finished} + + {^ref, :error, error} -> + {{:error, simplify_emission_error(error)}, :finished} + + {:DOWN, _mref, :process, _pid, :normal} -> + {[], {chunks, :continue}} + + {:DOWN, _mref, :process, _pid, reason} -> + {{:error, simplify_emission_error(reason)}, :finished} + end + + response_fun.(received) + end + ) + |> display_list_items(info_keys) + end + + def simplify_emission_error({:badrpc, {:EXIT, {{:nocatch, error}, error_details}}}) do + {error, error_details} + end + + def simplify_emission_error({{:nocatch, error}, error_details}) do + {error, error_details} + end + + def simplify_emission_error(other) do + other + end + + defp display_list_items(items, info_keys) do + items + |> Stream.filter(fn + [] -> false + _ -> true + end) + |> Stream.map(fn + {:error, error} -> + error + + # here item is a list of keyword lists: + [[{_, _} | _] | _] = item -> + Enum.map(item, fn i -> InfoKeys.info_for_keys(i, info_keys) end) + + item -> + InfoKeys.info_for_keys(item, info_keys) + end) + end + + defp init_items_stream(_node, _mod, _fun, _args, 0, pid, ref) do + set_stream_timeout(pid, ref, 0) + end + + defp init_items_stream(node, mod, fun, args, timeout, pid, ref) do + :rabbit_control_misc.spawn_emitter_caller(node, mod, fun, args, ref, pid, timeout) + set_stream_timeout(pid, ref, timeout) + end + + defp set_stream_timeout(_, _, :infinity) do + :ok + end + + defp set_stream_timeout(pid, ref, timeout) do + Process.send_after(pid, {ref, {:timeout, timeout}}, timeout) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/default_output.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/default_output.ex new file mode 100644 index 0000000000..d5e3f94a15 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/default_output.ex @@ -0,0 +1,94 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. +alias RabbitMQ.CLI.Formatters.FormatterHelpers + +defmodule RabbitMQ.CLI.DefaultOutput do + # When `use RabbitMQ.CLI.DefaultOutput` is invoked, + # this will define output/2 that delegates to RabbitMQ.CLI.DefaultOutput.output/2. + defmacro __using__(_) do + quote do + def output(result, opts) do + RabbitMQ.CLI.DefaultOutput.output(result, opts) + end + end + end + + def output(result, opts \\ %{}) do + format_output(normalize_output(result, opts)) + end + + def mnesia_running_error(node_name) do + "Mnesia is still running on node #{node_name}.\n" <> + "Please stop RabbitMQ with 'rabbitmqctl stop_app' first." + end + + defp normalize_output(:ok, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name}} + end + defp normalize_output(:ok, _opts), do: :ok + defp normalize_output({:ok, value}, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "value" => value}} + end + defp normalize_output({:ok, _} = input, _opts), do: input + defp normalize_output({:stream, _} = input, _opts), do: input + defp normalize_output({:badrpc_multi, _, _} = input, _opts), do: {:error, input} + defp normalize_output({:badrpc, :nodedown} = input, _opts), do: {:error, input} + defp normalize_output({:badrpc, :timeout} = input, _opts), do: {:error, input} + defp normalize_output({:badrpc, {:timeout, _n}} = input, _opts), do: {:error, input} + defp normalize_output({:badrpc, {:timeout, _n, _msg}} = input, _opts), do: {:error, input} + defp normalize_output({:badrpc, {:EXIT, reason}}, _opts), do: {:error, reason} + defp normalize_output({:error, exit_code, string}, _opts) when is_integer(exit_code) do + {:error, exit_code, to_string(string)} + end + defp normalize_output({:error, format, args}, _opts) + when (is_list(format) or is_binary(format)) and is_list(args) do + {:error, to_string(:rabbit_misc.format(format, args))} + end + defp normalize_output({:error, _} = input, _opts), do: input + defp normalize_output({:error_string, string}, _opts) do + {:error, to_string(string)} + end + defp normalize_output(unknown, _opts) when is_atom(unknown), do: {:error, unknown} + defp normalize_output({unknown, _} = input, _opts) when is_atom(unknown), do: {:error, input} + defp normalize_output(result, _opts) when not is_atom(result), do: {:ok, result} + + + defp format_output({:error, _} = result) do + result + end + defp format_output({:error, _, _} = result) do + result + end + + defp format_output(:ok) do + :ok + end + + defp format_output({:ok, output}) do + case Enumerable.impl_for(output) do + nil -> + {:ok, output} + + ## Do not streamify plain maps + Enumerable.Map -> + {:ok, output} + + ## Do not streamify proplists + Enumerable.List -> + case FormatterHelpers.proplist?(output) do + true -> {:ok, output} + false -> {:stream, output} + end + + _ -> + {:stream, output} + end + end + + defp format_output({:stream, stream}) do + {:stream, stream} + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/alarms_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/alarms_command.ex new file mode 100644 index 0000000000..7669a523eb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/alarms_command.ex @@ -0,0 +1,77 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.AlarmsCommand do + @moduledoc """ + Displays all alarms reported by the target node. + + Returns a code of 0 unless there were connectivity and authentication + errors. This command is not meant to be used in health checks. + """ + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + import RabbitMQ.CLI.Core.Alarms + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + # Example response when there are alarms: + # + # [ + # file_descriptor_limit, + # {{resource_limit,disk,hare@warp10},[]}, + # {{resource_limit,memory,hare@warp10},[]}, + # {{resource_limit,disk,rabbit@warp10},[]}, + # {{resource_limit,memory,rabbit@warp10},[]} + # ] + # + # The topmost file_descriptor_limit alarm is node-local. + :rabbit_misc.rpc_call(node_name, :rabbit_alarm, :get_alarms, [], timeout) + end + + def output([], %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "alarms" => []}} + end + + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported no alarms, local or clusterwide"} + end + + def output(alarms, %{node: node_name, formatter: "json"}) do + local = local_alarms(alarms, node_name) + global = clusterwide_alarms(alarms, node_name) + + {:ok, + %{ + "result" => "ok", + "local" => alarm_lines(local, node_name), + "global" => alarm_lines(global, node_name), + "message" => "Node #{node_name} reported alarms" + }} + end + + def output(alarms, %{node: node_name}) do + lines = alarm_lines(alarms, node_name) + + {:ok, Enum.join(lines, line_separator())} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists resource alarms (local or cluster-wide) in effect on the target node" + + def usage, do: "alarms" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} to report any known resource alarms ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/certificates_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/certificates_command.ex new file mode 100644 index 0000000000..33320d8e37 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/certificates_command.ex @@ -0,0 +1,55 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CertificatesCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + import RabbitMQ.CLI.Core.Listeners + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_networking, :active_listeners, [], timeout) do + {:error, _} = err -> + err + + {:error, _, _} = err -> + err + + xs when is_list(xs) -> + listeners = listeners_with_certificates(listeners_on(xs, node_name)) + + case listeners do + [] -> %{} + _ -> Enum.map(listeners, &listener_certs/1) + end + + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "certificates" + + def usage_doc_guides() do + [ + DocGuide.configuration(), + DocGuide.tls() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Displays certificates (public keys) for every listener on target node that is configured to use TLS" + + def banner(_, %{node: node_name}), do: "Certificates of node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_alarms_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_alarms_command.ex new file mode 100644 index 0000000000..04bb70317a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_alarms_command.ex @@ -0,0 +1,86 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckAlarmsCommand do + @moduledoc """ + Exits with a non-zero code if the target node reports any alarms, + local or clusterwide. + + This command is meant to be used in health checks. + """ + + import RabbitMQ.CLI.Core.Alarms + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + # Example response when there are alarms: + # + # [ + # file_descriptor_limit, + # {{resource_limit,disk,hare@warp10},[]}, + # {{resource_limit,memory,hare@warp10},[]}, + # {{resource_limit,disk,rabbit@warp10},[]}, + # {{resource_limit,memory,rabbit@warp10},[]} + # ] + # + # The topmost file_descriptor_limit alarm is node-local. + :rabbit_misc.rpc_call(node_name, :rabbit_alarm, :get_alarms, [], timeout) + end + + def output([], %{formatter: "json"}) do + {:ok, %{"result" => "ok"}} + end + + def output([], %{silent: true}) do + {:ok, :check_passed} + end + + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported no alarms, local or clusterwide"} + end + + def output(alarms, %{node: node_name, formatter: "json"}) do + local = local_alarms(alarms, node_name) + global = clusterwide_alarms(alarms, node_name) + + {:error, :check_failed, + %{ + "result" => "error", + "local" => alarm_lines(local, node_name), + "global" => alarm_lines(global, node_name), + "message" => "Node #{node_name} reported alarms" + }} + end + + def output(alarms, %{silent: true} = _opts) when is_list(alarms) do + {:error, :check_failed} + end + + def output(alarms, %{node: node_name}) when is_list(alarms) do + lines = alarm_lines(alarms, node_name) + + {:error, :check_failed, Enum.join(lines, line_separator())} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Health check that exits with a non-zero code if the target node reports any alarms, local or cluster-wide." + + def usage, do: "check_alarms" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} to report any local resource alarms ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_certificate_expiration_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_certificate_expiration_command.ex new file mode 100644 index 0000000000..d14ade59f6 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_certificate_expiration_command.ex @@ -0,0 +1,101 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckCertificateExpirationCommand do + alias RabbitMQ.CLI.Core.DocGuide + alias RabbitMQ.CLI.TimeUnit, as: TU + @behaviour RabbitMQ.CLI.CommandBehaviour + + import RabbitMQ.CLI.Core.Listeners + + def switches(), do: [unit: :string, within: :integer] + + def merge_defaults(args, opts) do + {args, Map.merge(%{unit: "weeks", within: 4}, opts)} + end + + def validate(args, _) when length(args) > 0 do + {:validation_failure, :too_many_args} + end + def validate(_, %{unit: unit}) do + case TU.known_unit?(unit) do + true -> + :ok + + false -> + {:validation_failure, "unit '#{unit}' is not supported. Please use one of: days, weeks, months, years"} + end + end + def validate(_, _), do: :ok + + def run([], %{node: node_name, unit: unit, within: within, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_networking, :active_listeners, [], timeout) do + {:error, _} = err -> + err + + {:error, _, _} = err -> + err + + {:badrpc, _} = err -> + err + + xs when is_list(xs) -> + listeners = listeners_on(xs, node_name) + seconds = TU.convert(within, unit) + Enum.reduce(listeners, [], fn (listener, acc) -> case listener_expiring_within(listener, seconds) do + false -> acc + expiring -> [expiring | acc] + end + end) + end + end + + def output([], %{formatter: "json"}) do + {:ok, %{"result" => "ok"}} + end + + def output([], %{unit: unit, within: within}) do + unit_label = unit_label(within, unit) + {:ok, "No certificates are expiring within #{within} #{unit_label}."} + end + + def output(listeners, %{formatter: "json"}) do + {:error, :check_failed, %{"result" => "error", "expired" => Enum.map(listeners, &expired_listener_map/1)}} + end + + def output(listeners, %{}) do + {:error, :check_failed, Enum.map(listeners, &expired_listener_map/1)} + end + + def unit_label(1, unit) do + unit |> String.slice(0..-2) + end + def unit_label(_within, unit) do + unit + end + + def usage, do: "check_certificate_expiration [--within <period>] [--unit <unit>]" + + def usage_additional() do + [ + ["<period>", "period of time to check. Default is four (weeks)."], + ["<unit>", "time unit for the period, can be days, weeks, months, years. Default is weeks."], + ] + end + + def usage_doc_guides() do + [ + DocGuide.tls(), + DocGuide.networking() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Checks the expiration date on the certificates for every listener configured to use TLS" + + def banner(_, %{node: node_name}), do: "Checking certificate expiration on node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_local_alarms_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_local_alarms_command.ex new file mode 100644 index 0000000000..1b11537793 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_local_alarms_command.ex @@ -0,0 +1,85 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckLocalAlarmsCommand do + @moduledoc """ + Exits with a non-zero code if the target node reports any local alarms. + + This command is meant to be used in health checks. + """ + + import RabbitMQ.CLI.Core.Alarms + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + # Example response when there are alarms: + # + # [ + # file_descriptor_limit, + # {{resource_limit,disk,hare@warp10},[]}, + # {{resource_limit,memory,hare@warp10},[]}, + # {{resource_limit,disk,rabbit@warp10},[]}, + # {{resource_limit,memory,rabbit@warp10},[]} + # ] + # + # The topmost file_descriptor_limit alarm is node-local. + case :rabbit_misc.rpc_call(node_name, :rabbit_alarm, :get_alarms, [], timeout) do + [] -> [] + xs when is_list(xs) -> local_alarms(xs, node_name) + other -> other + end + end + + def output([], %{formatter: "json"}) do + {:ok, %{"result" => "ok"}} + end + + def output([], %{silent: true}) do + {:ok, :check_passed} + end + + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported no local alarms"} + end + + def output(alarms, %{node: node_name, formatter: "json"}) do + {:error, :check_failed, + %{ + "result" => "error", + "local" => alarm_lines(alarms, node_name), + "message" => "Node #{node_name} reported local alarms" + }} + end + + def output(_alarms, %{silent: true}) do + {:error, :check_failed} + end + + def output(alarms, %{node: node_name}) do + lines = alarm_lines(alarms, node_name) + + {:error, Enum.join(lines, line_separator())} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Health check that exits with a non-zero code if the target node reports any local alarms" + + def usage, do: "check_local_alarms" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} to report any local resource alarms ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_port_connectivity_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_port_connectivity_command.ex new file mode 100644 index 0000000000..1c3d86ed83 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_port_connectivity_command.ex @@ -0,0 +1,119 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckPortConnectivityCommand do + @moduledoc """ + Checks all listeners on the target node by opening a TCP connection to each + and immediately closing it. + + Returns a code of 0 unless there were connectivity and authentication + errors. This command is meant to be used in health checks. + """ + + import RabbitMQ.CLI.Diagnostics.Helpers, + only: [check_listener_connectivity: 3] + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + import RabbitMQ.CLI.Core.Listeners + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 30_000 + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + + {args, Map.merge(opts, %{timeout: timeout})} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_networking, :active_listeners, [], timeout) do + {:error, _} = err -> + err + + {:error, _, _} = err -> + err + + xs when is_list(xs) -> + locals = listeners_on(xs, node_name) + + case locals do + [] -> {true, locals} + _ -> check_connectivity_of(locals, node_name, timeout) + end + + other -> + other + end + end + + def output({true, listeners}, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "listeners" => listener_maps(listeners)}} + end + + def output({true, listeners}, %{node: node_name}) do + ports = + listeners + |> listener_maps + |> Enum.map(fn %{port: p} -> p end) + |> Enum.sort() + |> Enum.join(", ") + + {:ok, "Successfully connected to ports #{ports} on node #{node_name}."} + end + + def output({false, failures}, %{formatter: "json", node: node_name}) do + {:error, %{"result" => "error", "node" => node_name, "failures" => listener_maps(failures)}} + end + + def output({false, failures}, %{node: node_name}) do + lines = [ + "Connection to ports of the following listeners on node #{node_name} failed: " + | listener_lines(failures) + ] + + {:error, Enum.join(lines, line_separator())} + end + + def description(), do: "Basic TCP connectivity health check for each listener's port on the target node" + + def help_section(), do: :observability_and_health_checks + + def usage, do: "check_port_connectivity" + + def banner([], %{node: node_name}) do + "Testing TCP connections to all active listeners on node #{node_name} ..." + end + + # + # Implementation + # + + defp check_connectivity_of(listeners, node_name, timeout) do + # per listener timeout + t = Kernel.trunc(timeout / (length(listeners) + 1)) + + failures = + Enum.reject( + listeners, + fn l -> check_listener_connectivity(listener_map(l), node_name, t) end + ) + + case failures do + [] -> {true, listeners} + fs -> {false, fs} + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_port_listener_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_port_listener_command.ex new file mode 100644 index 0000000000..f321d444db --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_port_listener_command.ex @@ -0,0 +1,82 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckPortListenerCommand do + @moduledoc """ + Exits with a non-zero code if there is no active listener + for the given port on the target node. + + This command is meant to be used in health checks. + """ + + import RabbitMQ.CLI.Core.Listeners, only: [listeners_on: 2, listener_maps: 1] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositiveIntegerArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([port], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_networking, :active_listeners, [], timeout) do + {:error, _} = err -> + err + + {:error, _, _} = err -> + err + + xs when is_list(xs) -> + locals = listeners_on(xs, node_name) |> listener_maps + + found = + Enum.any?(locals, fn %{port: p} -> + to_string(port) == to_string(p) + end) + + case found do + true -> {true, port} + false -> {false, port, locals} + end + + other -> + other + end + end + + def output({true, port}, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "port" => port}} + end + + def output({true, port}, %{node: node_name}) do + {:ok, "A listener for port #{port} is running on node #{node_name}."} + end + + def output({false, port, listeners}, %{formatter: "json"}) do + ports = Enum.map(listeners, fn %{port: p} -> p end) + + {:error, :check_failed, + %{"result" => "error", "missing" => port, "ports" => ports, "listeners" => listeners}} + end + + def output({false, port, listeners}, %{node: node_name}) do + ports = Enum.map(listeners, fn %{port: p} -> p end) |> Enum.sort() |> Enum.join(", ") + + {:error, :check_failed, + "No listener for port #{port} is active on node #{node_name}. " <> + "Found listeners that use the following ports: #{ports}"} + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Health check that exits with a non-zero code if target node does not have an active listener for given port" + + def usage, do: "check_port_listener <port>" + + def banner([port], %{node: node_name}) do + "Asking node #{node_name} if there's an active listener on port #{port} ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_protocol_listener_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_protocol_listener_command.ex new file mode 100644 index 0000000000..10c81c971e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_protocol_listener_command.ex @@ -0,0 +1,90 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckProtocolListenerCommand do + @moduledoc """ + Exits with a non-zero code if there is no active listener + for the given protocol on the target node. + + This command is meant to be used in health checks. + """ + + import RabbitMQ.CLI.Core.Listeners, + only: [listeners_on: 2, listener_maps: 1, normalize_protocol: 1] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([proto], %{node: node_name, timeout: timeout}) do + proto = normalize_protocol(proto) + + case :rabbit_misc.rpc_call(node_name, :rabbit_networking, :active_listeners, [], timeout) do + {:error, _} = err -> + err + + {:error, _, _} = err -> + err + + xs when is_list(xs) -> + locals = listeners_on(xs, node_name) |> listener_maps + + found = + Enum.any?(locals, fn %{protocol: p} -> + to_string(proto) == to_string(p) + end) + + case found do + true -> {true, proto} + false -> {false, proto, locals} + end + + other -> + other + end + end + + def output({true, proto}, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "protocol" => proto}} + end + + def output({true, proto}, %{node: node_name}) do + {:ok, "A listener for protocol #{proto} is running on node #{node_name}."} + end + + def output({false, proto, listeners}, %{formatter: "json"}) do + protocols = Enum.map(listeners, fn %{protocol: p} -> p end) + + {:error, + %{ + "result" => "error", + "missing" => proto, + "protocols" => protocols, + "listeners" => listeners + }} + end + + def output({false, proto, listeners}, %{node: node_name}) do + protocols = Enum.map(listeners, fn %{protocol: p} -> p end) |> Enum.sort() |> Enum.join(", ") + + {:error, + "No listener for protocol #{proto} is active on node #{node_name}. " <> + "Found listeners for the following protocols: #{protocols}"} + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Health check that exits with a non-zero code if target node does not have an active listener for given protocol" + + def usage, do: "check_protocol_listener <protocol>" + + def banner([proto], %{node: node_name}) do + "Asking node #{node_name} if there's an active listener for protocol #{proto} ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_running_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_running_command.ex new file mode 100644 index 0000000000..690f17e1e7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_running_command.ex @@ -0,0 +1,46 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckRunningCommand do + @moduledoc """ + Exits with a non-zero code if the RabbitMQ app on the target node is not running. + + This command is meant to be used in health checks. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + # Note: we use is_booted/1 over is_running/1 to avoid + # returning a positive result when the node is still booting + :rabbit_misc.rpc_call(node_name, :rabbit, :is_booted, [node_name], timeout) + end + + def output(true, %{node: node_name} = _options) do + {:ok, "RabbitMQ on node #{node_name} is fully booted and running"} + end + + def output(false, %{node: node_name} = _options) do + {:error, + "RabbitMQ on node #{node_name} is not running or has not fully booted yet (check with is_booting)"} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Health check that exits with a non-zero code if the RabbitMQ app on the target node is not running" + + def usage, do: "check_running" + + def banner([], %{node: node_name}) do + "Checking if RabbitMQ is running on node #{node_name} ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_virtual_hosts_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_virtual_hosts_command.ex new file mode 100644 index 0000000000..b3169b522d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/check_virtual_hosts_command.ex @@ -0,0 +1,71 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckVirtualHostsCommand do + @moduledoc """ + Exits with a non-zero code if the target node reports any vhost down. + + This command is meant to be used in health checks. + """ + + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vhost_sup_sup, :check, [], timeout) + end + + def output([], %{formatter: "json"}) do + {:ok, %{"result" => "ok"}} + end + + def output([], %{silent: true}) do + {:ok, :check_passed} + end + + def output([], %{formatter: "erlang"}) do + {:ok, :check_passed} + end + + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported all vhosts as running"} + end + + def output(vhosts, %{formatter: "erlang"} = _opts) when is_list(vhosts) do + {:error, :check_failed, {:down_vhosts, vhosts}} + end + + def output(vhosts, %{formatter: "json"} = _opts) when is_list(vhosts) do + {:error, :check_failed, %{"result" => "error", "down_vhosts" => vhosts}} + end + + def output(vhosts, %{silent: true} = _opts) when is_list(vhosts) do + {:error, :check_failed} + end + + def output(vhosts, %{node: node_name}) when is_list(vhosts) do + lines = Enum.join(vhosts, line_separator()) + {:error, "Some virtual hosts on node #{node_name} are down:\n#{lines}"} + end + + use RabbitMQ.CLI.DefaultOutput + + def description(), do: "Health check that checks if all vhosts are running in the target node" + + def help_section(), do: :observability_and_health_checks + + def usage, do: "check_virtual_hosts" + + def banner([], %{node: node_name}) do + "Checking if all vhosts are running on node #{node_name} ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/cipher_suites_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/cipher_suites_command.ex new file mode 100644 index 0000000000..86e8eee3a4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/cipher_suites_command.ex @@ -0,0 +1,122 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CipherSuitesCommand do + alias RabbitMQ.CLI.Core.Helpers + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{all: false, format: "openssl"}, Helpers.case_insensitive_format(opts))} + end + + def switches(), do: [timeout: :integer, + format: :string, + all: :boolean] + def aliases(), do: [t: :timeout] + + def validate(_, %{format: format}) + when format != "openssl" and format != "erlang" and format != "map" do + {:validation_failure, {:bad_argument, "Format should be either openssl, erlang or map"}} + end + def validate(args, _) when length(args) > 0 do + {:validation_failure, :too_many_args} + end + + def validate(_, _), do: :ok + + def run([], %{node: node_name, timeout: timeout, format: format} = opts) do + {mod, function} = case format do + "openssl" -> {:rabbit_ssl, :cipher_suites_openssl}; + "erlang" -> {:rabbit_ssl, :cipher_suites_erlang}; + "map" -> {:rabbit_ssl, :cipher_suites} + end + args = case opts do + %{all: true} -> [:all]; + %{} -> [:default] + end + :rabbit_misc.rpc_call(node_name, mod, function, args, timeout) + end + + use RabbitMQ.CLI.DefaultOutput + + def banner([], %{format: "openssl"}), do: "Listing available cipher suites in OpenSSL format" + def banner([], %{format: "erlang"}), do: "Listing available cipher suites in Erlang term format" + def banner([], %{format: "map"}), do: "Listing available cipher suites in map format" + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists cipher suites enabled by default. To list all available cipher suites, add the --all argument." + + def usage, do: "cipher_suites [--format <openssl | erlang | map>] [--all]" + + def usage_additional() do + [ + ["--format", "output format to use: openssl, erlang or map"], + ["--all", "list all available suites"] + ] + end + + defmodule Formatter do + alias RabbitMQ.CLI.Formatters.FormatterHelpers + + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(item, %{format: "erlang"}) do + to_string(:io_lib.format("~p", [item])) + end + + def format_output(item, %{format: "map"}) do + to_string(:io_lib.format("~p", [item])) + end + + def format_output(item, %{format: "openssl"} = opts) do + RabbitMQ.CLI.Formatters.String.format_output(item, opts) + end + + def format_stream(stream, %{format: "erlang"} = opts) do + comma_separated(stream, opts) + end + + def format_stream(stream, %{format: "map"} = opts) do + comma_separated(stream, opts) + end + + def format_stream(stream, %{format: "openssl"} = opts) do + Stream.map( + stream, + FormatterHelpers.without_errors_1(fn el -> + format_output(el, opts) + end) + ) + end + + defp comma_separated(stream, opts) do + elements = + Stream.scan( + stream, + :empty, + FormatterHelpers.without_errors_2(fn element, previous -> + separator = + case previous do + :empty -> "" + _ -> "," + end + + format_element(element, separator, opts) + end) + ) + + Stream.concat([["["], elements, ["]"]]) + end + + defp format_element(val, separator, opts) do + separator <> format_output(val, opts) + end + end + + def formatter(), do: Formatter +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/command_line_arguments_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/command_line_arguments_command.ex new file mode 100644 index 0000000000..adbf14cfc3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/command_line_arguments_command.ex @@ -0,0 +1,41 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.CommandLineArgumentsCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:diagnostics] + + use RabbitMQ.CLI.Core.MergesNoDefaults + + def validate(_, %{formatter: "json"}) do + {:validation_failure, :unsupported_formatter} + end + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :init, :get_arguments, []) + end + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang + + def usage, do: "command_line_arguments" + + def usage_doc_guides() do + [ + DocGuide.configuration(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Displays target node's command-line arguments and flags as reported by the runtime" + + def banner(_, %{node: node_name}), do: "Command line arguments of node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/consume_event_stream_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/consume_event_stream_command.ex new file mode 100644 index 0000000000..e7ad171d11 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/consume_event_stream_command.ex @@ -0,0 +1,71 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ConsumeEventStreamCommand do + @moduledoc """ + Displays standard log file location on the target node + """ + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [duration: :integer, pattern: :string, timeout: :integer] + def aliases(), do: [d: :duration, t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{duration: :infinity, pattern: ".*", quiet: true}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout, duration: duration, pattern: pattern}) do + pid = self() + ref = make_ref() + subscribed = :rabbit_misc.rpc_call( + node_name, + :rabbit_event_consumer, :register, + [pid, ref, duration, pattern], + timeout) + case subscribed do + {:ok, ^ref} -> + Stream.unfold(:confinue, + fn(:finished) -> nil + (:confinue) -> + receive do + {^ref, data, :finished} -> + {data, :finished}; + {^ref, data, :confinue} -> + {data, :confinue} + end + end) + error -> error + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.JsonStream + + def printer(), do: RabbitMQ.CLI.Printers.StdIORaw + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Streams internal events from a running node. Output is jq-compatible." + + def usage, do: "consume_event_stream [--duration|-d <seconds>] [--pattern <pattern>]" + + def usage_additional() do + [ + ["<duration_in_seconds>", "duration in seconds to stream log. Defaults to infinity"], + ["<pattern>", "regular expression to pick events"] + ] + end + + def banner([], %{node: node_name, duration: :infinity}) do + "Streaming logs from node #{node_name} ..." + end + def banner([], %{node: node_name, duration: duration}) do + "Streaming logs from node #{node_name} for #{duration} seconds ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/disable_auth_attempt_source_tracking_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/disable_auth_attempt_source_tracking_command.ex new file mode 100644 index 0000000000..df182a0c97 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/disable_auth_attempt_source_tracking_command.ex @@ -0,0 +1,35 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.DisableAuthAttemptSourceTrackingCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :application, :set_env, + [:rabbit, :track_auth_attempt_source, :false]) + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "disable_track_auth_attempt_source" + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Disables the tracking of peer IP address and username of authentication attempts" + + def banner([], _), do: "Disabling authentication attempt source tracking ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/discover_peers_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/discover_peers_command.ex new file mode 100644 index 0000000000..b23a13e370 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/discover_peers_command.ex @@ -0,0 +1,36 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.DiscoverPeersCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_peer_discovery, :discover_cluster_nodes, [], timeout) + end + + def output({:ok, {[], _}}, _options) do + {:ok, "No peers discovered"} + end + + def output({:ok, {nodes, _}}, _options) do + {:ok, nodes} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Performs peer discovery and lists discovered nodes, if any" + + def usage, do: "discover_peers" + + def banner(_, _), do: "Discovering peers nodes ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/enable_auth_attempt_source_tracking_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/enable_auth_attempt_source_tracking_command.ex new file mode 100644 index 0000000000..832891094b --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/enable_auth_attempt_source_tracking_command.ex @@ -0,0 +1,36 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.EnableAuthAttemptSourceTrackingCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :application, :set_env, + [:rabbit, :track_auth_attempt_source, :true]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "enable_auth_attempt_source_tracking" + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Enables the tracking of peer IP address and username of authentication attempts" + + def banner([], _), do: "Enabling authentication attempt source tracking ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_cookie_hash_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_cookie_hash_command.ex new file mode 100644 index 0000000000..b6e3186c94 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_cookie_hash_command.ex @@ -0,0 +1,35 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ErlangCookieHashCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_data_coercion.to_binary( + :rabbit_misc.rpc_call(node_name, :rabbit_nodes_common, :cookie_hash, [], timeout)) + end + + def output(result, %{formatter: "json"}) do + {:ok, %{"result" => "ok", "value" => result}} + end + def output(result, _options) when is_bitstring(result) do + {:ok, result} + end + + def help_section(), do: :configuration + + def description(), do: "Displays a hash of the Erlang cookie (shared secret) used by the target node" + + def usage, do: "erlang_cookie_hash" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} its Erlang cookie hash..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_cookie_sources_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_cookie_sources_command.ex new file mode 100644 index 0000000000..578ba31c73 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_cookie_sources_command.ex @@ -0,0 +1,116 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ErlangCookieSourcesCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + import RabbitMQ.CLI.Core.ANSI + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def distribution(_), do: :none + + def run([], opts) do + switch_cookie = opts[:erlang_cookie] + home_dir = get_home_dir() + cookie_file_path = Path.join(home_dir, ".erlang.cookie") + cookie_file_stat = case File.stat(Path.join(home_dir, ".erlang.cookie")) do + {:error, :enoent} -> nil + {:ok, value} -> value + end + cookie_file_type = case cookie_file_stat do + nil -> nil + value -> value.type + end + cookie_file_access = case cookie_file_stat do + nil -> nil + value -> value.access + end + cookie_file_size = case cookie_file_stat do + nil -> nil + value -> value.size + end + + %{ + os_env_cookie_set: System.get_env("RABBITMQ_ERLANG_COOKIE") != nil, + os_env_cookie_value_length: String.length(System.get_env("RABBITMQ_ERLANG_COOKIE") || ""), + switch_cookie_set: switch_cookie != nil, + switch_cookie_value_length: String.length(to_string(switch_cookie) || ""), + effective_user: System.get_env("USER"), + home_dir: home_dir, + cookie_file_path: cookie_file_path, + cookie_file_exists: File.exists?(cookie_file_path), + cookie_file_type: cookie_file_type, + cookie_file_access: cookie_file_access, + cookie_file_size: cookie_file_size + } + end + + def banner([], %{}), do: "Listing Erlang cookie sources used by CLI tools..." + + def output(result, %{formatter: "json"}) do + {:ok, result} + end + + def output(result, _opts) do + cookie_file_lines = [ + "#{bright("Cookie File")}\n", + "Effective user: #{result[:effective_user] || "(none)"}", + "Effective home directory: #{result[:home_dir] || "(none)"}", + "Cookie file path: #{result[:cookie_file_path]}", + "Cookie file exists? #{result[:cookie_file_exists]}", + "Cookie file type: #{result[:cookie_file_type] || "(n/a)"}", + "Cookie file access: #{result[:cookie_file_access] || "(n/a)"}", + "Cookie file size: #{result[:cookie_file_size] || "(n/a)"}", + ] + + switch_lines = [ + "\n#{bright("Cookie CLI Switch")}\n", + "--erlang-cookie value set? #{result[:switch_cookie_set]}", + "--erlang-cookie value length: #{result[:switch_cookie_value_length] || 0}" + ] + + os_env_lines = [ + "\n#{bright("Env variable ")} #{bright_red("(Deprecated)")}\n", + "RABBITMQ_ERLANG_COOKIE value set? #{result[:os_env_cookie_set]}", + "RABBITMQ_ERLANG_COOKIE value length: #{result[:os_env_cookie_value_length] || 0}" + ] + + lines = cookie_file_lines ++ switch_lines ++ os_env_lines + + {:ok, lines} + end + + def help_section(), do: :configuration + + def description() do + "Display Erlang cookie source (e.g. $HOME/.erlang.cookie file) information useful for troubleshooting" + end + + def usage, do: "erlang_cookie_sources" + + def formatter(), do: RabbitMQ.CLI.Formatters.StringPerLine + + # + # Implementation + # + + @doc """ + Computes HOME directory path the same way Erlang VM/ei does, + including taking Windows-specific env variables into account. + """ + def get_home_dir() do + homedrive = System.get_env("HOMEDRIVE") + homepath = System.get_env("HOMEPATH") + + case {homedrive != nil, homepath != nil} do + {true, true} -> "#{homedrive}#{homepath}" + _ -> System.get_env("HOME") + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_version_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_version_command.ex new file mode 100644 index 0000000000..053e0d142e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/erlang_version_command.ex @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ErlangVersionCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches() do + [details: :boolean, offline: :boolean, timeout: :integer] + end + def aliases(), do: [t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{details: false, offline: false}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{details: details, offline: true}) do + case details do + true -> + :rabbit_data_coercion.to_binary( + :rabbit_misc.otp_system_version()) + + false -> + :rabbit_data_coercion.to_binary( + :rabbit_misc.platform_and_version()) + end + end + def run([], %{node: node_name, timeout: timeout, details: details}) do + case details do + true -> + :rabbit_data_coercion.to_binary( + :rabbit_misc.rpc_call(node_name, :rabbit_misc, :otp_system_version, [], timeout)) + + false -> + :rabbit_data_coercion.to_binary( + :rabbit_misc.rpc_call(node_name, :rabbit_misc, :platform_and_version, [], timeout)) + end + end + + def output(result, %{formatter: "json"}) do + {:ok, %{"result" => "ok", "value" => result}} + end + def output(result, _opts) when is_bitstring(result) do + {:ok, result} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Displays Erlang/OTP version on the target node" + + def usage, do: "erlang_version" + + def usage_additional() do + [ + ["--details", "when set, display additional Erlang/OTP system information"], + ["--offline", "when set, displays local Erlang/OTP version (that used by CLI tools)"] + ] + end + + def banner([], %{offline: true}) do + "CLI Erlang/OTP version ..." + end + def banner([], %{node: node_name}) do + "Asking node #{node_name} for its Erlang/OTP version..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/is_booting_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/is_booting_command.ex new file mode 100644 index 0000000000..56b2253c90 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/is_booting_command.ex @@ -0,0 +1,53 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.IsBootingCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit, :is_booting, [node_name], timeout) + end + + def output(true, %{node: node_name, formatter: "json"}) do + m = %{ + "result" => true, + "message" => "RabbitMQ on node #{node_name} is booting" + } + {:ok, m} + end + + def output(false, %{node: node_name, formatter: "json"}) do + m = %{ + "result" => false, + "message" => "RabbitMQ on node #{node_name} is fully booted (check with is_running), stopped or has not started booting yet" + } + {:ok, m} + end + def output(true, %{node: node_name}) do + {:ok, "RabbitMQ on node #{node_name} is booting"} + end + + def output(false, %{node: node_name}) do + {:ok, + "RabbitMQ on node #{node_name} is fully booted (check with is_running), stopped or has not started booting yet"} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Checks if RabbitMQ is still booting on the target node" + + def usage, do: "is_booting" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} for its boot status ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/is_running_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/is_running_command.ex new file mode 100644 index 0000000000..ecf5ce9368 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/is_running_command.ex @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.IsRunningCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + # Note: we use is_booted/1 over is_running/1 to avoid + # returning a positive result when the node is still booting + :rabbit_misc.rpc_call(node_name, :rabbit, :is_booted, [node_name], timeout) + end + + def output(true, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => true, "message" => "RabbitMQ on node #{node_name} is fully booted and running"}} + end + def output(false, %{node: node_name, formatter: "json"}) do + {:ok, + %{"result" => false, "message" => "RabbitMQ on node #{node_name} is not running or has not fully booted yet (check with is_booting)"}} + end + def output(true, %{node: node_name}) do + {:ok, "RabbitMQ on node #{node_name} is fully booted and running"} + end + def output(false, %{node: node_name}) do + {:ok, + "RabbitMQ on node #{node_name} is not running or has not fully booted yet (check with is_booting)"} + end + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Checks if RabbitMQ is fully booted and running on the target node" + + def usage, do: "is_running" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} for its status ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/list_network_interfaces_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/list_network_interfaces_command.ex new file mode 100644 index 0000000000..d41409b8c4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/list_network_interfaces_command.ex @@ -0,0 +1,77 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ListNetworkInterfacesCommand do + @moduledoc """ + Displays all network interfaces (NICs) reported by the target node. + """ + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + import RabbitMQ.CLI.Core.ANSI + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [timeout: :integer, offline: :boolean] + def aliases(), do: [t: :timeout] + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{offline: true}) do + :rabbit_net.getifaddrs() + end + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_net, :getifaddrs, [], timeout) + end + + def output(nic_map, %{node: node_name, formatter: "json"}) when map_size(nic_map) == 0 do + {:ok, %{"result" => "ok", "node" => node_name, "interfaces" => %{}}} + end + def output(nic_map, %{node: node_name}) when map_size(nic_map) == 0 do + {:ok, "Node #{node_name} reported no network interfaces"} + end + def output(nic_map0, %{node: node_name, formatter: "json"}) do + nic_map = Enum.map(nic_map0, fn ({k, v}) -> {to_string(k), v} end) + {:ok, + %{ + "result" => "ok", + "interfaces" => Enum.into(nic_map, %{}), + "message" => "Node #{node_name} reported network interfaces" + }} + end + def output(nic_map, _) when is_map(nic_map) do + lines = nic_lines(nic_map) + + {:ok, Enum.join(lines, line_separator())} + end + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists network interfaces (NICs) on the target node" + + def usage, do: "list_network_interfaces" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} to report its network interfaces ..." + end + + # + # Implementation + # + + defp nic_lines(nic_map) do + Enum.reduce(nic_map, [], + fn({iface, props}, acc) -> + iface_lines = Enum.reduce(props, [], + fn({prop, val}, inner_acc) -> + ["#{prop}: #{val}" | inner_acc] + end) + + header = "#{bright("Interface #{iface}")}\n" + acc ++ [header | iface_lines] ++ ["\n"] + end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/list_node_auth_attempt_stats_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/list_node_auth_attempt_stats_command.ex new file mode 100644 index 0000000000..4793cf6c46 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/list_node_auth_attempt_stats_command.ex @@ -0,0 +1,75 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ListNodeAuthAttemptStatsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def switches(), do: [by_source: :boolean] + + def merge_defaults(args, opts) do + {args, Map.merge(%{by_source: false}, opts)} + end + + def validate([], _), do: :ok + def validate(_, _), do: {:validation_failure, :too_many_args} + + def run([], %{node: node_name, timeout: timeout, by_source: by_source}) do + case by_source do + :true -> + :rabbit_misc.rpc_call( + node_name, :rabbit_core_metrics, :get_auth_attempts_by_source, [], timeout) + :false -> + :rabbit_misc.rpc_call( + node_name, :rabbit_core_metrics, :get_auth_attempts, [], timeout) + end + end + + def output([], %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "attempts" => []}} + end + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported no authentication attempt stats"} + end + def output(rows, %{node: node_name, formatter: "json"}) do + maps = Enum.map(rows, &Map.new/1) + {:ok, + %{ + "result" => "ok", + "node" => node_name, + "attempts" => maps + }} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "list_node_auth_attempts [--by-source]" + + def usage_additional do + [ + ["--by-source", "list authentication attempts by remote address and username"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :observability_and_health_checks + def description(), do: "Lists authentication attempts on the target node" + + def banner([], %{node: node_name}), do: "Listing authentication + attempts for node \"#{node_name}\" ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/listeners_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/listeners_command.ex new file mode 100644 index 0000000000..f54ce3775e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/listeners_command.ex @@ -0,0 +1,92 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ListenersCommand do + @moduledoc """ + Displays all listeners on a node. + + Returns a code of 0 unless there were connectivity and authentication + errors. This command is not meant to be used in health checks. + """ + + import RabbitMQ.CLI.Core.Listeners, + only: [listeners_on: 2, listener_lines: 1, listener_maps: 1, listener_rows: 1] + + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + # Example listener list: + # + # [{listener,rabbit@warp10,clustering,"localhost", + # {0,0,0,0,0,0,0,0}, + # 25672,[]}, + # {listener,rabbit@warp10,amqp,"localhost", + # {0,0,0,0,0,0,0,0}, + # 5672, + # [{backlog,128}, + # {nodelay,true}, + # {linger,{true,0}}, + # {exit_on_close,false}]}, + # {listener,rabbit@warp10,stomp,"localhost", + # {0,0,0,0,0,0,0,0}, + # 61613, + # [{backlog,128},{nodelay,true}]}] + case :rabbit_misc.rpc_call(node_name, :rabbit_networking, :active_listeners, [], timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + xs when is_list(xs) -> listeners_on(xs, node_name) + other -> other + end + end + + def output([], %{formatter: fmt}) when fmt == "csv" or fmt == "erlang" do + {:ok, []} + end + + def output([], %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "listeners" => []}} + end + + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported no enabled listeners."} + end + + def output(listeners, %{formatter: "erlang"}) do + {:ok, listener_rows(listeners)} + end + + def output(listeners, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "listeners" => listener_maps(listeners)}} + end + + def output(listeners, %{formatter: "csv"}) do + {:stream, [listener_rows(listeners)]} + end + + def output(listeners, _opts) do + lines = listener_lines(listeners) + + {:ok, Enum.join(lines, line_separator())} + end + + def help_section(), do: :observability_and_health_checks + + def description(), + do: "Lists active connection listeners (bound interface, port, protocol) on the target node" + + def usage, do: "listeners" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} to report its protocol listeners ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_location_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_location_command.ex new file mode 100644 index 0000000000..36ff562b41 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_location_command.ex @@ -0,0 +1,56 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.LogLocationCommand do + @moduledoc """ + Displays standard log file location on the target node + """ + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.LogFiles + + def switches, do: [all: :boolean, timeout: :integer] + def aliases, do: [a: :all, t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{all: false}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout, all: all}) do + case all do + true -> LogFiles.get_log_locations(node_name, timeout); + false -> LogFiles.get_default_log_location(node_name, timeout) + end + end + + def output({:ok, location}, %{node: node_name, formatter: "json"}) do + {:ok, %{ + "result" => "ok", + "node_name" => node_name, + "paths" => [location] + }} + end + def output(locations, %{node: node_name, formatter: "json"}) do + {:ok, %{ + "result" => "ok", + "node_name" => node_name, + "paths" => locations + }} + end + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :configuration + + def description(), do: "Shows log file location(s) on target node" + + def usage, do: "log_location [--all|-a]" + + def banner([], %{node: node_name}) do + "Log file location(s) on node #{node_name} ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_tail_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_tail_command.ex new file mode 100644 index 0000000000..9717908f60 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_tail_command.ex @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.LogTailCommand do + @moduledoc """ + Displays standard log file location on the target node + """ + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.LogFiles + + def switches, do: [number: :integer, timeout: :integer] + def aliases, do: ['N': :number, t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{number: 50}, opts)} + end + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout, number: n}) do + case LogFiles.get_default_log_location(node_name, timeout) do + {:ok, file} -> + :rabbit_misc.rpc_call(node_name, + :rabbit_log_tail, :tail_n_lines, [file, n], + timeout) + error -> error + end + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Prints the last N lines of the log on the node" + + def usage, do: "log_tail [--number|-N <number>]" + + def usage_additional do + [ + ["<number>", "number of lines to print. Defaults to 50"] + ] + end + + def banner([], %{node: node_name, number: n}) do + "Last #{n} log lines on node #{node_name} ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_tail_stream_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_tail_stream_command.ex new file mode 100644 index 0000000000..5080fd0d1d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/log_tail_stream_command.ex @@ -0,0 +1,73 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.LogTailStreamCommand do + @moduledoc """ + Displays standard log file location on the target node + """ + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.LogFiles + + + def switches(), do: [duration: :integer, timeout: :integer] + def aliases(), do: [d: :duration, t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{duration: :infinity}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def printer(), do: RabbitMQ.CLI.Printers.StdIORaw + + def run([], %{node: node_name, timeout: timeout, duration: duration}) do + case LogFiles.get_default_log_location(node_name, timeout) do + {:ok, file} -> + pid = self() + ref = make_ref() + subscribed = :rabbit_misc.rpc_call( + node_name, + :rabbit_log_tail, :init_tail_stream, + [file, pid, ref, duration], + timeout) + case subscribed do + {:ok, ^ref} -> + Stream.unfold(:confinue, + fn(:finished) -> nil + (:confinue) -> + receive do + {^ref, data, :finished} -> {data, :finished}; + {^ref, data, :confinue} -> {data, :confinue} + end + end) + error -> error + end + error -> error + end + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Streams logs from a running node for a period of time" + + def usage, do: "log_tail_stream [--duration|-d <seconds>]" + + def usage_additional() do + [ + ["<duration_in_seconds>", "duration in seconds to stream log. Defaults to infinity"] + ] + end + + def banner([], %{node: node_name, duration: :infinity}) do + "Streaming logs from node #{node_name} ..." + end + def banner([], %{node: node_name, duration: duration}) do + "Streaming logs from node #{node_name} for #{duration} seconds ..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/maybe_stuck_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/maybe_stuck_command.ex new file mode 100644 index 0000000000..c241780f62 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/maybe_stuck_command.ex @@ -0,0 +1,29 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.MaybeStuckCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_diagnostics, :maybe_stuck, [], timeout) + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Detects Erlang processes (\"lightweight threads\") potentially not making progress on the target node" + + def usage, do: "maybe_stuck" + + def banner(_, %{node: node_name}) do + "Asking node #{node_name} to detect potentially stuck Erlang processes..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/memory_breakdown_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/memory_breakdown_command.ex new file mode 100644 index 0000000000..356358b7d7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/memory_breakdown_command.ex @@ -0,0 +1,103 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.MemoryBreakdownCommand do + alias RabbitMQ.CLI.InformationUnit, as: IU + import RabbitMQ.CLI.Core.Memory + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [unit: :string, timeout: :integer] + def aliases(), do: [t: :timeout] + + def merge_defaults(args, opts) do + {args, Map.merge(%{unit: "gb"}, opts)} + end + + def validate(args, _) when length(args) > 0 do + {:validation_failure, :too_many_args} + end + + def validate(_, %{unit: unit}) do + case IU.known_unit?(unit) do + true -> + :ok + + false -> + {:validation_failure, "unit '#{unit}' is not supported. Please use one of: bytes, mb, gb"} + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_misc.rpc_call(node_name, :rabbit_vm, :memory, [], timeout) + end + + def output(result, %{formatter: "json"} = _opts) do + {:ok, compute_relative_values(result)} + end + + def output(result, %{formatter: "csv"} = _opts) do + flattened = + compute_relative_values(result) + |> Enum.flat_map(fn {k, %{bytes: b, percentage: p}} -> + [{"#{k}.bytes", b}, {"#{k}.percentage", p}] + end) + |> Enum.sort_by(fn {key, _val} -> key end, &>=/2) + + headers = Enum.map(flattened, fn {k, _v} -> k end) + values = Enum.map(flattened, fn {_k, v} -> v end) + + {:stream, [headers, values]} + end + + def output(result, _opts) do + {:ok, compute_relative_values(result)} + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Provides a memory usage breakdown on the target node." + + def usage, do: "memory_breakdown [--unit <unit>]" + + def usage_additional() do + [ + ["--unit <bytes | mb | gb>", "byte multiple (bytes, megabytes, gigabytes) to use"], + ["--formatter <json | csv | erlang>", "alternative formatter to use, JSON, CSV or Erlang terms"] + ] + end + + def banner([], %{node: node_name}) do + "Reporting memory breakdown on node #{node_name}..." + end + + defmodule Formatter do + alias RabbitMQ.CLI.Formatters.FormatterHelpers + alias RabbitMQ.CLI.InformationUnit, as: IU + + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, %{unit: unit}) do + Enum.reduce(output, "", fn {key, %{bytes: bytes, percentage: percentage}}, acc -> + u = String.downcase(unit) + acc <> "#{key}: #{IU.convert(bytes, u)} #{u} (#{percentage}%)\n" + end) + end + + def format_stream(stream, options) do + Stream.map( + stream, + FormatterHelpers.without_errors_1(fn el -> + format_output(el, options) + end) + ) + end + end + + def formatter(), do: Formatter +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/observer_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/observer_command.ex new file mode 100644 index 0000000000..717e23e6b5 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/observer_command.ex @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ObserverCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def switches(), do: [interval: :integer] + def aliases(), do: [i: :interval] + + def merge_defaults(args, opts) do + {args, Map.merge(%{interval: 5}, opts)} + end + + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, interval: interval}) do + case :observer_cli.start(node_name, [{:interval, interval * 1000}]) do + # See zhongwencool/observer_cli#54 + {:badrpc, _} = err -> err + {:error, _} = err -> err + {:error, _, _} = err -> err + :ok -> {:ok, "Disconnected from #{node_name}."} + :quit -> {:ok, "Disconnected from #{node_name}."} + other -> other + end + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Starts a CLI observer interface on the target node" + + def usage, do: "observer [--interval <seconds>]" + + def usage_additional() do + [ + ["--interval <seconds>", "Update interval to use, in seconds"] + ] + end + + def banner(_, %{node: node_name}) do + "Starting a CLI observer interface on node #{node_name}..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/os_env_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/os_env_command.ex new file mode 100644 index 0000000000..63e8c18beb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/os_env_command.ex @@ -0,0 +1,67 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.OsEnvCommand do + @moduledoc """ + Lists RabbitMQ-specific environment variables defined on target node + """ + + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_env, :get_used_env_vars, [], timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + xs when is_list(xs) -> + # convert keys and values to binaries (Elixir strings) + xs + |> Enum.map(fn {k, v} -> {:rabbit_data_coercion.to_binary(k), :rabbit_data_coercion.to_binary(v)} end) + |> :maps.from_list + other -> other + end + end + + def output([], %{formatter: fmt}) when fmt == "csv" or fmt == "erlang" do + {:ok, []} + end + def output([], %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "variables" => []}} + end + def output([], %{node: node_name}) do + {:ok, "Node #{node_name} reported no relevant environment variables."} + end + def output(vars, %{node: node_name, formatter: "json"}) do + {:ok, %{"result" => "ok", "node" => node_name, "variables" => vars}} + end + def output(vars, %{formatter: "csv"}) do + {:stream, [Enum.map(vars, fn({k, v}) -> [variable: k, value: v] end)]} + end + def output(vars, _opts) do + lines = Enum.map(vars, fn({k, v}) -> "#{k}=#{v}" end) |> Enum.join(line_separator()) + {:ok, lines} + end + + def usage() do + "os_env" + end + + def help_section(), do: :configuration + + def description(), do: "Lists RabbitMQ-specific environment variables set on target node" + + def banner(_, %{node: node_name}) do + "Listing RabbitMQ-specific environment variables defined on node #{node_name}..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/reset_node_auth_attempt_metrics_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/reset_node_auth_attempt_metrics_command.ex new file mode 100644 index 0000000000..e3b08c2ac8 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/reset_node_auth_attempt_metrics_command.ex @@ -0,0 +1,37 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ResetNodeAuthAttemptMetricsCommand do + alias RabbitMQ.CLI.Core.DocGuide + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_core_metrics, :reset_auth_attempt_metrics, []) + end + + def usage, do: "reset_node_auth_attempt_metrics" + + def usage_doc_guides() do + [ + DocGuide.access_control(), + DocGuide.monitoring() + ] + end + + def help_section(), do: :configuration + + def description(), do: "Resets auth attempt metrics on the target node" + + def banner([], %{node: node_name}) do + "Reset auth attempt metrics on node #{node_name} ..." + end + + use RabbitMQ.CLI.DefaultOutput +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/resolve_hostname_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/resolve_hostname_command.ex new file mode 100644 index 0000000000..349dbee513 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/resolve_hostname_command.ex @@ -0,0 +1,94 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ResolveHostnameCommand do + @moduledoc """ + Resolves a hostname to one or more addresses of a given IP address family (IPv4 ot IPv6). + This command is not meant to compete with `dig` but rather provide a way + to perform basic resolution tests that take Erlang's inetrc file into account. + """ + + import RabbitCommon.Records + alias RabbitMQ.CLI.Core.Networking + alias RabbitMQ.CLI.Core.ExitCodes + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:diagnostics] + + def switches(), do: [address_family: :string, offline: :boolean] + def aliases(), do: [a: :address_family] + + def merge_defaults(args, opts) do + {args, Map.merge(%{address_family: "IPv4", offline: false}, opts)} + end + + def validate(args, _) when length(args) < 1, do: {:validation_failure, :not_enough_args} + def validate(args, _) when length(args) > 1, do: {:validation_failure, :too_many_args} + def validate([_], %{address_family: family}) do + case Networking.valid_address_family?(family) do + true -> :ok + false -> {:validation_failure, {:bad_argument, "unsupported IP address family #{family}. Valid values are: ipv4, ipv6"}} + end + end + def validate([_], _), do: :ok + + def run([hostname], %{address_family: family, offline: true}) do + :inet.gethostbyname(to_charlist(hostname), Networking.address_family(family)) + end + def run([hostname], %{node: node_name, address_family: family, offline: false, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :inet, :gethostbyname, + [to_charlist(hostname), Networking.address_family(family)], timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + {:ok, result} -> {:ok, result} + other -> other + end + end + + def output({:error, :nxdomain}, %{node: node_name, formatter: "json"}) do + m = %{ + "result" => "error", + "node" => node_name, + "message" => "Hostname does not resolve (resolution failed with an nxdomain)" + } + {:error, ExitCodes.exit_dataerr(), m} + end + def output({:error, :nxdomain}, _opts) do + {:error, ExitCodes.exit_dataerr(), "Hostname does not resolve (resolution failed with an nxdomain)"} + end + def output({:ok, result}, %{node: node_name, address_family: family, formatter: "json"}) do + hostname = hostent(result, :h_name) + addresses = hostent(result, :h_addr_list) + {:ok, %{ + "result" => "ok", + "node" => node_name, + "hostname" => to_string(hostname), + "address_family" => family, + "addresses" => Networking.format_addresses(addresses) + }} + end + def output({:ok, result}, _opts) do + addresses = hostent(result, :h_addr_list) + {:ok, Enum.join(Networking.format_addresses(addresses), "\n")} + end + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "resolve_hostname <hostname> [--address-family <ipv4 | ipv6>]" + end + + def help_section(), do: :configuration + + def description(), do: "Resolves a hostname to a set of addresses. Takes Erlang's inetrc file into account." + + def banner([hostname], %{offline: false, node: node_name, address_family: family}) do + "Asking node #{node_name} to resolve hostname #{hostname} to #{family} addresses..." + end + def banner([hostname], %{offline: true, address_family: family}) do + "Resolving hostname #{hostname} to #{family} addresses..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/resolver_info_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/resolver_info_command.ex new file mode 100644 index 0000000000..a4f3d8d7d3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/resolver_info_command.ex @@ -0,0 +1,84 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ResolverInfoCommand do + @moduledoc """ + Displays effective hostname resolver (inetrc) configuration on target node + """ + + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + import RabbitMQ.CLI.Core.ANSI, only: [bright: 1] + alias RabbitMQ.CLI.Core.Networking + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:diagnostics] + + def switches(), do: [offline: :boolean] + def aliases(), do: [] + + def merge_defaults(args, opts) do + {args, Map.merge(%{offline: false}, opts)} + end + + def validate(args, _) when length(args) > 0, do: {:validation_failure, :too_many_args} + def validate([], _), do: :ok + + def run([], %{offline: true}) do + Networking.inetrc_map(:inet.get_rc()) + end + def run([], %{node: node_name, timeout: timeout, offline: false}) do + case :rabbit_misc.rpc_call(node_name, :inet, :get_rc, [], timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + xs when is_list(xs) -> Networking.inetrc_map(xs) + other -> other + end + end + + def output(info, %{node: node_name, formatter: "json"}) do + {:ok, %{ + "result" => "ok", + "node" => node_name, + "resolver" => info + }} + end + def output(info, _opts) do + main_section = [ + "#{bright("Runtime Hostname Resolver (inetrc) Settings")}\n", + "Lookup order: #{info["lookup"]}", + "Hosts file: #{info["hosts_file"]}", + "Resolver conf file: #{info["resolv_conf"]}", + "Cache size: #{info["cache_size"]}" + ] + hosts_section = [ + "\n#{bright("inetrc File Host Entries")}\n" + ] ++ case info["hosts"] do + [] -> ["(none)"] + nil -> ["(none)"] + hs -> Enum.reduce(hs, [], fn {k, v}, acc -> ["#{k} #{Enum.join(v, ", ")}" | acc] end) + end + + lines = main_section ++ hosts_section + + {:ok, Enum.join(lines, line_separator())} + end + + def usage() do + "resolver_info" + end + + def help_section(), do: :configuration + + def description(), do: "Displays effective hostname resolver (inetrc) configuration on target node" + + def banner(_, %{node: node_name, offline: false}) do + "Asking node #{node_name} for its effective hostname resolver (inetrc) configuration..." + end + def banner(_, %{offline: true}) do + "Displaying effective hostname resolver (inetrc) configuration used by CLI tools..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/runtime_thread_stats_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/runtime_thread_stats_command.ex new file mode 100644 index 0000000000..ee5bb56566 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/runtime_thread_stats_command.ex @@ -0,0 +1,70 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.RuntimeThreadStatsCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches(), do: [sample_interval: :integer] + def aliases(), do: [i: :sample_interval] + + def merge_defaults(args, opts) do + {args, Map.merge(%{sample_interval: 5}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout, sample_interval: interval}) do + case :rabbit_misc.rpc_call( + node_name, + :rabbit_runtime, + :msacc_stats, + [interval * 1000], + timeout + ) do + {:ok, stats} -> stats + other -> other + end + end + + def output(result, %{formatter: "json"}) when is_list(result) do + {:error, "JSON formatter is not supported by this command"} + end + + def output(result, %{formatter: "csv"}) when is_list(result) do + {:error, "CSV formatter is not supported by this command"} + end + + def output(result, _options) when is_list(result) do + {:ok, result} + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Provides a breakdown of runtime thread activity stats on the target node" + + def usage, do: "runtime_thread_stats [--sample-interval <interval>]" + + def usage_additional() do + [ + ["--sample-interval <seconds>", "sampling interval to use in seconds"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.runtime_tuning() + ] + end + + def banner([], %{node: node_name, sample_interval: interval}) do + "Will collect runtime thread stats on #{node_name} for #{interval} seconds..." + end + + def formatter(), do: RabbitMQ.CLI.Formatters.Msacc +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/schema_info_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/schema_info_command.ex new file mode 100644 index 0000000000..50b750c772 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/schema_info_command.ex @@ -0,0 +1,73 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.SchemaInfoCommand do + @moduledoc """ + Lists all tables on the mnesia schema + """ + + alias RabbitMQ.CLI.Ctl.InfoKeys + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @info_keys ~w(name snmp load_order active_replicas all_nodes attributes checkpoints disc_copies + disc_only_copies external_copies frag_properties master_nodes ram_copies + storage_properties subscribers user_properties cstruct local_content + where_to_commit where_to_read name access_mode cookie load_by_force + load_node record_name size storage_type type where_to_write index arity + majority memory commit_work where_to_wlock load_reason record_validation + version wild_pattern index_info)a + + def info_keys(), do: @info_keys + + def scopes(), do: [:ctl, :diagnostics] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def merge_defaults([], opts) do + merge_defaults( + ~w(name cookie active_replicas user_properties), + opts + ) + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{table_headers: true}, opts)} + end + + def validate(args, _) do + case InfoKeys.validate_info_keys(args, @info_keys) do + {:ok, _} -> :ok + err -> err + end + end + + def run([_ | _] = args, %{node: node_name, timeout: timeout}) do + info_keys = InfoKeys.prepare_info_keys(args) + :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :schema_info, [info_keys], timeout) + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage() do + "schema_info [--no-table-headers] [<column> ...]" + end + + def usage_additional() do + [ + ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")] + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists schema database tables and their properties" + + def banner(_, %{node: node_name}), do: "Asking node #{node_name} to report its schema..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/server_version_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/server_version_command.ex new file mode 100644 index 0000000000..9f4068e459 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/server_version_command.ex @@ -0,0 +1,37 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.ServerVersionCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + :rabbit_data_coercion.to_binary( + :rabbit_misc.rpc_call(node_name, :rabbit_misc, :version, [], timeout)) + end + + def output(result, %{formatter: "json"}) do + {:ok, %{"result" => "ok", "value" => result}} + end + def output(result, _options) when is_bitstring(result) do + {:ok, result} + end + + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Displays server version on the target node" + + def usage, do: "server_version" + + def banner([], %{node: node_name}) do + "Asking node #{node_name} for its RabbitMQ version..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/tls_versions_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/tls_versions_command.ex new file mode 100644 index 0000000000..2f81bad889 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/commands/tls_versions_command.ex @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Commands.TlsVersionsCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout} = _opts) do + :rabbit_misc.rpc_call(node_name, :ssl, :versions, [], timeout) + end + + def banner([], %{}), do: "Listing all TLS versions supported by the runtime..." + + def output(result, %{formatter: "json"}) do + vs = Map.new(result) |> Map.get(:available) + + {:ok, %{versions: vs}} + end + + def output(result, _opts) do + vs = Map.new(result) |> Map.get(:available) + {:ok, vs} + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Lists TLS versions supported (but not necessarily allowed) on the target node" + + def usage, do: "tls_versions" + + def formatter(), do: RabbitMQ.CLI.Formatters.StringPerLine +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/diagnostics_helpers.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/diagnostics_helpers.ex new file mode 100644 index 0000000000..601cc842cb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/diagnostics/diagnostics_helpers.ex @@ -0,0 +1,38 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Diagnostics.Helpers do + def test_connection(hostname, port, timeout) do + case :gen_tcp.connect(hostname, port, [], timeout) do + {:error, _} -> :gen_tcp.connect(hostname, port, [:inet6], timeout) + r -> r + end + end + + def check_port_connectivity(port, node_name, timeout) do + regex = Regex.recompile!(~r/^(.+)@/) + hostname = Regex.replace(regex, to_string(node_name), "") |> to_charlist + try do + case test_connection(hostname, port, timeout) do + {:error, _} -> + false + + {:ok, port} -> + :ok = :gen_tcp.close(port) + true + end + + # `gen_tcp:connect/4` will throw if the port is outside of its + # expected domain + catch + :exit, _ -> false + end + end + + def check_listener_connectivity(%{port: port}, node_name, timeout) do + check_port_connectivity(port, node_name, timeout) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatter_behaviour.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatter_behaviour.ex new file mode 100644 index 0000000000..498ba114b9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatter_behaviour.ex @@ -0,0 +1,42 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Formats returned values e.g. to human-readable text or JSON. +defmodule RabbitMQ.CLI.FormatterBehaviour do + alias RabbitMQ.CLI.Core.Helpers + + @callback format_output(any, map()) :: String.t() | [String.t()] + @callback format_stream(Enumerable.t(), map()) :: Enumerable.t() + + @optional_callbacks switches: 0, + aliases: 0 + + @callback switches() :: Keyword.t() + @callback aliases() :: Keyword.t() + + def switches(formatter) do + Helpers.apply_if_exported(formatter, :switches, [], []) + end + + def aliases(formatter) do + Helpers.apply_if_exported(formatter, :aliases, [], []) + end + + def module_name(nil) do + nil + end + def module_name(formatter) do + mod = formatter |> String.downcase |> Macro.camelize + Module.safe_concat("RabbitMQ.CLI.Formatters", mod) + end + + def machine_readable?(nil) do + false + end + def machine_readable?(formatter) do + Helpers.apply_if_exported(module_name(formatter), :machine_readable?, [], false) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/csv.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/csv.ex new file mode 100644 index 0000000000..ab9acd613f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/csv.ex @@ -0,0 +1,127 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +alias RabbitMQ.CLI.Formatters.FormatterHelpers + +defmodule RabbitMQ.CLI.Formatters.Csv do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_stream(stream, _) do + ## Flatten list_consumers + Stream.flat_map( + stream, + fn + [first | _] = element -> + case FormatterHelpers.proplist?(first) or is_map(first) do + true -> element + false -> [element] + end + + other -> + [other] + end + ) + ## Add info_items names + |> Stream.transform( + :init, + FormatterHelpers.without_errors_2(fn + element, :init -> + { + case keys(element) do + nil -> [values(element)] + ks -> [ks, values(element)] + end, + :next + } + + element, :next -> + {[values(element)], :next} + end) + ) + |> CSV.encode(delimiter: "") + end + + def format_output(output, _) do + case keys(output) do + nil -> [values(output)] + ks -> [ks, values(output)] + end + |> CSV.encode() + |> Enum.join() + end + + def machine_readable?, do: true + + # + # Implementation + # + + defp keys(map) when is_map(map) do + Map.keys(map) + end + + defp keys(list) when is_list(list) do + case FormatterHelpers.proplist?(list) do + true -> Keyword.keys(list) + false -> nil + end + end + + defp keys(_other) do + nil + end + + defp values(map) when is_map(map) do + Map.values(map) + end + + defp values([]) do + [] + end + + defp values(list) when is_list(list) do + case FormatterHelpers.proplist?(list) do + true -> Keyword.values(list) + false -> list + end + end + + defp values(other) do + other + end +end + +defimpl CSV.Encode, for: PID do + def encode(pid, env \\ []) do + FormatterHelpers.format_info_item(pid) + |> to_string + |> CSV.Encode.encode(env) + end +end + +defimpl CSV.Encode, for: List do + def encode(list, env \\ []) do + FormatterHelpers.format_info_item(list) + |> to_string + |> CSV.Encode.encode(env) + end +end + +defimpl CSV.Encode, for: Tuple do + def encode(tuple, env \\ []) do + FormatterHelpers.format_info_item(tuple) + |> to_string + |> CSV.Encode.encode(env) + end +end + +defimpl CSV.Encode, for: Map do + def encode(map, env \\ []) do + FormatterHelpers.format_info_item(map) + |> to_string + |> CSV.Encode.encode(env) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/erlang.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/erlang.ex new file mode 100644 index 0000000000..0a8a78249f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/erlang.ex @@ -0,0 +1,18 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Formatters.Erlang do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, _) do + :io_lib.format("~p", [output]) + |> to_string + end + + def format_stream(stream, options) do + [format_output(Enum.to_list(stream), options)] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/formatter_helpers.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/formatter_helpers.ex new file mode 100644 index 0000000000..2ec4edc3d9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/formatter_helpers.ex @@ -0,0 +1,182 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Formatters.FormatterHelpers do + import RabbitCommon.Records + use Bitwise + + @type error :: {:error, term()} | {:error, integer(), String.t() | [String.t()]} + + @spec without_errors_1((el -> result)) :: error() | result when el: term(), result: term() + def without_errors_1(fun) do + fn + {:error, _} = err -> err + {:error, _, _} = err -> err + other -> fun.(other) + end + end + + @spec without_errors_1((el, acc -> result)) :: error() | result + when el: term(), result: term(), acc: term() + def without_errors_2(fun) do + fn + {:error, _} = err, _acc -> err + {:error, _, _} = err, _acc -> err + other, acc -> fun.(other, acc) + end + end + + def proplist?([{_key, _value} | rest]), do: proplist?(rest) + def proplist?([]), do: true + def proplist?(_other), do: false + + + defmacro is_u8(x) do + quote do + unquote(x) >= 0 and unquote(x) <= 255 + end + end + + defmacro is_u16(x) do + quote do + unquote(x) >= 0 and unquote(x) <= 65_535 + end + end + + def format_info_item(item, escaped \\ true) + + def format_info_item(map, escaped) when is_map(map) do + [ + "\#\{", + Enum.map( + map, + fn {k, v} -> + ["#{escape(k, escaped)} => ", format_info_item(v, escaped)] + end + ) + |> Enum.join(", "), + "}" + ] + end + + # when Record.is_record(res, :resource) do + def format_info_item(resource(name: name), escaped) do + # resource(name: name) = res + escape(name, escaped) + end + + def format_info_item({n1, n2, n3, n4} = value, _escaped) + when is_u8(n1) and is_u8(n2) and is_u8(n3) and is_u8(n4) do + :rabbit_misc.ntoa(value) + end + + def format_info_item({k1, k2, k3, k4, k5, k6, k7, k8} = value, _escaped) + when is_u16(k1) and is_u16(k2) and is_u16(k3) and is_u16(k4) and + is_u16(k5) and is_u16(k6) and is_u16(k7) and is_u16(k8) do + :rabbit_misc.ntoa(value) + end + + def format_info_item(value, _escaped) when is_pid(value) do + :rabbit_misc.pid_to_string(value) + end + + def format_info_item(value, escaped) when is_binary(value) do + escape(value, escaped) + end + + def format_info_item(value, escaped) when is_atom(value) do + escape(to_charlist(value), escaped) + end + + def format_info_item( + [{key, type, _table_entry_value} | _] = value, + escaped + ) + when is_binary(key) and + is_atom(type) do + :io_lib.format( + "~1000000000000tp", + [prettify_amqp_table(value, escaped)] + ) + end + + def format_info_item([t | _] = value, escaped) + when is_tuple(t) or is_pid(t) or is_binary(t) or is_atom(t) or is_list(t) do + [ + "[", + Enum.map( + value, + fn el -> + format_info_item(el, escaped) + end + ) + |> Enum.join(", "), + "]" + ] + end + + def format_info_item({key, value}, escaped) do + ["{", :io_lib.format("~p", [key]), ", ", format_info_item(value, escaped), "}"] + end + + def format_info_item(value, _escaped) do + :io_lib.format("~1000000000000tp", [value]) + end + + defp prettify_amqp_table(table, escaped) do + for {k, t, v} <- table do + {escape(k, escaped), prettify_typed_amqp_value(t, v, escaped)} + end + end + + defp prettify_typed_amqp_value(:longstr, value, escaped) do + escape(value, escaped) + end + + defp prettify_typed_amqp_value(:table, value, escaped) do + prettify_amqp_table(value, escaped) + end + + defp prettify_typed_amqp_value(:array, value, escaped) do + for {t, v} <- value, do: prettify_typed_amqp_value(t, v, escaped) + end + + defp prettify_typed_amqp_value(_type, value, _escaped) do + value + end + + defp escape(atom, escaped) when is_atom(atom) do + escape(to_charlist(atom), escaped) + end + + defp escape(bin, escaped) when is_binary(bin) do + escape(to_charlist(bin), escaped) + end + + defp escape(l, false) when is_list(l) do + escape_char(:lists.reverse(l), []) + end + + defp escape(l, true) when is_list(l) do + l + end + + defp escape_char([?\\ | t], acc) do + escape_char(t, [?\\, ?\\ | acc]) + end + + defp escape_char([x | t], acc) when x >= 32 and x != 127 do + escape_char(t, [x | acc]) + end + + defp escape_char([x | t], acc) do + escape_char(t, [?\\, ?0 + (x >>> 6), ?0 + (x &&& 0o070 >>> 3), ?0 + (x &&& 7) | acc]) + end + + defp escape_char([], acc) do + acc + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/inspect.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/inspect.ex new file mode 100644 index 0000000000..5939007cfe --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/inspect.ex @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. +alias RabbitMQ.CLI.Formatters.FormatterHelpers + +defmodule RabbitMQ.CLI.Formatters.Inspect do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, _) do + case is_binary(output) do + true -> output + false -> inspect(output) + end + end + + def format_stream(stream, options) do + elements = + Stream.scan( + stream, + :empty, + FormatterHelpers.without_errors_2(fn element, previous -> + separator = + case previous do + :empty -> "" + _ -> "," + end + + format_element(element, separator, options) + end) + ) + + Stream.concat([["["], elements, ["]"]]) + end + + def format_element(val, separator, options) do + separator <> format_output(val, options) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json.ex new file mode 100644 index 0000000000..eb55038715 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json.ex @@ -0,0 +1,69 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Basic JSON formatter. Supports 1-level of +# collection using start/finish_collection. +# Primary purpose is to translate stream from CTL, +# so there is no need for multiple collection levels +alias RabbitMQ.CLI.Formatters.FormatterHelpers + +defmodule RabbitMQ.CLI.Formatters.Json do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, opts) when is_bitstring(output) do + format_output(%{"message" => output}, opts) + end + def format_output(output, _opts) do + {:ok, json} = JSON.encode(keys_to_atoms(output)) + json + end + + def format_stream(stream, options) do + ## Flatten list_consumers + elements = + Stream.flat_map( + stream, + fn + [first | _] = element -> + case FormatterHelpers.proplist?(first) or is_map(first) do + true -> element + false -> [element] + end + + other -> + [other] + end + ) + |> Stream.scan( + :empty, + FormatterHelpers.without_errors_2(fn element, previous -> + separator = + case previous do + :empty -> "" + _ -> "," + end + + format_element(element, separator, options) + end) + ) + + Stream.concat([["["], elements, ["]"]]) + end + + def keys_to_atoms(enum) do + Enum.map(enum, + fn({k, v}) when is_binary(k) or is_list(k) -> + {String.to_atom(k), v} + (other) -> other + end) + end + + def format_element(val, separator, options) do + separator <> format_output(val, options) + end + + def machine_readable?, do: true +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json_stream.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json_stream.ex new file mode 100644 index 0000000000..a1bea3fc11 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json_stream.ex @@ -0,0 +1,75 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Basic JSON formatter. Supports 1-level of +# collection using start/finish_collection. +# Primary purpose is to translate stream from CTL, +# so there is no need for multiple collection levels + +defmodule RabbitMQ.CLI.Formatters.JsonStream do + @moduledoc """ + Formats a potentially infinite stream of maps, proplists, keyword lists, + and other things that are essentially a map. + + The output exclude JSON array boundaries. The output can be fed + to `jq' for pretty printing, filtering and querying. + """ + + @behaviour RabbitMQ.CLI.FormatterBehaviour + + alias RabbitMQ.CLI.Formatters.FormatterHelpers + alias RabbitMQ.CLI.Core.Platform + + def format_output("", _opts) do + # the empty string can be emitted along with a finishing marker that ends the stream + # (e.g. with commands that have a duration argument) + # we just emit the empty string as the last value for the stream in this case + "" + end + def format_output(output, _opts) do + {:ok, json} = JSON.encode(keys_to_atoms(output)) + json + end + + def format_stream(stream, options) do + elements = + Stream.flat_map( + stream, + fn + [first | _] = element -> + case FormatterHelpers.proplist?(first) or is_map(first) do + true -> element + false -> [element] + end + + other -> + [other] + end + ) + |> Stream.scan( + :empty, + FormatterHelpers.without_errors_2(fn element, _previous -> + format_element(element, options) + end) + ) + + elements + end + + def keys_to_atoms(enum) do + Enum.map(enum, + fn({k, v}) when is_binary(k) or is_list(k) -> + {String.to_atom(k), v} + (other) -> other + end) + end + + def format_element(val, options) do + format_output(val, options) <> Platform.line_separator() + end + + def machine_readable?, do: true +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/msacc.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/msacc.ex new file mode 100644 index 0000000000..992475a2d0 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/msacc.ex @@ -0,0 +1,19 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Formatters.Msacc do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, _) do + {:ok, io} = StringIO.open("") + :msacc.print(io, output, %{}) + StringIO.flush(io) + end + + def format_stream(stream, options) do + [format_output(Enum.to_list(stream), options)] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/plugins.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/plugins.ex new file mode 100644 index 0000000000..54881cc32f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/plugins.ex @@ -0,0 +1,247 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. +alias RabbitMQ.CLI.Formatters.FormatterHelpers + +defmodule RabbitMQ.CLI.Formatters.Plugins do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output( + %{status: status, format: format, plugins: plugins}, + options + ) do + legend(status, format, options) ++ format_plugins(plugins, format) + end + + def format_output(%{enabled: enabled, mode: _} = output, options) do + case length(enabled) do + 0 -> + ["Plugin configuration unchanged."] + + _ -> + [ + "The following plugins have been enabled:" + | for plugin <- enabled do + " #{plugin}" + end + ] ++ + [""] ++ + applying(output, options) ++ + log_offline(output) + end + end + + def format_output(%{disabled: disabled, mode: _} = output, options) do + case length(disabled) do + 0 -> + ["Plugin configuration unchanged."] + + _ -> + [ + "The following plugins have been disabled:" + | for plugin <- disabled do + " #{plugin}" + end + ] ++ + [""] ++ + applying(output, options) ++ + log_offline(output) + end + end + + ## Do not print enabled/disabled for set command + def format_output(%{} = output, options) do + applying(output, options) + end + + def format_output([], %{node: node}) do + ["All plugins have been disabled.", "Applying plugin configuration to #{node}..."] + end + + def format_output(plugins, %{node: node}) when is_list(plugins) do + [ + "The following plugins have been configured:" + | for plugin <- plugins do + " #{plugin}" + end + ] ++ + ["Applying plugin configuration to #{node}..."] + end + + def format_output(output, _) do + :io_lib.format("~p", [output]) + |> to_string + end + + def format_stream(stream, options) do + Stream.map( + stream, + FormatterHelpers.without_errors_1(fn element -> + format_output(element, options) + end) + ) + end + + defp format_plugins(plugins, format) do + max_name_length = + Enum.reduce(plugins, 0, fn %{name: name}, len -> + max(String.length(to_string(name)), len) + end) + + for plugin <- plugins do + format_plugin(plugin, format, max_name_length) + end + |> List.flatten() + end + + defp format_plugin(%{name: name}, :minimal, _) do + to_string(name) + end + + defp format_plugin(plugin, :normal, max_name_length) do + [summary(plugin) <> inline_version(plugin, max_name_length)] + end + + defp format_plugin(plugin, :verbose, _) do + [summary(plugin) | verbose(plugin)] + end + + defp summary(%{name: name, enabled: enabled, running: running}) do + enabled_sign = + case enabled do + :implicit -> "e" + :enabled -> "E" + :not_enabled -> " " + end + + running_sign = + case running do + true -> "*" + false -> " " + end + + "[#{enabled_sign}#{running_sign}] #{name}" + end + + defp inline_version(%{name: name} = plugin, max_name_length) do + spacing = + String.duplicate( + " ", + max_name_length - + String.length(to_string(name)) + ) + + spacing <> " " <> augment_version(plugin) + end + + defp verbose(%{dependencies: dependencies, description: description} = plugin) do + prettified = to_string(:io_lib.format("~p", [dependencies])) + + [ + " Version: \t#{augment_version(plugin)}", + " Dependencies:\t#{prettified}", + " Description: \t#{description}" + ] + end + + defp augment_version(%{version: version, running_version: nil}) do + to_string(version) + end + + defp augment_version(%{version: version, running_version: version}) do + to_string(version) + end + + defp augment_version(%{version: version, running_version: running_version}) do + "#{running_version} (pending upgrade to #{version})" + end + + ## Do not print legend in minimal, quiet or silent mode + defp legend(_, :minimal, _) do + [] + end + defp legend(_, _, %{quiet: true}) do + [] + end + defp legend(_, _, %{silent: true}) do + [] + end + + defp legend(status, _, %{node: node}) do + [ + " Configured: E = explicitly enabled; e = implicitly enabled", + " | Status: #{status_message(status, node)}", + " |/" + ] + end + + defp status_message(:running, node) do + "* = running on #{node}" + end + + defp status_message(:node_down, node) do + "[failed to contact #{node} - status not shown]" + end + + defp applying(%{mode: :offline, set: set_plugins}, _) do + set_plugins_message = + case length(set_plugins) do + 0 -> "nothing to do" + len -> "set #{len} plugins" + end + + [set_plugins_message <> "."] + end + + defp applying(%{mode: :offline, enabled: enabled}, _) do + enabled_message = + case length(enabled) do + 0 -> "nothing to do" + len -> "enabled #{len} plugins" + end + + [enabled_message <> "."] + end + + defp applying(%{mode: :offline, disabled: disabled}, _) do + disabled_message = + case length(disabled) do + 0 -> "nothing to do" + len -> "disabled #{len} plugins" + end + + [disabled_message <> "."] + end + + defp applying(%{mode: :online, started: started, stopped: stopped}, _) do + stopped_message = + case length(stopped) do + 0 -> [] + len -> ["stopped #{len} plugins"] + end + + started_message = + case length(started) do + 0 -> [] + len -> ["started #{len} plugins"] + end + + change_message = + case Enum.join(started_message ++ stopped_message, " and ") do + "" -> "nothing to do" + msg -> msg + end + + [change_message <> "."] + end + + defp log_offline(%{mode: :offline}) do + ["Offline change; changes will take effect at broker restart."] + end + + defp log_offline(_) do + [] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/pretty_table.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/pretty_table.ex new file mode 100644 index 0000000000..6b9b7ed9fd --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/pretty_table.ex @@ -0,0 +1,87 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Formatters.PrettyTable do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + alias RabbitMQ.CLI.Formatters.FormatterHelpers + + require Record + import Record + + defrecord :table , extract(:table, + from_lib: "stdout_formatter/include/stdout_formatter.hrl") + defrecord :cell, extract(:cell, + from_lib: "stdout_formatter/include/stdout_formatter.hrl") + defrecord :paragraph, extract(:paragraph, + from_lib: "stdout_formatter/include/stdout_formatter.hrl") + + def format_stream(stream, _opts) do + # Flatten for list_consumers + entries_with_keys = Stream.flat_map(stream, + fn([first | _] = element) -> + case FormatterHelpers.proplist?(first) or is_map(first) do + true -> element; + false -> [element] + end + (other) -> + [other] + end) + |> Enum.to_list() + + # Use stdout_formatter library to format the table. + case entries_with_keys do + [first_entry | _] -> + col_headers = Stream.map(first_entry, + fn({key, _}) -> + cell(content: key, props: %{:title => true}) + end) + |> Enum.to_list() + rows = Stream.map(entries_with_keys, + fn(element) -> + Stream.map(element, + fn({_, value}) -> + cell(content: value, props: %{}) + end) + |> Enum.to_list() + end) + |> Enum.to_list() + ret = :stdout_formatter.to_string( + table( + rows: [col_headers | rows], + props: %{:cell_padding => {0, 1}})) + [to_string ret] + [] -> + entries_with_keys + end + end + + def format_output(output, _opts) do + format = case is_binary(output) do + true -> "~s" + false -> "~p" + end + ret = :stdout_formatter.to_string( + table( + rows: [ + [cell(content: "Output", props: %{:title => true})], + [cell( + content: paragraph(content: output, + props: %{:format => format}))]], + props: %{:cell_padding => {0, 1}})) + to_string ret + end + + def format_value(value) do + case is_binary(value) do + true -> value + false -> case is_atom(value) do + true -> to_string(value) + false -> inspect(value) + end + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/report.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/report.ex new file mode 100644 index 0000000000..4db89d611f --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/report.ex @@ -0,0 +1,52 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2017-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Formatters.Report do + alias RabbitMQ.CLI.Formatters.FormatterHelpers + alias RabbitMQ.CLI.Core.{Output, Config} + + @behaviour RabbitMQ.CLI.FormatterBehaviour + def format_output(_, _) do + raise "format_output is not implemented for report formatter" + end + + def format_stream(stream, options) do + quiet = options[:quiet] || options[:silent] || false + + Stream.flat_map( + stream, + FormatterHelpers.without_errors_1(fn + {_command, _banner, {:error, _} = err} -> + err + + {_command, _banner, {:error, _, _} = err} -> + err + + {command, banner, result} -> + case quiet do + true -> + Stream.concat([""], format_result(command, result, options)) + + false -> + Stream.concat(["" | banner_list(banner)], format_result(command, result, options)) + end + end) + ) + end + + def format_result(command, output, options) do + formatter = Config.get_formatter(command, options) + + case Output.format_output(output, formatter, options) do + :ok -> [] + {:ok, val} -> [val] + {:stream, stream} -> stream + end + end + + def banner_list([_ | _] = list), do: list + def banner_list(val), do: [val] +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/string.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/string.ex new file mode 100644 index 0000000000..6fd7f2e0e3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/string.ex @@ -0,0 +1,26 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +## Prints values from a command as strings(if possible) +defmodule RabbitMQ.CLI.Formatters.String do + alias RabbitMQ.CLI.Core.Helpers + alias RabbitMQ.CLI.Formatters.FormatterHelpers + + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, _) do + Helpers.string_or_inspect(output) + end + + def format_stream(stream, options) do + Stream.map( + stream, + FormatterHelpers.without_errors_1(fn el -> + format_output(el, options) + end) + ) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/string_per_line.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/string_per_line.ex new file mode 100644 index 0000000000..4761b9a555 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/string_per_line.ex @@ -0,0 +1,42 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Formatters.StringPerLine do + @doc """ + Use this to output one stream (collection) element per line, + using the string formatter. Flattens the stream. + """ + + alias RabbitMQ.CLI.Formatters.FormatterHelpers + alias RabbitMQ.CLI.Core.Helpers + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def format_output(output, _) do + Enum.map(output, fn el -> Helpers.string_or_inspect(el) end) + end + + def format_stream(stream, options) do + Stream.scan( + stream, + :empty, + FormatterHelpers.without_errors_2(fn element, previous -> + separator = + case previous do + :empty -> "" + _ -> line_separator() + end + + format_element(element, separator, options) + end) + ) + end + + def format_element(val, separator, options) do + separator <> format_output(val, options) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/table.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/table.ex new file mode 100644 index 0000000000..72d1682202 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/table.ex @@ -0,0 +1,138 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +alias RabbitMQ.CLI.Formatters.FormatterHelpers + +defmodule RabbitMQ.CLI.Formatters.Table do + @behaviour RabbitMQ.CLI.FormatterBehaviour + + def switches(), do: [table_headers: :boolean, pad_to_header: :boolean] + + def format_stream(stream, options) do + # Flatten for list_consumers + Stream.flat_map( + stream, + fn + [first | _] = element -> + case FormatterHelpers.proplist?(first) or is_map(first) do + true -> element + false -> [element] + end + + other -> + [other] + end + ) + |> Stream.transform( + :init, + FormatterHelpers.without_errors_2(fn + element, :init -> + {maybe_header(element, options), :next} + + element, :next -> + {[format_output_1(element, options)], :next} + end) + ) + end + + def format_output(output, options) do + maybe_header(output, options) + end + + defp maybe_header(output, options) do + opt_table_headers = Map.get(options, :table_headers, true) + opt_silent = Map.get(options, :silent, false) + + case {opt_silent, opt_table_headers} do + {true, _} -> + [format_output_1(output, options)] + + {false, false} -> + [format_output_1(output, options)] + + {false, true} -> + format_header(output) ++ [format_output_1(output, options)] + end + end + + defp format_output_1(output, options) when is_map(output) do + escaped = escaped?(options) + pad_to_header = pad_to_header?(options) + format_line(output, escaped, pad_to_header) + end + + defp format_output_1([], _) do + "" + end + + defp format_output_1(output, options) do + escaped = escaped?(options) + pad_to_header = pad_to_header?(options) + + case FormatterHelpers.proplist?(output) do + true -> format_line(output, escaped, pad_to_header) + false -> format_inspect(output) + end + end + + defp escaped?(_), do: true + + defp pad_to_header?(%{pad_to_header: pad}), do: pad + defp pad_to_header?(_), do: false + + defp format_line(line, escaped, pad_to_header) do + values = + Enum.map( + line, + fn {k, v} -> + line = FormatterHelpers.format_info_item(v, escaped) + + case pad_to_header do + true -> + String.pad_trailing( + to_string(line), + String.length(to_string(k)) + ) + + false -> + line + end + end + ) + + Enum.join(values, "\t") + end + + defp format_inspect(output) do + case is_binary(output) do + true -> output + false -> inspect(output) + end + end + + @spec format_header(term()) :: [String.t()] + defp format_header(output) do + keys = + case output do + map when is_map(map) -> + Map.keys(map) + + keyword when is_list(keyword) -> + case FormatterHelpers.proplist?(keyword) do + true -> Keyword.keys(keyword) + false -> [] + end + + _ -> + [] + end + + case keys do + [] -> [] + _ -> [Enum.join(keys, "\t")] + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/information_unit.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/information_unit.ex new file mode 100644 index 0000000000..ebef8de0ba --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/information_unit.ex @@ -0,0 +1,68 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.InformationUnit do + require MapSet + + @kilobyte_bytes 1000 + @megabyte_bytes @kilobyte_bytes * 1000 + @gigabyte_bytes @megabyte_bytes * 1000 + @terabyte_bytes @gigabyte_bytes * 1000 + + def known_units() do + MapSet.new([ + "bytes", + "kb", + "kilobytes", + "mb", + "megabytes", + "gb", + "gigabytes", + "tb", + "terabytes" + ]) + end + + def parse(val) do + :rabbit_resource_monitor_misc.parse_information_unit(val) + end + + def convert(bytes, "bytes") do + bytes + end + + def convert(bytes, unit) do + do_convert(bytes, String.downcase(unit)) + end + + def known_unit?(val) do + MapSet.member?(known_units(), String.downcase(val)) + end + + defp do_convert(bytes, "kb") do + Float.round(bytes / @kilobyte_bytes, 4) + end + + defp do_convert(bytes, "kilobytes"), do: do_convert(bytes, "kb") + + defp do_convert(bytes, "mb") do + Float.round(bytes / @megabyte_bytes, 4) + end + + defp do_convert(bytes, "megabytes"), do: do_convert(bytes, "mb") + + defp do_convert(bytes, "gb") do + Float.round(bytes / @gigabyte_bytes, 4) + end + + defp do_convert(bytes, "gigabytes"), do: do_convert(bytes, "gb") + + defp do_convert(bytes, "tb") do + Float.round(bytes / @terabyte_bytes, 4) + end + + defp do_convert(bytes, "terabytes"), do: do_convert(bytes, "tb") +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/directories_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/directories_command.ex new file mode 100644 index 0000000000..c3b6aecc0d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/directories_command.ex @@ -0,0 +1,134 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Commands.DirectoriesCommand do + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + alias RabbitMQ.CLI.Core.{DocGuide, Validators, Config} + import RabbitMQ.CLI.Core.{CodePath, Paths} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, %{offline: true} = opts) do + {args, opts} + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{online: true, offline: false}, opts)} + end + + def distribution(%{offline: true}), do: :none + def distribution(%{offline: false}), do: :cli + + def switches(), do: [online: :boolean, offline: :boolean] + + def validate(_, %{online: true, offline: true}) do + {:validation_failure, {:bad_argument, "Cannot set both online and offline"}} + end + + def validate(_, %{online: false, offline: false}) do + {:validation_failure, {:bad_argument, "Cannot set online and offline to false"}} + end + + def validate([_ | _], _) do + {:validation_failure, :too_many_args} + end + + def validate([], _) do + :ok + end + + def validate_execution_environment(args, %{offline: true} = opts) do + Validators.chain( + [ + &require_rabbit_and_plugins/2, + &PluginHelpers.enabled_plugins_file/2, + &plugins_dir/2 + ], + [args, opts] + ) + end + + def validate_execution_environment(args, %{online: true} = opts) do + Validators.node_is_running(args, opts) + end + + def run([], %{online: true, node: node_name}) do + do_run(fn key -> + :rabbit_misc.rpc_call(node_name, :rabbit_plugins, key, []) + end) + end + + def run([], %{offline: true} = opts) do + do_run(fn key -> + Config.get_option(key, opts) + end) + end + + def output({:ok, _map} = res, %{formatter: "json"}) do + res + end + + def output({:ok, map}, _opts) do + s = """ + Plugin archives directory: #{Map.get(map, :plugins_dir)} + Plugin expansion directory: #{Map.get(map, :plugins_expand_dir)} + Enabled plugins file: #{Map.get(map, :enabled_plugins_file)} + """ + + {:ok, String.trim_trailing(s)} + end + + def output({:error, err}, _opts) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(), err} + end + + use RabbitMQ.CLI.DefaultOutput + + def banner([], %{offline: true}) do + "Listing plugin directories inferred from local environment..." + end + + def banner([], %{online: true, node: node}) do + "Listing plugin directories used by node #{node}" + end + + def usage, do: "directories [--offline] [--online]" + + def usage_additional() do + [ + ["--offline", "do not contact target node. Try to infer directories from the environment."], + ["--online", "infer directories from target node."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.plugins() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Displays plugin directory and enabled plugin file paths" + + # + # Implementation + # + + defp do_run(fun) do + # return an error or an {:ok, map} tuple + Enum.reduce([:plugins_dir, :plugins_expand_dir, :enabled_plugins_file], {:ok, %{}}, fn + _, {:error, err} -> + {:error, err} + + key, {:ok, acc} -> + case fun.(key) do + {:error, err} -> {:error, err} + val -> {:ok, Map.put(acc, key, :rabbit_data_coercion.to_binary(val))} + end + end) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex new file mode 100644 index 0000000000..4fea2ad34e --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/disable_command.ex @@ -0,0 +1,146 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Commands.DisableCommand do + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + import RabbitMQ.CLI.Core.{CodePath, Paths} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def formatter(), do: RabbitMQ.CLI.Formatters.Plugins + + def merge_defaults(args, opts) do + {args, Map.merge(%{online: false, offline: false, all: false}, opts)} + end + + def distribution(%{offline: true}), do: :none + def distribution(%{offline: false}), do: :cli + + def switches(), do: [online: :boolean, offline: :boolean, all: :boolean] + + def validate([], %{all: false}) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _], %{all: true}) do + {:validation_failure, {:bad_argument, "Cannot set both --all and a list of plugins"}} + end + + def validate(_, %{online: true, offline: true}) do + {:validation_failure, {:bad_argument, "Cannot set both online and offline"}} + end + + def validate(_args, _opts) do + :ok + end + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &PluginHelpers.can_set_plugins_with_mode/2, + &require_rabbit_and_plugins/2, + &PluginHelpers.enabled_plugins_file/2, + &plugins_dir/2 + ], + [args, opts] + ) + end + + def run(plugin_names, %{all: all_flag, node: node_name} = opts) do + plugins = + case all_flag do + false -> for s <- plugin_names, do: String.to_atom(s) + true -> PluginHelpers.plugin_names(PluginHelpers.list(opts)) + end + + enabled = PluginHelpers.read_enabled(opts) + all = PluginHelpers.list(opts) + implicit = :rabbit_plugins.dependencies(false, enabled, all) + to_disable_deps = :rabbit_plugins.dependencies(true, plugins, all) + plugins_to_set = MapSet.difference(MapSet.new(enabled), MapSet.new(to_disable_deps)) + + mode = PluginHelpers.mode(opts) + + case PluginHelpers.set_enabled_plugins(MapSet.to_list(plugins_to_set), opts) do + {:ok, enabled_plugins} -> + {:stream, + Stream.concat([ + [:rabbit_plugins.strictly_plugins(enabled_plugins, all)], + RabbitMQ.CLI.Core.Helpers.defer(fn -> + :timer.sleep(5000) + + case PluginHelpers.update_enabled_plugins(enabled_plugins, mode, node_name, opts) do + %{set: new_enabled} = result -> + disabled = implicit -- new_enabled + + filter_strictly_plugins( + Map.put(result, :disabled, :rabbit_plugins.strictly_plugins(disabled, all)), + all, + [:set, :started, :stopped] + ) + + other -> + other + end + end) + ])} + + {:error, _} = err -> + err + end + end + + use RabbitMQ.CLI.Plugins.ErrorOutput + + def banner([], %{all: true, node: node_name}) do + "Disabling ALL plugins on node #{node_name}" + end + + def banner(plugins, %{node: node_name}) do + ["Disabling plugins on node #{node_name}:" | plugins] + end + + def usage, do: "disable <plugin1> [ <plugin2>] | --all [--offline] [--online]" + + def usage_additional() do + [ + ["<plugin1> [ <plugin2>]", "names of plugins to disable separated by a space"], + ["--online", "contact target node to disable the plugins. Changes are applied immediately."], + ["--offline", "update enabled plugins file directly without contacting target node. Changes will be delayed until the node is restarted."], + ["--all", "disable all currently enabled plugins"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.plugins() + ] + end + + def help_section(), do: :plugin_management + + def description(), do: "Disables one or more plugins" + + # + # Implementation + # + + defp filter_strictly_plugins(map, _all, []) do + map + end + + defp filter_strictly_plugins(map, all, [head | tail]) do + case map[head] do + nil -> + filter_strictly_plugins(map, all, tail) + + other -> + value = :rabbit_plugins.strictly_plugins(other, all) + filter_strictly_plugins(Map.put(map, head, value), all, tail) + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex new file mode 100644 index 0000000000..530a2cbb6a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/enable_command.ex @@ -0,0 +1,156 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Commands.EnableCommand do + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + import RabbitMQ.CLI.Core.{CodePath, Paths} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def formatter(), do: RabbitMQ.CLI.Formatters.Plugins + + def merge_defaults(args, opts) do + {args, Map.merge(%{online: false, offline: false, all: false}, opts)} + end + + def distribution(%{offline: true}), do: :none + def distribution(%{offline: false}), do: :cli + + def switches(), do: [online: :boolean, offline: :boolean, all: :boolean] + + def validate([], %{all: false}) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _], %{all: true}) do + {:validation_failure, {:bad_argument, "Cannot set both --all and a list of plugins"}} + end + + def validate(_, %{online: true, offline: true}) do + {:validation_failure, {:bad_argument, "Cannot set both online and offline"}} + end + + def validate(_, _) do + :ok + end + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &PluginHelpers.can_set_plugins_with_mode/2, + &require_rabbit_and_plugins/2, + &PluginHelpers.enabled_plugins_file/2, + &plugins_dir/2 + ], + [args, opts] + ) + end + + def run(plugin_names, %{all: all_flag} = opts) do + plugins = + case all_flag do + false -> for s <- plugin_names, do: String.to_atom(s) + true -> PluginHelpers.plugin_names(PluginHelpers.list(opts)) + end + + case PluginHelpers.validate_plugins(plugins, opts) do + :ok -> do_run(plugins, opts) + other -> other + end + end + + use RabbitMQ.CLI.Plugins.ErrorOutput + + def banner([], %{all: true, node: node_name}) do + "Enabling ALL plugins on node #{node_name}" + end + + def banner(plugins, %{node: node_name}) do + ["Enabling plugins on node #{node_name}:" | plugins] + end + + def usage, do: "enable <plugin1> [ <plugin2>] | --all [--offline] [--online]" + + def usage_additional() do + [ + ["<plugin1> [ <plugin2>]", "names of plugins to enable separated by a space"], + ["--online", "contact target node to enable the plugins. Changes are applied immediately."], + ["--offline", "update enabled plugins file directly without contacting target node. Changes will be delayed until the node is restarted."], + ["--all", "enable all available plugins. Not recommended as some plugins may conflict or otherwise be incompatible!"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.plugins() + ] + end + + def help_section(), do: :plugin_management + + def description(), do: "Enables one or more plugins" + + # + # Implementation + # + + def do_run(plugins, %{node: node_name} = opts) do + enabled = PluginHelpers.read_enabled(opts) + all = PluginHelpers.list(opts) + implicit = :rabbit_plugins.dependencies(false, enabled, all) + enabled_implicitly = MapSet.difference(MapSet.new(implicit), MapSet.new(enabled)) + + plugins_to_set = + MapSet.union( + MapSet.new(enabled), + MapSet.difference(MapSet.new(plugins), enabled_implicitly) + ) + + mode = PluginHelpers.mode(opts) + + case PluginHelpers.set_enabled_plugins(MapSet.to_list(plugins_to_set), opts) do + {:ok, enabled_plugins} -> + {:stream, + Stream.concat([ + [:rabbit_plugins.strictly_plugins(enabled_plugins, all)], + RabbitMQ.CLI.Core.Helpers.defer(fn -> + case PluginHelpers.update_enabled_plugins(enabled_plugins, mode, node_name, opts) do + %{set: new_enabled} = result -> + enabled = new_enabled -- implicit + + filter_strictly_plugins( + Map.put(result, :enabled, :rabbit_plugins.strictly_plugins(enabled, all)), + all, + [:set, :started, :stopped] + ) + + other -> + other + end + end) + ])} + + {:error, _} = err -> + err + end + end + + defp filter_strictly_plugins(map, _all, []) do + map + end + + defp filter_strictly_plugins(map, all, [head | tail]) do + case map[head] do + nil -> + filter_strictly_plugins(map, all, tail) + + other -> + value = :rabbit_plugins.strictly_plugins(other, all) + filter_strictly_plugins(Map.put(map, head, value), all, tail) + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/is_enabled.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/is_enabled.ex new file mode 100644 index 0000000000..fa54b1eee3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/is_enabled.ex @@ -0,0 +1,154 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Commands.IsEnabledCommand do + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + import RabbitMQ.CLI.Core.{CodePath, Paths} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, %{offline: true} = opts) do + {args, opts} + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{online: true, offline: false}, opts)} + end + + def distribution(%{offline: true}), do: :none + def distribution(%{offline: false}), do: :cli + + def switches(), do: [online: :boolean, offline: :boolean] + + def validate(_, %{online: true, offline: true}) do + {:validation_failure, {:bad_argument, "Cannot set both online and offline"}} + end + + def validate(_, %{online: false, offline: false}) do + {:validation_failure, {:bad_argument, "Cannot set online and offline to false"}} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + + def validate([_ | _], _) do + :ok + end + + def validate_execution_environment(args, %{offline: true} = opts) do + Validators.chain( + [ + &require_rabbit_and_plugins/2, + &PluginHelpers.enabled_plugins_file/2, + &plugins_dir/2 + ], + [args, opts] + ) + end + + def validate_execution_environment(args, %{online: true} = opts) do + Validators.node_is_running(args, opts) + end + + def run(args, %{online: true, node: node_name} = opts) do + case :rabbit_misc.rpc_call(node_name, :rabbit_plugins, :active, []) do + {:error, _} = e -> + e + + plugins -> + plugins = Enum.map(plugins, &Atom.to_string/1) |> Enum.sort() + + case Enum.filter(args, fn x -> not Enum.member?(plugins, x) end) do + [] -> {:ok, positive_result_message(args, opts)} + xs -> {:error, negative_result_message(xs, opts, plugins)} + end + end + end + + def run(args, %{offline: true} = opts) do + plugins = PluginHelpers.list_names(opts) |> Enum.map(&Atom.to_string/1) |> Enum.sort() + + case Enum.filter(args, fn x -> not Enum.member?(plugins, x) end) do + [] -> {:ok, positive_result_message(args, opts)} + xs -> {:error, negative_result_message(xs, opts, plugins)} + end + end + + def output({:ok, msg}, %{formatter: "json"}) do + {:ok, %{"result" => "ok", "message" => msg}} + end + + def output({:error, msg}, %{formatter: "json"}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_unavailable(), + %{"result" => "error", "message" => msg}} + end + + def output({:error, err}, _opts) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_unavailable(), err} + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "is_enabled <plugin1> [ <plugin2>] [--offline] [--online]" + + def usage_additional() do + [ + ["<plugin1> [ <plugin2>]", "names of plugins to check separated by a space"], + ["--online", "contact target node to perform the check. Requires the node to be running and reachable."], + ["--offline", "check enabled plugins file directly without contacting target node."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.plugins() + ] + end + + def banner(args, %{offline: true}) do + "Inferring if #{plugin_or_plugins(args)} from local environment..." + end + + def banner(args, %{online: true, node: node}) do + "Asking node #{node} if #{plugin_or_plugins(args)} enabled..." + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Health check that exits with a non-zero code if provided plugins are not enabled on target node" + + # + # Implementation + # + + def plugin_or_plugins(args) when length(args) == 1 do + "plugin #{PluginHelpers.comma_separated_names(args)} is" + end + + def plugin_or_plugins(args) when length(args) > 1 do + "plugins #{PluginHelpers.comma_separated_names(args)} are" + end + + defp positive_result_message(args, %{online: true, node: node_name}) do + String.capitalize("#{plugin_or_plugins(args)} enabled on node #{node_name}") + end + + defp positive_result_message(args, %{offline: true}) do + String.capitalize("#{plugin_or_plugins(args)} enabled") + end + + defp negative_result_message(missing, %{online: true, node: node_name}, plugins) do + String.capitalize("#{plugin_or_plugins(missing)} not enabled on node #{node_name}. ") <> + "Enabled plugins and dependencies: #{PluginHelpers.comma_separated_names(plugins)}" + end + + defp negative_result_message(missing, %{offline: true}, plugins) do + String.capitalize("#{plugin_or_plugins(missing)} not enabled. ") <> + "Enabled plugins and dependencies: #{PluginHelpers.comma_separated_names(plugins)}" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex new file mode 100644 index 0000000000..a4e943a149 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/list_command.ex @@ -0,0 +1,188 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do + import RabbitCommon.Records + + alias RabbitMQ.CLI.Core.{DocGuide, Validators} + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + import RabbitMQ.CLI.Core.{CodePath, Paths} + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Plugins + + def merge_defaults([], opts), do: merge_defaults([".*"], opts) + def merge_defaults(args, opts), do: {args, Map.merge(default_opts(), opts)} + + def switches(), + do: [verbose: :boolean, minimal: :boolean, enabled: :boolean, implicitly_enabled: :boolean] + + def aliases(), do: [v: :verbose, m: :minimal, E: :enabled, e: :implicitly_enabled] + + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate(_, %{verbose: true, minimal: true}) do + {:validation_failure, {:bad_argument, "Cannot set both verbose and minimal"}} + end + + def validate(_, _) do + :ok + end + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &require_rabbit_and_plugins/2, + &PluginHelpers.enabled_plugins_file/2, + &plugins_dir/2 + ], + [args, opts] + ) + end + + def run([pattern], %{node: node_name} = opts) do + %{verbose: verbose, minimal: minimal, enabled: only_enabled, implicitly_enabled: all_enabled} = + opts + + all = PluginHelpers.list(opts) + enabled = PluginHelpers.read_enabled(opts) + + missing = MapSet.difference(MapSet.new(enabled), MapSet.new(PluginHelpers.plugin_names(all))) + + case Enum.empty?(missing) do + true -> + :ok + + false -> + names = Enum.join(Enum.to_list(missing), ", ") + IO.puts("WARNING - plugins currently enabled but missing: #{names}\n") + end + + implicit = :rabbit_plugins.dependencies(false, enabled, all) + enabled_implicitly = implicit -- enabled + + {status, running} = + case remote_running_plugins(node_name) do + :error -> {:node_down, []} + {:ok, active} -> {:running, active} + end + + {:ok, re} = Regex.compile(pattern) + + format = + case {verbose, minimal} do + {true, false} -> :verbose + {false, true} -> :minimal + {false, false} -> :normal + end + + plugins = + Enum.filter( + all, + fn plugin -> + name = PluginHelpers.plugin_name(plugin) + + :rabbit_plugins.is_strictly_plugin(plugin) and + Regex.match?(re, to_string(name)) and + cond do + only_enabled -> Enum.member?(enabled, name) + all_enabled -> Enum.member?(enabled ++ enabled_implicitly, name) + true -> true + end + end + ) + + %{ + status: status, + format: format, + plugins: format_plugins(plugins, format, enabled, enabled_implicitly, running) + } + end + + def banner([pattern], _), do: "Listing plugins with pattern \"#{pattern}\" ..." + + def usage, do: "list [pattern] [--verbose] [--minimal] [--enabled] [--implicitly-enabled]" + + def usage_additional() do + [ + ["<pattern>", "only list plugins that match a regular expression pattern"], + ["--verbose", "output more information"], + ["--minimal", "only print plugin names. Most useful in compbination with --silent and --enabled."], + ["--enabled", "only list enabled plugins"], + ["--implicitly-enabled", "include plugins enabled as dependencies of other plugins"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.plugins() + ] + end + + def help_section(), do: :plugin_management + + def description(), do: "Lists plugins and their state" + + # + # Implementation + # + + defp remote_running_plugins(node) do + case :rabbit_misc.rpc_call(node, :rabbit_plugins, :running_plugins, []) do + {:badrpc, _} -> :error + active_with_version -> active_with_version + end + end + + defp format_plugins(plugins, format, enabled, enabled_implicitly, running) do + plugins + |> sort_plugins + |> Enum.map(fn plugin -> + format_plugin(plugin, format, enabled, enabled_implicitly, running) + end) + end + + defp sort_plugins(plugins) do + Enum.sort_by(plugins, &PluginHelpers.plugin_name/1) + end + + defp format_plugin(plugin, :minimal, _, _, _) do + %{name: PluginHelpers.plugin_name(plugin)} + end + + defp format_plugin(plugin, :normal, enabled, enabled_implicitly, running) do + plugin(name: name, version: version) = plugin + + enabled_mode = + case {Enum.member?(enabled, name), Enum.member?(enabled_implicitly, name)} do + {true, false} -> :enabled + {false, true} -> :implicit + {false, false} -> :not_enabled + end + + %{ + name: name, + version: version, + running_version: running[name], + enabled: enabled_mode, + running: Keyword.has_key?(running, name) + } + end + + defp format_plugin(plugin, :verbose, enabled, enabled_implicitly, running) do + normal = format_plugin(plugin, :normal, enabled, enabled_implicitly, running) + plugin(dependencies: dependencies, description: description) = plugin + Map.merge(normal, %{dependencies: dependencies, description: description}) + end + + defp default_opts() do + %{minimal: false, verbose: false, enabled: false, implicitly_enabled: false} + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex new file mode 100644 index 0000000000..68b442a547 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/commands/set_command.ex @@ -0,0 +1,133 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Commands.SetCommand do + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Validators} + import RabbitMQ.CLI.Core.{CodePath, Paths} + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def formatter(), do: RabbitMQ.CLI.Formatters.Plugins + + def merge_defaults(args, opts) do + {args, Map.merge(%{online: false, offline: false}, opts)} + end + + def distribution(%{offline: true}), do: :none + def distribution(%{offline: false}), do: :cli + + def switches(), do: [online: :boolean, offline: :boolean] + + def validate(_, %{online: true, offline: true}) do + {:validation_failure, {:bad_argument, "Cannot set both online and offline"}} + end + + def validate(_, _) do + :ok + end + + def validate_execution_environment(args, opts) do + Validators.chain( + [ + &PluginHelpers.can_set_plugins_with_mode/2, + &require_rabbit_and_plugins/2, + &PluginHelpers.enabled_plugins_file/2, + &plugins_dir/2 + ], + [args, opts] + ) + end + + def run(plugin_names, opts) do + plugins = for s <- plugin_names, do: String.to_atom(s) + + case PluginHelpers.validate_plugins(plugins, opts) do + :ok -> do_run(plugins, opts) + other -> other + end + end + + def output({:error, {:plugins_not_found, missing}}, _opts) do + {:error, ExitCodes.exit_dataerr(), + "The following plugins were not found: #{Enum.join(Enum.to_list(missing), ", ")}"} + end + + use RabbitMQ.CLI.Plugins.ErrorOutput + + def banner(plugins, %{node: node_name}) do + ["Enabling plugins on node #{node_name}:" | plugins] + end + + def usage, do: "set <plugin1> [ <plugin2>] [--offline] [--online]" + + def usage_additional() do + [ + ["<plugin1> [ <plugin2>]", "names of plugins to enable separated by a space. All other plugins will be disabled."], + ["--online", "contact target node to enable the plugins. Changes are applied immediately."], + ["--offline", "update enabled plugins file directly without contacting target node. Changes will be delayed until the node is restarted."] + ] + end + + def usage_doc_guides() do + [ + DocGuide.plugins() + ] + end + + def help_section(), do: :plugin_management + + def description(), do: "Enables one or more plugins, disables the rest" + + # + # Implementation + # + + def do_run(plugins, %{node: node_name} = opts) do + all = PluginHelpers.list(opts) + mode = PluginHelpers.mode(opts) + + case PluginHelpers.set_enabled_plugins(plugins, opts) do + {:ok, enabled_plugins} -> + {:stream, + Stream.concat([ + [:rabbit_plugins.strictly_plugins(enabled_plugins, all)], + RabbitMQ.CLI.Core.Helpers.defer(fn -> + case PluginHelpers.update_enabled_plugins( + enabled_plugins, + mode, + node_name, + opts + ) do + %{set: _} = map -> + filter_strictly_plugins(map, all, [:set, :started, :stopped]) + + {:error, _} = err -> + err + end + end) + ])} + + {:error, _} = err -> + err + end + end + + defp filter_strictly_plugins(map, _all, []) do + map + end + + defp filter_strictly_plugins(map, all, [head | tail]) do + case map[head] do + nil -> + filter_strictly_plugins(map, all, tail) + + other -> + value = :rabbit_plugins.strictly_plugins(other, all) + filter_strictly_plugins(Map.put(map, head, value), all, tail) + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/error_output.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/error_output.ex new file mode 100644 index 0000000000..51c75ed99a --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/error_output.ex @@ -0,0 +1,55 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +# Default output implementation for plugin commands +defmodule RabbitMQ.CLI.Plugins.ErrorOutput do + alias RabbitMQ.CLI.Core.ExitCodes + + defmacro __using__(_) do + quote do + def output({:error, {:enabled_plugins_mismatch, cli_path, node_path}}, opts) do + {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at #{cli_path}: target node #{opts[:node]} uses a different path (#{ + node_path + })"} + end + + def output({:error, {:cannot_read_enabled_plugins_file, path, :eacces}}, _opts) do + {:error, ExitCodes.exit_dataerr(), + "Could not read enabled plugins file at #{path}: the file does not exist or permission was denied (EACCES)"} + end + + def output({:error, {:cannot_read_enabled_plugins_file, path, :enoent}}, _opts) do + {:error, ExitCodes.exit_dataerr(), + "Could not read enabled plugins file at #{path}: the file does not exist (ENOENT)"} + end + + def output({:error, {:cannot_write_enabled_plugins_file, path, :eacces}}, _opts) do + {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at #{path}: the file does not exist or permission was denied (EACCES)"} + end + + def output({:error, {:cannot_write_enabled_plugins_file, path, :enoent}}, _opts) do + {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at #{path}: the file does not exist (ENOENT)"} + end + + def output({:error, err}, _opts) do + {:error, ExitCodes.exit_software(), err} + end + + def output({:stream, stream}, _opts) do + {:stream, stream} + end + end + + # quote + end + + # defmacro +end + +# defmodule diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/plugins_helpers.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/plugins_helpers.ex new file mode 100644 index 0000000000..bf8b4f772b --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/plugins/plugins_helpers.ex @@ -0,0 +1,232 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Plugins.Helpers do + import RabbitMQ.CLI.Core.DataCoercion + import RabbitCommon.Records + import RabbitMQ.CLI.Core.Platform, only: [path_separator: 0] + import RabbitMQ.CLI.Core.{CodePath, Paths} + alias RabbitMQ.CLI.Core.{Config, Validators} + + def mode(opts) do + %{online: online, offline: offline} = opts + + case {online, offline} do + {true, false} -> :online + {false, true} -> :offline + {false, false} -> :best_effort + end + end + + def can_set_plugins_with_mode(args, opts) do + case mode(opts) do + # can always set offline plugins list + :offline -> + :ok + + # assume online mode, fall back to offline mode in case of errors + :best_effort -> + :ok + + # a running node is required + :online -> + Validators.chain( + [&Validators.node_is_running/2, &Validators.rabbit_is_running/2], + [args, opts] + ) + end + end + + def list(opts) do + {:ok, dir} = plugins_dir(opts) + add_all_to_path(dir) + :lists.usort(:rabbit_plugins.list(to_charlist(dir))) + end + + def list_names(opts) do + list(opts) |> plugin_names + end + + def read_enabled(opts) do + case enabled_plugins_file(opts) do + {:ok, enabled} -> + :rabbit_plugins.read_enabled(to_charlist(enabled)) + + # Existence of enabled_plugins_file should be validated separately + {:error, :no_plugins_file} -> + [] + end + end + + def enabled_plugins_file(opts) do + case Config.get_option(:enabled_plugins_file, opts) do + nil -> {:error, :no_plugins_file} + file -> {:ok, file} + end + end + + def enabled_plugins_file(_, opts) do + enabled_plugins_file(opts) + end + + def set_enabled_plugins(plugins, opts) do + plugin_atoms = :lists.usort(for plugin <- plugins, do: to_atom(plugin)) + require_rabbit_and_plugins(opts) + {:ok, plugins_file} = enabled_plugins_file(opts) + write_enabled_plugins(plugin_atoms, plugins_file, opts) + end + + @spec update_enabled_plugins( + [atom()], + :online | :offline | :best_effort, + node(), + map() + ) :: map() | {:error, any()} + def update_enabled_plugins(enabled_plugins, mode, node_name, opts) do + {:ok, plugins_file} = enabled_plugins_file(opts) + + case mode do + :online -> + case update_enabled_plugins(node_name, plugins_file) do + {:ok, started, stopped} -> + %{ + mode: :online, + started: Enum.sort(started), + stopped: Enum.sort(stopped), + set: Enum.sort(enabled_plugins) + } + + {:error, _} = err -> + err + end + + :best_effort -> + case update_enabled_plugins(node_name, plugins_file) do + {:ok, started, stopped} -> + %{ + mode: :online, + started: Enum.sort(started), + stopped: Enum.sort(stopped), + set: Enum.sort(enabled_plugins) + } + + {:error, :offline} -> + %{mode: :offline, set: Enum.sort(enabled_plugins)} + + {:error, {:enabled_plugins_mismatch, _, _}} = err -> + err + end + + :offline -> + %{mode: :offline, set: Enum.sort(enabled_plugins)} + end + end + + def validate_plugins(requested_plugins, opts) do + ## Maybe check all plugins + plugins = + case opts do + %{all: true} -> plugin_names(list(opts)) + _ -> requested_plugins + end + + all = list(opts) + deps = :rabbit_plugins.dependencies(false, plugins, all) + + deps_plugins = + Enum.filter(all, fn plugin -> + name = plugin_name(plugin) + Enum.member?(deps, name) + end) + + case :rabbit_plugins.validate_plugins(deps_plugins) do + {_, []} -> :ok + {_, invalid} -> {:error, :rabbit_plugins.format_invalid_plugins(invalid)} + end + end + + def plugin_name(plugin) when is_binary(plugin) do + plugin + end + + def plugin_name(plugin) when is_atom(plugin) do + Atom.to_string(plugin) + end + + def plugin_name(plugin) do + plugin(name: name) = plugin + name + end + + def plugin_names(plugins) do + for plugin <- plugins, do: plugin_name(plugin) + end + + def comma_separated_names(plugins) do + Enum.join(plugin_names(plugins), ", ") + end + + # + # Implementation + # + + defp to_list(str) when is_binary(str) do + :erlang.binary_to_list(str) + end + + defp to_list(lst) when is_list(lst) do + lst + end + + defp to_list(atm) when is_atom(atm) do + to_list(Atom.to_string(atm)) + end + + defp write_enabled_plugins(plugins, plugins_file, opts) do + all = list(opts) + all_plugin_names = Enum.map(all, &plugin_name/1) + missing = MapSet.difference(MapSet.new(plugins), MapSet.new(all_plugin_names)) + + case Enum.empty?(missing) do + true -> + case :rabbit_file.write_term_file(to_charlist(plugins_file), [plugins]) do + :ok -> + all_enabled = :rabbit_plugins.dependencies(false, plugins, all) + {:ok, Enum.sort(all_enabled)} + + {:error, reason} -> + {:error, {:cannot_write_enabled_plugins_file, plugins_file, reason}} + end + + false -> + {:error, {:plugins_not_found, Enum.to_list(missing)}} + end + end + + defp update_enabled_plugins(node_name, plugins_file) do + case :rabbit_misc.rpc_call(node_name, :rabbit_plugins, :ensure, [to_list(plugins_file)]) do + {:badrpc, :nodedown} -> {:error, :offline} + {:error, :rabbit_not_running} -> {:error, :offline} + {:ok, start, stop} -> {:ok, start, stop} + {:error, _} = err -> err + end + end + + defp add_all_to_path(plugins_directories) do + directories = String.split(to_string(plugins_directories), path_separator()) + + Enum.map(directories, fn directory -> + with {:ok, subdirs} <- File.ls(directory) do + for subdir <- subdirs do + Path.join([directory, subdir, "ebin"]) + |> Code.append_path() + end + end + end) + + :ok + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/printer_behaviour.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/printer_behaviour.ex new file mode 100644 index 0000000000..b2bedfdaad --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/printer_behaviour.ex @@ -0,0 +1,21 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.PrinterBehaviour do + @callback init(options :: map()) :: {:ok, printer_state :: any} | {:error, error :: any} + @callback finish(printer_state :: any) :: :ok + + @callback print_output(output :: String.t() | [String.t()], printer_state :: any) :: :ok + @callback print_ok(printer_state :: any) :: :ok + + def module_name(nil) do + nil + end + def module_name(printer) do + mod = printer |> String.downcase |> Macro.camelize + String.to_atom("RabbitMQ.CLI.Printers." <> mod) + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/file.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/file.ex new file mode 100644 index 0000000000..ba0daaeebb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/file.ex @@ -0,0 +1,36 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Printers.File do + @behaviour RabbitMQ.CLI.PrinterBehaviour + + def init(options) do + file = options[:file] + + case File.open(file) do + {:ok, io_device} -> {:ok, %{device: io_device}} + {:error, err} -> {:error, err} + end + end + + def finish(%{device: io_device}) do + :ok = File.close(io_device) + end + + def print_output(output, %{device: io_device}) when is_list(output) do + for line <- output do + IO.puts(io_device, line) + end + end + + def print_output(output, %{device: io_device}) do + IO.puts(io_device, output) + end + + def print_ok(_) do + :ok + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/std_io.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/std_io.ex new file mode 100644 index 0000000000..206feff56d --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/std_io.ex @@ -0,0 +1,28 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Printers.StdIO do + @behaviour RabbitMQ.CLI.PrinterBehaviour + + def init(_), do: {:ok, :ok} + def finish(_), do: :ok + + def print_output(nil, _), do: :ok + + def print_output(output, _) when is_list(output) do + for line <- output do + IO.puts(line) + end + end + + def print_output(output, _) do + IO.puts(output) + end + + def print_ok(_) do + :ok + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/std_io_raw.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/std_io_raw.ex new file mode 100644 index 0000000000..16846907b4 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/printers/std_io_raw.ex @@ -0,0 +1,28 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Printers.StdIORaw do + @behaviour RabbitMQ.CLI.PrinterBehaviour + + def init(_), do: {:ok, :ok} + def finish(_), do: :ok + + def print_output(nil, _), do: :ok + + def print_output(output, _) when is_list(output) do + for line <- output do + IO.write(line) + end + end + + def print_output(output, _) do + IO.write(output) + end + + def print_ok(_) do + :ok + end +end 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 new file mode 100644 index 0000000000..e789d00343 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/add_member_command.ex @@ -0,0 +1,70 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.AddMemberCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 5_000 + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + other -> other + end + {args, Map.merge(%{vhost: "/", timeout: timeout}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + 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, [ + vhost, + name, + to_atom(node), + timeout + ]) do + {:error, :classic_queue_not_supported} -> + {:error, "Cannot add members to a classic queue"} + + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "add_member [--vhost <vhost>] <queue> <node>" + + def usage_additional do + [ + ["<queue>", "quorum queue name"], + ["<node>", "node to add a new replica on"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def help_section, do: :replication + + def description, do: "Adds a quorum queue member (replica) on the given node." + + def banner([name, node], _) do + [ + "Adding a replica for queue #{name} on node #{node}..." + ] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/check_if_node_is_mirror_sync_critical_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/check_if_node_is_mirror_sync_critical_command.ex new file mode 100644 index 0000000000..c31b83d29c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/check_if_node_is_mirror_sync_critical_command.ex @@ -0,0 +1,105 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.CheckIfNodeIsMirrorSyncCriticalCommand do + @moduledoc """ + Exits with a non-zero code if there are classic mirrored queues that don't + have any in sync mirrors online and would potentially lose data + if the target node is shut down. + + This command is meant to be used as a pre-upgrade (pre-shutdown) check. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + def scopes(), do: [:diagnostics, :queues] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, + :rabbit_nodes, :is_single_node_cluster, [], timeout) do + # if target node is the only one in the cluster, the check makes little sense + # and false positives can be misleading + true -> {:ok, :single_node_cluster} + false -> + case :rabbit_misc.rpc_call(node_name, + :rabbit_amqqueue, :list_local_mirrored_classic_without_synchronised_mirrors_for_cli, [], timeout) do + [] -> {:ok, []} + qs when is_list(qs) -> {:ok, qs} + other -> other + end + other -> other + end + end + + def output({:ok, :single_node_cluster}, %{formatter: "json"}) do + {:ok, %{ + "result" => "ok", + "message" => "Target node seems to be the only one in a single node cluster, the check does not apply" + }} + end + def output({:ok, []}, %{formatter: "json"}) do + {:ok, %{"result" => "ok"}} + end + def output({:ok, :single_node_cluster}, %{silent: true}) do + {:ok, :check_passed} + end + def output({:ok, []}, %{silent: true}) do + {:ok, :check_passed} + end + def output({:ok, :single_node_cluster}, %{node: node_name}) do + {:ok, "Node #{node_name} seems to be the only one in a single node cluster, the check does not apply"} + end + def output({:ok, []}, %{node: node_name}) do + {:ok, "Node #{node_name} reported no classic mirrored queues without online synchronised mirrors"} + end + def output({:ok, qs}, %{node: node_name, formatter: "json"}) when is_list(qs) do + {:error, :check_failed, + %{ + "result" => "error", + "queues" => qs, + "message" => "Node #{node_name} reported local classic mirrored queues without online synchronised mirrors" + }} + end + def output({:ok, qs}, %{silent: true}) when is_list(qs) do + {:error, :check_failed} + end + def output({:ok, qs}, %{node: node_name}) when is_list(qs) do + lines = queue_lines(qs, node_name) + + {:error, :check_failed, Enum.join(lines, line_separator())} + end + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description() do + "Health check that exits with a non-zero code if there are classic mirrored queues " <> + "without online synchronised mirrors (queues that would potentially lose data if the target node is shut down)" + end + + def usage, do: "check_if_node_is_mirror_sync_critical" + + def banner([], %{node: node_name}) do + "Checking if node #{node_name} is critical for data safety of any classic mirrored queues ..." + end + + # + # Implementation + # + + def queue_lines(qs, node_name) do + for q <- qs do + "#{q["readable_name"]} would lose its only synchronised replica (master) if node #{node_name} is stopped" + end + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/check_if_node_is_quorum_critical_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/check_if_node_is_quorum_critical_command.ex new file mode 100644 index 0000000000..d8f4a34c1c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/check_if_node_is_quorum_critical_command.ex @@ -0,0 +1,118 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.CheckIfNodeIsQuorumCriticalCommand do + @moduledoc """ + Exits with a non-zero code if there are quorum queues that would lose their quorum + if the target node is shut down. + + This command is meant to be used as a pre-upgrade (pre-shutdown) check. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] + + def scopes(), do: [:diagnostics, :queues] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_nodes, :is_single_node_cluster, [], timeout) do + # if target node is the only one in the cluster, the check makes little sense + # and false positives can be misleading + true -> {:ok, :single_node_cluster} + false -> + case :rabbit_misc.rpc_call(node_name, :rabbit_maintenance, :is_being_drained_local_read, [node_name]) do + # if target node is under maintenance, it has already transferred all of its quorum queue + # replicas. Don't consider it to be quorum critical. See rabbitmq/rabbitmq-server#2469 + true -> {:ok, :under_maintenance} + false -> + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :list_with_minimum_quorum_for_cli, [], timeout) do + [] -> {:ok, []} + qs when is_list(qs) -> {:ok, qs} + other -> other + end + end + other -> other + end + end + + def output({:ok, :single_node_cluster}, %{formatter: "json"}) do + {:ok, %{ + "result" => "ok", + "message" => "Target node seems to be the only one in a single node cluster, the check does not apply" + }} + end + def output({:ok, :under_maintenance}, %{formatter: "json"}) do + {:ok, %{ + "result" => "ok", + "message" => "Target node seems to be in maintenance mode, the check does not apply" + }} + end + def output({:ok, []}, %{formatter: "json"}) do + {:ok, %{"result" => "ok"}} + end + def output({:ok, :single_node_cluster}, %{silent: true}) do + {:ok, :check_passed} + end + def output({:ok, :under_maintenance}, %{silent: true}) do + {:ok, :check_passed} + end + def output({:ok, []}, %{silent: true}) do + {:ok, :check_passed} + end + def output({:ok, :single_node_cluster}, %{node: node_name}) do + {:ok, "Node #{node_name} seems to be the only one in a single node cluster, the check does not apply"} + end + def output({:ok, :under_maintenance}, %{node: node_name}) do + {:ok, "Node #{node_name} seems to be in maintenance mode, the check does not apply"} + end + def output({:ok, []}, %{node: node_name}) do + {:ok, "Node #{node_name} reported no quorum queues with minimum quorum"} + end + def output({:ok, qs}, %{node: node_name, formatter: "json"}) when is_list(qs) do + {:error, :check_failed, + %{ + "result" => "error", + "queues" => qs, + "message" => "Node #{node_name} reported local queues with minimum online quorum" + }} + end + def output({:ok, qs}, %{silent: true}) when is_list(qs) do + {:error, :check_failed} + end + def output({:ok, qs}, %{node: node_name}) when is_list(qs) do + lines = queue_lines(qs, node_name) + + {:error, :check_failed, Enum.join(lines, line_separator())} + end + use RabbitMQ.CLI.DefaultOutput + + def help_section(), do: :observability_and_health_checks + + def description() do + "Health check that exits with a non-zero code if there are queues " <> + "with minimum online quorum (queues that would lose their quorum if the target node is shut down)" + end + + def usage, do: "check_if_node_is_quorum_critical" + + def banner([], %{node: node_name}) do + "Checking if node #{node_name} is critical for quorum of any quorum queues ..." + end + + # + # Implementation + # + + def queue_lines(qs, node_name) do + for q <- qs, do: "#{q["readable_name"]} would lose quorum if node #{node_name} is stopped" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/delete_member_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/delete_member_command.ex new file mode 100644 index 0000000000..1579bf6809 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/delete_member_command.ex @@ -0,0 +1,58 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.DeleteMemberCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, node] = _args, %{vhost: vhost, node: node_name}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :delete_member, [ + vhost, + name, + to_atom(node) + ]) do + {:error, :classic_queue_not_supported} -> + {:error, "Cannot add members to a classic queue"} + + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "delete_member [--vhost <vhost>] <queue> <node>" + + def usage_additional do + [ + ["<queue>", "quorum queue name"], + ["<node>", "node to remove a new replica on"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def help_section, do: :replication + + def description, do: "Removes a quorum queue member (replica) on the given node." + + def banner([name, node], _) do + "Removing a replica of queue #{name} on node #{node}..." + end +end 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 new file mode 100644 index 0000000000..4e0ce903fe --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/grow_command.ex @@ -0,0 +1,126 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + defp default_opts, do: %{vhost_pattern: ".*", + queue_pattern: ".*", + errors_only: false} + + def switches(), + do: [ + vhost_pattern: :string, + queue_pattern: :string, + errors_only: :boolean + ] + + def merge_defaults(args, opts) do + {args, Map.merge(default_opts(), opts)} + end + + def validate(args, _) when length(args) < 2 do + {:validation_failure, :not_enough_args} + end + + def validate(args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + def validate([_, s], _) do + case s do + "all" -> :ok + "even" -> :ok + _ -> + {:validation_failure, "strategy '#{s}' is not recognised."} + end + end + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([node, strategy], %{node: node_name, + vhost_pattern: vhost_pat, + queue_pattern: queue_pat, + errors_only: errors_only}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :grow, [ + to_atom(node), + vhost_pat, + queue_pat, + to_atom(strategy)]) do + {:error, _} = error -> error; + {:badrpc, _} = error -> error; + results when errors_only -> + for {{:resource, vhost, _kind, name}, {:errors, _, _} = res} <- results, + do: [{:vhost, vhost}, + {:name, name}, + {:size, format_size res}, + {:result, format_result res}] + results -> + for {{:resource, vhost, _kind, name}, res} <- results, + do: [{:vhost, vhost}, + {:name, name}, + {:size, format_size res}, + {:result, format_result res}] + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def usage, do: "grow <node> <all | even> [--vhost-pattern <pattern>] [--queue-pattern <pattern>]" + + def usage_additional do + [ + ["<node>", "node name to place replicas on"], + ["<all | even>", "how many matching quorum queues should have a replica added on this node: all or half (evenly numbered)?"], + ["--queue-pattern <pattern>", "regular expression to match queue names"], + ["--vhost-pattern <pattern>", "regular expression to match virtual host names"], + ["--errors-only", "only list queues which reported an error"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def help_section, do: :cluster_management + + def description, do: "Grows quorum queue clusters by adding a member (replica) to all or half of matching quorum queues on the given node." + + def banner([node, strategy], _) do + "Growing #{strategy} quorum queues on #{node}..." + end + + # + # Implementation + # + + defp format_size({:ok, size}) do + size + end + defp format_size({:error, _size, :timeout}) do + # the actual size is uncertain here + "?" + end + defp format_size({:error, size, _}) do + size + end + + defp format_result({:ok, _size}) do + "ok" + end + defp format_result({:error, _size, :timeout}) do + "error: the operation timed out and may not have been completed" + end + defp format_result({:error, _size, err}) do + :io.format "error: ~W", [err, 10] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/peek_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/peek_command.ex new file mode 100644 index 0000000000..a159c119e9 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/peek_command.ex @@ -0,0 +1,112 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.PeekCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + def scopes(), do: [:queues] + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + + use RabbitMQ.CLI.Core.MergesDefaultVirtualHost + + def validate(args, _) when length(args) < 2 do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 2 do + {:validation_failure, :too_many_args} + end + def validate([_, raw_pos], _) do + pos = case Integer.parse(raw_pos) do + {n, _} -> n + :error -> :error + _ -> :error + end + + invalid_pos = {:validation_failure, "position value must be a positive integer"} + case pos do + :error -> invalid_pos + num when num < 1 -> invalid_pos + num when num >= 1 -> :ok + end + end + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, pos] = _args, %{node: node_name, vhost: vhost}) do + {pos, _} = Integer.parse(pos) + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :peek, [vhost, name, pos]) do + {:error, :classic_queue_not_supported} -> + {:error, "Cannot peek into a classic queue"} + {:ok, msg} -> + {:ok, msg} + err -> + err + end + end + + def output({:error, :not_found}, %{vhost: vhost, formatter: "json"}) do + {:error, + %{ + "result" => "error", + "message" => "Target queue was not found in virtual host '#{vhost}'" + }} + end + def output({:error, :no_message_at_pos}, %{formatter: "json"}) do + {:error, + %{ + "result" => "error", + "message" => "Target queue does not have a message at that position" + }} + end + def output({:error, error}, %{formatter: "json"}) do + {:error, + %{ + "result" => "error", + "message" => "Failed to perform the operation: #{error}" + }} + end + def output({:error, :not_found}, %{vhost: vhost}) do + {:error, "Target queue was not found in virtual host '#{vhost}'"} + end + def output({:error, :no_message_at_pos}, _) do + {:error, "Target queue does not have a message at that position"} + end + def output({:ok, msg}, %{formatter: "json"}) do + {:ok, %{"result" => "ok", "message" => Enum.into(msg, %{})}} + end + def output({:ok, msg}, _) do + res = Enum.map(msg, fn {k,v} -> [{"keys", k}, {"values", v}] end) + {:stream, res} + end + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.PrettyTable + + def usage() do + "peek [--vhost <vhost>] <queue> <position>" + end + + def usage_additional do + [ + ["<queue>", "Name of the queue", + "<position>", "Position in the queue, starts at 1"] + ] + end + + def help_section(), do: :observability_and_health_checks + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def description(), do: "Peeks at the given position of a quorum queue" + + def banner([name, pos], %{node: node_name}), + do: "Peeking at quorum queue #{name} at position #{pos} on node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/quorum_status_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/quorum_status_command.ex new file mode 100644 index 0000000000..01a61b2536 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/quorum_status_command.ex @@ -0,0 +1,54 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.QuorumStatusCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + def scopes(), do: [:diagnostics, :queues] + + def merge_defaults(args, opts), do: {args, Map.merge(%{vhost: "/"}, opts)} + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name] = _args, %{node: node_name, vhost: vhost}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :status, [vhost, name]) do + {:error, :classic_queue_not_supported} -> + {:error, "Cannot get quorum status of a classic queue"} + + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.PrettyTable + + def usage() do + "quorum_status [--vhost <vhost>] <queue>" + end + + def usage_additional do + [ + ["<queue>", "Name of the queue"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def help_section(), do: :observability_and_health_checks + + def description(), do: "Displays quorum status of a quorum queue" + + def banner([name], %{node: node_name}), + do: "Status of quorum queue #{name} on node #{node_name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/rebalance_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/rebalance_command.ex new file mode 100644 index 0000000000..1416a7c570 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/rebalance_command.ex @@ -0,0 +1,84 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.RebalanceCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + + @known_types [ + "all", + "classic", + "quorum" + ] + + defp default_opts, do: %{vhost_pattern: ".*", + queue_pattern: ".*"} + + def switches(), + do: [ + vhost_pattern: :string, + queue_pattern: :string + ] + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def merge_defaults(args, opts) do + {args, Map.merge(default_opts(), opts)} + end + + def validate([], _) do + {:validation_failure, :not_enough_args} + end + def validate(args, _) when length(args) > 1 do + {:validation_failure, :too_many_args} + end + + def validate([type], _) do + case Enum.member?(@known_types, type) do + true -> + :ok + + false -> + {:error, "type #{type} is not supported. Try one of all, classic, quorum."} + end + end + + def run([type], %{node: node_name, + vhost_pattern: vhost_pat, + queue_pattern: queue_pat}) do + arg = String.to_atom(type) + :rabbit_misc.rpc_call(node_name, :rabbit_amqqueue, :rebalance, [arg, vhost_pat, queue_pat]) + end + + def formatter(), do: RabbitMQ.CLI.Formatters.PrettyTable + + def usage, do: "rebalance < all | classic | quorum > [--vhost-pattern <pattern>] [--queue-pattern <pattern>]" + + def usage_additional do + [ + ["<type>", "queue type, must be one of: all, classic, quorum"], + ["--queue-pattern <pattern>", "regular expression to match queue names"], + ["--vhost-pattern <pattern>", "regular expression to match virtual host names"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def help_section, do: :cluster_management + + def description, do: "Rebalances queues." + + def banner([type], _) do + "Rebalancing #{type} queues..." + end + +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/reclaim_quorum_memory_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/reclaim_quorum_memory_command.ex new file mode 100644 index 0000000000..3452cc8741 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/reclaim_quorum_memory_command.ex @@ -0,0 +1,69 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.ReclaimQuorumMemoryCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + def scopes(), do: [:diagnostics, :queues] + + use RabbitMQ.CLI.Core.MergesDefaultVirtualHost + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name] = _args, %{node: node_name, vhost: vhost}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :reclaim_memory, [vhost, name]) do + {:error, :classic_queue_not_supported} -> + {:error, "This operation is not applicable to classic queues"} + + other -> + other + end + end + + def output({:error, :not_found}, %{vhost: vhost, formatter: "json"}) do + {:error, + %{ + "result" => "error", + "message" => "Target queue was not found in virtual host '#{vhost}'" + }} + end + def output({:error, error}, %{formatter: "json"}) do + {:error, + %{ + "result" => "error", + "message" => "Failed to perform the operation: #{error}" + }} + end + def output({:error, :not_found}, %{vhost: vhost}) do + {:error, "Target queue was not found in virtual host '#{vhost}'"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage() do + "reclaim_quorum_memory [--vhost <vhost>] <quorum queue>" + end + + def usage_additional do + [ + ["<queue>", "Name of the quorum queue"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues(), + DocGuide.memory_use() + ] + end + + def help_section(), do: :operations + + def description(), do: "Flushes quorum queue processes WAL, performs a full sweep GC on all of its local Erlang processes" + + def banner([name], %{}), + do: "Will flush Raft WAL of quorum queue #{name} ..." +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/shrink_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/shrink_command.ex new file mode 100644 index 0000000000..1bff2b9a1c --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/shrink_command.ex @@ -0,0 +1,97 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.ShrinkCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def switches() do + [errors_only: :boolean] + end + + def merge_defaults(args, opts) do + {args, Map.merge(%{errors_only: false}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([node], %{node: node_name, errors_only: errs}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :shrink_all, [to_atom(node)]) do + {:error, _} = error -> error; + {:badrpc, _} = error -> error; + results when errs -> + for {{:resource, vhost, _kind, name}, {:error, _, _} = res} <- results, + do: [{:vhost, vhost}, + {:name, name}, + {:size, format_size(res)}, + {:result, format_result(res)}] + results -> + for {{:resource, vhost, _kind, name}, res} <- results, + do: [{:vhost, vhost}, + {:name, name}, + {:size, format_size(res)}, + {:result, format_result(res)}] + end + end + + use RabbitMQ.CLI.DefaultOutput + + def formatter(), do: RabbitMQ.CLI.Formatters.Table + + def banner([node], _) do + "Shrinking quorum queues on #{node}..." + end + + def usage, do: "shrink <node> [--errors-only]" + + def usage_additional() do + [ + ["<node>", "node name to remove replicas from"], + ["--errors-only", "only list queues which reported an error"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.quorum_queues() + ] + end + + def help_section, do: :cluster_management + + def description, do: "Shrinks quorum queue clusters by removing any members (replicas) on the given node." + + # + # Implementation + # + + defp format_size({:ok, size}) do + size + end + defp format_size({:error, _size, :timeout}) do + # the actual size is uncertain here + "?" + end + defp format_size({:error, size, _}) do + size + end + + defp format_result({:ok, _size}) do + "ok" + end + defp format_result({:error, _size, :last_node}) do + "error: the last node cannot be removed" + end + defp format_result({:error, _size, :timeout}) do + "error: the operation timed out and may not have been completed" + end + defp format_result({:error, _size, err}) do + :io.format "error: ~W", [err, 10] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/add_replica_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/add_replica_command.ex new file mode 100644 index 0000000000..8dc6da0281 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/add_replica_command.ex @@ -0,0 +1,64 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Streams.Commands.AddReplicaCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, node] = _args, %{vhost: vhost, node: node_name}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_stream_queue, :add_replica, [ + vhost, + name, + to_atom(node) + ]) do + {:error, :classic_queue_not_supported} -> + {:error, "Cannot add replicas to a classic queue"} + + {:error, :quorum_queue_not_supported} -> + {:error, "Cannot add replicas to a quorum queue"} + + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "add_replica [--vhost <vhost>] <queue> <node>" + + def usage_additional do + [ + ["<queue>", "stream queue name"], + ["<node>", "node to add a new replica on"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.stream_queues() + ] + end + + def help_section, do: :replication + + def description, do: "Adds a stream queue replica on the given node." + + def banner([name, node], _) do + [ + "Adding a replica for queue #{name} on node #{node}..." + ] + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/delete_replica_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/delete_replica_command.ex new file mode 100644 index 0000000000..ca15282949 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/delete_replica_command.ex @@ -0,0 +1,61 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Streams.Commands.DeleteReplicaCommand do + alias RabbitMQ.CLI.Core.DocGuide + import RabbitMQ.CLI.Core.DataCoercion + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts) do + {args, Map.merge(%{vhost: "/"}, opts)} + end + + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, node] = _args, %{vhost: vhost, node: node_name}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_stream_queue, :delete_replica, [ + vhost, + name, + to_atom(node) + ]) do + {:error, :classic_queue_not_supported} -> + {:error, "Cannot delete replicas from a classic queue"} + + {:error, :quorum_queue_not_supported} -> + {:error, "Cannot delete replicas from a quorum queue"} + + other -> + other + end + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "delete_replica [--vhost <vhost>] <queue> <node>" + + def usage_additional do + [ + ["<queue>", "stream queue name"], + ["<node>", "node to remove a replica from"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.stream_queues() + ] + end + + def help_section, do: :replication + + def description, do: "Removes a stream queue replica on the given node." + + def banner([name, node], _) do + "Removing a replica of queue #{name} on node #{node}..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/set_stream_retention_policy_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/set_stream_retention_policy_command.ex new file mode 100644 index 0000000000..ee89a1ec57 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/streams/commands/set_stream_retention_policy_command.ex @@ -0,0 +1,58 @@ +## 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 https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 Pivotal Software, Inc. All rights reserved. + +defmodule RabbitMQ.CLI.Streams.Commands.SetStreamRetentionPolicyCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def merge_defaults(args, opts), do: {args, Map.merge(%{vhost: "/"}, opts)} + + use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + + def run([name, retention_policy], %{node: node_name, vhost: vhost}) do + :rabbit_misc.rpc_call(node_name, :rabbit_stream_queue, :set_retention_policy, [ + name, + vhost, + retention_policy + ]) + end + + use RabbitMQ.CLI.DefaultOutput + + def banner([name, retention_policy], _) do + "Setting retention policy of stream queue #{name} to #{retention_policy} ..." + end + + def usage, do: "set_stream_retention_policy [--vhost <vhost>] <name> <policy>" + + def usage_additional() do + [ + ["<name>", "stream queue name"], + ["<policy>", "retention policy"] + ] + end + + def usage_doc_guides() do + [ + DocGuide.stream_queues() + ] + end + + def help_section(), do: :policies + + def description(), do: "Sets the retention policy of a stream queue" +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/time_unit.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/time_unit.ex new file mode 100644 index 0000000000..fa08c4befe --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/time_unit.ex @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.TimeUnit do + require MapSet + + @days_seconds 86400 + @weeks_seconds @days_seconds * 7 + @months_seconds @days_seconds * (365 / 12) + @years_seconds @days_seconds * 365 + + def known_units() do + MapSet.new([ + "days", + "weeks", + "months", + "years" + ]) + end + + def convert(time, unit) do + do_convert(time, String.downcase(unit)) + end + + def known_unit?(val) do + MapSet.member?(known_units(), String.downcase(val)) + end + + defp do_convert(time, "days") do + time * @days_seconds + end + + defp do_convert(time, "weeks") do + time * @weeks_seconds + end + + defp do_convert(time, "months") do + time * @months_seconds + end + + defp do_convert(time, "years") do + time * @years_seconds + end + +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/await_online_quorum_plus_one_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/await_online_quorum_plus_one_command.ex new file mode 100644 index 0000000000..ca00ddbbb7 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/await_online_quorum_plus_one_command.ex @@ -0,0 +1,65 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Upgrade.Commands.AwaitOnlineQuorumPlusOneCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 120_000 + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + val -> val + end + + {args, Map.put(opts, :timeout, timeout)} + end + + + def run([], %{node: node_name, timeout: timeout}) do + rpc_timeout = timeout + 500 + case :rabbit_misc.rpc_call(node_name, :rabbit_upgrade_preparation, :await_online_quorum_plus_one, [timeout], rpc_timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + {:badrpc, _} = err -> err + + true -> :ok + false -> {:error, "time is up, no quorum + 1 online replicas came online for at least some quorum queues"} + end + end + + def output({:error, msg}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => msg}} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "await_online_quorum_plus_one" + + def usage_doc_guides() do + [ + DocGuide.quorum_queues(), + DocGuide.upgrade() + ] + end + + def help_section, do: :upgrade + + def description() do + "Waits for all quorum queues to have an above minimum online quorum. " <> + "This makes sure that no queues would lose their quorum if the target node is shut down" + end + + def banner([], %{timeout: timeout}) do + "Will wait for a quorum + 1 of nodes to be online for all quorum queues for #{round(timeout/1000)} seconds..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/await_online_synchronized_mirror_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/await_online_synchronized_mirror_command.ex new file mode 100644 index 0000000000..d6fb40bad2 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/await_online_synchronized_mirror_command.ex @@ -0,0 +1,65 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Upgrade.Commands.AwaitOnlineSynchronizedMirrorCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + @default_timeout 120_000 + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def merge_defaults(args, opts) do + timeout = + case opts[:timeout] do + nil -> @default_timeout + :infinity -> @default_timeout + val -> val + end + + {args, Map.put(opts, :timeout, timeout)} + end + + + def run([], %{node: node_name, timeout: timeout}) do + rpc_timeout = timeout + 500 + case :rabbit_misc.rpc_call(node_name, :rabbit_upgrade_preparation, :await_online_synchronised_mirrors, [timeout], rpc_timeout) do + {:error, _} = err -> err + {:error, _, _} = err -> err + {:badrpc, _} = err -> err + + true -> :ok + false -> {:error, "time is up, no synchronised mirror came online for at least some classic mirrored queues"} + end + end + + def output({:error, msg}, %{node: node_name, formatter: "json"}) do + {:error, %{"result" => "error", "node" => node_name, "message" => msg}} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "await_online_synchronized_mirror" + + def usage_doc_guides() do + [ + DocGuide.mirroring(), + DocGuide.upgrade() + ] + end + + def help_section, do: :upgrade + + def description() do + "Waits for all classic mirrored queues hosted on the target node to have at least one synchronized mirror online. " <> + "This makes sure that if target node is shut down, there will be an up-to-date mirror to promote." + end + + def banner([], %{timeout: timeout}) do + "Will wait for a synchronised mirror be online for all classic mirrored queues for #{round(timeout/1000)} seconds..." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/drain_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/drain_command.ex new file mode 100644 index 0000000000..c6d2fc86eb --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/drain_command.ex @@ -0,0 +1,54 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Upgrade.Commands.DrainCommand do + @moduledoc """ + Puts the node in maintenance mode. Such node would not accept any + new client connections, closes the connections it previously had, + transfers leadership of locally hosted queues, and will not be considered + for primary queue replica placement. + + This command is meant to be used when automating upgrades. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.DocGuide + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_maintenance, :drain, [], timeout) do + # Server does not support maintenance mode + {:badrpc, {:EXIT, {:undef, _}}} -> {:error, :unsupported} + {:badrpc, _} = err -> err + other -> other + end + end + + def output({:error, :unsupported}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Maintenance mode is not supported by node #{node_name}"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "drain" + + def usage_doc_guides() do + [ + DocGuide.upgrade() + ] + end + + def help_section(), do: :upgrade + + def description(), do: "Puts the node in maintenance mode. Such nodes will not serve any client traffic or host any primary queue replicas" + + def banner(_, %{node: node_name}) do + "Will put node #{node_name} into maintenance mode. " + <> "The node will no longer serve any client traffic!" + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/post_upgrade_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/post_upgrade_command.ex new file mode 100644 index 0000000000..76453ce9f3 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/post_upgrade_command.ex @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Upgrade.Commands.PostUpgradeCommand do + alias RabbitMQ.CLI.Core.DocGuide + + @behaviour RabbitMQ.CLI.CommandBehaviour + + use RabbitMQ.CLI.Core.RequiresRabbitAppRunning + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name}) do + :rabbit_misc.rpc_call(node_name, :rabbit_amqqueue, :rebalance, [:all, ".*", ".*"]) + end + + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "post_upgrade" + + def usage_doc_guides() do + [ + DocGuide.upgrade() + ] + end + + def help_section, do: :upgrade + + def description, do: "Runs post-upgrade tasks" + + def banner([], _) do + "Executing post upgrade tasks...\n" <> + "Rebalancing queue masters..." + end + +end diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/revive_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/revive_command.ex new file mode 100644 index 0000000000..a594561a55 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/upgrade/commands/revive_command.ex @@ -0,0 +1,56 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Upgrade.Commands.ReviveCommand do + @moduledoc """ + Puts the node out of maintenance and into regular operating mode. + Such nodes will again serve client traffic and be considered for + primary queue replica placement. + + A node will automatically go into regular operational mode + after a restart. + + This command is meant to be used when automating upgrades. + """ + + @behaviour RabbitMQ.CLI.CommandBehaviour + + alias RabbitMQ.CLI.Core.DocGuide + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def run([], %{node: node_name, timeout: timeout}) do + case :rabbit_misc.rpc_call(node_name, :rabbit_maintenance, :revive, [], timeout) do + # Server does not support maintenance mode + {:badrpc, {:EXIT, {:undef, _}}} -> {:error, :unsupported} + {:badrpc, _} = err -> err + other -> other + end + end + + def output({:error, :unsupported}, %{node: node_name}) do + {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "Maintenance mode is not supported by node #{node_name}"} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "revive" + + def usage_doc_guides() do + [ + DocGuide.upgrade() + ] + end + + def help_section(), do: :upgrade + + def description(), do: "Puts the node out of maintenance and into regular operating mode. Such nodes will again serve client traffic and host primary queue replicas" + + def banner(_, %{node: node_name}) do + "Will put node #{node_name} back into regular operating mode. " + <> "The node will again serve client traffic and host primary queue replicas." + end +end diff --git a/deps/rabbitmq_cli/lib/rabbitmqctl.ex b/deps/rabbitmq_cli/lib/rabbitmqctl.ex new file mode 100644 index 0000000000..42a0f20434 --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmqctl.ex @@ -0,0 +1,620 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQCtl do + alias RabbitMQ.CLI.Core.{ + CommandModules, + Config, + Distribution, + ExitCodes, + Helpers, + Output, + Parser + } + alias RabbitMQ.CLI.{CommandBehaviour, FormatterBehaviour} + alias RabbitMQ.CLI.Ctl.Commands.HelpCommand + + # Enable unit tests for private functions + @compile if Mix.env() == :test, do: :export_all + + @type options() :: map() + @type command_result() :: {:error, ExitCodes.exit_code(), term()} | term() + + def main(["--auto-complete" | []]) do + handle_shutdown(:ok) + end + + def main(unparsed_command) do + exec_command(unparsed_command, &process_output/3) + |> handle_shutdown + end + + def exec_command([] = unparsed_command, _) do + {_args, parsed_options, _} = Parser.parse_global(unparsed_command) + + # this invocation is considered to be invalid. curl and grep do the + # same thing. + {:error, ExitCodes.exit_usage(), Enum.join(HelpCommand.all_usage(parsed_options), "")}; + end + def exec_command(["--help"] = unparsed_command, _) do + {_args, parsed_options, _} = Parser.parse_global(unparsed_command) + + # the user asked for --help and we are displaying it to her, + # reporting a success + {:ok, ExitCodes.exit_ok(), Enum.join(HelpCommand.all_usage(parsed_options), "")}; + end + def exec_command(["--version"] = _unparsed_command, opts) do + # rewrite `--version` as `version` + exec_command(["version"], opts) + end + def exec_command(["--auto-complete" | args], opts) do + # rewrite `--auto-complete` as `autocomplete` + exec_command(["autocomplete" | args], opts) + end + def exec_command(unparsed_command, output_fun) do + {command, command_name, arguments, parsed_options, invalid} = Parser.parse(unparsed_command) + + case {command, invalid} do + {:no_command, _} -> + command_not_found_string = + case command_name do + "" -> "" + _ -> "\nCommand '#{command_name}' not found. \n" + end + + usage_string = + command_not_found_string <> + Enum.join(HelpCommand.all_usage(parsed_options), "") + + {:error, ExitCodes.exit_usage(), usage_string} + + {{:suggest, suggested}, _} -> + suggest_message = + "\nCommand '#{command_name}' not found. \n" <> + "Did you mean '#{suggested}'? \n" + + {:error, ExitCodes.exit_usage(), suggest_message} + + {_, [_ | _]} -> + argument_validation_error_output( + {:bad_option, invalid}, + command, + unparsed_command, + parsed_options + ) + + _ -> + options = parsed_options |> merge_all_defaults |> normalise_options + + try do + do_exec_parsed_command(unparsed_command, output_fun, arguments, command, options) + catch error_type, error -> + maybe_print_stacktrace(error_type, error, __STACKTRACE__, options) + format_error(error, options, command) + end + end + end + + def do_exec_parsed_command(unparsed_command, output_fun, arguments, command, options) do + case options[:help] do + true -> + {:ok, ExitCodes.exit_ok(), HelpCommand.command_usage(command, options)}; + _ -> + {arguments, options} = command.merge_defaults(arguments, options) + + maybe_with_distribution(command, options, fn -> + # rabbitmq/rabbitmq-cli#278 + case Helpers.normalise_node_option(options) do + {:error, _} = err -> + format_error(err, options, command) + {:ok, options} -> + # The code below implements a tiny decision tree that has + # to do with CLI argument and environment state validation. + case command.validate(arguments, options) do + :ok -> + # then optionally validate execution environment + case CommandBehaviour.validate_execution_environment(command, arguments, options) do + :ok -> + result = proceed_to_execution(command, arguments, options) + handle_command_output(result, command, options, output_fun) + + {:validation_failure, err} -> + environment_validation_error_output(err, command, unparsed_command, options) + + {:error, _} = err -> + format_error(err, options, command) + end + + {:validation_failure, err} -> + argument_validation_error_output(err, command, unparsed_command, options) + + {:error, _} = err -> + format_error(err, options, command) + end + end + end) + end + end + + defp proceed_to_execution(command, arguments, options) do + maybe_print_banner(command, arguments, options) + maybe_run_command(command, arguments, options) + end + + defp maybe_run_command(_, _, %{dry_run: true}) do + :ok + end + + defp maybe_run_command(command, arguments, options) do + try do + command.run(arguments, options) |> command.output(options) + catch error_type, error -> + maybe_print_stacktrace(error_type, error, __STACKTRACE__, options) + format_error(error, options, command) + end + end + + def maybe_print_stacktrace(error_type, :undef = error, stacktrace, _opts) do + do_print_stacktrace(error_type, error, stacktrace) + end + def maybe_print_stacktrace(error_type, :function_clause = error, stacktrace, _opts) do + do_print_stacktrace(error_type, error, stacktrace) + end + def maybe_print_stacktrace(error_type, :badarg = error, stacktrace, _opts) do + do_print_stacktrace(error_type, error, stacktrace) + end + def maybe_print_stacktrace(error_type, {:case_clause, _val} = error, stacktrace, _opts) do + do_print_stacktrace(error_type, error, stacktrace) + end + def maybe_print_stacktrace(error_type, error, stacktrace, %{print_stacktrace: true}) do + do_print_stacktrace(error_type, error, stacktrace) + end + def maybe_print_stacktrace(_error_type, _error, _stacktrace, %{print_stacktrace: false}) do + nil + end + def maybe_print_stacktrace(_error_type, _error, _stacktrace, _opts) do + nil + end + + defp do_print_stacktrace(error_type, error, stacktrace) do + IO.puts("Stack trace: \n") + IO.puts(Exception.format(error_type, error, stacktrace)) + end + + def handle_command_output(output, command, options, output_fun) do + case output do + {:error, _, _} = err -> + format_error(err, options, command) + + {:error, _} = err -> + format_error(err, options, command) + + _ -> + output_fun.(output, command, options) + end + end + + defp process_output(output, command, options) do + formatter = Config.get_formatter(command, options) + printer = Config.get_printer(command, options) + + output + |> Output.format_output(formatter, options) + |> Output.print_output(printer, options) + |> case do + {:error, _} = err -> format_error(err, options, command) + other -> other + end + end + + defp output_device(exit_code) do + case exit_code == ExitCodes.exit_ok() do + true -> :stdio + false -> :stderr + end + end + + defp handle_shutdown({:error, exit_code, nil}) do + exit_program(exit_code) + end + + defp handle_shutdown({_, exit_code, output}) do + device = output_device(exit_code) + + for line <- List.flatten([output]) do + IO.puts(device, Helpers.string_or_inspect(line)) + end + + exit_program(exit_code) + end + + defp handle_shutdown(_) do + exit_program(ExitCodes.exit_ok()) + end + + def merge_all_defaults(%{} = options) do + options + |> merge_defaults_node + |> merge_defaults_timeout + |> merge_defaults_longnames + end + + defp merge_defaults_node(%{} = opts) do + longnames_opt = Config.get_option(:longnames, opts) + try do + default_rabbit_nodename = Helpers.get_rabbit_hostname(longnames_opt) + Map.merge(%{node: default_rabbit_nodename}, opts) + catch _error_type, _err -> + opts + end + end + + defp merge_defaults_timeout(%{} = opts), do: Map.merge(%{timeout: :infinity}, opts) + + defp merge_defaults_longnames(%{} = opts), do: Map.merge(%{longnames: false}, opts) + + defp normalise_options(opts) do + opts |> normalise_timeout + end + + defp normalise_timeout(%{timeout: timeout} = opts) + when is_integer(timeout) do + Map.put(opts, :timeout, timeout * 1000) + end + + defp normalise_timeout(opts) do + opts + end + + # Suppress banner if --quiet option is provided + defp maybe_print_banner(_, _, %{quiet: true}) do + nil + end + + # Suppress banner if --silent option is provided + defp maybe_print_banner(_, _, %{silent: true}) do + nil + end + + defp maybe_print_banner(command, args, opts) do + # Suppress banner if a machine-readable formatter is used + formatter = Map.get(opts, :formatter) + case FormatterBehaviour.machine_readable?(formatter) do + true -> nil + false -> + case command.banner(args, opts) do + nil -> + nil + + banner -> + case banner do + list when is_list(list) -> + for line <- list, do: IO.puts(line) + + binary when is_binary(binary) -> + IO.puts(binary) + end + end + end + end + + def argument_validation_error_output(err_detail, command, unparsed_command, options) do + err = format_validation_error(err_detail) + + base_error = + "Error (argument validation): #{err}\nArguments given:\n\t#{ + unparsed_command |> Enum.join(" ") + }" + + validation_error_output(err_detail, base_error, command, options) + end + + def environment_validation_error_output(err_detail, command, unparsed_command, options) do + err = format_validation_error(err_detail) + base_error = "Error: #{err}\nArguments given:\n\t#{unparsed_command |> Enum.join(" ")}" + validation_error_output(err_detail, base_error, command, options) + end + + defp validation_error_output(err_detail, base_error, command, options) do + usage = HelpCommand.base_usage(command, options) + message = base_error <> "\n" <> usage + {:error, ExitCodes.exit_code_for({:validation_failure, err_detail}), message} + end + + defp format_validation_error(:not_enough_args), do: "not enough arguments." + defp format_validation_error({:not_enough_args, detail}), do: "not enough arguments: #{detail}" + defp format_validation_error(:too_many_args), do: "too many arguments." + defp format_validation_error({:too_many_args, detail}), do: "too many arguments: #{detail}" + defp format_validation_error(:bad_argument), do: "Bad argument." + defp format_validation_error({:bad_argument, detail}), do: "Bad argument: #{detail}" + + defp format_validation_error({:unsupported_target, details}) do + details + end + + defp format_validation_error({:bad_option, opts}) do + header = "Invalid options for this command:" + + Enum.join( + [ + header + | for {key, val} <- opts do + "#{key} : #{val}" + end + ], + "\n" + ) + end + + defp format_validation_error({:bad_info_key, keys}), + do: "Info key(s) #{Enum.join(keys, ",")} are not supported" + + defp format_validation_error(:rabbit_app_is_stopped), + do: + "this command requires the 'rabbit' app to be running on the target node. Start it with 'rabbitmqctl start_app'." + + defp format_validation_error(:rabbit_app_is_running), + do: + "this command requires the 'rabbit' app to be stopped on the target node. Stop it with 'rabbitmqctl stop_app'." + + defp format_validation_error(:node_running), + do: "this command requires the target node to be stopped." + + defp format_validation_error(:node_not_running), + do: "this command requires the target node to be running." + + defp format_validation_error(:unsupported_formatter), + do: "the requested formatter is not supported by this command" + + defp format_validation_error(err), do: inspect(err) + + defp exit_program(code) do + :net_kernel.stop() + exit({:shutdown, code}) + end + + defp format_error({:error, {:node_name, :hostname_not_allowed}}, _, _) do + {:error, ExitCodes.exit_dataerr(), + "Unsupported node name: hostname is invalid (possibly contains unsupported characters).\nIf using FQDN node names, use the -l / --longnames argument."} + end + defp format_error({:error, {:node_name, :invalid_node_name_head}}, _, _) do + {:error, ExitCodes.exit_dataerr(), + "Unsupported node name: node name head (the part before the @) is invalid. Only alphanumerics, _ and - characters are allowed.\nIf using FQDN node names, use the -l / --longnames argument"} + end + defp format_error({:error, {:node_name, err_reason} = result}, opts, module) do + op = CommandModules.module_to_command(module) + node = opts[:node] || "(failed to parse or compute default value)" + {:error, ExitCodes.exit_code_for(result), + "Error: operation #{op} failed due to invalid node name (node: #{node}, reason: #{err_reason}).\nIf using FQDN node names, use the -l / --longnames argument"} + end + + defp format_error({:error, {:badrpc_multi, :nodedown, [node | _]} = result}, opts, _) do + diagnostics = get_node_diagnostics(node) + + {:error, ExitCodes.exit_code_for(result), + badrpc_error_message_header(node, opts) <> diagnostics} + end + + defp format_error({:error, {:badrpc_multi, :timeout, [node | _]} = result}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for(result), + "Error: operation #{op} on node #{node} timed out. Timeout value used: #{opts[:timeout]}"} + end + + defp format_error({:error, {:badrpc, :nodedown} = result}, opts, _) do + diagnostics = get_node_diagnostics(opts[:node]) + + {:error, ExitCodes.exit_code_for(result), + badrpc_error_message_header(opts[:node], opts) <> diagnostics} + end + + defp format_error({:error, {:badrpc, :timeout} = result}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for(result), + "Error: operation #{op} on node #{opts[:node]} timed out. Timeout value used: #{ + opts[:timeout] + }"} + end + + defp format_error({:error, {:badrpc, {:timeout, to}} = result}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for(result), + "Error: operation #{op} on node #{opts[:node]} timed out. Timeout value used: #{to}"} + end + + defp format_error({:error, {:badrpc, {:timeout, to, warning}}}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for({:timeout, to}), + "Error: operation #{op} on node #{opts[:node]} timed out. Timeout value used: #{to}. #{ + warning + }"} + end + + defp format_error({:error, {:no_such_vhost, vhost} = result}, _opts, _) do + {:error, ExitCodes.exit_code_for(result), "Virtual host '#{vhost}' does not exist"} + end + + defp format_error({:error, {:incompatible_version, local_version, remote_version} = result}, _opts, _) do + {:error, ExitCodes.exit_code_for(result), "Detected potential version incompatibility. CLI tool version: #{local_version}, server: #{remote_version}"} + end + + defp format_error({:error, {:timeout, to} = result}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for(result), + "Error: operation #{op} on node #{opts[:node]} timed out. Timeout value used: #{to}"} + end + + defp format_error({:error, :timeout = result}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for(result), + "Error: operation #{op} on node #{opts[:node]} timed out. Timeout value used: #{opts[:timeout]}"} + end + + defp format_error({:error, :timeout, msg}, opts, module) do + op = CommandModules.module_to_command(module) + + {:error, ExitCodes.exit_code_for(:timeout), + "Error: operation #{op} on node #{opts[:node]} timed out: #{msg}. Timeout value used: #{opts[:timeout]}"} + end + + # Plugins + defp format_error({:error, {:enabled_plugins_mismatch, cli_path, node_path}}, opts, _module) do + {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at #{cli_path}: target node #{opts[:node]} uses a different path (#{ + node_path + })"} + end + + defp format_error({:error, {:cannot_read_enabled_plugins_file, path, :eacces}}, _opts, _module) do + {:error, ExitCodes.exit_dataerr(), + "Could not read enabled plugins file at #{path}: the file does not exist or permission was denied (EACCES)"} + end + + defp format_error({:error, {:cannot_read_enabled_plugins_file, path, :enoent}}, _opts, _module) do + {:error, ExitCodes.exit_dataerr(), + "Could not read enabled plugins file at #{path}: the file does not exist (ENOENT)"} + end + + defp format_error({:error, {:cannot_write_enabled_plugins_file, path, :eacces}}, _opts, _module) do + {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at #{path}: the file does not exist or permission was denied (EACCES)"} + end + + defp format_error({:error, {:cannot_write_enabled_plugins_file, path, :enoent}}, _opts, _module) do + {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at #{path}: the file does not exist (ENOENT)"} + end + + # Special case health checks. This makes it easier to change + # output of all health checks at once. + defp format_error({:error, :check_failed}, %{formatter: "json"}, _) do + {:error, ExitCodes.exit_unavailable(), nil} + end + defp format_error({:error, :check_failed}, _, _) do + {:error, ExitCodes.exit_unavailable(), nil} + end + defp format_error({:error, :check_failed, err}, %{formatter: "json"}, _) when is_map(err) do + {:ok, res} = JSON.encode(err) + {:error, ExitCodes.exit_unavailable(), res} + end + defp format_error({:error, :check_failed, err}, %{formatter: "json"}, _) do + {:error, ExitCodes.exit_unavailable(), err} + end + defp format_error({:error, :check_failed, err}, _, _) do + {:error, ExitCodes.exit_unavailable(), err} + end + + defp format_error({:error, nil}, _, _) do + # the command intends to produce no output, e.g. a return code + # is sufficient + {:error, ExitCodes.exit_unavailable(), nil} + end + + # Catch all clauses + defp format_error({:error, err}, %{formatter: "json"}, _) when is_map(err) do + {:ok, res} = JSON.encode(err) + {:error, ExitCodes.exit_unavailable(), res} + end + defp format_error({:error, exit_code, err}, %{formatter: "json"}, _) when is_map(err) do + {:ok, res} = JSON.encode(err) + {:error, exit_code, res} + end + + defp format_error({:error, exit_code, err}, _, _) do + string_err = Helpers.string_or_inspect(err) + + {:error, exit_code, "Error:\n#{string_err}"} + end + + defp format_error({:error, err} = result, _, _) do + string_err = Helpers.string_or_inspect(err) + + {:error, ExitCodes.exit_code_for(result), "Error:\n#{string_err}"} + end + + defp format_error(error, _opts, _module) do + {:error, ExitCodes.exit_software(), "#{inspect(error)}\n"} + end + + defp get_node_diagnostics(nil) do + "Target node is not defined" + end + + defp get_node_diagnostics(node_name) do + to_string(:rabbit_nodes_common.diagnostics([node_name])) + end + + defp badrpc_error_message_header(node, _opts) do + """ + Error: unable to perform an operation on node '#{node}'. Please see diagnostics information and suggestions below. + + Most common reasons for this are: + + * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues) + * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server) + * Target node is not running + + In addition to the diagnostics info below: + + * See the CLI, clustering and networking guides on https://rabbitmq.com/documentation.html to learn more + * Consult server logs on node #{node} + * If target node is configured to use long node names, don't forget to use --longnames with CLI tools + """ + end + + ## Tries to enable erlang distribution, which can be configured + ## via distribution callback in the command as :cli, :none or {:custom, fun()}. + ## :cli - default rabbitmqctl node name + ## :none - do not start a distribution (e.g. offline command) + ## {:fun, fun} - run a custom function to enable distribution. + ## custom mode is usefult for commands which should have specific node name. + ## Runs code if distribution is successful, or not needed. + @spec maybe_with_distribution(module(), options(), (() -> command_result())) :: command_result() + defp maybe_with_distribution(command, options, code) do + try do + maybe_with_distribution_without_catch(command, options, code) + catch error_type, error -> + maybe_print_stacktrace(error_type, error, __STACKTRACE__, options) + format_error(error, options, command) + end + end + + defp maybe_with_distribution_without_catch(command, options, code) do + distribution_type = CommandBehaviour.distribution(command, options) + + case distribution_type do + :none -> + code.() + + :cli -> + case Distribution.start(options) do + :ok -> + code.() + + {:ok, _} -> + code.() + + {:error, reason} -> + {:error, ExitCodes.exit_config(), "Distribution failed: #{inspect(reason)}"} + end + + {:fun, fun} -> + case fun.(options) do + :ok -> + code.() + + {:error, reason} -> + {:error, ExitCodes.exit_config(), "Distribution failed: #{inspect(reason)}"} + end + end + end +end diff --git a/deps/rabbitmq_cli/mix.exs b/deps/rabbitmq_cli/mix.exs new file mode 100644 index 0000000000..09bbda3846 --- /dev/null +++ b/deps/rabbitmq_cli/mix.exs @@ -0,0 +1,209 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQCtl.MixfileBase do + use Mix.Project + + def project do + [ + app: :rabbitmqctl, + version: "3.8.0-dev", + elixir: ">= 1.10.4 and < 1.12.0", + build_embedded: Mix.env == :prod, + start_permanent: Mix.env == :prod, + escript: [main_module: RabbitMQCtl, + emu_args: "-hidden", + path: "escript/rabbitmqctl"], + deps: deps(), + aliases: aliases(), + xref: [ + exclude: [ + CSV, + CSV.Encode, + JSON, + :mnesia, + :msacc, + :observer_cli, + :public_key, + :pubkey_cert, + :rabbit, + :rabbit_control_misc, + :rabbit_data_coercion, + :rabbit_env, + :rabbit_event, + :rabbit_file, + :rabbit_net, + :rabbit_lager, + :rabbit_log, + :rabbit_misc, + :rabbit_mnesia, + :rabbit_mnesia_rename, + :rabbit_nodes_common, + :rabbit_pbe, + :rabbit_plugins, + :rabbit_resource_monitor_misc, + :stdout_formatter + ] + ] + ] + end + + # Configuration for the OTP application + # + # Type "mix help compile.app" for more information + def application do + [applications: [:logger], + env: [scopes: ['rabbitmq-plugins': :plugins, + rabbitmqctl: :ctl, + 'rabbitmq-diagnostics': :diagnostics, + 'rabbitmq-queues': :queues, + 'rabbitmq-streams': :streams, + 'rabbitmq-upgrade': :upgrade]] + ] + |> add_modules(Mix.env) + end + + + defp add_modules(app, :test) do + # There are issues with building a package without this line ¯\_(ツ)_/¯ + Mix.Project.get + path = Mix.Project.compile_path + mods = modules_from(Path.wildcard("#{path}/*.beam")) + test_modules = [RabbitMQ.CLI.Ctl.Commands.DuckCommand, + RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand, + RabbitMQ.CLI.Ctl.Commands.UglyDucklingCommand, + RabbitMQ.CLI.Plugins.Commands.StorkCommand, + RabbitMQ.CLI.Plugins.Commands.HeronCommand, + RabbitMQ.CLI.Custom.Commands.CrowCommand, + RabbitMQ.CLI.Custom.Commands.RavenCommand, + RabbitMQ.CLI.Seagull.Commands.SeagullCommand, + RabbitMQ.CLI.Seagull.Commands.PacificGullCommand, + RabbitMQ.CLI.Seagull.Commands.HerringGullCommand, + RabbitMQ.CLI.Seagull.Commands.HermannGullCommand, + RabbitMQ.CLI.Wolf.Commands.CanisLupusCommand, + RabbitMQ.CLI.Wolf.Commands.CanisLatransCommand, + RabbitMQ.CLI.Wolf.Commands.CanisAureusCommand + ] + [{:modules, mods ++ test_modules |> Enum.sort} | app] + end + defp add_modules(app, _) do + app + end + + defp modules_from(beams) do + Enum.map beams, &(&1 |> Path.basename |> Path.rootname(".beam") |> String.to_atom) + end + + # Dependencies can be Hex packages: + # + # {:mydep, "~> 0.3.0"} + # + # Or git/path repositories: + # + # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} + # + # Type "mix help deps" for more examples and options + # + # CAUTION: Dependencies which are shipped with RabbitMQ *MUST* com + # from Hex.pm! Therefore it's ok to fetch dependencies from Git if + # they are test dependencies or it is temporary while testing a patch. + # But that's about it. If in doubt, use Hex.pm! + # + # The reason is that we have some Makefile code to put dependencies + # from Hex.pm in RabbitMQ source archive (the source archive must be + # self-contained and RabbitMQ must be buildable offline). However, we + # don't have the equivalent for other methods. + defp deps() do + elixir_deps = [ + {:json, "~> 1.2.0"}, + {:csv, "~> 2.3.0"}, + {:stdout_formatter, "~> 0.2.3"}, + {:observer_cli, "~> 1.5.0"}, + + {:amqp, "~> 1.2.0", only: :test}, + {:dialyxir, "~> 0.5", only: :test, runtime: false}, + {:temp, "~> 0.4", only: :test}, + {:x509, "~> 0.7", only: :test} + ] + + rabbitmq_deps = case System.get_env("DEPS_DIR") do + nil -> + # rabbitmq_cli is built as a standalone Elixir application. + [ + {:rabbit_common, "~> 3.7.0"}, + {:amqp_client, "~> 3.7.0", only: :test} + ] + deps_dir -> + # rabbitmq_cli is built as part of RabbitMQ. + + # Mix is confused by any `rebar.{config,lock}` we might have left in + # `rabbit_common` or `amqp_client`. So just remove those files to be + # safe, as they are generated when we publish to Hex.pm only. + for dir <- ["rabbit_common", "amqp_client"] do + for file <- ["rebar.config", "rebar.lock"] do + File.rm(Path.join([deps_dir, dir, file])) + end + end + + # We disable compilation for rabbit_common and amqp_client + # because Erlang.mk already built them. + [ + { + :rabbit_common, + path: Path.join(deps_dir, "rabbit_common"), + compile: false, + override: true + }, + { + :goldrush, + path: Path.join(deps_dir, "goldrush"), + compile: false, + override: true + }, + { + :lager, + path: Path.join(deps_dir, "lager"), + compile: false, + override: true + }, + { + :amqp_client, + path: Path.join(deps_dir, "amqp_client"), + compile: false, + override: true, + only: :test + }, + ] + end + + elixir_deps ++ rabbitmq_deps + end + + defp aliases do + [ + make_deps: [ + "deps.get", + "deps.compile", + ], + make_app: [ + "compile", + "escript.build", + ], + make_all: [ + "deps.get", + "deps.compile", + "compile", + "escript.build", + ], + make_all_in_src_archive: [ + "deps.get --only prod", + "deps.compile", + "compile", + "escript.build", + ], + ] + end +end diff --git a/deps/rabbitmq_cli/rabbitmq-components.mk b/deps/rabbitmq_cli/rabbitmq-components.mk new file mode 100644 index 0000000000..b2a3be8b35 --- /dev/null +++ b/deps/rabbitmq_cli/rabbitmq-components.mk @@ -0,0 +1,359 @@ +ifeq ($(.DEFAULT_GOAL),) +# Define default goal to `all` because this file defines some targets +# before the inclusion of erlang.mk leading to the wrong target becoming +# the default. +.DEFAULT_GOAL = all +endif + +# PROJECT_VERSION defaults to: +# 1. the version exported by rabbitmq-server-release; +# 2. the version stored in `git-revisions.txt`, if it exists; +# 3. a version based on git-describe(1), if it is a Git clone; +# 4. 0.0.0 + +PROJECT_VERSION := $(RABBITMQ_VERSION) + +ifeq ($(PROJECT_VERSION),) +PROJECT_VERSION := $(shell \ +if test -f git-revisions.txt; then \ + head -n1 git-revisions.txt | \ + awk '{print $$$(words $(PROJECT_DESCRIPTION) version);}'; \ +else \ + (git describe --dirty --abbrev=7 --tags --always --first-parent \ + 2>/dev/null || echo rabbitmq_v0_0_0) | \ + sed -e 's/^rabbitmq_v//' -e 's/^v//' -e 's/_/./g' -e 's/-/+/' \ + -e 's/-/./g'; \ +fi) +endif + +# -------------------------------------------------------------------- +# RabbitMQ components. +# -------------------------------------------------------------------- + +# For RabbitMQ repositories, we want to checkout branches which match +# the parent project. For instance, if the parent project is on a +# release tag, dependencies must be on the same release tag. If the +# parent project is on a topic branch, dependencies must be on the same +# topic branch or fallback to `stable` or `master` whichever was the +# base of the topic branch. + +dep_amqp_client = git_rmq rabbitmq-erlang-client $(current_rmq_ref) $(base_rmq_ref) master +dep_amqp10_client = git_rmq rabbitmq-amqp1.0-client $(current_rmq_ref) $(base_rmq_ref) master +dep_amqp10_common = git_rmq rabbitmq-amqp1.0-common $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbit = git_rmq rabbitmq-server $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbit_common = git_rmq rabbitmq-common $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_amqp1_0 = git_rmq rabbitmq-amqp1.0 $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_auth_backend_amqp = git_rmq rabbitmq-auth-backend-amqp $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_auth_backend_cache = git_rmq rabbitmq-auth-backend-cache $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_auth_backend_http = git_rmq rabbitmq-auth-backend-http $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_auth_backend_ldap = git_rmq rabbitmq-auth-backend-ldap $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_auth_backend_oauth2 = git_rmq rabbitmq-auth-backend-oauth2 $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_auth_mechanism_ssl = git_rmq rabbitmq-auth-mechanism-ssl $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_aws = git_rmq rabbitmq-aws $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_boot_steps_visualiser = git_rmq rabbitmq-boot-steps-visualiser $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_cli = git_rmq rabbitmq-cli $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_codegen = git_rmq rabbitmq-codegen $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_consistent_hash_exchange = git_rmq rabbitmq-consistent-hash-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_ct_client_helpers = git_rmq rabbitmq-ct-client-helpers $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_ct_helpers = git_rmq rabbitmq-ct-helpers $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_delayed_message_exchange = git_rmq rabbitmq-delayed-message-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_dotnet_client = git_rmq rabbitmq-dotnet-client $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_event_exchange = git_rmq rabbitmq-event-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_federation = git_rmq rabbitmq-federation $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_federation_management = git_rmq rabbitmq-federation-management $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_java_client = git_rmq rabbitmq-java-client $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_jms_client = git_rmq rabbitmq-jms-client $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_jms_cts = git_rmq rabbitmq-jms-cts $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_jms_topic_exchange = git_rmq rabbitmq-jms-topic-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_lvc_exchange = git_rmq rabbitmq-lvc-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_management = git_rmq rabbitmq-management $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_management_agent = git_rmq rabbitmq-management-agent $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_management_exchange = git_rmq rabbitmq-management-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_management_themes = git_rmq rabbitmq-management-themes $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_message_timestamp = git_rmq rabbitmq-message-timestamp $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_metronome = git_rmq rabbitmq-metronome $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_mqtt = git_rmq rabbitmq-mqtt $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_objc_client = git_rmq rabbitmq-objc-client $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_peer_discovery_aws = git_rmq rabbitmq-peer-discovery-aws $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_peer_discovery_common = git_rmq rabbitmq-peer-discovery-common $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_peer_discovery_consul = git_rmq rabbitmq-peer-discovery-consul $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_peer_discovery_etcd = git_rmq rabbitmq-peer-discovery-etcd $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_peer_discovery_k8s = git_rmq rabbitmq-peer-discovery-k8s $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_prometheus = git_rmq rabbitmq-prometheus $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_random_exchange = git_rmq rabbitmq-random-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_recent_history_exchange = git_rmq rabbitmq-recent-history-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_routing_node_stamp = git_rmq rabbitmq-routing-node-stamp $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_rtopic_exchange = git_rmq rabbitmq-rtopic-exchange $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_server_release = git_rmq rabbitmq-server-release $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_sharding = git_rmq rabbitmq-sharding $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_shovel = git_rmq rabbitmq-shovel $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_shovel_management = git_rmq rabbitmq-shovel-management $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_stomp = git_rmq rabbitmq-stomp $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_stream = git_rmq rabbitmq-stream $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_toke = git_rmq rabbitmq-toke $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_top = git_rmq rabbitmq-top $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_tracing = git_rmq rabbitmq-tracing $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_trust_store = git_rmq rabbitmq-trust-store $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_test = git_rmq rabbitmq-test $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_web_dispatch = git_rmq rabbitmq-web-dispatch $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_web_stomp = git_rmq rabbitmq-web-stomp $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_web_stomp_examples = git_rmq rabbitmq-web-stomp-examples $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_web_mqtt = git_rmq rabbitmq-web-mqtt $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_web_mqtt_examples = git_rmq rabbitmq-web-mqtt-examples $(current_rmq_ref) $(base_rmq_ref) master +dep_rabbitmq_website = git_rmq rabbitmq-website $(current_rmq_ref) $(base_rmq_ref) live master +dep_toke = git_rmq toke $(current_rmq_ref) $(base_rmq_ref) master + +dep_rabbitmq_public_umbrella = git_rmq rabbitmq-public-umbrella $(current_rmq_ref) $(base_rmq_ref) master + +# Third-party dependencies version pinning. +# +# We do that in this file, which is copied in all projects, to ensure +# all projects use the same versions. It avoids conflicts and makes it +# possible to work with rabbitmq-public-umbrella. + +dep_accept = hex 0.3.5 +dep_cowboy = hex 2.8.0 +dep_cowlib = hex 2.9.1 +dep_jsx = hex 2.11.0 +dep_lager = hex 3.8.0 +dep_prometheus = git https://github.com/deadtrickster/prometheus.erl.git master +dep_ra = git https://github.com/rabbitmq/ra.git master +dep_ranch = hex 1.7.1 +dep_recon = hex 2.5.1 +dep_observer_cli = hex 1.5.4 +dep_stdout_formatter = hex 0.2.4 +dep_sysmon_handler = hex 1.3.0 + +RABBITMQ_COMPONENTS = amqp_client \ + amqp10_common \ + amqp10_client \ + rabbit \ + rabbit_common \ + rabbitmq_amqp1_0 \ + rabbitmq_auth_backend_amqp \ + rabbitmq_auth_backend_cache \ + rabbitmq_auth_backend_http \ + rabbitmq_auth_backend_ldap \ + rabbitmq_auth_backend_oauth2 \ + rabbitmq_auth_mechanism_ssl \ + rabbitmq_aws \ + rabbitmq_boot_steps_visualiser \ + rabbitmq_cli \ + rabbitmq_codegen \ + rabbitmq_consistent_hash_exchange \ + rabbitmq_ct_client_helpers \ + rabbitmq_ct_helpers \ + rabbitmq_delayed_message_exchange \ + rabbitmq_dotnet_client \ + rabbitmq_event_exchange \ + rabbitmq_federation \ + rabbitmq_federation_management \ + rabbitmq_java_client \ + rabbitmq_jms_client \ + rabbitmq_jms_cts \ + rabbitmq_jms_topic_exchange \ + rabbitmq_lvc_exchange \ + rabbitmq_management \ + rabbitmq_management_agent \ + rabbitmq_management_exchange \ + rabbitmq_management_themes \ + rabbitmq_message_timestamp \ + rabbitmq_metronome \ + rabbitmq_mqtt \ + rabbitmq_objc_client \ + rabbitmq_peer_discovery_aws \ + rabbitmq_peer_discovery_common \ + rabbitmq_peer_discovery_consul \ + rabbitmq_peer_discovery_etcd \ + rabbitmq_peer_discovery_k8s \ + rabbitmq_prometheus \ + rabbitmq_random_exchange \ + rabbitmq_recent_history_exchange \ + rabbitmq_routing_node_stamp \ + rabbitmq_rtopic_exchange \ + rabbitmq_server_release \ + rabbitmq_sharding \ + rabbitmq_shovel \ + rabbitmq_shovel_management \ + rabbitmq_stomp \ + rabbitmq_stream \ + rabbitmq_toke \ + rabbitmq_top \ + rabbitmq_tracing \ + rabbitmq_trust_store \ + rabbitmq_web_dispatch \ + rabbitmq_web_mqtt \ + rabbitmq_web_mqtt_examples \ + rabbitmq_web_stomp \ + rabbitmq_web_stomp_examples \ + rabbitmq_website + +# Erlang.mk does not rebuild dependencies by default, once they were +# compiled once, except for those listed in the `$(FORCE_REBUILD)` +# variable. +# +# We want all RabbitMQ components to always be rebuilt: this eases +# the work on several components at the same time. + +FORCE_REBUILD = $(RABBITMQ_COMPONENTS) + +# Several components have a custom erlang.mk/build.config, mainly +# to disable eunit. Therefore, we can't use the top-level project's +# erlang.mk copy. +NO_AUTOPATCH += $(RABBITMQ_COMPONENTS) + +ifeq ($(origin current_rmq_ref),undefined) +ifneq ($(wildcard .git),) +current_rmq_ref := $(shell (\ + ref=$$(LANG=C git branch --list | awk '/^\* \(.*detached / {ref=$$0; sub(/.*detached [^ ]+ /, "", ref); sub(/\)$$/, "", ref); print ref; exit;} /^\* / {ref=$$0; sub(/^\* /, "", ref); print ref; exit}');\ + if test "$$(git rev-parse --short HEAD)" != "$$ref"; then echo "$$ref"; fi)) +else +current_rmq_ref := master +endif +endif +export current_rmq_ref + +ifeq ($(origin base_rmq_ref),undefined) +ifneq ($(wildcard .git),) +possible_base_rmq_ref := master +ifeq ($(possible_base_rmq_ref),$(current_rmq_ref)) +base_rmq_ref := $(current_rmq_ref) +else +base_rmq_ref := $(shell \ + (git rev-parse --verify -q master >/dev/null && \ + git rev-parse --verify -q $(possible_base_rmq_ref) >/dev/null && \ + git merge-base --is-ancestor $$(git merge-base master HEAD) $(possible_base_rmq_ref) && \ + echo $(possible_base_rmq_ref)) || \ + echo master) +endif +else +base_rmq_ref := master +endif +endif +export base_rmq_ref + +# Repository URL selection. +# +# First, we infer other components' location from the current project +# repository URL, if it's a Git repository: +# - We take the "origin" remote URL as the base +# - The current project name and repository name is replaced by the +# target's properties: +# eg. rabbitmq-common is replaced by rabbitmq-codegen +# eg. rabbit_common is replaced by rabbitmq_codegen +# +# If cloning from this computed location fails, we fallback to RabbitMQ +# upstream which is GitHub. + +# Macro to transform eg. "rabbit_common" to "rabbitmq-common". +rmq_cmp_repo_name = $(word 2,$(dep_$(1))) + +# Upstream URL for the current project. +RABBITMQ_COMPONENT_REPO_NAME := $(call rmq_cmp_repo_name,$(PROJECT)) +RABBITMQ_UPSTREAM_FETCH_URL ?= https://github.com/rabbitmq/$(RABBITMQ_COMPONENT_REPO_NAME).git +RABBITMQ_UPSTREAM_PUSH_URL ?= git@github.com:rabbitmq/$(RABBITMQ_COMPONENT_REPO_NAME).git + +# Current URL for the current project. If this is not a Git clone, +# default to the upstream Git repository. +ifneq ($(wildcard .git),) +git_origin_fetch_url := $(shell git config remote.origin.url) +git_origin_push_url := $(shell git config remote.origin.pushurl || git config remote.origin.url) +RABBITMQ_CURRENT_FETCH_URL ?= $(git_origin_fetch_url) +RABBITMQ_CURRENT_PUSH_URL ?= $(git_origin_push_url) +else +RABBITMQ_CURRENT_FETCH_URL ?= $(RABBITMQ_UPSTREAM_FETCH_URL) +RABBITMQ_CURRENT_PUSH_URL ?= $(RABBITMQ_UPSTREAM_PUSH_URL) +endif + +# Macro to replace the following pattern: +# 1. /foo.git -> /bar.git +# 2. /foo -> /bar +# 3. /foo/ -> /bar/ +subst_repo_name = $(patsubst %/$(1)/%,%/$(2)/%,$(patsubst %/$(1),%/$(2),$(patsubst %/$(1).git,%/$(2).git,$(3)))) + +# Macro to replace both the project's name (eg. "rabbit_common") and +# repository name (eg. "rabbitmq-common") by the target's equivalent. +# +# This macro is kept on one line because we don't want whitespaces in +# the returned value, as it's used in $(dep_fetch_git_rmq) in a shell +# single-quoted string. +dep_rmq_repo = $(if $(dep_$(2)),$(call subst_repo_name,$(PROJECT),$(2),$(call subst_repo_name,$(RABBITMQ_COMPONENT_REPO_NAME),$(call rmq_cmp_repo_name,$(2)),$(1))),$(pkg_$(1)_repo)) + +dep_rmq_commits = $(if $(dep_$(1)), \ + $(wordlist 3,$(words $(dep_$(1))),$(dep_$(1))), \ + $(pkg_$(1)_commit)) + +define dep_fetch_git_rmq + fetch_url1='$(call dep_rmq_repo,$(RABBITMQ_CURRENT_FETCH_URL),$(1))'; \ + fetch_url2='$(call dep_rmq_repo,$(RABBITMQ_UPSTREAM_FETCH_URL),$(1))'; \ + if test "$$$$fetch_url1" != '$(RABBITMQ_CURRENT_FETCH_URL)' && \ + git clone -q -n -- "$$$$fetch_url1" $(DEPS_DIR)/$(call dep_name,$(1)); then \ + fetch_url="$$$$fetch_url1"; \ + push_url='$(call dep_rmq_repo,$(RABBITMQ_CURRENT_PUSH_URL),$(1))'; \ + elif git clone -q -n -- "$$$$fetch_url2" $(DEPS_DIR)/$(call dep_name,$(1)); then \ + fetch_url="$$$$fetch_url2"; \ + push_url='$(call dep_rmq_repo,$(RABBITMQ_UPSTREAM_PUSH_URL),$(1))'; \ + fi; \ + cd $(DEPS_DIR)/$(call dep_name,$(1)) && ( \ + $(foreach ref,$(call dep_rmq_commits,$(1)), \ + git checkout -q $(ref) >/dev/null 2>&1 || \ + ) \ + (echo "error: no valid pathspec among: $(call dep_rmq_commits,$(1))" \ + 1>&2 && false) ) && \ + (test "$$$$fetch_url" = "$$$$push_url" || \ + git remote set-url --push origin "$$$$push_url") +endef + +# -------------------------------------------------------------------- +# Component distribution. +# -------------------------------------------------------------------- + +list-dist-deps:: + @: + +prepare-dist:: + @: + +# -------------------------------------------------------------------- +# Umbrella-specific settings. +# -------------------------------------------------------------------- + +# If the top-level project is a RabbitMQ component, we override +# $(DEPS_DIR) for this project to point to the top-level's one. +# +# We also verify that the guessed DEPS_DIR is actually named `deps`, +# to rule out any situation where it is a coincidence that we found a +# `rabbitmq-components.mk` up upper directories. + +possible_deps_dir_1 = $(abspath ..) +possible_deps_dir_2 = $(abspath ../../..) + +ifeq ($(notdir $(possible_deps_dir_1)),deps) +ifneq ($(wildcard $(possible_deps_dir_1)/../rabbitmq-components.mk),) +deps_dir_overriden = 1 +DEPS_DIR ?= $(possible_deps_dir_1) +DISABLE_DISTCLEAN = 1 +endif +endif + +ifeq ($(deps_dir_overriden),) +ifeq ($(notdir $(possible_deps_dir_2)),deps) +ifneq ($(wildcard $(possible_deps_dir_2)/../rabbitmq-components.mk),) +deps_dir_overriden = 1 +DEPS_DIR ?= $(possible_deps_dir_2) +DISABLE_DISTCLEAN = 1 +endif +endif +endif + +ifneq ($(wildcard UMBRELLA.md),) +DISABLE_DISTCLEAN = 1 +endif + +# We disable `make distclean` so $(DEPS_DIR) is not accidentally removed. + +ifeq ($(DISABLE_DISTCLEAN),1) +ifneq ($(filter distclean distclean-deps,$(MAKECMDGOALS)),) +SKIP_DEPS = 1 +endif +endif diff --git a/deps/rabbitmq_cli/test/core/args_processing_test.exs b/deps/rabbitmq_cli/test/core/args_processing_test.exs new file mode 100644 index 0000000000..18c67d3a4a --- /dev/null +++ b/deps/rabbitmq_cli/test/core/args_processing_test.exs @@ -0,0 +1,90 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ArgsProcessingTest do + use ExUnit.Case, async: false + import TestHelper + + defp list_commands() do + [ + RabbitMQ.CLI.Ctl.Commands.ListBindingsCommand, + RabbitMQ.CLI.Ctl.Commands.ListChannelsCommand, + RabbitMQ.CLI.Ctl.Commands.ListConnectionsCommand, + RabbitMQ.CLI.Ctl.Commands.ListConsumersCommand, + RabbitMQ.CLI.Ctl.Commands.ListExchangesCommand, + RabbitMQ.CLI.Ctl.Commands.ListQueuesCommand, + RabbitMQ.CLI.Ctl.Commands.ListVhostsCommand + ] + end + + defp all_commands() do + RabbitMQ.CLI.Core.CommandModules.load_commands(:all, %{}) + |> Map.values + end + + defp line_filter([_, description]) do + Regex.match?(~r/must be one of/, description) + end + defp line_filter(line) do + Regex.match?(~r/must be one of/, line) + end + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + on_exit(context, fn -> delete_user(context[:user]) end) + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 50_000, vhost: "/"}} + end + + test "defaults are merged with positinal args", _context do + commands = all_commands() + Enum.each(commands, + fn(command) -> + command.merge_defaults([], %{}) + command.merge_defaults(["arg"], %{}) + command.merge_defaults(["two", "args"], %{}) + command.merge_defaults(["even", "more", "args"], %{}) + + command.merge_defaults([], %{unknown: "option"}) + command.merge_defaults(["arg"], %{unknown: "option"}) + end) + end + + # this test parses info keys mentioned in the usage_additional section + # and makes sure they pass validation, including when separated by a comma + # or a mix of commas and spaces + test "comma-separated info items are supported", context do + commands = list_commands() + Enum.each(commands, fn(command) -> + items_usage = case command.usage_additional() do + # find the line with info items, ignore the rest + list when is_list(list) -> + # list items can be strings or pairs + Enum.filter(list, &line_filter/1) |> List.first |> Enum.join(" ") + string -> + string + end + # info_item, info_item2, … + case Regex.run(~r/.*one of (.*)$/, items_usage, [capture: :all_but_first]) do + nil -> + throw "Command #{command} does not list info items in usage_additional or the format has changed. Output: #{items_usage}" + [info_items] -> + :ok = command.validate([info_items], context[:opts]) + :ok = command.validate(String.split(info_items, " "), context[:opts]) + run_command_ok(command, [info_items], context[:opts]) + run_command_ok(command, String.split(info_items, " "), context[:opts]) + end + end) + end + + def run_command_ok(command, args_init, options_init) do + {args, options} = command.merge_defaults(args_init, options_init) + assert_stream_without_errors(command.run(args, options)) + end +end diff --git a/deps/rabbitmq_cli/test/core/auto_complete_test.exs b/deps/rabbitmq_cli/test/core/auto_complete_test.exs new file mode 100644 index 0000000000..d410ec6640 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/auto_complete_test.exs @@ -0,0 +1,85 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AutoCompleteTest do + use ExUnit.Case, async: false + + @subject RabbitMQ.CLI.AutoComplete + + + test "Auto-completes a command" do + ["canis_aureus", "canis_latrans", "canis_lupus"] = @subject.complete("rabbitmqctl", ["canis"]) + ["canis_aureus", "canis_latrans", "canis_lupus"] = @subject.complete("rabbitmqctl", ["canis_"]) + ["canis_latrans", "canis_lupus"] = @subject.complete("rabbitmqctl", ["canis_l"]) + ["canis_latrans"] = @subject.complete("rabbitmqctl", ["canis_la"]) + ["canis_aureus"] = @subject.complete("rabbitmqctl", ["canis_a"]) + ["canis_aureus"] = @subject.complete("rabbitmqctl", ["--node", "foo", "--quet", "canis_a"]) + end + + test "Auto-completes default options if command is not specified" do + ["--vhost"] = @subject.complete("rabbitmqctl", ["--vh"]) + ## Prints script_name as script-name + ["--script-name"] = @subject.complete("rabbitmqctl", ["--script"]) + ["--script-name"] = @subject.complete("rabbitmqctl", ["--node", "foo", "--script"]) + end + + test "Auto-completes the command options if full command is specified" do + ["--colour", "--dingo", "--dog"] = @subject.complete("rabbitmqctl", ["canis_lupus", "-"]) + ["--colour", "--dingo", "--dog"] = @subject.complete("rabbitmqctl", ["canis_lupus", "--"]) + ["--dingo", "--dog"] = @subject.complete("rabbitmqctl", ["canis_lupus", "--d"]) + end + + test "Auto-completes scoped command" do + ["enable"] = @subject.complete("rabbitmq-plugins", ["enab"]) + scopes = Application.get_env(:rabbitmqctl, :scopes) + scopes_with_wolf = Keyword.put(scopes, :rabbitmq_wolf, :wolf) + Application.put_env(:rabbitmqctl, :scopes, scopes_with_wolf) + on_exit(fn() -> + Application.put_env(:rabbitmqctl, :scopes, scopes) + end) + + ["canis_aureus", "canis_latrans", "canis_lupus"] = @subject.complete("rabbitmq_wolf", ["canis"]) + end + + test "Auto-completes scoped command with --script-name flag" do + ["enable"] = @subject.complete("rabbitmqctl", ["--script-name", "rabbitmq-plugins", "enab"]) + end +end + +defmodule RabbitMQ.CLI.Wolf.Commands.CanisLupusCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["canis_lupus"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok + def switches(), do: [colour: :string, dingo: :boolean, dog: :boolean] + def scopes, do: [:ctl, :wolf] +end + +defmodule RabbitMQ.CLI.Wolf.Commands.CanisLatransCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["canis_latrans"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok + def scopes, do: [:ctl, :wolf] +end + +defmodule RabbitMQ.CLI.Wolf.Commands.CanisAureusCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["canis_aureus"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok + def scopes, do: [:ctl, :wolf] +end diff --git a/deps/rabbitmq_cli/test/core/command_modules_test.exs b/deps/rabbitmq_cli/test/core/command_modules_test.exs new file mode 100644 index 0000000000..8617415a22 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/command_modules_test.exs @@ -0,0 +1,202 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CommandModulesTest do + use ExUnit.Case, async: false + import TestHelper + + @subject RabbitMQ.CLI.Core.CommandModules + + setup_all do + on_exit(fn -> + set_scope(:none) + Application.put_env(:rabbitmqctl, :commands, nil) + end) + :ok + end + + test "command modules has existing commands" do + assert @subject.load_commands(:all, %{})["duck"] == + RabbitMQ.CLI.Ctl.Commands.DuckCommand + end + + test "command with multiple underscores shows up in map" do + assert @subject.load_commands(:all, %{})["gray_goose"] == + RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand + end + + test "command modules does not have non-existent commands" do + assert @subject.load_commands(:all, %{})["usurper"] == nil + end + + test "non command modules do not show in command map" do + assert @subject.load_commands(:all, %{})["ugly_duckling"] == nil + end + + test "loaded commands are saved in env variable" do + set_scope(:ctl) + commands = @subject.module_map + assert commands == @subject.module_map + assert commands == Application.get_env(:rabbitmqctl, :commands) + end + + test "load commands for current scope" do + set_scope(:ctl) + commands = @subject.load(%{}) + assert commands == @subject.load_commands(:ctl, %{}) + + assert commands["duck"] == RabbitMQ.CLI.Ctl.Commands.DuckCommand + assert commands["gray_goose"] == RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand + + assert commands["stork"] == nil + assert commands["heron"] == nil + + assert commands["crow"] == nil + assert commands["raven"] == nil + + set_scope(:plugins) + commands = @subject.load(%{}) + assert commands == @subject.load_commands(:plugins, %{}) + assert commands["duck"] == nil + assert commands["gray_goose"] == nil + + assert commands["stork"] == RabbitMQ.CLI.Plugins.Commands.StorkCommand + assert commands["heron"] == RabbitMQ.CLI.Plugins.Commands.HeronCommand + + assert commands["crow"] == nil + assert commands["raven"] == nil + end + + test "can set scopes inside command" do + plugin_commands = @subject.load_commands(:plugins, %{}) + + assert plugin_commands["duck"] == nil + assert plugin_commands["gray_goose"] == nil + + assert plugin_commands["stork"] == RabbitMQ.CLI.Plugins.Commands.StorkCommand + assert plugin_commands["heron"] == RabbitMQ.CLI.Plugins.Commands.HeronCommand + + assert plugin_commands["crow"] == nil + assert plugin_commands["raven"] == nil + + # SeagullCommand has scopes() defined as [:plugins, :custom] + assert plugin_commands["seagull"] == RabbitMQ.CLI.Seagull.Commands.SeagullCommand + + custom_commands = @subject.load_commands(:custom, %{}) + + assert custom_commands["duck"] == nil + assert custom_commands["gray_goose"] == nil + + assert custom_commands["stork"] == nil + assert custom_commands["heron"] == nil + + assert custom_commands["crow"] == RabbitMQ.CLI.Custom.Commands.CrowCommand + assert custom_commands["raven"] == RabbitMQ.CLI.Custom.Commands.RavenCommand + + # SeagullCommand has scopes() defined as [:plugins, :custom] + assert custom_commands["seagull"] == RabbitMQ.CLI.Seagull.Commands.SeagullCommand + + end + + ## ------------------- commands/0 tests -------------------- + + test "command_modules has existing commands" do + set_scope(:ctl) + @subject.load(%{}) + assert @subject.module_map["status"] == RabbitMQ.CLI.Ctl.Commands.StatusCommand + assert @subject.module_map["environment"] == RabbitMQ.CLI.Ctl.Commands.EnvironmentCommand + end + + test "command_modules does not have non-existent commands" do + set_scope(:ctl) + @subject.load(%{}) + assert @subject.module_map[:p_equals_np_proof] == nil + end +end + +# Mock command modules for Ctl + +defmodule RabbitMQ.CLI.Ctl.Commands.DuckCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["duck"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +defmodule RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["gray_goose"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +defmodule RabbitMQ.CLI.Ctl.Commands.UglyDucklingCommand do +end + + +# Mock command modules for Plugins + +defmodule RabbitMQ.CLI.Plugins.Commands.StorkCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["stork"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +defmodule RabbitMQ.CLI.Plugins.Commands.HeronCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["heron"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +# Mock command modules for Custom + +defmodule RabbitMQ.CLI.Custom.Commands.CrowCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["crow"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok + def scopes(), do: [:custom, ] +end + +defmodule RabbitMQ.CLI.Custom.Commands.RavenCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["raven"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +defmodule RabbitMQ.CLI.Seagull.Commands.SeagullCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["seagull"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok + def scopes(), do: [:plugins, :custom] +end + + diff --git a/deps/rabbitmq_cli/test/core/default_output_test.exs b/deps/rabbitmq_cli/test/core/default_output_test.exs new file mode 100644 index 0000000000..f567c5cc96 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/default_output_test.exs @@ -0,0 +1,114 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule DefaultOutputTest do + use ExUnit.Case, async: false + + test "ok is passed as is" do + assert match?(:ok, ExampleCommand.output(:ok, %{})) + end + + test "ok with message is passed as is" do + assert match?({:ok, :message}, ExampleCommand.output({:ok, :message}, %{})) + assert match?({:ok, {:complex, "message"}}, ExampleCommand.output({:ok, {:complex, "message"}}, %{})) + end + + test "enumerable is passed as stream" do + assert match?({:stream, 'list'}, ExampleCommand.output({:ok, 'list'}, %{})) + assert match?({:stream, 'list'}, ExampleCommand.output('list', %{})) + + assert match?({:stream, [1,2,3]}, ExampleCommand.output({:ok, [1,2,3]}, %{})) + assert match?({:stream, [1,2,3]}, ExampleCommand.output([1,2,3], %{})) + + stream = Stream.timer(10000) + assert match?({:stream, ^stream}, ExampleCommand.output({:ok, stream}, %{})) + assert match?({:stream, ^stream}, ExampleCommand.output(stream, %{})) + end + + test "badrpc is an error" do + {:error, {:badrpc, :nodedown}} = + ExampleCommand.output({:badrpc, :nodedown}, %{}) + + {:error, {:badrpc, :timeout}} = + ExampleCommand.output({:badrpc, :timeout}, %{}) + end + + test "unknown atom is error" do + {:error, :error_message} = ExampleCommand.output(:error_message, %{}) + end + + test "unknown tuple is error" do + {:error, {:left, :right}} = ExampleCommand.output({:left, :right}, %{}) + end + + test "error_string is error" do + assert {:error, "I am string"} == ExampleCommand.output({:error_string, "I am string"}, %{}) + end + + test "error_string is converted to string" do + assert match?({:error, "I am charlist"}, + ExampleCommand.output({:error_string, 'I am charlist'}, %{})) + end + + test "error is formatted" do + {:error, "I am formatted \"string\""} = + ExampleCommand.output({:error, 'I am formatted ~p', ['string']}, %{}) + end + + test "non atom value is ok" do + val = "foo" + assert match?({:ok, ^val}, ExampleCommand.output(val, %{})) + val = 125 + assert match?({:ok, ^val}, ExampleCommand.output(val, %{})) + val = 100.2 + assert match?({:ok, ^val}, ExampleCommand.output(val, %{})) + val = {:one, :two, :three} + assert match?({:ok, ^val}, ExampleCommand.output(val, %{})) + end + + test "custom output function can be defined" do + assert {:error, 125, "Non standard"} == ExampleCommandWithCustomOutput.output(:non_standard_output, %{}) + end + + test "default output works even if custom output is defined" do + assert :ok == ExampleCommandWithCustomOutput.output(:ok, %{}) + assert {:ok, {:complex, "message"}} == ExampleCommandWithCustomOutput.output({:ok, {:complex, "message"}}, %{}) + + assert {:stream, [1,2,3]} == ExampleCommandWithCustomOutput.output({:ok, [1,2,3]}, %{}) + assert {:stream, [1,2,3]} == ExampleCommandWithCustomOutput.output([1,2,3], %{}) + + assert {:error, {:badrpc, :nodedown}} == + ExampleCommandWithCustomOutput.output({:badrpc, :nodedown}, %{}) + assert {:error, {:badrpc, :timeout}} == + ExampleCommandWithCustomOutput.output({:badrpc, :timeout}, %{}) + + error = %{i: [am: "arbitrary", error: 1]} + {:error, ^error} = ExampleCommandWithCustomOutput.output({:error, error}, %{}) + + assert {:error, "I am string"} == ExampleCommandWithCustomOutput.output({:error_string, "I am string"}, %{}) + + val = "foo" + assert match?({:ok, ^val}, ExampleCommandWithCustomOutput.output(val, %{})) + val = 125 + assert match?({:ok, ^val}, ExampleCommandWithCustomOutput.output(val, %{})) + val = 100.2 + assert match?({:ok, ^val}, ExampleCommandWithCustomOutput.output(val, %{})) + val = {:one, :two, :three} + assert match?({:ok, ^val}, ExampleCommandWithCustomOutput.output(val, %{})) + end +end + +defmodule ExampleCommand do + use RabbitMQ.CLI.DefaultOutput +end + +defmodule ExampleCommandWithCustomOutput do + def output(:non_standard_output, _) do + {:error, 125, "Non standard"} + end + use RabbitMQ.CLI.DefaultOutput +end diff --git a/deps/rabbitmq_cli/test/core/distribution_test.exs b/deps/rabbitmq_cli/test/core/distribution_test.exs new file mode 100644 index 0000000000..00dd872ab4 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/distribution_test.exs @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +alias RabbitMQ.CLI.Core.Distribution + +defmodule DistributionTest do + use ExUnit.Case, async: false + + setup_all do + :net_kernel.stop() + :ok + end + + test "set cookie via environment variable" do + on_exit(fn -> + :net_kernel.stop() + System.delete_env("RABBITMQ_ERLANG_COOKIE") + end) + try do + :nocookie = Node.get_cookie() + catch + # one of net_kernel processes is not running ¯\_(ツ)_/¯ + :exit, _ -> :ok + end + System.put_env("RABBITMQ_ERLANG_COOKIE", "mycookie") + opts = %{} + Distribution.start(opts) + :mycookie = Node.get_cookie() + end + + test "set cookie via argument" do + on_exit(fn -> + :net_kernel.stop() + end) + try do + :nocookie = Node.get_cookie() + catch + # one of net_kernel processes is not running ¯\_(ツ)_/¯ + :exit, _ -> :ok + end + opts = %{erlang_cookie: :mycookie} + Distribution.start(opts) + :mycookie = Node.get_cookie() + end +end diff --git a/deps/rabbitmq_cli/test/core/helpers_test.exs b/deps/rabbitmq_cli/test/core/helpers_test.exs new file mode 100644 index 0000000000..71d107bef8 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/helpers_test.exs @@ -0,0 +1,140 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule HelpersTest do + alias RabbitMQ.CLI.Core.{Config, Helpers} + import RabbitMQ.CLI.Core.{CodePath, Memory} + + use ExUnit.Case, async: false + import TestHelper + + ## --------------------- get_rabbit_hostname()/0 tests ------------------------- + + test "RabbitMQ hostname is properly formed" do + assert Helpers.get_rabbit_hostname() |> Atom.to_string =~ ~r/rabbit@\w+/ + end + + ## ------------------- memory_unit* tests -------------------- + + test "an invalid memory unit fails " do + assert memory_unit_absolute(10, "gigantibytes") == {:bad_argument, ["gigantibytes"]} + end + + test "an invalid number fails " do + assert memory_unit_absolute("lots", "gigantibytes") == {:bad_argument, ["lots", "gigantibytes"]} + assert memory_unit_absolute(-1, "gigantibytes") == {:bad_argument, [-1, "gigantibytes"]} + end + + test "valid number and unit returns a valid result " do + assert memory_unit_absolute(10, "k") == 10240 + assert memory_unit_absolute(10, "kiB") == 10240 + assert memory_unit_absolute(10, "M") == 10485760 + assert memory_unit_absolute(10, "MiB") == 10485760 + assert memory_unit_absolute(10, "G") == 10737418240 + assert memory_unit_absolute(10, "GiB")== 10737418240 + assert memory_unit_absolute(10, "kB")== 10000 + assert memory_unit_absolute(10, "MB")== 10000000 + assert memory_unit_absolute(10, "GB")== 10000000000 + assert memory_unit_absolute(10, "") == 10 + end + + ## ------------------- Helpers.normalise_node_option tests -------------------- + + test "longnames: 'rabbit' as node name, correct domain is used" do + default_name = Config.default(:node) + options = %{node: default_name, longnames: true} + {:ok, options} = Helpers.normalise_node_option(options) + assert options[:node] == :"rabbit@#{hostname()}.#{domain()}" + end + + test "shortnames: 'rabbit' as node name, no domain is used" do + options = %{node: :rabbit, longnames: false} + {:ok, options} = Helpers.normalise_node_option(options) + assert options[:node] == :"rabbit@#{hostname()}" + end + + ## ------------------- normalise_node tests (:shortnames) -------------------- + + test "shortnames: if nil input, retrieve standard rabbit hostname" do + assert Helpers.normalise_node(nil, :shortnames) == get_rabbit_hostname() + end + + test "shortnames: if input is an atom short name, return the atom with hostname" do + want = String.to_atom("rabbit_test@#{hostname()}") + got = Helpers.normalise_node(:rabbit_test, :shortnames) + assert want == got + end + + test "shortnames: if input is a string fully qualified node name, return an atom" do + want = String.to_atom("rabbit_test@#{hostname()}") + got = Helpers.normalise_node("rabbit_test@#{hostname()}", :shortnames) + assert want == got + end + + test "shortnames: if input is a short node name, host name is added" do + want = String.to_atom("rabbit_test@#{hostname()}") + got = Helpers.normalise_node("rabbit_test", :shortnames) + assert want == got + end + + test "shortnames: if input is a hostname without a node name, default node name is added" do + default_name = Config.default(:node) + want = String.to_atom("#{default_name}@#{hostname()}") + got = Helpers.normalise_node("@#{hostname()}", :shortnames) + assert want == got + end + + test "shortnames: if input is a short node name with an @ and no hostname, local host name is added" do + want = String.to_atom("rabbit_test@#{hostname()}") + got = Helpers.normalise_node("rabbit_test@", :shortnames) + assert want == got + end + + test "shortnames: if input contains more than one @, return an atom" do + want = String.to_atom("rabbit@rabbit_test@#{hostname()}") + got = Helpers.normalise_node("rabbit@rabbit_test@#{hostname()}", :shortnames) + assert want == got + end + + ## ------------------- normalise_node tests (:longnames) -------------------- + + test "longnames: if nil input, retrieve standard rabbit hostname" do + want = get_rabbit_hostname(:longnames) + got = Helpers.normalise_node(nil, :longnames) + assert want == got + end + + test "longnames: if input is an atom short name, return the atom with full hostname" do + want = String.to_atom("rabbit_test@#{hostname()}.#{domain()}") + got = Helpers.normalise_node(:rabbit_test, :longnames) + assert want == got + end + + ## ------------------- require_rabbit/1 tests -------------------- + + test "locate plugin with version number in filename" do + plugins_directory_03 = fixture_plugins_path("plugins-subdirectory-03") + rabbitmq_home = :rabbit_misc.rpc_call(node(), :code, :lib_dir, [:rabbit]) + opts = %{plugins_dir: to_string(plugins_directory_03), + rabbitmq_home: rabbitmq_home} + assert Enum.member?(Application.loaded_applications(), {:mock_rabbitmq_plugins_03, 'New project', '0.1.0'}) == false + require_rabbit_and_plugins(opts) + Application.load(:mock_rabbitmq_plugins_03) + assert Enum.member?(Application.loaded_applications(), {:mock_rabbitmq_plugins_03, 'New project', '0.1.0'}) + end + + test "locate plugin without version number in filename" do + plugins_directory_04 = fixture_plugins_path("plugins-subdirectory-04") + rabbitmq_home = :rabbit_misc.rpc_call(node(), :code, :lib_dir, [:rabbit]) + opts = %{plugins_dir: to_string(plugins_directory_04), + rabbitmq_home: rabbitmq_home} + assert Enum.member?(Application.loaded_applications(), {:mock_rabbitmq_plugins_04, 'New project', 'rolling'}) == false + require_rabbit_and_plugins(opts) + Application.load(:mock_rabbitmq_plugins_04) + assert Enum.member?(Application.loaded_applications(), {:mock_rabbitmq_plugins_04, 'New project', 'rolling'}) + end + +end diff --git a/deps/rabbitmq_cli/test/core/information_unit_test.exs b/deps/rabbitmq_cli/test/core/information_unit_test.exs new file mode 100644 index 0000000000..568b687b2d --- /dev/null +++ b/deps/rabbitmq_cli/test/core/information_unit_test.exs @@ -0,0 +1,44 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule InformationUnitTest do + use ExUnit.Case, async: true + + alias RabbitMQ.CLI.InformationUnit, as: IU + + test "bytes, MB, GB, TB are known units" do + Enum.each(["bytes", "mb", "MB", "gb", "GB", "tb", "TB"], + fn x -> assert IU.known_unit?(x) end) + end + + test "glip-glops, millibars, gold pressed latinum bars and looney and are not known units" do + Enum.each(["glip-glops", "millibars", "gold pressed latinum bars", "looney"], + fn x -> assert not IU.known_unit?(x) end) + end + + test "conversion to bytes" do + assert IU.convert(0, "bytes") == 0 + assert IU.convert(100, "bytes") == 100 + assert IU.convert(9988, "bytes") == 9988 + end + + test "conversion to MB" do + assert IU.convert(1000000, "mb") == 1.0 + assert IU.convert(9500000, "mb") == 9.5 + assert IU.convert(97893000, "mb") == 97.893 + assert IU.convert(978930000, "mb") == 978.93 + end + + test "conversion to GB" do + assert IU.convert(978930000, "gb") == 0.9789 + + assert IU.convert(1000000000, "gb") == 1.0 + assert IU.convert(9500000000, "gb") == 9.5 + assert IU.convert(97893000000, "gb") == 97.893 + assert IU.convert(978930000000, "gb") == 978.93 + end +end diff --git a/deps/rabbitmq_cli/test/core/json_stream_test.exs b/deps/rabbitmq_cli/test/core/json_stream_test.exs new file mode 100644 index 0000000000..ab3bebd62c --- /dev/null +++ b/deps/rabbitmq_cli/test/core/json_stream_test.exs @@ -0,0 +1,24 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2019-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule JsonStreamTest do + use ExUnit.Case, async: false + + @formatter RabbitMQ.CLI.Formatters.JsonStream + + test "format_output map with atom keys is converted to JSON object" do + assert @formatter.format_output(%{a: :apple, b: :beer}, %{}) == "{\"a\":\"apple\",\"b\":\"beer\"}" + end + + test "format_output map with binary keys is converted to JSON object" do + assert @formatter.format_output(%{"a" => :apple, "b" => :beer}, %{}) == "{\"a\":\"apple\",\"b\":\"beer\"}" + end + + test "format_output empty binary is converted to empty JSON array" do + assert @formatter.format_output("", %{}) == "" + end + +end diff --git a/deps/rabbitmq_cli/test/core/listeners_test.exs b/deps/rabbitmq_cli/test/core/listeners_test.exs new file mode 100644 index 0000000000..266413c6fa --- /dev/null +++ b/deps/rabbitmq_cli/test/core/listeners_test.exs @@ -0,0 +1,64 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CoreListenersTest do + use ExUnit.Case, async: true + + import RabbitMQ.CLI.Core.Listeners + import RabbitCommon.Records + + test "listener record translation to a map" do + assert listener_map(listener(node: :rabbit@mercurio, + protocol: :stomp, + ip_address: {0,0,0,0,0,0,0,0}, + port: 61613)) == + %{ + interface: "[::]", + node: :rabbit@mercurio, + port: 61613, + protocol: :stomp, + purpose: "STOMP" + } + end + + test "[human-readable] protocol labels" do + assert protocol_label(:amqp) == "AMQP 0-9-1 and AMQP 1.0" + assert protocol_label(:'amqp/ssl') == "AMQP 0-9-1 and AMQP 1.0 over TLS" + assert protocol_label(:mqtt) == "MQTT" + assert protocol_label(:'mqtt/ssl') == "MQTT over TLS" + assert protocol_label(:stomp) == "STOMP" + assert protocol_label(:'stomp/ssl') == "STOMP over TLS" + assert protocol_label(:http) == "HTTP API" + assert protocol_label(:https) == "HTTP API over TLS (HTTPS)" + assert protocol_label(:'https/web-stomp') == "STOMP over WebSockets and TLS (HTTPS)" + assert protocol_label(:'https/web-mqtt') == "MQTT over WebSockets and TLS (HTTPS)" + + assert protocol_label(:'http/prometheus') == "Prometheus exporter API over HTTP" + assert protocol_label(:'https/prometheus') == "Prometheus exporter API over TLS (HTTPS)" + end + + test "listener expiring within" do + validityInDays = 10 + validity = X509.Certificate.Validity.days_from_now(validityInDays) + ca_key = X509.PrivateKey.new_ec(:secp256r1) + ca = X509.Certificate.self_signed(ca_key, + "/C=US/ST=CA/L=San Francisco/O=Megacorp/CN=Megacorp Intermediate CA", + template: :root_ca, + validity: validity + ) + pem = X509.Certificate.to_pem(ca) + + opts = [{:certfile, {:pem, pem}}, {:cacertfile, {:pem, pem}}] + listener = listener(node: :rabbit@mercurio, + protocol: :stomp, + ip_address: {0,0,0,0,0,0,0,0}, + port: 61613, + opts: opts) + + assert not listener_expiring_within(listener, 86400 * (validityInDays - 5)) + assert listener_expiring_within(listener, 86400 * (validityInDays + 5)) + end +end diff --git a/deps/rabbitmq_cli/test/core/node_name_test.exs b/deps/rabbitmq_cli/test/core/node_name_test.exs new file mode 100644 index 0000000000..89bc1484bc --- /dev/null +++ b/deps/rabbitmq_cli/test/core/node_name_test.exs @@ -0,0 +1,73 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule NodeNameTest do + use ExUnit.Case, async: true + + @subject RabbitMQ.CLI.Core.NodeName + + test "shortnames: RabbitMQ nodename is properly formed from atom" do + want = String.to_atom("rabbit@#{:inet_db.gethostname()}") + {:ok, got} = @subject.create(:rabbit, :shortnames) + assert want == got + end + + test "shortnames: RabbitMQ nodename is properly formed from string" do + want = String.to_atom("rabbit@#{:inet_db.gethostname()}") + {:ok, got} = @subject.create("rabbit", :shortnames) + assert want == got + end + + test "shortnames: RabbitMQ nodename is properly formed with trailing @" do + want = String.to_atom("rabbit@#{:inet_db.gethostname()}") + {:ok, got} = @subject.create(:rabbit@, :shortnames) + assert want == got + end + + test "shortnames: RabbitMQ nodename is properly formed with host part" do + want = :rabbit@foofoo + {:ok, got} = @subject.create(want, :shortnames) + assert want == got + end + + test "shortnames: nodename head only supports alphanumerics, underscores and hyphens in name head" do + {:error, {:node_name, :invalid_node_name_head}} = @subject.create("кириллица", :shortnames) + end + + test "longnames: RabbitMQ nodename is properly formed from atom" do + {:ok, got} = @subject.create(:rabbit, :longnames) + assert Atom.to_string(got) =~ ~r/rabbit@[\w\-]+\.\w+/ + end + + test "longnames: RabbitMQ nodename is properly formed from string" do + {:ok, got} = @subject.create("rabbit", :longnames) + assert Atom.to_string(got) =~ ~r/rabbit@[\w\-]+\.\w+/ + end + + test "longnames: RabbitMQ nodename is properly formed from atom with domain" do + want = :"rabbit@localhost.localdomain" + {:ok, got} = @subject.create(want, :longnames) + assert want == got + end + + test "longnames: RabbitMQ nodename is properly formed from string with domain" do + name_str = "rabbit@localhost.localdomain" + want = String.to_atom(name_str) + {:ok, got} = @subject.create(name_str, :longnames) + assert want == got + end + + test "longnames: RabbitMQ nodename is properly formed from string with partial domain" do + name_str = "rabbit@localhost" + want = String.to_atom(name_str <> "." <> @subject.domain()) + {:ok, got} = @subject.create(name_str, :longnames) + assert want == got + end + + test "longnames: nodename head only supports alphanumerics, underscores and hyphens in name head" do + {:error, {:node_name, :invalid_node_name_head}} = @subject.create("кириллица", :longnames) + end +end diff --git a/deps/rabbitmq_cli/test/core/os_pid_test.exs b/deps/rabbitmq_cli/test/core/os_pid_test.exs new file mode 100644 index 0000000000..2d110f591f --- /dev/null +++ b/deps/rabbitmq_cli/test/core/os_pid_test.exs @@ -0,0 +1,54 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule OsPidTest do + use ExUnit.Case, async: false + import TestHelper + + @subject RabbitMQ.CLI.Core.OsPid + + # + # Tests + # + + describe "#read_pid_from_file with should_wait = false" do + test "with a valid pid file returns an integer value" do + path = fixture_file_path("valid_pidfile.pid") + + assert (File.exists?(path) and File.regular?(path)) + assert @subject.read_pid_from_file(path, false) == 13566 + end + + test "with a valid pid file that includes spaces returns an integer value" do + path = fixture_file_path("valid_pidfile_with_spaces.pid") + + assert (File.exists?(path) and File.regular?(path)) + assert @subject.read_pid_from_file(path, false) == 83777 + end + + test "with an empty file" do + path = fixture_file_path("empty_pidfile.pid") + + assert (File.exists?(path) and File.regular?(path)) + assert match?({:error, :could_not_read_pid_from_file, _}, @subject.read_pid_from_file(path, false)) + end + + test "with a non-empty file full of garbage (that doesn't parse)" do + path = fixture_file_path("invalid_pidfile.pid") + + assert (File.exists?(path) and File.regular?(path)) + assert match?({:error, :could_not_read_pid_from_file, _}, @subject.read_pid_from_file(path, false)) + end + + test "with a file that does not exist" do + path = fixture_file_path("pidfile_that_does_not_exist_128787df8s7f8%4&^.pid") + + assert !File.exists?(path) + assert match?({:error, :could_not_read_pid_from_file, _}, @subject.read_pid_from_file(path, false)) + end + end +end diff --git a/deps/rabbitmq_cli/test/core/parser_test.exs b/deps/rabbitmq_cli/test/core/parser_test.exs new file mode 100644 index 0000000000..b483db1fdd --- /dev/null +++ b/deps/rabbitmq_cli/test/core/parser_test.exs @@ -0,0 +1,369 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +## Mock command for command specific parser +defmodule RabbitMQ.CLI.Seagull.Commands.HerringGullCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["herring_gull"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok + def switches(), do: [herring: :string, garbage: :boolean] + def aliases(), do: [h: :herring, g: :garbage] +end + +defmodule RabbitMQ.CLI.Seagull.Commands.PacificGullCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["pacific_gull"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +defmodule RabbitMQ.CLI.Seagull.Commands.HermannGullCommand do + @behaviour RabbitMQ.CLI.CommandBehaviour + use RabbitMQ.CLI.DefaultOutput + def usage(), do: ["hermann_gull"] + def validate(_,_), do: :ok + def merge_defaults(_,_), do: {[], %{}} + def banner(_,_), do: "" + def run(_,_), do: :ok +end + +defmodule ParserTest do + use ExUnit.Case, async: true + import ExUnit.CaptureIO + import TestHelper + + @subject RabbitMQ.CLI.Core.Parser + + setup_all do + Code.ensure_loaded(RabbitMQ.CLI.Seagull.Commands.HerringGullCommand) + Code.ensure_loaded(RabbitMQ.CLI.Seagull.Commands.PacificGullCommand) + set_scope(:seagull) + on_exit(fn -> + set_scope(:none) + end) + :ok + end + + test "one arity 0 command, no options" do + assert @subject.parse_global(["sandwich"]) == {["sandwich"], %{}, []} + end + + test "one arity 1 command, no options" do + assert @subject.parse_global(["sandwich", "pastrami"]) == {["sandwich", "pastrami"], %{}, []} + end + + test "no commands, no options (empty string)" do + assert @subject.parse_global([""]) == {[""], %{}, []} + end + + test "no commands, no options (empty array)" do + assert @subject.parse_global([]) == {[],%{}, []} + end + + test "one arity 1 command, one double-dash quiet flag" do + assert @subject.parse_global(["sandwich", "pastrami", "--quiet"]) == + {["sandwich", "pastrami"], %{quiet: true}, []} + end + + test "one arity 1 command, one single-dash quiet flag" do + assert @subject.parse_global(["sandwich", "pastrami", "-q"]) == + {["sandwich", "pastrami"], %{quiet: true}, []} + end + + test "one arity 1 command, one double-dash silent flag" do + assert @subject.parse_global(["sandwich", "pastrami", "--silent"]) == + {["sandwich", "pastrami"], %{silent: true}, []} + end + + test "one arity 1 command, one single-dash silent flag" do + assert @subject.parse_global(["sandwich", "pastrami", "-s"]) == + {["sandwich", "pastrami"], %{silent: true}, []} + end + + test "one arity 0 command, one single-dash node option" do + assert @subject.parse_global(["sandwich", "-n", "rabbitmq@localhost"]) == + {["sandwich"], %{node: :rabbitmq@localhost}, []} + end + + test "one arity 1 command, one single-dash node option" do + assert @subject.parse_global(["sandwich", "pastrami", "-n", "rabbitmq@localhost"]) == + {["sandwich", "pastrami"], %{node: :rabbitmq@localhost}, []} + end + + test "one arity 1 command, one single-dash node option and one quiet flag" do + assert @subject.parse_global(["sandwich", "pastrami", "-n", "rabbitmq@localhost", "--quiet"]) == + {["sandwich", "pastrami"], %{node: :rabbitmq@localhost, quiet: true}, []} + end + + test "single-dash node option before command" do + assert @subject.parse_global(["-n", "rabbitmq@localhost", "sandwich", "pastrami"]) == + {["sandwich", "pastrami"], %{node: :rabbitmq@localhost}, []} + end + + test "no commands, one double-dash node option" do + assert @subject.parse_global(["--node=rabbitmq@localhost"]) == {[], %{node: :rabbitmq@localhost}, []} + end + + test "no commands, one single-dash -p option" do + assert @subject.parse_global(["-p", "sandwich"]) == {[], %{vhost: "sandwich"}, []} + end + + test "global parse treats command-specific arguments as invalid (ignores them)" do + command_line = ["seagull", "--herring", "atlantic", "-g", "-p", "my_vhost"] + {args, options, invalid} = @subject.parse_global(command_line) + assert {args, options, invalid} == + {["seagull", "atlantic"], %{vhost: "my_vhost"}, [{"--herring", nil}, {"-g", nil}]} + end + + test "global parse treats command-specific arguments that are separated by an equals sign as invalid (ignores them)" do + command_line = ["seagull", "--herring=atlantic", "-g", "-p", "my_vhost"] + {args, options, invalid} = @subject.parse_global(command_line) + assert {args, options, invalid} == + {["seagull"], %{vhost: "my_vhost"}, [{"--herring", nil}, {"-g", nil}]} + end + + test "command-specific parse recognizes command switches" do + command_line = ["seagull", "--herring", "atlantic", "-g", "-p", "my_vhost"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse_command_specific(command_line, command) == + {["seagull"], %{vhost: "my_vhost", herring: "atlantic", garbage: true}, []} + end + + test "command-specific parse recognizes command switches that are separated by an equals sign" do + command_line = ["seagull", "--herring=atlantic", "-g", "-p", "my_vhost"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse_command_specific(command_line, command) == + {["seagull"], %{vhost: "my_vhost", herring: "atlantic", garbage: true}, []} + end + + test "command-specific switches and aliases are optional" do + command_line = ["seagull", "-p", "my_vhost"] + command = RabbitMQ.CLI.Seagull.Commands.PacificGullCommand + assert @subject.parse_command_specific(command_line, command) == + {["seagull"], %{vhost: "my_vhost"}, []} + end + + test "--timeout can be specified before command" do + # for backwards compatibility + assert @subject.parse_global(["-n", "rabbitmq@localhost", "--timeout", "5", "sandwich", "pastrami"]) == + {["sandwich", "pastrami"], %{node: :rabbitmq@localhost, timeout: 5}, []} + end + + test "-t can be specified before command" do + # for backwards compatibility + assert @subject.parse_global(["-n", "rabbitmq@localhost", "-t", "5", "sandwich", "pastrami"]) == + {["sandwich", "pastrami"], %{node: :rabbitmq@localhost, timeout: 5}, []} + end + + test "parse/1 returns command name" do + command_line = ["pacific_gull", "fly", "-p", "my_vhost"] + command = RabbitMQ.CLI.Seagull.Commands.PacificGullCommand + assert @subject.parse(command_line) == + {command, "pacific_gull", ["fly"], %{vhost: "my_vhost"}, []} + end + + test "parse/1 returns command name when a global flag comes before the command" do + command_line = ["-p", "my_vhost", "pacific_gull", "fly"] + command = RabbitMQ.CLI.Seagull.Commands.PacificGullCommand + assert @subject.parse(command_line) == + {command, "pacific_gull", ["fly"], %{vhost: "my_vhost"}, []} + end + + test "parse/1 returns command name when a global flag separated by an equals sign comes before the command" do + command_line = ["-p=my_vhost", "pacific_gull", "fly"] + command = RabbitMQ.CLI.Seagull.Commands.PacificGullCommand + assert @subject.parse(command_line) == + {command, "pacific_gull", ["fly"], %{vhost: "my_vhost"}, []} + end + + test "parse/1 returns :no_command when given an empty argument list" do + command_line = ["-p", "my_vhost"] + assert @subject.parse(command_line) == + {:no_command, "", [], %{vhost: "my_vhost"}, []} + end + + test "parse/1 returns :no_command and command name when command isn't known" do + command_line = ["atlantic_gull", "-p", "my_vhost"] + assert @subject.parse(command_line) == + {:no_command, "atlantic_gull", [], %{vhost: "my_vhost"}, []} + end + + test "parse/1 returns :no command if command-specific options come before the command" do + command_line = ["--herring", "atlantic", "herring_gull", "-p", "my_vhost"] + assert @subject.parse(command_line) == + {:no_command, "atlantic", ["herring_gull"], + %{vhost: "my_vhost"}, [{"--herring", nil}]} + end + + test "parse/1 returns command name if a global option comes before the command" do + command_line = ["-p", "my_vhost", "herring_gull"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse(command_line) == + {command, "herring_gull", [], %{vhost: "my_vhost"}, []} + end + + test "parse/1 returns command name if multiple global options come before the command" do + command_line = ["-p", "my_vhost", "-q", "-n", "rabbit@test", "herring_gull"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse(command_line) == + {command, "herring_gull", [], %{vhost: "my_vhost", node: :rabbit@test, quiet: true}, []} + end + + test "parse/1 returns command name if multiple global options separated by an equals sign come before the command" do + command_line = ["-p=my_vhost", "-q", "--node=rabbit@test", "herring_gull"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse(command_line) == + {command, "herring_gull", [], %{vhost: "my_vhost", node: :rabbit@test, quiet: true}, []} + end + + test "parse/1 returns command with command specific options" do + command_line = ["herring_gull", "--herring", "atlantic", + "-g", "fly", "-p", "my_vhost"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse(command_line) == + {command, "herring_gull", ["fly"], + %{vhost: "my_vhost", herring: "atlantic", garbage: true}, []} + end + + test "parse/1 returns command with command specific options that are separated by an equals sign" do + command_line = ["herring_gull", "--herring=atlantic", + "-g", "fly", "-p=my_vhost"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse(command_line) == + {command, "herring_gull", ["fly"], + %{vhost: "my_vhost", herring: "atlantic", garbage: true}, []} + end + + test "parse/1 expands command-defined aliases" do + command_line = ["herring_gull", "fly", "-g"] + command = RabbitMQ.CLI.Seagull.Commands.HerringGullCommand + assert @subject.parse(command_line) == + {command, "herring_gull", ["fly"], %{garbage: true}, []} + end + + test "parse/1 returns invalid/extra options for command" do + command_line = ["pacific_gull", "fly", + "--herring", "atlantic", + "-p", "my_vhost"] + pacific_gull = RabbitMQ.CLI.Seagull.Commands.PacificGullCommand + assert @subject.parse(command_line) == + {pacific_gull, "pacific_gull", ["fly", "atlantic"], + %{vhost: "my_vhost"}, + [{"--herring", nil}]} + end + + test "parse/1 suggests similar command" do + # One letter difference + assert @subject.parse(["pacific_gulf"]) == + {{:suggest, "pacific_gull"}, "pacific_gulf", [], %{}, []} + + # One letter missing + assert @subject.parse(["pacific_gul"]) == + {{:suggest, "pacific_gull"}, "pacific_gul", [], %{}, []} + + # One letter extra + assert @subject.parse(["pacific_gulll"]) == + {{:suggest, "pacific_gull"}, "pacific_gulll", [], %{}, []} + + # Five letter difference + assert @subject.parse(["pacifistcatl"]) == + {{:suggest, "pacific_gull"}, "pacifistcatl", [], %{}, []} + + # Five letters missing + assert @subject.parse(["pacific"]) == + {{:suggest, "pacific_gull"}, "pacific", [], %{}, []} + + # Closest to similar + assert @subject.parse(["herrdog_gull"]) == + {{:suggest, "herring_gull"}, "herrdog_gull", [], %{}, []} + + # Closest to similar + assert @subject.parse(["hermaug_gull"]) == + {{:suggest, "hermann_gull"}, "hermaug_gull", [], %{}, []} + end + + @tag cd: "fixtures" + test "parse/1 supports aliases" do + aliases = """ + larus_pacificus = pacific_gull + gull_with_herring = herring_gull --herring atlantic + flying_gull = herring_gull fly + garbage_gull = herring_gull -g + complex_gull = herring_gull --herring pacific -g --formatter=erlang eat + invalid_gull = herring_gull --invalid + unknown_gull = mysterious_gull + """ + + aliases_file_name = "aliases.ini" + File.write(aliases_file_name, aliases) + on_exit(fn() -> + File.rm(aliases_file_name) + end) + + assert @subject.parse(["larus_pacificus", "--aliases-file", aliases_file_name]) == + {RabbitMQ.CLI.Seagull.Commands.PacificGullCommand, + "larus_pacificus", + [], + %{aliases_file: aliases_file_name}, + []} + + assert @subject.parse(["gull_with_herring", "--aliases-file", aliases_file_name]) == + {RabbitMQ.CLI.Seagull.Commands.HerringGullCommand, + "gull_with_herring", + [], + %{aliases_file: aliases_file_name, herring: "atlantic"}, + []} + + assert @subject.parse(["flying_gull", "--aliases-file", aliases_file_name]) == + {RabbitMQ.CLI.Seagull.Commands.HerringGullCommand, + "flying_gull", + ["fly"], + %{aliases_file: aliases_file_name}, + []} + + assert @subject.parse(["garbage_gull", "--aliases-file", aliases_file_name]) == + {RabbitMQ.CLI.Seagull.Commands.HerringGullCommand, + "garbage_gull", + [], + %{aliases_file: aliases_file_name, garbage: true}, + []} + + assert @subject.parse(["complex_gull", "--aliases-file", aliases_file_name]) == + {RabbitMQ.CLI.Seagull.Commands.HerringGullCommand, + "complex_gull", + ["eat"], + %{aliases_file: aliases_file_name, garbage: true, herring: "pacific", formatter: "erlang"}, + []} + + assert @subject.parse(["invalid_gull", "--aliases-file", aliases_file_name]) == + {RabbitMQ.CLI.Seagull.Commands.HerringGullCommand, + "invalid_gull", + [], + %{aliases_file: aliases_file_name}, + [{"--invalid", nil}]} + + assert @subject.parse(["unknown_gull", "--aliases-file", aliases_file_name]) == + {:no_command, "unknown_gull", [], %{aliases_file: aliases_file_name}, []} + + File.rm(aliases_file_name) + + + assert capture_io(:stderr, + fn -> + assert @subject.parse(["larus_pacificus", "--aliases-file", aliases_file_name]) == + {:no_command, "larus_pacificus", [], %{aliases_file: aliases_file_name}, []} + end) =~ "Error reading aliases file" + + end + +end diff --git a/deps/rabbitmq_cli/test/core/rpc_stream_test.exs b/deps/rabbitmq_cli/test/core/rpc_stream_test.exs new file mode 100644 index 0000000000..cadd303f23 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/rpc_stream_test.exs @@ -0,0 +1,94 @@ +defmodule RpcStreamTest do + use ExUnit.Case, async: false + + require RabbitMQ.CLI.Ctl.RpcStream + alias RabbitMQ.CLI.Ctl.RpcStream + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + + :ok + + end + + test "emit empty list" do + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list, [[]], :infinity, []]) + + assert [] == items + end + + test "emit list without filters" do + list = [:one, :two, :three] + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list, [list], :infinity, []]) + + assert list == items + end + + + test "emit list with filters" do + list = [[one: 1, two: 2, three: 3], [one: 11, two: 12, three: 13]] + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list, [list], :infinity, [:one, :two]]) + + assert [[one: 1, two: 2], [one: 11, two: 12]] == items + end + + test "emit list of lists with filters" do + list = [[[one: 1, two: 2, three: 3], [one: 11, two: 12, three: 13]], + [[one: 21, two: 22, three: 23], [one: 31, two: 32, three: 33]]] + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list, [list], :infinity, [:one, :two]]) + + assert [[[one: 1, two: 2], [one: 11, two: 12]], [[one: 21, two: 22], [one: 31, two: 32]]] == items + end + + test "emission timeout 0 return badrpc" do + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list, [[]], 0, []]) + + assert [{:badrpc, {:timeout, 0.0}}] == items + end + + test "emission timeout return badrpc with timeout value in seconds" do + timeout_fun = fn(x) -> :timer.sleep(1000); x end + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list_map, [[1,2,3], timeout_fun], 100, []]) + assert [{:badrpc, {:timeout, 0.1}}] == items + end + + test "emission timeout in progress return badrpc with timeout value in seconds as last element" do + timeout_fun = fn(x) -> :timer.sleep(100); x end + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list_map, [[1,2,3], timeout_fun], 150, []]) + assert [1, {:badrpc, {:timeout, 0.15}}] == items + end + + test "parallel emission do not mix values" do + {:ok, agent} = Agent.start_link(fn() -> :init end) + list1 = [:one, :two, :three] + list2 = [:dog, :cat, :pig] + # Adding timeout to make sure emissions are executed in parallel + timeout_fun = fn(x) -> :timer.sleep(10); x end + Agent.update(agent, + fn(:init) -> + receive_list_items_to_list([Kernel.node, TestHelper, :emit_list_map, [list2, timeout_fun], :infinity, []]) + end) + items1 = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list_map, [list1, timeout_fun], :infinity, []]) + items2 = Agent.get(agent, fn(x) -> x end) + + assert items1 == list1 + assert items2 == list2 + end + + test "can receive from multiple emission sources in parallel" do + list1 = [:one, :two, :three] + list2 = [:dog, :cat, :pig] + items = receive_list_items_to_list([Kernel.node, TestHelper, :emit_list_multiple_sources, [list1, list2], :infinity, []], 2) + assert Kernel.length(list1 ++ list2) == Kernel.length(items) + assert MapSet.new(list1 ++ list2) == MapSet.new(items) + end + + def receive_list_items_to_list(args, chunks \\ 1) do + res = Kernel.apply(RpcStream, :receive_list_items, args ++ [chunks]) + case Enumerable.impl_for(res) do + nil -> res; + _ -> Enum.to_list(res) + end + end +end diff --git a/deps/rabbitmq_cli/test/core/table_formatter_test.exs b/deps/rabbitmq_cli/test/core/table_formatter_test.exs new file mode 100644 index 0000000000..60bf2060f1 --- /dev/null +++ b/deps/rabbitmq_cli/test/core/table_formatter_test.exs @@ -0,0 +1,46 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule TableFormatterTest do + use ExUnit.Case, async: false + + @formatter RabbitMQ.CLI.Formatters.Table + + test "format_output tab-separates map values" do + assert @formatter.format_output(%{a: :apple, b: :beer}, %{}) == ["a\tb", "apple\tbeer"] + assert @formatter.format_output(%{a: :apple, b: :beer, c: 1}, %{}) == ["a\tb\tc", "apple\tbeer\t1"] + assert @formatter.format_output(%{a: "apple", b: 'beer', c: 1}, %{}) == ["a\tb\tc", "apple\t\"beer\"\t1"] + end + + test "format_output tab-separates keyword values" do + assert @formatter.format_output([a: :apple, b: :beer], %{}) == ["a\tb", "apple\tbeer"] + assert @formatter.format_output([a: :apple, b: :beer, c: 1], %{}) == ["a\tb\tc", "apple\tbeer\t1"] + assert @formatter.format_output([a: "apple", b: 'beer', c: 1], %{}) == ["a\tb\tc", "apple\t\"beer\"\t1"] + end + + test "format_stream tab-separates map values" do + assert @formatter.format_stream([%{a: :apple, b: :beer, c: 1}, + %{a: "aadvark", b: 'bee', c: 2}], %{}) + |> Enum.to_list == + ["a\tb\tc", "apple\tbeer\t1", "aadvark\t\"bee\"\t2"] + end + + test "format_stream tab-separates keyword values" do + assert @formatter.format_stream([[a: :apple, b: :beer, c: 1], + [a: "aadvark", b: 'bee', c: 2]], %{}) + |> Enum.to_list == + ["a\tb\tc", "apple\tbeer\t1", "aadvark\t\"bee\"\t2"] + end + + test "format_output formats non-string values with inspect recursively" do + assert @formatter.format_output(%{a: :apple, b: "beer", c: {:carp, "fish"}, d: [door: :way], e: %{elk: "horn", for: :you}}, %{}) == + ["a\tb\tc\td\te", "apple\tbeer\t{carp, fish}\t[{door, way}]\t\#{elk => horn, for => you}"] + + assert @formatter.format_output(%{a: :apple, b: "beer", c: {:carp, {:small, :fish}}, d: [door: {:way, "big"}], e: %{elk: [horn: :big]}}, %{}) == + ["a\tb\tc\td\te", "apple\tbeer\t{carp, {small, fish}}\t[{door, {way, big}}]\t\#{elk => [{horn, big}]}"] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/add_user_command_test.exs b/deps/rabbitmq_cli/test/ctl/add_user_command_test.exs new file mode 100644 index 0000000000..ec21691da9 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/add_user_command_test.exs @@ -0,0 +1,86 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AddUserCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.AddUserCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + on_exit(context, fn -> delete_user(context[:user]) end) + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: no positional arguments fails" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: too many positional arguments fails" do + assert @command.validate(["user", "password", "extra"], %{}) == + {:validation_failure, :too_many_args} + end + + test "validate: two arguments passes" do + assert @command.validate(["user", "password"], %{}) == :ok + end + + test "validate: one argument passes" do + assert @command.validate(["user"], %{}) == :ok + end + + @tag user: "", password: "password" + test "validate: an empty username fails", context do + assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([context[:user], context[:password]], context[:opts])) + end + + # Blank passwords are currently allowed, they make sense + # e.g. when a user only authenticates using X.509 certificates. + # Credential validators can be used to require passwords of a certain length + # or following a certain pattern. This is a core server responsibility. MK. + @tag user: "some_rando", password: "" + test "validate: an empty password is allowed", context do + assert @command.validate([context[:user], context[:password]], context[:opts]) == :ok + end + + @tag user: "someone", password: "password" + test "run: request to a non-existent node returns a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([context[:user], context[:password]], opts)) + end + + @tag user: "someone", password: "password" + test "run: default case completes successfully", context do + assert @command.run([context[:user], context[:password]], context[:opts]) == :ok + assert list_users() |> Enum.count(fn(record) -> record[:user] == context[:user] end) == 1 + end + + @tag user: "someone", password: "password" + test "run: adding an existing user returns an error", context do + add_user(context[:user], context[:password]) + assert @command.run([context[:user], context[:password]], context[:opts]) == {:error, {:user_already_exists, context[:user]}} + assert list_users() |> Enum.count(fn(record) -> record[:user] == context[:user] end) == 1 + end + + @tag user: "someone", password: "password" + test "banner", context do + assert @command.banner([context[:user], context[:password]], context[:opts]) + =~ ~r/Adding user \"#{context[:user]}\" \.\.\./ + end + + @tag user: "someone" + test "output: formats a user_already_exists error", context do + {:error, 70, "User \"someone\" already exists"} = + @command.output({:error, {:user_already_exists, context[:user]}}, %{}) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/add_vhost_command_test.exs b/deps/rabbitmq_cli/test/ctl/add_vhost_command_test.exs new file mode 100644 index 0000000000..f9f6362c19 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/add_vhost_command_test.exs @@ -0,0 +1,68 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AddVhostCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.AddVhostCommand + @vhost "test" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + setup context do + on_exit(context, fn -> delete_vhost(context[:vhost]) end) + :ok + end + + test "validate: no arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: too many arguments fails validation" do + assert @command.validate(["test", "extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: one argument passes validation" do + assert @command.validate(["new-vhost"], %{}) == :ok + assert @command.validate(["new-vhost"], %{description: "Used by team A"}) == :ok + assert @command.validate(["new-vhost"], %{description: "Used by team A for QA purposes", tags: "qa,team-a"}) == :ok + end + + @tag vhost: @vhost + test "run: passing a valid vhost name to a running RabbitMQ node succeeds", context do + assert @command.run([context[:vhost]], context[:opts]) == :ok + assert list_vhosts() |> Enum.count(fn(record) -> record[:name] == context[:vhost] end) == 1 + end + + @tag vhost: "" + test "run: passing an empty string for vhost name with a running RabbitMQ node still succeeds", context do + assert @command.run([context[:vhost]], context[:opts]) == :ok + assert list_vhosts() |> Enum.count(fn(record) -> record[:name] == context[:vhost] end) == 1 + end + + test "run: attempt to use an unreachable node returns a nodedown" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["na"], opts)) + end + + test "run: adding the same host twice is idempotent", context do + add_vhost context[:vhost] + + assert @command.run([context[:vhost]], context[:opts]) == :ok + assert list_vhosts() |> Enum.count(fn(record) -> record[:name] == context[:vhost] end) == 1 + end + + @tag vhost: @vhost + test "banner", context do + assert @command.banner([context[:vhost]], context[:opts]) + =~ ~r/Adding vhost \"#{context[:vhost]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/authenticate_user_command_test.exs b/deps/rabbitmq_cli/test/ctl/authenticate_user_command_test.exs new file mode 100644 index 0000000000..506dfad367 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/authenticate_user_command_test.exs @@ -0,0 +1,81 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AuthenticateUserCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands. AuthenticateUserCommand + @user "user1" + @password "password" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_user(@user, @password) + on_exit(context, fn -> delete_user(@user) end) + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: no positional arguments fails" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: too many positional arguments fails" do + assert @command.validate(["user", "password", "extra"], %{}) == + {:validation_failure, :too_many_args} + end + + test "validate: one argument passes" do + assert @command.validate(["user"], %{}) == :ok + end + + test "validate: two arguments passes" do + assert @command.validate(["user", "password"], %{}) == :ok + end + + @tag user: @user, password: @password + test "run: a valid username and password returns okay", context do + assert {:ok, _} = @command.run([context[:user], context[:password]], context[:opts]) + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["user", "password"], opts)) + end + + @tag user: @user, password: "treachery" + test "run: a valid username and invalid password returns refused", context do + assert {:refused, _, _, _} = @command.run([context[:user], context[:password]], context[:opts]) + end + + @tag user: "interloper", password: @password + test "run: an invalid username returns refused", context do + assert {:refused, _, _, _} = @command.run([context[:user], context[:password]], context[:opts]) + end + + @tag user: @user, password: @password + test "banner", context do + assert @command.banner([context[:user], context[:password]], context[:opts]) + =~ ~r/Authenticating user/ + assert @command.banner([context[:user], context[:password]], context[:opts]) + =~ ~r/"#{context[:user]}"/ + end + + test "output: refused error", context do + user = "example_user" + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_dataerr + assert match?({:error, ^exit_code, + "Error: failed to authenticate user \"example_user\"\n" <> + "Unable to foo"}, + @command.output({:refused, user, "Unable to ~s", ["foo"]}, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/autocomplete_command_test.exs b/deps/rabbitmq_cli/test/ctl/autocomplete_command_test.exs new file mode 100644 index 0000000000..52b3c8d026 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/autocomplete_command_test.exs @@ -0,0 +1,52 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AutocompleteCommandTest do + use ExUnit.Case, async: true + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.AutocompleteCommand + setup do + {:ok, opts: %{ + script_name: "rabbitmqctl", + node: get_rabbit_hostname() + }} + end + + test "shows up in help" do + s = @command.usage() + assert s =~ ~r/autocomplete/ + end + + test "enforces --silent" do + assert @command.merge_defaults(["list_"], %{}) == {["list_"], %{silent: true}} + end + + test "validate: providing no arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing two or more arguments fails validation" do + assert @command.validate(["list_", "extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: providing a single argument passes validation" do + assert @command.validate(["list_c"], %{}) == :ok + end + + test "run: lists completion options", context do + {:stream, completion_options} = @command.run(["list_c"], context[:opts]) + + assert Enum.member?(completion_options, "list_channels") + assert Enum.member?(completion_options, "list_connections") + assert Enum.member?(completion_options, "list_consumers") + end + + test "banner shows that the name is being set", context do + assert @command.banner(["list_"], context[:opts]) == nil + end +end diff --git a/deps/rabbitmq_cli/test/ctl/await_online_nodes_command_test.exs b/deps/rabbitmq_cli/test/ctl/await_online_nodes_command_test.exs new file mode 100644 index 0000000000..bf9eeb574d --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/await_online_nodes_command_test.exs @@ -0,0 +1,44 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AwaitOnlineNodesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.AwaitOnlineNodesCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 300_000}} + end + + setup context do + on_exit(context, fn -> delete_vhost(context[:vhost]) end) + :ok + end + + test "validate: wrong number of arguments results in arg count errors" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["1", "1"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: a call with node count of 1 with a running RabbitMQ node succeeds", context do + assert @command.run(["1"], context[:opts]) == :ok + end + + test "run: a call to an unreachable RabbitMQ node returns a nodedown" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["1"], opts)) + end + + test "banner", context do + assert @command.banner(["1"], context[:opts]) + =~ ~r/Will wait for at least 1 nodes to join the cluster of #{context[:opts][:node]}. Timeout: 300 seconds./ + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/await_startup_command_test.exs b/deps/rabbitmq_cli/test/ctl/await_startup_command_test.exs new file mode 100644 index 0000000000..554ec5ee77 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/await_startup_command_test.exs @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +# Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule AwaitStartupCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.AwaitStartupCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 300_000}} + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "merge_defaults: default timeout is 5 minutes" do + assert @command.merge_defaults([], %{}) == {[], %{timeout: 300_000}} + end + + test "validate: accepts no arguments", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "run: request to a fully booted node succeeds", context do + # this timeout value is in seconds + assert @command.run([], Map.merge(context[:opts], %{timeout: 5})) == :ok + end + + test "empty banner", context do + nil = @command.banner([], context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/cancel_sync_command_test.exs b/deps/rabbitmq_cli/test/ctl/cancel_sync_command_test.exs new file mode 100644 index 0000000000..8503e6ab5f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/cancel_sync_command_test.exs @@ -0,0 +1,64 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CancelSyncQueueCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.CancelSyncQueueCommand + + @vhost "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname(), + vhost: @vhost + }} + end + + test "validate: specifying no queue name is reported as an error", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate: specifying two queue names is reported as an error", context do + assert @command.validate(["q1", "q2"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: specifying three queue names is reported as an error", context do + assert @command.validate(["q1", "q2", "q3"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: specifying one queue name succeeds", context do + assert @command.validate(["q1"], context[:opts]) == :ok + end + + test "run: request to a non-existent RabbitMQ node returns a nodedown" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + assert match?({:badrpc, _}, @command.run(["q1"], opts)) + end + + test "banner", context do + s = @command.banner(["q1"], context[:opts]) + + assert s =~ ~r/Stopping synchronising queue/ + assert s =~ ~r/q1/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/change_cluster_node_type_command_test.exs b/deps/rabbitmq_cli/test/ctl/change_cluster_node_type_command_test.exs new file mode 100644 index 0000000000..8fcb7de3ae --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/change_cluster_node_type_command_test.exs @@ -0,0 +1,84 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ChangeClusterNodeTypeCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ChangeClusterNodeTypeCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname() + }} + end + + test "validate: node type of disc, disk, and ram pass validation", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["foo"], context[:opts])) + + assert :ok == @command.validate(["ram"], context[:opts]) + assert :ok == @command.validate(["disc"], context[:opts]) + assert :ok == @command.validate(["disk"], context[:opts]) + end + + test "validate: providing no arguments fails validation", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + test "validate: providing too many arguments fails validation", context do + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + # TODO + #test "run: change ram node to disc node", context do + #end + + # TODO + #test "run: change disk node to ram node", context do + #end + + test "run: request to a node with running RabbitMQ app fails", context do + assert match?( + {:error, :mnesia_unexpectedly_running}, + @command.run(["ram"], context[:opts])) + end + + test "run: request to an unreachable node returns a badrpc", _context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?( + {:badrpc, :nodedown}, + @command.run(["ram"], opts)) + end + + test "banner", context do + assert @command.banner(["ram"], context[:opts]) =~ + ~r/Turning #{get_rabbit_hostname()} into a ram node/ + end + + test "output mnesia is running error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, + "Mnesia is still running on node " <> _}, + @command.output({:error, :mnesia_unexpectedly_running}, context[:opts])) + + end +end diff --git a/deps/rabbitmq_cli/test/ctl/change_password_command_test.exs b/deps/rabbitmq_cli/test/ctl/change_password_command_test.exs new file mode 100644 index 0000000000..3a415085dd --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/change_password_command_test.exs @@ -0,0 +1,80 @@ +## at https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ChangePasswordCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands. ChangePasswordCommand + @user "user1" + @password "password" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_user(@user, @password) + on_exit(context, fn -> delete_user(@user) end) + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: no positional arguments fails" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: too many positional arguments fails" do + assert @command.validate(["user", "password", "extra"], %{}) == + {:validation_failure, :too_many_args} + end + + test "validate: two arguments passes" do + assert @command.validate(["user", "password"], %{}) == :ok + end + + test "validate: one argument passes" do + assert @command.validate(["user"], %{}) == :ok + end + + @tag user: @user, password: "new_password" + test "run: a valid username and new password return ok", context do + assert @command.run([context[:user], context[:password]], context[:opts]) == :ok + assert {:ok, _} = authenticate_user(context[:user], context[:password]) + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["user", "password"], opts)) + end + + @tag user: @user, password: @password + test "run: changing password to the same thing is ok", context do + assert @command.run([context[:user], context[:password]], context[:opts]) == :ok + assert {:ok, _} = authenticate_user(context[:user], context[:password]) + end + + @tag user: "interloper", password: "new_password" + test "run: an invalid user returns an error", context do + assert @command.run([context[:user], context[:password]], context[:opts]) == {:error, {:no_such_user, "interloper"}} + end + + @tag user: @user, password: @password + test "banner", context do + assert @command.banner([context[:user], context[:password]], context[:opts]) + =~ ~r/Changing password for user/ + assert @command.banner([context[:user], context[:password]], context[:opts]) + =~ ~r/"#{context[:user]}"/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_global_parameter_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_global_parameter_command_test.exs new file mode 100644 index 0000000000..adadc3c223 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_global_parameter_command_test.exs @@ -0,0 +1,86 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearGlobalParameterCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClearGlobalParameterCommand + @key :mqtt_default_vhosts + @value "{\"O=client,CN=dummy\":\"somevhost\"}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + on_exit(context, fn -> + clear_global_parameter context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + } + } + end + + test "validate: expects a single argument" do + assert @command.validate(["one"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this is", "too many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag key: @key + test "run: when global parameter does not exist, returns an error", context do + assert @command.run( + [context[:key]], + context[:opts] + ) == {:error_string, 'Parameter does not exist'} + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([@key], opts)) + end + + @tag key: @key + test "run: clears the parameter", context do + set_global_parameter(context[:key], @value) + + assert @command.run( + [context[:key]], + context[:opts] + ) == :ok + + assert_parameter_empty(context) + end + + @tag key: @key, value: @value + test "banner", context do + set_global_parameter(context[:key], @value) + + s = @command.banner( + [context[:key]], + context[:opts] + ) + + assert s =~ ~r/Clearing global runtime parameter/ + assert s =~ ~r/"#{context[:key]}"/ + end + + defp assert_parameter_empty(context) do + parameter = list_global_parameters() + |> Enum.filter(fn(param) -> + param[:key] == context[:key] + end) + assert parameter === [] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_operator_policy_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_operator_policy_command_test.exs new file mode 100644 index 0000000000..834caf89f7 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_operator_policy_command_test.exs @@ -0,0 +1,127 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearOperatorPolicyCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClearOperatorPolicyCommand + @vhost "test1" + @key "message-expiry" + @pattern "^queue\." + @value "{\"message-ttl\":10}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + on_exit(context, fn -> + clear_operator_policy(context[:vhost], context[:key]) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + } + } + end + + test "merge_defaults: adds default vhost if missing" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + end + + test "merge_defaults: does not change provided vhost" do + assert @command.merge_defaults([], %{vhost: "test_vhost"}) == {[], %{vhost: "test_vhost"}} + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: providing one argument and no options passes validation" do + assert @command.validate(["a-policy"], %{}) == :ok + end + + @tag pattern: @pattern, key: @key, vhost: @vhost + test "run: if policy does not exist, returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + assert match?({:badrpc, _}, @command.run([@key], opts)) + end + + + @tag pattern: @pattern, key: @key, vhost: @vhost + test "run: if policy exists, returns ok and removes it", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + set_operator_policy(context[:vhost], context[:key], context[:pattern], @value) + + assert @command.run( + [context[:key]], + vhost_opts + ) == :ok + + assert_operator_policy_does_not_exist(context) + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: "bad-vhost" + test "run: a non-existent vhost returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + set_operator_policy(context[:vhost], context[:key], context[:pattern], @value) + + s = @command.banner( + [context[:key]], + vhost_opts + ) + + assert s =~ ~r/Clearing operator policy/ + assert s =~ ~r/"#{context[:key]}"/ + end + + defp assert_operator_policy_does_not_exist(context) do + policy = context[:vhost] + |> list_operator_policies + |> Enum.filter(fn(param) -> + param[:pattern] == context[:pattern] and + param[:key] == context[:key] + end) + assert policy === [] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_parameter_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_parameter_command_test.exs new file mode 100644 index 0000000000..4f08234cb6 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_parameter_command_test.exs @@ -0,0 +1,138 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearParameterCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClearParameterCommand + @vhost "test1" + @root "/" + @component_name "federation-upstream" + @key "reconnect-delay" + @value "{\"uri\":\"amqp://127.0.0.1:5672\"}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + enable_federation_plugin() + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + on_exit(context, fn -> + clear_parameter context[:vhost], context[:component_name], context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + } + } + end + + test "merge_defaults: adds default vhost if missing" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: argument validation" do + assert @command.validate(["one", "two"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this", "is", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag component_name: @component_name, key: @key, vhost: @vhost + test "run: returns error, if parameter does not exist", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:component_name], context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + assert match?({:badrpc, _}, @command.run([@component_name, @key], opts)) + end + + + @tag component_name: @component_name, key: @key, vhost: @vhost + test "run: returns ok and clears parameter, if it exists", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + set_parameter(context[:vhost], context[:component_name], context[:key], @value) + + assert @command.run( + [context[:component_name], context[:key]], + vhost_opts + ) == :ok + + assert_parameter_empty(context) + end + + @tag component_name: "bad-component-name", key: @key, value: @value, vhost: @root + test "run: an invalid component_name returns a 'parameter does not exist' error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + assert @command.run( + [context[:component_name], context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + + assert list_parameters(context[:vhost]) == [] + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: "bad-vhost" + test "run: an invalid vhost returns a 'parameter does not exist' error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:component_name], context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + set_parameter(context[:vhost], context[:component_name], context[:key], @value) + + s = @command.banner( + [context[:component_name], context[:key]], + vhost_opts + ) + + assert s =~ ~r/Clearing runtime parameter/ + assert s =~ ~r/"#{context[:key]}"/ + assert s =~ ~r/"#{context[:component_name]}"/ + assert s =~ ~r/"#{context[:vhost]}"/ + end + + defp assert_parameter_empty(context) do + parameter = context[:vhost] + |> list_parameters + |> Enum.filter(fn(param) -> + param[:component_name] == context[:component_name] and + param[:key] == context[:key] + end) + assert parameter === [] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_password_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_password_command_test.exs new file mode 100644 index 0000000000..0843ca3970 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_password_command_test.exs @@ -0,0 +1,64 @@ +## at https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearPasswordCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands. ClearPasswordCommand + @user "user1" + @password "password" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_user(@user, @password) + on_exit(context, fn -> delete_user(@user) end) + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: argument count is correct" do + assert @command.validate(["username"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["username", "extra"], %{}) == + {:validation_failure, :too_many_args} + end + + @tag user: @user, password: @password + test "run: a valid username clears the password and returns okay", context do + assert @command.run([context[:user]], context[:opts]) == :ok + assert {:refused, _, _, _} = authenticate_user(context[:user], context[:password]) + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["user"], opts)) + end + + @tag user: "interloper" + test "run: An invalid username returns a no-such-user error message", context do + assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_user, "interloper"}} + end + + @tag user: @user + test "banner", context do + s = @command.banner([context[:user]], context[:opts]) + + assert s =~ ~r/Clearing password/ + assert s =~ ~r/"#{context[:user]}"/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_permissions_command_test.exs new file mode 100644 index 0000000000..89bfe8c457 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_permissions_command_test.exs @@ -0,0 +1,100 @@ +## at https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearPermissionsTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands. ClearPermissionsCommand + @user "user1" + @password "password" + @default_vhost "/" + @specific_vhost "vhost1" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_user(@user, @password) + add_vhost(@specific_vhost) + + on_exit([], fn -> + delete_user(@user) + delete_vhost(@specific_vhost) + end) + + :ok + end + + setup context do + set_permissions(@user, @default_vhost, ["^#{@user}-.*", ".*", ".*"]) + set_permissions(@user, @specific_vhost, ["^#{@user}-.*", ".*", ".*"]) + + { + :ok, + opts: %{node: get_rabbit_hostname(), vhost: context[:vhost]} + } + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: argument count validates" do + assert @command.validate(["one"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag user: "fake_user" + test "run: can't clear permissions for non-existing user", context do + assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_user, context[:user]}} + end + + @tag user: @user, vhost: @default_vhost + test "run: a valid username clears permissions", context do + assert @command.run([context[:user]], context[:opts]) == :ok + + assert list_permissions(@default_vhost) + |> Enum.filter(fn(record) -> record[:user] == context[:user] end) == [] + end + + test "run: on an invalid node, return a badrpc message" do + arg = ["some_name"] + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run(arg, opts)) + end + + @tag user: @user, vhost: @specific_vhost + test "run: on a valid specified vhost, clear permissions", context do + assert @command.run([context[:user]], context[:opts]) == :ok + + assert list_permissions(context[:vhost]) + |> Enum.filter(fn(record) -> record[:user] == context[:user] end) == [] + end + + @tag user: @user, vhost: "bad_vhost" + test "run: on an invalid vhost, return no_such_vhost error", context do + assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag user: @user, vhost: @specific_vhost + test "banner", context do + s = @command.banner([context[:user]], context[:opts]) + + assert s =~ ~r/Clearing permissions/ + assert s =~ ~r/\"#{context[:user]}\"/ + assert s =~ ~r/\"#{context[:vhost]}\"/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_policy_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_policy_command_test.exs new file mode 100644 index 0000000000..f36f65d25f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_policy_command_test.exs @@ -0,0 +1,129 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearPolicyCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClearPolicyCommand + @vhost "test1" + @key "federate" + @pattern "^fed\." + @value "{\"federation-upstream-set\":\"all\"}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + enable_federation_plugin() + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + on_exit(context, fn -> + clear_policy context[:vhost], context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + } + } + end + + test "merge_defaults: adds default vhost if missing" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + end + + test "merge_defaults: does not change defined vhost" do + assert @command.merge_defaults([], %{vhost: "test_vhost"}) == {[], %{vhost: "test_vhost"}} + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: providing one argument and no options passes validation" do + assert @command.validate(["a-policy"], %{}) == :ok + end + + @tag pattern: @pattern, key: @key, vhost: @vhost + test "run: if policy does not exist, returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + assert match?({:badrpc, _}, @command.run([@key], opts)) + end + + + @tag pattern: @pattern, key: @key, vhost: @vhost + test "run: if policy exists, returns ok and removes it", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + set_policy(context[:vhost], context[:key], context[:pattern], @value) + + assert @command.run( + [context[:key]], + vhost_opts + ) == :ok + + assert_policy_does_not_exist(context) + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: "bad-vhost" + test "run: a non-existent vhost returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key]], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + set_policy(context[:vhost], context[:key], context[:pattern], @value) + + s = @command.banner( + [context[:key]], + vhost_opts + ) + + assert s =~ ~r/Clearing policy/ + assert s =~ ~r/"#{context[:key]}"/ + end + + defp assert_policy_does_not_exist(context) do + policy = context[:vhost] + |> list_policies + |> Enum.filter(fn(param) -> + param[:pattern] == context[:pattern] and + param[:key] == context[:key] + end) + assert policy === [] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_topic_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_topic_permissions_command_test.exs new file mode 100644 index 0000000000..2b5fb6e12a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_topic_permissions_command_test.exs @@ -0,0 +1,107 @@ +## at https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearTopicPermissionsTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands. ClearTopicPermissionsCommand + @user "user1" + @password "password" + @specific_vhost "vhost1" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_user(@user, @password) + add_vhost(@specific_vhost) + + on_exit([], fn -> + clear_topic_permissions(@user, @specific_vhost) + delete_user(@user) + delete_vhost(@specific_vhost) + end) + + :ok + end + + setup context do + set_topic_permissions(@user, @specific_vhost, "amq.topic", "^a", "^b") + set_topic_permissions(@user, @specific_vhost, "topic1", "^a", "^b") + { + :ok, + opts: %{node: get_rabbit_hostname(), vhost: context[:vhost]} + } + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: expects username and optional exchange" do + assert @command.validate(["username"], %{}) == :ok + assert @command.validate(["username", "exchange"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag user: "fake_user" + test "run: can't clear topic permissions for non-existing user", context do + assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_user, context[:user]}} + end + + @tag user: @user, vhost: "bad_vhost" + test "run: on an invalid vhost, return no_such_vhost error", context do + assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag user: @user, vhost: @specific_vhost + test "run: with a valid username clears all permissions for vhost", context do + assert Enum.count(list_user_topic_permissions(@user)) == 2 + assert @command.run([context[:user]], context[:opts]) == :ok + + assert Enum.count(list_user_topic_permissions(@user)) == 0 + end + + @tag user: @user, vhost: @specific_vhost + test "run: with a valid username and exchange clears only exchange permissions", context do + assert Enum.count(list_user_topic_permissions(@user)) == 2 + assert @command.run([context[:user], "amq.topic"], context[:opts]) == :ok + + assert Enum.count(list_user_topic_permissions(@user)) == 1 + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + arg = ["some_name"] + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run(arg, opts)) + end + + @tag user: @user, vhost: @specific_vhost + test "banner with username only", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:user]], vhost_opts) + =~ ~r/Clearing topic permissions for user \"#{context[:user]}\" in vhost \"#{context[:vhost]}\" \.\.\./ + end + + @tag user: @user, vhost: @specific_vhost + test "banner with username and exchange name", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:user], "amq.topic"], vhost_opts) + =~ ~r/Clearing topic permissions on \"amq.topic\" for user \"#{context[:user]}\" in vhost \"#{context[:vhost]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_user_limits_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_user_limits_command_test.exs new file mode 100644 index 0000000000..eb05a875bc --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_user_limits_command_test.exs @@ -0,0 +1,115 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ClearUserLimitsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClearUserLimitsCommand + + @user "someone" + @password "password" + @limittype "max-channels" + @channel_definition "{\"max-channels\":100}" + @definition "{\"max-channels\":500, \"max-connections\":100}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_user @user, @password + + on_exit([], fn -> + delete_user @user + end) + + :ok + end + + setup context do + on_exit(context, fn -> + clear_user_limits(context[:user]) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + } + } + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not-enough"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([@user, @limittype], opts)) + end + + test "run: if limit exists, returns ok and clears it", context do + :ok = set_user_limits(@user, @channel_definition) + + assert get_user_limits(@user) != [] + + assert @command.run( + [@user, @limittype], + context[:opts] + ) == :ok + + assert get_user_limits(@user) == %{} + end + + test "run: if limit exists, returns ok and clears all limits for the given user", context do + :ok = set_user_limits(@user, @definition) + + assert get_user_limits(@user) != [] + + assert @command.run( + [@user, "all"], + context[:opts] + ) == :ok + + assert get_user_limits(@user) == %{} + end + + @tag user: "bad-user" + test "run: a non-existent user returns an error", context do + + assert @command.run( + [context[:user], @limittype], + context[:opts] + ) == {:error, {:no_such_user, "bad-user"}} + end + + test "banner: for a limit type", context do + + s = @command.banner( + [@user, @limittype], + context[:opts] + ) + + assert s == "Clearing \"#{@limittype}\" limit for user \"#{@user}\" ..." + end + + test "banner: for all", context do + + s = @command.banner( + [@user, "all"], + context[:opts] + ) + + assert s == "Clearing all limits for user \"#{@user}\" ..." + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/clear_vhost_limits_command_test.exs b/deps/rabbitmq_cli/test/ctl/clear_vhost_limits_command_test.exs new file mode 100644 index 0000000000..4dd681c901 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/clear_vhost_limits_command_test.exs @@ -0,0 +1,103 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClearVhostLimitsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClearVhostLimitsCommand + @vhost "test1" + @definition "{\"max-connections\":100}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + on_exit(context, fn -> + clear_vhost_limits(context[:vhost]) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + } + } + end + + test "merge_defaults: adds default vhost if missing" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + end + + test "merge_defaults: does not change defined vhost" do + assert @command.merge_defaults([], %{vhost: "test_vhost"}) == {[], %{vhost: "test_vhost"}} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: providing zero arguments and no options passes validation" do + assert @command.validate([], %{}) == :ok + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + + @tag vhost: @vhost + test "run: if limits exist, returns ok and clears them", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + :ok = set_vhost_limits(context[:vhost], @definition) + + assert get_vhost_limits(context[:vhost]) != [] + + assert @command.run( + [], + vhost_opts + ) == :ok + + assert get_vhost_limits(context[:vhost]) == %{} + end + + @tag vhost: "bad-vhost" + test "run: a non-existent vhost returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [], + vhost_opts + ) == {:error_string, 'Parameter does not exist'} + end + + @tag vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + s = @command.banner( + [], + vhost_opts + ) + + assert s =~ ~r/Clearing vhost \"#{context[:vhost]}\" limits .../ + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/close_all_connections_command_test.exs b/deps/rabbitmq_cli/test/ctl/close_all_connections_command_test.exs new file mode 100644 index 0000000000..f08969f319 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/close_all_connections_command_test.exs @@ -0,0 +1,147 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule CloseAllConnectionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + alias RabbitMQ.CLI.Ctl.RpcStream + @helpers RabbitMQ.CLI.Core.Helpers + @command RabbitMQ.CLI.Ctl.Commands.CloseAllConnectionsCommand + + @vhost "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + close_all_connections(get_rabbit_hostname()) + + on_exit([], fn -> + close_all_connections(get_rabbit_hostname()) + end) + + :ok + end + + setup context do + node_name = get_rabbit_hostname() + close_all_connections(node_name) + await_no_client_connections(node_name, 5_000) + + {:ok, context} + end + + test "validate: with an invalid number of arguments returns an arg count error", context do + assert @command.validate(["random", "explanation"], context[:opts]) == {:validation_failure, :too_many_args} + assert @command.validate([], context[:opts]) == {:validation_failure, :not_enough_args} + end + + test "validate: with the correct number of arguments returns ok", context do + assert @command.validate(["explanation"], context[:opts]) == :ok + end + + test "run: a close connections request in an existing vhost with all defaults closes all connections", context do + with_connection(@vhost, fn(_) -> + node = @helpers.normalise_node(context[:node], :shortnames) + nodes = @helpers.nodes_in_cluster(node) + [[vhost: @vhost]] = fetch_connection_vhosts(node, nodes) + opts = %{node: node, vhost: @vhost, global: false, per_connection_delay: 0, limit: 0} + assert {:ok, "Closed 1 connections"} == @command.run(["test"], opts) + + await_no_client_connections(node, 5_000) + assert fetch_connection_vhosts(node, nodes) == [] + end) + end + + test "run: close a limited number of connections in an existing vhost closes a subset of connections", context do + with_connections([@vhost, @vhost, @vhost], fn(_) -> + node = @helpers.normalise_node(context[:node], :shortnames) + nodes = @helpers.nodes_in_cluster(node) + [[vhost: @vhost], [vhost: @vhost], [vhost: @vhost]] = fetch_connection_vhosts(node, nodes) + opts = %{node: node, vhost: @vhost, global: false, per_connection_delay: 0, limit: 2} + assert {:ok, "Closed 2 connections"} == @command.run(["test"], opts) + Process.sleep(100) + assert fetch_connection_vhosts(node, nodes) == [[vhost: @vhost]] + end) + end + + test "run: a close connections request for a non-existing vhost does nothing", context do + with_connection(@vhost, fn(_) -> + node = @helpers.normalise_node(context[:node], :shortnames) + nodes = @helpers.nodes_in_cluster(node) + [[vhost: @vhost]] = fetch_connection_vhosts(node, nodes) + opts = %{node: node, vhost: "non_existent-9288737", global: false, per_connection_delay: 0, limit: 0} + assert {:ok, "Closed 0 connections"} == @command.run(["test"], opts) + assert fetch_connection_vhosts(node, nodes) == [[vhost: @vhost]] + end) + end + + test "run: a close connections request to an existing node with --global (all vhosts)", context do + with_connection(@vhost, fn(_) -> + node = @helpers.normalise_node(context[:node], :shortnames) + nodes = @helpers.nodes_in_cluster(node) + [[vhost: @vhost]] = fetch_connection_vhosts(node, nodes) + opts = %{node: node, global: true, per_connection_delay: 0, limit: 0} + assert {:ok, "Closed 1 connections"} == @command.run(["test"], opts) + await_no_client_connections(node, 5_000) + assert fetch_connection_vhosts(node, nodes) == [] + end) + end + + test "run: a close_all_connections request to non-existent RabbitMQ node returns a badrpc" do + opts = %{node: :jake@thedog, vhost: @vhost, global: true, per_connection_delay: 0, limit: 0, timeout: 200} + assert match?({:badrpc, _}, @command.run(["test"], opts)) + end + + test "banner for vhost option", context do + node = @helpers.normalise_node(context[:node], :shortnames) + opts = %{node: node, vhost: "burrow", global: false, per_connection_delay: 0, limit: 0} + s = @command.banner(["some reason"], opts) + assert s =~ ~r/Closing all connections in vhost burrow/ + assert s =~ ~r/some reason/ + end + + test "banner for vhost option with limit", context do + node = @helpers.normalise_node(context[:node], :shortnames) + opts = %{node: node, vhost: "burrow", global: false, per_connection_delay: 0, limit: 2} + s = @command.banner(["some reason"], opts) + assert s =~ ~r/Closing 2 connections in vhost burrow/ + assert s =~ ~r/some reason/ + end + + test "banner for global option" do + opts = %{node: :test@localhost, vhost: "burrow", global: true, per_connection_delay: 0, limit: 0} + s = @command.banner(["some reason"], opts) + assert s =~ ~r/Closing all connections to node test@localhost/ + assert s =~ ~r/some reason/ + end + + defp fetch_connection_vhosts(node, nodes) do + fetch_connection_vhosts(node, nodes, 50) + end + + defp fetch_connection_vhosts(node, nodes, retries) do + stream = RpcStream.receive_list_items(node, + :rabbit_networking, + :emit_connection_info_all, + [nodes, [:vhost]], + :infinity, + [:vhost], + Kernel.length(nodes)) + xs = Enum.to_list(stream) + + case {xs, retries} do + {xs, 0} -> + xs + {[], n} when n >= 0 -> + Process.sleep(10) + fetch_connection_vhosts(node, nodes, retries - 1) + _ -> + xs + end + end +end diff --git a/deps/rabbitmq_cli/test/ctl/close_connection_command_test.exs b/deps/rabbitmq_cli/test/ctl/close_connection_command_test.exs new file mode 100644 index 0000000000..0d1271a67f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/close_connection_command_test.exs @@ -0,0 +1,96 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule CloseConnectionCommandTest do + use ExUnit.Case, async: false + import TestHelper + + alias RabbitMQ.CLI.Ctl.RpcStream + + @helpers RabbitMQ.CLI.Core.Helpers + + @command RabbitMQ.CLI.Ctl.Commands.CloseConnectionCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + close_all_connections(get_rabbit_hostname()) + + on_exit([], fn -> + close_all_connections(get_rabbit_hostname()) + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: :infinity}} + end + + test "validate: with an invalid number of arguments returns an arg count error", context do + assert @command.validate(["pid", "explanation", "extra"], context[:opts]) == {:validation_failure, :too_many_args} + assert @command.validate(["pid"], context[:opts]) == {:validation_failure, :not_enough_args} + end + + test "validate: with the correct number of arguments returns ok", context do + assert @command.validate(["pid", "test"], context[:opts]) == :ok + end + + test "run: a close connection request on an existing connection", context do + with_connection("/", fn(_) -> + Process.sleep(500) + node = @helpers.normalise_node(context[:node], :shortnames) + nodes = @helpers.nodes_in_cluster(node) + [[pid: pid]] = fetch_connection_pids(node, nodes) + assert :ok == @command.run([:rabbit_misc.pid_to_string(pid), "test"], %{node: node}) + Process.sleep(500) + assert fetch_connection_pids(node, nodes) == [] + end) + end + + test "run: a close connection request on for a non existing connection returns successfully", context do + assert match?(:ok, + @command.run(["<#{node()}.2.121.12>", "test"], %{node: @helpers.normalise_node(context[:node], :shortnames)})) + end + + test "run: a close_connection request on nonexistent RabbitMQ node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["<rabbit@localhost.1.2.1>", "test"], opts)) + end + + test "banner", context do + s = @command.banner(["<rabbit@bananas.1.2.3>", "some reason"], context[:opts]) + assert s =~ ~r/Closing connection/ + assert s =~ ~r/<rabbit@bananas.1.2.3>/ + end + + defp fetch_connection_pids(node, nodes) do + fetch_connection_pids(node, nodes, 10) + end + + defp fetch_connection_pids(node, nodes, retries) do + stream = RpcStream.receive_list_items(node, + :rabbit_networking, + :emit_connection_info_all, + [nodes, [:pid]], + :infinity, + [:pid], + Kernel.length(nodes)) + xs = Enum.to_list(stream) + + case {xs, retries} do + {xs, 0} -> + xs + {[], n} when n >= 0 -> + Process.sleep(100) + fetch_connection_pids(node, nodes, retries - 1) + _ -> + xs + end + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/cluster_status_command_test.exs b/deps/rabbitmq_cli/test/ctl/cluster_status_command_test.exs new file mode 100644 index 0000000000..e582355e7a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/cluster_status_command_test.exs @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ClusterStatusCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ClusterStatusCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 12000}} + end + + test "validate: argument count validates", context do + assert @command.validate([], context[:opts]) == :ok + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: status request to a reachable node returns cluster information", context do + n = context[:opts][:node] + res = @command.run([], context[:opts]) + + assert Enum.member?(res[:nodes][:disc], n) + assert res[:partitions] == [] + assert res[:alarms][n] == [] + end + + test "run: status request on nonexistent RabbitMQ node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + s = @command.banner([], context[:opts]) + + assert s =~ ~r/Cluster status of node/ + assert s =~ ~r/#{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/decode_command_test.exs b/deps/rabbitmq_cli/test/ctl/decode_command_test.exs new file mode 100644 index 0000000000..79850d7786 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/decode_command_test.exs @@ -0,0 +1,95 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule DecodeCommandTest do + use ExUnit.Case, async: false + @command RabbitMQ.CLI.Ctl.Commands.DecodeCommand + + setup _context do + {:ok, opts: %{ + cipher: :rabbit_pbe.default_cipher, + hash: :rabbit_pbe.default_hash, + iterations: :rabbit_pbe.default_iterations + }} + end + + test "validate: providing exactly 2 positional arguments passes", context do + assert :ok == @command.validate(["value", "secret"], context[:opts]) + end + + test "validate: providing zero or one positional argument fails", context do + assert match?({:validation_failure, {:not_enough_args, _}}, + @command.validate([], context[:opts])) + assert match?({:validation_failure, {:not_enough_args, _}}, + @command.validate(["value"], context[:opts])) + end + + test "validate: providing three or more positional argument fails", context do + assert match?({:validation_failure, :too_many_args}, + @command.validate(["value", "secret", "incorrect"], context[:opts])) + end + + test "validate: hash and cipher must be supported", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{cipher: :funny_cipher})) + ) + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{hash: :funny_hash})) + ) + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{cipher: :funny_cipher, hash: :funny_hash})) + ) + assert :ok == @command.validate(["value", "secret"], context[:opts]) + end + + test "validate: number of iterations must greater than 0", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{iterations: 0})) + ) + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{iterations: -1})) + ) + assert :ok == @command.validate(["value", "secret"], context[:opts]) + end + + test "run: encrypt/decrypt", context do + # an Erlang list/bitstring + encrypt_decrypt(to_charlist("foobar"), context) + # a binary + encrypt_decrypt("foobar", context) + # a tuple + encrypt_decrypt({:password, "secret"}, context) + end + + defp encrypt_decrypt(secret, context) do + passphrase = "passphrase" + cipher = context[:opts][:cipher] + hash = context[:opts][:hash] + iterations = context[:opts][:iterations] + output = {:encrypted, _encrypted} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, secret) + + {:encrypted, encrypted} = output + # decode plain value + assert {:ok, secret} === @command.run([format_as_erlang_term(encrypted), passphrase], context[:opts]) + # decode {encrypted, ...} tuple form + assert {:ok, secret} === @command.run([format_as_erlang_term(output), passphrase], context[:opts]) + + # wrong passphrase + assert match?({:error, _}, + @command.run([format_as_erlang_term(encrypted), "wrong/passphrase"], context[:opts])) + assert match?({:error, _}, + @command.run([format_as_erlang_term(output), "wrong passphrase"], context[:opts])) + end + + defp format_as_erlang_term(value) do + :io_lib.format("~p", [value]) |> :lists.flatten() |> to_string() + end +end diff --git a/deps/rabbitmq_cli/test/ctl/delete_queue_command_test.exs b/deps/rabbitmq_cli/test/ctl/delete_queue_command_test.exs new file mode 100644 index 0000000000..b0971b8961 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/delete_queue_command_test.exs @@ -0,0 +1,119 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule DeleteQueueCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand + @user "guest" + @vhost "delete-queue-vhost" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + vhost: @vhost, + timeout: context[:test_timeout], + if_empty: false, + if_unused: false + }} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/", if_empty: false, if_unused: false}} + assert @command.merge_defaults([], %{vhost: "non_default", if_empty: true}) == + {[], %{vhost: "non_default", if_empty: true, if_unused: false}} + end + + test "validate: providing no queue name fails validation", context do + assert match?( + {:validation_failure, :not_enough_args}, + @command.validate([], context[:opts]) + ) + end + + test "validate: providing an empty queue name fails validation", context do + assert match?( + {:validation_failure, {:bad_argument, "queue name cannot be an empty string"}}, + @command.validate([""], context[:opts]) + ) + end + + test "validate: providing a non-blank queue name and -u succeeds", context do + assert @command.validate(["a-queue"], %{ + node: get_rabbit_hostname(), + vhost: @vhost, + timeout: context[:test_timeout], + if_unused: false + }) == :ok + end + + @tag test_timeout: 30000 + test "run: request to an existent queue on active node succeeds", context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(context, fn -> delete_vhost(@vhost) end) + + q = "foo" + n = 20 + + declare_queue(q, @vhost) + publish_messages(@vhost, q, n) + + assert @command.run([q], context[:opts]) == {:ok, n} + {:error, :not_found} = lookup_queue(q, @vhost) + end + + @tag test_timeout: 30000 + test "run: request to a non-existent queue on active node returns not found", context do + assert @command.run(["non-existent"], context[:opts]) == {:error, :not_found} + end + + @tag test_timeout: 0 + test "run: timeout causes command to return a bad RPC", context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(context, fn -> delete_vhost(@vhost) end) + + q = "foo" + declare_queue(q, @vhost) + assert @command.run([q], context[:opts]) == {:badrpc, :timeout} + end + + test "shows up in help" do + s = @command.usage() + assert s =~ ~r/delete_queue/ + end + + test "defaults to vhost /" do + assert @command.merge_defaults(["foo"], %{bar: "baz"}) == {["foo"], %{bar: "baz", vhost: "/", if_unused: false, if_empty: false}} + end + + test "validate: with extra arguments returns an arg count error" do + assert @command.validate(["queue-name", "extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: with no arguments returns an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: with correct args returns ok" do + assert @command.validate(["q"], %{}) == :ok + end + + test "banner informs that vhost's queue is deleted" do + assert @command.banner(["my-q"], %{vhost: "/foo", if_empty: false, if_unused: false}) == "Deleting queue 'my-q' on vhost '/foo' ..." + assert @command.banner(["my-q"], %{vhost: "/foo", if_empty: true, if_unused: false}) == "Deleting queue 'my-q' on vhost '/foo' if queue is empty ..." + assert @command.banner(["my-q"], %{vhost: "/foo", if_empty: true, if_unused: true}) == "Deleting queue 'my-q' on vhost '/foo' if queue is empty and if queue is unused ..." + end +end diff --git a/deps/rabbitmq_cli/test/ctl/delete_user_command_test.exs b/deps/rabbitmq_cli/test/ctl/delete_user_command_test.exs new file mode 100644 index 0000000000..97f09654a9 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/delete_user_command_test.exs @@ -0,0 +1,59 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule DeleteUserCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.DeleteUserCommand + @user "username" + @password "password" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + add_user(context[:user], @password) + on_exit(context, fn -> delete_user(context[:user]) end) + + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + @tag user: @user + test "validate: argument count validates" do + assert @command.validate(["one"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag user: @user + test "run: A valid username returns ok", context do + assert @command.run([context[:user]], context[:opts]) == :ok + + assert list_users() |> Enum.count(fn(record) -> record[:user] == context[:user] end) == 0 + end + + test "run: An invalid Rabbit node returns a bad rpc message" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(["username"], opts)) + end + + @tag user: @user + test "run: An invalid username returns an error", context do + assert @command.run(["no_one"], context[:opts]) == {:error, {:no_such_user, "no_one"}} + end + + @tag user: @user + test "banner", context do + s = @command.banner([context[:user]], context[:opts]) + assert s =~ ~r/Deleting user/ + assert s =~ ~r/\"#{context[:user]}\"/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/delete_vhost_command_test.exs b/deps/rabbitmq_cli/test/ctl/delete_vhost_command_test.exs new file mode 100644 index 0000000000..057f0789dc --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/delete_vhost_command_test.exs @@ -0,0 +1,67 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule DeleteVhostCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.DeleteVhostCommand + @vhost "test" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_vhost(context[:vhost]) + on_exit(context, fn -> delete_vhost(context[:vhost]) end) + + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: argument count validates" do + assert @command.validate(["tst"], %{}) == :ok + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["test", "extra"], %{}) == {:validation_failure, :too_many_args} + end + + @tag vhost: @vhost + test "run: A valid name to an active RabbitMQ node is successful", context do + assert @command.run([context[:vhost]], context[:opts]) == :ok + + assert list_vhosts() |> Enum.count(fn(record) -> record[:name] == context[:vhost] end) == 0 + end + + @tag vhost: "" + test "run: An empty string to an active RabbitMQ node is successful", context do + assert @command.run([context[:vhost]], context[:opts]) == :ok + + assert list_vhosts() |> Enum.count(fn(record) -> record[:name] == context[:vhost] end) == 0 + end + + test "run: A call to invalid or inactive RabbitMQ node returns a nodedown" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(["na"], opts)) + end + + @tag vhost: @vhost + test "run: Deleting the same host twice results in a host not found message", context do + @command.run([context[:vhost]], context[:opts]) + assert @command.run([context[:vhost]], context[:opts]) == + {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag vhost: @vhost + test "banner", context do + s = @command.banner([context[:vhost]], context[:opts]) + assert s =~ ~r/Deleting vhost/ + assert s =~ ~r/\"#{context[:vhost]}\"/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs b/deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs new file mode 100644 index 0000000000..f8a3e62920 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs @@ -0,0 +1,70 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule EnableFeatureFlagCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand + @feature_flag :ff_from_enable_ff_testsuite + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + # Define an arbitrary feature flag for the test. + node = get_rabbit_hostname() + new_feature_flags = %{ + @feature_flag => + %{desc: "My feature flag", + provided_by: :EnableFeatureFlagCommandTest, + stability: :stable}} + :ok = :rabbit_misc.rpc_call( + node, :rabbit_feature_flags, :initialize_registry, [new_feature_flags]) + + { + :ok, + opts: %{node: get_rabbit_hostname()}, + feature_flag: @feature_flag + } + end + + test "validate: wrong number of arguments results in arg count errors" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["ff_from_enable_ff_testsuite", "whoops"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: passing an empty string for feature_flag name is an arg error", context do + assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([""], context[:opts])) + end + + test "run: passing a valid feature_flag name to a running RabbitMQ node succeeds", context do + assert @command.run([Atom.to_string(context[:feature_flag])], context[:opts]) == :ok + assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag]) + end + + test "run: attempt to use an unreachable node returns a nodedown" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["na"], opts)) + end + + test "run: enabling the same feature flag twice is idempotent", context do + enable_feature_flag context[:feature_flag] + assert @command.run([Atom.to_string(context[:feature_flag])], context[:opts]) == :ok + assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag]) + end + + test "run: enabling all feature flags succeeds", context do + enable_feature_flag context[:feature_flag] + assert @command.run(["all"], context[:opts]) == :ok + assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag]) + end + + test "banner", context do + assert @command.banner([context[:feature_flag]], context[:opts]) + =~ ~r/Enabling feature flag \"#{context[:feature_flag]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/encode_command_test.exs b/deps/rabbitmq_cli/test/ctl/encode_command_test.exs new file mode 100644 index 0000000000..550e4b24da --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/encode_command_test.exs @@ -0,0 +1,92 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule EncodeCommandTest do + use ExUnit.Case, async: false + + @command RabbitMQ.CLI.Ctl.Commands.EncodeCommand + + setup _context do + {:ok, opts: %{ + cipher: :rabbit_pbe.default_cipher, + hash: :rabbit_pbe.default_hash, + iterations: :rabbit_pbe.default_iterations + }} + end + + test "validate: providing exactly 2 positional arguments passes", context do + assert :ok == @command.validate(["value", "secret"], context[:opts]) + end + + test "validate: providing zero or one positional argument fails", context do + assert match?({:validation_failure, {:not_enough_args, _}}, + @command.validate([], context[:opts])) + assert match?({:validation_failure, {:not_enough_args, _}}, + @command.validate(["value"], context[:opts])) + end + + test "validate: providing three or more positional argument fails", context do + assert match?({:validation_failure, :too_many_args}, + @command.validate(["value", "secret", "incorrect"], context[:opts])) + end + + test "validate: hash and cipher must be supported", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{cipher: :funny_cipher})) + ) + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{hash: :funny_hash})) + ) + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{cipher: :funny_cipher, hash: :funny_hash})) + ) + assert :ok == @command.validate(["value", "secret"], context[:opts]) + end + + test "validate: number of iterations must greater than 0", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{iterations: 0})) + ) + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["value", "secret"], Map.merge(context[:opts], %{iterations: -1})) + ) + assert :ok == @command.validate(["value", "secret"], context[:opts]) + end + + test "run: encrypt/decrypt", context do + # an Erlang list/bitstring + encrypt_decrypt(to_charlist("foobar"), context) + # a binary + encrypt_decrypt("foobar", context) + # a tuple + encrypt_decrypt({:password, "secret"}, context) + end + + defp encrypt_decrypt(secret, context) do + secret_as_erlang_term = format_as_erlang_term(secret) + passphrase = "passphrase" + + cipher = context[:opts][:cipher] + hash = context[:opts][:hash] + iterations = context[:opts][:iterations] + + {:ok, output} = @command.run([secret_as_erlang_term, passphrase], context[:opts]) + {:encrypted, encrypted} = output + # decode plain value + assert secret === :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, {:plaintext, secret}) + # decode {encrypted, ...} tuple form + assert secret === :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, {:encrypted, encrypted}) + end + + defp format_as_erlang_term(value) do + :io_lib.format("~p", [value]) |> :lists.flatten() |> to_string() + end +end diff --git a/deps/rabbitmq_cli/test/ctl/environment_command_test.exs b/deps/rabbitmq_cli/test/ctl/environment_command_test.exs new file mode 100644 index 0000000000..7f801d54dc --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/environment_command_test.exs @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule EnvironmentCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.EnvironmentCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: argument count validates" do + assert @command.validate([], %{}) == :ok + assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} + end + + @tag target: get_rabbit_hostname() + test "run: environment request on a named, active RMQ node is successful", context do + assert @command.run([], context[:opts])[:kernel] != nil + assert @command.run([], context[:opts])[:rabbit] != nil + end + + test "run: environment request on nonexistent RabbitMQ node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Application environment of node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/eval_command_test.exs b/deps/rabbitmq_cli/test/ctl/eval_command_test.exs new file mode 100644 index 0000000000..92a2d77667 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/eval_command_test.exs @@ -0,0 +1,74 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule EvalCommandTest do + use ExUnit.Case, async: false + import TestHelper + import ExUnit.CaptureIO + + @command RabbitMQ.CLI.Ctl.Commands.EvalCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup _ do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: providing no arguments succeeds" do + # expression is expected to be provided via standard input + assert @command.validate([], %{}) == :ok + end + + test "validate: empty expression to eval fails validation" do + assert @command.validate([""], %{}) == {:validation_failure, "Expression must not be blank"} + assert @command.validate(["", "foo"], %{}) == {:validation_failure, "Expression must not be blank"} + end + + test "validate: syntax error in expression to eval fails validation" do + assert @command.validate(["foo bar"], %{}) == {:validation_failure, "syntax error before: bar"} + assert @command.validate(["foo bar", "foo"], %{}) == {:validation_failure, "syntax error before: bar"} + end + + test "run: request to a non-existent node returns a badrpc", _context do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(["ok."], opts)) + end + + test "run: evaluates provided Erlang expression", context do + assert @command.run(["foo."], context[:opts]) == {:ok, :foo} + assert @command.run(["length([1,2,3])."], context[:opts]) == {:ok, 3} + assert @command.run(["lists:sum([1,2,3])."], context[:opts]) == {:ok, 6} + {:ok, apps} = @command.run(["application:loaded_applications()."], context[:opts]) + assert is_list(apps) + end + + test "run: evaluates provided expression on the target server node", context do + {:ok, apps} = @command.run(["application:loaded_applications()."], context[:opts]) + assert is_list(apps) + assert List.keymember?(apps, :rabbit, 0) + end + + test "run: returns stdout output", context do + assert capture_io(fn -> + assert @command.run(["io:format(\"output\")."], context[:opts]) == {:ok, :ok} + end) == "output" + end + + test "run: passes parameters to the expression as positional/numerical variables", context do + assert @command.run(["binary_to_atom(_1, utf8).", "foo"], context[:opts]) == {:ok, :foo} + assert @command.run(["{_1, _2}.", "foo", "bar"], context[:opts]) == {:ok, {"foo", "bar"}} + end + + test "run: passes globally recognised options as named variables", context do + assert @command.run(["{_vhost, _node}."], Map.put(context[:opts], :vhost, "a-node")) == + {:ok, {"a-node", context[:opts][:node]}} + end +end diff --git a/deps/rabbitmq_cli/test/ctl/eval_file_command_test.exs b/deps/rabbitmq_cli/test/ctl/eval_file_command_test.exs new file mode 100644 index 0000000000..74cb272f98 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/eval_file_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule EvalFileCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.EvalFileCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup _ do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: providing no arguments fails validation" do + # expression is expected to be provided via standard input + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: empty file path fails validation" do + assert @command.validate([""], %{}) == {:validation_failure, "File path must not be blank"} + end + + test "validate: path to a non-existent file fails validation" do + path = "/tmp/rabbitmq/cli-tests/12937293782368263726.lolz.escript" + assert @command.validate([path], %{}) == {:validation_failure, "File #{path} does not exist"} + end + + test "run: request to a non-existent node returns a badrpc", _context do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([valid_file_path()], opts)) + end + + test "run: evaluates expressions in the file on the target server node", context do + {:ok, apps} = @command.run([loaded_applications_file_path()], context[:opts]) + assert is_list(apps) + assert List.keymember?(apps, :rabbit, 0) + end + + test "run: returns evaluation result", context do + assert {:ok, 2} == @command.run([valid_file_path()], context[:opts]) + end + + test "run: reports invalid syntax errors", context do + assert match?({:error, _}, @command.run([invalid_file_path()], context[:opts])) + end + + # + # Implementation + # + + defp valid_file_path() do + Path.join([File.cwd!(), "test", "fixtures", "files", "valid_erl_expression.escript"]) + end + + defp invalid_file_path() do + Path.join([File.cwd!(), "test", "fixtures", "files", "invalid_erl_expression.escript"]) + end + + defp loaded_applications_file_path() do + Path.join([File.cwd!(), "test", "fixtures", "files", "loaded_applications.escript"]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/exec_command_test.exs b/deps/rabbitmq_cli/test/ctl/exec_command_test.exs new file mode 100644 index 0000000000..bb839f5434 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/exec_command_test.exs @@ -0,0 +1,47 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ExecCommandTest do + use ExUnit.Case, async: false + + @command RabbitMQ.CLI.Ctl.Commands.ExecCommand + + setup _ do + {:ok, opts: %{}} + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: there should be only one argument" do + assert @command.validate(["foo", "bar"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["", "bar"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: empty expression to exec fails validation" do + assert @command.validate([""], %{}) == {:validation_failure, "Expression must not be blank"} + end + + test "validate: success" do + :ok = @command.validate([":ok"], %{}) + end + + test "run: executes elixir code" do + {:ok, :ok} = @command.run([":ok"], %{}) + node = Node.self() + {:ok, ^node} = @command.run(["Node.self()"], %{}) + {:ok, 3} = @command.run(["1 + 2"], %{}) + end + + test "run: binds options variable" do + opts = %{my: :custom, option: 123} + {:ok, ^opts} = @command.run(["options"], opts) + {:ok, 123} = @command.run(["options[:option]"], opts) + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/export_definitions_command_test.exs b/deps/rabbitmq_cli/test/ctl/export_definitions_command_test.exs new file mode 100644 index 0000000000..3506b1ea80 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/export_definitions_command_test.exs @@ -0,0 +1,138 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ExportDefinitionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ExportDefinitionsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + format: context[:format] || "json" + }} + end + + test "merge_defaults: defaults to JSON for format" do + assert @command.merge_defaults([valid_file_path()], %{}) == + {[valid_file_path()], %{format: "json"}} + end + + test "merge_defaults: defaults to --silent if target is stdout" do + assert @command.merge_defaults(["-"], %{}) == {["-"], %{format: "json", silent: true}} + end + + test "merge_defaults: format is case insensitive" do + assert @command.merge_defaults([valid_file_path()], %{format: "JSON"}) == + {[valid_file_path()], %{format: "json"}} + assert @command.merge_defaults([valid_file_path()], %{format: "Erlang"}) == + {[valid_file_path()], %{format: "erlang"}} + end + + test "merge_defaults: format can be overridden" do + assert @command.merge_defaults([valid_file_path()], %{format: "erlang"}) == + {[valid_file_path()], %{format: "erlang"}} + end + + test "validate: accepts a file path argument", context do + assert @command.validate([valid_file_path()], context[:opts]) == :ok + end + + test "validate: accepts a dash for stdout", context do + assert @command.validate(["-"], context[:opts]) == :ok + end + + test "validate: unsupported format fails validation", context do + assert match?({:validation_failure, {:bad_argument, _}}, + @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "yolo"}))) + end + + test "validate: no positional arguments fails validation", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate: more than one positional argument fails validation", context do + assert @command.validate([valid_file_path(), "extra-arg"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: supports JSON and Erlang formats", context do + assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "json"})) == :ok + assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "erlang"})) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + result = @command.run([valid_file_path()], + %{node: :jake@thedog, + timeout: context[:test_timeout], + format: "json"}) + assert match?({:badrpc, _}, result) + end + + @tag format: "json" + test "run: returns a list of definitions when target is stdout and format is JSON", context do + {:ok, map} = @command.run(["-"], context[:opts]) + assert Map.has_key?(map, :rabbitmq_version) + end + + @tag format: "erlang" + test "run: returns a list of definitions when target is stdout and format is Erlang Terms", context do + {:ok, map} = @command.run(["-"], context[:opts]) + assert Map.has_key?(map, :rabbitmq_version) + end + + @tag format: "json" + test "run: writes to a file and returns nil when target is a file and format is JSON", context do + File.rm(valid_file_path()) + {:ok, nil} = @command.run([valid_file_path()], context[:opts]) + + {:ok, bin} = File.read(valid_file_path()) + {:ok, map} = JSON.decode(bin) + assert Map.has_key?(map, "rabbitmq_version") + end + + @tag format: "json" + test "run: correctly formats runtime parameter values", context do + File.rm(valid_file_path()) + imported_file_path = Path.join([File.cwd!(), "test", "fixtures", "files", "definitions.json"]) + # prepopulate some runtime parameters + RabbitMQ.CLI.Ctl.Commands.ImportDefinitionsCommand.run([imported_file_path], context[:opts]) + + {:ok, nil} = @command.run([valid_file_path()], context[:opts]) + + # clean up the state we've modified + clear_parameter("/", "federation-upstream", "up-1") + + {:ok, bin} = File.read(valid_file_path()) + {:ok, map} = JSON.decode(bin) + assert Map.has_key?(map, "rabbitmq_version") + params = map["parameters"] + assert is_map(hd(params)["value"]) + end + + @tag format: "erlang" + test "run: writes to a file and returns nil when target is a file and format is Erlang Terms", context do + File.rm(valid_file_path()) + {:ok, nil} = @command.run([valid_file_path()], context[:opts]) + + {:ok, bin} = File.read(valid_file_path()) + map = :erlang.binary_to_term(bin) + assert Map.has_key?(map, :rabbitmq_version) + end + + defp valid_file_path(), do: "#{System.tmp_dir()}/definitions" +end diff --git a/deps/rabbitmq_cli/test/ctl/force_boot_command_test.exs b/deps/rabbitmq_cli/test/ctl/force_boot_command_test.exs new file mode 100644 index 0000000000..a33d7b2e89 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/force_boot_command_test.exs @@ -0,0 +1,63 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ForceBootCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ForceBootCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup _ do + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: 1000 + } + } + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: the rabbit app running on target node fails validation", context do + assert @command.validate_execution_environment([], context[:opts]) == + {:validation_failure, :rabbit_app_is_running} + end + + test "run: sets a force boot marker file on target node", context do + stop_rabbitmq_app() + on_exit(fn -> start_rabbitmq_app() end) + assert @command.run([], context[:opts]) == :ok + mnesia_dir = :rpc.call(get_rabbit_hostname(), :rabbit_mnesia, :dir, []) + + path = Path.join(mnesia_dir, "force_load") + assert File.exists?(path) + File.rm(path) + end + + test "run: if RABBITMQ_MNESIA_DIR is defined, creates a force boot marker file" do + node = :unknown@localhost + temp_dir = "#{Mix.Project.config()[:elixirc_paths]}/tmp" + File.mkdir_p!(temp_dir) + on_exit(fn -> File.rm_rf!(temp_dir) end) + System.put_env("RABBITMQ_MNESIA_DIR", temp_dir) + + assert @command.run([], %{node: node}) == :ok + assert File.exists?(Path.join(temp_dir, "force_load")) + + System.delete_env("RABBITMQ_MNESIA_DIR") + File.rm_rf(temp_dir) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/force_gc_command_test.exs b/deps/rabbitmq_cli/test/ctl/force_gc_command_test.exs new file mode 100644 index 0000000000..b9583931d3 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/force_gc_command_test.exs @@ -0,0 +1,46 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ForceGcCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ForceGcCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + reset_vm_memory_high_watermark() + + on_exit([], fn -> + reset_vm_memory_high_watermark() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 200}} + end + + + test "merge_defaults: merge not defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: with extra arguments returns an error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: request to a non-existent node returns a badrpc" do + assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, timeout: 200})) + end + + test "run: request to a named, active node succeeds", context do + assert @command.run([], context[:opts]) == :ok + end +end diff --git a/deps/rabbitmq_cli/test/ctl/force_reset_command_test.exs b/deps/rabbitmq_cli/test/ctl/force_reset_command_test.exs new file mode 100644 index 0000000000..5b695302f4 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/force_reset_command_test.exs @@ -0,0 +1,68 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ForceResetCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ForceResetCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: force reset request to an active node with a stopped rabbit app succeeds", context do + add_vhost "some_vhost" + # ensure the vhost really does exist + assert vhost_exists? "some_vhost" + stop_rabbitmq_app() + assert :ok == @command.run([], context[:opts]) + start_rabbitmq_app() + # check that the created vhost no longer exists + assert match?([_], list_vhosts()) + end + + test "run: reset request to an active node with a running rabbit app fails", context do + add_vhost "some_vhost" + assert vhost_exists? "some_vhost" + assert match?({:error, :mnesia_unexpectedly_running}, @command.run([], context[:opts])) + assert vhost_exists? "some_vhost" + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Forcefully resetting node #{get_rabbit_hostname()}/ + end + + test "output mnesia is running error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, + "Mnesia is still running on node " <> _}, + @command.output({:error, :mnesia_unexpectedly_running}, context[:opts])) + + end +end diff --git a/deps/rabbitmq_cli/test/ctl/forget_cluster_node_command_test.exs b/deps/rabbitmq_cli/test/ctl/forget_cluster_node_command_test.exs new file mode 100644 index 0000000000..0f09e4fee8 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/forget_cluster_node_command_test.exs @@ -0,0 +1,132 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ForgetClusterNodeCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ForgetClusterNodeCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + start_rabbitmq_app() + + {:ok, plugins_dir} = + :rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir]) + + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + mnesia_dir = :rabbit_misc.rpc_call(node, :rabbit_mnesia, :dir, []) + + feature_flags_file = + :rabbit_misc.rpc_call(node, :rabbit_feature_flags, :enabled_feature_flags_list_file, []) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + {:ok, + opts: %{ + rabbitmq_home: rabbitmq_home, + plugins_dir: plugins_dir, + mnesia_dir: mnesia_dir, + feature_flags_file: feature_flags_file, + offline: false + }} + end + + setup context do + {:ok, opts: Map.merge(context[:opts], %{node: get_rabbit_hostname()})} + end + + test "validate: specifying no target node is reported as an error", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate: specifying multiple target nodes is reported as an error", context do + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate_execution_environment: offline request to a running node fails", context do + assert match?( + {:validation_failure, :node_running}, + @command.validate_execution_environment( + ["other_node@localhost"], + Map.merge(context[:opts], %{offline: true}) + ) + ) + end + + test "validate_execution_environment: offline forget without mnesia dir fails", context do + offline_opts = + Map.merge( + context[:opts], + %{offline: true, node: :non_exist@localhost} + ) + + opts_without_mnesia = Map.delete(offline_opts, :mnesia_dir) + Application.put_env(:mnesia, :dir, "/tmp") + on_exit(fn -> Application.delete_env(:mnesia, :dir) end) + + assert match?( + :ok, + @command.validate_execution_environment( + ["other_node@localhost"], + opts_without_mnesia + ) + ) + + Application.delete_env(:mnesia, :dir) + System.put_env("RABBITMQ_MNESIA_DIR", "/tmp") + on_exit(fn -> System.delete_env("RABBITMQ_MNESIA_DIR") end) + + assert match?( + :ok, + @command.validate_execution_environment( + ["other_node@localhost"], + opts_without_mnesia + ) + ) + + System.delete_env("RABBITMQ_MNESIA_DIR") + + assert match?( + :ok, + @command.validate_execution_environment(["other_node@localhost"], offline_opts) + ) + end + + test "validate_execution_environment: online mode does not fail is mnesia is not loaded", + context do + opts_without_mnesia = Map.delete(context[:opts], :mnesia_dir) + + assert match?( + :ok, + @command.validate_execution_environment( + ["other_node@localhost"], + opts_without_mnesia + ) + ) + end + + test "run: online request to a non-existent node returns a badrpc", context do + assert match?( + {:badrpc, :nodedown}, + @command.run( + [context[:opts][:node]], + Map.merge(context[:opts], %{node: :non_exist@localhost}) + ) + ) + end + + test "banner", context do + assert @command.banner(["a"], context[:opts]) =~ + ~r/Removing node a from the cluster/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/help_command_test.exs b/deps/rabbitmq_cli/test/ctl/help_command_test.exs new file mode 100644 index 0000000000..d30a4d98c7 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/help_command_test.exs @@ -0,0 +1,76 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule HelpCommandTest do + use ExUnit.Case, async: false + import TestHelper + + alias RabbitMQ.CLI.Core.{CommandModules} + + @command RabbitMQ.CLI.Ctl.Commands.HelpCommand + + setup_all do + set_scope(:all) + :ok + end + + test "validate: providing no position arguments passes validation" do + assert @command.validate([], %{}) == :ok + end + + test "validate: providing one position argument passes validation" do + assert @command.validate(["status"], %{}) == :ok + end + + test "validate: providing two or more position arguments fails validation" do + assert @command.validate(["extra1", "extra2"], %{}) == + {:validation_failure, :too_many_args} + end + + test "run: prints basic usage info" do + {:ok, lines} = @command.run([], %{}) + output = Enum.join(lines, "\n") + assert output =~ ~r/[-n <node>] [-t <timeout>]/ + assert output =~ ~r/commands/i + end + + test "run: ctl command usage info is printed if command is specified" do + ctl_commands = CommandModules.module_map + |> Enum.filter(fn({_name, command_mod}) -> + to_string(command_mod) =~ ~r/^RabbitMQ\.CLI\.Ctl\.Commands/ + end) + |> Enum.map(fn({name, _}) -> name end) + + IO.inspect(ctl_commands) + Enum.each( + ctl_commands, + fn(command) -> + assert @command.run([command], %{}) =~ ~r/#{command}/ + end) + end + + test "run prints command info" do + ctl_commands = CommandModules.module_map + |> Enum.filter(fn({_name, command_mod}) -> + to_string(command_mod) =~ ~r/^RabbitMQ\.CLI\.Ctl\.Commands/ + end) + |> Enum.map(fn({name, _}) -> name end) + + Enum.each( + ctl_commands, + fn(command) -> + {:ok, lines} = @command.run([], %{}) + output = Enum.join(lines, "\n") + assert output =~ ~r/\n\s+#{command}.*\n/ + end) + end + + test "run: exits with the code of OK" do + assert @command.output({:ok, "Help string"}, %{}) == + {:ok, "Help string"} + end +end diff --git a/deps/rabbitmq_cli/test/ctl/import_definitions_command_test.exs b/deps/rabbitmq_cli/test/ctl/import_definitions_command_test.exs new file mode 100644 index 0000000000..fb7f975ec5 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/import_definitions_command_test.exs @@ -0,0 +1,88 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ImportDefinitionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ImportDefinitionsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + format: context[:format] || "json" + }} + end + + test "merge_defaults: defaults to JSON for format" do + assert @command.merge_defaults([valid_file_path()], %{}) == + {[valid_file_path()], %{format: "json"}} + end + + test "merge_defaults: defaults to --silent if target is stdout" do + assert @command.merge_defaults(["-"], %{}) == {["-"], %{format: "json", silent: true}} + end + + test "merge_defaults: format is case insensitive" do + assert @command.merge_defaults([valid_file_path()], %{format: "JSON"}) == + {[valid_file_path()], %{format: "json"}} + assert @command.merge_defaults([valid_file_path()], %{format: "Erlang"}) == + {[valid_file_path()], %{format: "erlang"}} + end + + test "merge_defaults: format can be overridden" do + assert @command.merge_defaults([valid_file_path()], %{format: "erlang"}) == + {[valid_file_path()], %{format: "erlang"}} + end + + test "validate: accepts a file path argument", context do + assert @command.validate([valid_file_path()], context[:opts]) == :ok + end + + test "validate: unsupported format fails validation", context do + assert match?({:validation_failure, {:bad_argument, _}}, + @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "yolo"}))) + end + + test "validate: more than one positional argument fails validation", context do + assert @command.validate([valid_file_path(), "extra-arg"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: supports JSON and Erlang formats", context do + assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "json"})) == :ok + assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "erlang"})) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + result = @command.run([valid_file_path()], + %{node: :jake@thedog, + timeout: context[:test_timeout], + format: "json"}) + assert match?({:badrpc, _}, result) + end + + @tag format: "json" + test "run: imports definitions from a file", context do + assert :ok == @command.run([valid_file_path()], context[:opts]) + + # clean up the state we've modified + clear_parameter("/", "federation-upstream", "up-1") + end + + defp valid_file_path() do + Path.join([File.cwd!(), "test", "fixtures", "files", "definitions.json"]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/join_cluster_command_test.exs b/deps/rabbitmq_cli/test/ctl/join_cluster_command_test.exs new file mode 100644 index 0000000000..2a9c7ec861 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/join_cluster_command_test.exs @@ -0,0 +1,104 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule JoinClusterCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.JoinClusterCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname(), + disc: true, + ram: false, + }} + end + + test "validate: specifying both --disc and --ram is reported as invalid", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["a"], Map.merge(context[:opts], %{disc: true, ram: true})) + ) + end + test "validate: specifying no target node is reported as an error", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + test "validate: specifying multiple target nodes is reported as an error", context do + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + # TODO + #test "run: successful join as a disc node", context do + #end + + # TODO + #test "run: successful join as a RAM node", context do + #end + + test "run: joining self is invalid", context do + stop_rabbitmq_app() + assert match?( + {:error, :cannot_cluster_node_with_itself}, + @command.run([context[:opts][:node]], context[:opts])) + start_rabbitmq_app() + end + + # TODO + test "run: request to an active node fails", context do + assert match?( + {:error, :mnesia_unexpectedly_running}, + @command.run([context[:opts][:node]], context[:opts])) + end + + test "run: request to a non-existent node returns a badrpc", context do + opts = %{ + node: :jake@thedog, + disc: true, + ram: false, + timeout: 200 + } + assert match?( + {:badrpc, _}, + @command.run([context[:opts][:node]], opts)) + end + + test "run: joining a non-existent node returns a badrpc", context do + stop_rabbitmq_app() + assert match?( + {:badrpc_multi, _, [_]}, + @command.run([:jake@thedog], context[:opts])) + start_rabbitmq_app() + end + + test "banner", context do + assert @command.banner(["a"], context[:opts]) =~ + ~r/Clustering node #{get_rabbit_hostname()} with a/ + end + + test "output mnesia is running error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, + "Mnesia is still running on node " <> _}, + @command.output({:error, :mnesia_unexpectedly_running}, context[:opts])) + + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_bindings_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_bindings_command_test.exs new file mode 100644 index 0000000000..dae2377322 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_bindings_command_test.exs @@ -0,0 +1,85 @@ +defmodule ListBindingsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListBindingsCommand + @vhost "test1" + @user "guest" + @default_timeout :infinity + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(fn -> + delete_vhost @vhost + end) + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout, + vhost: @vhost + } + } + end + + test "merge_defaults: adds all keys if none specificed", context do + default_keys = ~w(source_name source_kind destination_name destination_kind routing_key arguments) + declare_queue("test_queue", @vhost) + :timer.sleep(100) + + {keys, _} = @command.merge_defaults([], context[:opts]) + assert default_keys == keys + end + + test "merge_defaults: includes table headers by default", _context do + {_, opts} = @command.merge_defaults([], %{}) + assert opts[:table_headers] + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: returns multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "source_name"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["source_name", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["source_kind", "oink", "source_name"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["source_name"], context[:opts]]) == + [{:badrpc, {:timeout, 0.0}}] + end + + test "run: no bindings for no queues", context do + [] = run_command_to_list(@command, [["source_name"], context[:opts]]) + end + + test "run: can filter info keys", context do + wanted_keys = ~w(source_name destination_name routing_key) + declare_queue("test_queue", @vhost) + assert run_command_to_list(@command, [wanted_keys, context[:opts]]) == + [[source_name: "", destination_name: "test_queue", routing_key: "test_queue"]] + end + + test "banner" do + assert String.starts_with?(@command.banner([], %{vhost: "some_vhost"}), "Listing bindings") + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_channels_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_channels_command_test.exs new file mode 100644 index 0000000000..6ccf602211 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_channels_command_test.exs @@ -0,0 +1,118 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListChannelsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListChannelsCommand + @default_timeout :infinity + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + close_all_connections(get_rabbit_hostname()) + + on_exit([], fn -> + close_all_connections(get_rabbit_hostname()) + end) + + :ok + end + + setup context do + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout + } + } + end + + test "merge_defaults: default channel info keys are pid, user, consumer_count, and messages_unacknowledged", context do + assert match?({~w(pid user consumer_count messages_unacknowledged), _}, @command.merge_defaults([], context[:opts])) + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: returns multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: returns bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "pid"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["user", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["user", "oink", "pid"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: zero timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["user"], context[:opts]]) == + [{:badrpc, {:timeout, 0.0}}] + end + + test "run: multiple channels on multiple connections", context do + node_name = get_rabbit_hostname() + close_all_connections(node_name) + existent_channels = :rabbit_misc.rpc_call(node_name,:rabbit_channel, :list, []) + with_channel("/", fn(_channel1) -> + with_channel("/", fn(_channel2) -> + all_channels = run_command_to_list(@command, [["pid", "user", "connection"], context[:opts]]) + channels = Enum.filter(all_channels, + fn(ch) -> + not Enum.member?(existent_channels, ch[:pid]) + end) + chan1 = Enum.at(channels, 0) + chan2 = Enum.at(channels, 1) + assert Keyword.keys(chan1) == ~w(pid user connection)a + assert Keyword.keys(chan2) == ~w(pid user connection)a + assert "guest" == chan1[:user] + assert "guest" == chan2[:user] + assert chan1[:pid] !== chan2[:pid] + end) + end) + end + + test "run: multiple channels on single connection", context do + node_name = get_rabbit_hostname() + close_all_connections(get_rabbit_hostname()) + with_connection("/", fn(conn) -> + existent_channels = :rabbit_misc.rpc_call(node_name,:rabbit_channel, :list, []) + {:ok, _} = AMQP.Channel.open(conn) + {:ok, _} = AMQP.Channel.open(conn) + all_channels = run_command_to_list(@command, [["pid", "user", "connection"], context[:opts]]) + channels = Enum.filter(all_channels, + fn(ch) -> + not Enum.member?(existent_channels, ch[:pid]) + end) + + chan1 = Enum.at(channels, 0) + chan2 = Enum.at(channels, 1) + assert Keyword.keys(chan1) == ~w(pid user connection)a + assert Keyword.keys(chan2) == ~w(pid user connection)a + assert "guest" == chan1[:user] + assert "guest" == chan2[:user] + assert chan1[:pid] !== chan2[:pid] + end) + end + + test "run: info keys order is preserved", context do + close_all_connections(get_rabbit_hostname()) + with_channel("/", fn(_channel) -> + channels = run_command_to_list(@command, [~w(connection vhost name pid number user), context[:opts]]) + chan = Enum.at(channels, 0) + assert Keyword.keys(chan) == ~w(connection vhost name pid number user)a + end) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_ciphers_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_ciphers_command_test.exs new file mode 100644 index 0000000000..6f600ba5d8 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_ciphers_command_test.exs @@ -0,0 +1,29 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListCiphersCommandTest do + use ExUnit.Case + @command RabbitMQ.CLI.Ctl.Commands.ListCiphersCommand + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + test "run: lists ciphers", _context do + assert match?( + {:ok, _}, + @command.run([], %{}) + ) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_connections_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_connections_command_test.exs new file mode 100644 index 0000000000..9cfcb8787f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_connections_command_test.exs @@ -0,0 +1,90 @@ +defmodule ListConnectionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListConnectionsCommand + @user "guest" + @default_timeout 15000 + @default_options %{table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + close_all_connections(get_rabbit_hostname()) + + on_exit([], fn -> + close_all_connections(get_rabbit_hostname()) + end) + + :ok + end + + setup context do + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout + } + } + end + + test "merge_defaults: user, peer_host, peer_port and state by default" do + assert @command.merge_defaults([], %{}) == {~w(user peer_host peer_port state), @default_options} + end + + test "merge_defaults: includes table headers by default", _context do + {_, opts} = @command.merge_defaults([], %{}) + assert opts[:table_headers] + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "peer_host"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["user", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["user", "oink", "peer_host"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["name"], context[:opts]]) == + [{:badrpc, {:timeout, 0.0}}] + end + + test "run: filter single key", context do + vhost = "/" + with_connection(vhost, fn(_conn) -> + conns = run_command_to_list(@command, [["name"], context[:opts]]) + assert (Enum.map(conns, &Keyword.keys/1) |> Enum.uniq) == [[:name]] + assert Enum.any?(conns, fn(conn) -> conn[:name] != nil end) + end) + end + + test "run: show connection vhost", context do + vhost = "custom_vhost" + add_vhost vhost + set_permissions @user, vhost, [".*", ".*", ".*"] + on_exit(fn -> + delete_vhost vhost + end) + with_connection(vhost, fn(_conn) -> + conns = run_command_to_list(@command, [["vhost"], context[:opts]]) + assert (Enum.map(conns, &Keyword.keys/1) |> Enum.uniq) == [[:vhost]] + assert Enum.any?(conns, fn(conn) -> conn[:vhost] == vhost end) + end) + end + + +end diff --git a/deps/rabbitmq_cli/test/ctl/list_consumers_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_consumers_command_test.exs new file mode 100644 index 0000000000..d49313162a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_consumers_command_test.exs @@ -0,0 +1,213 @@ +defmodule ListConsumersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListConsumersCommand + + @vhost "test1" + @user "guest" + @default_timeout :infinity + @info_keys ~w(queue_name channel_pid consumer_tag ack_required prefetch_count active arguments) + @default_options %{vhost: "/", table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(fn -> + delete_vhost @vhost + end) + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout, + vhost: @vhost + } + } + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {@info_keys, @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {@info_keys, %{vhost: "non_default", + table_headers: true}} + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: returns multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "queue_name"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["queue_name", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["channel_pid", "oink", "queue_name"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: zero timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["queue_name"], context[:opts]]) == + [{:badrpc, {:timeout, 0.0}}] + end + + test "run: no consumers for no open connections", context do + close_all_connections(get_rabbit_hostname()) + [] = run_command_to_list(@command, [["queue_name"], context[:opts]]) + end + + test "run: defaults test", context do + queue_name = "test_queue1" + consumer_tag = "i_am_consumer" + info_keys_s = ~w(queue_name channel_pid consumer_tag ack_required prefetch_count arguments) + info_keys_a = Enum.map(info_keys_s, &String.to_atom/1) + declare_queue(queue_name, @vhost) + with_channel(@vhost, fn(channel) -> + {:ok, _} = AMQP.Basic.consume(channel, queue_name, nil, [consumer_tag: consumer_tag]) + :timer.sleep(100) + [[consumer]] = run_command_to_list(@command, [info_keys_s, context[:opts]]) + assert info_keys_a == Keyword.keys(consumer) + assert consumer[:consumer_tag] == consumer_tag + assert consumer[:queue_name] == queue_name + assert Keyword.delete(consumer, :channel_pid) == + [queue_name: queue_name, consumer_tag: consumer_tag, + ack_required: true, prefetch_count: 0, arguments: []] + + end) + end + + test "run: consumers are grouped by queues (multiple consumer per queue)", context do + queue_name1 = "test_queue1" + queue_name2 = "test_queue2" + declare_queue("test_queue1", @vhost) + declare_queue("test_queue2", @vhost) + with_channel(@vhost, fn(channel) -> + {:ok, tag1} = AMQP.Basic.consume(channel, queue_name1) + {:ok, tag2} = AMQP.Basic.consume(channel, queue_name2) + {:ok, tag3} = AMQP.Basic.consume(channel, queue_name2) + :timer.sleep(100) + try do + consumers = run_command_to_list(@command, [["queue_name", "consumer_tag"], context[:opts]]) + {[[consumer1]], [consumers2]} = Enum.split_with(consumers, fn([_]) -> true; ([_,_]) -> false end) + assert [queue_name: queue_name1, consumer_tag: tag1] == consumer1 + assert Keyword.equal?([{tag2, queue_name2}, {tag3, queue_name2}], + for([queue_name: q, consumer_tag: t] <- consumers2, do: {t, q})) + after + AMQP.Basic.cancel(channel, tag1) + AMQP.Basic.cancel(channel, tag2) + AMQP.Basic.cancel(channel, tag3) + end + end) + end + + test "run: active and activity status fields are set properly when requested", context do + queue_types = ["classic", "quorum"] + Enum.each queue_types, fn queue_type -> + queue_name = "active-activity-status-fields-" <> queue_type + declare_queue(queue_name, @vhost, true, false, [{"x-queue-type", :longstr, queue_type}]) + :timer.sleep(200) + with_channel(@vhost, fn(channel) -> + {:ok, tag1} = AMQP.Basic.consume(channel, queue_name) + {:ok, tag2} = AMQP.Basic.consume(channel, queue_name) + {:ok, tag3} = AMQP.Basic.consume(channel, queue_name) + :timer.sleep(100) + try do + consumers = List.first(run_command_to_list(@command, [["queue_name", "consumer_tag", "active", "activity_status"], context[:opts]])) + assert Keyword.equal?([{tag1, queue_name, true, :up}, + {tag2, queue_name, true, :up}, {tag3, queue_name, true, :up}], + for([queue_name: q, consumer_tag: t, active: a, activity_status: as] <- consumers, do: {t, q, a, as})) + after + AMQP.Basic.cancel(channel, tag1) + AMQP.Basic.cancel(channel, tag2) + AMQP.Basic.cancel(channel, tag3) + :timer.sleep(100) + delete_queue(queue_name, @vhost) + end + end) + end + end + + test "run: active and activity status fields are set properly when requested and single active consumer is enabled", context do + queue_types = ["classic", "quorum"] + Enum.each queue_types, fn queue_type -> + queue_name = "single-active-consumer-" <> queue_type + declare_queue(queue_name, @vhost, true, false, + [{"x-single-active-consumer", :bool, true}, {"x-queue-type", :longstr, queue_type}]) + :timer.sleep(200) + with_channel(@vhost, fn(channel) -> + {:ok, tag1} = AMQP.Basic.consume(channel, queue_name) + {:ok, tag2} = AMQP.Basic.consume(channel, queue_name) + {:ok, tag3} = AMQP.Basic.consume(channel, queue_name) + :timer.sleep(100) + try do + consumers = List.first(run_command_to_list(@command, [["queue_name", "consumer_tag", "active", "activity_status"], context[:opts]])) + assert Keyword.equal?([{tag1, queue_name, true, :single_active}, + {tag2, queue_name, false, :waiting}, {tag3, queue_name, false, :waiting}], + for([queue_name: q, consumer_tag: t, active: a, activity_status: as] <- consumers, do: {t, q, a, as})) + AMQP.Basic.cancel(channel, tag1) + :timer.sleep(100) + consumers = List.first(run_command_to_list(@command, [["queue_name", "consumer_tag", "active", "activity_status"], context[:opts]])) + assert Keyword.equal?([{tag2, queue_name, true, :single_active}, {tag3, queue_name, false, :waiting}], + for([queue_name: q, consumer_tag: t, active: a, activity_status: as] <- consumers, do: {t, q, a, as})) + after + AMQP.Basic.cancel(channel, tag2) + AMQP.Basic.cancel(channel, tag3) + :timer.sleep(100) + delete_queue(queue_name, @vhost) + end + end) + end + end + + test "fill_consumer_active_fields: add missing fields if necessary" do + consumer38 = [ + queue_name: {:resource, "/", :queue, "queue1"}, + channel_pid: "", + consumer_tag: "ctag1", + ack_required: false, + prefetch_count: 0, + active: true, + activity_status: :up, + arguments: [] + ] + assert @command.fill_consumer_active_fields({[ + consumer38 + ], {1, :continue}}) == {[consumer38], {1, :continue}} + + assert @command.fill_consumer_active_fields({[ + [ + queue_name: {:resource, "/", :queue, "queue2"}, + channel_pid: "", + consumer_tag: "ctag2", + ack_required: false, + prefetch_count: 0, + arguments: [] + ] + ], {1, :continue}}) == {[ + [ + queue_name: {:resource, "/", :queue, "queue2"}, + channel_pid: "", + consumer_tag: "ctag2", + ack_required: false, + prefetch_count: 0, + active: true, + activity_status: :up, + arguments: [] + ] + ], {1, :continue}} + + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/list_exchanges_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_exchanges_command_test.exs new file mode 100644 index 0000000000..fd89cfd066 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_exchanges_command_test.exs @@ -0,0 +1,160 @@ +defmodule ListExchangesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListExchangesCommand + + @vhost "test1" + @user "guest" + @default_timeout :infinity + @default_exchanges [{"amq.direct", :direct}, + {"amq.fanout", :fanout}, + {"amq.match", :headers}, + {"amq.rabbitmq.trace", :topic}, + {"amq.headers", :headers}, + {"amq.topic", :topic}, + {"", :direct}] + @default_options %{vhost: "/", table_headers: true} + + defp default_exchange_names() do + {names, _types} = Enum.unzip(@default_exchanges) + names + end + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(fn -> + delete_vhost @vhost + end) + { + :ok, + opts: %{ + quiet: true, + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout, + vhost: @vhost + } + } + end + + test "merge_defaults: should include name and type when no arguments provided and add default vhost to opts" do + assert @command.merge_defaults([], %{}) + == {["name", "type"], @default_options} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {["name", "type"], @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {["name", "type"], %{vhost: "non_default", + table_headers: true}} + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: returns multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "type"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["name", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["name", "oink", "type"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: zero timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["name"], context[:opts]]) == + [{:badrpc, {:timeout, 0.0}}] + end + + test "run: show default exchanges by default", context do + assert MapSet.new(run_command_to_list(@command, [["name"], context[:opts]])) == + MapSet.new(for {ex_name, _ex_type} <- @default_exchanges, do: [name: ex_name]) + end + + test "run: default options test", context do + exchange_name = "test_exchange" + declare_exchange(exchange_name, @vhost) + assert MapSet.new(run_command_to_list(@command, [["name", "type"], context[:opts]])) == + MapSet.new( + for({ex_name, ex_type} <- @default_exchanges, do: [name: ex_name, type: ex_type]) ++ + [[name: exchange_name, type: :direct]]) + end + + test "run: list multiple exchanges", context do + declare_exchange("test_exchange_1", @vhost, :direct) + declare_exchange("test_exchange_2", @vhost, :fanout) + non_default_exchanges = run_command_to_list(@command, [["name", "type"], context[:opts]]) + |> without_default_exchanges + assert_set_equal( + non_default_exchanges, + [[name: "test_exchange_1", type: :direct], + [name: "test_exchange_2", type: :fanout]]) + end + + def assert_set_equal(one, two) do + assert MapSet.new(one) == MapSet.new(two) + end + + test "run: info keys filter single key", context do + declare_exchange("test_exchange_1", @vhost) + declare_exchange("test_exchange_2", @vhost) + non_default_exchanges = run_command_to_list(@command, [["name"], context[:opts]]) + |> without_default_exchanges + assert_set_equal( + non_default_exchanges, + [[name: "test_exchange_1"], + [name: "test_exchange_2"]]) + end + + + test "run: info keys add additional keys", context do + declare_exchange("durable_exchange", @vhost, :direct, true) + declare_exchange("auto_delete_exchange", @vhost, :fanout, false, true) + non_default_exchanges = run_command_to_list(@command, [["name", "type", "durable", "auto_delete"], context[:opts]]) + |> without_default_exchanges + assert_set_equal( + non_default_exchanges, + [[name: "auto_delete_exchange", type: :fanout, durable: false, auto_delete: true], + [name: "durable_exchange", type: :direct, durable: true, auto_delete: false]]) + end + + test "run: specifying a vhost returns the targeted vhost exchanges", context do + other_vhost = "other_vhost" + add_vhost other_vhost + on_exit(fn -> + delete_vhost other_vhost + end) + declare_exchange("test_exchange_1", @vhost) + declare_exchange("test_exchange_2", other_vhost) + non_default_exchanges1 = run_command_to_list(@command, [["name"], context[:opts]]) + |> without_default_exchanges + + non_default_exchanges2 = run_command_to_list(@command, [["name"], %{context[:opts] | :vhost => other_vhost}]) + |> without_default_exchanges + + assert non_default_exchanges1 == [[name: "test_exchange_1"]] + assert non_default_exchanges2 == [[name: "test_exchange_2"]] + end + + defp without_default_exchanges(xs) do + Enum.filter(xs, + fn(x) -> + not Enum.member?(default_exchange_names(), x[:name]) + end) + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/list_feature_flags_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_feature_flags_command_test.exs new file mode 100644 index 0000000000..b2cf1ad52a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_feature_flags_command_test.exs @@ -0,0 +1,122 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListFeatureFlagsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListFeatureFlagsCommand + + @flag1 :ff1_from_list_ff_testsuite + @flag2 :ff2_from_list_ff_testsuite + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + # Define an arbitrary feature flag for the test. + node = get_rabbit_hostname() + new_feature_flags = %{ + @flag1 => + %{desc: "My feature flag #1", + provided_by: :ListFeatureFlagsCommandTest, + stability: :stable}, + @flag2 => + %{desc: "My feature flag #2", + provided_by: :ListFeatureFlagsCommandTest, + stability: :stable}} + :ok = :rabbit_misc.rpc_call( + node, :rabbit_feature_flags, :initialize_registry, [new_feature_flags]) + :ok = :rabbit_misc.rpc_call( + node, :rabbit_feature_flags, :enable_all, []) + + name_result = [ + [{:name, @flag1}], + [{:name, @flag2}] + ] + + full_result = [ + [{:name, @flag1}, {:state, :enabled}], + [{:name, @flag2}, {:state, :enabled}] + ] + + { + :ok, + name_result: name_result, + full_result: full_result + } + end + + setup context do + { + :ok, + opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]} + } + end + + test "merge_defaults with no command, print just use the names" do + assert match?({["name", "state"], %{}}, @command.merge_defaults([], %{})) + end + + test "validate: return bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "name"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["name", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["name", "oink", "state"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + test "run: on a bad RabbitMQ node, return a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run(["name"], opts)) + end + + @tag test_timeout: :infinity + test "run: with the name tag, print just the names", context do + matches_found = @command.run(["name"], context[:opts]) + assert Enum.all?(context[:name_result], fn(feature_name) -> + Enum.find(matches_found, fn(found) -> found == feature_name end) + end) + end + + @tag test_timeout: :infinity + test "run: duplicate args do not produce duplicate entries", context do + # checks to ensure that all expected feature flags are in the results + matches_found = @command.run(["name", "name"], context[:opts]) + assert Enum.all?(context[:name_result], fn(feature_name) -> + Enum.find(matches_found, fn(found) -> found == feature_name end) + end) + end + + @tag test_timeout: 30000 + test "run: sufficiently long timeouts don't interfere with results", context do + matches_found = @command.run(["name", "state"], context[:opts]) + assert Enum.all?(context[:full_result], fn(feature_name) -> + Enum.find(matches_found, fn(found) -> found == feature_name end) + end) + end + + @tag test_timeout: 0, username: "guest" + test "run: timeout causes command to return a bad RPC", context do + assert @command.run(["name", "state"], context[:opts]) == + {:badrpc, :timeout} + end + + @tag test_timeout: :infinity + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Listing feature flags \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_global_parameters_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_global_parameters_command_test.exs new file mode 100644 index 0000000000..eabd6a3628 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_global_parameters_command_test.exs @@ -0,0 +1,86 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListGlobalParametersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListGlobalParametersCommand + + @key :mqtt_default_vhosts + @value "{\"O=client,CN=dummy\":\"somevhost\"}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + on_exit(fn -> + clear_global_parameter context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: (context[:timeout] || :infinity), + } + } + end + + test "validate: wrong number of arguments leads to an arg count error" do + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag key: @key, value: @value + test "run: a well-formed command returns list of global parameters", context do + set_global_parameter(context[:key], @value) + @command.run([], context[:opts]) + |> assert_parameter_list(context) + end + + @tag key: @key, value: @value + test "run: zero timeout return badrpc", context do + set_global_parameter(context[:key], @value) + assert @command.run([], Map.put(context[:opts], :timeout, 0)) == {:badrpc, :timeout} + end + + test "run: multiple parameters returned in list", context do + initial = for param <- @command.run([], context[:opts]), do: Map.new(param) + parameters = [ + %{name: :global_param_1, value: "{\"key1\":\"value1\"}"}, + %{name: :global_param_2, value: "{\"key2\":\"value2\"}"} + ] + + + Enum.each(parameters, fn(%{name: name, value: value}) -> + set_global_parameter(name, value) + on_exit(fn -> + clear_global_parameter(name) + end) + end) + + parameters = initial ++ parameters + params = for param <- @command.run([], context[:opts]), do: Map.new(param) + + assert MapSet.new(params) == MapSet.new(parameters) + end + + @tag key: @key, value: @value + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Listing global runtime parameters \.\.\./ + end + + # Checks each element of the first parameter against the expected context values + defp assert_parameter_list(params, context) do + [param | _] = params + assert MapSet.new(param) == MapSet.new([name: context[:key], + value: context[:value]]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_hashes_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_hashes_command_test.exs new file mode 100644 index 0000000000..2869479a8a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_hashes_command_test.exs @@ -0,0 +1,29 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListHashesCommandTest do + use ExUnit.Case + @command RabbitMQ.CLI.Ctl.Commands.ListHashesCommand + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + test "run: lists hashes", _context do + assert match?( + {:ok, _}, + @command.run([], %{}) + ) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_operator_policies_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_operator_policies_command_test.exs new file mode 100644 index 0000000000..6c86fe8441 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_operator_policies_command_test.exs @@ -0,0 +1,142 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListOperatorPoliciesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListOperatorPoliciesCommand + + @vhost "test1" + @root "/" + @key "message-expiry" + @pattern "^queue\." + @value "{\"message-ttl\":10}" + @apply_to "all" + @default_options %{vhost: "/", table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit(fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + on_exit(fn -> + clear_operator_policy context[:vhost], context[:key] + end) + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: (context[:timeout] || :infinity), + vhost: context[:vhost], + apply_to: @apply_to, + priority: 0 + } + } + end + + test "merge_defaults: default vhost is '/'" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default", + table_headers: true}} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "run: a well-formed, host-specific command returns list of policies", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + set_operator_policy(context[:vhost], context[:key], context[:pattern], @value) + @command.run([], vhost_opts) + |> assert_operator_policy_list(context) + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @root + test "run: a well-formed command with no vhost runs against the default one", context do + + set_operator_policy("/", context[:key], context[:pattern], @value) + on_exit(fn -> + clear_operator_policy("/", context[:key]) + end) + + @command.run([], context[:opts]) + |> assert_operator_policy_list(context) + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "run: providing a timeout of 0 returns a badrpc", context do + set_operator_policy(context[:vhost], context[:key], context[:pattern], @value) + assert @command.run([], Map.put(context[:opts], :timeout, 0)) == {:badrpc, :timeout} + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: "bad-vhost" + test "run: providing a non-existent vhost returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag vhost: @vhost + test "run: when multiple policies exist in the vhost, returns them all", context do + policies = [ + %{vhost: @vhost, name: "some-policy", pattern: "foo", definition: "{\"message-ttl\":10}", 'apply-to': "all", priority: 0}, + %{vhost: @vhost, name: "other-policy", pattern: "bar", definition: "{\"expires\":20}", 'apply-to': "all", priority: 0} + ] + policies + |> Enum.map( + fn(%{name: name, pattern: pattern, definition: value}) -> + set_operator_policy(context[:vhost], name, pattern, value) + on_exit(fn -> + clear_operator_policy(context[:vhost], name) + end) + end) + + pols = for policy <- @command.run([], context[:opts]), do: Map.new(policy) + + assert MapSet.new(pols) == MapSet.new(policies) + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([], vhost_opts) + =~ ~r/Listing operator policy overrides for vhost \"#{context[:vhost]}\" \.\.\./ + end + + # Checks each element of the first policy against the expected context values + defp assert_operator_policy_list(policies, context) do + [policy] = policies + assert MapSet.new(policy) == MapSet.new([name: context[:key], + pattern: context[:pattern], + definition: context[:value], + vhost: context[:vhost], + priority: context[:opts][:priority], + "apply-to": context[:opts][:apply_to]]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_parameters_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_parameters_command_test.exs new file mode 100644 index 0000000000..f42e55353a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_parameters_command_test.exs @@ -0,0 +1,154 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListParametersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListParametersCommand + + @vhost "test1" + @root "/" + @component_name "federation-upstream" + @key "reconnect-delay" + @value "{\"uri\":\"amqp://\"}" + @default_options %{vhost: "/", table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + + {:ok, [enabled_plugins]} = :file.consult(plugins_file) + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home} + + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], :online, node, opts) + + add_vhost @vhost + + enable_federation_plugin() + + on_exit(fn -> + set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(), opts) + delete_vhost @vhost + end) + + :ok + end + + setup context do + on_exit(fn -> + clear_parameter context[:vhost], context[:component_name], context[:key] + end) + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: (context[:timeout] || :infinity), + vhost: context[:vhost] + } + } + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default", + table_headers: true}} + end + + test "validate: wrong number of arguments leads to an arg count error" do + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @vhost + test "run: a well-formed, host-specific command returns list of parameters", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + set_parameter(context[:vhost], context[:component_name], context[:key], @value) + @command.run([], vhost_opts) + |> assert_parameter_list(context) + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @root + test "run: a well-formed command with no vhost runs against the default", context do + + set_parameter("/", context[:component_name], context[:key], @value) + on_exit(fn -> + clear_parameter("/", context[:component_name], context[:key]) + end) + + @command.run([], context[:opts]) + |> assert_parameter_list(context) + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @vhost + test "run: zero timeout return badrpc", context do + set_parameter(context[:vhost], context[:component_name], context[:key], @value) + assert @command.run([], Map.put(context[:opts], :timeout, 0)) == {:badrpc, :timeout} + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: "bad-vhost" + test "run: an invalid vhost returns a no-such-vhost error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag vhost: @vhost + test "run: multiple parameters returned in list", context do + parameters = [ + %{component: "federation-upstream", name: "my-upstream", value: "{\"uri\":\"amqp://\"}"}, + %{component: "exchange-delete-in-progress", name: "my-key", value: "{\"foo\":\"bar\"}"} + ] + parameters + |> Enum.map( + fn(%{component: component, name: name, value: value}) -> + set_parameter(context[:vhost], component, name, value) + on_exit(fn -> + clear_parameter(context[:vhost], component, name) + end) + end) + + params = for param <- @command.run([], context[:opts]), do: Map.new(param) + + assert MapSet.new(params) == MapSet.new(parameters) + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([], vhost_opts) + =~ ~r/Listing runtime parameters for vhost \"#{context[:vhost]}\" \.\.\./ + end + + # Checks each element of the first parameter against the expected context values + defp assert_parameter_list(params, context) do + [param] = params + assert MapSet.new(param) == MapSet.new([component: context[:component_name], + name: context[:key], + value: context[:value]]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_permissions_command_test.exs new file mode 100644 index 0000000000..eda8f001af --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_permissions_command_test.exs @@ -0,0 +1,92 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListPermissionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListPermissionsCommand + + @vhost "test1" + @user "guest" + @root "/" + @default_timeout :infinity + @default_options %{vhost: "/", table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + set_permissions @user, @vhost, ["^guest-.*", ".*", ".*"] + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout], + vhost: "/" + } + } + end + + test "merge_defaults adds default options" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default", + table_headers: true}} + end + + test "validate: invalid parameters yield an arg count error" do + assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: on a bad RabbitMQ node, return a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag test_timeout: @default_timeout, vhost: @vhost + test "run: specifying a vhost returns the targeted vhost permissions", context do + assert @command.run( + [], + Map.merge(context[:opts], %{vhost: @vhost}) + ) == [[user: "guest", configure: "^guest-.*", write: ".*", read: ".*"]] + end + + @tag test_timeout: 30000 + test "run: sufficiently long timeouts don't interfere with results", context do + results = @command.run([], context[:opts]) + Enum.all?([[user: "guest", configure: ".*", write: ".*", read: ".*"]], fn(perm) -> + Enum.find(results, fn(found) -> found == perm end) + end) + end + + @tag test_timeout: 0 + test "run: timeout causes command to return a bad RPC", context do + assert @command.run([], context[:opts]) == + {:badrpc, :timeout} + end + + @tag vhost: @root + test "banner", context do + ctx = Map.merge(context[:opts], %{vhost: @vhost}) + assert @command.banner([], ctx ) + =~ ~r/Listing permissions for vhost \"#{Regex.escape(ctx[:vhost])}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_policies_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_policies_command_test.exs new file mode 100644 index 0000000000..49ef6ee856 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_policies_command_test.exs @@ -0,0 +1,144 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListPoliciesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListPoliciesCommand + + @vhost "test1" + @default_vhost "/" + @key "federate" + @pattern "^fed\." + @value "{\"federation-upstream-set\":\"all\"}" + @apply_to "all" + @default_options %{vhost: "/", table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + enable_federation_plugin() + + on_exit(fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + + on_exit(fn -> + clear_policy context[:vhost], context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: (context[:timeout] || :infinity), + vhost: context[:vhost], + apply_to: @apply_to, + priority: 0 + } + } + end + + test "merge_defaults: default vhost is '/'" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default", + table_headers: true}} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "run: a well-formed, host-specific command returns list of policies", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + set_policy(context[:vhost], context[:key], context[:pattern], @value) + @command.run([], vhost_opts) + |> assert_policy_list(context) + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @default_vhost + test "run: a well-formed command with no vhost runs against the default one", context do + set_policy("/", context[:key], context[:pattern], @value) + on_exit(fn -> + clear_policy("/", context[:key]) + end) + + @command.run([], context[:opts]) + |> assert_policy_list(context) + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "run: providing a timeout of 0 returns a badrpc", context do + set_policy(context[:vhost], context[:key], context[:pattern], @value) + assert @command.run([], Map.put(context[:opts], :timeout, 0)) == {:badrpc, :timeout} + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: "bad-vhost" + test "run: providing a non-existent vhost returns an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag vhost: @vhost + test "run: when multiple policies exist in the vhost, returns them all", context do + policies = [ + %{vhost: @vhost, name: "some-policy", pattern: "foo", definition: "{\"federation-upstream-set\":\"all\"}", 'apply-to': "all", priority: 0}, + %{vhost: @vhost, name: "other-policy", pattern: "bar", definition: "{\"ha-mode\":\"all\"}", 'apply-to': "all", priority: 0} + ] + policies + |> Enum.map( + fn(%{name: name, pattern: pattern, definition: value}) -> + set_policy(context[:vhost], name, pattern, value) + on_exit(fn -> + clear_policy(context[:vhost], name) + end) + end) + + pols = for policy <- @command.run([], context[:opts]), do: Map.new(policy) + + assert MapSet.new(pols) == MapSet.new(policies) + end + + @tag key: @key, pattern: @pattern, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([], vhost_opts) + =~ ~r/Listing policies for vhost \"#{context[:vhost]}\" \.\.\./ + end + + # Checks each element of the first policy against the expected context values + defp assert_policy_list(policies, context) do + [policy | _] = policies + assert MapSet.new(policy) == MapSet.new([name: context[:key], + pattern: context[:pattern], + definition: context[:value], + vhost: context[:vhost], + priority: context[:opts][:priority], + "apply-to": context[:opts][:apply_to]]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_queues_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_queues_command_test.exs new file mode 100644 index 0000000000..a6635c7933 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_queues_command_test.exs @@ -0,0 +1,145 @@ +defmodule ListQueuesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListQueuesCommand + + @vhost "test1" + @user "guest" + @default_timeout 15000 + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + reset_vm_memory_high_watermark() + delete_all_queues() + close_all_connections(get_rabbit_hostname()) + + on_exit([], fn -> + delete_all_queues() + close_all_connections(get_rabbit_hostname()) + end) + + :ok + end + + setup context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(fn -> + delete_vhost @vhost + end) + { + :ok, + opts: %{ + quiet: true, + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout, + vhost: @vhost, + offline: false, + online: false, + local: false + } + } + end + + test "merge_defaults: no info keys returns names and message count" do + assert match?({["name", "messages"], _}, @command.merge_defaults([], %{})) + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "messages"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["name", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["name", "oink", "messages"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["name"], context[:opts]]) == + [{:badrpc, {:timeout, 0.0, "Some queue(s) are unresponsive, use list_unresponsive_queues command."}}] + end + + @tag test_timeout: 1 + test "run: command timeout (several thousands queues in 1ms) return badrpc with timeout value in seconds", context do + # we assume it will take longer than 1 ms to list thousands of queues + n = 5000 + for i <- 1..n do + declare_queue("test_queue_" <> Integer.to_string(i), @vhost) + end + assert run_command_to_list(@command, [["name"], context[:opts]]) == + [{:badrpc, {:timeout, 0.001, "Some queue(s) are unresponsive, use list_unresponsive_queues command."}}] + for i <- 1..n do + delete_queue("test_queue_" <> Integer.to_string(i), @vhost) + end + end + + @tag test_timeout: 5000 + test "run: return multiple queues", context do + declare_queue("test_queue_1", @vhost) + publish_messages(@vhost, "test_queue_1", 3) + declare_queue("test_queue_2", @vhost) + publish_messages(@vhost, "test_queue_2", 1) + assert Keyword.equal?(run_command_to_list(@command, [["name", "messages"], context[:opts]]), + [[name: "test_queue_1", messages: 3], + [name: "test_queue_2", messages: 1]]) + end + + @tag test_timeout: 5000 + test "run: info keys filter single key", context do + declare_queue("test_queue_1", @vhost) + declare_queue("test_queue_2", @vhost) + assert Keyword.equal?(run_command_to_list(@command, [["name"], context[:opts]]), + [[name: "test_queue_1"], + [name: "test_queue_2"]]) + end + + @tag test_timeout: 5000 + test "run: info keys add additional keys", context do + declare_queue("durable_queue", @vhost, true) + publish_messages(@vhost, "durable_queue", 3) + declare_queue("auto_delete_queue", @vhost, false, true) + publish_messages(@vhost, "auto_delete_queue", 1) + assert Keyword.equal?( + run_command_to_list(@command, [["name", "messages", "durable", "auto_delete"], context[:opts]]), + [[name: "durable_queue", messages: 3, durable: true, auto_delete: false], + [name: "auto_delete_queue", messages: 1, durable: false, auto_delete: true]]) + end + + @tag test_timeout: 5000 + test "run: info keys order is preserved", context do + declare_queue("durable_queue", @vhost, true) + publish_messages(@vhost, "durable_queue", 3) + declare_queue("auto_delete_queue", @vhost, false, true) + publish_messages(@vhost, "auto_delete_queue", 1) + assert Keyword.equal?( + run_command_to_list(@command, [["messages", "durable", "name", "auto_delete"], context[:opts]]), + [[messages: 3, durable: true, name: "durable_queue", auto_delete: false], + [messages: 1, durable: false, name: "auto_delete_queue", auto_delete: true]]) + end + + @tag test_timeout: 5000 + test "run: specifying a vhost returns the targeted vhost queues", context do + other_vhost = "other_vhost" + add_vhost other_vhost + on_exit(fn -> + delete_vhost other_vhost + end) + declare_queue("test_queue_1", @vhost) + declare_queue("test_queue_2", other_vhost) + assert run_command_to_list(@command, [["name"], context[:opts]]) == [[name: "test_queue_1"]] + assert run_command_to_list(@command, [["name"], %{context[:opts] | :vhost => other_vhost}]) == [[name: "test_queue_2"]] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_topic_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_topic_permissions_command_test.exs new file mode 100644 index 0000000000..8de1f2536a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_topic_permissions_command_test.exs @@ -0,0 +1,85 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListTopicPermissionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListTopicPermissionsCommand + + @vhost "test1" + @user "user1" + @password "password" + @root "/" + @default_timeout :infinity + @default_options %{vhost: "/", table_headers: true} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost(@vhost) + add_user(@user, @password) + set_topic_permissions(@user, @vhost, "amq.topic", "^a", "^b") + set_topic_permissions(@user, @vhost, "topic1", "^a", "^b") + + on_exit([], fn -> + clear_topic_permissions(@user, @vhost) + delete_user(@user) + delete_vhost @vhost + end) + + :ok + end + + setup context do + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout], + vhost: "/" + } + } + end + + test "merge_defaults adds default vhost" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], @default_options} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default", + table_headers: true}} + end + + test "validate: does not expect any parameter" do + assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag test_timeout: @default_timeout, vhost: @vhost + test "run: specifying a vhost returns the topic permissions for the targeted vhost", context do + permissions = @command.run([], Map.merge(context[:opts], %{vhost: @vhost})) + assert Enum.count(permissions) == 2 + assert Enum.sort(permissions) == [ + [user: @user, exchange: "amq.topic", write: "^a", read: "^b"], + [user: @user, exchange: "topic1", write: "^a", read: "^b"] + ] + end + + @tag vhost: @root + test "banner", context do + ctx = Map.merge(context[:opts], %{vhost: @vhost}) + assert @command.banner([], ctx ) + =~ ~r/Listing topic permissions for vhost \"#{Regex.escape(ctx[:vhost])}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_user_limits_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_user_limits_command_test.exs new file mode 100644 index 0000000000..7b0370f940 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_user_limits_command_test.exs @@ -0,0 +1,103 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListUserLimitsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListUserLimitsCommand + + @user "guest" + @user1 "test_user1" + @password1 "password1" + @connection_limit_defn "{\"max-connections\":100}" + @channel_limit_defn "{\"max-channels\":1000}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + user = context[:user] || @user + + clear_user_limits(user) + + on_exit(context, fn -> + clear_user_limits(user) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + global: true + }, + user: user + } + end + + test "merge_defaults: does not change defined user" do + assert match?({[], %{user: "test_user"}}, @command.merge_defaults([], %{user: "test_user"})) + end + + test "validate: providing arguments fails validation" do + assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: a well-formed command returns an empty list if there are no limits", context do + assert @command.run([], context[:opts]) == [] + end + + test "run: a well-formed user specific command returns an empty json object if there are no limits" do + assert @command.run([], %{node: get_rabbit_hostname(), + user: @user}) == "{}" + end + + test "run: list limits for all users", context do + add_user(@user1, @password1) + on_exit(fn() -> + delete_user(@user1) + end) + set_user_limits(@user, @connection_limit_defn) + set_user_limits(@user1, @channel_limit_defn) + + assert Enum.sort(@command.run([], context[:opts])) == + Enum.sort([[user: @user, limits: @connection_limit_defn], + [user: @user1, limits: @channel_limit_defn]]) + end + + test "run: list limits for a single user", context do + user_opts = Map.put(context[:opts], :user, @user) + set_user_limits(@user, @connection_limit_defn) + + assert @command.run([], user_opts) == + [[user: @user, limits: @connection_limit_defn]] + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, user: "guest", timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag user: "user" + test "run: providing a non-existent user reports an error", _context do + s = "non-existent-user" + + assert @command.run([], %{node: get_rabbit_hostname(), + user: s}) == {:error, {:no_such_user, s}} + end + + test "banner", context do + assert @command.banner([], %{user: context[:user]}) + == "Listing limits for user \"#{context[:user]}\" ..." + assert @command.banner([], %{global: true}) + == "Listing limits for all users ..." + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_user_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_user_permissions_command_test.exs new file mode 100644 index 0000000000..ddd44c0e01 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_user_permissions_command_test.exs @@ -0,0 +1,91 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListUserPermissionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListUserPermissionsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + default_result = [ + [ + {:vhost,<<"/">>}, + {:configure,<<".*">>}, + {:write,<<".*">>}, + {:read,<<".*">>} + ] + ] + + no_such_user_result = {:error, {:no_such_user, context[:username]}} + + { + :ok, + opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}, + result: default_result, + no_such_user: no_such_user_result, + timeout: {:badrpc, :timeout} + } + end + +## -------------------------------- Usage ------------------------------------- + + test "validate: wrong number of arguments results in an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["guest", "extra"], %{}) == {:validation_failure, :too_many_args} + end + +## ------------------------------- Username ----------------------------------- + + @tag test_timeout: :infinity, username: "guest" + test "run: valid user returns a list of permissions", context do + results = @command.run([context[:username]], context[:opts]) + assert Enum.all?(context[:result], fn(perm) -> + Enum.find(results, fn(found) -> found == perm end) + end) + end + + @tag test_timeout: :infinity, username: "interloper" + test "run: invalid user returns a no-such-user error", context do + assert @command.run( + [context[:username]], context[:opts]) == context[:no_such_user] + end + +## --------------------------------- Flags ------------------------------------ + + test "run: unreachable RabbitMQ node returns a badrpc" do + assert match?({:badrpc, _}, @command.run(["guest"], %{node: :jake@thedog, timeout: 200})) + end + + @tag test_timeout: 30000, username: "guest" + test "run: long user-defined timeout doesn't interfere with operation", context do + results = @command.run([context[:username]], context[:opts]) + Enum.all?(context[:result], fn(perm) -> + Enum.find(results, fn(found) -> found == perm end) + end) + end + + @tag test_timeout: 0, username: "guest" + test "run: timeout causes command to return a bad RPC", context do + assert @command.run( + [context[:username]], + context[:opts] + ) == context[:timeout] + end + + @tag test_timeout: :infinity + test "banner", context do + assert @command.banner( [context[:username]], context[:opts]) + =~ ~r/Listing permissions for user \"#{context[:username]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_user_topic_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_user_topic_permissions_command_test.exs new file mode 100644 index 0000000000..edf935de77 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_user_topic_permissions_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListUserTopicPermissionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListUserTopicPermissionsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + set_topic_permissions("guest", "/", "amq.topic", "^a", "^b") + set_topic_permissions("guest", "/", "topic1", "^a", "^b") + + on_exit([], fn -> + clear_topic_permissions("guest", "/") + end) + + :ok + end + + setup context do + no_such_user_result = {:error, {:no_such_user, context[:username]}} + + { + :ok, + opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}, + no_such_user: no_such_user_result, + timeout: {:badrpc, :timeout} + } + end + +## -------------------------------- Usage ------------------------------------- + + test "validate: expect username argument" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["guest", "extra"], %{}) == {:validation_failure, :too_many_args} + end + +## ------------------------------- Username ----------------------------------- + + @tag test_timeout: :infinity, username: "guest" + test "run: valid user returns a list of topic permissions", context do + results = @command.run([context[:username]], context[:opts]) + assert Enum.count(results) == 2 + end + + @tag test_timeout: :infinity, username: "interloper" + test "run: invalid user returns a no-such-user error", context do + assert @command.run( + [context[:username]], context[:opts]) == context[:no_such_user] + end + +## --------------------------------- Flags ------------------------------------ + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(["guest"], opts)) + end + + @tag test_timeout: :infinity + test "banner", context do + assert @command.banner( [context[:username]], context[:opts]) + =~ ~r/Listing topic permissions for user \"#{context[:username]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_users_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_users_command_test.exs new file mode 100644 index 0000000000..bcfdb84b2b --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_users_command_test.exs @@ -0,0 +1,74 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListUsersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListUsersCommand + + @user "user1" + @password "password" + @guest "guest" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + std_result = [ + [{:user,@guest},{:tags,[:administrator]}], + [{:user,@user},{:tags,[]}] + ] + + {:ok, std_result: std_result} + end + + setup context do + add_user @user, @password + on_exit([], fn -> delete_user @user end) + + {:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}} + end + + test "validate: On incorrect number of commands, return an arg count error" do + assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} + end + + @tag test_timeout: 15000 + test "run: On a successful query, return an array of lists of tuples", context do + matches_found = @command.run([], context[:opts]) + + assert Enum.all?(context[:std_result], fn(user) -> + Enum.find(matches_found, fn(found) -> found == user end) + end) + end + + test "run: On an invalid rabbitmq node, return a bad rpc" do + assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, timeout: 200})) + end + + @tag test_timeout: 30000 + test "run: sufficiently long timeouts don't interfere with results", context do + # checks to ensure that all expected users are in the results + matches_found = @command.run([], context[:opts]) + + assert Enum.all?(context[:std_result], fn(user) -> + Enum.find(matches_found, fn(found) -> found == user end) + end) + end + + @tag test_timeout: 0 + test "run: timeout causes command to return a bad RPC", context do + assert @command.run([], context[:opts]) == + {:badrpc, :timeout} + end + + @tag test_timeout: :infinity + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Listing users \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_vhost_limits_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_vhost_limits_command_test.exs new file mode 100644 index 0000000000..f07d40672a --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_vhost_limits_command_test.exs @@ -0,0 +1,111 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListVhostLimitsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListVhostLimitsCommand + + @vhost "test_vhost" + @vhost1 "test_vhost1" + @connection_limit_defn "{\"max-connections\":100}" + @queue_limit_defn "{\"max-queues\":1000}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + vhost = context[:vhost] || @vhost + + clear_vhost_limits(vhost) + + on_exit(context, fn -> + clear_vhost_limits(vhost) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + global: true + }, + vhost: vhost + } + end + + test "merge_defaults: does not change defined vhost" do + assert match?({[], %{vhost: "test_vhost"}}, @command.merge_defaults([], %{vhost: "test_vhost"})) + end + + test "validate: providing arguments fails validation" do + assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: a well-formed command returns an empty list if there are no limits", context do + assert @command.run([], context[:opts]) == [] + end + + test "run: a well-formed vhost specific command returns an empty list if there are no limits", context do + vhost_opts = Map.put(context[:opts], :vhost, @vhost) + assert @command.run([], vhost_opts) == [] + end + + test "run: list limits for all vhosts", context do + add_vhost(@vhost1) + on_exit(fn() -> + delete_vhost(@vhost1) + end) + set_vhost_limits(@vhost, @connection_limit_defn) + set_vhost_limits(@vhost1, @queue_limit_defn) + + assert Enum.sort(@command.run([], context[:opts])) == + Enum.sort([[vhost: @vhost, limits: @connection_limit_defn], + [vhost: @vhost1, limits: @queue_limit_defn]]) + end + + test "run: list limits for a single vhost", context do + vhost_opts = Map.put(context[:opts], :vhost, @vhost) + set_vhost_limits(@vhost, @connection_limit_defn) + + assert @command.run([], vhost_opts) == + [[vhost: @vhost, limits: @connection_limit_defn]] + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag vhost: "bad-vhost" + test "run: providing a non-existent vhost reports an error", _context do + s = "non-existent-vhost-a9sd89" + + assert @command.run([], %{node: get_rabbit_hostname(), + vhost: s}) == {:error, {:no_such_vhost, s}} + end + + test "banner", context do + assert @command.banner([], %{vhost: context[:vhost]}) + == "Listing limits for vhost \"#{context[:vhost]}\" ..." + assert @command.banner([], %{global: true}) + == "Listing limits for all vhosts ..." + end +end diff --git a/deps/rabbitmq_cli/test/ctl/list_vhosts_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_vhosts_command_test.exs new file mode 100644 index 0000000000..76f46af422 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/list_vhosts_command_test.exs @@ -0,0 +1,160 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ListVhostsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ListVhostsCommand + + @vhost1 "test1" + @vhost2 "test2" + @root "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost1 + add_vhost @vhost2 + trace_off @root + + on_exit([], fn -> + delete_vhost @vhost1 + delete_vhost @vhost2 + end) + + name_result = [ + [{:name, @vhost1}], + [{:name, @vhost2}], + [{:name, @root}] + ] + + tracing_result = [ + [{:tracing, false}], + [{:tracing, false}], + [{:tracing, false}] + ] + + full_result = [ + [{:name, @vhost1}, {:tracing, false}], + [{:name, @vhost2}, {:tracing, false}], + [{:name, @root}, {:tracing, false}] + ] + + transposed_result = [ + [{:tracing, false}, {:name, @vhost1}], + [{:tracing, false}, {:name, @vhost2}], + [{:tracing, false}, {:name, @root}] + ] + + { + :ok, + name_result: name_result, + tracing_result: tracing_result, + full_result: full_result, + transposed_result: transposed_result + } + end + + setup context do + { + :ok, + opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]} + } + end + + test "merge_defaults with no command, print just use the names" do + assert match?({["name"], %{}}, @command.merge_defaults([], %{})) + end + + test "validate: return bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "tracing"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["name", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["name", "oink", "tracing"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + test "run: on a bad RabbitMQ node, return a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(["name"], opts)) + end + + @tag test_timeout: :infinity + test "run: with the name tag, print just the names", context do + # checks to ensure that all expected vhosts are in the results + matches_found = @command.run(["name"], context[:opts]) + assert Enum.all?(context[:name_result], fn(vhost) -> + Enum.find(matches_found, fn(found) -> found == vhost end) + end) + end + + @tag test_timeout: :infinity + test "run: with the tracing tag, print just say if tracing is on", context do + # checks to ensure that all expected vhosts are in the results + matches_found = @command.run(["tracing"], context[:opts]) + assert Enum.all?(context[:tracing_result], fn(vhost) -> + Enum.find(matches_found, fn(found) -> found == vhost end) + end) + end + + @tag test_timeout: :infinity + test "run: with name and tracing keys, print both", context do + # checks to ensure that all expected vhosts are in the results + matches_found = @command.run(["name", "tracing"], context[:opts]) + assert Enum.all?(context[:full_result], fn(vhost) -> + Enum.find(matches_found, fn(found) -> found == vhost end) + end) + + # checks to ensure that all expected vhosts are in the results + matches_found = @command.run(["tracing", "name"], context[:opts]) + assert Enum.all?(context[:transposed_result], fn(vhost) -> + Enum.find(matches_found, fn(found) -> found == vhost end) + end) + end + + @tag test_timeout: :infinity + test "run: duplicate args do not produce duplicate entries", context do + # checks to ensure that all expected vhosts are in the results + matches_found = @command.run(["name", "name"], context[:opts]) + assert Enum.all?(context[:name_result], fn(vhost) -> + Enum.find(matches_found, fn(found) -> found == vhost end) + end) + end + + @tag test_timeout: 30000 + test "run: sufficiently long timeouts don't interfere with results", context do + # checks to ensure that all expected vhosts are in the results + matches_found = @command.run(["name", "tracing"], context[:opts]) + assert Enum.all?(context[:full_result], fn(vhost) -> + Enum.find(matches_found, fn(found) -> found == vhost end) + end) + end + + @tag test_timeout: 0, username: "guest" + test "run: timeout causes command to return a bad RPC", context do + assert @command.run(["name", "tracing"], context[:opts]) == + {:badrpc, :timeout} + end + + @tag test_timeout: :infinity + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Listing vhosts \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/node_health_check_command_test.exs b/deps/rabbitmq_cli/test/ctl/node_health_check_command_test.exs new file mode 100644 index 0000000000..12ff786bfb --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/node_health_check_command_test.exs @@ -0,0 +1,65 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule NodeHealthCheckCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.NodeHealthCheckCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + reset_vm_memory_high_watermark() + + on_exit([], fn -> + reset_vm_memory_high_watermark() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 20000}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "validate: with no arguments succeeds", _context do + assert @command.validate([], []) == :ok + end + + test "validate: with a named, active node argument succeeds", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "run: request to a named, active node succeeds", context do + assert @command.run([], context[:opts]) + end + + test "run: request to a named, active node with an alarm in effect fails", context do + set_vm_memory_high_watermark(0.0000000000001) + # give VM memory monitor check some time to kick in + :timer.sleep(1500) + {:healthcheck_failed, _message} = @command.run([], context[:opts]) + + reset_vm_memory_high_watermark() + :timer.sleep(1500) + assert @command.run([], context[:opts]) == :ok + end + + test "run: request to a non-existent node returns a badrpc" do + assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, timeout: 200})) + end + + test "banner", context do + assert @command.banner([], context[:opts]) |> Enum.join("\n") =~ ~r/Checking health/ + assert @command.banner([], context[:opts]) |> Enum.join("\n") =~ ~r/#{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/ping_command_test.exs b/deps/rabbitmq_cli/test/ctl/ping_command_test.exs new file mode 100644 index 0000000000..347013a4a8 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/ping_command_test.exs @@ -0,0 +1,56 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule PingCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.PingCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + reset_vm_memory_high_watermark() + + on_exit([], fn -> + reset_vm_memory_high_watermark() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 200}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "validate: with no arguments succeeds", _context do + assert @command.validate([], []) == :ok + end + + test "validate: with a named, active node argument succeeds", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "run: request to a named, active node succeeds", context do + assert @command.run([], context[:opts]) + end + + test "run: request to a non-existent node returns a badrpc" do + assert match?({:error, _}, @command.run([], %{node: :jake@thedog, timeout: 200})) + end + + test "banner", context do + banner = @command.banner([], context[:opts]) + + assert banner =~ ~r/Will ping/ + assert banner =~ ~r/#{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/purge_queue_command_test.exs b/deps/rabbitmq_cli/test/ctl/purge_queue_command_test.exs new file mode 100644 index 0000000000..9891175f15 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/purge_queue_command_test.exs @@ -0,0 +1,88 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule PurgeQueueCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.PurgeQueueCommand + @user "guest" + @vhost "purge-queue-vhost" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + vhost: @vhost, + timeout: context[:test_timeout] + }} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + @tag test_timeout: 30000 + test "request to an existent queue on active node succeeds", context do + add_vhost @vhost + set_permissions @user, @vhost, [".*", ".*", ".*"] + on_exit(context, fn -> delete_vhost(@vhost) end) + + q = "foo" + n = 20 + + declare_queue(q, @vhost) + assert message_count(@vhost, q) == 0 + + publish_messages(@vhost, q, n) + assert message_count(@vhost, q) == n + + assert @command.run([q], context[:opts]) == :ok + assert message_count(@vhost, q) == 0 + end + + @tag test_timeout: 30000 + test "request to a non-existent queue on active node returns not found", context do + assert @command.run(["non-existent"], context[:opts]) == {:error, :not_found} + end + + @tag test_timeout: 0 + test "run: timeout causes command to return a bad RPC", context do + assert @command.run(["foo"], context[:opts]) == {:badrpc, :timeout} + end + + test "shows up in help" do + s = @command.usage() + assert s =~ ~r/purge_queue/ + end + + test "defaults to vhost /" do + assert @command.merge_defaults(["foo"], %{bar: "baz"}) == {["foo"], %{bar: "baz", vhost: "/"}} + end + + test "validate: with extra arguments returns an arg count error" do + assert @command.validate(["queue-name", "extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: with no arguments returns an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: with correct args returns ok" do + assert @command.validate(["q"], %{}) == :ok + end + + test "banner informs that vhost's queue is purged" do + assert @command.banner(["my-q"], %{vhost: "/foo"}) == "Purging queue 'my-q' in vhost '/foo' ..." + end +end diff --git a/deps/rabbitmq_cli/test/ctl/rename_cluster_node_command_test.exs b/deps/rabbitmq_cli/test/ctl/rename_cluster_node_command_test.exs new file mode 100644 index 0000000000..02bf2ad795 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/rename_cluster_node_command_test.exs @@ -0,0 +1,102 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RenameClusterNodeCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.RenameClusterNodeCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + start_rabbitmq_app() + + {:ok, plugins_dir} = + :rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir]) + + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + mnesia_dir = :rabbit_misc.rpc_call(node, :rabbit_mnesia, :dir, []) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + {:ok, opts: %{rabbitmq_home: rabbitmq_home, plugins_dir: plugins_dir, mnesia_dir: mnesia_dir}} + end + + setup context do + {:ok, + opts: + Map.merge( + context[:opts], + %{node: :not_running@localhost} + )} + end + + test "validate: specifying no nodes fails validation", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate: specifying one node only fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate_execution_environment: specifying an uneven number of arguments fails validation", + context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate_execution_environment(["a", "b", "c"], context[:opts]) + ) + end + + test "validate_execution_environment: request to a running node fails", _context do + node = get_rabbit_hostname() + + assert match?( + {:validation_failure, :node_running}, + @command.validate_execution_environment([to_string(node), "other_node@localhost"], %{ + node: node + }) + ) + end + + test "validate_execution_environment: not providing node mnesia dir fails validation", + context do + opts_without_mnesia = Map.delete(context[:opts], :mnesia_dir) + Application.put_env(:mnesia, :dir, "/tmp") + on_exit(fn -> Application.delete_env(:mnesia, :dir) end) + + assert :ok == + @command.validate( + ["some_node@localhost", "other_node@localhost"], + opts_without_mnesia + ) + + Application.delete_env(:mnesia, :dir) + System.put_env("RABBITMQ_MNESIA_DIR", "/tmp") + on_exit(fn -> System.delete_env("RABBITMQ_MNESIA_DIR") end) + + assert :ok == + @command.validate( + ["some_node@localhost", "other_node@localhost"], + opts_without_mnesia + ) + + System.delete_env("RABBITMQ_MNESIA_DIR") + + assert :ok == + @command.validate(["some_node@localhost", "other_node@localhost"], context[:opts]) + end + + test "banner", context do + assert @command.banner(["a", "b"], context[:opts]) =~ + ~r/Renaming cluster nodes: \n a -> b/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/report_command_test.exs b/deps/rabbitmq_cli/test/ctl/report_command_test.exs new file mode 100644 index 0000000000..f207ab8c2b --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/report_command_test.exs @@ -0,0 +1,44 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ReportTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ReportCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: :infinity}} + end + + test "validate: with extra arguments, status returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: report request to a reachable node succeeds", context do + output = @command.run([], context[:opts]) |> Enum.to_list + + assert_stream_without_errors(output) + end + + test "run: report request on nonexistent RabbitMQ node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Reporting server status of node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/reset_command_test.exs b/deps/rabbitmq_cli/test/ctl/reset_command_test.exs new file mode 100644 index 0000000000..8bded47377 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/reset_command_test.exs @@ -0,0 +1,68 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ResetCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ResetCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: reset request to an active node with a stopped rabbit app succeeds", context do + add_vhost "some_vhost" + #ensure the vhost really does exist + assert vhost_exists? "some_vhost" + stop_rabbitmq_app() + assert :ok == @command.run([], context[:opts]) + start_rabbitmq_app() + #check that the created vhost no longer exists + assert match?([_], list_vhosts()) + end + + test "run: reset request to an active node with a running rabbit app fails", context do + add_vhost "some_vhost" + assert vhost_exists? "some_vhost" + assert match?({:error, :mnesia_unexpectedly_running}, @command.run([], context[:opts])) + assert vhost_exists? "some_vhost" + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Resetting node #{get_rabbit_hostname()}/ + end + + test "output mnesia is running error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, + "Mnesia is still running on node " <> _}, + @command.output({:error, :mnesia_unexpectedly_running}, context[:opts])) + + end +end diff --git a/deps/rabbitmq_cli/test/ctl/restart_vhost_command_test.exs b/deps/rabbitmq_cli/test/ctl/restart_vhost_command_test.exs new file mode 100644 index 0000000000..c8d2fe7c48 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/restart_vhost_command_test.exs @@ -0,0 +1,95 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule RestartVhostCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.RestartVhostCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + @vhost "vhost_to_restart" + @timeout 10000 + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname(), + vhost: @vhost, + timeout: @timeout + }} + end + + test "validate: specifying arguments is reported as an error", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + assert @command.validate(["a", "b"], context[:opts]) == + {:validation_failure, :too_many_args} + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: request to a non-existent node returns a badrpc", _context do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: @timeout} + assert match?( + {:badrpc, _}, + @command.run([], opts)) + end + + test "banner", context do + expected = "Trying to restart vhost '#{@vhost}' on node '#{get_rabbit_hostname()}' ..." + ^expected = @command.banner([], context[:opts]) + end + + test "run: restarting an existing vhost returns already_started", context do + setup_vhosts() + {:error, {:already_started, _}} = @command.run([], context[:opts]) + end + + test "run: restarting an failed vhost returns ok", context do + setup_vhosts() + vhost = context[:opts][:vhost] + node_name = context[:opts][:node] + force_vhost_failure(node_name, vhost) + {:ok, _} = @command.run([], context[:opts]) + {:ok, _} = :rpc.call(node_name, :rabbit_vhost_sup_sup, :get_vhost_sup, [vhost]) + end + + # + # Implementation + # + + defp setup_vhosts do + add_vhost @vhost + # give the vhost a chance to fully start and initialise + :timer.sleep(1000) + on_exit(fn -> + delete_vhost @vhost + end) + end + + defp force_vhost_failure(node_name, vhost) do + case :rpc.call(node_name, :rabbit_vhost_sup_sup, :get_vhost_sup, [vhost]) do + {:ok, sup} -> + case :lists.keyfind(:msg_store_persistent, 1, :supervisor.which_children(sup)) do + {_, pid, _, _} -> + Process.exit(pid, :foo) + :timer.sleep(5000) + force_vhost_failure(node_name, vhost); + false -> + Process.exit(sup, :foo) + :timer.sleep(5000) + force_vhost_failure(node_name, vhost) + end; + {:error, {:vhost_supervisor_not_running, _}} -> + :ok + end + end +end diff --git a/deps/rabbitmq_cli/test/ctl/resume_listeners_command_test.exs b/deps/rabbitmq_cli/test/ctl/resume_listeners_command_test.exs new file mode 100644 index 0000000000..3aad0b355b --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/resume_listeners_command_test.exs @@ -0,0 +1,67 @@ +## 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 https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ResumeListenersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ResumeListenersCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + resume_all_client_listeners() + + node_name = get_rabbit_hostname() + on_exit(fn -> + resume_all_client_listeners() + close_all_connections(node_name) + end) + + {:ok, opts: %{node: node_name, timeout: 30_000}} + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "merge_defaults: merges no defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: accepts no arguments", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "run: resumes all client TCP listeners so new client connects are accepted", context do + suspend_all_client_listeners() + expect_client_connection_failure() + + assert @command.run([], Map.merge(context[:opts], %{timeout: 5_000})) == :ok + + # implies a successful connection + with_channel("/", fn _ -> :ok end) + close_all_connections(get_rabbit_hostname()) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/rotate_logs_command_test.exs b/deps/rabbitmq_cli/test/ctl/rotate_logs_command_test.exs new file mode 100644 index 0000000000..13eed87d43 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/rotate_logs_command_test.exs @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule RotateLogsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.RotateLogsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: request to a named, active node succeeds", context do + assert @command.run([], context[:opts]) == :ok + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Rotating logs for node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_cluster_name_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_cluster_name_command_test.exs new file mode 100644 index 0000000000..a0852522e4 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_cluster_name_command_test.exs @@ -0,0 +1,63 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetClusterNameCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetClusterNameCommand + + setup_all do + :net_kernel.start([:rabbitmqctl, :shortnames]) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "shows up in help" do + s = @command.usage() + assert s =~ ~r/set_cluster_name/ + end + + test "has no defaults" do + assert @command.merge_defaults(["foo"], %{bar: "baz"}) == {["foo"], %{bar: "baz"}} + end + + test "validate: with insufficient number of arguments, return arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: with too many arguments, return arg count error" do + assert @command.validate(["foo", "bar"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: with correct number of arguments, return ok" do + assert @command.validate(["mynewname"], %{}) == :ok + end + + test "run: valid name returns ok", context do + s = get_cluster_name() + assert @command.run(["agoodname"], context[:opts]) == :ok + # restore original name + @command.run([s], context[:opts]) + end + + test "run: An invalid Rabbit node returns a bad rpc message" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(["clustername"], opts)) + end + + test "banner shows that the name is being set" do + s = @command.banner(["annoyyou"], %{}) + assert s == "Setting cluster name to annoyyou ..." + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/set_disk_free_limit_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_disk_free_limit_command_test.exs new file mode 100644 index 0000000000..80f0e1511f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_disk_free_limit_command_test.exs @@ -0,0 +1,173 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetDiskFreeLimitCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetDiskFreeLimitCommand + + @default_limit 1048576 + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + set_disk_free_limit(@default_limit) + + on_exit([], fn -> + set_disk_free_limit(@default_limit) + end) + + end + + setup context do + context[:tag] # silences warnings + on_exit([], fn -> set_disk_free_limit(@default_limit) end) + + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: an invalid number of arguments results in arg count errors" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag limit: "2097152bytes" + test "run: an invalid string input returns a bad arg and does not change the limit", context do + assert @command.validate([context[:limit]], context[:opts]) == + {:validation_failure, :bad_argument} + end + + test "validate: valid fractional inputs return an ok", context do + assert @command.validate( + ["mem_relative", "0.0"], + context[:opts] + ) == :ok + + assert @command.validate( + ["mem_relative", "0.5"], + context[:opts] + ) == :ok + + assert @command.validate( + ["mem_relative", "1.8"], + context[:opts] + ) == :ok + end + + test "validate: a value outside the accepted range returns an error", context do + assert @command.validate( + ["mem_relative", "-1.0"], + context[:opts] + ) == {:validation_failure, :bad_argument} + end + + @tag fraction: "1.3" + test "validate: a valid float string input returns ok", context do + assert @command.validate( + ["mem_relative", context[:fraction]], + context[:opts] + ) == :ok + end + + @tag fraction: "1.3salt" + test "validate: an invalid string input returns a bad argument", context do + assert @command.validate( + ["mem_relative", context[:fraction]], + context[:opts] + ) == {:validation_failure, :bad_argument} + end + +## ------------------------ validate mem_relative command ------------------------------------------- + + test "validate: an invalid number of mem_relative arguments results in an arg count error" do + assert @command.validate(["mem_relative"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["mem_relative", 1.3, "extra"], %{}) == {:validation_failure, :too_many_args} + end + + +## ------------------------ run absolute command ------------------------------------------- + + @tag test_timeout: 3000 + test "run: an invalid node returns a bad rpc" do + args = [@default_limit] + opts = %{node: :jake@thedog} + + assert match?({:badrpc, _}, @command.run(args, opts)) + end + + @tag limit: 2097152 + test "run: a valid integer input returns an ok and sets the disk free limit", context do + assert @command.run([context[:limit]], context[:opts]) == :ok + assert status()[:disk_free_limit] === context[:limit] + end + + @tag limit: 2097152.0 + test "run: a valid non-fractional float input returns an ok and sets the disk free limit", context do + assert @command.run([context[:limit]], context[:opts]) == :ok + assert status()[:disk_free_limit] === round(context[:limit]) + end + + @tag limit: 2097152.9 + test "run: a valid fractional float input returns an ok and sets the disk free limit", context do + assert @command.run([context[:limit]], context[:opts]) == :ok + assert status()[:disk_free_limit] === context[:limit] |> Float.floor |> round + end + + @tag limit: "2097152" + test "run: an integer string input returns an ok and sets the disk free limit", context do + assert @command.run([context[:limit]], context[:opts]) == :ok + assert status()[:disk_free_limit] === String.to_integer(context[:limit]) + end + + @tag limit: "2MB" + test "run: an valid unit string input returns an ok and changes the limit", context do + assert @command.run([context[:limit]], context[:opts]) == :ok + assert status()[:disk_free_limit] === 2000000 + end + +## ------------------------ run relative command ------------------------------------------- + + @tag fraction: 1 + test "run: an integer input returns ok", context do + assert @command.run( + ["mem_relative", context[:fraction]], + context[:opts] + ) == :ok + end + + @tag fraction: 1.1 + test "run: a factional input returns ok", context do + assert @command.run( + ["mem_relative", context[:fraction]], + context[:opts] + ) == :ok + end + + + test "banner: returns absolute message", context do + assert @command.banner(["10"], context[:opts]) + =~ ~r/Setting disk free limit on #{get_rabbit_hostname()} to 10 bytes .../ + + assert @command.banner(["-10"], context[:opts]) + =~ ~r/Setting disk free limit on #{get_rabbit_hostname()} to -10 bytes .../ + + assert @command.banner(["sandwich"], context[:opts]) + =~ ~r/Setting disk free limit on #{get_rabbit_hostname()} to sandwich bytes .../ + end + + test "banner: returns memory-relative message", context do + assert @command.banner(["mem_relative", "1.3"], context[:opts]) + =~ ~r/Setting disk free limit on #{get_rabbit_hostname()} to 1\.3 times the total RAM \.\.\./ + + assert @command.banner(["mem_relative", "-1.3"], context[:opts]) + =~ ~r/Setting disk free limit on #{get_rabbit_hostname()} to -1\.3 times the total RAM \.\.\./ + + assert @command.banner(["mem_relative", "sandwich"], context[:opts]) + =~ ~r/Setting disk free limit on #{get_rabbit_hostname()} to sandwich times the total RAM \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_global_parameter_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_global_parameter_command_test.exs new file mode 100644 index 0000000000..848f29a0b8 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_global_parameter_command_test.exs @@ -0,0 +1,82 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetGlobalParameterCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetGlobalParameterCommand + + @key :mqtt_default_vhosts + @value "{\"O=client,CN=dummy\":\"somevhost\"}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + on_exit(context, fn -> + clear_global_parameter context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + } + } + end + + test "validate: expects a key and a value" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag key: @key, value: @value + test "run: expects a key and a value", context do + assert @command.run( + [context[:key], context[:value]], + context[:opts] + ) == :ok + + assert_parameter_fields(context) + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([@key, @value], opts)) + end + + @tag key: @key, value: "bad-value" + test "run: a value that fails to parse as JSON returns a decoding error", context do + initial = list_global_parameters() + assert match?({:error_string, _}, + @command.run([context[:key], context[:value]], + context[:opts])) + + assert list_global_parameters() == initial + end + + @tag key: @key, value: @value + test "banner", context do + assert @command.banner([context[:key], context[:value]], context[:opts]) + =~ ~r/Setting global runtime parameter \"#{context[:key]}\" to \"#{context[:value]}\" \.\.\./ + end + + # Checks each element of the first parameter against the expected context values + defp assert_parameter_fields(context) do + result_param = list_global_parameters() |> List.first + + assert result_param[:value] == context[:value] + assert result_param[:name] == context[:key] + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/set_log_level_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_log_level_command_test.exs new file mode 100644 index 0000000000..b4108219ba --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_log_level_command_test.exs @@ -0,0 +1,44 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule SetLogLevelCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetLogLevelCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + {:ok, + log_level: "debug", + opts: %{node: get_rabbit_hostname()}} + end + + test "validate: with a single known level succeeds", context do + assert @command.validate([context[:log_level]], context[:opts]) == :ok + end + + test "validate: with a single unsupported level fails", context do + assert match?({:error, _}, @command.validate(["lolwut"], context[:opts])) + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate([context[:log_level], "whoops"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: request to a named, active node succeeds", context do + assert @command.run([context[:log_level]], context[:opts]) == :ok + end + + test "run: request to a non-existent node returns a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([context[:log_level]], opts)) + end + + test "banner", context do + assert @command.banner([context[:log_level]], context[:opts]) == "Setting log level to \"debug\" ..." + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_operator_policy_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_operator_policy_command_test.exs new file mode 100644 index 0000000000..5911132a32 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_operator_policy_command_test.exs @@ -0,0 +1,153 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetOperatorPolicyCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetOperatorPolicyCommand + + @vhost "test1" + @root "/" + @key "message-expiry" + @pattern "^queue\." + @value "{\"message-ttl\":10}" + @apply_to "all" + @priority 0 + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + + on_exit(context, fn -> + clear_operator_policy(context[:vhost], context[:key]) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + vhost: "/", + apply_to: @apply_to, + priority: @priority + } + } + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: @root + test "merge_defaults: a well-formed command with no vhost runs against the default" do + assert match?({_, %{vhost: "/"}}, @command.merge_defaults([], %{})) + end + + test "merge_defaults: does not change defined vhost" do + assert match?({[], %{vhost: "test_vhost"}}, @command.merge_defaults([], %{vhost: "test_vhost"})) + end + + test "merge_defaults: default apply_to is \"all\"" do + assert match?({_, %{apply_to: "all"}}, @command.merge_defaults([], %{})) + assert match?({_, %{apply_to: "custom"}}, @command.merge_defaults([], %{apply_to: "custom"})) + end + + test "merge_defaults: default priority is 0" do + assert match?({_, %{priority: 0}}, @command.merge_defaults([], %{})) + assert match?({_, %{priority: 3}}, @command.merge_defaults([], %{priority: 3})) + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not", "enough"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: @vhost + test "run: a well-formed, host-specific command returns okay", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key], context[:pattern], context[:value]], + vhost_opts + ) == :ok + + assert_operator_policy_fields(context) + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", priority: 0, apply_to: "all", timeout: 200} + + assert match?({:badrpc, _}, @command.run([@key, @pattern, @value], opts)) + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: "bad-vhost" + test "run: providing a non-existent vhost reports an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key], context[:pattern], context[:value]], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag pattern: @pattern, key: @key, value: "bad-value", vhost: @root + test "run: an invalid value returns a JSON decoding error", context do + assert match?({:error_string, _}, + @command.run([context[:key], context[:pattern], context[:value]], + context[:opts])) + + assert list_operator_policies(context[:vhost]) == [] + end + + @tag pattern: @pattern, key: @key, value: "{\"foo\":\"bar\"}", vhost: @root + test "run: invalid policy returns an error", context do + assert @command.run( + [context[:key], context[:pattern], context[:value]], + context[:opts] + ) == {:error_string, 'Validation failed\n\n[{<<"foo">>,<<"bar">>}] are not recognised policy settings\n'} + + assert list_operator_policies(context[:vhost]) == [] + end + + @tag pattern: @pattern, key: @key, value: "{}", vhost: @root + test "run: an empty JSON object value returns an error", context do + assert @command.run( + [context[:key], context[:pattern], context[:value]], + context[:opts] + ) == {:error_string, 'Validation failed\n\nno policy provided\n'} + + assert list_operator_policies(context[:vhost]) == [] + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:key], context[:pattern], context[:value]], vhost_opts) + == "Setting operator policy override \"#{context[:key]}\" for pattern \"#{context[:pattern]}\" to \"#{context[:value]}\" with priority \"#{context[:opts][:priority]}\" for vhost \"#{context[:vhost]}\" \.\.\." + end + + # Checks each element of the first policy against the expected context values + defp assert_operator_policy_fields(context) do + result_policy = context[:vhost] |> list_operator_policies |> List.first + assert result_policy[:definition] == context[:value] + assert result_policy[:vhost] == context[:vhost] + assert result_policy[:pattern] == context[:pattern] + assert result_policy[:name] == context[:key] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_parameter_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_parameter_command_test.exs new file mode 100644 index 0000000000..50a2543dee --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_parameter_command_test.exs @@ -0,0 +1,136 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetParameterCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetParameterCommand + + @vhost "test1" + @root "/" + @component_name "federation-upstream" + @key "reconnect-delay" + @value "{\"uri\":\"amqp://127.0.0.1:5672\"}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + enable_federation_plugin() + + on_exit([], fn -> + delete_vhost @vhost + end) + + # featured in a definitions file imported by other tests + clear_parameter("/", "federation-upstream", "up-1") + + :ok + end + + setup context do + on_exit(context, fn -> + clear_parameter context[:vhost], context[:component_name], context[:key] + end) + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + vhost: context[:vhost] + } + } + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @root + test "merge_defaults: a well-formed command with no vhost runs against the default" do + assert match?({_, %{vhost: "/"}}, @command.merge_defaults([], %{})) + assert match?({_, %{vhost: "non_default"}}, @command.merge_defaults([], %{vhost: "non_default"})) + end + + test "validate: wrong number of arguments leads to an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not", "enough"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @vhost + test "run: a well-formed, host-specific command returns okay", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:component_name], context[:key], context[:value]], + vhost_opts + ) == :ok + + assert_parameter_fields(context) + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run([@component_name, @key, @value], opts)) + end + + @tag component_name: "bad-component-name", key: @key, value: @value, vhost: @root + test "run: an invalid component_name returns a validation failed error", context do + assert @command.run( + [context[:component_name], context[:key], context[:value]], + context[:opts] + ) == {:error_string, 'Validation failed\n\ncomponent #{context[:component_name]} not found\n'} + + assert list_parameters(context[:vhost]) == [] + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: "bad-vhost" + test "run: an invalid vhost returns a no-such-vhost error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:component_name], context[:key], context[:value]], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag component_name: @component_name, key: @key, value: "bad-value", vhost: @root + test "run: an invalid value returns a JSON decoding error", context do + assert match?({:error_string, _}, + @command.run([context[:component_name], context[:key], context[:value]], + context[:opts])) + + assert list_parameters(context[:vhost]) == [] + end + + @tag component_name: @component_name, key: @key, value: "{}", vhost: @root + test "run: an empty JSON object value returns a key \"uri\" not found error", context do + assert @command.run( + [context[:component_name], context[:key], context[:value]], + context[:opts] + ) == {:error_string, 'Validation failed\n\nKey "uri" not found in reconnect-delay\n'} + + assert list_parameters(context[:vhost]) == [] + end + + @tag component_name: @component_name, key: @key, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:component_name], context[:key], context[:value]], vhost_opts) + =~ ~r/Setting runtime parameter \"#{context[:key]}\" for component \"#{context[:component_name]}\" to \"#{context[:value]}\" in vhost \"#{context[:vhost]}\" \.\.\./ + end + + # Checks each element of the first parameter against the expected context values + defp assert_parameter_fields(context) do + result_param = context[:vhost] |> list_parameters |> List.first + + assert result_param[:value] == context[:value] + assert result_param[:component] == context[:component_name] + assert result_param[:name] == context[:key] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_permissions_command_test.exs new file mode 100644 index 0000000000..c2628f2728 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_permissions_command_test.exs @@ -0,0 +1,114 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetPermissionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand + + @vhost "test1" + @user "guest" + @root "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + + on_exit(context, fn -> + set_permissions context[:user], context[:vhost], [".*", ".*", ".*"] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + vhost: context[:vhost] + } + } + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: wrong number of arguments leads to an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not", "enough"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not", "quite", "enough"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this", "is", "way", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag user: @user, vhost: @vhost + test "run: a well-formed, host-specific command returns okay", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:user], "^#{context[:user]}-.*", ".*", ".*"], + vhost_opts + ) == :ok + + u = Enum.find(list_permissions(context[:vhost]), fn(x) -> x[:user] == context[:user] end) + assert u[:configure] == "^#{context[:user]}-.*" + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + + assert match?({:badrpc, _}, @command.run([@user, ".*", ".*", ".*"], opts)) + end + + @tag user: "interloper", vhost: @root + test "run: an invalid user returns a no-such-user error", context do + assert @command.run( + [context[:user], "^#{context[:user]}-.*", ".*", ".*"], + context[:opts] + ) == {:error, {:no_such_user, context[:user]}} + end + + @tag user: @user, vhost: "wintermute" + test "run: an invalid vhost returns a no-such-vhost error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:user], "^#{context[:user]}-.*", ".*", ".*"], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag user: @user, vhost: @root + test "run: invalid regex patterns returns an error", context do + assert @command.run( + [context[:user], "^#{context[:user]}-.*", ".*", "*"], + context[:opts] + ) == {:error, {:invalid_regexp, '*', {'nothing to repeat', 0}}} + + # asserts that the failed command didn't change anything + u = Enum.find(list_permissions(context[:vhost]), fn(x) -> x[:user] == context[:user] end) + assert u == [user: context[:user], configure: ".*", write: ".*", read: ".*"] + end + + @tag user: @user, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:user], "^#{context[:user]}-.*", ".*", ".*"], vhost_opts) + =~ ~r/Setting permissions for user \"#{context[:user]}\" in vhost \"#{context[:vhost]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_policy_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_policy_command_test.exs new file mode 100644 index 0000000000..0422933ecb --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_policy_command_test.exs @@ -0,0 +1,217 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetPolicyCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetPolicyCommand + + @vhost "test1" + @root "/" + @key "federate" + @pattern "^fed\." + @value "{\"federation-upstream-set\":\"all\"}" + @apply_to "all" + @priority 0 + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + enable_federation_plugin() + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + + on_exit(context, fn -> + clear_policy context[:vhost], context[:key] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + vhost: "/", + apply_to: @apply_to, + priority: @priority + } + } + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: @root + test "merge_defaults: a well-formed command with no vhost runs against the default" do + assert match?({_, %{vhost: "/"}}, @command.merge_defaults([], %{})) + end + + test "merge_defaults: does not change defined vhost" do + assert match?({[], %{vhost: "test_vhost"}}, @command.merge_defaults([], %{vhost: "test_vhost"})) + end + + test "merge_defaults: default apply_to is \"all\"" do + assert match?({_, %{apply_to: "all"}}, @command.merge_defaults([], %{})) + assert match?({_, %{apply_to: "custom"}}, @command.merge_defaults([], %{apply_to: "custom"})) + end + + test "merge_defaults: default priority is 0" do + assert match?({_, %{priority: 0}}, @command.merge_defaults([], %{})) + assert match?({_, %{priority: 3}}, @command.merge_defaults([], %{priority: 3})) + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not", "enough"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: @vhost + test "run: a well-formed, host-specific command returns okay", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key], context[:pattern], context[:value]], + vhost_opts + ) == :ok + + assert_policy_fields(context) + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", priority: 0, apply_to: "all", timeout: 200} + + assert match?({:badrpc, _}, @command.run([@key, @pattern, @value], opts)) + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: "bad-vhost" + test "run: providing a non-existent vhost reports an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:key], context[:pattern], context[:value]], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + @tag pattern: @pattern, key: @key, value: "bad-value", vhost: @root + test "run: an invalid value returns a JSON decoding error", context do + assert match?({:error_string, _}, + @command.run([context[:key], context[:pattern], context[:value]], + context[:opts])) + + assert list_policies(context[:vhost]) == [] + end + + @tag pattern: @pattern, key: @key, value: "{\"foo\":\"bar\"}", vhost: @root + test "run: invalid policy returns an error", context do + assert @command.run( + [context[:key], context[:pattern], context[:value]], + context[:opts] + ) == {:error_string, 'Validation failed\n\n[{<<"foo">>,<<"bar">>}] are not recognised policy settings\n'} + + assert list_policies(context[:vhost]) == [] + end + + @tag pattern: @pattern, key: @key, value: "{}", vhost: @root + test "run: an empty JSON object value returns an error", context do + assert @command.run( + [context[:key], context[:pattern], context[:value]], + context[:opts] + ) == {:error_string, 'Validation failed\n\nno policy provided\n'} + + assert list_policies(context[:vhost]) == [] + end + + @tag pattern: @pattern, key: @key, value: @value, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:key], context[:pattern], context[:value]], vhost_opts) + == "Setting policy \"#{context[:key]}\" for pattern \"#{context[:pattern]}\" to \"#{context[:value]}\" with priority \"#{context[:opts][:priority]}\" for vhost \"#{context[:vhost]}\" \.\.\." + end + + @tag pattern: "ha_", key: "ha_policy_test", vhost: @vhost + test "ha policy validation", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + context = Map.put(context, :opts, vhost_opts) + pass_validation(context, "{\"ha-mode\":\"all\"}") + fail_validation(context, "{\"ha-mode\":\"made_up\"}") + + fail_validation(context, "{\"ha-mode\":\"nodes\"}") + fail_validation(context, "{\"ha-mode\":\"nodes\",\"ha-params\":2}") + fail_validation(context, "{\"ha-mode\":\"nodes\",\"ha-params\":[\"a\",2]}") + pass_validation(context, "{\"ha-mode\":\"nodes\",\"ha-params\":[\"a\",\"b\"]}") + fail_validation(context, "{\"ha-params\":[\"a\",\"b\"]}") + + fail_validation(context, "{\"ha-mode\":\"exactly\"}") + fail_validation(context, "{\"ha-mode\":\"exactly\",\"ha-params\":[\"a\",\"b\"]}") + pass_validation(context, "{\"ha-mode\":\"exactly\",\"ha-params\":2}") + fail_validation(context, "{\"ha-params\":2}") + + pass_validation(context, "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}") + pass_validation(context, "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}") + fail_validation(context, "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"made_up\"}") + fail_validation(context, "{\"ha-sync-mode\":\"manual\"}") + fail_validation(context, "{\"ha-sync-mode\":\"automatic\"}") + end + + @tag pattern: "ha_", key: "ha_policy_test", vhost: @vhost + test "queue master locator policy validation", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + context = Map.put(context, :opts, vhost_opts) + pass_validation(context, "{\"queue-master-locator\":\"min-masters\"}") + pass_validation(context, "{\"queue-master-locator\":\"client-local\"}") + pass_validation(context, "{\"queue-master-locator\":\"random\"}") + fail_validation(context, "{\"queue-master-locator\":\"made_up\"}") + end + + @tag pattern: "ha_", key: "ha_policy_test", vhost: @vhost + test "queue modes policy validation", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + context = Map.put(context, :opts, vhost_opts) + pass_validation(context, "{\"queue-mode\":\"lazy\"}") + pass_validation(context, "{\"queue-mode\":\"default\"}") + fail_validation(context, "{\"queue-mode\":\"wrong\"}") + end + + def pass_validation(context, value) do + assert @command.run( + [context[:key], context[:pattern], value], + context[:opts] + ) == :ok + assert_policy_fields(Map.merge(context, %{value: value})) + end + + def fail_validation(context, value) do + result = @command.run( + [context[:key], context[:pattern], value], + context[:opts] + ) + assert {:error_string, _} = result + {:error_string, msg} = result + assert "Validation failed"<>_ = to_string(msg) + end + + # Checks each element of the first policy against the expected context values + defp assert_policy_fields(context) do + result_policy = context[:vhost] |> list_policies |> List.first + assert result_policy[:definition] == context[:value] + assert result_policy[:vhost] == context[:vhost] + assert result_policy[:pattern] == context[:pattern] + assert result_policy[:name] == context[:key] + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_topic_permissions_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_topic_permissions_command_test.exs new file mode 100644 index 0000000000..f117f5a789 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_topic_permissions_command_test.exs @@ -0,0 +1,114 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetTopicPermissionsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetTopicPermissionsCommand + + @vhost "test1" + @user "guest" + @root "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + + on_exit(context, fn -> + clear_topic_permissions context[:user], context[:vhost] + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + vhost: context[:vhost] + } + } + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: expects username, exchange, and pattern arguments" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not", "enough"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["still", "not", "enough"], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["this", "is", "way", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + @tag user: @user, vhost: @vhost + test "run: a well-formed, host-specific command returns okay", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:user], "amq.topic", "^a", "^b"], + vhost_opts + ) == :ok + + assert List.first(list_user_topic_permissions(context[:user]))[:write] == "^a" + assert List.first(list_user_topic_permissions(context[:user]))[:read] == "^b" + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + + assert match?({:badrpc, _}, @command.run([@user, "amq.topic", "^a", "^b"], opts)) + end + + @tag user: "interloper", vhost: @root + test "run: an invalid user returns a no-such-user error", context do + assert @command.run( + [context[:user], "amq.topic", "^a", "^b"], + context[:opts] + ) == {:error, {:no_such_user, context[:user]}} + end + + @tag user: @user, vhost: "wintermute" + test "run: an invalid vhost returns a no-such-vhost error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:user], "amq.topic", "^a", "^b"], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + + assert Enum.count(list_user_topic_permissions(context[:user])) == 0 + end + + @tag user: @user, vhost: @root + test "run: invalid regex patterns return error", context do + n = Enum.count(list_user_topic_permissions(context[:user])) + {:error, {:invalid_regexp, _, _}} = @command.run( + [context[:user], "amq.topic", "[", "^b"], + context[:opts] + ) + assert Enum.count(list_user_topic_permissions(context[:user])) == n + end + + @tag user: @user, vhost: @vhost + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:user], "amq.topic", "^a", "^b"], vhost_opts) + =~ ~r/Setting topic permissions on \"amq.topic\" for user \"#{context[:user]}\" in vhost \"#{context[:vhost]}\" \.\.\./ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_user_limits_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_user_limits_command_test.exs new file mode 100644 index 0000000000..6179267396 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_user_limits_command_test.exs @@ -0,0 +1,137 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule SetUserLimitsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetUserLimitsCommand + + @user "someone" + @password "password" + @conn_definition "{\"max-connections\":100}" + @channel_definition "{\"max-channels\":200}" + @definition "{\"max-connections\":50, \"max-channels\":500}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_user @user, @password + + on_exit([], fn -> + delete_user @user + end) + + :ok + end + + setup context do + user = context[:user] || @user + + clear_user_limits(user) + + on_exit(context, fn -> + clear_user_limits(user) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname() + }, + user: user + } + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["not-enough"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: a well-formed, host-specific command returns okay", context do + assert @command.run( + [context[:user], + @conn_definition], + context[:opts] + ) == :ok + + assert_limits(context, @conn_definition) + clear_user_limits(context[:user]) + + assert @command.run( + [context[:user], + @channel_definition], + context[:opts] + ) == :ok + + assert_limits(context, @channel_definition) + end + + test "run: a well-formed command to set both max-connections and max-channels returns okay", context do + assert @command.run( + [context[:user], + @definition], + context[:opts] + ) == :ok + + assert_limits(context, @definition) + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([@user, @conn_definition], opts)) + end + + @tag user: "non-existent-user" + test "run: providing a non-existent user reports an error", context do + + assert @command.run( + [context[:user], + @conn_definition], + context[:opts] + ) == {:error, {:no_such_user, context[:user]}} + end + + test "run: an invalid definition returns a JSON decoding error", context do + assert match?({:error_string, _}, + @command.run( + [context[:user], + ["this_is_not_json"]], + context[:opts])) + + assert get_user_limits(context[:user]) == %{} + end + + test "run: invalid limit returns an error", context do + assert @command.run( + [context[:user], + "{\"foo\":\"bar\"}"], + context[:opts] + ) == {:error_string, 'Unrecognised terms [{<<"foo">>,<<"bar">>}] in user-limits'} + + assert get_user_limits(context[:user]) == %{} + end + + test "banner", context do + assert @command.banner([context[:user], context[:conn_definition]], context[:opts]) + == "Setting user limits to \"#{context[:conn_definition]}\" for user \"#{context[:user]}\" ..." + end + + # + # Implementation + # + + defp assert_limits(context, definition) do + limits = get_user_limits(context[:user]) + assert {:ok, limits} == JSON.decode(definition) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_user_tags_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_user_tags_command_test.exs new file mode 100644 index 0000000000..cdc51e673f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_user_tags_command_test.exs @@ -0,0 +1,144 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetUserTagsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetUserTagsCommand + + @user "user1" + @password "password" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_user @user, @password + + on_exit([], fn -> + delete_user(@user) + end) + + :ok + end + + setup context do + context[:user] # silences warnings + on_exit([], fn -> set_user_tags(context[:user], []) end) + + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: on an incorrect number of arguments, return an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run([@user, :imperator], opts)) + end + + @tag user: @user, tags: [:imperator] + test "run: on a single optional argument, add a flag to the user", context do + @command.run( + [context[:user] | context[:tags]], + context[:opts] + ) + + result = Enum.find( + list_users(), + fn(record) -> record[:user] == context[:user] end + ) + + assert result[:tags] == context[:tags] + end + + @tag user: "interloper", tags: [:imperator] + test "run: on an invalid user, get a no such user error", context do + assert @command.run( + [context[:user] | context[:tags]], + context[:opts] + ) == {:error, {:no_such_user, context[:user]}} + end + + @tag user: @user, tags: [:imperator, :generalissimo] + test "run: on multiple optional arguments, add all flags to the user", context do + @command.run( + [context[:user] | context[:tags]], + context[:opts] + ) + + result = Enum.find( + list_users(), + fn(record) -> record[:user] == context[:user] end + ) + + assert result[:tags] == context[:tags] + end + + @tag user: @user, tags: [:imperator] + test "run: with no optional arguments, clear user tags", context do + + set_user_tags(context[:user], context[:tags]) + + @command.run([context[:user]], context[:opts]) + + result = Enum.find( + list_users(), + fn(record) -> record[:user] == context[:user] end + ) + + assert result[:tags] == [] + end + + @tag user: @user, tags: [:imperator] + test "run: identical calls are idempotent", context do + + set_user_tags(context[:user], context[:tags]) + + assert @command.run( + [context[:user] | context[:tags]], + context[:opts] + ) == :ok + + result = Enum.find( + list_users(), + fn(record) -> record[:user] == context[:user] end + ) + + assert result[:tags] == context[:tags] + end + + @tag user: @user, old_tags: [:imperator], new_tags: [:generalissimo] + test "run: if different tags exist, overwrite them", context do + + set_user_tags(context[:user], context[:old_tags]) + + assert @command.run( + [context[:user] | context[:new_tags]], + context[:opts] + ) == :ok + + result = Enum.find( + list_users(), + fn(record) -> record[:user] == context[:user] end + ) + + assert result[:tags] == context[:new_tags] + end + + @tag user: @user, tags: ["imperator"] + test "banner", context do + assert @command.banner( + [context[:user] | context[:tags]], + context[:opts] + ) + =~ ~r/Setting tags for user \"#{context[:user]}\" to \[#{context[:tags]}\] \.\.\./ + end + +end diff --git a/deps/rabbitmq_cli/test/ctl/set_vhost_limits_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_vhost_limits_command_test.exs new file mode 100644 index 0000000000..b5c679b02f --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_vhost_limits_command_test.exs @@ -0,0 +1,137 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule SetVhostLimitsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SetVhostLimitsCommand + + @vhost "test1" + @definition "{\"max-connections\":100}" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost @vhost + + on_exit([], fn -> + delete_vhost @vhost + end) + + :ok + end + + setup context do + + vhost = context[:vhost] || @vhost + + clear_vhost_limits(vhost) + + on_exit(context, fn -> + clear_vhost_limits(vhost) + end) + + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + vhost: vhost + }, + definition: context[:definition] || @definition, + vhost: vhost + } + end + + test "merge_defaults: a well-formed command with no vhost runs against the default" do + assert match?({_, %{vhost: "/"}}, @command.merge_defaults([], %{})) + end + + test "merge_defaults: does not change defined vhost" do + assert match?({[], %{vhost: "test_vhost"}}, @command.merge_defaults([], %{vhost: "test_vhost"})) + end + + test "validate: providing too few arguments fails validation" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation" do + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: a well-formed, host-specific command returns okay", context do + assert @command.run( + [context[:definition]], + context[:opts] + ) == :ok + + assert_limits(context) + end + + test "run: an unreachable node throws a badrpc" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run([@definition], opts)) + end + + @tag vhost: "bad-vhost" + test "run: providing a non-existent vhost reports an error", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.run( + [context[:definition]], + vhost_opts + ) == {:error, {:no_such_vhost, context[:vhost]}} + end + + test "run: an invalid definition returns a JSON decoding error", context do + assert match?({:error_string, _}, + @command.run(["bad_value"], context[:opts])) + + assert get_vhost_limits(context[:vhost]) == %{} + end + + test "run: invalid limit returns an error", context do + assert @command.run( + ["{\"foo\":\"bar\"}"], + context[:opts] + ) == {:error_string, 'Validation failed\n\nUnrecognised terms [{<<"foo">>,<<"bar">>}] in limits\n'} + + assert get_vhost_limits(context[:vhost]) == %{} + end + + test "run: an empty JSON object definition unsets all limits for vhost", context do + + assert @command.run( + [@definition], + context[:opts] + ) == :ok + + assert_limits(context) + + assert @command.run( + ["{}"], + context[:opts] + ) == :ok + + assert get_vhost_limits(context[:vhost]) == %{} + end + + test "banner", context do + vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]}) + + assert @command.banner([context[:definition]], vhost_opts) + == "Setting vhost limits to \"#{context[:definition]}\" for vhost \"#{context[:vhost]}\" ..." + end + + defp assert_limits(context) do + limits = get_vhost_limits(context[:vhost]) + assert {:ok, limits} == JSON.decode(context[:definition]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/set_vm_memory_high_watermark_command_test.exs b/deps/rabbitmq_cli/test/ctl/set_vm_memory_high_watermark_command_test.exs new file mode 100644 index 0000000000..bd9719ab40 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/set_vm_memory_high_watermark_command_test.exs @@ -0,0 +1,162 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule SetVmMemoryHighWatermarkCommandTest do + use ExUnit.Case, async: false + import TestHelper + import RabbitMQ.CLI.Core.{Alarms, Memory} + + @command RabbitMQ.CLI.Ctl.Commands.SetVmMemoryHighWatermarkCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + start_rabbitmq_app() + reset_vm_memory_high_watermark() + + on_exit([], fn -> + start_rabbitmq_app() + reset_vm_memory_high_watermark() + end) + + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: a string returns an error", context do + assert @command.validate(["sandwich"], context[:opts]) == {:validation_failure, :bad_argument} + assert @command.validate(["0.4sandwich"], context[:opts]) == {:validation_failure, :bad_argument} + end + + test "validate: valid numerical value returns valid", context do + assert @command.validate(["0.7"], context[:opts]) == :ok + assert @command.validate(["1"], context[:opts]) == :ok + end + + test "run: valid numerical value returns valid", context do + assert @command.run([0.7], context[:opts]) == :ok + assert status()[:vm_memory_high_watermark] == 0.7 + + assert @command.run([1], context[:opts]) == :ok + assert status()[:vm_memory_high_watermark] == 1 + end + + test "validate: validate a valid numerical string value returns valid", context do + assert @command.validate(["0.7"], context[:opts]) == :ok + assert @command.validate(["1"], context[:opts]) == :ok + end + + test "validate: the wrong number of arguments returns an arg count error" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: a negative number returns a bad argument", context do + assert @command.validate(["-0.1"], context[:opts]) == {:validation_failure, {:bad_argument, "The threshold should be a fraction between 0.0 and 1.0"}} + end + + test "validate: a percentage returns a bad argument", context do + assert @command.validate(["40"], context[:opts]) == {:validation_failure, {:bad_argument, "The threshold should be a fraction between 0.0 and 1.0"}} + end + + test "validate: a value greater than 1.0 returns a bad argument", context do + assert @command.validate(["1.1"], context[:opts]) == {:validation_failure, {:bad_argument, "The threshold should be a fraction between 0.0 and 1.0"}} + end + + @tag test_timeout: 3000 + test "run: on an invalid node, return a bad rpc" do + args = [0.7] + opts = %{node: :jake@thedog, timeout: 200} + + assert match?({:badrpc, _}, @command.run(args, opts)) + end + +## ---------------------------- Absolute tests -------------------------------- + + test "validate: an absolute call without an argument returns not enough args" do + assert @command.validate(["absolute"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: an absolute call with too many arguments returns too many args" do + assert @command.validate(["absolute", "too", "many"], %{}) == + {:validation_failure, :too_many_args} + end + + test "validate: a single absolute integer return valid", context do + assert @command.validate(["absolute","10"], context[:opts]) == :ok + end + test "run: a single absolute integer return ok", context do + assert @command.run(["absolute","10"], context[:opts]) == :ok + assert status()[:vm_memory_high_watermark] == {:absolute, memory_unit_absolute(10, "")} + end + + test "validate: a single absolute integer with an invalid memory unit fails ", context do + assert @command.validate(["absolute","10bytes"], context[:opts]) == {:validation_failure, {:bad_argument, "Invalid units."}} + end + + test "validate: a single absolute float with a valid memory unit fails ", context do + assert @command.validate(["absolute","10.0MB"], context[:opts]) == {:validation_failure, {:bad_argument, "The threshold should be an integer."}} + end + + test "validate: a single absolute float with an invalid memory unit fails ", context do + assert @command.validate(["absolute","10.0bytes"], context[:opts]) == {:validation_failure, {:bad_argument, "The threshold should be an integer."}} + end + + test "validate: a single absolute string fails ", context do + assert @command.validate(["absolute","large"], context[:opts]) == {:validation_failure, :bad_argument} + end + + test "validate: a single absolute string with a valid unit fails ", context do + assert @command.validate(["absolute","manyGB"], context[:opts]) == {:validation_failure, :bad_argument} + end + + test "run: a single absolute integer with memory units return ok", context do + memory_units() + |> Enum.each(fn mu -> + arg = "10#{mu}" + assert @command.run(["absolute",arg], context[:opts]) == :ok + assert status()[:vm_memory_high_watermark] == {:absolute, memory_unit_absolute(10, mu)} + end) + end + + test "run: low watermark sets alarm", context do + old_watermark = status()[:vm_memory_high_watermark] + on_exit(fn() -> + args = case old_watermark do + {:absolute, val} -> ["absolute", to_string(val)]; + other -> [to_string(other)] + end + @command.run(args, context[:opts]) + end) + ## this will trigger an alarm + @command.run(["absolute", "2000"], context[:opts]) + + assert [:memory] == alarm_types(status()[:alarms]) + end + + test "banner: absolute memory request prints info message", context do + assert @command.banner(["absolute", "10"], context[:opts]) + =~ ~r/Setting memory threshold on #{get_rabbit_hostname()} to 10 bytes .../ + + assert @command.banner(["absolute", "-10"], context[:opts]) + =~ ~r/Setting memory threshold on #{get_rabbit_hostname()} to -10 bytes .../ + + assert @command.banner(["absolute", "sandwich"], context[:opts]) + =~ ~r/Setting memory threshold on #{get_rabbit_hostname()} to sandwich bytes .../ + end + + test "banner, relative memory", context do + assert @command.banner(["0.7"], context[:opts]) + =~ ~r/Setting memory threshold on #{get_rabbit_hostname()} to 0.7 .../ + + assert @command.banner(["-0.7"], context[:opts]) + =~ ~r/Setting memory threshold on #{get_rabbit_hostname()} to -0.7 .../ + + assert @command.banner(["sandwich"], context[:opts]) + =~ ~r/Setting memory threshold on #{get_rabbit_hostname()} to sandwich .../ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/shutdown_command_test.exs b/deps/rabbitmq_cli/test/ctl/shutdown_command_test.exs new file mode 100644 index 0000000000..153c136c4b --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/shutdown_command_test.exs @@ -0,0 +1,53 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ShutdownCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.ShutdownCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 15}} + end + + test "validate: accepts no arguments", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "validate: in wait mode, checks if local and target node hostnames match" do + assert match?({:validation_failure, {:unsupported_target, _}}, + @command.validate([], %{wait: true, node: :'rabbit@some.remote.hostname'})) + end + + test "validate: in wait mode, always assumes @localhost nodes are local" do + assert @command.validate([], %{wait: true, node: :rabbit@localhost}) == :ok + end + + test "validate: in no wait mode, passes unconditionally", context do + assert @command.validate([], Map.merge(%{wait: false}, context[:opts])) == :ok + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, wait: false, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "empty banner", context do + nil = @command.banner([], context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/start_app_command_test.exs b/deps/rabbitmq_cli/test/ctl/start_app_command_test.exs new file mode 100644 index 0000000000..bdd8632842 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/start_app_command_test.exs @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule StartAppCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.StartAppCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: request to an active node succeeds", context do + node = RabbitMQ.CLI.Core.Helpers.normalise_node(context[:node], :shortnames) + stop_rabbitmq_app() + refute :rabbit_misc.rpc_call(node, :rabbit, :is_running, []) + assert @command.run([], context[:opts]) + assert :rabbit_misc.rpc_call(node, :rabbit, :is_running, []) + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Starting node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/status_command_test.exs b/deps/rabbitmq_cli/test/ctl/status_command_test.exs new file mode 100644 index 0000000000..03ab6cb8fc --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/status_command_test.exs @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule StatusCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.StatusCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: 60_000}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: request to a named, active node succeeds", context do + assert @command.run([], context[:opts])[:pid] != nil + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Status of node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/stop_app_command_test.exs b/deps/rabbitmq_cli/test/ctl/stop_app_command_test.exs new file mode 100644 index 0000000000..60551b2189 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/stop_app_command_test.exs @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule StopAppCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.StopAppCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: request to an active node succeeds", context do + node = RabbitMQ.CLI.Core.Helpers.normalise_node(context[:node], :shortnames) + assert :rabbit_misc.rpc_call(node, :rabbit, :is_running, []) + assert @command.run([], context[:opts]) + refute :rabbit_misc.rpc_call(node, :rabbit, :is_running, []) + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Stopping rabbit application on node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/stop_command_test.exs b/deps/rabbitmq_cli/test/ctl/stop_command_test.exs new file mode 100644 index 0000000000..2f1dca2eae --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/stop_command_test.exs @@ -0,0 +1,52 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule StopCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.StopCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), + idempotent: false}} + end + + test "validate accepts no arguments", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate accepts a PID file path", context do + assert @command.validate(["/path/to/pidfile.pid"], context[:opts]) == :ok + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["/path/to/pidfile.pid", "extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + # NB: as this commands shuts down the Erlang vm it isn't really practical to test it here + + test "run: request to a non-existent node with --idempotent=false returns a badrpc" do + opts = %{node: :jake@thedog, idempotent: false, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "run: request to a non-existent node with --idempotent returns ok" do + opts = %{node: :jake@thedog, idempotent: true, timeout: 200} + assert match?({:ok, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) =~ ~r/Stopping and halting node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/suspend_listeners_command_test.exs b/deps/rabbitmq_cli/test/ctl/suspend_listeners_command_test.exs new file mode 100644 index 0000000000..602cdf9f8b --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/suspend_listeners_command_test.exs @@ -0,0 +1,67 @@ +## 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 https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule SuspendListenersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SuspendListenersCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + resume_all_client_listeners() + + node_name = get_rabbit_hostname() + on_exit(fn -> + resume_all_client_listeners() + close_all_connections(node_name) + end) + + {:ok, opts: %{node: node_name, timeout: 30_000}} + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname()}} + end + + test "merge_defaults: merges no defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: accepts no arguments", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: with extra arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: request to a non-existent node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "run: suspends all client TCP listeners so no new client connects are accepted", context do + assert @command.run([], Map.merge(context[:opts], %{timeout: 5_000})) == :ok + + expect_client_connection_failure() + resume_all_client_listeners() + + # implies a successful connection + with_channel("/", fn _ -> :ok end) + close_all_connections(get_rabbit_hostname()) + end +end diff --git a/deps/rabbitmq_cli/test/ctl/sync_queue_command_test.exs b/deps/rabbitmq_cli/test/ctl/sync_queue_command_test.exs new file mode 100644 index 0000000000..3d3f866dd0 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/sync_queue_command_test.exs @@ -0,0 +1,64 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule SyncQueueCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.SyncQueueCommand + + @vhost "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname(), + vhost: @vhost + }} + end + + test "validate: specifying no queue name is reported as an error", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate: specifying two queue names is reported as an error", context do + assert @command.validate(["q1", "q2"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: specifying three queue names is reported as an error", context do + assert @command.validate(["q1", "q2", "q3"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: specifying one queue name succeeds", context do + assert @command.validate(["q1"], context[:opts]) == :ok + end + + test "run: request to a non-existent RabbitMQ node returns a nodedown" do + opts = %{node: :jake@thedog, vhost: @vhost, timeout: 200} + assert match?({:badrpc, _}, @command.run(["q1"], opts)) + end + + test "banner", context do + s = @command.banner(["q1"], context[:opts]) + + assert s =~ ~r/Synchronising queue/ + assert s =~ ~r/q1/ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/trace_off_command_test.exs b/deps/rabbitmq_cli/test/ctl/trace_off_command_test.exs new file mode 100644 index 0000000000..0ea53774cb --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/trace_off_command_test.exs @@ -0,0 +1,78 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule TraceOffCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.TraceOffCommand + + @test_vhost "test" + @default_vhost "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost(@test_vhost) + + on_exit([], fn -> + delete_vhost(@test_vhost) + end) + + :ok + end + + setup context do + trace_on(context[:vhost]) + on_exit(context, fn -> trace_off(context[:vhost]) end) + {:ok, opts: %{node: get_rabbit_hostname(), vhost: context[:vhost]}} + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: wrong number of arguments triggers arg count error" do + assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: on an active node, trace_off command works on default" do + opts = %{node: get_rabbit_hostname()} + opts_with_vhost = %{node: get_rabbit_hostname(), vhost: "/"} + trace_on(@default_vhost) + + assert @command.merge_defaults([], opts) == {[], opts_with_vhost} + end + + test "run: on an invalid RabbitMQ node, return a nodedown" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag target: get_rabbit_hostname(), vhost: @default_vhost + test "run: calls to trace_off are idempotent", context do + @command.run([], context[:opts]) + assert @command.run([], context[:opts]) == {:ok, "Trace disabled for vhost #{@default_vhost}"} + end + + @tag vhost: @test_vhost + test "run: on an active node, trace_off command works on named vhost", context do + assert @command.run([], context[:opts]) == {:ok, "Trace disabled for vhost #{@test_vhost}"} + end + + @tag vhost: "toast" + test "run: Turning tracing off on invalid host returns successfully", context do + assert @command.run([], context[:opts]) == {:ok, "Trace disabled for vhost toast"} + end + + @tag vhost: @default_vhost + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Stopping tracing for vhost "#{context[:vhost]}" .../ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/trace_on_command_test.exs b/deps/rabbitmq_cli/test/ctl/trace_on_command_test.exs new file mode 100644 index 0000000000..4db58772a1 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/trace_on_command_test.exs @@ -0,0 +1,79 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule TraceOnCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.TraceOnCommand + + @test_vhost "test" + @default_vhost "/" + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + add_vhost(@test_vhost) + + on_exit([], fn -> + delete_vhost(@test_vhost) + end) + + :ok + end + + setup context do + on_exit(context, fn -> trace_off(context[:vhost]) end) + {:ok, opts: %{node: get_rabbit_hostname(), vhost: context[:vhost]}} + end + + test "merge_defaults: on an active node, trace_on command works on default" do + opts = %{node: get_rabbit_hostname()} + opts_with_vhost = %{node: get_rabbit_hostname(), vhost: "/"} + + assert @command.merge_defaults([], opts) == {[], opts_with_vhost} + + trace_off(@default_vhost) + end + + test "merge_defaults: defaults can be overridden" do + assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} + assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}} + end + + test "validate: wrong number of arguments triggers arg count error" do + assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} + end + + test "run: on an invalid RabbitMQ node, return a nodedown" do + opts = %{node: :jake@thedog, vhost: "/", timeout: 200} + + assert match?({:badrpc, _}, @command.run([], opts)) + end + + @tag vhost: @default_vhost + test "run: calls to trace_on are idempotent", context do + @command.run([], context[:opts]) + assert @command.run([], context[:opts]) == {:ok, "Trace enabled for vhost #{@default_vhost}"} + end + + @tag vhost: @test_vhost + test "run: on an active node, trace_on command works on named vhost", context do + assert @command.run([], context[:opts]) == {:ok, "Trace enabled for vhost #{@test_vhost}"} + end + + @tag vhost: "toast" + test "run: Turning tracing on on invalid host returns successfully", context do + assert @command.run([], context[:opts]) == {:ok, "Trace enabled for vhost toast"} + end + + @tag vhost: @default_vhost + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Starting tracing for vhost "#{context[:vhost]}" .../ + end +end diff --git a/deps/rabbitmq_cli/test/ctl/update_cluster_nodes_command_test.exs b/deps/rabbitmq_cli/test/ctl/update_cluster_nodes_command_test.exs new file mode 100644 index 0000000000..b94c21f1be --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/update_cluster_nodes_command_test.exs @@ -0,0 +1,80 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule UpdateClusterNodesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.UpdateClusterNodesCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname() + }} + end + + test "validate: providing too few arguments fails validation", context do + assert @command.validate([], context[:opts]) == + {:validation_failure, :not_enough_args} + end + + test "validate: providing too many arguments fails validation", context do + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: specifying self as seed node fails validation", context do + stop_rabbitmq_app() + assert match?( + {:error, :cannot_cluster_node_with_itself}, + @command.run([context[:opts][:node]], context[:opts])) + start_rabbitmq_app() + end + + test "run: request to an unreachable node returns a badrpc", context do + opts = %{ + node: :jake@thedog, + timeout: 200 + } + assert match?( + {:badrpc, :nodedown}, + @command.run([context[:opts][:node]], opts)) + end + + test "run: specifying an unreachable node as seed returns a badrpc", context do + stop_rabbitmq_app() + assert match?( + {:badrpc_multi, _, [_]}, + @command.run([:jake@thedog], context[:opts])) + start_rabbitmq_app() + end + + test "banner", context do + assert @command.banner(["a"], context[:opts]) =~ + ~r/Will seed #{get_rabbit_hostname()} from a on next start/ + end + + test "output mnesia is running error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, + "Mnesia is still running on node " <> _}, + @command.output({:error, :mnesia_unexpectedly_running}, context[:opts])) + + end +end diff --git a/deps/rabbitmq_cli/test/ctl/version_command_test.exs b/deps/rabbitmq_cli/test/ctl/version_command_test.exs new file mode 100644 index 0000000000..76216b6cf0 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/version_command_test.exs @@ -0,0 +1,24 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule VersionCommandTest do + use ExUnit.Case + + @command RabbitMQ.CLI.Ctl.Commands.VersionCommand + + test "merge_defaults: merges no defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end +end diff --git a/deps/rabbitmq_cli/test/ctl/wait_command_test.exs b/deps/rabbitmq_cli/test/ctl/wait_command_test.exs new file mode 100644 index 0000000000..c1fd604245 --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/wait_command_test.exs @@ -0,0 +1,114 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule WaitCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.WaitCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + RabbitMQ.CLI.Core.Distribution.start() + rabbitmq_home = :rabbit_misc.rpc_call(get_rabbit_hostname(), :code, :lib_dir, [:rabbit]) + + {:ok, opts: %{node: get_rabbit_hostname(), + rabbitmq_home: rabbitmq_home, + timeout: 500}} + end + + + test "validate: cannot have both pid and pidfile", context do + {:validation_failure, "Cannot specify both pid and pidfile"} = + @command.validate(["pid_file"], Map.merge(context[:opts], %{pid: 123})) + end + + test "validate: should have either pid or pidfile", context do + {:validation_failure, "No pid or pidfile specified"} = + @command.validate([], context[:opts]) + end + + test "validate: with more than one argument returns an arg count error", context do + assert @command.validate(["pid_file", "extra"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "run: times out waiting for non-existent pid file", context do + {:error, {:timeout, _}} = @command.run(["pid_file"], context[:opts]) |> Enum.to_list |> List.last + end + + test "run: fails if pid process does not exist", context do + non_existent_pid = get_non_existent_os_pid() + {:error, :process_not_running} = + @command.run([], Map.merge(context[:opts], %{pid: non_existent_pid})) + |> Enum.to_list + |> List.last + end + + test "run: times out if unable to communicate with the node", context do + pid = String.to_integer(System.get_pid()) + {:error, {:timeout, _}} = + @command.run([], Map.merge(context[:opts], %{pid: pid, node: :nonode@nohost})) + |> Enum.to_list + |> List.last + end + + test "run: happy path", context do + pid = :erlang.list_to_integer(:rpc.call(context[:opts][:node], :os, :getpid, [])) + output = @command.run([], Map.merge(context[:opts], %{pid: pid})) + assert_stream_without_errors(output) + end + + test "run: happy path in quiet mode", context do + pid = :erlang.list_to_integer(:rpc.call(context[:opts][:node], :os, :getpid, [])) + output = @command.run([], Map.merge(context[:opts], %{pid: pid, quiet: true})) + [] = Enum.to_list(output) + end + + test "no banner", context do + nil = @command.banner([], context[:opts]) + end + + test "output: process not running error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, "Error: process is not running."}, + @command.output({:error, :process_not_running}, context[:opts])) + end + + test "output: garbage in pid file error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, "Error: garbage in pid file."}, + @command.output({:error, {:garbage_in_pid_file, "somefile"}}, context[:opts])) + end + + test "output: could not read pid error", context do + exit_code = RabbitMQ.CLI.Core.ExitCodes.exit_software + assert match?({:error, ^exit_code, "Error: could not read pid. Detail: something wrong"}, + @command.output({:error, {:could_not_read_pid, "something wrong"}}, context[:opts])) + end + + test "output: default output is fine", context do + assert match?({:error, "message"}, @command.output({:error, "message"}, context[:opts])) + assert match?({:error, :message}, @command.output({:error, :message}, context[:opts])) + assert match?({:error, :message}, @command.output(:message, context[:opts])) + assert match?({:ok, "ok"}, @command.output({:ok, "ok"}, context[:opts])) + assert match?(:ok, @command.output(:ok, context[:opts])) + assert match?({:ok, "ok"}, @command.output("ok", context[:opts])) + end + + def get_non_existent_os_pid(pid \\ 2) do + case :rabbit_misc.is_os_process_alive(to_charlist(pid)) do + true -> get_non_existent_os_pid(pid + 1) + false -> pid + end + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/alarms_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/alarms_command_test.exs new file mode 100644 index 0000000000..70a2bfda64 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/alarms_command_test.exs @@ -0,0 +1,69 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule AlarmsCommandTest do + use ExUnit.Case, async: false + import TestHelper + import RabbitMQ.CLI.Core.Alarms, only: [alarm_types: 1] + + @command RabbitMQ.CLI.Diagnostics.Commands.AlarmsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: when target node has no alarms in effect, returns an empty list", context do + assert [] == status()[:alarms] + + assert @command.run([], context[:opts]) == [] + end + + test "run: when target node has an alarm in effect, returns it", context do + old_watermark = status()[:vm_memory_high_watermark] + on_exit(fn() -> + set_vm_memory_high_watermark(old_watermark) + end) + # 2000 bytes will trigger an alarm + set_vm_memory_high_watermark({:absolute, 2000}) + + assert [:memory] == alarm_types(status()[:alarms]) + assert length(@command.run([], context[:opts])) == 1 + + set_vm_memory_high_watermark(old_watermark) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_alarms_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_alarms_command_test.exs new file mode 100644 index 0000000000..f5b64282e3 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_alarms_command_test.exs @@ -0,0 +1,118 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckAlarmsCommandTest do + use ExUnit.Case, async: false + import TestHelper + import RabbitMQ.CLI.Core.Alarms, only: [alarm_types: 1] + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckAlarmsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when target node has no alarms in effect, returns an empty list", context do + assert [] == status()[:alarms] + + assert @command.run([], context[:opts]) == [] + end + + test "run: when target node has an alarm in effect, returns it", context do + old_watermark = status()[:vm_memory_high_watermark] + on_exit(fn() -> + set_vm_memory_high_watermark(old_watermark) + end) + # 2000 bytes will trigger an alarm + set_vm_memory_high_watermark({:absolute, 2000}) + + assert [:memory] == alarm_types(status()[:alarms]) + assert length(@command.run([], context[:opts])) == 1 + + set_vm_memory_high_watermark(old_watermark) + end + + + test "output: when target node has no alarms in effect, returns a success", context do + assert [] == status()[:alarms] + + assert match?({:ok, _}, @command.output([], context[:opts])) + end + + test "output: when target node has an alarm in effect, returns a failure", context do + for input <- [ + [ + :file_descriptor_limit + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, :hare@warp10}, []} + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, :hare@warp10}, []}, + {{:resource_limit, :memory, :hare@warp10}, []}, + {{:resource_limit, :disk, :rabbit@warp10}, []}, + {{:resource_limit, :memory, :rabbit@warp10}, []} + ] + ] do + assert match?({:error, _, _}, @command.output(input, context[:opts])) + end + end + + test "output: when target node has an alarm in effect and --silent is passed, returns a silent failure", _context do + for input <- [ + [ + :file_descriptor_limit + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, :hare@warp10}, []} + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, :hare@warp10}, []}, + {{:resource_limit, :memory, :hare@warp10}, []}, + {{:resource_limit, :disk, :rabbit@warp10}, []}, + {{:resource_limit, :memory, :rabbit@warp10}, []} + ] + ] do + assert {:error, :check_failed} == @command.output(input, %{silent: true}) + end + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_local_alarms_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_local_alarms_command_test.exs new file mode 100644 index 0000000000..0aaf66c707 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_local_alarms_command_test.exs @@ -0,0 +1,111 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckLocalAlarmsCommandTest do + use ExUnit.Case, async: false + import TestHelper + import RabbitMQ.CLI.Core.Alarms, only: [alarm_types: 1] + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckLocalAlarmsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when target node has no alarms in effect, returns an empty list", context do + assert [] == status()[:alarms] + + assert @command.run([], context[:opts]) == [] + end + + test "run: when target node has a local alarm in effect, returns it", context do + old_watermark = status()[:vm_memory_high_watermark] + on_exit(fn() -> + set_vm_memory_high_watermark(old_watermark) + end) + # 2000 bytes will trigger an alarm + set_vm_memory_high_watermark({:absolute, 2000}) + + assert [:memory] == alarm_types(status()[:alarms]) + assert length(@command.run([], context[:opts])) == 1 + + set_vm_memory_high_watermark(old_watermark) + end + + test "output: when target node has no local alarms in effect, returns a success", context do + assert [] == status()[:alarms] + + assert match?({:ok, _}, @command.output([], context[:opts])) + end + + # note: it's run/2 that filters out non-local alarms + test "output: when target node has a local alarm in effect, returns a failure", context do + for input <- [ + [ + :file_descriptor_limit + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, get_rabbit_hostname()}, []}, + {{:resource_limit, :memory, get_rabbit_hostname()}, []} + ] + ] do + assert match?({:error, _}, @command.output(input, context[:opts])) + end + end + + # note: it's run/2 that filters out non-local alarms + test "output: when target node has an alarm in effect and --silent is passed, returns a silent failure", _context do + for input <- [ + [ + :file_descriptor_limit + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, :hare@warp10}, []} + ], + [ + :file_descriptor_limit, + {{:resource_limit, :disk, get_rabbit_hostname()}, []}, + {{:resource_limit, :memory, get_rabbit_hostname()}, []} + ] + ] do + assert {:error, :check_failed} == @command.output(input, %{silent: true}) + end + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_port_connectivity_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_port_connectivity_command_test.exs new file mode 100644 index 0000000000..845a7b6f1d --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_port_connectivity_command_test.exs @@ -0,0 +1,59 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckPortConnectivityCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckPortConnectivityCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: provides a default timeout" do + assert @command.merge_defaults([], %{}) == {[], %{timeout: 30000}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: tries to connect to every inferred active listener", context do + assert match?({true, _}, @command.run([], context[:opts])) + end + + + test "output: when all connections succeeded, returns a success", context do + assert match?({:ok, _}, @command.output({true, []}, context[:opts])) + end + + # note: it's run/2 that filters out non-local alarms + test "output: when target node has a local alarm in effect, returns a failure", context do + failure = {:listener, :rabbit@mercurio, :lolz, 'mercurio', + {0, 0, 0, 0, 0, 0, 0, 0}, 7761613, + [backlog: 128, nodelay: true]} + assert match?({:error, _}, @command.output({false, [failure]}, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_port_listener_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_port_listener_command_test.exs new file mode 100644 index 0000000000..7c0428c190 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_port_listener_command_test.exs @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckPortListenerCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckPortListenerCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when two or more arguments are provided, returns a failure" do + assert @command.validate([5672, 61613], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats a single positional argument and default switches as a success" do + assert @command.validate([1883], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([61613], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when a listener for the protocol is active, returns a success", context do + assert match?({true, _}, @command.run([5672], context[:opts])) + end + + test "run: when a listener on the port is not active or unknown, returns an error", context do + assert match?({false, _, _}, @command.run([47777], context[:opts])) + end + + test "output: when a listener for the port is active, returns a success", context do + assert match?({:ok, _}, @command.output({true, 5672}, context[:opts])) + end + + test "output: when a listener for the port is not active, returns an error", context do + assert match?({:error, _, _}, @command.output({false, 15672, []}, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_protocol_listener_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_protocol_listener_command_test.exs new file mode 100644 index 0000000000..a6aef88bc1 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_protocol_listener_command_test.exs @@ -0,0 +1,68 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckProtocolListenerCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckProtocolListenerCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when two or more arguments are provided, returns a failure" do + assert @command.validate(["amqp", "stomp"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats a single positional argument and default switches as a success" do + assert @command.validate(["mqtt"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run(["stomp"], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when a listener for the protocol is active, returns a success", context do + assert match?({true, _}, @command.run(["amqp"], context[:opts])) + end + + test "run: accepts a number of alternative protocol names/spellings", context do + for p <- ["amqp", "amqp1.0", "amqp10", "amqp091", "stomp1.2", "distribution"] do + assert match?({true, _}, @command.run([p], context[:opts])) + end + end + + test "run: when a listener for the protocol is not active or unknown, returns an error", context do + assert match?({false, _, _}, @command.run(["non-existent-proto"], context[:opts])) + end + + test "output: when a listener for the protocol is active, returns a success", context do + assert match?({:ok, _}, @command.output({true, "amqp"}, context[:opts])) + end + + test "output: when a listener for the protocol is not active, returns an error", context do + assert match?({:error, _}, @command.output({false, "http", []}, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_running_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_running_command_test.exs new file mode 100644 index 0000000000..ab89d1e89e --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_running_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckRunningCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckRunningCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when the RabbitMQ app is booted and started, returns true", context do + await_rabbitmq_startup() + + assert @command.run([], context[:opts]) + end + + test "run: when the RabbitMQ app is stopped, returns false", context do + stop_rabbitmq_app() + + refute is_rabbitmq_app_running() + refute @command.run([], context[:opts]) + + start_rabbitmq_app() + end + + test "output: when the result is true, returns successfully", context do + assert match?({:ok, _}, @command.output(true, context[:opts])) + end + + # this is a check command + test "output: when the result is false, returns an error", context do + assert match?({:error, _}, @command.output(false, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/check_virtual_hosts_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/check_virtual_hosts_command_test.exs new file mode 100644 index 0000000000..2fab76ae9b --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/check_virtual_hosts_command_test.exs @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule CheckVirtualHostsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CheckVirtualHostsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: is a no-op" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "output: when all virtual hosts are reported as up, returns a success", context do + assert match?({:ok, _}, @command.output([], context[:opts])) + end + + test "output: when target node reports a virtual host as down, returns a failure", context do + assert match?({:error, _}, @command.output(["a-down-vhost"], context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/cipher_suites_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/cipher_suites_command_test.exs new file mode 100644 index 0000000000..2ee5edddb8 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/cipher_suites_command_test.exs @@ -0,0 +1,101 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule CipherSuitesCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CipherSuitesCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + format: context[:format] || "openssl", + all: false + }} + end + + test "merge_defaults: defaults to the OpenSSL format" do + assert @command.merge_defaults([], %{}) == {[], %{format: "openssl", all: false}} + end + + test "merge_defaults: format is case insensitive" do + assert @command.merge_defaults([], %{format: "OpenSSL"}) == {[], %{format: "openssl", all: false}} + assert @command.merge_defaults([], %{format: "Erlang"}) == {[], %{format: "erlang", all: false}} + assert @command.merge_defaults([], %{format: "Map"}) == {[], %{format: "map", all: false}} + end + + test "merge_defaults: format can be overridden" do + assert @command.merge_defaults([], %{format: "map"}) == {[], %{format: "map", all: false}} + end + + test "validate: treats positional arguments as a failure", context do + assert @command.validate(["extra-arg"], context[:opts]) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: supports openssl, erlang and map formats", context do + assert @command.validate([], Map.merge(context[:opts], %{format: "openssl"})) == :ok + assert @command.validate([], Map.merge(context[:opts], %{format: "erlang"})) == :ok + assert @command.validate([], Map.merge(context[:opts], %{format: "map"})) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag format: "openssl" + test "run: returns a list of cipher suites in OpenSSL format", context do + res = @command.run([], context[:opts]) + for cipher <- res, do: assert true == is_list(cipher) + # the list is long and its values are environment-specific, + # so we simply assert that it is non-empty. MK. + assert length(res) > 0 + end + + @tag format: "erlang" + test "run: returns a list of cipher suites in erlang format", context do + res = @command.run([], context[:opts]) + + for cipher <- res, do: assert true = is_tuple(cipher) + # the list is long and its values are environment-specific, + # so we simply assert that it is non-empty. MK. + assert length(res) > 0 + end + + @tag format: "map" + test "run: returns a list of cipher suites in map format", context do + res = @command.run([], context[:opts]) + for cipher <- res, do: assert true = is_map(cipher) + # the list is long and its values are environment-specific, + # so we simply assert that it is non-empty. MK. + assert length(res) > 0 + end + + test "run: returns more cipher suites when all suites requested", context do + default_suites_opts = Map.merge(context[:opts], %{all: false}) + default_suites = @command.run([], default_suites_opts) + + all_suites_opts = Map.merge(context[:opts], %{all: true}) + all_suites = @command.run([], all_suites_opts) + + assert length(all_suites) > length(default_suites) + assert length(default_suites -- all_suites) == 0 + end + +end diff --git a/deps/rabbitmq_cli/test/diagnostics/command_line_arguments_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/command_line_arguments_command_test.exs new file mode 100644 index 0000000000..caa959ce44 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/command_line_arguments_command_test.exs @@ -0,0 +1,44 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule CommandLineArgumentsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.CommandLineArgumentsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: :infinity}} + end + + test "validate: with extra arguments, command line arguments returns an arg count error", context do + assert @command.validate(["extra"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "run: command line arguments request to a reachable node succeeds", context do + output = @command.run([], context[:opts]) |> Enum.to_list + + assert_stream_without_errors(output) + end + + test "run: command line arguments request on nonexistent RabbitMQ node returns a badrpc" do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + assert @command.banner([], context[:opts]) + =~ ~r/Command line arguments of node #{get_rabbit_hostname()}/ + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/consume_event_stream_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/consume_event_stream_command_test.exs new file mode 100644 index 0000000000..b11cdb38c2 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/consume_event_stream_command_test.exs @@ -0,0 +1,73 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ConsumeEventStreamCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ConsumeEventStreamCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + duration: :infinity, + pattern: ".*" + }} + end + + test "merge_defaults: duration defaults to infinity, pattern to anything" do + assert @command.merge_defaults([], %{}) == {[], %{duration: :infinity, + pattern: ".*", + quiet: true}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success", context do + assert @command.validate([], context[:opts]) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: consumes events for N seconds", context do + + stream = @command.run([], Map.merge(context[:opts], %{duration: 5})) + :rpc.call(get_rabbit_hostname(), :rabbit_event, :notify, [String.to_atom("event_type1"), + [{String.to_atom("args"), 1}]]) + :rpc.call(get_rabbit_hostname(), :rabbit_event, :notify, [String.to_atom("event_type2"), + [{String.to_atom("pid"), self()}]]) + + + event1 = Enum.find(stream, nil, fn x -> Keyword.get(x, :event, nil) == "event.type1" end) + event2 = Enum.find(stream, nil, fn x -> Keyword.get(x, :event, nil) == "event.type2" end) + assert event1 != nil + assert event2 != nil + assert Keyword.get(event1, :args, nil) == 1 + assert is_binary(Keyword.get(event2, :pid, nil)) + + end + +end diff --git a/deps/rabbitmq_cli/test/diagnostics/disable_auth_attempt_source_tracking_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/disable_auth_attempt_source_tracking_command_test.exs new file mode 100644 index 0000000000..7a2b4295c7 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/disable_auth_attempt_source_tracking_command_test.exs @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule DisbleAuthAttemptSourceTrackingCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.DisableAuthAttemptSourceTrackingCommand + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}} + end + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 15000 + test "run: disables source tracking for auth attempt stats", context do + assert :ok = @command.run([], context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/discover_peers_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/discover_peers_command_test.exs new file mode 100644 index 0000000000..dd54d6eed9 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/discover_peers_command_test.exs @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule DiscoverPeersCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.DiscoverPeersCommand + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}} + end + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 15000 + test "run: returns a list of nodes when the backend isn't configured", context do + assert match?({:ok, {[], _}}, @command.run([], context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/enable_auth_attempt_source_tracking_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/enable_auth_attempt_source_tracking_command_test.exs new file mode 100644 index 0000000000..c55ac6134b --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/enable_auth_attempt_source_tracking_command_test.exs @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule EnableAuthAttemptSourceTrackingCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.EnableAuthAttemptSourceTrackingCommand + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}} + end + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 15000 + test "run: enables source tracking for auth attempt stats", context do + assert :ok = @command.run([], context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/erlang_cookie_hash_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/erlang_cookie_hash_command_test.exs new file mode 100644 index 0000000000..5dff653989 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/erlang_cookie_hash_command_test.exs @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ErlangCookieHashCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ErlangCookieHashCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 5000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], opts))) + end + + test "run: returns the erlang cookie hash", context do + res = @command.run([], context[:opts]) + assert is_bitstring(res) + end + +end diff --git a/deps/rabbitmq_cli/test/diagnostics/erlang_cookie_sources_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/erlang_cookie_sources_command_test.exs new file mode 100644 index 0000000000..794dd52a44 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/erlang_cookie_sources_command_test.exs @@ -0,0 +1,37 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ErlangCookieSourcesCommandTest do + use ExUnit.Case, async: true + + @command RabbitMQ.CLI.Diagnostics.Commands.ErlangCookieSourcesCommand + + setup _context do + {:ok, opts: %{}} + end + + test "merge_defaults: merges no defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + test "run: returns Erlang cookie sources info", context do + result = @command.run([], context[:opts]) + + assert result[:effective_user] != nil + assert result[:home_dir] != nil + assert result[:cookie_file_path] != nil + assert result[:cookie_file_exists] != nil + assert result[:cookie_file_access] != nil + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/erlang_version_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/erlang_version_command_test.exs new file mode 100644 index 0000000000..3bdaa645e2 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/erlang_version_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ErlangVersionCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ErlangVersionCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + details: false, + offline: false + }} + end + + test "merge_defaults: defaults to remote version and abbreviated output" do + assert @command.merge_defaults([], %{}) == {[], %{details: false, offline: false}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + test "validate: treats empty positional arguments and --details as a success" do + assert @command.validate([], %{details: true}) == :ok + end + + test "validate: treats empty positional arguments and --offline as a success" do + assert @command.validate([], %{offline: true}) == :ok + end + + test "validate: treats empty positional arguments, --details and --offline as a success" do + assert @command.validate([], %{details: true, offline: true}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, details: false}))) + end + + test "run: returns Erlang/OTP version on the target node", context do + res = @command.run([], context[:opts]) + assert is_bitstring(res) + end + + test "run with --details: returns Erlang/OTP version on the target node", context do + res = @command.run([], Map.merge(%{details: true}, context[:opts])) + assert is_bitstring(res) + end + + test "run: when --offline is used, returns local Erlang/OTP version", context do + res = @command.run([], Map.merge(context[:opts], %{offline: true})) + assert is_bitstring(res) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/is_booting_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/is_booting_command_test.exs new file mode 100644 index 0000000000..fc7c2595a9 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/is_booting_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule IsBootingCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.IsBootingCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when the RabbitMQ app is fully booted and running, returns false", context do + await_rabbitmq_startup() + + refute @command.run([], context[:opts]) + end + + test "run: when the RabbitMQ app is stopped, returns false", context do + stop_rabbitmq_app() + + refute is_rabbitmq_app_running() + refute @command.run([], context[:opts]) + + start_rabbitmq_app() + end + + test "output: when the result is true, returns successfully", context do + assert match?({:ok, _}, @command.output(true, context[:opts])) + end + + # this is an info command and not a check one + test "output: when the result is false, returns successfully", context do + assert match?({:ok, _}, @command.output(false, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/is_running_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/is_running_command_test.exs new file mode 100644 index 0000000000..120af9d7d7 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/is_running_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule IsRunningCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.IsRunningCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: when the RabbitMQ app is booted and started, returns true", context do + await_rabbitmq_startup() + + assert @command.run([], context[:opts]) + end + + test "run: when the RabbitMQ app is stopped, returns false", context do + stop_rabbitmq_app() + + refute is_rabbitmq_app_running() + refute @command.run([], context[:opts]) + + start_rabbitmq_app() + end + + test "output: when the result is true, returns successfully", context do + assert match?({:ok, _}, @command.output(true, context[:opts])) + end + + # this is an info command and not a check one + test "output: when the result is false, returns successfully", context do + assert match?({:ok, _}, @command.output(false, context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/list_network_interfaces_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/list_network_interfaces_command_test.exs new file mode 100644 index 0000000000..ccaac33d9b --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/list_network_interfaces_command_test.exs @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListNetworkInterfacesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ListNetworkInterfacesCommand + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}} + end + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 15000 + test "run: returns a map of interfaces", context do + assert match?(%{}, @command.run([], context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/list_node_auth_attempt_stats_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/list_node_auth_attempt_stats_command_test.exs new file mode 100644 index 0000000000..c6ac28a340 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/list_node_auth_attempt_stats_command_test.exs @@ -0,0 +1,39 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListNodeAuthAttemptStatsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ListNodeAuthAttemptStatsCommand + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + :ok + end + + setup context do + {:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout], by_source: false}} + end + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 15000 + test "run: returns auth attempt stats", context do + assert is_list(@command.run([], context[:opts])) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/listeners_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/listeners_command_test.exs new file mode 100644 index 0000000000..fc20cae7fc --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/listeners_command_test.exs @@ -0,0 +1,78 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListenersCommandTest do + use ExUnit.Case, async: false + import TestHelper + import RabbitMQ.CLI.Core.Listeners, only: [listener_maps: 1] + + @command RabbitMQ.CLI.Diagnostics.Commands.ListenersCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: returns a list of node-local listeners", context do + xs = @command.run([], context[:opts]) |> listener_maps + + assert length(xs) >= 3 + for p <- [5672, 61613, 25672] do + assert Enum.any?(xs, fn %{port: port} -> port == p end) + end + end + + test "output: returns a formatted list of node-local listeners", context do + raw = @command.run([], context[:opts]) + {:ok, msg} = @command.output(raw, context[:opts]) + + for p <- [5672, 61613, 25672] do + assert msg =~ ~r/#{p}/ + end + end + + test "output: when formatter is JSON, returns an array of listener maps", context do + raw = @command.run([], context[:opts]) + {:ok, doc} = @command.output(raw, Map.merge(%{formatter: "json"}, context[:opts])) + xs = doc["listeners"] + + assert length(xs) >= 3 + for p <- [5672, 61613, 25672] do + assert Enum.any?(xs, fn %{port: port} -> port == p end) + end + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/log_location_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/log_location_command_test.exs new file mode 100644 index 0000000000..64a85fc519 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/log_location_command_test.exs @@ -0,0 +1,98 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule LogLocationCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.LogLocationCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + all: false + }} + end + + test "merge_defaults: all is false" do + assert @command.merge_defaults([], %{}) == {[], %{all: :false}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{all: :false}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: prints default log location", context do + # Let Lager's log message rate lapse or else some messages + # we assert on might be dropped. MK. + Process.sleep(1000) + {:ok, logfile} = @command.run([], context[:opts]) + log_message = "file location" + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, [log_message]) + wait_for_log_message(log_message, logfile) + {:ok, log_file_data} = File.read(logfile) + assert String.match?(log_file_data, Regex.compile!(log_message)) + end + + test "run: shows all log locations", context do + # Let Lager's log message rate lapse or else some messages + # we assert on might be dropped. MK. + Process.sleep(1000) + # This assumes default configuration + [logfile, upgrade_log_file] = + @command.run([], Map.merge(context[:opts], %{all: true})) + + log_message = "checking the default log file when checking all" + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, [log_message]) + wait_for_log_message(log_message, logfile) + + log_message_upgrade = "checking the upgrade log file when checking all" + :rpc.call(get_rabbit_hostname(), + :rabbit_log, :log, [:upgrade, :error, log_message_upgrade, []]) + wait_for_log_message(log_message_upgrade, upgrade_log_file) + end + + test "run: fails if there is no log file configured", context do + {:ok, upgrade_file} = :rpc.call(get_rabbit_hostname(), :application, :get_env, [:rabbit, :lager_upgrade_file]) + {:ok, default_file} = :rpc.call(get_rabbit_hostname(), :application, :get_env, [:rabbit, :lager_default_file]) + on_exit([], fn -> + :rpc.call(get_rabbit_hostname(), :application, :set_env, [:rabbit, :lager_upgrade_file, upgrade_file]) + :rpc.call(get_rabbit_hostname(), :application, :set_env, [:rabbit, :lager_default_file, default_file]) + :rpc.call(get_rabbit_hostname(), :rabbit_lager, :configure_lager, []) + start_rabbitmq_app() + end) + stop_rabbitmq_app() + :rpc.call(get_rabbit_hostname(), :application, :unset_env, [:rabbit, :lager_upgrade_file]) + :rpc.call(get_rabbit_hostname(), :application, :unset_env, [:rabbit, :lager_default_file]) + :rpc.call(get_rabbit_hostname(), :application, :unset_env, [:rabbit, :log]) + :rpc.call(get_rabbit_hostname(), :rabbit_lager, :configure_lager, []) + {:error, "No log files configured on the node"} = @command.run([], context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/log_tail_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/log_tail_command_test.exs new file mode 100644 index 0000000000..fb19821d55 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/log_tail_command_test.exs @@ -0,0 +1,115 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule LogTailCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.LogTailCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + number: 50 + }} + end + + test "merge_defaults: number is 50" do + assert @command.merge_defaults([], %{}) == {[], %{number: 50}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success", context do + assert @command.validate([], context[:opts]) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: shows last 50 lines from the log by default", context do + # Let Lager's log message rate lapse or else some messages + # we assert on might be dropped. MK. + Process.sleep(1000) + clear_log_files() + log_messages = + Enum.map(:lists.seq(1, 50), + fn(n) -> + message = "Getting log tail #{n}" + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, [message]) + message + end) + wait_for_log_message("Getting log tail 50") + lines = @command.run([], context[:opts]) + assert Enum.count(lines) == 50 + + Enum.map(Enum.zip(log_messages, lines), + fn({message, line}) -> + assert String.match?(line, Regex.compile!(message)) + end) + end + + test "run: returns N lines", context do + # Let Lager's log message rate lapse or else some messages + # we assert on might be dropped. MK. + Process.sleep(1000) + + ## Log a bunch of lines + Enum.map(:lists.seq(1, 50), + fn(n) -> + message = "More lines #{n}" + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, [message]) + message + end) + wait_for_log_message("More lines 50") + assert Enum.count(@command.run([], Map.merge(context[:opts], %{number: 20}))) == 20 + assert Enum.count(@command.run([], Map.merge(context[:opts], %{number: 30}))) == 30 + assert Enum.count(@command.run([], Map.merge(context[:opts], %{number: 40}))) == 40 + end + + test "run: may return less than N lines if N is high", context do + # Let Lager's log message rate lapse or else some messages + # we assert on might be dropped. MK. + Process.sleep(1000) + clear_log_files() + ## Log a bunch of lines + Enum.map(:lists.seq(1, 100), + fn(n) -> + message = "More lines #{n}" + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, [message]) + message + end) + wait_for_log_message("More lines 50") + assert Enum.count(@command.run([], Map.merge(context[:opts], %{number: 50}))) == 50 + assert Enum.count(@command.run([], Map.merge(context[:opts], %{number: 200}))) < 200 + end + + def clear_log_files() do + [_|_] = logs = :rpc.call(get_rabbit_hostname(), :rabbit_lager, :log_locations, []) + Enum.map(logs, fn(log) -> + File.write(log, "") + end) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/log_tail_stream_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/log_tail_stream_command_test.exs new file mode 100644 index 0000000000..4ad2785604 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/log_tail_stream_command_test.exs @@ -0,0 +1,107 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule LogTailStreamCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.LogTailStreamCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + duration: :infinity + }} + end + + test "merge_defaults: duration defaults to infinity" do + assert @command.merge_defaults([], %{}) == {[], %{duration: :infinity}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success", context do + assert @command.validate([], context[:opts]) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: streams messages for N seconds", context do + ensure_log_file() + time_before = System.system_time(:second) + + stream = @command.run([], Map.merge(context[:opts], %{duration: 15})) + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, ["Message"]) + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, ["Message1"]) + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, ["Message2"]) + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, ["Message3"]) + + # This may take a long time and fail with an ExUnit timeout + data = Enum.join(stream) + + time_after = System.system_time(:second) + + assert String.match?(data, ~r/Message/) + assert String.match?(data, ~r/Message1/) + assert String.match?(data, ~r/Message2/) + assert String.match?(data, ~r/Message3/) + + time_spent = time_after - time_before + assert time_spent > 15 + # This my take longer then duration but not too long + assert time_spent < 45 + end + + test "run: may return an error if there is no log", context do + delete_log_files() + {:error, :enoent} = @command.run([], Map.merge(context[:opts], %{duration: 5})) + end + + def ensure_log_file() do + [log|_] = :rpc.call(get_rabbit_hostname(), :rabbit_lager, :log_locations, []) + ensure_file(log, 100) + end + + def ensure_file(log, 0) do + flunk("timed out trying to ensure the log file #{log}") + end + def ensure_file(log, attempts) do + case File.exists?(log) do + true -> :ok + false -> + :rpc.call(get_rabbit_hostname(), :rabbit_log, :error, ["Ping"]) + :timer.sleep(100) + ensure_file(log, attempts - 1) + end + end + + def delete_log_files() do + [_|_] = logs = :rpc.call(get_rabbit_hostname(), :rabbit_lager, :log_locations, []) + Enum.map(logs, fn(log) -> + File.rm(log) + end) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/maybe_stuck_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/maybe_stuck_command_test.exs new file mode 100644 index 0000000000..3b70966d1c --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/maybe_stuck_command_test.exs @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule MaybeStuckCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.MaybeStuckCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 15000 + }} + end + + test "merge_defaults: returns inputs" do + assert @command.merge_defaults([], %{timeout: 30}) == {[], %{timeout: 30}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 0 + test "run: timeout throws a badrpc", context do + assert @command.run([], context[:opts]) == {:badrpc, :timeout} + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/memory_breakdown_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/memory_breakdown_command_test.exs new file mode 100644 index 0000000000..8f7ffb14dc --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/memory_breakdown_command_test.exs @@ -0,0 +1,72 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2016-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule MemoryBreakdownCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.MemoryBreakdownCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: 5000, + unit: "gb" + }} + end + + test "validate: specifying a positional argument fails validation", context do + assert @command.validate(["abc"], context[:opts]) == + {:validation_failure, :too_many_args} + + assert @command.validate(["abc", "def"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: specifying no positional arguments and no options succeeds", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: specifying gigabytes as a --unit succeeds", context do + assert @command.validate([], Map.merge(context[:opts], %{unit: "gb"})) == :ok + end + + test "validate: specifying bytes as a --unit succeeds", context do + assert @command.validate([], Map.merge(context[:opts], %{unit: "bytes"})) == :ok + end + + test "validate: specifying megabytes as a --unit succeeds", context do + assert @command.validate([], Map.merge(context[:opts], %{unit: "mb"})) == :ok + end + + test "validate: specifying glip-glops as a --unit fails validation", context do + assert @command.validate([], Map.merge(context[:opts], %{unit: "glip-glops"})) == + {:validation_failure, "unit 'glip-glops' is not supported. Please use one of: bytes, mb, gb"} + end + + test "run: request to a non-existent RabbitMQ node returns a nodedown" do + opts = %{node: :jake@thedog, timeout: 200, unit: "gb"} + assert match?({:badrpc, _}, @command.run([], opts)) + end + + test "banner", context do + s = @command.banner([], context[:opts]) + + assert s =~ ~r/Reporting memory breakdown on node/ + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/observer_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/observer_command_test.exs new file mode 100644 index 0000000000..8ff97abb0b --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/observer_command_test.exs @@ -0,0 +1,44 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ObserverCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ObserverCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + interval: 5, + timeout: context[:test_timeout] || 15000 + }} + end + + test "merge_defaults: injects a default interval of 5s" do + assert @command.merge_defaults([], %{}) == {[], %{interval: 5}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, interval: 5})) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/os_env_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/os_env_command_test.exs new file mode 100644 index 0000000000..254b41c9f2 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/os_env_command_test.exs @@ -0,0 +1,62 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule OsEnvCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.OsEnvCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + all: false + }} + end + + test "merge_defaults: merges no defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: returns defined RabbitMQ-specific environment variables", context do + vars = @command.run([], context[:opts]) + + # Only variables that are used by RABBITMQ are returned. + # They can be prefixed with RABBITMQ_ or not, rabbit_env tries both + # when filtering env variables down. + assert Enum.any?(vars, fn({k, _v}) -> + String.starts_with?(k, "RABBITMQ_") or String.starts_with?(k, "rabbitmq_") + end) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/resolve_hostname_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/resolve_hostname_command_test.exs new file mode 100644 index 0000000000..2019154f0c --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/resolve_hostname_command_test.exs @@ -0,0 +1,85 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ResolveHostnameCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ResolveHostnameCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + address_family: "ipv4", + offline: false + }} + end + + test "merge_defaults: defaults to IPv4 address family" do + assert @command.merge_defaults([], %{}) == {[], %{address_family: "IPv4", offline: false}} + end + + test "validate: a single positional argument passes validation" do + assert @command.validate(["rabbitmq.com"], %{}) == :ok + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["elixir-lang.org", "extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: address family other than IPv4 or IPv6 fails validation" do + assert match?({:validation_failure, {:bad_argument, _}}, + @command.validate(["elixir-lang.org"], %{address_family: "ipv5"})) + + assert match?({:validation_failure, {:bad_argument, _}}, + @command.validate(["elixir-lang.org"], %{address_family: "IPv5"})) + end + + test "validate: IPv4 for address family passes validation" do + assert @command.validate(["elixir-lang.org"], %{address_family: "ipv4"}) == :ok + assert @command.validate(["elixir-lang.org"], %{address_family: "IPv4"}) == :ok + end + + test "validate: IPv6 for address family passes validation" do + assert @command.validate(["elixir-lang.org"], %{address_family: "ipv6"}) == :ok + assert @command.validate(["elixir-lang.org"], %{address_family: "IPv6"}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}) + assert match?({:badrpc, _}, @command.run(["elixir-lang.org"], opts)) + end + + test "run: returns a resolution result", context do + case @command.run(["github.com"], context[:opts]) do + {:ok, _hostent} -> :ok + {:error, :nxdomain} -> :ok + other -> flunk("hostname resolution returned #{other}") + end + end + + test "run with --offline: returns a resolution result", context do + case @command.run(["github.com"], Map.merge(context[:opts], %{offline: true})) do + {:ok, _hostent} -> :ok + {:error, :nxdomain} -> :ok + other -> flunk("hostname resolution returned #{other}") + end + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/resolver_info_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/resolver_info_command_test.exs new file mode 100644 index 0000000000..001371ed37 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/resolver_info_command_test.exs @@ -0,0 +1,65 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ResolverInfoCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ResolverInfoCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + start_rabbitmq_app() + + ExUnit.configure([max_cases: 1]) + + on_exit([], fn -> + start_rabbitmq_app() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + offline: false + }} + end + + test "merge_defaults: defaults to offline mode" do + assert @command.merge_defaults([], %{}) == {[], %{offline: false}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog, timeout: 100}))) + end + + test "run: returns host resolver (inetrc) information", context do + result = @command.run([], context[:opts]) + + assert result["lookup"] != nil + assert result["hosts_file"] != nil + end + + test "run: returns host resolver (inetrc) information with --offline", context do + result = @command.run([], Map.merge(context[:opts], %{offline: true})) + + assert result["lookup"] != nil + assert result["hosts_file"] != nil + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/runtime_thread_stats_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/runtime_thread_stats_command_test.exs new file mode 100644 index 0000000000..34ab7b9c63 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/runtime_thread_stats_command_test.exs @@ -0,0 +1,50 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule RuntimeThreadStatsCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.RuntimeThreadStatsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 10000, + sample_interval: 1 + }} + end + + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 2000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + @tag test_timeout: 6000 + test "run: returns msacc-formatted output", context do + res = @command.run([], context[:opts]) + # the output is long and its values are environment-specific, + # so we simply assert that it is non-empty. MK. + assert length(res) > 0 + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/schema_info_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/schema_info_command_test.exs new file mode 100644 index 0000000000..369592522a --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/schema_info_command_test.exs @@ -0,0 +1,69 @@ +defmodule SchemaInfoCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.SchemaInfoCommand + @default_timeout :infinity + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + { + :ok, + opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || @default_timeout + } + } + end + + test "merge_defaults: adds all keys if none specificed", context do + default_keys = ~w(name cookie active_replicas user_properties) + + {keys, _} = @command.merge_defaults([], context[:opts]) + assert default_keys == keys + end + + test "merge_defaults: includes table headers by default", _context do + {_, opts} = @command.merge_defaults([], %{}) + assert opts[:table_headers] + end + + test "validate: returns bad_info_key on a single bad arg", context do + assert @command.validate(["quack"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + end + + test "validate: returns multiple bad args return a list of bad info key values", context do + assert @command.validate(["quack", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink, :quack]}} + end + + test "validate: return bad_info_key on mix of good and bad args", context do + assert @command.validate(["quack", "cookie"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:quack]}} + assert @command.validate(["access_mode", "oink"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + assert @command.validate(["access_mode", "oink", "name"], context[:opts]) == + {:validation_failure, {:bad_info_key, [:oink]}} + end + + @tag test_timeout: 0 + test "run: timeout causes command to return badrpc", context do + assert run_command_to_list(@command, [["source_name"], context[:opts]]) == + {:badrpc, :timeout} + end + + test "run: can filter info keys", context do + wanted_keys = ~w(name access_mode) + assert match?([[name: _, access_mode: _] | _], run_command_to_list(@command, [wanted_keys, context[:opts]])) + end + + test "banner" do + assert String.starts_with?(@command.banner([], %{node: "node@node"}), "Asking node") + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/server_version_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/server_version_command_test.exs new file mode 100644 index 0000000000..72c32e32f1 --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/server_version_command_test.exs @@ -0,0 +1,48 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule ServerVersionCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.ServerVersionCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run: returns RabbitMQ version on the target node", context do + res = @command.run([], context[:opts]) + assert is_bitstring(res) + end +end diff --git a/deps/rabbitmq_cli/test/diagnostics/tls_versions_command_test.exs b/deps/rabbitmq_cli/test/diagnostics/tls_versions_command_test.exs new file mode 100644 index 0000000000..0e38a0461e --- /dev/null +++ b/deps/rabbitmq_cli/test/diagnostics/tls_versions_command_test.exs @@ -0,0 +1,60 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule TlsVersionsCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Diagnostics.Commands.TlsVersionsCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "merge_defaults: is a no-op" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))) + end + + test "run when formatter is set to JSON: return a document with a list of supported TLS versions", context do + m = @command.run([], Map.merge(context[:opts], %{formatter: "json"})) |> Map.new + xs = Map.get(m, :available) + + # assert that we have a list and tlsv1.2 is included + assert length(xs) > 0 + assert Enum.member?(xs, :"tlsv1.2") + end + + test "run and output: return a list of supported TLS versions", context do + m = @command.run([], context[:opts]) + {:ok, res} = @command.output(m, context[:opts]) + + # assert that we have a list and tlsv1.2 is included + assert length(res) > 0 + assert Enum.member?(res, :"tlsv1.2") + end +end diff --git a/deps/rabbitmq_cli/test/fixtures/files/definitions.json b/deps/rabbitmq_cli/test/fixtures/files/definitions.json new file mode 100644 index 0000000000..1391870028 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/definitions.json @@ -0,0 +1,40 @@ +{ + "rabbit_version": "3.7.21", + "vhosts": [ + { + "name": "\/" + } + ], + "queues": [ + + ], + "exchanges": [ + { + "name": "project.topic.default", + "vhost": "\/", + "type": "topic", + "durable": true, + "auto_delete": false, + "internal": false, + "arguments": { + + } + } + ], + "bindings": [ + + ], + + "parameters": [ + { + "component": "federation-upstream", + "name": "up-1", + "value": { + "ack-mode": "on-confirm", + "trust-user-id": false, + "uri": "amqp://127.0.0.1:5672" + }, + "vhost": "/" + } + ] +} diff --git a/deps/rabbitmq_cli/test/fixtures/files/empty_pidfile.pid b/deps/rabbitmq_cli/test/fixtures/files/empty_pidfile.pid new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/empty_pidfile.pid diff --git a/deps/rabbitmq_cli/test/fixtures/files/invalid_erl_expression.escript b/deps/rabbitmq_cli/test/fixtures/files/invalid_erl_expression.escript new file mode 100644 index 0000000000..6802266390 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/invalid_erl_expression.escript @@ -0,0 +1 @@ +1 + . $$$ ///\\\ diff --git a/deps/rabbitmq_cli/test/fixtures/files/invalid_pidfile.pid b/deps/rabbitmq_cli/test/fixtures/files/invalid_pidfile.pid new file mode 100644 index 0000000000..a4fa4cf4e4 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/invalid_pidfile.pid @@ -0,0 +1 @@ +invalid///& diff --git a/deps/rabbitmq_cli/test/fixtures/files/loaded_applications.escript b/deps/rabbitmq_cli/test/fixtures/files/loaded_applications.escript new file mode 100644 index 0000000000..6db0ab52d3 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/loaded_applications.escript @@ -0,0 +1 @@ +application:loaded_applications(). diff --git a/deps/rabbitmq_cli/test/fixtures/files/valid_erl_expression.escript b/deps/rabbitmq_cli/test/fixtures/files/valid_erl_expression.escript new file mode 100644 index 0000000000..fb18c2507a --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/valid_erl_expression.escript @@ -0,0 +1 @@ +1 + 1. diff --git a/deps/rabbitmq_cli/test/fixtures/files/valid_pidfile.pid b/deps/rabbitmq_cli/test/fixtures/files/valid_pidfile.pid new file mode 100644 index 0000000000..8b64142ea1 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/valid_pidfile.pid @@ -0,0 +1 @@ +13566 diff --git a/deps/rabbitmq_cli/test/fixtures/files/valid_pidfile_with_spaces.pid b/deps/rabbitmq_cli/test/fixtures/files/valid_pidfile_with_spaces.pid new file mode 100644 index 0000000000..83a97170f4 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/files/valid_pidfile_with_spaces.pid @@ -0,0 +1 @@ + 83777 diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/.gitignore b/deps/rabbitmq_cli/test/fixtures/plugins/.gitignore new file mode 100644 index 0000000000..8f884eb3ab --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/.gitignore @@ -0,0 +1 @@ +!*.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_01-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_01-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..8cbe118971 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_01-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_01-0.2.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_01-0.2.0.ez Binary files differnew file mode 100644 index 0000000000..57e93ba7a0 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_01-0.2.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_02-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_02-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..c9cbef855c --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-01/mock_rabbitmq_plugins_02-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-02/mock_rabbitmq_plugins_02-0.2.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-02/mock_rabbitmq_plugins_02-0.2.0.ez Binary files differnew file mode 100644 index 0000000000..c68a17d33b --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-02/mock_rabbitmq_plugins_02-0.2.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-03/mock_rabbitmq_plugins_03-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-03/mock_rabbitmq_plugins_03-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..448518b9d6 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-03/mock_rabbitmq_plugins_03-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-04/mock_rabbitmq_plugins_04.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-04/mock_rabbitmq_plugins_04.ez Binary files differnew file mode 100644 index 0000000000..8d48fe534a --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins-subdirectory-04/mock_rabbitmq_plugins_04.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_7-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_7-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..1596be2d90 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_7-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..d5d32bd490 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugin_for_3_8.app b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugin_for_3_8.app new file mode 100644 index 0000000000..dae70550b6 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugin_for_3_8.app @@ -0,0 +1,10 @@ +{application, mock_rabbitmq_plugin_for_3_8, [ + {description, "New project"}, + {vsn, "0.1.0"}, + {modules, ['mock_rabbitmq_plugins_01_app','mock_rabbitmq_plugins_01_sup']}, + {registered, [mock_rabbitmq_plugins_01_sup]}, + {applications, [kernel,stdlib,rabbit]}, + {mod, {mock_rabbitmq_plugins_01_app, []}}, + {env, []}, + {broker_version_requirements, ["3.8.0", "3.9.0"]} +]}. diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugins_01_app.beam b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugins_01_app.beam Binary files differnew file mode 100644 index 0000000000..903e1c3f6e --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugins_01_app.beam diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugins_01_sup.beam b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugins_01_sup.beam Binary files differnew file mode 100644 index 0000000000..7d6dd6820c --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugin_for_3_8-0.1.0/ebin/mock_rabbitmq_plugins_01_sup.beam diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugins_01-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugins_01-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..6eacd2cd1e --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugins_01-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugins_02-0.1.0.ez b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugins_02-0.1.0.ez Binary files differnew file mode 100644 index 0000000000..1c085e6104 --- /dev/null +++ b/deps/rabbitmq_cli/test/fixtures/plugins/plugins_with_version_requirements/mock_rabbitmq_plugins_02-0.1.0.ez diff --git a/deps/rabbitmq_cli/test/json_formatting.exs b/deps/rabbitmq_cli/test/json_formatting.exs new file mode 100644 index 0000000000..c0e35e2ad3 --- /dev/null +++ b/deps/rabbitmq_cli/test/json_formatting.exs @@ -0,0 +1,59 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule JSONFormattingTest do + use ExUnit.Case, async: false + import ExUnit.CaptureIO + import RabbitMQ.CLI.Core.ExitCodes + import TestHelper + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + set_scope(:all) + + :ok + end + + test "JSON output of status" do + set_scope(:ctl) + + node = to_string(get_rabbit_hostname()) + command = ["status", "-n", node, "--formatter=json"] + output = capture_io(:stdio, fn -> + error_check(command, exit_ok()) + end) + {:ok, doc} = JSON.decode(output) + + assert Map.has_key?(doc, "memory") + assert Map.has_key?(doc, "file_descriptors") + assert Map.has_key?(doc, "listeners") + assert Map.has_key?(doc, "processes") + assert Map.has_key?(doc, "os") + assert Map.has_key?(doc, "pid") + assert Map.has_key?(doc, "rabbitmq_version") + + assert doc["alarms"] == [] + end + + test "JSON output of cluster_status" do + set_scope(:ctl) + + node = to_string(get_rabbit_hostname()) + command = ["cluster_status", "-n", node, "--formatter=json"] + output = capture_io(:stdio, fn -> + error_check(command, exit_ok()) + end) + {:ok, doc} = JSON.decode(output) + + assert Enum.member?(doc["disk_nodes"], node) + assert Map.has_key?(doc["listeners"], node) + assert Map.has_key?(doc["versions"], node) + assert doc["alarms"] == [] + assert doc["partitions"] == %{} + end +end diff --git a/deps/rabbitmq_cli/test/plugins/directories_command_test.exs b/deps/rabbitmq_cli/test/plugins/directories_command_test.exs new file mode 100644 index 0000000000..cae418717a --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/directories_command_test.exs @@ -0,0 +1,103 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule DirectoriesCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Plugins.Commands.DirectoriesCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + {:ok, plugins_expand_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_expand_dir]) + + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + + {:ok, opts: %{ + plugins_file: plugins_file, + plugins_dir: plugins_dir, + plugins_expand_dir: plugins_expand_dir, + rabbitmq_home: rabbitmq_home, + }} + end + + setup context do + { + :ok, + opts: Map.merge(context[:opts], %{ + node: get_rabbit_hostname(), + timeout: 1000 + }) + } + end + + test "validate: providing no arguments passes validation", context do + assert @command.validate([], context[:opts]) == :ok + end + + test "validate: providing --online passes validation", context do + assert @command.validate([], Map.merge(%{online: true}, context[:opts])) == :ok + end + + test "validate: providing --offline passes validation", context do + assert @command.validate([], Map.merge(%{offline: true}, context[:opts])) == :ok + end + + test "validate: providing any arguments fails validation", context do + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate: setting both --online and --offline to false fails validation", context do + assert @command.validate([], Map.merge(context[:opts], %{online: false, offline: false})) == + {:validation_failure, {:bad_argument, "Cannot set online and offline to false"}} + end + + test "validate: setting both --online and --offline to true fails validation", context do + assert @command.validate([], Map.merge(context[:opts], %{online: true, offline: true})) == + {:validation_failure, {:bad_argument, "Cannot set both online and offline"}} + end + + test "validate_execution_environment: when --offline is used, specifying a non-existent enabled_plugins_file passes validation", context do + opts = context[:opts] |> Map.merge(%{offline: true, enabled_plugins_file: "none"}) + assert @command.validate_execution_environment([], opts) == :ok + end + + test "validate_execution_environment: when --offline is used, specifying a non-existent plugins_dir fails validation", context do + opts = context[:opts] |> Map.merge(%{offline: true, plugins_dir: "none"}) + assert @command.validate_execution_environment([], opts) == {:validation_failure, :plugins_dir_does_not_exist} + end + + test "validate_execution_environment: when --online is used, specifying a non-existent enabled_plugins_file passes validation", context do + opts = context[:opts] |> Map.merge(%{online: true, enabled_plugins_file: "none"}) + assert @command.validate_execution_environment([], opts) == :ok + end + + test "validate_execution_environment: when --online is used, specifying a non-existent plugins_dir passes validation", context do + opts = context[:opts] |> Map.merge(%{online: true, plugins_dir: "none"}) + assert @command.validate_execution_environment([], opts) == :ok + end + + + test "run: when --online is used, lists plugin directories", context do + opts = Map.merge(context[:opts], %{online: true}) + dirs = %{plugins_dir: to_string(Map.get(opts, :plugins_dir)), + plugins_expand_dir: to_string(Map.get(opts, :plugins_expand_dir)), + enabled_plugins_file: to_string(Map.get(opts, :plugins_file))} + + assert @command.run([], opts) == {:ok, dirs} + end +end diff --git a/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs new file mode 100644 index 0000000000..dc5d92e090 --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/disable_plugins_command_test.exs @@ -0,0 +1,187 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule DisablePluginsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + alias RabbitMQ.CLI.Core.ExitCodes + + @command RabbitMQ.CLI.Plugins.Commands.DisableCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + + IO.puts("plugins disable tests default env: enabled plugins = #{plugins_file}, plugins dir = #{plugins_dir}, RabbitMQ home directory = #{rabbitmq_home}") + + {:ok, [enabled_plugins]} = :file.consult(plugins_file) + IO.puts("plugins disable tests will assume tnat #{Enum.join(enabled_plugins, ",")} is the list of enabled plugins to revert to") + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home, + online: false, offline: false, + all: false} + + on_exit(fn -> + set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(), opts) + end) + + {:ok, opts: opts} + end + + setup context do + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], + :online, + get_rabbit_hostname(), + context[:opts]) + + + { + :ok, + opts: Map.merge(context[:opts], %{ + node: get_rabbit_hostname(), + timeout: 1000 + }) + } + end + + test "validate: specifying both --online and --offline is reported as invalid", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["a"], Map.merge(context[:opts], %{online: true, offline: true})) + ) + end + + test "validate: not specifying plugins to enable is reported as invalid", context do + assert match?( + {:validation_failure, :not_enough_args}, + @command.validate([], Map.merge(context[:opts], %{online: true, offline: false})) + ) + end + + test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine", context do + assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{enabled_plugins_file: "none"})) == :ok + end + + test "validate_execution_environment: specifying a non-existent plugins_dir is reported as an error", context do + assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) == + {:validation_failure, :plugins_dir_does_not_exist} + end + + test "node is inaccessible, writes out enabled plugins file and returns implicitly enabled plugin list", context do + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{node: :nonode})) + assert [[:rabbitmq_federation], + %{mode: :offline, disabled: [:rabbitmq_stomp], set: [:rabbitmq_federation]}] == + Enum.to_list(test_stream) + assert {:ok, [[:rabbitmq_federation]]} == :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] == + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "in offline mode, writes out enabled plugins and reports implicitly enabled plugin list", context do + assert {:stream, test_stream} = @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[:rabbitmq_federation], + %{mode: :offline, disabled: [:rabbitmq_stomp], set: [:rabbitmq_federation]}] == Enum.to_list(test_stream) + assert {:ok, [[:rabbitmq_federation]]} == :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] == + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "in offline mode , removes implicitly enabled plugins when last explicitly enabled one is removed", context do + assert {:stream, test_stream0} = + @command.run(["rabbitmq_federation"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[:rabbitmq_stomp], + %{mode: :offline, disabled: [:rabbitmq_federation], set: [:rabbitmq_stomp]}] == Enum.to_list(test_stream0) + assert {:ok, [[:rabbitmq_stomp]]} == :file.consult(context[:opts][:enabled_plugins_file]) + + assert {:stream, test_stream1} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[], + %{mode: :offline, disabled: [:rabbitmq_stomp], set: []}] == + Enum.to_list(test_stream1) + assert {:ok, [[]]} = :file.consult(context[:opts][:enabled_plugins_file]) + end + + test "updates plugin list and stops disabled plugins", context do + assert {:stream, test_stream0} = + @command.run(["rabbitmq_stomp"], context[:opts]) + assert [[:rabbitmq_federation], + %{mode: :online, + started: [], stopped: [:rabbitmq_stomp], + disabled: [:rabbitmq_stomp], + set: [:rabbitmq_federation]}] == + Enum.to_list(test_stream0) + assert {:ok, [[:rabbitmq_federation]]} == :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation] == + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + + assert {:stream, test_stream1} = + @command.run(["rabbitmq_federation"], context[:opts]) + assert [[], + %{mode: :online, + started: [], stopped: [:rabbitmq_federation], + disabled: [:rabbitmq_federation], + set: []}] == + Enum.to_list(test_stream1) + assert {:ok, [[]]} == :file.consult(context[:opts][:enabled_plugins_file]) + assert Enum.empty?(Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, []))) + end + + test "can disable multiple plugins at once", context do + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp", "rabbitmq_federation"], context[:opts]) + assert [[], + %{mode: :online, + started: [], stopped: [:rabbitmq_federation, :rabbitmq_stomp], + disabled: [:rabbitmq_federation, :rabbitmq_stomp], + set: []}] == + Enum.to_list(test_stream) + assert {:ok, [[]]} == :file.consult(context[:opts][:enabled_plugins_file]) + assert Enum.empty?(Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, []))) + end + + test "disabling a dependency disables all plugins that depend on it", context do + assert {:stream, test_stream} = @command.run(["amqp_client"], context[:opts]) + assert [[], + %{mode: :online, + started: [], stopped: [:rabbitmq_federation, :rabbitmq_stomp], + disabled: [:rabbitmq_federation, :rabbitmq_stomp], + set: []}] == + Enum.to_list(test_stream) + assert {:ok, [[]]} == :file.consult(context[:opts][:enabled_plugins_file]) + assert Enum.empty?(Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, []))) + end + + test "formats enabled plugins mismatch errors", context do + err = {:enabled_plugins_mismatch, '/tmp/a/cli/path', '/tmp/a/server/path'} + assert {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at /tmp/a/cli/path: target node #{context[:opts][:node]} uses a different path (/tmp/a/server/path)"} + == @command.output({:error, err}, context[:opts]) + end + + test "formats enabled plugins write errors", context do + err1 = {:cannot_write_enabled_plugins_file, "/tmp/a/path", :eacces} + assert {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at /tmp/a/path: the file does not exist or permission was denied (EACCES)"} == + @command.output({:error, err1}, context[:opts]) + + err2 = {:cannot_write_enabled_plugins_file, "/tmp/a/path", :enoent} + assert {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at /tmp/a/path: the file does not exist (ENOENT)"} == + @command.output({:error, err2}, context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs new file mode 100644 index 0000000000..09aaf38351 --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test.exs @@ -0,0 +1,243 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule EnablePluginsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + alias RabbitMQ.CLI.Core.ExitCodes + + @command RabbitMQ.CLI.Plugins.Commands.EnableCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + + IO.puts("plugins enable tests default env: enabled plugins = #{plugins_file}, plugins dir = #{plugins_dir}, RabbitMQ home directory = #{rabbitmq_home}") + + {:ok, [enabled_plugins]} = :file.consult(plugins_file) + IO.puts("plugins enable tests will assume tnat #{Enum.join(enabled_plugins, ",")} is the list of enabled plugins to revert to") + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home, + online: false, offline: false, + all: false} + + on_exit(fn -> + set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(), opts) + end) + + + {:ok, opts: opts} + end + + setup context do + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], + :online, + get_rabbit_hostname(), + context[:opts]) + + { + :ok, + opts: Map.merge(context[:opts], %{ + node: get_rabbit_hostname(), + timeout: 1000 + }) + } + end + + test "validate: specifying both --online and --offline is reported as invalid", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["a"], Map.merge(context[:opts], %{online: true, offline: true})) + ) + end + + test "validate: not specifying any plugins to enable is reported as invalid", context do + assert match?( + {:validation_failure, :not_enough_args}, + @command.validate([], Map.merge(context[:opts], %{online: true, offline: false})) + ) + end + + test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine", context do + assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{enabled_plugins_file: "none"})) == :ok + end + + test "validate_execution_environment: specifying a non-existent plugins_dir is reported as an error", context do + assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) == + {:validation_failure, :plugins_dir_does_not_exist} + end + + test "if node is inaccessible, writes enabled plugins file and reports implicitly enabled plugin list", context do + # Clears enabled plugins file + set_enabled_plugins([], :offline, :nonode, context[:opts]) + + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{node: :nonode})) + assert [[:rabbitmq_stomp], + %{mode: :offline, enabled: [:rabbitmq_stomp], set: [:rabbitmq_stomp]}] == + Enum.to_list(test_stream) + check_plugins_enabled([:rabbitmq_stomp], context) + assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] == + currently_active_plugins(context) + end + + test "in offline mode, writes enabled plugins and reports implicitly enabled plugin list", context do + # Clears enabled plugins file + set_enabled_plugins([], :offline, :nonode, context[:opts]) + + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[:rabbitmq_stomp], + %{mode: :offline, enabled: [:rabbitmq_stomp], set: [:rabbitmq_stomp]}] == + Enum.to_list(test_stream) + check_plugins_enabled([:rabbitmq_stomp], context) + assert_equal_sets( + [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp], + currently_active_plugins(context)) + end + + test "adds additional plugins to those already enabled", context do + # Clears enabled plugins file + set_enabled_plugins([], :offline, :nonode, context[:opts]) + + assert {:stream, test_stream0} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[:rabbitmq_stomp], + %{mode: :offline, enabled: [:rabbitmq_stomp], set: [:rabbitmq_stomp]}] == + Enum.to_list(test_stream0) + check_plugins_enabled([:rabbitmq_stomp], context) + assert {:stream, test_stream1} = + @command.run(["rabbitmq_federation"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[:rabbitmq_federation, :rabbitmq_stomp], + %{mode: :offline, enabled: [:rabbitmq_federation], + set: [:rabbitmq_federation, :rabbitmq_stomp]}] == + Enum.to_list(test_stream1) + check_plugins_enabled([:rabbitmq_stomp, :rabbitmq_federation], context) + end + + test "updates plugin list and starts newly enabled plugins", context do + # Clears enabled plugins file and stop all plugins + set_enabled_plugins([], :online, context[:opts][:node], context[:opts]) + + assert {:stream, test_stream0} = + @command.run(["rabbitmq_stomp"], context[:opts]) + assert [[:rabbitmq_stomp], + %{mode: :online, + started: [:rabbitmq_stomp], stopped: [], + enabled: [:rabbitmq_stomp], + set: [:rabbitmq_stomp]}] == + Enum.to_list(test_stream0) + + check_plugins_enabled([:rabbitmq_stomp], context) + assert_equal_sets([:amqp_client, :rabbitmq_stomp], currently_active_plugins(context)) + + {:stream, test_stream1} = + @command.run(["rabbitmq_federation"], context[:opts]) + assert [[:rabbitmq_federation, :rabbitmq_stomp], + %{mode: :online, + started: [:rabbitmq_federation], stopped: [], + enabled: [:rabbitmq_federation], + set: [:rabbitmq_federation, :rabbitmq_stomp]}] == + Enum.to_list(test_stream1) + + check_plugins_enabled([:rabbitmq_stomp, :rabbitmq_federation], context) + assert_equal_sets([:amqp_client, :rabbitmq_federation, :rabbitmq_stomp], currently_active_plugins(context)) + end + + test "can enable multiple plugins at once", context do + # Clears plugins file and stop all plugins + set_enabled_plugins([], :online, context[:opts][:node], context[:opts]) + + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp", "rabbitmq_federation"], context[:opts]) + assert [[:rabbitmq_federation, :rabbitmq_stomp], + %{mode: :online, + started: [:rabbitmq_federation, :rabbitmq_stomp], stopped: [], + enabled: [:rabbitmq_federation, :rabbitmq_stomp], + set: [:rabbitmq_federation, :rabbitmq_stomp]}] == + Enum.to_list(test_stream) + check_plugins_enabled([:rabbitmq_stomp, :rabbitmq_federation], context) + + assert_equal_sets([:amqp_client, :rabbitmq_federation, :rabbitmq_stomp], currently_active_plugins(context)) + end + + test "does not enable an already implicitly enabled plugin", context do + # Clears enabled plugins file and stop all plugins + set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + assert {:stream, test_stream} = + @command.run(["amqp_client"], context[:opts]) + assert [[:rabbitmq_federation], + %{mode: :online, + started: [], stopped: [], + enabled: [], + set: [:rabbitmq_federation]}] == + Enum.to_list(test_stream) + check_plugins_enabled([:rabbitmq_federation], context) + assert [:amqp_client, :rabbitmq_federation] == + currently_active_plugins(context) + + end + + test "run: does not enable plugins with unmet version requirements", context do + set_enabled_plugins([], :online, context[:opts][:node], context[:opts]) + + plugins_directory = fixture_plugins_path("plugins_with_version_requirements") + opts = get_opts_with_plugins_directories(context, [plugins_directory]) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + + {:stream, _} = + @command.run(["mock_rabbitmq_plugin_for_3_8"], opts) + check_plugins_enabled([:mock_rabbitmq_plugin_for_3_8], context) + + # Not changed + {:error, _version_error} = @command.run(["mock_rabbitmq_plugin_for_3_7"], opts) + check_plugins_enabled([:mock_rabbitmq_plugin_for_3_8], context) + + end + + test "run: does not enable plugins with unmet version requirements even when enabling all plugins", context do + set_enabled_plugins([], :online, context[:opts][:node], context[:opts]) + + plugins_directory = fixture_plugins_path("plugins_with_version_requirements") + opts = get_opts_with_plugins_directories(context, [plugins_directory]) + opts = Map.merge(opts, %{all: true}) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + + {:error, _version_error} = @command.run([], opts) + + check_plugins_enabled([], context) + end + + test "formats enabled plugins mismatch errors", context do + err = {:enabled_plugins_mismatch, '/tmp/a/cli/path', '/tmp/a/server/path'} + assert {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at /tmp/a/cli/path: target node #{context[:opts][:node]} uses a different path (/tmp/a/server/path)"} + == @command.output({:error, err}, context[:opts]) + end + + test "formats enabled plugins write errors", context do + err1 = {:cannot_write_enabled_plugins_file, "/tmp/a/path", :eacces} + assert {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at /tmp/a/path: the file does not exist or permission was denied (EACCES)"} == + @command.output({:error, err1}, context[:opts]) + + err2 = {:cannot_write_enabled_plugins_file, "/tmp/a/path", :enoent} + assert {:error, ExitCodes.exit_dataerr(), + "Could not update enabled plugins file at /tmp/a/path: the file does not exist (ENOENT)"} == + @command.output({:error, err2}, context[:opts]) + end +end diff --git a/deps/rabbitmq_cli/test/plugins/is_enabled_command_test.exs b/deps/rabbitmq_cli/test/plugins/is_enabled_command_test.exs new file mode 100644 index 0000000000..af2900228b --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/is_enabled_command_test.exs @@ -0,0 +1,103 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule PluginIsEnabledCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Plugins.Commands.IsEnabledCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + + {:ok, [enabled_plugins]} = :file.consult(plugins_file) + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home, + online: false, offline: false} + + on_exit(fn -> + set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(), opts) + end) + + + {:ok, opts: opts} + end + + setup context do + { + :ok, + opts: Map.merge(context[:opts], %{ + node: get_rabbit_hostname(), + timeout: 1000 + }) + } + end + + + + test "validate: specifying both --online and --offline is reported as invalid", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate(["rabbitmq_stomp"], Map.merge(context[:opts], %{online: true, offline: true})) + ) + end + + test "validate: not specifying any plugins to check is reported as invalid", context do + opts = context[:opts] |> Map.merge(%{online: true, offline: false}) + assert match?({:validation_failure, :not_enough_args}, @command.validate([], opts)) + end + + test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine", context do + assert @command.validate_execution_environment(["rabbitmq_stomp"], + Map.merge(context[:opts], %{online: false, + offline: true, + enabled_plugins_file: "none"})) == :ok + end + + test "validate_execution_environment: specifying a non-existent plugins_dir is reported as an error", context do + opts = context[:opts] |> Map.merge(%{online: false, + offline: true, + plugins_dir: "none"}) + + assert @command.validate_execution_environment(["rabbitmq_stomp"], opts) == + {:validation_failure, :plugins_dir_does_not_exist} + end + + test "run: when given a single enabled plugin, reports it as such", context do + opts = context[:opts] |> Map.merge(%{online: true, offline: false}) + assert match?({:ok, _}, + assert @command.run(["rabbitmq_stomp"], opts)) + end + + test "run: when given a list of actually enabled plugins, reports them as such", context do + opts = context[:opts] |> Map.merge(%{online: true, offline: false}) + assert match?({:ok, _}, + assert @command.run(["rabbitmq_stomp", "rabbitmq_federation"], opts)) + end + + test "run: when given a list of non-existent plugins, reports them as not enabled", context do + opts = context[:opts] |> Map.merge(%{online: true, offline: false}) + assert match?({:error, _}, + assert @command.run(["rabbitmq_xyz", "abc_xyz"], opts)) + end + + test "run: when given a list with one non-existent plugin, reports the group as not [all] enabled", context do + opts = context[:opts] |> Map.merge(%{online: true, offline: false}) + assert match?({:error, _}, + assert @command.run(["rabbitmq_stomp", "abc_xyz"], opts)) + end +end diff --git a/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs new file mode 100644 index 0000000000..33d9420435 --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs @@ -0,0 +1,235 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ListPluginsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Plugins.Commands.ListCommand + + def reset_enabled_plugins_to_preconfigured_defaults(context) do + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], + :online, + get_rabbit_hostname(), context[:opts]) + end + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + IO.puts("plugins list tests default env: enabled plugins = #{plugins_file}, plugins dir = #{plugins_dir}, RabbitMQ home directory = #{rabbitmq_home}") + + {:ok, [enabled_plugins]} = :file.consult(plugins_file) + IO.puts("plugins list tests will assume tnat #{Enum.join(enabled_plugins, ",")} is the list of enabled plugins to revert to") + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home, + minimal: false, verbose: false, + enabled: false, implicitly_enabled: false} + + on_exit(fn -> + set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(), opts) + end) + + + {:ok, opts: opts} + end + + setup context do + reset_enabled_plugins_to_preconfigured_defaults(context) + + { + :ok, + opts: Map.merge(context[:opts], %{ + node: get_rabbit_hostname(), + timeout: 1000 + }) + } + end + + test "validate: specifying both --minimal and --verbose is reported as invalid", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate([], Map.merge(context[:opts], %{minimal: true, verbose: true})) + ) + end + + test "validate: specifying multiple patterns is reported as an error", context do + assert @command.validate(["a", "b", "c"], context[:opts]) == + {:validation_failure, :too_many_args} + end + + test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine", context do + assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{enabled_plugins_file: "none"})) == :ok + end + + test "validate_execution_environment: specifying non existent plugins_dir is reported as an error", context do + assert @command.validate_execution_environment(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) == + {:validation_failure, :plugins_dir_does_not_exist} + end + + test "will report list of plugins from file for stopped node", context do + node = context[:opts][:node] + :ok = :rabbit_misc.rpc_call(node, :application, :stop, [:rabbitmq_stomp]) + on_exit(fn -> + :rabbit_misc.rpc_call(node, :application, :start, [:rabbitmq_stomp]) + end) + assert %{status: :node_down, + plugins: [%{name: :rabbitmq_federation, enabled: :enabled, running: false}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: false}]} = + @command.run([".*"], Map.merge(context[:opts], %{node: :nonode})) + end + + test "will report list of started plugins for started node", context do + node = context[:opts][:node] + :ok = :rabbit_misc.rpc_call(node, :application, :stop, [:rabbitmq_stomp]) + on_exit(fn -> + :rabbit_misc.rpc_call(node, :application, :start, [:rabbitmq_stomp]) + end) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation, enabled: :enabled, running: true}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: false}]} = + @command.run([".*"], context[:opts]) + end + + test "will report description and dependencies for verbose mode", context do + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation, enabled: :enabled, running: true, description: _, dependencies: [:amqp_client]}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: true, description: _, dependencies: [:amqp_client]}]} = + @command.run([".*"], Map.merge(context[:opts], %{verbose: true})) + end + + test "will report plugin names in minimal mode", context do + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation}, %{name: :rabbitmq_stomp}]} = + @command.run([".*"], Map.merge(context[:opts], %{minimal: true})) + end + + test "by default lists all plugins", context do + set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + on_exit(fn -> + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + end) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation, enabled: :enabled, running: true}, + %{name: :rabbitmq_stomp, enabled: :not_enabled, running: false}]} = + @command.run([".*"], context[:opts]) + end + + test "with enabled flag lists only explicitly enabled plugins", context do + set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + on_exit(fn -> + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + end) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation, enabled: :enabled, running: true}]} = + @command.run([".*"], Map.merge(context[:opts], %{enabled: true})) + end + + test "with implicitly_enabled flag lists explicitly and implicitly enabled plugins", context do + set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + on_exit(fn -> + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + end) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation, enabled: :enabled, running: true}]} = + @command.run([".*"], Map.merge(context[:opts], %{implicitly_enabled: true})) + end + + test "will filter plugins by name with pattern provided", context do + set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + on_exit(fn -> + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], :online, context[:opts][:node], context[:opts]) + end) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation}]} = + @command.run(["fede"], Map.merge(context[:opts], %{minimal: true})) + assert %{status: :running, + plugins: [%{name: :rabbitmq_stomp}]} = + @command.run(["stomp$"], Map.merge(context[:opts], %{minimal: true})) + end + + test "validate: validation is OK when we use multiple plugins directories, one of them does not exist", context do + opts = get_opts_with_non_existing_plugins_directory(context) + assert @command.validate([], opts) == :ok + end + + test "validate: validation is OK when we use multiple plugins directories, directories do exist", context do + opts = get_opts_with_existing_plugins_directory(context) + assert @command.validate([], opts) == :ok + end + + test "should succeed when using multiple plugins directories, one of them does not exist", context do + opts = get_opts_with_non_existing_plugins_directory(context) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation}, %{name: :rabbitmq_stomp}]} = + @command.run([".*"], Map.merge(opts, %{minimal: true})) + end + + + test "should succeed when using multiple plugins directories, directories do exist and do contain plugins", context do + opts = get_opts_with_existing_plugins_directory(context) + assert %{status: :running, + plugins: [%{name: :rabbitmq_federation}, %{name: :rabbitmq_stomp}]} = + @command.run([".*"], Map.merge(opts, %{minimal: true})) + end + + test "should list plugins when using multiple plugins directories", context do + plugins_directory = fixture_plugins_path("plugins-subdirectory-01") + opts = get_opts_with_plugins_directories(context, [plugins_directory]) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + reset_enabled_plugins_to_preconfigured_defaults(context) + assert %{status: :running, + plugins: [%{name: :mock_rabbitmq_plugins_01}, %{name: :mock_rabbitmq_plugins_02}, + %{name: :rabbitmq_federation}, %{name: :rabbitmq_stomp}]} = + @command.run([".*"], Map.merge(opts, %{minimal: true})) + end + + test "will report list of plugins with latest version picked", context do + plugins_directory_01 = fixture_plugins_path("plugins-subdirectory-01") + plugins_directory_02 = fixture_plugins_path("plugins-subdirectory-02") + opts = get_opts_with_plugins_directories(context, [plugins_directory_01, plugins_directory_02]) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + reset_enabled_plugins_to_preconfigured_defaults(context) + assert %{status: :running, + plugins: [%{name: :mock_rabbitmq_plugins_01, enabled: :not_enabled, running: false, version: '0.2.0'}, + %{name: :mock_rabbitmq_plugins_02, enabled: :not_enabled, running: false, version: '0.2.0'}, + %{name: :rabbitmq_federation, enabled: :enabled, running: true}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: true}]} = + @command.run([".*"], opts) + end + + test "will report both running and pending upgrade versions", context do + plugins_directory_01 = fixture_plugins_path("plugins-subdirectory-01") + plugins_directory_02 = fixture_plugins_path("plugins-subdirectory-02") + opts = get_opts_with_plugins_directories(context, [plugins_directory_01]) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + set_enabled_plugins([:mock_rabbitmq_plugins_02, :rabbitmq_federation, :rabbitmq_stomp], + :online, get_rabbit_hostname(), opts) + assert %{status: :running, + plugins: [%{name: :mock_rabbitmq_plugins_01, enabled: :not_enabled, running: false, version: '0.2.0'}, + %{name: :mock_rabbitmq_plugins_02, enabled: :enabled, running: true, version: '0.1.0', running_version: '0.1.0'}, + %{name: :rabbitmq_federation, enabled: :enabled, running: true}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: true}]} = + @command.run([".*"], opts) + opts = get_opts_with_plugins_directories(context, [plugins_directory_01, plugins_directory_02]) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + assert %{status: :running, + plugins: [%{name: :mock_rabbitmq_plugins_01, enabled: :not_enabled, running: false, version: '0.2.0'}, + %{name: :mock_rabbitmq_plugins_02, enabled: :enabled, running: true, version: '0.2.0', running_version: '0.1.0'}, + %{name: :rabbitmq_federation, enabled: :enabled, running: true}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: true}]} = + @command.run([".*"], opts) + end +end diff --git a/deps/rabbitmq_cli/test/plugins/plugins_formatter_test.exs b/deps/rabbitmq_cli/test/plugins/plugins_formatter_test.exs new file mode 100644 index 0000000000..9bf185d7e0 --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/plugins_formatter_test.exs @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule PluginsFormatterTest do + use ExUnit.Case, async: false + + @formatter RabbitMQ.CLI.Formatters.Plugins + + test "format_output with --silent and --minimal" do + result = @formatter.format_output( + %{status: :running, + plugins: [%{name: :amqp_client, enabled: :implicit, running: true, version: '3.7.0', running_version: nil}, + %{name: :mock_rabbitmq_plugins_01, enabled: :not_enabled, running: false, version: '0.2.0', running_version: nil}, + %{name: :mock_rabbitmq_plugins_02, enabled: :enabled, running: true, version: '0.2.0', running_version: '0.1.0'}, + %{name: :rabbitmq_federation, enabled: :enabled, running: true, version: '3.7.0', running_version: nil}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: true, version: '3.7.0', running_version: nil}], + format: :minimal}, %{node: "rabbit@localhost", silent: true}) + assert result == ["amqp_client", + "mock_rabbitmq_plugins_01", + "mock_rabbitmq_plugins_02", + "rabbitmq_federation", + "rabbitmq_stomp"] + end + + test "format_output pending upgrade version message" do + result = @formatter.format_output( + %{status: :running, + plugins: [%{name: :amqp_client, enabled: :implicit, running: true, version: '3.7.0', running_version: nil}, + %{name: :mock_rabbitmq_plugins_01, enabled: :not_enabled, running: false, version: '0.2.0', running_version: nil}, + %{name: :mock_rabbitmq_plugins_02, enabled: :enabled, running: true, version: '0.2.0', running_version: '0.1.0'}, + %{name: :rabbitmq_federation, enabled: :enabled, running: true, version: '3.7.0', running_version: nil}, + %{name: :rabbitmq_stomp, enabled: :enabled, running: true, version: '3.7.0', running_version: nil}], + format: :normal}, %{node: "rabbit@localhost"}) + assert result == [" Configured: E = explicitly enabled; e = implicitly enabled", + " | Status: * = running on rabbit@localhost", " |/", + "[e*] amqp_client 3.7.0", "[ ] mock_rabbitmq_plugins_01 0.2.0", + "[E*] mock_rabbitmq_plugins_02 0.1.0 (pending upgrade to 0.2.0)", + "[E*] rabbitmq_federation 3.7.0", "[E*] rabbitmq_stomp 3.7.0"] + end + +end diff --git a/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs new file mode 100644 index 0000000000..3ebc3dfc98 --- /dev/null +++ b/deps/rabbitmq_cli/test/plugins/set_plugins_command_test.exs @@ -0,0 +1,157 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule SetPluginsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Plugins.Commands.SetCommand + + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + node = get_rabbit_hostname() + + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + + {:ok, [enabled_plugins]} = :file.consult(plugins_file) + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home, + online: false, offline: false} + + on_exit(fn -> + set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(),opts) + end) + + {:ok, opts: opts} + end + + setup context do + + set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation], + :online, + get_rabbit_hostname(), + context[:opts]) + + { + :ok, + opts: Map.merge(context[:opts], %{ + node: get_rabbit_hostname(), + timeout: 1000 + }) + } + end + + test "validate: specifying both --online and --offline is reported as invalid", context do + assert match?( + {:validation_failure, {:bad_argument, _}}, + @command.validate([], Map.merge(context[:opts], %{online: true, offline: true})) + ) + end + + test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine", context do + assert @command.validate_execution_environment([], Map.merge(context[:opts], %{enabled_plugins_file: "none"})) == + :ok + end + + test "validate_execution_environment: specifying non existent plugins_dir is reported as an error", context do + assert @command.validate_execution_environment([], Map.merge(context[:opts], %{plugins_dir: "none"})) == + {:validation_failure, :plugins_dir_does_not_exist} + end + + test "will write enabled plugins file if node is inaccessible and report implicitly enabled list", context do + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{node: :nonode})) + assert [[:rabbitmq_stomp], + %{mode: :offline, set: [:rabbitmq_stomp]}] = + Enum.to_list(test_stream) + assert {:ok, [[:rabbitmq_stomp]]} = :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] = + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "will write enabled plugins in offline mode and report implicitly enabled list", context do + assert {:stream, test_stream} = + @command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false})) + assert [[:rabbitmq_stomp], + %{mode: :offline, set: [:rabbitmq_stomp]}] = + Enum.to_list(test_stream) + assert {:ok, [[:rabbitmq_stomp]]} = :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] = + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "will update list of plugins and start/stop enabled/disabled plugins", context do + assert {:stream, test_stream0} = @command.run(["rabbitmq_stomp"], context[:opts]) + assert [[:rabbitmq_stomp], + %{mode: :online, + started: [], stopped: [:rabbitmq_federation], + set: [:rabbitmq_stomp]}] = + Enum.to_list(test_stream0) + assert {:ok, [[:rabbitmq_stomp]]} = :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_stomp] = + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + assert {:stream, test_stream1} = @command.run(["rabbitmq_federation"], context[:opts]) + assert [[:rabbitmq_federation], + %{mode: :online, + started: [:rabbitmq_federation], stopped: [:rabbitmq_stomp], + set: [:rabbitmq_federation]}] = + Enum.to_list(test_stream1) + assert {:ok, [[:rabbitmq_federation]]} = :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation] = + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "can disable all plugins", context do + assert {:stream, test_stream} = @command.run([], context[:opts]) + assert [[], + %{mode: :online, + started: [], stopped: [:rabbitmq_federation, :rabbitmq_stomp], + set: []}] = + Enum.to_list(test_stream) + assert {:ok, [[]]} = :file.consult(context[:opts][:enabled_plugins_file]) + assert [] = Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "can set multiple plugins", context do + set_enabled_plugins([], :online, get_rabbit_hostname(), context[:opts]) + assert {:stream, test_stream} = + @command.run(["rabbitmq_federation", "rabbitmq_stomp"], context[:opts]) + assert [[:rabbitmq_federation, :rabbitmq_stomp], + %{mode: :online, + started: [:rabbitmq_federation, :rabbitmq_stomp], + stopped: [], + set: [:rabbitmq_federation, :rabbitmq_stomp]}] = + Enum.to_list(test_stream) + assert {:ok, [[:rabbitmq_federation, :rabbitmq_stomp]]} = + :file.consult(context[:opts][:enabled_plugins_file]) + assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] = + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + test "run: does not enable plugins with unmet version requirements", context do + set_enabled_plugins([], :online, context[:opts][:node], context[:opts]) + + plugins_directory = fixture_plugins_path("plugins_with_version_requirements") + opts = get_opts_with_plugins_directories(context, [plugins_directory]) + switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir]) + + + {:stream, _} = @command.run(["mock_rabbitmq_plugin_for_3_8"], opts) + check_plugins_enabled([:mock_rabbitmq_plugin_for_3_8], context) + + {:error, _version_error} = @command.run(["mock_rabbitmq_plugin_for_3_7"], opts) + check_plugins_enabled([:mock_rabbitmq_plugin_for_3_8], context) + end +end diff --git a/deps/rabbitmq_cli/test/queues/add_member_command_test.exs b/deps/rabbitmq_cli/test/queues/add_member_command_test.exs new file mode 100644 index 0000000000..71705ccb2c --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/add_member_command_test.exs @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.AddMemberCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.AddMemberCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a failure" do + assert @command.validate(["quorum-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when three or more arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "rabbit@new-node", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "rabbit@new-node", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats two positional arguments and default switches as a success" do + assert @command.validate(["quorum-queue-a", "rabbit@new-node"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a", "rabbit@new-node"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/check_if_node_is_mirror_sync_critical_command_test.exs b/deps/rabbitmq_cli/test/queues/check_if_node_is_mirror_sync_critical_command_test.exs new file mode 100644 index 0000000000..cbab5a6470 --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/check_if_node_is_mirror_sync_critical_command_test.exs @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.CheckIfNodeIsMirrorSyncCriticalCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.CheckIfNodeIsMirrorSyncCriticalCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "validate: accepts no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + test "validate: any positional arguments fail validation" do + assert @command.validate(["quorum-queue-a"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "two"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "two", "three"], %{}) == {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/check_if_node_is_quorum_critical_command_test.exs b/deps/rabbitmq_cli/test/queues/check_if_node_is_quorum_critical_command_test.exs new file mode 100644 index 0000000000..3a1b8abf34 --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/check_if_node_is_quorum_critical_command_test.exs @@ -0,0 +1,40 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.CheckIfNodeIsQuorumCriticalCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.CheckIfNodeIsQuorumCriticalCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + test "validate: accepts no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + test "validate: any positional arguments fail validation" do + assert @command.validate(["quorum-queue-a"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "two"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "two", "three"], %{}) == {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/delete_member_command_test.exs b/deps/rabbitmq_cli/test/queues/delete_member_command_test.exs new file mode 100644 index 0000000000..6880de6399 --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/delete_member_command_test.exs @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.DeleteMemberCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.DeleteMemberCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a failure" do + assert @command.validate(["quorum-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when three or more arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "rabbit@new-node", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "rabbit@new-node", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats two positional arguments and default switches as a success" do + assert @command.validate(["quorum-queue-a", "rabbit@new-node"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a", "rabbit@new-node"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/grow_command_test.exs b/deps/rabbitmq_cli/test/queues/grow_command_test.exs new file mode 100644 index 0000000000..d0d459065b --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/grow_command_test.exs @@ -0,0 +1,67 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.GrowCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.GrowCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + vhost_pattern: ".*", + queue_pattern: ".*", + errors_only: false + }} + end + + test "merge_defaults: defaults to reporting complete results" do + assert @command.merge_defaults([], %{}) == + {[], %{vhost_pattern: ".*", + queue_pattern: ".*", + errors_only: false}} + end + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a failure" do + assert @command.validate(["quorum-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when a node and even are provided, returns a success" do + assert @command.validate(["quorum-queue-a", "even"], %{}) == :ok + end + + test "validate: when a node and all are provided, returns a success" do + assert @command.validate(["quorum-queue-a", "all"], %{}) == :ok + end + + test "validate: when a node and something else is provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "banana"], %{}) == + {:validation_failure, "strategy 'banana' is not recognised."} + end + + test "validate: when three arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "extra-arg", "another-extra-arg"], %{}) == + {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a", "all"], + Map.merge(context[:opts], %{node: :jake@thedog, timeout: 200}))) + end +end diff --git a/deps/rabbitmq_cli/test/queues/peek_command_test.exs b/deps/rabbitmq_cli/test/queues/peek_command_test.exs new file mode 100644 index 0000000000..567de4f4d2 --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/peek_command_test.exs @@ -0,0 +1,59 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.PeekCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.PeekCommand + @invalid_position {:validation_failure, "position value must be a positive integer"} + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: treats no arguments as a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: treats a single positional argument as a failure" do + assert @command.validate(["quorum-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when two or more arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "1"], %{}) == :ok + assert @command.validate(["quorum-queue-a", "extra-arg", "another-extra-arg"], %{}) == + {:validation_failure, :too_many_args} + end + + test "validate: when position is a negative number, returns a failure" do + assert @command.validate(["quorum-queue-a", "-1"], %{}) == @invalid_position + end + + test "validate: when position is zero, returns a failure" do + assert @command.validate(["quorum-queue-a", "0"], %{}) == @invalid_position + end + + test "validate: when position cannot be parsed to an integer, returns a failure" do + assert @command.validate(["quorum-queue-a", "third"], %{}) == @invalid_position + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a", "1"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/quorum_status_command_test.exs b/deps/rabbitmq_cli/test/queues/quorum_status_command_test.exs new file mode 100644 index 0000000000..ec694db9ba --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/quorum_status_command_test.exs @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.QuorumStatusCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.QuorumStatusCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: treats no arguments as a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: accepts a single positional argument" do + assert @command.validate(["quorum-queue-a"], %{}) == :ok + end + + test "validate: when two or more arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/reclaim_quorum_memory_command_test.exs b/deps/rabbitmq_cli/test/queues/reclaim_quorum_memory_command_test.exs new file mode 100644 index 0000000000..bec7bab50d --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/reclaim_quorum_memory_command_test.exs @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.ReclaimQuorumMemoryCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.ReclaimQuorumMemoryCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: treats no arguments as a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: accepts a single positional argument" do + assert @command.validate(["quorum-queue-a"], %{}) == :ok + end + + test "validate: when two or more arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/queues/shrink_command_test.exs b/deps/rabbitmq_cli/test/queues/shrink_command_test.exs new file mode 100644 index 0000000000..8441deaeb0 --- /dev/null +++ b/deps/rabbitmq_cli/test/queues/shrink_command_test.exs @@ -0,0 +1,55 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Queues.Commands.ShrinkCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Queues.Commands.ShrinkCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000, + errors_only: false + }} + end + + test "merge_defaults: defaults to reporting complete results" do + assert @command.merge_defaults([], %{}) == {[], %{errors_only: false}} + end + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a success" do + assert @command.validate(["quorum-queue-a"], %{}) == :ok + end + + test "validate: when three or more arguments are provided, returns a failure" do + assert @command.validate(["quorum-queue-a", "one-extra-arg"], %{}) == + {:validation_failure, :too_many_args} + assert @command.validate(["quorum-queue-a", "extra-arg", "another-extra-arg"], %{}) == + {:validation_failure, :too_many_args} + end + + test "validate: treats one positional arguments and default switches as a success" do + assert @command.validate(["quorum-queue-a"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + assert match?({:badrpc, _}, @command.run(["quorum-queue-a"], + Map.merge(context[:opts], %{node: :jake@thedog, vhost: "/", timeout: 200}))) + end +end diff --git a/deps/rabbitmq_cli/test/rabbitmqctl_test.exs b/deps/rabbitmq_cli/test/rabbitmqctl_test.exs new file mode 100644 index 0000000000..c6b085daad --- /dev/null +++ b/deps/rabbitmq_cli/test/rabbitmqctl_test.exs @@ -0,0 +1,301 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule RabbitMQCtlTest do + use ExUnit.Case, async: false + import ExUnit.CaptureIO + import RabbitMQ.CLI.Core.ExitCodes + import TestHelper + + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + set_scope(:all) + + :ok + end + + # + # --help and `help [command]` + # + + test "--help option prints help for the command and exits with an OK" do + command = ["status", "--help"] + assert capture_io(fn -> + error_check(command, exit_ok()) + end) =~ ~r/Usage/ + end + + test "bare --help prints general help and exits with an OK" do + command = ["--help"] + assert capture_io(fn -> + error_check(command, exit_ok()) + end) =~ ~r/Usage/ + end + + test "help [command] prints help for the command and exits with an OK" do + command = ["help", "status"] + assert capture_io(fn -> + error_check(command, exit_ok()) + end) =~ ~r/Usage/ + end + + test "bare help command prints general help and exits with an OK" do + command = ["help"] + assert capture_io(fn -> + error_check(command, exit_ok()) + end) =~ ~r/Usage/ + end + + # + # Validation and Error Handling + # + + test "print error message on a bad connection" do + command = ["status", "-n", "sandwich@pastrami"] + assert capture_io(:stderr, fn -> + error_check(command, exit_unavailable()) + end) =~ ~r/unable to perform an operation on node 'sandwich@pastrami'/ + end + + test "when an RPC call times out, prints a timeout message" do + command = ["list_users", "-t", "0"] + assert capture_io(:stderr, fn -> + error_check(command, exit_tempfail()) + end) =~ ~r/Error: operation list_users on node #{get_rabbit_hostname()} timed out. Timeout value used: 0/ + end + + test "when authentication fails, prints an authentication error message" do + add_user "kirk", "khaaaaaan" + command = ["authenticate_user", "kirk", "makeitso"] + assert capture_io(:stderr, + fn -> error_check(command, exit_dataerr()) + end) =~ ~r/Error: failed to authenticate user \"kirk\"/ + delete_user "kirk" + end + + test "when invoked without arguments, displays a generic usage message and exits with a non-zero code" do + command = [] + assert capture_io(:stderr, fn -> + error_check(command, exit_usage()) + end) =~ ~r/usage/i + end + + test "when invoked with only a --help, shows a generic usage message and exits with a success" do + command = ["--help"] + assert capture_io(:stdio, fn -> + error_check(command, exit_ok()) + end) =~ ~r/usage/i + end + + test "when invoked with --help [command], shows a generic usage message and exits with a success" do + command = ["--help", "status"] + assert capture_io(:stdio, fn -> + error_check(command, exit_ok()) + end) =~ ~r/usage/i + end + + test "when no command name is provided, displays usage" do + command = ["-n", "sandwich@pastrami"] + assert capture_io(:stderr, fn -> + error_check(command, exit_usage()) + end) =~ ~r/usage/i + end + + test "short node name without the host part connects properly" do + command = ["status", "-n", "rabbit"] + capture_io(:stderr, fn -> error_check(command, exit_ok()) end) + end + + test "a non-existent command results in help message displayed" do + command = ["not_real"] + assert capture_io(:stderr, fn -> + error_check(command, exit_usage()) + end) =~ ~r/Usage/ + end + + test "a command that's been provided extra arguments exits with a reasonable error code" do + command = ["status", "extra"] + output = capture_io(:stderr, fn -> + error_check(command, exit_usage()) + end) + assert output =~ ~r/too many arguments/ + assert output =~ ~r/Usage/ + assert output =~ ~r/status/ + end + + test "a command that's been provided insufficient arguments exits with a reasonable error code" do + command = ["list_user_permissions"] + output = capture_io(:stderr, fn -> + error_check(command, exit_usage()) + end) + assert output =~ ~r/not enough arguments/ + assert output =~ ~r/Usage/ + assert output =~ ~r/list_user_permissions/ + end + + test "a command that's provided an invalid argument exits a reasonable error" do + command = ["set_disk_free_limit", "2097152bytes"] + capture_io(:stderr, fn -> error_check(command, exit_dataerr()) end) + end + + test "a command that fails with an error exits with a reasonable error code" do + command = ["delete_user", "voldemort"] + capture_io(:stderr, fn -> error_check(command, exit_nouser()) end) + end + + test "a mcommand with an unsupported option as the first command-line arg fails gracefully" do + command1 = ["--invalid=true", "list_permissions", "-p", "/"] + assert capture_io(:stderr, fn -> + error_check(command1, exit_usage()) + end) =~ ~r/Invalid options for this command/ + + command2 = ["--node", "rabbit", "status", "quack"] + assert capture_io(:stderr, fn -> + error_check(command2, exit_usage()) + end) =~ ~r/too many arguments./ + + command3 = ["--node", "rabbit", "add_user"] + assert capture_io(:stderr, fn -> + error_check(command3, exit_usage()) + end) =~ ~r/not enough arguments./ + end + +## ------------------------- Default Flags ------------------------------------ + + test "an empty node option is filled with the default rabbit node" do + assert RabbitMQCtl.merge_all_defaults(%{})[:node] == + TestHelper.get_rabbit_hostname() + end + + test "a non-empty node option is not overwritten" do + assert RabbitMQCtl.merge_all_defaults(%{node: :jake@thedog})[:node] == + :jake@thedog + end + + test "an empty timeout option is set to infinity" do + assert RabbitMQCtl.merge_all_defaults(%{})[:timeout] == :infinity + end + + test "a non-empty timeout option is not overridden" do + assert RabbitMQCtl.merge_all_defaults(%{timeout: 60})[:timeout] == 60 + end + + test "other parameters are not overridden by the default" do + assert RabbitMQCtl.merge_all_defaults(%{vhost: "quack"})[:vhost] == "quack" + end + + test "any flags that aren't global or command-specific cause a bad option" do + command1 = ["status", "--nod=rabbit"] + assert capture_io(:stderr, fn -> + error_check(command1, exit_usage()) + end) =~ ~r/Invalid options for this command/ + + command2 = ["list_permissions", "-o", "/"] + assert capture_io(:stderr, fn -> + error_check(command2, exit_usage()) + end) =~ ~r/Invalid options for this command/ + end + + # + # --auto-complete and `autocomplete [command]` + # + + test "--auto-complete delegates to the autocomplete command" do + # Note: these are not script name (scope) aware without --script-name + # but the actual command invoked in a shell will be + check_output(["--auto-complete", "list_q"], "list_queues\n") + check_output(["--auto-complete", "list_con", "--script-name", "rabbitmq-diagnostics"], "list_connections\nlist_consumers\n") + check_output(["--auto-complete", "--script-name", "rabbitmq-diagnostics", "mem"], "memory_breakdown\n") + check_output(["--auto-complete", "--script-name", "rabbitmq-queues", "add_m"], "add_member\n") + end + + test "autocompletion command used directly" do + # Note: these are not script name (scope) aware without --script-name + # but the actual command invoked in a shell will be + check_output(["autocomplete", "list_q"], "list_queues\n") + check_output(["autocomplete", "list_con", "--script-name", "rabbitmq-diagnostics"], "list_connections\nlist_consumers\n") + check_output(["autocomplete", "--script-name", "rabbitmq-diagnostics", "mem"], "memory_breakdown\n") + check_output(["autocomplete", "--script-name", "rabbitmq-queues", "add_m"], "add_member\n") + end + + defp check_output(cmd, out) do + assert capture_io(fn -> + error_check(cmd, exit_ok()) + end) == out + end + + +## ------------------------- Error formatting --------------------------------- + + test "badrpc nodedown error" do + exit_code = exit_unavailable() + node = :example@node + {:error, ^exit_code, message} = + RabbitMQCtl.handle_command_output( + {:error, {:badrpc, :nodedown}}, + :no_command, %{node: node}, + fn(output, _, _) -> output end) + + assert message =~ ~r/Error: unable to perform an operation on node/ + assert message =~ ~r/DIAGNOSTICS/ + assert message =~ ~r/attempted to contact/ + + localnode = :non_existent_node@localhost + {:error, ^exit_code, message} = + RabbitMQCtl.handle_command_output( + {:error, {:badrpc, :nodedown}}, + :no_command, %{node: localnode}, + fn(output, _, _) -> output end) + assert message =~ ~r/DIAGNOSTICS/ + assert message =~ ~r/attempted to contact/ + assert message =~ ~r/suggestion: start the node/ + end + + test "badrpc timeout error" do + exit_code = exit_tempfail() + timeout = 1000 + nodename = :node@host + err_msg = "Error: operation example on node node@host timed out. Timeout value used: #{timeout}" + {:error, ^exit_code, ^err_msg} = + RabbitMQCtl.handle_command_output( + {:error, {:badrpc, :timeout}}, + ExampleCommand, %{timeout: timeout, node: nodename}, + fn(output, _, _) -> output end) + end + + test "generic error" do + exit_code = exit_unavailable() + {:error, ^exit_code, "Error:\nerror message"} = + RabbitMQCtl.handle_command_output( + {:error, "error message"}, + :no_command, %{}, + fn(output, _, _) -> output end) + end + + test "inspect arbitrary error" do + exit_code = exit_unavailable() + error = %{i: [am: "arbitrary", error: 1]} + inspected = inspect(error) + {:error, ^exit_code, "Error:\n" <> ^inspected} = + RabbitMQCtl.handle_command_output( + {:error, error}, + :no_command, %{}, + fn(output, _, _) -> output end) + end + + test "atom error" do + exit_code = exit_unavailable() + {:error, ^exit_code, "Error:\nerror_message"} = + RabbitMQCtl.handle_command_output( + {:error, :error_message}, + :no_command, %{}, + fn(output, _, _) -> output end) + end + +end diff --git a/deps/rabbitmq_cli/test/streams/add_replica_command_test.exs b/deps/rabbitmq_cli/test/streams/add_replica_command_test.exs new file mode 100644 index 0000000000..cffcd2e34d --- /dev/null +++ b/deps/rabbitmq_cli/test/streams/add_replica_command_test.exs @@ -0,0 +1,57 @@ +## 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 https://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. +## +## Copyright (c) 2012-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Streams.Commands.AddReplicaCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Streams.Commands.AddReplicaCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a failure" do + assert @command.validate(["stream-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when three or more arguments are provided, returns a failure" do + assert @command.validate(["stream-queue-a", "rabbit@new-node", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["stream-queue-a", "rabbit@new-node", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats two positional arguments and default switches as a success" do + assert @command.validate(["stream-queue-a", "rabbit@new-node"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["stream-queue-a", "rabbit@new-node"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/streams/delete_replica_command_test.exs b/deps/rabbitmq_cli/test/streams/delete_replica_command_test.exs new file mode 100644 index 0000000000..cf6bcbe20d --- /dev/null +++ b/deps/rabbitmq_cli/test/streams/delete_replica_command_test.exs @@ -0,0 +1,57 @@ +## 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 https://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. +## +## Copyright (c) 2012-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule RabbitMQ.CLI.Streams.Commands.DeleteReplicaCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Streams.Commands.DeleteReplicaCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a failure" do + assert @command.validate(["stream-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when three or more arguments are provided, returns a failure" do + assert @command.validate(["stream-queue-a", "rabbit@new-node", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["stream-queue-a", "rabbit@new-node", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats two positional arguments and default switches as a success" do + assert @command.validate(["stream-queue-a", "rabbit@new-node"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["stream-queue-a", "rabbit@new-node"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end +end diff --git a/deps/rabbitmq_cli/test/streams/set_stream_retention_policy_command_test.exs b/deps/rabbitmq_cli/test/streams/set_stream_retention_policy_command_test.exs new file mode 100644 index 0000000000..56f960320b --- /dev/null +++ b/deps/rabbitmq_cli/test/streams/set_stream_retention_policy_command_test.exs @@ -0,0 +1,63 @@ +## 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 https://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 Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2020 Pivotal Software, Inc. All rights reserved. + +defmodule RabbitMQ.CLI.Streams.Commands.SetStreamRetentionPolicyCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Streams.Commands.SetStreamRetentionPolicyCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 30000 + }} + end + + + test "validate: when no arguments are provided, returns a failure" do + assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when one argument is provided, returns a failure" do + assert @command.validate(["stream-queue-a"], %{}) == {:validation_failure, :not_enough_args} + end + + test "validate: when three or more arguments are provided, returns a failure" do + assert @command.validate(["stream-queue-a", "1D", "one-extra-arg"], %{}) == {:validation_failure, :too_many_args} + assert @command.validate(["stream-queue-a", "1D", "extra-arg", "another-extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats two positional arguments and default switches as a success" do + assert @command.validate(["stream-queue-a", "2Y"], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc" do + assert match?({:badrpc, _}, @command.run(["stream-queue-a", "1Y"], + %{node: :jake@thedog, vhost: "/", timeout: 200})) + end + + test "run: targeting an unknown queue returns an error", context do + assert match?({:error, _}, @command.run(["stream-queue-a", "1Y"], + Map.merge(context[:opts], %{vhost: "/"}))) + end +end diff --git a/deps/rabbitmq_cli/test/test_helper.exs b/deps/rabbitmq_cli/test/test_helper.exs new file mode 100644 index 0000000000..fca68e57bd --- /dev/null +++ b/deps/rabbitmq_cli/test/test_helper.exs @@ -0,0 +1,620 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +four_hours = 240 * 60 * 1000 +ExUnit.configure( + exclude: [disabled: true], + module_load_timeout: four_hours, + timeout: four_hours) + +ExUnit.start() + +defmodule TestHelper do + import ExUnit.Assertions + alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers + + alias RabbitMQ.CLI.Core.{CommandModules, Config, Helpers, NodeName} + import RabbitMQ.CLI.Core.Platform + + def get_rabbit_hostname(node_name_type \\ :shortnames) do + Helpers.get_rabbit_hostname(node_name_type) + end + + def hostname, do: NodeName.hostname() + + def domain, do: NodeName.domain() + + def fixture_file_path(filename) do + Path.join([File.cwd!(), "test", "fixtures", "files", filename]) + end + + def fixture_plugins_path(plugins_directory) do + Path.join([File.cwd!(), "test", "fixtures", "plugins", plugins_directory]) + end + + def get_cluster_name() do + :rpc.call(get_rabbit_hostname(), :rabbit_nodes, :cluster_name, []) + end + + def add_vhost(name) do + :rpc.call(get_rabbit_hostname(), :rabbit_vhost, :add, [name, "acting-user"]) + end + + def delete_vhost(name) do + # some quick tests create and delete a vhost immediately, resulting + # in a high enough restart intensity in rabbit_vhost_sup_wrapper to + # make the rabbit app terminate. See https://github.com/rabbitmq/rabbitmq-server/issues/1280. + :timer.sleep(250) + :rpc.call(get_rabbit_hostname(), :rabbit_vhost, :delete, [name, "acting-user"]) + end + + def list_vhosts() do + :rpc.call(get_rabbit_hostname(), :rabbit_vhost, :info_all, []) + end + + def enable_feature_flag(feature_flag) do + :rpc.call(get_rabbit_hostname(), :rabbit_feature_flags, :enable, [feature_flag]) + end + + def list_feature_flags(arg) do + :rpc.call(get_rabbit_hostname(), :rabbit_feature_flags, :list, [arg]) + end + + def add_user(name, password) do + :rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :add_user, + [name, password, "acting-user"]) + end + + def delete_user(name) do + :rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :delete_user, + [name, "acting-user"]) + end + + def list_users() do + :rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :list_users, []) + end + + def trace_on(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_trace, :start, [vhost]) + end + + def trace_off(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_trace, :stop, [vhost]) + end + + def set_user_tags(name, tags) do + :rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :set_tags, + [name, tags, "acting-user"]) + end + + def authenticate_user(name, password) do + :rpc.call(get_rabbit_hostname(), :rabbit_access_control,:check_user_pass_login, [name, password]) + end + + def set_parameter(vhost, component_name, key, value) do + :ok = :rpc.call(get_rabbit_hostname(), :rabbit_runtime_parameters, :parse_set, [vhost, component_name, key, value, :nouser]) + end + + def clear_parameter(vhost, component_name, key) do + :rpc.call(get_rabbit_hostname(), :rabbit_runtime_parameters, :clear, [vhost, component_name, key, <<"acting-user">>]) + end + + def list_parameters(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_runtime_parameters, :list_formatted, [vhost]) + end + + def set_global_parameter(key, value) do + :ok = :rpc.call(get_rabbit_hostname(), :rabbit_runtime_parameters, :parse_set_global, + [key, value, "acting-user"]) + end + + def clear_global_parameter(key) do + :rpc.call(get_rabbit_hostname(), :rabbit_runtime_parameters, :clear_global, + [key, "acting-user"]) + end + + def list_global_parameters() do + :rpc.call(get_rabbit_hostname(), :rabbit_runtime_parameters, :list_global_formatted, []) + end + + def set_permissions(user, vhost, [conf, write, read]) do + :rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :set_permissions, [user, vhost, conf, write, read, "acting-user"]) + end + + def list_policies(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_policy, :list_formatted, [vhost]) + end + + def set_policy(vhost, name, pattern, value) do + {:ok, decoded} = :rabbit_json.try_decode(value) + parsed = :maps.to_list(decoded) + :ok = :rpc.call(get_rabbit_hostname(), :rabbit_policy, :set, [vhost, name, pattern, parsed, 0, "all", "acting-user"]) + end + + def clear_policy(vhost, key) do + :rpc.call(get_rabbit_hostname(), :rabbit_policy, :delete, [vhost, key, "acting-user"]) + end + + def list_operator_policies(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_policy, :list_formatted_op, [vhost]) + end + + def set_operator_policy(vhost, name, pattern, value) do + {:ok, decoded} = :rabbit_json.try_decode(value) + parsed = :maps.to_list(decoded) + :ok = :rpc.call(get_rabbit_hostname(), :rabbit_policy, :set_op, [vhost, name, pattern, parsed, 0, "all", "acting-user"]) + end + + def clear_operator_policy(vhost, key) do + :rpc.call(get_rabbit_hostname(), :rabbit_policy, :delete_op, [vhost, key, "acting-user"]) + end + + def declare_queue(name, vhost, durable \\ false, auto_delete \\ false, args \\ [], owner \\ :none) do + queue_name = :rabbit_misc.r(vhost, :queue, name) + :rpc.call(get_rabbit_hostname(), + :rabbit_amqqueue, :declare, + [queue_name, durable, auto_delete, args, owner, "acting-user"]) + end + + def delete_queue(name, vhost) do + queue_name = :rabbit_misc.r(vhost, :queue, name) + :rpc.call(get_rabbit_hostname(), + :rabbit_amqqueue, :delete, + [queue_name, false, false, "acting-user"]) + end + + def lookup_queue(name, vhost) do + queue_name = :rabbit_misc.r(vhost, :queue, name) + :rpc.call(get_rabbit_hostname(), + :rabbit_amqqueue, :lookup, + [queue_name]) + end + + def declare_exchange(name, vhost, type \\ :direct, durable \\ false, auto_delete \\ false, internal \\ false, args \\ []) do + exchange_name = :rabbit_misc.r(vhost, :exchange, name) + :rpc.call(get_rabbit_hostname(), + :rabbit_exchange, :declare, + [exchange_name, type, durable, auto_delete, internal, args, "acting-user"]) + end + + def list_permissions(vhost) do + :rpc.call( + get_rabbit_hostname(), + :rabbit_auth_backend_internal, + :list_vhost_permissions, + [vhost], + :infinity + ) + end + + def set_topic_permissions(user, vhost, exchange, writePerm, readPerm) do + :rpc.call( + get_rabbit_hostname(), + :rabbit_auth_backend_internal, + :set_topic_permissions, + [user, vhost, exchange, writePerm, readPerm, "acting-user"], + :infinity + ) + end + + def list_user_topic_permissions(user) do + :rpc.call( + get_rabbit_hostname(), + :rabbit_auth_backend_internal, + :list_user_topic_permissions, + [user], + :infinity + ) + end + + def clear_topic_permissions(user, vhost) do + :rpc.call( + get_rabbit_hostname(), + :rabbit_auth_backend_internal, + :clear_topic_permissions, + [user, vhost, "acting-user"], + :infinity + ) + end + + def set_vm_memory_high_watermark(limit) do + :rpc.call(get_rabbit_hostname(), :vm_memory_monitor, :set_vm_memory_high_watermark, [limit]) + end + + def set_disk_free_limit(limit) do + :rpc.call(get_rabbit_hostname(), :rabbit_disk_monitor, :set_disk_free_limit, [limit]) + end + + + # + # App lifecycle + # + + def await_rabbitmq_startup() do + await_rabbitmq_startup_with_retries(100) + end + + def await_rabbitmq_startup_with_retries(0) do + throw({:error, "Failed to call rabbit.await_startup/0 with retries: node #{get_rabbit_hostname()} was down"}) + end + def await_rabbitmq_startup_with_retries(retries_left) do + case :rabbit_misc.rpc_call(get_rabbit_hostname(), :rabbit, :await_startup, []) do + :ok -> + :ok + {:badrpc, :nodedown} -> + :timer.sleep(50) + await_rabbitmq_startup_with_retries(retries_left - 1) + end + end + + def await_condition(fun, timeout) do + retries = Integer.floor_div(timeout, 50) + await_condition_with_retries(fun, retries) + end + + def await_condition_with_retries(_fun, 0) do + throw({:error, "Condition did not materialize"}) + end + def await_condition_with_retries(fun, retries_left) do + case fun.() do + true -> :ok + _ -> + :timer.sleep(50) + await_condition_with_retries(fun, retries_left - 1) + end + end + + def is_rabbitmq_app_running() do + :rabbit_misc.rpc_call(get_rabbit_hostname(), :rabbit, :is_booted, []) + end + + def start_rabbitmq_app do + :rabbit_misc.rpc_call(get_rabbit_hostname(), :rabbit, :start, []) + await_rabbitmq_startup() + :timer.sleep(250) + end + + def stop_rabbitmq_app do + :rabbit_misc.rpc_call(get_rabbit_hostname(), :rabbit, :stop, []) + :timer.sleep(1200) + end + + def drain_node() do + :rpc.call(get_rabbit_hostname(), :rabbit_maintenance, :drain, []) + end + + def revive_node() do + :rpc.call(get_rabbit_hostname(), :rabbit_maintenance, :revive, []) + end + + def is_draining_node() do + node = get_rabbit_hostname() + :rpc.call(node, :rabbit_maintenance, :is_being_drained_local_read, [node]) + end + + def status do + :rpc.call(get_rabbit_hostname(), :rabbit, :status, []) + end + + def error_check(cmd_line, code) do + assert catch_exit(RabbitMQCtl.main(cmd_line)) == {:shutdown, code} + end + + def with_channel(vhost, fun) do + with_connection(vhost, + fn(conn) -> + {:ok, chan} = AMQP.Channel.open(conn) + AMQP.Confirm.select(chan) + fun.(chan) + end) + end + + def with_connection(vhost, fun) do + Application.ensure_all_started(:amqp) + {:ok, conn} = AMQP.Connection.open(virtual_host: vhost) + ExUnit.Callbacks.on_exit(fn -> + try do + :amqp_connection.close(conn, 1000) + catch + :exit, _ -> :ok + end + end) + fun.(conn) + end + + def with_connections(vhosts, fun) do + Application.ensure_all_started(:amqp) + conns = for v <- vhosts do + {:ok, conn} = AMQP.Connection.open(virtual_host: v) + conn + end + ExUnit.Callbacks.on_exit(fn -> + try do + for c <- conns, do: :amqp_connection.close(c, 1000) + catch + :exit, _ -> :ok + end + end) + fun.(conns) + end + + def message_count(vhost, queue_name) do + with_channel(vhost, fn(channel) -> + {:ok, %{message_count: mc}} = AMQP.Queue.declare(channel, queue_name) + mc + end) + end + + def publish_messages(vhost, queue_name, count) do + with_channel(vhost, fn(channel) -> + AMQP.Queue.purge(channel, queue_name) + for i <- 1..count do + AMQP.Basic.publish(channel, "", queue_name, + "test_message" <> Integer.to_string(i)) + end + AMQP.Confirm.wait_for_confirms(channel, 30) + end) + end + + def await_no_client_connections(node, timeout) do + iterations = timeout / 10 + await_no_client_connections_with_iterations(node, iterations) + end + + def await_no_client_connections_with_iterations(_node, n) when n <= 0 do + flunk "Ran out of retries, still have active client connections" + end + def await_no_client_connections_with_iterations(node, n) when n > 0 do + case :rpc.call(node, :rabbit_networking, :connections_local, []) do + [] -> :ok + _xs -> + :timer.sleep(10) + await_no_client_connections_with_iterations(node, n - 1) + end + end + + def close_all_connections(node) do + # we intentionally use connections_local/0 here because connections/0, + # the cluster-wide version, loads some bits around cluster membership + # that are not normally ready with a single node. MK. + # + # when/if we decide to test + # this project against a cluster of nodes this will need revisiting. MK. + for pid <- :rpc.call(node, :rabbit_networking, :connections_local, []) do + :rpc.call(node, :rabbit_networking, :close_connection, [pid, :force_closed]) + end + await_no_client_connections(node, 5000) + end + + def expect_client_connection_failure() do + expect_client_connection_failure("/") + end + def expect_client_connection_failure(vhost) do + Application.ensure_all_started(:amqp) + assert {:error, :econnrefused} == AMQP.Connection.open(virtual_host: vhost) + end + + def delete_all_queues() do + try do + immediately_delete_all_queues(:rabbit_amqqueue.list()) + catch + _, _ -> :ok + end + end + + def delete_all_queues(vhost) do + try do + immediately_delete_all_queues(:rabbit_amqqueue.list(vhost)) + catch + _, _ -> :ok + end + end + + defp immediately_delete_all_queues(qs) do + for q <- qs do + try do + :rpc.call( + get_rabbit_hostname(), + :rabbit_amqeueue, + :delete, + [q, false, false], + 5000 + ) + catch + _, _ -> :ok + end + end + end + + def reset_vm_memory_high_watermark() do + try do + :rpc.call( + get_rabbit_hostname(), + :vm_memory_monitor, + :set_vm_memory_high_watermark, + [0.4], + 5000 + ) + catch + _, _ -> :ok + end + end + + def emit_list_multiple_sources(list1, list2, ref, pid) do + pids = for list <- [list1, list2], do: Kernel.spawn_link(TestHelper, :emit_list, [list, ref, pid]) + :rabbit_control_misc.await_emitters_termination(pids) + end + + def emit_list(list, ref, pid) do + emit_list_map(list, &(&1), ref, pid) + end + + def emit_list_map(list, fun, ref, pid) do + :rabbit_control_misc.emitting_map(pid, ref, fun, list) + end + + def run_command_to_list(command, args) do + res = Kernel.apply(command, :run, args) + case Enumerable.impl_for(res) do + nil -> res; + _ -> Enum.to_list(res) + end + end + + def vhost_exists?(vhost) do + Enum.any?(list_vhosts(), fn(v) -> v[:name] == vhost end) + end + + def set_enabled_plugins(plugins, mode, node, opts) do + {:ok, enabled} = PluginHelpers.set_enabled_plugins(plugins, opts) + + PluginHelpers.update_enabled_plugins(enabled, mode, node, opts) + end + + def currently_active_plugins(context) do + Enum.sort(:rabbit_misc.rpc_call(context[:opts][:node], :rabbit_plugins, :active, [])) + end + + def enable_federation_plugin() do + node = get_rabbit_hostname() + {:ok, plugins_file} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, + :application, :get_env, + [:rabbit, :plugins_dir]) + rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) + {:ok, [_enabled_plugins]} = :file.consult(plugins_file) + + opts = %{enabled_plugins_file: plugins_file, + plugins_dir: plugins_dir, + rabbitmq_home: rabbitmq_home, + online: true, offline: false} + + plugins = currently_active_plugins(%{opts: %{node: node}}) + case Enum.member?(plugins, :rabbitmq_federation) do + true -> :ok + false -> + set_enabled_plugins(plugins ++ [:rabbitmq_federation], :online, get_rabbit_hostname(), opts) + end + end + + def set_vhost_limits(vhost, limits) do + :rpc.call(get_rabbit_hostname(), + :rabbit_vhost_limit, :parse_set, [vhost, limits, <<"acting-user">>]) + end + def get_vhost_limits(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_vhost_limit, :list, [vhost]) + |> Map.new + end + + def clear_vhost_limits(vhost) do + :rpc.call(get_rabbit_hostname(), :rabbit_vhost_limit, :clear, [vhost, <<"acting-user">>]) + end + + def resume_all_client_listeners() do + :rpc.call(get_rabbit_hostname(), :rabbit_maintenance, :resume_all_client_listeners, []) + end + + def suspend_all_client_listeners() do + :rpc.call(get_rabbit_hostname(), :rabbit_maintenance, :suspend_all_client_listeners, []) + end + + def set_user_limits(user, limits) do + :rpc.call(get_rabbit_hostname(), + :rabbit_auth_backend_internal, :set_user_limits, [user, limits, <<"acting-user">>]) + end + + def get_user_limits(user) do + :rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :get_user_limits, [user]) + |> Map.new + end + + def clear_user_limits(user) do + clear_user_limits user, "max-connections" + clear_user_limits user, "max-channels" + end + + def clear_user_limits(user, limittype) do + :rpc.call(get_rabbit_hostname(), + :rabbit_auth_backend_internal, :clear_user_limits, [user, limittype, <<"acting-user">>]) + end + + def set_scope(scope) do + script_name = Config.get_option(:script_name, %{}) + scopes = Keyword.put(Application.get_env(:rabbitmqctl, :scopes), script_name, scope) + Application.put_env(:rabbitmqctl, :scopes, scopes) + CommandModules.load(%{}) + end + + def switch_plugins_directories(old_value, new_value) do + :rabbit_misc.rpc_call(get_rabbit_hostname(), :application, :set_env, + [:rabbit, :plugins_dir, new_value]) + ExUnit.Callbacks.on_exit(fn -> + :rabbit_misc.rpc_call(get_rabbit_hostname(), :application, :set_env, + [:rabbit, :plugins_dir, old_value]) + end) + end + + def get_opts_with_non_existing_plugins_directory(context) do + get_opts_with_plugins_directories(context, ["/tmp/non_existing_rabbitmq_dummy_plugins"]) + end + + def get_opts_with_plugins_directories(context, plugins_directories) do + opts = context[:opts] + plugins_dir = opts[:plugins_dir] + all_directories = Enum.join([to_string(plugins_dir) | plugins_directories], path_separator()) + %{opts | plugins_dir: to_charlist(all_directories)} + end + + def get_opts_with_existing_plugins_directory(context) do + extra_plugin_directory = System.tmp_dir!() |> Path.join("existing_rabbitmq_dummy_plugins") + File.mkdir!(extra_plugin_directory) + ExUnit.Callbacks.on_exit(fn -> + File.rm_rf(extra_plugin_directory) + end) + get_opts_with_plugins_directories(context, [extra_plugin_directory]) + end + + def check_plugins_enabled(plugins, context) do + {:ok, [xs]} = :file.consult(context[:opts][:enabled_plugins_file]) + assert_equal_sets(plugins, xs) + end + + def assert_equal_sets(a, b) do + asorted = Enum.sort(a) + bsorted = Enum.sort(b) + assert asorted == bsorted + end + + def assert_stream_without_errors(stream) do + true = Enum.all?(stream, fn({:error, _}) -> false; + ({:error, _, _}) -> false; + (_) -> true end) + end + + def wait_for_log_message(message, file \\ nil, attempts \\ 100) do + ## Assume default log is the first one + log_file = case file do + nil -> + [default_log | _] = :rpc.call(get_rabbit_hostname(), :rabbit_lager, :log_locations, []) + default_log + _ -> file + end + case File.read(log_file) do + {:ok, data} -> + case String.match?(data, Regex.compile!(message)) do + true -> :ok + false -> + :timer.sleep(100) + wait_for_log_message(message, log_file, attempts - 1) + end + _ -> + :timer.sleep(100) + wait_for_log_message(message, log_file, attempts - 1) + end + end +end diff --git a/deps/rabbitmq_cli/test/upgrade/await_online_quorum_plus_one_command_test.exs b/deps/rabbitmq_cli/test/upgrade/await_online_quorum_plus_one_command_test.exs new file mode 100644 index 0000000000..c169f9ff5d --- /dev/null +++ b/deps/rabbitmq_cli/test/upgrade/await_online_quorum_plus_one_command_test.exs @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AwaitOnlineQuorumPlusOneCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Upgrade.Commands.AwaitOnlineQuorumPlusOneCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 5000 + }} + end + + test "merge_defaults: overrides a timeout" do + assert @command.merge_defaults([], %{}) == {[], %{timeout: 120_000}} + end + + test "validate: accepts no positional arguments" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: succeeds with no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], opts))) + end + +end diff --git a/deps/rabbitmq_cli/test/upgrade/await_online_synchronized_mirror_command_test.exs b/deps/rabbitmq_cli/test/upgrade/await_online_synchronized_mirror_command_test.exs new file mode 100644 index 0000000000..7089dada2c --- /dev/null +++ b/deps/rabbitmq_cli/test/upgrade/await_online_synchronized_mirror_command_test.exs @@ -0,0 +1,45 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule AwaitOnlineSynchronizedMirrorsCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Upgrade.Commands.AwaitOnlineSynchronizedMirrorCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 5000 + }} + end + + test "merge_defaults: overrides a timeout" do + assert @command.merge_defaults([], %{}) == {[], %{timeout: 120_000}} + end + + test "validate: accepts no positional arguments" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: succeeds with no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], opts))) + end + +end diff --git a/deps/rabbitmq_cli/test/upgrade/drain_command_test.exs b/deps/rabbitmq_cli/test/upgrade/drain_command_test.exs new file mode 100644 index 0000000000..3533f7feff --- /dev/null +++ b/deps/rabbitmq_cli/test/upgrade/drain_command_test.exs @@ -0,0 +1,57 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule DrainCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Upgrade.Commands.DrainCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + revive_node() + + on_exit(fn -> + revive_node() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 5000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: accepts no positional arguments" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: succeeds with no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], opts))) + end + + test "run: puts target node into maintenance mode", context do + assert not is_draining_node() + assert :ok == @command.run([], context[:opts]) + + await_condition(fn -> is_draining_node() end, 7000) + revive_node() + end +end diff --git a/deps/rabbitmq_cli/test/upgrade/post_upgrade_command_test.exs b/deps/rabbitmq_cli/test/upgrade/post_upgrade_command_test.exs new file mode 100644 index 0000000000..e77390ecf0 --- /dev/null +++ b/deps/rabbitmq_cli/test/upgrade/post_upgrade_command_test.exs @@ -0,0 +1,49 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + + +defmodule PostUpgradeCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Upgrade.Commands.PostUpgradeCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 5000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: accepts no positional arguments" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: succeeds with no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], opts))) + end + + test "run: returns an OK", context do + assert match?({:ok, _}, @command.run([], context[:opts])) + end + +end diff --git a/deps/rabbitmq_cli/test/upgrade/revive_command_test.exs b/deps/rabbitmq_cli/test/upgrade/revive_command_test.exs new file mode 100644 index 0000000000..6d43d59b83 --- /dev/null +++ b/deps/rabbitmq_cli/test/upgrade/revive_command_test.exs @@ -0,0 +1,57 @@ +## This Source Code Form is subject to the terms of the Mozilla Public +## License, v. 2.0. If a copy of the MPL was not distributed with this +## file, You can obtain one at https://mozilla.org/MPL/2.0/. +## +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. + +defmodule ReviveCommandTest do + use ExUnit.Case, async: false + import TestHelper + + @command RabbitMQ.CLI.Upgrade.Commands.ReviveCommand + + setup_all do + RabbitMQ.CLI.Core.Distribution.start() + + revive_node() + + on_exit(fn -> + revive_node() + end) + + :ok + end + + setup context do + {:ok, opts: %{ + node: get_rabbit_hostname(), + timeout: context[:test_timeout] || 5000 + }} + end + + test "merge_defaults: nothing to do" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: accepts no positional arguments" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: succeeds with no positional arguments" do + assert @command.validate([], %{}) == :ok + end + + @tag test_timeout: 3000 + test "run: targeting an unreachable node throws a badrpc", context do + opts = %{node: :jake@thedog, timeout: 200} + assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], opts))) + end + + test "run: puts target node into regular operating mode", context do + assert not is_draining_node() + drain_node() + await_condition(fn -> is_draining_node() end, 7000) + assert :ok == @command.run([], context[:opts]) + await_condition(fn -> not is_draining_node() end, 7000) + end +end |