Class: Kitchenplan::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchenplan/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



17
18
19
20
21
22
23
# File 'lib/kitchenplan/config.rb', line 17

def initialize
  self.detect_platform
  self.parse_default_config
  self.parse_people_config
  self.parse_system_config
  self.parse_group_configs
end

Instance Attribute Details

#default_configObject (readonly)

Returns the value of attribute default_config.



12
13
14
# File 'lib/kitchenplan/config.rb', line 12

def default_config
  @default_config
end

#group_configsObject (readonly)

Returns the value of attribute group_configs.



15
16
17
# File 'lib/kitchenplan/config.rb', line 15

def group_configs
  @group_configs
end

#people_configObject (readonly)

Returns the value of attribute people_config.



13
14
15
# File 'lib/kitchenplan/config.rb', line 13

def people_config
  @people_config
end

#platformObject (readonly)

Returns the value of attribute platform.



11
12
13
# File 'lib/kitchenplan/config.rb', line 11

def platform
  @platform
end

#system_configObject (readonly)

Returns the value of attribute system_config.



14
15
16
# File 'lib/kitchenplan/config.rb', line 14

def system_config
  @system_config
end

Instance Method Details

#configObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kitchenplan/config.rb', line 71

def config
  config = {}
  config['recipes'] = []
  config['recipes'] |= hash_path(@default_config, 'recipes', 'global') || []
  config['recipes'] |= hash_path(@default_config, 'recipes', @platform) || []
  @group_configs.each do |group_name, group_config|
    config['recipes'] |= hash_path(group_config, 'recipes', 'global') || []
    config['recipes'] |= hash_path(group_config, 'recipes', @platform) || []
  end
  people_recipes = @people_config['recipes'] || {}
  config['recipes'] |= people_recipes['global'] || []
  config['recipes'] |= people_recipes[@platform] || []
      
  system_recipes = @system_config['recipes'] || {}  
  config['recipes'] |= system_recipes['global'] || []
  config['recipes'] |= system_recipes[@platform] || []

  # First take the attributes from default.yml
  config['attributes'] = {}
  config['attributes'].deep_merge!(@default_config['attributes'] || {}) { |key, old, new| Array.wrap(old) + Array.wrap(new) }

  # then override and extend them with the group attributes
  @group_configs.each do |group_name, group_config|
    config['attributes'].deep_merge!(group_config['attributes']) { |key, old, new| Array.wrap(old) + Array.wrap(new) } unless group_config['attributes'].nil?
  end

  # then override and extend them with the people attributes
  people_attributes = @people_config['attributes'] || {}
  config['attributes'].deep_merge!(people_attributes) { |key, old, new| Array.wrap(old) + Array.wrap(new) }

  # lastly override from the system files
  system_attributes = @system_config['attributes'] || {}
  config['attributes'].deep_merge!(system_attributes) { |key, old, new| Array.wrap(old) + Array.wrap(new) }

  config
end

#detect_platformObject



25
26
27
28
29
30
31
# File 'lib/kitchenplan/config.rb', line 25

def detect_platform
  #ohai = Ohai::System.new
  #ohai.require_plugin('os')
  #ohai.require_plugin('platform')
  #@platform = ohai[:platform_family]
  @platform = 'mac_os_x' # We only support osx at the moment, and it saves a large dependency
end

#hardware_modelObject



33
34
35
# File 'lib/kitchenplan/config.rb', line 33

def hardware_model
   `sysctl -n hw.model | tr -d '\n'`
end

#parse_default_configObject



37
38
39
40
# File 'lib/kitchenplan/config.rb', line 37

def parse_default_config
  default_config_path = 'config/default.yml'
  @default_config = (YAML.load(ERB.new(File.read(default_config_path)).result) if File.exist?(default_config_path)) || {}
end

#parse_group_config(group) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/kitchenplan/config.rb', line 60

def parse_group_config(group)
  unless @group_configs[group]
    group_config_path = "config/groups/#{group}.yml"
    @group_configs[group] = (YAML.load(ERB.new(File.read(group_config_path)).result) if File.exist?(group_config_path)) || {}
    defined_groups = @group_configs[group]['groups']
    if defined_groups
      self.parse_group_configs(defined_groups)
    end
  end
end

#parse_group_configs(group = (( @default_config['groups'] || [] ) | ( @people_config['groups'] || [] ))) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/kitchenplan/config.rb', line 52

def parse_group_configs(group = (( @default_config['groups'] || [] ) | ( @people_config['groups'] || [] )))
  @group_configs = @group_configs || {}
  defined_groups = group || []
  defined_groups.each do |group|
    self.parse_group_config(group)
  end
end

#parse_people_configObject



42
43
44
45
# File 'lib/kitchenplan/config.rb', line 42

def parse_people_config
  people_config_path = "config/people/#{Etc.getlogin}.yml"
  @people_config = (YAML.load(ERB.new(File.read(people_config_path)).result) if File.exist?(people_config_path)) || {}
end

#parse_system_configObject



47
48
49
50
# File 'lib/kitchenplan/config.rb', line 47

def parse_system_config
  system_config_path = "config/system/#{hardware_model}.yml"
  @system_config = (YAML.load(ERB.new(File.read(system_config_path)).result) if File.exist?(system_config_path)) || {}
end