Class: Lono::Template::Template

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/lono/template/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, block, options = {}) ⇒ Template

Returns a new instance of Template.



9
10
11
12
13
# File 'lib/lono/template/template.rb', line 9

def initialize(name, block, options={})
  @name = name
  @block = block
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/lono/template/template.rb', line 8

def name
  @name
end

Instance Method Details

#base64(value) ⇒ Object



100
101
102
# File 'lib/lono/template/template.rb', line 100

def base64(value)
  %Q|{"Fn::Base64"=>"#{value}"}|
end

#bracket_positions(line) ⇒ Object

Input:

String

Output:

Array of parse positions

The positions of tokens taking into account when brackets start and close, handles nested brackets.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/lono/template/template.rb', line 142

def bracket_positions(line)
  positions,pair,count = [],[],0

  line.split('').each_with_index do |char,i|
    pair << i if pair.empty?

    first_pair_char = line[pair[0]]
    if first_pair_char == '{' # object logic
      if char == '{'
        count += 1
      end

      if char == '}'
        count -= 1
        if count == 0
          pair << i
          positions << pair
          pair = []
        end
      end
    else # string logic
      lookahead = line[i+1]
      if lookahead == '{'
        pair << i
        positions << pair
        pair = []
      end
    end
  end # end of loop

  # for string logic when lookahead does not contain a object token
  # need to clear out what's left to match the final pair
  if !pair.empty?
    pair << line.size - 1
    positions << pair
  end

  positions
end

#buildObject



15
16
17
18
19
# File 'lib/lono/template/template.rb', line 15

def build
  instance_eval(&@block)
  template = IO.read(@source)
  erb_result(@source, template)
end

#cfn_object?(s) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
235
# File 'lib/lono/template/template.rb', line 229

def cfn_object?(s)
  exact = %w[Ref]
  pattern = %w[Fn::]
  exact_match = !!exact.detect {|word| s.include?(word)}
  pattern_match = !!pattern.detect {|p| s =~ Regexp.new(p)}
  (exact_match || pattern_match) && s =~ /^{/ && s =~ /=>/
end

#decompose(line) ⇒ Object

Input

String line of code to decompose into chunks, some can be transformed into objects

Output

Array of strings, some can be transformed into objects

Example: line = ‘abcd{dd}e’ # nested brackets template.decompose(line).should == [‘a’,‘b’,‘c’,‘d{dd}’,‘e’]



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/lono/template/template.rb', line 199

def decompose(line)
  positions = parse_positions(line)
  return [line] if positions.empty?

  result = []
  str = ''
  until positions.empty?
    left = positions.shift
    right = positions.shift
    token = line[left..right]
    # if cfn object, add to the result set but after clearing out
    # the temp str that is being built up when the token is just a string
    if cfn_object?(token)
      unless str.empty? # first token might be a object
        result << str
        str = ''
      end
      result << token
    else
      str << token # keeps building up the string
    end
  end

  # at the of the loop there's a leftover string, unless the last token
  # is an object
  result << str unless str.empty?

  result
end

#encode_base64(text) ⇒ Object

For simple just parameters files that can also be generated with lono, the CFN Fn::Base64 function is not available and as lono is not being used in the context of CloudFormation. So this can be used in it’s place.



248
249
250
# File 'lib/lono/template/template.rb', line 248

def encode_base64(text)
  Base64.strict_encode64(text).strip
end

#erb_result(path, template) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lono/template/template.rb', line 47

def erb_result(path, template)
  begin
    ERB.new(template, nil, "-").result(binding)
  rescue Exception => e
    puts e

    # how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051
    # syntax errors have the (erb):xxx info in e.message
    # undefined variables have (erb):xxx info in e.backtrac
    error_info = e.message.split("\n").grep(/\(erb\)/)[0]
    error_info ||= e.backtrace.grep(/\(erb\)/)[0]
    raise unless error_info # unable to find the (erb):xxx: error line
    line = error_info.split(':')[1].to_i
    puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '')}"

    template_lines = template.split("\n")
    context = 5 # lines of context
    top, bottom = [line-context-1, 0].max, line+context-1
    spacing = template_lines.size.to_s.size
    template_lines[top..bottom].each_with_index do |line_content, index|
      line_number = top+index+1
      if line_number == line
        printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content)
      else
        printf("%#{spacing}d %s\n", line_number, line_content)
      end
    end
    exit 1 unless ENV['TEST']
  end
