Class: Terraspace::Compiler::Cleaner::BackendChange

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/terraspace/compiler/cleaner/backend_change.rb

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(mod, options = {}) ⇒ BackendChange

Returns a new instance of BackendChange.



5
6
7
# File 'lib/terraspace/compiler/cleaner/backend_change.rb', line 5

def initialize(mod, options={})
  @mod, @options = mod, options
end

Instance Method Details

#current_backendObject



33
34
35
36
# File 'lib/terraspace/compiler/cleaner/backend_change.rb', line 33

def current_backend
  materialized_path = find_src_path("#{@mod.cache_dir}/backend*")
  IO.read(materialized_path) if materialized_path
end

#fresh_backendObject



38
39
40
41
# File 'lib/terraspace/compiler/cleaner/backend_change.rb', line 38

def fresh_backend
  src_path = find_src_path("#{Terraspace.root}/config/terraform/backend*")
  Terraspace::Compiler::Strategy::Mod.new(@mod, src_path).run if src_path
end

#local_statefile_exist?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/terraspace/compiler/cleaner/backend_change.rb', line 24

def local_statefile_exist?
  # Note: Will not go into .terraform folders. No need to for terraform.tfstate
  Dir.glob("#{Terraspace.cache_root}/**/*").each do |path|
    basename = File.basename(path)
    return true if basename == 'terraform.tfstate'
  end
  false
end

#purgeObject



9
10
11
12
13
14
15
16
# File 'lib/terraspace/compiler/cleaner/backend_change.rb', line 9

def purge
  return unless purge?

  are_you_sure? if local_statefile_exist?
  cache_root = Terraspace::Util.pretty_path(Terraspace.cache_root)
  logger.debug "Backend change detected. Removing #{cache_root} for complete reinitialization"
  FileUtils.rm_rf(Terraspace.cache_root)
end

#purge?Boolean

Whenever the backend is changed, purge the cache entirely

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/terraspace/compiler/cleaner/backend_change.rb', line 19

def purge?
  return false unless current_backend
  current_backend != fresh_backend
end