class Terraspace::All::Runner

Public Class Methods

new(command, options={}) click to toggle source
Calls superclass method Terraspace::All::Base::new
# File lib/terraspace/all/runner.rb, line 5
def initialize(command, options={})
  @command, @options = command, options
  super(options)
end

Public Instance Methods

are_you_sure?() click to toggle source
# File lib/terraspace/all/runner.rb, line 154
def are_you_sure?
  return true unless sure_command?
  sure? # from Util
end
build_batches() click to toggle source
# File lib/terraspace/all/runner.rb, line 23
def build_batches
  @batches = run_builder(quiet: false)
  @batches.reverse! if @command == "down"
  @batches
end
command_map(name) click to toggle source
# File lib/terraspace/all/runner.rb, line 146
def command_map(name)
  map = {
    up:   "apply",
    down: "destroy",
  }.stringify_keys
  map[name] || name
end
deploy_batch(batch) click to toggle source
# File lib/terraspace/all/runner.rb, line 44
def deploy_batch(batch)
  @pids = {} # stores child processes pids. map of pid to mod_name, reset this list on each batch run
  concurrency = Terraspace.config.all.concurrency
  batch.sort_by(&:name).each_slice(concurrency) do |slice|
    slice.each do |node|
      pid = fork do
        run_terraspace(node.name)
      end
      @pids[pid] = node.name # store mod_name mapping
    end
  end
  wait_for_child_proccess
  summarize     # also reports lower-level error info
  report_errors # reports finall errors and possibly exit
end
deploy_batches() click to toggle source
# File lib/terraspace/all/runner.rb, line 29
def deploy_batches
  truncate_logs if ENV['TS_TRUNCATE_LOGS']
  @batches.each_with_index do |batch,i|
    logger.info "Batch Run #{i+1}:"
    run_builder unless i == 0 # already handled by build_batches the first time
    deploy_batch(batch)
  end
end
exit_on_fail?() click to toggle source

Precendence:

  1. env var

  2. cli

  3. config/app.rb setting

# File lib/terraspace/all/runner.rb, line 84
def exit_on_fail?
  return false if ENV['TS_EXIT_ON_FAIL'] == '0'
  if @options[:exit_on_fail].nil?
    Terraspace.config.all.exit_on_fail[@command]
  else
    @options[:exit_on_fail]
  end
end
log_path(mod_name) click to toggle source
# File lib/terraspace/all/runner.rb, line 133
def log_path(mod_name)
  "#{Terraspace.config.log.root}/#{@command}/#{mod_name}.log"
end
preview() click to toggle source
# File lib/terraspace/all/runner.rb, line 19
def preview
  Preview.new(@command, @batches, @options).show
end
report_errors() click to toggle source
# File lib/terraspace/all/runner.rb, line 69
def report_errors
  @errors.each do |pid|
    mod_name = @pids[pid]
    terraspace_command = terraspace_command(mod_name)
    logger.error "Error running: #{terraspace_command}. Check logs and fix the error.".color(:red)
  end
  unless @errors.empty?
    exit 2 if exit_on_fail?
  end
end
run() click to toggle source
# File lib/terraspace/all/runner.rb, line 10
def run
  time_took do
    @batches = build_batches
    preview
    are_you_sure?
    deploy_batches
  end
end
run_builder(quiet: true) click to toggle source

Should run after each batch run. run_builder also calls replace_outputs. Important: rebuild from source so placeholders are in place.

# File lib/terraspace/all/runner.rb, line 40
def run_builder(quiet: true)
  Terraspace::Builder.new(@options.merge(mod: "placeholder", quiet: quiet)).run
end
run_terraspace(mod_name) click to toggle source
# File lib/terraspace/all/runner.rb, line 104
def run_terraspace(mod_name)
  set_log_path!(mod_name)
  name = command_map(@command)
  o = @options.merge(mod: mod_name, yes: true, build: false, input: false, log_to_stderr: true)
  o.merge!(quiet: false) if @command == "init" # noisy so can filter and summarize output
  case @command
  when "up"
    Terraspace::CLI::Up.new(o).run
  when "down"
    Terraspace::CLI::Down.new(o).run
  else
    Terraspace::CLI::Commander.new(name, o).run
  end
end
set_log_path!(mod_name) click to toggle source
# File lib/terraspace/all/runner.rb, line 119
def set_log_path!(mod_name)
  command = terraspace_command(mod_name)
  path = log_path(mod_name)
  pretty_path = Terraspace::Util.pretty_path(path)
  logger.info "Running: #{command.bright} Logs: #{pretty_path}"

  FileUtils.mkdir_p(File.dirname(path))
  logger = Terraspace::Logger.new(path)
  logger.level = Terraspace.config.logger.level # match the level that user configured
  logger.formatter = Terraspace.config.logger.formatter # match the level that user configured
  logger.progname = command
  Terraspace.logger = logger
end
summarize() click to toggle source
# File lib/terraspace/all/runner.rb, line 93
def summarize
  @pids.each do |_, mod_name|
    data = {
      command: @command,
      log_path: log_path(mod_name),
      terraspace_command: terraspace_command(mod_name),
    }
    Summary.new(data).run
  end
end
sure_command?() click to toggle source
# File lib/terraspace/all/runner.rb, line 159
def sure_command?
  %w[up down].include?(@command)
end
terraspace_command(name) click to toggle source
# File lib/terraspace/all/runner.rb, line 142
def terraspace_command(name)
  "terraspace #{@command} #{name}"
end
time_took() { || ... } click to toggle source
# File lib/terraspace/all/runner.rb, line 163
def time_took
  t1 = Time.now
  yield
  t2 = Time.now
  logger.info "Time took: #{pretty_time(t2-t1)}"
end
truncate_logs() click to toggle source
# File lib/terraspace/all/runner.rb, line 137
def truncate_logs
  logs = Terraspace::CLI::Log::Tasks.new(mute: true)
  logs.truncate
end
wait_for_child_proccess() click to toggle source
# File lib/terraspace/all/runner.rb, line 60
def wait_for_child_proccess
  @errors = [] # stores child processes pids that errored
  @pids.each do |pid, _|
    pid, status = Process.wait2(pid)
    success = status.exitstatus == 0
    @errors << pid unless success
  end
end