1## This Source Code Form is subject to the terms of the Mozilla Public 2## License, v. 2.0. If a copy of the MPL was not distributed with this 3## file, You can obtain one at https://mozilla.org/MPL/2.0/. 4## 5## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved. 6 7defmodule RabbitMQ.CLI.Diagnostics.Commands.ResolverInfoCommand do 8 @moduledoc """ 9 Displays effective hostname resolver (inetrc) configuration on target node 10 """ 11 12 import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] 13 import RabbitMQ.CLI.Core.ANSI, only: [bright: 1] 14 alias RabbitMQ.CLI.Core.Networking 15 16 @behaviour RabbitMQ.CLI.CommandBehaviour 17 18 def scopes(), do: [:diagnostics] 19 20 def switches(), do: [offline: :boolean] 21 def aliases(), do: [] 22 23 def merge_defaults(args, opts) do 24 {args, Map.merge(%{offline: false}, opts)} 25 end 26 27 def validate(args, _) when length(args) > 0, do: {:validation_failure, :too_many_args} 28 def validate([], _), do: :ok 29 30 def run([], %{offline: true}) do 31 Networking.inetrc_map(:inet.get_rc()) 32 end 33 def run([], %{node: node_name, timeout: timeout, offline: false}) do 34 case :rabbit_misc.rpc_call(node_name, :inet, :get_rc, [], timeout) do 35 {:error, _} = err -> err 36 {:error, _, _} = err -> err 37 xs when is_list(xs) -> Networking.inetrc_map(xs) 38 other -> other 39 end 40 end 41 42 def output(info, %{node: node_name, formatter: "json"}) do 43 {:ok, %{ 44 "result" => "ok", 45 "node" => node_name, 46 "resolver" => info 47 }} 48 end 49 def output(info, _opts) do 50 main_section = [ 51 "#{bright("Runtime Hostname Resolver (inetrc) Settings")}\n", 52 "Lookup order: #{info["lookup"]}", 53 "Hosts file: #{info["hosts_file"]}", 54 "Resolver conf file: #{info["resolv_conf"]}", 55 "Cache size: #{info["cache_size"]}" 56 ] 57 hosts_section = [ 58 "\n#{bright("inetrc File Host Entries")}\n" 59 ] ++ case info["hosts"] do 60 [] -> ["(none)"] 61 nil -> ["(none)"] 62 hs -> Enum.reduce(hs, [], fn {k, v}, acc -> ["#{k} #{Enum.join(v, ", ")}" | acc] end) 63 end 64 65 lines = main_section ++ hosts_section 66 67 {:ok, Enum.join(lines, line_separator())} 68 end 69 70 def usage() do 71 "resolver_info" 72 end 73 74 def help_section(), do: :configuration 75 76 def description(), do: "Displays effective hostname resolver (inetrc) configuration on target node" 77 78 def banner(_, %{node: node_name, offline: false}) do 79 "Asking node #{node_name} for its effective hostname resolver (inetrc) configuration..." 80 end 81 def banner(_, %{offline: true}) do 82 "Displaying effective hostname resolver (inetrc) configuration used by CLI tools..." 83 end 84end 85