Module: Lono::Template::Helpers

Included in:
Template
Defined in:
lib/lono/template/helpers.rb

Instance Method Summary collapse

Instance Method Details

#base64(value) ⇒ Object



53
54
55
# File 'lib/lono/template/helpers.rb', line 53

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

#find_in_map(*args) ⇒ Object



49
50
51
# File 'lib/lono/template/helpers.rb', line 49

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

#get_att(*args) ⇒ Object



57
58
59
# File 'lib/lono/template/helpers.rb', line 57

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

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



61
62
63
# File 'lib/lono/template/helpers.rb', line 61

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

#indent(text, indentation_amount) ⇒ Object

add indentation



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

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

#join(delimiter, values) ⇒ Object



65
66
67
# File 'lib/lono/template/helpers.rb', line 65

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

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

The partial’s path is a relative path given without the extension and

Example: Given: file in templates/partial/iam/docker.yml The path should be: iam/docker

If the user specifies the extension then use that instead of auto-adding the detected format.



87
88
89
90
91
92
93
94
95
96
# File 'lib/lono/template/helpers.rb', line 87

def partial(path,vars={}, options={})
  path = partial_path_for(path)
  path = auto_add_format(path)

  template = IO.read(path)
  variables(vars)
  result = erb_result(path, template)
  result = indent(result, options[:indent]) if options[:indent]
  result
end

#partial_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/lono/template/helpers.rb', line 73

def partial_exist?(path)
  path = partial_path_for(path)
  path = auto_add_format(path)
  path && File.exist?(path)
end

#ref(name) ⇒ Object



45
46
47
# File 'lib/lono/template/helpers.rb', line 45

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

#select(index, list) ⇒ Object



69
70
71
# File 'lib/lono/template/helpers.rb', line 69

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

#template_params(param_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/lono/template/helpers.rb', line 19

def template_params(param_name)
  param_path = "params/#{Lono.env}/#{param_name}.txt"
  generator_options = {
    path: param_path,
    allow_no_file: true
  }.merge(@_options)
  generator = Lono::Param::Generator.new(param_name, generator_options)
  # do not generate because lono cfn calling logic already generated it we only need the values
  generator.params    # Returns Array in underscore keys format
end

#template_s3_path(template_name) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/lono/template/helpers.rb', line 2

def template_s3_path(template_name)
  format = @_detected_format.sub('yaml','yml')
  template_path = "#{template_name}.#{format}"

  # must have settings.s3_path for this to owrk
  settings = Lono::Settings.new
  if settings.s3_path
    # high jacking Upload for useful s3_https_url method
    upload = Lono::Template::Upload.new(@_options)
    upload.s3_https_url(template_path)
  else
    message = "template_s3_path helper called but s3.path not configured in settings.yml"
    puts "WARN: #{message}".colorize(:yellow)
    message
  end
end

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

Really only useful if using json format



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lono/template/helpers.rb', line 31

def user_data(path, vars={})
  path = "#{Lono.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