summaryrefslogtreecommitdiff
path: root/lib/leap_cli/macros/hosts.rb
blob: 541bbc13caf4d30b48703db8fd68ec133ee0f90f (plain)
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
# encoding: utf-8

module LeapCli
  module Macro

    ##
    ## IPs
    ##

    #
    # returns a simple array of all the IPs for the specified node list
    #
    def host_ips(node_list)
      if self.vagrant?
        node_list = node_list['environment' => 'local']
      else
        node_list = node_list['environment' => '!local']
      end
      node_list.map {|name, n|
        [n.ip_address, (global.facts[name]||{})['ec2_public_ipv4']]
      }.flatten.compact.uniq
    end

    ##
    ## HOSTS
    ##

    #
    # records the list of hosts that are encountered for this node
    #
    def hostnames(nodes)
      @referenced_nodes ||= Config::ObjectList.new
      nodes = listify(nodes)
      nodes.each_node do |node|
        @referenced_nodes[node.name] ||= node
      end
      return nodes.values.collect {|node| node.domain.name}
    end

    #
    # Generates entries needed for updating /etc/hosts on a node (as a hash).
    #
    # Argument `nodes` can be nil or a list of nodes. If nil, only include the
    # IPs of the other nodes this @node as has encountered (plus all mx nodes).
    #
    # Also, for virtual machines, we use the local address if this @node is in
    # the same location as the node in question.
    #
    # We include the ssh public key for each host, so that the hash can also
    # be used to generate the /etc/ssh/known_hosts
    #
    def hosts_file(nodes=nil)
      if nodes.nil?
        if @referenced_nodes && @referenced_nodes.any?
          nodes = @referenced_nodes
          nodes = nodes.merge(nodes_like_me[:services => 'mx'])  # all nodes always need to communicate with mx nodes.
        end
      end
      return {} unless nodes
      hosts = {}
      my_location = @node['location'] ? @node['location']['name'] : nil
      nodes.each_node do |node|
        hosts[node.name] = {
          'ip_address' => node.ip_address,
          'domain_internal' => node.domain.internal,
          'domain_full' => node.domain.full,
          'port' => node.ssh.port
        }
        if node.dns['aliases'] && node.dns['aliases'].any?
          # include aliases, but without domain.full
          hosts[node.name]['aliases'] = node.dns['aliases'] - [node.domain.full]
        end
        node_location = node['location'] ? node['location']['name'] : nil
        if my_location == node_location
          if facts = @node.manager.facts[node.name]
            if facts['ec2_public_ipv4']
              hosts[node.name]['ip_address'] = facts['ec2_public_ipv4']
            end
          end
        end
        host_pub_key   = Util::read_file([:node_ssh_pub_key,node.name])
        if host_pub_key
          hosts[node.name]['host_pub_key'] = host_pub_key
        end
      end
      hosts
    end

  end
end