Class: AttributesMap

Inherits:
Object
  • Object
show all
Defined in:
lib/mofa/attributes_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributesMap

Returns a new instance of AttributesMap.



19
20
21
# File 'lib/mofa/attributes_map.rb', line 19

def initialize
  @mp = {}
end

Instance Attribute Details

#cookbookObject

Returns the value of attribute cookbook.



3
4
5
# File 'lib/mofa/attributes_map.rb', line 3

def cookbook
  @cookbook
end

#hostlistObject

Returns the value of attribute hostlist.



4
5
6
# File 'lib/mofa/attributes_map.rb', line 4

def hostlist
  @hostlist
end

#mpObject

Returns the value of attribute mp.



2
3
4
# File 'lib/mofa/attributes_map.rb', line 2

def mp
  @mp
end

#option_attributesObject

Returns the value of attribute option_attributes.



7
8
9
# File 'lib/mofa/attributes_map.rb', line 7

def option_attributes
  @option_attributes
end

#option_runlistObject

Returns the value of attribute option_runlist.



6
7
8
# File 'lib/mofa/attributes_map.rb', line 6

def option_runlist
  @option_runlist
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/mofa/attributes_map.rb', line 5

def token
  @token
end

Class Method Details

.create(cookbook, hostlist, token, option_runlist = nil, option_attributes = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/mofa/attributes_map.rb', line 9

def self.create(cookbook, hostlist, token, option_runlist = nil, option_attributes = nil)
  a = AttributesMap.new
  a.cookbook = cookbook
  a.hostlist = hostlist
  a.token = token
  a.option_runlist = option_runlist
  a.option_attributes = option_attributes.nil? ? {} : JSON.parse(option_attributes)
  a
end

Instance Method Details

#deep_merge(attr_hash, attr_hash_local) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mofa/attributes_map.rb', line 65

def deep_merge(attr_hash, attr_hash_local)
  new_attr_hash = Marshal.load(Marshal.dump(attr_hash))
  attr_hash.each do |key, value|
    if attr_hash_local.key?(key)
      if value.is_a?(Hash) && attr_hash_local[key].is_a?(Hash)
        new_attr_hash[key] = deep_merge(value, attr_hash_local[key])
      else
        new_attr_hash[key] = attr_hash_local[key]
      end
    end
  end
  # and now add all attributes that are in attr_hash_local but not in attr_hash
  attr_hash_local.each do |key, value|
    unless attr_hash.key?(key)
      new_attr_hash.store(key, value)
    end
  end

  new_attr_hash
end

#deep_parse(attr_hash, placeholder, content) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mofa/attributes_map.rb', line 42

def deep_parse(attr_hash, placeholder, content)
  new_attr_hash = Marshal.load(Marshal.dump(attr_hash))
  attr_hash.each do |key, value|
    if value.is_a?(Hash)
      new_attr_hash[key] = deep_parse(value, placeholder, content)
    elsif value.is_a?(Array)
      new_attr_hash[key] = []
      value.each do |value_item|
        if value_item.is_a?(Hash)
          new_attr_hash[key] = deep_parse(value, placeholder, content)
        else
          new_attr_hash[key].push(value_item.gsub(Regexp.new(Regexp.escape(placeholder)), content))
        end
      end
    else
      if value
        new_attr_hash[key] = value.gsub(Regexp.new(Regexp.escape(placeholder)), content)
      end
    end
  end
  new_attr_hash
end

#generateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mofa/attributes_map.rb', line 23

def generate
  attr_all_roles = deep_merge(option_attributes, cookbook.mofa_yml.get_attr_for_role('all'))
  attr_all_roles_local = cookbook.mofa_yml_local.get_attr_for_role('all')
  attr_all_roles = deep_merge(attr_all_roles, attr_all_roles_local)

  hostlist.list.each do |hostname|
    # Again: the underlying rule here is -> shortname = role
    attr_host_role = cookbook.mofa_yml.get_attr_for_role(Hostlist::get_role(hostname))
    attr_host_role_local = cookbook.mofa_yml_local.get_attr_for_role(Hostlist::get_role(hostname))

    attr_host_role = deep_merge(attr_host_role, attr_host_role_local)
    attr_per_host = deep_merge(attr_all_roles, attr_host_role)

    attr_per_host = deep_parse(attr_per_host, '__SHORTNAME__', Hostlist::get_shortname(hostname))

    @mp.store(hostname, attr_per_host)
  end
end