Class: CemAcpt::Provision::Terraform

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

Overview

Class to handle provisioning infrastructure using Terraform. This class abstracts away the details of running Terraform commands and managing the working directory, allowing for easy provisioning and destruction of infrastructure for testing purposes.

Constant Summary collapse

DEFAULT_PLAN_NAME =
'testplan.tfplan'
DEFAULT_VARS_FILE =
'testvars.json'

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(config, provision_data) ⇒ Terraform

Initializes a new Terraform provisioner with the given configuration and provision data. The provision data should include all necessary information about the nodes to be provisioned, as well as any necessary credentials and module package paths. The configuration should include any necessary settings for the provisioner, such as the base directory for Terraform working directories and any environment variables to set when running Terraform commands.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cem_acpt/provision/terraform.rb', line 30

def initialize(config, provision_data)
  @config = config
  @provision_data = provision_data
  @backend = new_backend(@provision_data[:test_data].first[:test_name])
  @environment = new_environment(@config)
  @working_dir = nil
  @module_package_path = nil
  @private_key = nil
  @public_key = nil
  @applied = false
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



20
21
22
# File 'lib/cem_acpt/provision/terraform.rb', line 20

def environment
  @environment
end

#module_package_pathObject (readonly)

Returns the value of attribute module_package_path.



20
21
22
# File 'lib/cem_acpt/provision/terraform.rb', line 20

def module_package_path
  @module_package_path
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



20
21
22
# File 'lib/cem_acpt/provision/terraform.rb', line 20

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



20
21
22
# File 'lib/cem_acpt/provision/terraform.rb', line 20

def public_key
  @public_key
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



20
21
22
# File 'lib/cem_acpt/provision/terraform.rb', line 20

def working_dir
  @working_dir
end

Instance Method Details

#destroyObject

Destroys the infrastructure provisioned by Terraform and deletes the working directory.



73
74
75
76
77
78
79
80
81
# File 'lib/cem_acpt/provision/terraform.rb', line 73

def destroy
  terraform_destroy(formatted_vars)
  logger.verbose('CemAcpt::Provision::Terraform') { "Deleting old working directory #{working_dir}" }
  FileUtils.rm_rf(working_dir)
  @working_dir = nil
  @module_package_path = nil
  @private_key = nil
  @public_key = nil
end

#outputHash

Returns the output of the Terraform apply command as a hash of instance names and IPs.

Raises:

  • (RuntimeError)

    If Terraform has not been applied yet.

  • (JSON::ParserError)

    If there is an error parsing the Terraform output.



61
62
63
64
65
66
67
68
69
70
# File 'lib/cem_acpt/provision/terraform.rb', line 61

def output
  raise 'Terraform has not been applied yet' unless @applied

  output = terraform_output('instance_name_ip', json: true)
  logger.debug('CemAcpt::Provision::Terraform') { "Terraform output:\n#{output}" }
  JSON.parse(output)
rescue JSON::ParserError => e
  logger.error('CemAcpt::Provision::Terraform') { "Error parsing Terraform output: #{output}" }
  raise e
end

#provision(reuse_working_dir: false) ⇒ Hash

Provisions infrastructure using Terraform and returns a hash of instance names and IPs.



46
47
48
49
50
51
52
53
54
55
# File 'lib/cem_acpt/provision/terraform.rb', line 46

def provision(reuse_working_dir: false)
  logger.info('CemAcpt::Provision::Terraform') { 'Provisioning nodes...' }
  @working_dir = new_working_dir unless reuse_working_dir
  validate_working_dir!
  save_vars_to_file!(formatted_vars) # Easier to reuse nodes this way
  terraform_init
  terraform_plan(formatted_vars, DEFAULT_PLAN_NAME)
  terraform_apply(DEFAULT_PLAN_NAME)
  @applied = true
end

#showObject

Shows the current state of the Terraform-managed infrastructure.



84
85
86
# File 'lib/cem_acpt/provision/terraform.rb', line 84

def show
  terraform_show
end