Class: CemAcpt::ImageBuilder::TerraformBuilder

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

Overview

This class builds test node images using Terraform NOTE: There is a huge amount of overlap between this class and CemAcpt::Provision::Terraform. This isn’t ideal, but there are enough differences that a new class was needed. This should be refactored in the future.

Constant Summary collapse

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

Constants included from Logging

Logging::LEVEL_MAP

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

Constructor Details

#initialize(config) ⇒ TerraformBuilder

Returns a new instance of TerraformBuilder.



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

def initialize(config)
  @config = config
  @image_terraform_dir = File.join(config.get('terraform.dir'), 'image', config.get('platform.name'))
  @exec = CemAcpt::ImageBuilder::Exec.new_exec(@config)
  @environment = new_environment(@config)
  @all_tfvars = { node_data: {} }
  @linux_tfvars = { node_data: {} }
  @windows_tfvars = { node_data: {} }
  @duration = 0
  @exit_code = 0
  @private_key = nil
  @public_key = nil
end

Instance Method Details

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cem_acpt/image_builder.rb', line 44

def run
  @start_time = Time.now
  logger.with_ci_group("CemAcptImage v#{CemAcpt::VERSION} run started at #{@start_time}") do
    logger.info('CemAcptImage') { "Using working directory: #{@working_dir}..." }
    keep_terminal_alive
    @all_tfvars = new_tfvars(@config)
    @working_dir = new_working_dir
    @linux_tfvars, @windows_tfvars = divide_tfvars_by_os(@all_tfvars)
    save_vars_to_file!('linux', @linux_tfvars)
    save_vars_to_file!('windows', @windows_tfvars)
    terraform_init
    image_types = []
    image_types << [@linux_tfvars, 'linux'] unless no_linux?
    image_types << [@windows_tfvars, 'windows'] unless no_windows?
    image_types.each do |tfvars, os_str|
      terraform_plan(os_str, tfvars, DEFAULT_PLAN_NAME)
      begin
        terraform_apply(os_str, DEFAULT_PLAN_NAME)
        output = JSON.parse(terraform_output(os_str, 'node-data', json: true))
        output.each do |instance_name, data|
          logger.info('CemAcptImage') { "Stopping instance #{instance_name}..." }
          @exec.run('compute', 'instances', 'stop', instance_name)
          deprecate_old_images_in_family(data['image_family'])
          create_image_from_disk(data['disk_link'], image_name_from_image_family(data['image_family']), data['image_family'])
        end
      ensure
        terraform_destroy(os_str, tfvars)
      end
    end
  end
end