module MuxTf::TerraformHelpers

Public Instance Methods

tf_apply(filename: nil, targets: []) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 11
def tf_apply(filename: nil, targets: [])
  args = []
  args << filename if filename
  if targets && !targets.empty?
    targets.each do |target|
      args << "-target=#{target}"
    end
  end

  cmd = tf_prepare_command(["apply", *args], need_auth: true)
  run_terraform(cmd)
end
tf_force_unlock(id:) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 7
def tf_force_unlock(id:)
  run_terraform(tf_prepare_command(["force-unlock", "-force", id], need_auth: true))
end
tf_init(input: nil, upgrade: nil, reconfigure: nil, color: true, &block) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 29
def tf_init(input: nil, upgrade: nil, reconfigure: nil, color: true, &block)
  args = []
  args << "-input=#{input.inspect}" unless input.nil?
  args << "-upgrade" unless upgrade.nil?
  args << "-reconfigure" unless reconfigure.nil?
  args << "-no-color" unless color

  cmd = tf_prepare_command(["init", *args], need_auth: true)
  stream_or_run_terraform(cmd, &block)
end
tf_plan(out:, color: true, detailed_exitcode: nil, compact_warnings: false, input: nil, targets: [], &block) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 40
def tf_plan(out:, color: true, detailed_exitcode: nil, compact_warnings: false, input: nil, targets: [], &block)
  args = []
  args += ["-out", out]
  args << "-input=#{input.inspect}" unless input.nil?
  args << "-compact-warnings" if compact_warnings
  args << "-no-color" unless color
  args << "-detailed-exitcode" if detailed_exitcode
  if targets && !targets.empty?
    targets.each do |target|
      args << "-target=#{target}"
    end
  end

  cmd = tf_prepare_command(["plan", *args], need_auth: true)
  stream_or_run_terraform(cmd, &block)
end
tf_show(file, json: false) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 57
def tf_show(file, json: false)
  if json
    args = ["show", "-json", file]
    cmd = tf_prepare_command(args, need_auth: true)
    capture_terraform(cmd, json: true)
  else
    args = ["show", file]
    cmd = tf_prepare_command(args, need_auth: true)
    run_terraform(cmd)
  end
end
tf_validate() click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 24
def tf_validate
  cmd = tf_prepare_command(["validate", "-json"], need_auth: true)
  capture_terraform(cmd, json: true)
end

Private Instance Methods

capture_terraform(args, json: nil) click to toggle source

error: true, echo_command: true, indent: 0, raise_on_error: false, detailed_result: false

# File lib/mux_tf/terraform_helpers.rb, line 107
def capture_terraform(args, json: nil)
  result = capture_shell(args, error: true, echo_command: false, raise_on_error: false, detailed_result: true)
  parsed_output = JSON.parse(result.output) if json
  OpenStruct.new({
    status: result.status,
    success?: result.status == 0,
    output: result.output,
    parsed_output: parsed_output
  })
rescue JSON::ParserError => e
  fail_with "Execution Failed! - #{result.inspect}"
end
run_terraform(args, **_options) click to toggle source

return_status: false, echo_command: true, quiet: false, indent: 0

# File lib/mux_tf/terraform_helpers.rb, line 89
def run_terraform(args, **_options)
  status = run_shell(args, return_status: true, echo_command: true, quiet: false)
  OpenStruct.new({
    status: status,
    success?: status == 0
  })
end
stream_or_run_terraform(args, &block) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 80
def stream_or_run_terraform(args, &block)
  if block
    stream_terraform(args, &block)
  else
    run_terraform(args)
  end
end
stream_terraform(args, &block) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 97
def stream_terraform(args, &block)
  status = run_with_each_line(args, &block)
  # status is a Process::Status
  OpenStruct.new({
    status: status.exitstatus,
    success?: status.exitstatus == 0
  })
end
tf_prepare_command(args, need_auth:) click to toggle source
# File lib/mux_tf/terraform_helpers.rb, line 71
def tf_prepare_command(args, need_auth:)
  if ENV["MUX_TF_AUTH_WRAPPER"] && need_auth
    words = Shellwords.shellsplit(ENV["MUX_TF_AUTH_WRAPPER"])
    [*words, "terraform", *args]
  else
    ["terraform", *args]
  end
end