Class: Terraspace::Builder

Inherits:
CLI::Base show all
Includes:
Compiler::CommandsConcern, Compiler::DirsConcern, Hooks::Concern
Defined in:
lib/terraspace/builder.rb,
lib/terraspace/builder/allow.rb

Defined Under Namespace

Classes: Allow

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks::Concern

#run_hooks

Methods included from Compiler::DirsConcern

#cache_dirs, #dirs, #local_paths, #mod_names, #select_stack?, #stack_names, #with_each_mod

Methods included from Compiler::CommandsConcern

#command_is?, #requires_backend?, #requires_backend_commands

Methods inherited from CLI::Base

#initialize

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Constructor Details

This class inherits a constructor from Terraspace::CLI::Base

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



7
8
9
# File 'lib/terraspace/builder.rb', line 7

def graph
  @graph
end

Instance Method Details

#auto_create_backendObject

Auto create after build_unresolved since will need to run state pull for dependencies



70
71
72
73
74
# File 'lib/terraspace/builder.rb', line 70

def auto_create_backend
  return if Terraspace.config.auto_create_backend == false
  return unless requires_backend?
  Terraspace::Compiler::Backend.new(@mod).create
end

#build_allObject



42
43
44
45
46
47
# File 'lib/terraspace/builder.rb', line 42

def build_all
  # At this point dependencies have been resolved.
  Terraspace::Terraform::RemoteState::Fetcher.flush!
  @resolved = true
  build_unresolved
end

#build_batchesObject

Builds dependency graph and returns the batches to run



36
37
38
39
40
# File 'lib/terraspace/builder.rb', line 36

def build_batches
  dependencies = Terraspace::Dependency::Registry.data # populated after build_unresolved
  @graph = Terraspace::Dependency::Graph.new(stack_names, dependencies, @options)
  @graph.build
end

#build_dir(type_dir) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/terraspace/builder.rb', line 60

def build_dir(type_dir)
  with_each_mod(type_dir) do |mod|
    mod.resolved = @resolved
    is_root_module = mod.cache_dir == @mod.cache_dir
    next if is_root_module # handled by build_root_module
    Compiler::Builder.new(mod).build
  end
end

#build_root_moduleObject



55
56
57
58
# File 'lib/terraspace/builder.rb', line 55

def build_root_module
  @mod.resolved = @resolved
  Compiler::Builder.new(@mod).build
end

#build_unresolvedObject



49
50
51
52
53
# File 'lib/terraspace/builder.rb', line 49

def build_unresolved
  build_dir("modules")
  build_dir("stacks")
  build_root_module
end

#check_allow!Object



31
32
33
# File 'lib/terraspace/builder.rb', line 31

def check_allow!
  Allow.new(@mod).check!
end

#cleanObject



76
77
78
# File 'lib/terraspace/builder.rb', line 76

def clean
  Compiler::Cleaner.new(@mod, @options).clean if clean?
end

#clean?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/terraspace/builder.rb', line 80

def clean?
  if @options[:clean].nil?
    clean_cache = Terraspace.config.build.clean_cache
    clean_cache.nil? ? true : clean_cache
  else
    @options[:clean]
  end
end

#placeholder_stack_messageObject



89
90
91
92
93
# File 'lib/terraspace/builder.rb', line 89

def placeholder_stack_message
  return if @options[:quiet]
  return unless @options[:mod] == "placeholder"
  logger.info "Building one stack to build all stacks"
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/terraspace/builder.rb', line 9

def run
  return if @options[:build] == false
  Terraspace::CLI::CheckSetup.check!
  @mod.root_module = true
  clean
  build_dir = Util.pretty_path(@mod.cache_dir)
  placeholder_stack_message
  logger.info "Building #{build_dir}" unless @options[:quiet] # from terraspace all

  batches = nil
  FileUtils.mkdir_p(@mod.cache_dir) # so terraspace before build hooks work
  run_hooks("terraspace.rb", "build") do
    check_allow!
    build_unresolved
    auto_create_backend
    batches = build_batches
    build_all
    logger.info "Built in #{build_dir}" unless @options[:quiet] # from terraspace all
  end
  batches
end