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
- #check_s3_folder_settings! ⇒ Object
- #current_region ⇒ Object
-
#extract_scripts(options = {}) ⇒ Object
Bash code that is meant to included in user-data.
-
#indent(text, indentation_amount) ⇒ Object
add indentation.
-
#partial(path, vars = {}, options = {}) ⇒ Object
The partial’s path is a relative path.
- #partial_exist?(path) ⇒ Boolean
- #scripts_name ⇒ Object
- #scripts_s3_path ⇒ Object
- #template_params(param_name) ⇒ Object
- #template_s3_path(template_name) ⇒ Object
-
#user_data(path, vars = {}, options = {}) ⇒ Object
Adjust the partial path so that it will use app/user_data.
Instance Method Details
#check_s3_folder_settings! ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lono/template/helper.rb', line 37 def check_s3_folder_settings! return if setting.s3_folder puts "Helper method called that requires the s3_folder to be set at:" lines = caller.reject { |l| l =~ %r{lib/lono} } # hide internal lono trace puts " #{lines[0]}" puts "Please configure your settings.yml with an s3_folder.".colorize(:red) puts "Detected AWS_PROFILE #{ENV['AWS_PROFILE'].inspect}" exit 1 end |
#current_region ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/lono/template/helper.rb', line 121 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 |
#extract_scripts(options = {}) ⇒ Object
Bash code that is meant to included in user-data
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lono/template/helper.rb', line 9 def extract_scripts(={}) check_s3_folder_settings! settings = setting.data["extract_scripts"] || {} = settings.merge() # defaults also here in case they are removed from settings to = [:to] || "/opt" user = [:as] || "ec2-user" if Dir.glob("#{Lono.config.scripts_path}/*").empty? puts "WARN: you are using the extract_scripts helper method but you do not have any app/scripts.".colorize(:yellow) calling_line = caller[0].split(':')[0..1].join(':') puts "Called from: #{calling_line}" return "" end <<-BASH_CODE # Generated from the lono extract_scripts helper. # Downloads scripts from s3, extract them, and setup. mkdir -p #{to} aws s3 cp #{scripts_s3_path} #{to}/ cd #{to} tar zxf #{to}/#{scripts_name} chmod -R a+x #{to}/scripts chown -R #{user}:#{user} #{to}/scripts BASH_CODE end |
#indent(text, indentation_amount) ⇒ Object
add indentation
109 110 111 112 113 |
# File 'lib/lono/template/helper.rb', line 109 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.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lono/template/helper.rb', line 95 def partial(path,vars={}, ={}) path = [: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, [:indent]) if [:indent] result + "\n" end |
#partial_exist?(path) ⇒ Boolean
115 116 117 118 119 |
# File 'lib/lono/template/helper.rb', line 115 def partial_exist?(path) path = partial_path_for(path) path = auto_add_format(path) path && File.exist?(path) end |
#scripts_name ⇒ Object
49 50 51 |
# File 'lib/lono/template/helper.rb', line 49 def scripts_name File.basename(scripts_s3_path) end |
#scripts_s3_path ⇒ Object
53 54 55 56 |
# File 'lib/lono/template/helper.rb', line 53 def scripts_s3_path upload = Lono::Script::Upload.new upload.s3_dest end |
#template_params(param_name) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lono/template/helper.rb', line 66 def template_params(param_name) = { allow_no_file: true }.merge(@options) generator = Lono::Param::Generator.new(param_name, ) # do not generate because lono cfn calling logic already generated it we only need the values params = generator.params # Returns Array in underscore keys format # convert Array to simplified hash structure params.inject({}) do |h, param| h.merge(param[:parameter_key] => param[:parameter_value]) end end |
#template_s3_path(template_name) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/lono/template/helper.rb', line 58 def template_s3_path(template_name) check_s3_folder_settings! # high jacking Upload for useful s3_https_url method template_path = "#{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
80 81 82 83 |
# File 'lib/lono/template/helper.rb', line 80 def user_data(path,vars={}, ={}) .merge!(user_data: true) partial(path,vars, ) end |