Class: Chef::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-helpers/node.rb

Instance Method Summary collapse

Instance Method Details

#alliesArray<Chef::Node>

Node's "allies" are all nodes in the same environment (if the environment is not _default), and nodes specified by allies attribute. The allies attribute - if set - should be an array of node names or node search queries; the named nodes and search results will be added to node's allies.

This is mostly useful when defining firewall or other access rules, to easily limit access to insides of a cluster plus a handful of friendly machines.

Returns:

  • (Array<Chef::Node>)

    Array of node's "allies".


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chef-helpers/node.rb', line 17

def allies
  @allies ||=
    begin
      rv = []
      q =  Chef::Search::Query.new
      rv += q.search(:node, "chef_environment:#{self.chef_environment}").first unless self.chef_environment == '_default'
      if self['allies']
        self['allies'].each do |ally|
          ally = "name:#{ally}" unless ally.include?(':')
          rv += q.search(:node, ally).first
        end
      end
      rv
    end
end

#brackets_with_jsonpath(key) ⇒ Object Also known as: []

jsonpath access to node attributes


57
58
59
60
61
62
63
# File 'lib/chef-helpers/node.rb', line 57

def brackets_with_jsonpath(key)
  case key
  when JsonPath then key.on(self.to_hash)
  when /^\$/    then JsonPath.on(self.to_hash, key)
  else               self.brackets_without_jsonpath(key)
  end
end

#ip_for(other_node) ⇒ String

Note:

This method may return wrong IP with non-EC2 cloud providers, and can use some tweaking for such cases.

Find out, which IP should be used to contact with other node.

If both nodes are on EC2 and in the same region, then other node's ec2.local_ipv4 attribute is used. Otherwise, if other node is a cloud instance, its cloud_public.ipv4 attribute is used. Otherwise, other node's ipaddress is used.

Parameters:

  • other_node (Chef::Node)

    Node, whose IP we need to know

Returns:

  • (String)

    IP of other_node


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef-helpers/node.rb', line 44

def ip_for(other_node)
  # Strip last letter from availability zone to get region.
  if other_node['ec2'] && self['ec2'] &&
     self['ec2']['placement_availability_zone'].sub(/[a-z]$/,'') == other_node['ec2']['placement_availability_zone'].sub(/[a-z]$/,'')
    other_node['ec2']['local_ipv4']
  elsif other_node['cloud']
    other_node['cloud']['public_ipv4']
  else
    other_node['ipaddress']
  end
end