Module: Lono::Template::Helper

Included in:
Context
Defined in:
lib/lono/template/helper.rb

Overview

This is included into Lono::Template::Context. It has access to the original thor CLI options via @options.

Instance Method Summary collapse

Instance Method Details

#current_regionObject



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
107
108
109
110
111
112
# File 'lib/lono/template/helper.rb', line 73

def current_region
  region = Aws.config[:region]
  region ||= ENV['AWS_REGION']
  return region if region

  default_region = 'us-east-1' # fallback if default not found in ~/.aws/config
  if ENV['AWS_PROFILE']
    path = "#{ENV['HOME']}/.aws/config"
    if File.exist?(path)
      lines = IO.readlines(path)
      capture_default, capture_current = false, false
      lines.each do | line|
        if line.include?('[default]')
          capture_default = true # next line
          next
        end
        if capture_default && line.match(/region = /)
          # over default from above
          default_region = line.split(' = ').last.strip
          capture_default = false
        end

        md = line.match(/\[profile (.*)\]/)
        if md && md[1] == ENV['AWS_PROFILE']
          capture_current = true
          next
        end
        if capture_current && line.match(/region = /)
          region = line.split(' = ').last.strip
          capture_current = false
        end
      end
    end

    region ||= default_region
    return region if region
  end

  'us-east-1' # default
end

#indent(text, indentation_amount) ⇒ Object

add indentation



61
62
63
64
65
# File 'lib/lono/template/helper.rb', line 61

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

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

The partial’s path is a relative path.

Example: Given file in app/partials/iam/docker.yml

<%= partial("iam/docker", {}, indent: 10) %>
<%= partial("iam/docker.yml", {}, indent: 10) %>

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



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lono/template/helper.rb', line 47

def partial(path,vars={}, options={})
  path = options[:user_data] ?
            user_data_path_for(path) :
            partial_path_for(path)
  path = auto_add_format(path)

  instance_variables!(vars)
  result = render_path(path)

  result = indent(result, options[:indent]) if options[:indent]
  result + "\n"
end

#partial_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/lono/template/helper.rb', line 67

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

#template_params(param_name) ⇒ Object



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

def template_params(param_name)
  o = {
    allow_not_exists: true
  }.merge(@options)
  o["param"] = param_name
  generator = Lono::Param::Generator.new(o)
  # do not generate because lono cfn calling logic already generated it we only need the values
  parameters = generator.parameters    # Returns Array in underscore keys format
  # convert Array to simplified hash structure
  parameters.inject({}) do |h, param|
    h.merge(param[:parameter_key] => param[:parameter_value])
  end
end

#template_s3_path(template_name) ⇒ Object



10
11
12
13
14
15
# File 'lib/lono/template/helper.rb', line 10

def template_s3_path(template_name)
  # high jacking Upload for useful s3_https_url method
  template_path = "output/#{@blueprint}/templates/#{template_name}.yml"
  upload = Lono::Template::Upload.new(@options)
  upload.s3_https_url(template_path)
end

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

Adjust the partial path so that it will use app/user_data



32
33
34
35
# File 'lib/lono/template/helper.rb', line 32

def user_data(path,vars={}, options={})
  options.merge!(user_data: true)
  partial(path,vars, options)
end