end

#evaluate(line) ⇒ Object



241
242
243
# File 'lib/lono/template/template.rb', line 241

def evaluate(line)
  recompose(decompose(line))
end

#find_in_map(*args) ⇒ Object



96
97
98
# File 'lib/lono/template/template.rb', line 96

def find_in_map(*args)
  %Q|{"Fn::FindInMap" => [ #{transform_array(args)} ]}|
end

#get_att(*args) ⇒ Object



104
105
106
# File 'lib/lono/template/template.rb', line 104

def get_att(*args)
  %Q|{"Fn::GetAtt" => [ #{transform_array(args)} ]}|
end

#get_azs(region = "AWS::Region") ⇒ Object



108
109
110
# File 'lib/lono/template/template.rb', line 108

def get_azs(region="AWS::Region")
  %Q|{"Fn::GetAZs"=>"#{region}"}|
end

#indent(result, indentation_amount) ⇒ Object

add indentation



41
42
43
44
45
# File 'lib/lono/template/template.rb', line 41

def indent(result, indentation_amount)
  result.split("\n").map do |line|
    " " * indentation_amount + line
  end.join("\n")
end

#join(delimiter, values) ⇒ Object



112
113
114
# File 'lib/lono/template/template.rb', line 112

def join(delimiter, values)
  %Q|{"Fn::Join" => ["#{delimiter}", [ #{transform_array(values)} ]]}|
end

#parse_positions(line) ⇒ Object

Input:

Array - bracket_positions

Ouput:

Array - positions that can be use to determine what to parse


186
187
188
189
# File 'lib/lono/template/template.rb', line 186

def parse_positions(line)
  positions = bracket_positions(line)
  positions.flatten
end

#partial(path, vars = {}, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/lono/template/template.rb', line 31

def partial(path,vars={}, options={})
  path = "#{@options[:project_root]}/templates/partial/#{path}"
  template = IO.read(path)
  variables(vars)
  result = erb_result(path, template)
  result = indent(result, options[:indent]) if options[:indent]
  result
end

#recompose(decomposition) ⇒ Object



237
238
239
# File 'lib/lono/template/template.rb', line 237

def recompose(decomposition)
  decomposition.map { |s| cfn_object?(s) ? eval(s) : s }
end

#ref(name) ⇒ Object



92
93
94
# File 'lib/lono/template/template.rb', line 92

def ref(name)
  %Q|{"Ref"=>"#{name}"}|
end

#select(index, list) ⇒ Object



116
117
118
# File 'lib/lono/template/template.rb', line 116

def select(index, list)
  %Q|{"Fn::Select" => ["#{index}", [ #{transform_array(list)} ]]}|
end

#source(path) ⇒ Object



21
22
23
# File 'lib/lono/template/template.rb', line 21

def source(path)
  @source = path[0..0] == '/' ? path : "#{@options[:project_root]}/templates/#{path}"
end

#transform(data) ⇒ Object

transform each line of bash script to array with cloudformation template objects



126
127
128
129
130
131
132
133
# File 'lib/lono/template/template.rb', line 126

def transform(data)
  data = evaluate(data)
  if data[-1].is_a?(String)
    data[0..-2] + ["#{data[-1]}\n"]
  else
    data + ["\n"]
  end
end

#transform_array(arr) ⇒ Object



120
121
122
123
# File 'lib/lono/template/template.rb', line 120

def transform_array(arr)
  arr.map! {|x| x =~ /=>/ ? x : x.inspect }
  arr.join(',')
end

#user_data(path, vars = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lono/template/template.rb', line 78

def user_data(path, vars={})
  path = "#{@options[:project_root]}/templates/user_data/#{path}"
  template = IO.read(path)
  variables(vars)
  result = erb_result(path, template)
  output = []
  result.split("\n").each do |line|
    output += transform(line)
  end
  json = output.to_json
  json[0] = '' # remove first char: [
  json.chop!   # remove last char:  ]
end

#variables(vars = {}) ⇒ Object



25
26
27
28
29
# File 'lib/lono/template/template.rb', line 25

def variables(vars={})
  vars.each do |var,value|
    instance_variable_set("@#{var}", value)
  end
end