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
# File 'lib/terraspace/cli/init.rb', line 5

def initialize(options={})
  # terraform init output goes to default Terraspace.logger.info which is stderr
  # Unless the logger has been overridden.
  super(options.merge(log_to_stderr: true))
end

Instance Method Details

#auto?Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/terraspace/cli/init.rb', line 58

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

#build_remote_dependenciesObject

Currently only handles remote modules only one-level deep.



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

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



50
51
52
53
54
55
56
# File 'lib/terraspace/cli/init.rb', line 50

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::Perform.new(remote_mod).build
end

#initObject

Note the init will always create the Terraform Cloud Workspace



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

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



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

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

#sync_cloudObject



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

def sync_cloud
  Terraspace::Terraform::Tfc::Sync.new(@options).run if sync_cloud?
end

#sync_cloud?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/terraspace/cli/init.rb', line 34

def sync_cloud?
   %w[apply down plan up].include?(calling_command)
end