Class: CemAcpt::Provision::TerraformCmd
- Inherits:
-
Object
- Object
- CemAcpt::Provision::TerraformCmd
- 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
Instance Attribute Summary collapse
-
#bin_path ⇒ Object
readonly
Returns the value of attribute bin_path.
-
#working_dir ⇒ Object
Returns the value of attribute working_dir.
Instance Method Summary collapse
-
#apply(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String
Runs the ‘terraform apply` command with the provided options and environment variables.
-
#destroy(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String
Runs the ‘terraform destroy` command with the provided options and environment variables.
-
#environment(env = {}) ⇒ Hash
Returns a hash of environment variables to be used when running terraform commands.
-
#environment=(env) ⇒ Object
Sets the environment variables to be used when running terraform commands.
-
#init(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String
Runs the ‘terraform init` command with the provided options and environment variables.
-
#initialize(working_dir = nil, environment = {}) ⇒ TerraformCmd
constructor
Initializes a new TerraformCmd instance.
-
#inspect ⇒ String
A string representation of the TerraformCmd instance.
-
#output(opts = {}, env = {}, raise_on_fail: true, combine_out_err: false) ⇒ String
Runs the ‘terraform output` command with the provided options and environment variables.
-
#plan(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String
Runs the ‘terraform plan` command with the provided options and environment variables.
-
#show(opts = {}, env = {}, raise_on_fail: true, combine_out_err: true) ⇒ String
Runs the ‘terraform show` command with the provided options and environment variables.
-
#to_s ⇒ String
A string representation of the TerraformCmd instance, which includes the class name and object ID.
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.
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_path ⇒ Object (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_dir ⇒ Object
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.
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.
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.
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.
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.
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 |
#inspect ⇒ String
Returns 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.
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.
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.
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_s ⇒ String
Returns 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 |