Class: Terraspace::Mod

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util
Defined in:
lib/terraspace/mod.rb,
lib/terraspace/mod/remote.rb

Overview

Example properties:

name: vpc
root: app/modules/vpc, app/stacks/vpc, vendor/modules/vpc or vendor/stacks/vpc
type: module or stack

Direct Known Subclasses

Remote

Defined Under Namespace

Classes: Remote

Instance Attribute Summary collapse

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

Constructor Details

#initialize(name, options = {}) ⇒ Mod

Returns a new instance of Mod.



14
15
16
17
18
19
# File 'lib/terraspace/mod.rb', line 14

def initialize(name, options={})
  @name, @options = placeholder(name), options
  @consider_stacks = options[:consider_stacks].nil? ? true : options[:consider_stacks]
  @extra = Terraspace.extra || options[:instance]
  @resolved = true # more common case
end

Instance Attribute Details

#consider_stacksObject (readonly)

Returns the value of attribute consider_stacks.



12
13
14
# File 'lib/terraspace/mod.rb', line 12

def consider_stacks
  @consider_stacks
end

#instanceObject (readonly)

Returns the value of attribute instance.



12
13
14
# File 'lib/terraspace/mod.rb', line 12

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/terraspace/mod.rb', line 12

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/terraspace/mod.rb', line 12

def options
  @options
end

#resolvedObject

dependencies resolved



13
14
15
# File 'lib/terraspace/mod.rb', line 13

def resolved
  @resolved
end

#root_moduleObject

Returns the value of attribute root_module.



29
30
31
# File 'lib/terraspace/mod.rb', line 29

def root_module
  @root_module
end

Instance Method Details

#build_dir(disable_extra: false) ⇒ Object

Relative folder path without app or vendor. For example, the actual location can be found in a couple of places

app/modules/vpc
app/stacks/vpc
vendor/modules/vpc
vendor/stacks/vpc

The build folder does not include the app or vendor info.

modules/vpc


102
103
104
105
106
107
108
109
110
111
# File 'lib/terraspace/mod.rb', line 102

def build_dir(disable_extra: false)
  if !@extra.nil? && type_dir == "stacks" && !disable_extra
    # add _ in front so instance doesnt collide with other default stacks
    # never add for app/modules sources
    instance_name = [name, @extra].compact.join('.')
  else
    instance_name = name
  end
  [type_dir, instance_name].compact.join('/')
end

#cache_dir(options = {}) ⇒ Object

Full path with build_dir



114
115
116
117
118
119
120
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
# File 'lib/terraspace/mod.rb', line 114

def cache_dir(options={})
  # config.build.cache_dir is a String or object that respond_to call. IE:
  #   :REGION/:ENV/:BUILD_DIR
  #   CustomBuildDir.call
  # The call method should return a String pattern used for substitutions
  object = Terraspace.config.build.cache_dir
  pattern = if object.is_a?(String)
      object
    elsif object.respond_to?(:call)
      object.call(self)
    elsif object.public_instance_methods.include?(:call)
      instance = object.new
      instance.call(self)
    else
      raise "ERROR: config.build.cache_dir is not a String or responds to the .call method."
    end

  if pattern.include?(":CACHE_ROOT")
    old_pattern = pattern
    pattern = pattern.sub(':CACHE_ROOT/', '')
    logger.info "WARN: Detected :CACHE_ROOT in config.build.cache_dir".color(:yellow)
    logger.info <<~EOL
      This has been deprecated and :CACHE_ROOT should not be in the config.build.cache_dir setting.
      The :CACHE_ROOT pattern has been removed from config.build.cache_dir automatically.

      To remove this warning, remove the :CACHE_ROOT. For example:

      config/app.rb

          config.build.cache_dir = "#{old_pattern}"

      Update it to:

          config.build.cache_dir = "#{pattern}"

    EOL
  end

  path = expansion(pattern)
  path = "#{Terraspace.cache_root}/#{path}"
  path.gsub!(%r{/+},'/') # remove double slashes are more. IE: // -> / Useful since region is '' in generic expander
  path
end

#check_exist!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/terraspace/mod.rb', line 34

def check_exist!
  return if root

  pretty_paths = paths.map { |p| Terraspace::Util.pretty_path(p) }.join(", ")
  logger.error <<~EOL
    ERROR: Unable to find #{@name.color(:green)}. Searched paths:

        #{pretty_paths}

    To see available stacks, try running:

        terraspace list

  EOL
  ENV['TS_TEST'] ? raise : exit(1)
end

#exist?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/terraspace/mod.rb', line 87

def exist?
  !!root
end

#expansion(pattern) ⇒ Object



159
160
161
162
# File 'lib/terraspace/mod.rb', line 159

def expansion(pattern)
  @expander ||= Terraspace::Compiler::Expander.autodetect(self)
  @expander.expansion(pattern) # pattern is a String that contains placeholders for substitutions
end

#out_optionObject



164
165
166
167
# File 'lib/terraspace/mod.rb', line 164

def out_option
  expand = Terraspace::Terraform::Args::Expand.new(self, @options)
  expand.out
end

#placeholder(name) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/terraspace/mod.rb', line 21

def placeholder(name)
  if name == "placeholder"
    Terraspace::CLI::Build::Placeholder.new(@options).find_stack
  else
    name
  end
end

#possible_fake_rootObject

If the app/stacks/NAME has been removed in source code but stack still exist in the cloud. allow user to delete by materializing an empty stack with the backend.tf Note this does not seem to work for Terraform Cloud as terraform init doesnt seem to download the plugins required. It only works for s3, azurerm, and gcs backends. On TFC, you can delete the stack via the GUI though.

down - so user can delete stacks w/o needing to create an empty app/stacks/demo folder
null - for the terraspace summary command when there are zero stacks.
       Also useful for terraspace tfc list_workspaces


81
82
83
84
85
# File 'lib/terraspace/mod.rb', line 81

def possible_fake_root
  if @options[:command] == "down"
    "#{Terraspace.root}/app/stacks/#{@name}" # fake stack root
  end
end

#rootObject



62
63
64
65
66
67
68
69
# File 'lib/terraspace/mod.rb', line 62

def root
  root = paths.find { |p| File.exist?(p) }
  if root.nil?
    possible_fake_root
  else
    root
  end
end

#root_module?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/terraspace/mod.rb', line 30

def root_module?
  @root_module
end

#to_infoObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/terraspace/mod.rb', line 51

def to_info
  {
    build_dir: build_dir,
    cache_dir: Terraspace::Util.pretty_path(cache_dir),
    name: name,
    root: Terraspace::Util.pretty_path(root),
    type: type,
    type_dir: type_dir,
  }
end

#typeObject



169
170
171
# File 'lib/terraspace/mod.rb', line 169

def type
  root.include?("/stacks/") ? "stack" : "module"
end

#type_dirObject



173
174
175
# File 'lib/terraspace/mod.rb', line 173

def type_dir
  type.pluralize
end