File: resolver_info_command.ex

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (92 lines) | stat: -rw-r--r-- 2,764 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  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