class Kitchen::Terraform::ShellOut

Terraform commands are run by shelling out and using the {www.terraform.io/docs/commands/index.html command-line interface}. The shell out environment includes the TF_IN_AUTOMATION environment variable as specified by the {www.terraform.io/guides/running-terraform-in-automation.html#controlling-terraform-output-in-automation Running Terraform in Automation guide}.

Attributes

command[RW]
logger[RW]
shell_out[RW]

Public Class Methods

new(command:, logger:, options:) click to toggle source

initialize prepares a new instance of the class.

@param command [String] the command to run. @param logger [Kitchen::Logger] a logger for logging messages. @param options [Hash] options which adjust the execution of the command. @return [Kitchen::Terraform::CommandExecutor]

# File lib/kitchen/terraform/shell_out.rb, line 34
def initialize(command:, logger:, options:)
  self.command = command
  self.logger = logger
  self.shell_out = ::Mixlib::ShellOut.new(
    command,
    {
      environment: { "LC_ALL" => nil, "TF_IN_AUTOMATION" => "true" },
      live_stream: logger,
    }.merge(options)
  )
end

Public Instance Methods

run() { |stdout| ... } click to toggle source

run executes a command.

@yieldparam standard_output [String] the standard output of the command. @raise [Kitchen::TransientFailure] if running the command results in failure. @return [self]

# File lib/kitchen/terraform/shell_out.rb, line 51
def run
  execute_workflow

  yield shell_out.stdout

  self
end

Private Instance Methods

error_access_message() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 63
def error_access_message
  "Running the command `#{command}` failed due to unauthorized access to the Terraform client. Authorize the " \
  "#{::Etc.getlogin} user to execute the client to avoid this error."
end
error_no_entry_message() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 68
def error_no_entry_message
  "Running the command `#{command}` failed due to a nonexistent Terraform client. Set `driver.client` to the " \
  "pathname of a client to avoid this error."
end
error_non_zero_message() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 73
def error_non_zero_message
  "Running the command `#{command}` failed due to a non-zero exit code of #{shell_out.exitstatus}."
end
error_timeout_message() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 77
def error_timeout_message
  "Running the command `#{command}` failed due to an excessive execution time. Increase " \
  "`driver.command_timeout` to avoid this error."
end
execute_workflow() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 82
def execute_workflow
  logger.debug run_start_message
  run_command
  logger.debug run_finish_message
  verify_exit_code
end
run_command() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 89
def run_command
  shell_out.run_command
rescue ::Errno::EACCES
  raise ::Kitchen::TransientFailure, error_access_message
rescue ::Errno::ENOENT
  raise ::Kitchen::TransientFailure, error_no_entry_message
rescue ::Mixlib::ShellOut::CommandTimeout
  raise ::Kitchen::TransientFailure, error_timeout_message
end
run_finish_message() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 99
def run_finish_message
  "Finished running command `#{command}` in #{shell_out.execution_time} seconds."
end
run_start_message() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 103
def run_start_message
  "Running command `#{command}` in directory #{shell_out.cwd} with a timeout of #{shell_out.timeout} seconds..."
end
verify_exit_code() click to toggle source
# File lib/kitchen/terraform/shell_out.rb, line 107
def verify_exit_code
  shell_out.error!
rescue ::Mixlib::ShellOut::ShellCommandFailed
  raise ::Kitchen::TransientFailure, error_non_zero_message
end