Class: Fhcap::FHConfig
- Inherits:
-
Object
- Object
- Fhcap::FHConfig
- Defined in:
- lib/fhcap/misc/fh_config.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#file ⇒ Object
Returns the value of attribute file.
-
#node ⇒ Object
Returns the value of attribute node.
Instance Method Summary collapse
- #add(key, value, force = false, overrides = []) ⇒ Object
- #attribute_file_content(component, sort) ⇒ Object
- #from_attributes_file(component, filename, force = false) ⇒ Object
- #from_file(filename) ⇒ Object
-
#has_upper_recipe_override(key) ⇒ Object
Traverse the key and check it isn’t overridden at any point further up the chain.
-
#initialize ⇒ FHConfig
constructor
A new instance of FHConfig.
- #is_json?(value) ⇒ Boolean
- #parse_key(key) ⇒ Object
- #parse_value(value) ⇒ Object
- #to_chef_attributes(sort = false) ⇒ Object
Constructor Details
#initialize ⇒ FHConfig
Returns a new instance of FHConfig.
8 9 10 11 12 |
# File 'lib/fhcap/misc/fh_config.rb', line 8 def initialize #ToDO Should really get rid of this extra config object and just use @node to store everything @config = {} @node = Fhcap::DummyNode.new end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/fhcap/misc/fh_config.rb', line 6 def config @config end |
#file ⇒ Object
Returns the value of attribute file.
6 7 8 |
# File 'lib/fhcap/misc/fh_config.rb', line 6 def file @file end |
#node ⇒ Object
Returns the value of attribute node.
6 7 8 |
# File 'lib/fhcap/misc/fh_config.rb', line 6 def node @node end |
Instance Method Details
#add(key, value, force = false, overrides = []) ⇒ Object
14 15 16 17 18 |
# File 'lib/fhcap/misc/fh_config.rb', line 14 def add(key, value, force=false, overrides=[]) if force || !@config.has_key?(key) || (overrides.include? key.join('.')) @config[key] = parse_value(value) end end |
#attribute_file_content(component, sort) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fhcap/misc/fh_config.rb', line 108 def attribute_file_content(component, sort) content = "# Default conf.rb attributes file, generated by fhcap, do not edit\n\n" @node.included_recipe_attributes.each do |recipe| content += "include_attribute '#{recipe}'\n" end self.to_chef_attributes(sort).collect do |key, value| content += "default['#{component}']['conf']#{key} = #{value}\n" end content end |
#from_attributes_file(component, filename, force = false) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/fhcap/misc/fh_config.rb', line 99 def from_attributes_file(component, filename, force=false) @node.from_file(filename) conf = Fhcap::JsonConfig.new conf.from_json(@node.default_attrs[component][:conf].to_json) conf.config.each() do |k, v| add(k, v, force) end end |
#from_file(filename) ⇒ Object
96 97 |
# File 'lib/fhcap/misc/fh_config.rb', line 96 def from_file(filename) end |
#has_upper_recipe_override(key) ⇒ Object
Traverse the key and check it isn’t overridden at any point further up the chain
21 22 23 24 25 26 |
# File 'lib/fhcap/misc/fh_config.rb', line 21 def has_upper_recipe_override(key) key.pop return true if @config.has_key?(key) && @config[key] =~ /RECIPE_OVERRIDE/ has_upper_recipe_override(key) unless key.empty? false end |
#is_json?(value) ⇒ Boolean
88 89 90 91 92 93 94 |
# File 'lib/fhcap/misc/fh_config.rb', line 88 def is_json?(value) begin !!JSON.parse(value) rescue false end end |
#parse_key(key) ⇒ Object
64 65 66 |
# File 'lib/fhcap/misc/fh_config.rb', line 64 def parse_key(key) key end |
#parse_value(value) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fhcap/misc/fh_config.rb', line 68 def parse_value(value) if value =~ /^[0-9]+$/ # Got a number elsif value =~ /^true$|^false$/ #Take any quotes off around true/false return value.gsub("'", "") elsif value.is_a? String if is_json?(value) return JSON.parse(value).to_json.inspect elsif value.empty? return "\"\"" else value.gsub!(/^"|"$|^'|'$/, "") #value.gsub!(/\\/, "") return "\"#{value}\"" end end value end |
#to_chef_attributes(sort = false) ⇒ Object
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 |
# File 'lib/fhcap/misc/fh_config.rb', line 28 def to_chef_attributes(sort=false) attrs = {} if sort conf = {} @config.keys.sort.each do |key| conf[key] = @config[key] end else conf = @config end conf.each do |key, value| #Skip any keys that are being overridden further up the chain unless has_upper_recipe_override(key.clone) #Since we converted all node['blah'] to strings, we need to convert them back #Ahh. this is horrible, must be a better way to do this val = value.to_s val.gsub!(/node(\[['a-zA-Z0-9_-]*\])+/, '#{\&}') if val =~ /^"#\{node(\[['a-zA-Z0-9_-]*\])+\}"$/ val.gsub!(/^"#\{|\}"$/, '') end val.gsub!(/node.chef_environment/, '#{\&}') if val =~ /^"#\{node.chef_environment\}"$/ val.gsub!(/^"#\{|\}"$/, '') end if val =~ /^"\{\}"$/ val.gsub!(/"/, '') end attrs[key.collect { |part| "['#{part}']" }.join()] = val end end attrs end |