Class: TerraformWrapper::Tasks::Binary

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Shared::Logging
Defined in:
lib/terraform-wrapper/tasks/binary.rb

Instance Method Summary collapse

Methods included from Shared::Logging

configure_logger_for, logger_for

Constructor Details

#initialize(binary:) {|_self| ... } ⇒ Binary

Returns a new instance of Binary.

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
34
# File 'lib/terraform-wrapper/tasks/binary.rb', line 28

def initialize(binary:)
  @binary = binary

  yield self if block_given?

  binary_task
end

Instance Method Details

#binary_taskObject



38
39
40
41
42
43
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
75
76
# File 'lib/terraform-wrapper/tasks/binary.rb', line 38

def binary_task
  desc 'Downloads and extracts the expected version of the Terraform binary if it is not already present.'
  task :binary do |_t, _args|
    logger.info("Checking Terraform binary for platform: #{@binary.platform}, version: #{@binary.version}")

    unless @binary.exists
      logger.info('Terraform binary not found. Preparing binary...')

      logger.fatal("Failed to create binary directory: #{directory}") unless ::TerraformWrapper.create_directory(
        directory: @binary.directory, purpose: 'binaries'
      )

      archive_binary = 'terraform'
      archive_file   = "terraform_#{@binary.version}_#{@binary.platform}_amd64.zip"
      archive_path   = File.join(@binary.directory, archive_file)
      archive_uri    = "https://releases.hashicorp.com/terraform/#{@binary.version}/#{archive_file}"

      sums_file = "terraform_#{@binary.version}_SHA256SUMS"
      sums_path = File.join(@binary.directory, sums_file)
      sums_uri  = "https://releases.hashicorp.com/terraform/#{@binary.version}/#{sums_file}"

      begin
        download(path: archive_path, uri: archive_uri) unless File.file?(archive_path)
        download(path: sums_path, uri: sums_uri) unless File.file?(sums_path)
        verify(file: archive_file, path: archive_path, sums: sums_path)
        extract(archive: archive_path, binary: archive_binary, destination: @binary.path)
      ensure
        clean(archive: archive_path, sums: sums_path)
      end
    end

    unless @binary.executable
      logger.info('Terraform binary not executable. Setting permissions...')
      executable(path: @binary.path)
    end

    logger.fatal('Problem with checking the Terraform binary!') unless @binary.check
  end
end