Class: Ufo::TemplateScope
- Inherits:
-
Object
- Object
- Ufo::TemplateScope
- Extended by:
- Memoist
- Defined in:
- lib/ufo/template_scope.rb
Instance Attribute Summary collapse
-
#helper ⇒ Object
readonly
Returns the value of attribute helper.
-
#task_definition_name ⇒ Object
readonly
Returns the value of attribute task_definition_name.
Instance Method Summary collapse
-
#assign_instance_variables(vars) ⇒ Object
Add additional instance variables to template_scope.
- #cfn ⇒ Object
- #custom_properties(resource) ⇒ Object
- #default_elb_protocol ⇒ Object
- #default_target_group_protocol ⇒ Object
-
#initialize(helper = nil, task_definition_name = nil) ⇒ TemplateScope
constructor
A new instance of TemplateScope.
-
#load_variables_file(filename) ⇒ Object
Load the variables defined in ufo/variables/* to make available in the template blocks in ufo/templates/*.
- #network ⇒ Object
- #pretty_name? ⇒ Boolean
- #settings ⇒ Object
-
#substitute_variables!(properties) ⇒ Object
Substitute special variables that cannot be baked into the template because they are dynamically assigned.
Constructor Details
#initialize(helper = nil, task_definition_name = nil) ⇒ TemplateScope
Returns a new instance of TemplateScope.
7 8 9 10 11 12 13 |
# File 'lib/ufo/template_scope.rb', line 7 def initialize(helper=nil, task_definition_name=nil) @helper = helper @task_definition_name = task_definition_name # only available from task_definition # not available from params load_variables_file("base") load_variables_file(Ufo.env) end |
Instance Attribute Details
#helper ⇒ Object (readonly)
Returns the value of attribute helper.
5 6 7 |
# File 'lib/ufo/template_scope.rb', line 5 def helper @helper end |
#task_definition_name ⇒ Object (readonly)
Returns the value of attribute task_definition_name.
6 7 8 |
# File 'lib/ufo/template_scope.rb', line 6 def task_definition_name @task_definition_name end |
Instance Method Details
#assign_instance_variables(vars) ⇒ Object
Add additional instance variables to template_scope
41 42 43 44 45 |
# File 'lib/ufo/template_scope.rb', line 41 def assign_instance_variables(vars) vars.each do |k,v| instance_variable_set("@#{k}".to_sym, v) end end |
#cfn ⇒ Object
52 53 54 |
# File 'lib/ufo/template_scope.rb', line 52 def cfn Ufo::Setting::Profile.new(:cfn, settings[:cfn_profile]).data end |
#custom_properties(resource) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ufo/template_scope.rb', line 61 def custom_properties(resource) resource = resource.to_s.underscore properties = cfn[resource.to_sym] return unless properties # transform keys: camelize properties = properties.deep_stringify_keys.deep_transform_keys do |key| if key == key.upcase # trying to generalize special rule for dns.TTL key # leave key alone if key is already in all upcase else key.camelize end end substitute_variables!(properties) yaml = YAML.dump(properties) # add spaces in front on each line yaml.split("\n")[1..-1].map do |line| " #{line}" end.join("\n") + "\n" end |
#default_elb_protocol ⇒ Object
109 110 111 |
# File 'lib/ufo/template_scope.rb', line 109 def default_elb_protocol @elb_type == "application" ? "HTTP" : "TCP" end |
#default_target_group_protocol ⇒ Object
105 106 107 |
# File 'lib/ufo/template_scope.rb', line 105 def default_target_group_protocol default_elb_protocol end |
#load_variables_file(filename) ⇒ Object
Load the variables defined in ufo/variables/* to make available in the template blocks in ufo/templates/*.
Example:
`ufo/variables/base.rb`:
@name = "docker-process-name"
@image = "docker-image-name"
`ufo/templates/main.json.erb`:
{
"containerDefinitions": [
{
"name": "<%= @name %>",
"image": "<%= @image %>",
....
}
NOTE: Only able to make instance variables avaialble with instance_eval
Wasnt able to make local variables available.
35 36 37 38 |
# File 'lib/ufo/template_scope.rb', line 35 def load_variables_file(filename) path = "#{Ufo.root}/.ufo/variables/#{filename}.rb" instance_eval(IO.read(path), path) if File.exist?(path) end |
#network ⇒ Object
47 48 49 |
# File 'lib/ufo/template_scope.rb', line 47 def network Ufo::Setting::Profile.new(:network, settings[:network_profile]).data end |
#pretty_name? ⇒ Boolean
113 114 115 116 117 118 119 120 |
# File 'lib/ufo/template_scope.rb', line 113 def pretty_name? # env variable takes highest precedence if ENV["STATIC_NAME"] ENV["STATIC_NAME"] != "0" else settings[:pretty_name] end end |
#settings ⇒ Object
57 58 59 |
# File 'lib/ufo/template_scope.rb', line 57 def settings Ufo.settings end |
#substitute_variables!(properties) ⇒ Object
Substitute special variables that cannot be baked into the template because they are dynamically assigned. Only one special variable:
{stack_name}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ufo/template_scope.rb', line 88 def substitute_variables!(properties) # transform values and substitute for special values # https://stackoverflow.com/questions/34595142/process-nested-hash-to-convert-all-values-to-strings # # Examples: # "{stack_name}.stag.boltops.com." => development-demo-web.stag.boltops.com. # "{stack_name}.stag.boltops.com." => dev-demo-web-2.stag.boltops.com. properties.deep_merge(properties) do |_,_,v| if v.is_a?(String) v.sub!('{stack_name}', @stack_name) # unsure why need shebang, but it works else v end end properties end |