Class: CemAcpt::Provision::Terraform

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

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

Returns a new instance of Terraform.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cem_acpt/provision/terraform.rb', line 19

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.



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

def environment
  @environment
end

#module_package_pathObject (readonly)

Returns the value of attribute module_package_path.



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

def module_package_path
  @module_package_path
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



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

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



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

def public_key
  @public_key
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



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

def working_dir
  @working_dir
end

Instance Method Details

#destroyObject



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

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

#outputObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/cem_acpt/provision/terraform.rb', line 43

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

Returns A hash of instance names and IPs.

Returns:

  • (Hash)

    A hash of instance names and IPs



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

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



64
65
66
# File 'lib/cem_acpt/provision/terraform.rb', line 64

def show
  terraform_show
end