class Terraspace::Terraform::Runner::Retryer

Public Class Methods

new(mod, options, command_name, exception) click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 6
def initialize(mod, options, command_name, exception)
  @mod, @options, @command_name, @exception = mod, options, command_name, exception
  @retries = 1
end

Public Instance Methods

init_required_error() click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 47
def init_required_error
  logger.info "Terraform reinitialization required detected. Will run `terraform init` and try again."
  logger.debug "Retry attempt: #{@retries}"
  logger.debug "#{@exception.class}"
  reinit
end
purge_caches() click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 58
def purge_caches
  dir = "#{@mod.cache_dir}/.terraform"
  logger.info "Purging #{pretty_path(dir)}"
  FileUtils.rm_rf(dir)

  dir = "#{Terraspace.config.terraform.plugin_cache.dir}"
  logger.info "Purging #{pretty_path(dir)}"
  FileUtils.rm_rf(dir)
  FileUtils.mkdir_p(dir) # need /tmp/terraspace/plugin_cache dir to exist
end
reinit() click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 54
def reinit
  Terraspace::Terraform::Runner.new("init", @options).run unless @command_name == "init"
end
retry?() click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 11
def retry?
  if @retries <= 3 && !@stop_retrying
    true # will retry
  else
    logger.info "ERROR: #{@exception.message}"
    false # will not retry
  end
end
run() click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 20
def run
  backoff = 2 ** @retries # 2, 4, 8
  logger.debug "Waiting #{backoff}s before retrying"
  sleep(backoff)
  @retries += 1

  case @exception
  when Terraspace::SharedCacheError
    shared_cache_error
  when Terraspace::InitRequiredError
    init_required_error
  end
end
shared_cache_error() click to toggle source
# File lib/terraspace/terraform/runner/retryer.rb, line 34
def shared_cache_error
  logger.info "Terraform Shared Cache error detected. Will purge caches and run `terraform init` to try again."
  logger.debug "Retry attempt: #{@retries}"
  logger.debug "#{@exception.class}"
  logger.debug "#{@exception.message}"
  if Terraspace.config.terraform.plugin_cache.purge_on_error # Purging the cache "fixes" this terraform bug
    purge_caches
    reinit
  else
    @stop_retrying = true
  end
end