Class: Terraspace::Terraform::Runner

Inherits:
CLI::Base
  • Object
show all
Extended by:
Memoist
Includes:
Hooks::Concern, Util
Defined in:
lib/terraspace/terraform/runner.rb,
lib/terraspace/terraform/runner/retryer.rb

Defined Under Namespace

Classes: Retryer

Constant Summary collapse

@@current_dir_message_shown =
false

Instance Attribute Summary collapse

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

Methods included from Hooks::Concern

#run_hooks

Constructor Details

#initialize(name, options = {}) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
# File 'lib/terraspace/terraform/runner.rb', line 8

def initialize(name, options={})
  @name = name
  super(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#argsObject



61
62
63
64
# File 'lib/terraspace/terraform/runner.rb', line 61

def args
  # base at end in case of redirection. IE: terraform output > /path
  custom.args + custom.var_files + default.args
end

#current_dir_messageObject



50
51
52
53
54
# File 'lib/terraspace/terraform/runner.rb', line 50

def current_dir_message
  return if @@current_dir_message_shown
  log "Current directory: #{Terraspace::Util.pretty_path(@mod.cache_dir)}"
  @@current_dir_message_shown = true
end

#customObject



66
67
68
# File 'lib/terraspace/terraform/runner.rb', line 66

def custom
  Args::Custom.new(@mod, @name)
end

#defaultObject



71
72
73
# File 'lib/terraspace/terraform/runner.rb', line 71

def default
  Args::Default.new(@mod, @name, @options)
end

#log(msg) ⇒ Object



56
57
58
59
# File 'lib/terraspace/terraform/runner.rb', line 56

def log(msg)
  # quiet useful for RemoteState::Fetcher
  @options[:quiet] ? logger.debug(msg) : logger.info(msg)
end

#runObject



13
14
15
16
17
# File 'lib/terraspace/terraform/runner.rb', line 13

def run
  time_took do
    terraform(name, args)
  end
end

#run_internal_hook(type, name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/terraspace/terraform/runner.rb', line 39

def run_internal_hook(type, name)
  begin
    klass = "Terraspace::Terraform::Ihooks::#{type.to_s.classify}::#{name.classify}".constantize
  rescue NameError
    return
  end
  ihook = klass.new(name, @options)
  ihook.run
end

#terraform(name, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/terraspace/terraform/runner.rb', line 19

def terraform(name, *args)
  current_dir_message # only show once

  params = args.flatten.join(' ')
  command = "terraform #{name} #{params}".squish
  run_hooks("terraform.rb", name) do
    run_internal_hook(:before, name)
    Terraspace::Shell.new(@mod, command, @options.merge(env: custom.env_vars)).run
    run_internal_hook(:after, name)
  end
rescue Terraspace::SharedCacheError, Terraspace::InitRequiredError
  @retryer ||= Retryer.new(@mod, @options, name, $!)
  if @retryer.retry?
    @retryer.run
    retry
  else
    exit(1)
  end
end