Class: Terraspace::Compiler::Strategy::Tfvar::Layer

Inherits:
Object
  • Object
show all
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

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Methods included from Plugin::Expander::Friendly

#friendly_name

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

#app_stacks_tfvars_dirObject

IE: app/stacks/demo/tfvars



141
142
143
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 141

def app_stacks_tfvars_dir
  "#{@mod.root}/tfvars"
end

#config_stacks_tfvars_dirObject

IE: config/stacks/demo/tfvars



146
147
148
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 146

def config_stacks_tfvars_dir
  "#{Terraspace.root}/config/#{@mod.build_dir(disable_extra: true)}/tfvars"
end

#config_terraform_tfvars_dirObject

IE: config/terraform/tfvars



136
137
138
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 136

def config_terraform_tfvars_dir
  "#{Terraspace.root}/config/terraform/tfvars"
end

#full_layering(tfvars_dir, remove_first: false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 67

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 53

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}"


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 91

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

#pathsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 41

def paths
  config_terraform = full_paths(config_terraform_tfvars_dir) # deprecated
  app_stacks = full_paths(app_stacks_tfvars_dir) # deprecated
  config_stacks = full_paths(config_stacks_tfvars_dir)   # recommended
  paths = (config_terraform + app_stacks + config_stacks).uniq
  show_layers(paths)
  paths.select do |path|
    File.exist?(path)
  end
end

#pluginsObject



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
133
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 106

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

#show_layers(paths) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/terraspace/compiler/strategy/tfvar/layer.rb', line 151

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']
      message = "    #{pretty_path(path)}"
      message = "#{message} (found)".color(:yellow) if File.exist?(path)
      logger.info message
    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