Class: Terraspace::Compiler::Strategy::Tfvar::Layer
- Inherits:
-
Object
- Object
- Terraspace::Compiler::Strategy::Tfvar::Layer
- Extended by:
- Memoist
- Includes:
- Layering, Plugin::Expander::Friendly, Util
- Defined in:
- lib/terraspace/compiler/strategy/tfvar/layer.rb
Constant Summary collapse
- @@shown_layers =
{}
Instance Method Summary collapse
- #full_layering(tfvars_dir, remove_first: false) ⇒ Object
- #full_paths(tfvars_dir) ⇒ Object
-
#initialize(mod) ⇒ Layer
constructor
A new instance of Layer.
-
#layer_levels(prefix = nil) ⇒ Object
adds prefix and to each layer pair that has base and Terraspace.env.
- #paths ⇒ Object
- #plugins ⇒ Object
- #project_tfvars_dir ⇒ Object
- #show_layers(paths) ⇒ Object
-
#stack_tfvars_dir ⇒ Object
seed dir takes higher precedence than the tfvars folder within the stack module.
Methods included from Util::Pretty
Methods included from Util::Sure
Methods included from Util::Logging
Methods included from Plugin::Expander::Friendly
Methods included from Layering
#layers, #main_layers, #post_layers, #pre_layers
Constructor Details
#initialize(mod) ⇒ Layer
Returns a new instance of Layer.
37 38 39 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 37 def initialize(mod) @mod = mod end |
Instance Method Details
#full_layering(tfvars_dir, remove_first: false) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 66 def full_layering(tfvars_dir, remove_first: false) # layers defined in Terraspace::Layering module all = layers.map { |layer| layer.sub(/\/$/,'') } # strip trailing slash all = all.inject([]) do |sum, layer| sum += layer_levels(layer) unless layer.nil? sum end all = all.reject { |layer| layer.ends_with?('-') } all.map! do |layer| layer = layer.blank? ? layer : "/#{layer}" [ "#{tfvars_dir}#{layer}.tfvars", "#{tfvars_dir}#{layer}.rb", ] end.flatten! all.shift if remove_first # IE: app/stacks/demo/tfvars.tfvars all end |
#full_paths(tfvars_dir) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 52 def full_paths(tfvars_dir) layers = full_layering(tfvars_dir, remove_first: true) if Terraspace.role layers += full_layering("#{tfvars_dir}/#{Terraspace.role}") end if Terraspace.app layers += full_layering("#{tfvars_dir}/#{Terraspace.app}") end if Terraspace.app && Terraspace.role layers += full_layering("#{tfvars_dir}/#{Terraspace.app}/#{Terraspace.role}") end layers end |
#layer_levels(prefix = nil) ⇒ Object
adds prefix and to each layer pair that has base and Terraspace.env. IE:
"#{prefix}/base"
"#{prefix}/#{Terraspace.env}"
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 90 def layer_levels(prefix=nil) if @mod.instance logger.info "WARN: The instance option is deprecated. Instead use TS_EXTRA" logger.info "See: http://terraspace.test/docs/layering/instance-option/" end extra = Terraspace.extra || @mod.instance levels = ["base", Terraspace.env, extra, "#{Terraspace.env}-#{extra}"].reject(&:blank?) # layer levels. @mod.instance can be nil levels.map! do |i| # base layer has prefix of '', reject with blank so it doesnt produce '//' [prefix, i].reject(&:blank?).join('/') end levels.unshift(prefix) if !prefix.nil? levels end |
#paths ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 41 def paths project_paths = full_paths(project_tfvars_dir) stack_paths = full_paths(stack_tfvars_dir) paths = (project_paths + stack_paths).uniq show_layers(paths) paths.select do |path| File.exist?(path) end end |
#plugins ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 105 def plugins layers = [] Terraspace::Plugin.layer_classes.each do |klass| layer = klass.new # region is high up because its simpler and the more common case is a single provider layers << layer.region namespace = friendly_name(layer.namespace) mode = Terraspace.config.layering.mode # simple, namespace, provider if mode == "namespace" || mode == "provider" # namespace is a simple way keep different tfvars between different engineers on different accounts layers << namespace layers << "#{namespace}/#{layer.region}" end if mode == "provider" # in case using multiple providers and one region layers << layer.provider layers << "#{layer.provider}/#{layer.region}" # also in case another provider has colliding regions # Most general layering layers << "#{layer.provider}/#{namespace}" layers << "#{layer.provider}/#{namespace}/#{layer.region}" end end layers end |
#project_tfvars_dir ⇒ Object
134 135 136 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 134 def project_tfvars_dir "#{Terraspace.root}/config/terraform/tfvars" end |
#show_layers(paths) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 158 def show_layers(paths) return unless @mod.resolved return if @@shown_layers[@mod.name] logger.debug "Layers for #{@mod.name}:" show = Terraspace.config.layering.show || ENV['TS_LAYERING_SHOW_ALL'] paths.each do |path| next if ARGV[0] == "all" # dont show layers with all command since fork happens after build next unless path.include?('.tfvars') if ENV['TS_LAYERING_SHOW_ALL'] = " #{pretty_path(path)}" = "#{} (found)".color(:yellow) if File.exist?(path) logger.info elsif show logger.info " #{pretty_path(path)}" if File.exist?(path) end end # do not logger.info "" it creates a newline with all @@shown_layers[@mod.name] = true end |
#stack_tfvars_dir ⇒ Object
seed dir takes higher precedence than the tfvars folder within the stack module. Example:
seed/tfvars/stacks/demo (folder must have *.tfvars or *.rb files)
app/stacks/demo/tfvars
This allows user to take over the tfvars embedded in the stack if they need to. Generally, putting tfvars in within the app/stacks/MOD/tfvars folder seems cleaner and easier to follow.
Will also consider app/modules/demo/tfvars. Though modules to be reuseable and stacks is where business logic should go.
149 150 151 152 153 154 155 |
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 149 def stack_tfvars_dir seed_dir = "#{Terraspace.root}/seed/tfvars/#{@mod.build_dir(disable_extra: true)}" mod_dir = "#{@mod.root}/tfvars" empty = Dir.glob("#{seed_dir}/*").empty? empty ? mod_dir : seed_dir end |