Class: Terraspace::CLI::Init

Inherits:
Base
  • Object
show all
Defined in:
lib/terraspace/cli/init.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(options = {}) ⇒ Init

Returns a new instance of Init.



5
6
7
8
9
10
# File 'lib/terraspace/cli/init.rb', line 5

def initialize(options={})
  # Original calling command. Can be from Commander which is a terraform command. IE: terraform apply
  # Or can be from terraspace cloud setup. Which will be cloud-setup.
  @calling_command = options[:calling_command]
  super(options)
end

Instance Method Details

#auto?Boolean

Returns:

  • (Boolean)


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

def auto?
  # command is only passed from CLI in the update specifically for this check
  @options[:auto] && @calling_command == "apply"
end

#build_remote_dependenciesObject

Currently only handles remote modules only one-level deep.



36
37
38
39
40
41
42
43
44
45
# File 'lib/terraspace/cli/init.rb', line 36

def build_remote_dependencies
  modules_json_path = "#{@mod.cache_dir}/.terraform/modules/modules.json"
  return unless File.exist?(modules_json_path)

  initialized_modules = JSON.load(IO.read(modules_json_path))
  # For example of structure see spec/fixtures/initialized/modules.json
  initialized_modules["Modules"].each do |meta|
    build_remote_mod(meta)
  end
end

#build_remote_mod(meta) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/terraspace/cli/init.rb', line 47

def build_remote_mod(meta)
  return if local_source?(meta["Source"])
  return if meta['Dir'] == '.' # root is already built

  remote_mod = Terraspace::Mod::Remote.new(meta, @mod)
  Terraspace::Compiler::Builder.new(remote_mod).build
end

#initObject

Note the init will always create the Terraform Cloud Workspace



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/terraspace/cli/init.rb', line 19

def init
  return unless run_init? # check here because RemoteState::Fetcher#pull calls init directly
  # default init timeout is pretty generous in case of slow internet to download the provider plugins
  init_timeout = Integer(ENV['TS_INIT_TIMEOUT'] || 600)
  Timeout::timeout(init_timeout) do
    Terraspace::Terraform::Runner.new("init", @options).run if !auto? && @options[:init] != false # will run on @options[:init].nil?
  end
rescue Timeout::Error
  logger.error "ERROR: It took too long to run terraform init.  Here is the output logs of terraform init:".color(:red)
  logger.error IO.read(Terraspace::Terraform::Args::Default.terraform_init_log)
end

#runObject



12
13
14
15
16
# File 'lib/terraspace/cli/init.rb', line 12

def run
  init
  # build_remote_dependencies # runs after terraform init, which downloads remote modules
  sync_cloud
end

#sync_cloudObject



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

def sync_cloud
  Terraspace::Terraform::Cloud::Sync.new(@options).run if %w[apply plan destroy cloud-setup].include?(@calling_command)
end