Class: VariableStore

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/retrospec/variable_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVariableStore

Returns a new instance of VariableStore.



10
11
12
13
14
15
# File 'lib/retrospec/variable_store.rb', line 10

def initialize
  @store = {}
  # a defined type will sometimes use the name variable within other variables
  # so we need to setup a name to reference
  @store['$name'] = 'XXreplace_meXX'
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/retrospec/variable_store.rb', line 6

def store
  @store
end

Class Method Details

.add(key, value, enable_value_conversion = true) ⇒ Object

key should be a variable starting with $ enable_value_conversion converts the value to a string



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

def self.add(key, value, enable_value_conversion=true)
  if key.respond_to?(:value)
    key = key.value
  end
  # the key name must always be the same and look like $key_name or $key_name::other::name
  key = "#{'$' + key.to_s}" unless key.to_s.start_with?('$')
  key = key.to_s.gsub('$::', '$') if key.to_s.start_with?('$::')    # sometimes variables start with $::
  #return if instance.store[key] == value
  if value.instance_of?(String) or value.instance_of?(Puppet::Parser::AST::String)
    value = value.to_s.gsub("\"",'')
  end
  if enable_value_conversion
    instance.store[key.to_s] = value.to_s
  else
    instance.store[key.to_s] = value
  end
end

.instanceObject



17
18
19
# File 'lib/retrospec/variable_store.rb', line 17

def self.instance
  @@instance ||= new
end

.lookup(key) ⇒ Object

lookup the key in the hash, if we dont’ find it lets just return the string representation



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

def self.lookup(key)
  if key.respond_to?(:value)
    key = key.value
  end
  key = "$#{key.to_s}" unless key.to_s.start_with?('$')
  key = key.gsub('$::', '$') if key.to_s.start_with?('$::')    # sometimes variables start with $::
  #puts "Looking up: #{key}"
  begin
    value = VariableStore.instance.store.fetch(key.to_s)
    # try and resolve if necessary
    if [Puppet::Parser::AST::Variable,Puppet::Parser::AST::VarDef].include?(value.class)
      value = resolve(value)
    else
      if captures = value.scan(/(\$[::]?[\w+::]*\w+)/).flatten
        # produces an array of variables that have not been resolved yet
        #  ["$concat1", "$concat"] = "$concat1/test3183/$concat"
        captures.each { |c| value.gsub(c, resolve(c))}
      end
    end
  rescue
    return key
  end
  value
end

.populate(type, is_parent = false) ⇒ Object

gets all the variables and parameters and passes them through the resolve function to populate the variable store we use recursion to evaluate inherited manifests TODO use the same recursion method to evaluate included manifests



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/retrospec/variable_store.rb', line 103

def self.populate(type, is_parent=false)
  if type.parent
    p = type.parent.gsub(/\A::/, '')
    parent = type.resource_type_collection.hostclasses[p]
  else
    parent = nil
  end
  if parent
    # type has a parent and we want to add the parent variables first so we call populate first
    # then we load the type's variable
    populate(parent,true)
    # we load the local scope and top scope variables because we need access to both
    # there is a chance some of the local scope variables will be overwritten but by the time that happens
    # we won't need them anymore.
    type.arguments.each do |k,v|
      # store the class params
      add(k.to_s,resolve(v),true)
      add(("$#{type.namespace}::" << k.to_s),resolve(v),true)
    end
    TypeCode.new(type).variables.each do |v|
      add(v.name.to_s, resolve(v.value))
      add(("$#{type.namespace}::" << v.name.to_s), resolve(v.value))
    end
  elsif is_parent
    # if this is the parent we load the variables
    type.arguments.each {|k,v| add(("$#{type.namespace}::" << k.to_s),resolve(v),true)}
    TypeCode.new(type).variables.each {|v| add(("$#{type.namespace}::" << v.name.to_s), resolve(v.value))}
  else
    # if the type does not have a parent we load the variables
    type.arguments.each do |k,v|
      # store the class params
      add(k.to_s,resolve(v),true)
      add(("$#{type.namespace}::" << k.to_s),resolve(v),true)
    end
    TypeCode.new(type).variables.each do |v|
      add(v.name.to_s, resolve(v.value))
      add(("$#{type.namespace}::" << v.name.to_s), resolve(v.value))
    end
  end
end

.resolve(variable) ⇒ Object

will try and resolve the variable to a value by consulting the variables hash



96
97
98
# File 'lib/retrospec/variable_store.rb', line 96

def self.resolve(variable)
  res = variable_resolution(variable)
end

.variable_resolution(variable) ⇒ Object

the problem is that when it tries to lookup file name pieces it doesn’t have the namespace ie. $file_name/test3183/$some_var



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/retrospec/variable_store.rb', line 68

def self.variable_resolution(variable)
  res = nil
  if variable.instance_of? Puppet::Parser::AST::VarDef
    res = lookup(variable.name.value)
    #add(variable.name, variable.value,false) unless res.nil?
  elsif variable.instance_of?(Puppet::Parser::AST::Variable)
    res = lookup(variable.value)
  elsif variable.instance_of?(Puppet::Parser::AST::Concat)
    begin
      res = variable.value.map { |i| variable_resolution(i)}.join.gsub("\"",'')
    rescue
      res = variable.value
    end
  else
    # I give up, I can't find the variable value so will just assign the variable name
    res = variable.to_s
  end
  unless res.nil?
    if variable.instance_of?(Puppet::Parser::AST::Variable)
      unless res = lookup(variable.to_s)
        #add(variable.to_s, res)
      end
    end
  end
  res
end