Class: CemAcpt::Provision::TerraformCmd

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_acpt/provision/terraform/terraform_cmd.rb

Overview

Stand-in for ruby-terraform because ruby-terraform doesn’t work with Ruby 3

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, current_log_level, #current_log_level, included, logger, #logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, new_log_level, #new_log_level, new_logger, #new_logger, verbose?, #verbose?

Constructor Details

#initialize(working_dir = nil, environment = {}) ⇒ TerraformCmd

Initializes a new TerraformCmd instance.

Parameters:

  • working_dir (String, nil) (defaults to: nil)

    The working directory to run terraform commands in.

  • environment (Hash) (defaults to: {})

    A hash of environment variables to set when running terraform commands. These will be merged with any additional environment variables passed in when running commands.



22
23
24
25
26
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 22

def initialize(working_dir = nil, environment = {})
  @working_dir = working_dir
  @environment = environment
  @bin_path = which_terraform
end

Instance Attribute Details

#bin_pathObject (readonly)

Returns the value of attribute bin_path.



16
17
18
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 16

def bin_path
  @bin_path
end

#working_dirObject

Returns the value of attribute working_dir.



15
16
17
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 15

def working_dir
  @working_dir
end

Instance Method Details

#apply(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String

Runs the ‘terraform apply` command with the provided options and environment variables. The :plan option is required and will be used as the input file for the apply.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to pass to the ‘terraform apply` command. The :plan option is required and will be used as the input file for the apply.

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance when running the command.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails. Defaults to true.

  • combine_out_err (Boolean) (defaults to: true)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string. Defaults to true.

Returns:

  • (String)

    The string output of the command.

Raises:

  • (ArgumentError)

    If the :plan option is not provided in the opts hash or is nil/empty.



104
105
106
107
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 104

def apply(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true)
  plan = extract_arg!(opts, :plan, required: true)
  run_cmd('apply', opts, env, suffix: plan, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
end

#destroy(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String

Runs the ‘terraform destroy` command with the provided options and environment variables.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to pass to the ‘terraform destroy` command.

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance when running the command.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails. Defaults to true.

  • combine_out_err (Boolean) (defaults to: true)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string. Defaults to true.

Returns:

  • (String)

    The string output of the command.



117
118
119
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 117

def destroy(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true)
  run_cmd('destroy', opts, env, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
end

#environment(env = {}) ⇒ Hash

Returns a hash of environment variables to be used when running terraform commands. This will merge any environment variables set on the instance with any additional environment variables passed in. Passed in environment variables will take precedence over those set on the instance, but will not persist on the instance.

Parameters:

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance.

Returns:

  • (Hash)

    A hash of environment variables to be used when running terraform commands.

Raises:

  • (ArgumentError)

    If the provided additional environment is not a Hash.



55
56
57
58
59
60
61
62
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 55

def environment(env = {})
  raise ArgumentError, 'additional environment must be a Hash' unless env.is_a?(Hash)

  if env.key?(:environment) && env[:environment].is_a?(Hash)
    env = env[:environment]
  end
  @environment.merge(env)
end

#environment=(env) ⇒ Object

Sets the environment variables to be used when running terraform commands. This will replace any existing environment variables set on the instance.

Parameters:

  • env (Hash)

    A hash of environment variables to set when running terraform commands. These will be merged with any additional environment variables passed in when running commands.

Raises:

  • (ArgumentError)

    If the provided environment is not a Hash.



43
44
45
46
47
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 43

def environment=(env)
  raise ArgumentError, 'environment must be a Hash' unless env.is_a?(Hash)

  @environment = env
end

#init(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String

Runs the ‘terraform init` command with the provided options and environment variables.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to pass to the ‘terraform init` command.

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance when running the command.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails. Defaults to true.

  • combine_out_err (Boolean) (defaults to: true)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string. Defaults to true.

Returns:

  • (String)

    The string output of the command.



72
73
74
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 72

def init(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true)
  run_cmd('init', opts, env, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
end

#inspectString

Returns A string representation of the TerraformCmd instance.

Returns:

  • (String)

    A string representation of the TerraformCmd instance.



29
30
31
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 29

def inspect
  to_s
end

#output(opts = {}, env = {}, raise_on_fail: true, combine_out_err: false) ⇒ String

Runs the ‘terraform output` command with the provided options and environment variables. The :name option is required and will be used as the name of the output to show.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to pass to the ‘terraform output` command. The :name option is required and will be used as the name of the output to show.

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance when running the command.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails. Defaults to true.

  • combine_out_err (Boolean) (defaults to: false)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string. Defaults to false because output command is typically used to get specific output values and we don’t want error messages mixed in with the output. Setting this to true can cause JSON parsing of the output to fail if there are error messages included in the output string.

Returns:

  • (String)

    The string output of the command.

Raises:

  • (ArgumentError)

    If the :name option is not provided in the opts hash or is nil/empty.



147
148
149
150
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 147

def output(opts = {}, env = {}, raise_on_fail: true, combine_out_err: false)
  name = extract_arg!(opts, :name, required: true)
  run_cmd('output', opts, env, suffix: name, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
end

#plan(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String

Runs the ‘terraform plan` command with the provided options and environment variables. The :plan option is required and will be used as the output file for the plan.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to pass to the ‘terraform plan` command. The :plan option is required and will be used as the output file for the plan.

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance when running the command.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails. Defaults to true.

  • combine_out_err (Boolean) (defaults to: true)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string. Defaults to true.

Returns:

  • (String)

    The string output of the command.

Raises:

  • (ArgumentError)

    If the :plan option is not provided in the opts hash or is nil/empty.



87
88
89
90
91
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 87

def plan(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true)
  plan = extract_arg!(opts, :plan, required: true)
  opts[:out] = plan
  run_cmd('plan', opts, env, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
end

#show(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String

Runs the ‘terraform show` command with the provided options and environment variables.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to pass to the ‘terraform show` command.

  • env (Hash) (defaults to: {})

    A hash of additional environment variables to merge with those set on the instance when running the command.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails. Defaults to true.

  • combine_out_err (Boolean) (defaults to: true)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string. Defaults to true.

Returns:

  • (String)

    The string output of the command.



129
130
131
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 129

def show(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true)
  run_cmd('show', opts, env, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
end

#to_sString

Returns A string representation of the TerraformCmd instance, which includes the class name and object ID.

Returns:

  • (String)

    A string representation of the TerraformCmd instance, which includes the class name and object ID.



35
36
37
# File 'lib/cem_acpt/provision/terraform/terraform_cmd.rb', line 35

def to_s
  "#<#{self.class.name}:0x#{object_id.to_s(16)}>"
end