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,
lib/terraspace/builder/children.rb

Defined Under Namespace

Classes: Allow, Children

Instance Method Summary collapse

Methods included from Hooks::Concern

#run_hooks

Methods included from Compiler::DirsConcern

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

Methods included from Compiler::CommandsConcern

#command_is?

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Builder

terraspace all:

none: dont build any stacks at all. used by `terraspace all up`
root_only: only build root stack. used by `terraspace all up`
root_with_children: build all children stacks as well as the root stack. normal `terraspace all down`

terraspace up:

root_with_children: build all children stacks as well as the root stack. normal `terraspace up`


19
20
21
22
# File 'lib/terraspace/builder.rb', line 19

def initialize(options={})
  super
  @include_stacks = @options[:include_stacks] || :root_with_children
end

Instance Method Details

#build(modules: true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/terraspace/builder.rb', line 39

def build(modules: true)
  build_dir = Util.pretty_path(@mod.cache_dir)
  placeholder_stack_message
  logger.info "Building #{build_dir}" unless @options[:quiet] # from terraspace all
  FileUtils.mkdir_p(@mod.cache_dir) # so terraspace before build hooks work
  run_hooks("terraspace.rb", "build") do
    build_dir("modules") if modules
    build_stacks
    logger.debug "Built in #{build_dir}"
  end
end

#build_children_stacksObject

Build stacks that are part of the dependency graph. Because .terraspace-cache folders need to exist so ‘terraform state pull` works to get the state info.



63
64
65
66
# File 'lib/terraspace/builder.rb', line 63

def build_children_stacks
  children = Children.new(@mod, @options)
  children.build
end

#build_dir(type_dir) ⇒ Object



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

def build_dir(type_dir)
  with_each_mod(type_dir) do |mod|
    is_root_module = mod.cache_dir == @mod.cache_dir
    next if is_root_module # handled by build_stacks
    Compiler::Perform.new(mod, type_dir: type_dir).compile
  end
end

#build_stacksObject



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

def build_stacks
  return if @include_stacks == :none
  build_children_stacks if @include_stacks == :root_with_children
  Compiler::Perform.new(@mod).compile # @include_stacks :root or :root_with_children
end

#check_allow!Object



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

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

#resolve_dependenciesObject



34
35
36
37
# File 'lib/terraspace/builder.rb', line 34

def resolve_dependencies
  resolver = Terraspace::Dependency::Resolver.new(@options.merge(quiet: true))
  resolver.resolve # returns batches
end

#runObject



24
25
26
27
28
29
30
31
32
# File 'lib/terraspace/builder.rb', line 24

def run
  return if @options[:build] == false
  Terraspace::Check.check!
  check_allow!
  @mod.root_module = true
  clean
  resolve_dependencies if @include_stacks == :root_with_children
  build
end