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'
GCE_IMAGE_NAME_MAX =

GCE image names must match RFC 1035 label rules: 1-63 chars, lowercase alphanumeric and dashes, no trailing dash. See cloud.google.com/compute/docs/reference/rest/v1/images.

63

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) ⇒ TerraformBuilder

Returns a new instance of TerraformBuilder.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cem_acpt/image_builder.rb', line 47

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 = 1
  @private_key = nil
  @public_key = nil
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



45
46
47
# File 'lib/cem_acpt/image_builder.rb', line 45

def duration
  @duration
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



45
46
47
# File 'lib/cem_acpt/image_builder.rb', line 45

def exit_code
  @exit_code
end

Instance Method Details

#runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cem_acpt/image_builder.rb', line 61

def run
  @start_time = Time.now
  logger.start_ci_group("CemAcptImage v#{CemAcpt::VERSION} run started at #{@start_time}")
  @all_tfvars = new_tfvars(@config)
  @linux_tfvars, @windows_tfvars = divide_tfvars_by_os(@all_tfvars)
  image_types = []
  image_types << [@linux_tfvars, 'linux'] unless no_linux?
  image_types << [@windows_tfvars, 'windows'] unless no_windows?
  if image_types.empty?
    raise 'No images to build. Ensure the images config is populated and that --filter (if used) matches at least one image.'
  end
  image_types.each { |_, os_str| assert_template_present!(os_str) }
  return dry_run(image_types) if @config.get('dry_run')

  @working_dir = new_working_dir
  logger.info('CemAcpt::ImageBuilder') { "Using working directory: #{@working_dir}..." }
  keep_terminal_alive
  save_vars_to_file!('linux', @linux_tfvars)
  save_vars_to_file!('windows', @windows_tfvars)
  terraform_init
  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|
        unless @config.get('no_destroy_nodes')
          logger.info('CemAcpt::ImageBuilder') { "Stopping instance #{instance_name}..." }
          @exec.run('compute', 'instances', 'stop', instance_name, '--zone', @config.get('platform.zone'))
        end
        unless @config.get('no_build_images')
          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
        @exit_code = 0
      end
    ensure
      terraform_destroy(os_str, tfvars) unless @config.get('no_destroy_nodes')
    end
  end
rescue StandardError => e
  logger.error('CemAcpt::ImageBuilder') { "Image builder failed with error: #{e}" }
  logger.verbose('CemAcpt::ImageBuilder') { e.backtrace.join("\n") }
  @exit_code = 1
ensure
  if @start_time
    @duration = Time.now - @start_time
  else
    @duration = 0
  end
  logger.end_ci_group
end