Module: Capichef::Role

Defined in:
lib/capichef/role.rb

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



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
# File 'lib/capichef/role.rb', line 18

def self.load_into(configuration)
  configuration.load do
    
    def search(type, query) 
      nodes = []
      Chef::Search::Query.new.search(type, query) do |result|
        nodes.push(result)
      end
      
      nodes
    end
    
    def chef_role(name, chef_role_name, options = {})
      options         = {:attribute => :ipaddress, :options_mapping => {}}.merge(options)
      attribute       = options.delete(:attribute)
      options_mapping = options.delete(:options_mapping)
  
      Capichef::Role.node_list(chef_role_name, chef_environment).each do |n|
        node_options = Hash.new
        
        options_mapping.each do |option, value|
          case value
          when Proc
            node_options[option] = value.call(n)
          when Symbol, String
            node_options[option] = n[value.to_s]
          else
            raise ArgumentError, 'Option mapping value must be Proc, Symbol, String.'
          end
        end
        
        options[:node] = n
        
        case attribute
        when Proc
          role name, attribute.call(n), name, options.merge(node_options)
        when Hash
          iface, family = attribute.keys.first.to_s, attribute.values.first.to_s
          addresses = n["network"]["interfaces"][iface]["addresses"]
          role name, addresses.select{|address, data| data["family"] == family }.to_a.first.first, options.merge(node_options)
        when Symbol, String
          role name, n[attribute.to_s].to_s, name, options.merge(node_options)
        else
          raise ArgumentError, 'Attribute option must be Proc, Hash, Symbol, String.'
        end
      end
    end
    
  end
end

.node_list(role_name, environment = "_default") ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/capichef/role.rb', line 7

def self.node_list(role_name, environment="_default")
  nodes = []
  query = sprintf("chef_environment:%s AND roles:%s", environment, role_name)

  Chef::Search::Query.new.search(:node, query) do |result|
    nodes.push(result)
  end
  
  nodes
end