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/backend.rb,
lib/terraspace/terraform/runner/retryer.rb

Defined Under Namespace

Classes: Backend, 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

#shell_exceptionObject (readonly)

Returns the value of attribute shell_exception.



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

def shell_exception
  @shell_exception
end

Instance Method Details

#argsObject

default at end in case of redirection. IE: terraform output > /path



21
22
23
24
# File 'lib/terraspace/terraform/runner.rb', line 21

def args
  args = custom.args + pass.args + thor.args
  args.uniq
end

#current_dir_messageObject



83
84
85
86
87
# File 'lib/terraspace/terraform/runner.rb', line 83

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

From config/args/terraform.rb terraspace.cloud/docs/config/args/terraform/



27
28
29
# File 'lib/terraspace/terraform/runner.rb', line 27

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

#log(msg) ⇒ Object



89
90
91
92
# File 'lib/terraspace/terraform/runner.rb', line 89

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

#passObject

From Thor passthrough cli @options



33
34
35
# File 'lib/terraspace/terraform/runner.rb', line 33

def pass
  Args::Pass.new(@mod, @name, @options)
end

#runObject



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

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

#terraform(name, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/terraspace/terraform/runner.rb', line 44

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

  params = args.flatten.join(' ')
  command = "#{Terraspace.terraform_bin} #{name} #{params}".squish
  @shell_exception = nil
  run_hooks("terraform.rb", name) do
    Backend.new(@mod).create
    begin
      Terraspace::Shell.new(@mod, command, @options.merge(env: custom.env_vars)).run
      @success = true
    rescue Terraspace::ShellError => exception
      @shell_exception = exception
      @success = false
    end
  end
rescue Terraspace::SharedCacheError, Terraspace::InitRequiredError
  @retryer ||= Retryer.new(@mod, @options, name, $!)
  if @retryer.retry?
    @retryer.run
    retry
  else
    exit(1)
  end
rescue Terraspace::StateLockError => e
  logger.debug "ERROR: #{e.class}".color(:red)
  logger.info e.message
  md = e.message.match(/\s+ID:\s+(.*)/)
  return unless md
  return unless lock_id = md[1]
  logger.info <<~EOL
    You can force release the lock with:

        terraspace force_unlock #{@mod.name} #{lock_id}

  EOL
end

#thorObject

From Thor defined/managed cli @options



39
40
41
# File 'lib/terraspace/terraform/runner.rb', line 39

def thor
  Args::Thor.new(@mod, @name, @options)
end