Class: Packer::Binary::Executable

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/packer/binary/executable.rb

Overview

This class handles downloading, extracting, and saving the associated binary file

Defined Under Namespace

Classes: PlatformUnsupported

Constant Summary collapse

PACKER_DOWNLOAD_URI =

The download URI for the binary package

'releases.hashicorp.com'.freeze

Instance Method Summary collapse

Methods included from Helpers

debug, err, msg, system_command

Constructor Details

#initializeExecutable

Loads global config values and creates the download directory if the current platform is supported



16
17
18
19
20
21
22
23
24
# File 'lib/packer/binary/executable.rb', line 16

def initialize
  @packer_version = Packer::Binary.config.version
  @download_path = "#{Packer::Binary.config.download_path.chomp('/')}/packer-binary/#{@packer_version}/bin"
  @download_filename = "#{@packer_version}-packer.zip"

  FileUtils.mkdir_p @download_path

  raise PlatformUnsupported unless supported?
end

Instance Method Details

#binaryString

Returns the path to the binary if it exists

Returns:

  • (String)

    absolute path of the binary



28
29
30
# File 'lib/packer/binary/executable.rb', line 28

def binary
  Dir["#{@download_path}/packer*"][0]
end

#downloadObject

Downloads the zipfile from the origin



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/packer/binary/executable.rb', line 33

def download
  return if binary_exist?

  msg("Downloading https://#{download_domain}/#{download_uri}")

  require 'open-uri'

  File.open("#{@download_path}/#{@download_filename}", 'wb') do |saved_file|
    # the following "open" is provided by open-uri
    open("https://#{download_domain}/#{download_uri}", 'rb') do |read_file|
      saved_file.write(read_file.read)
    end
  end
end

#extractObject

Extracts the binary from the zipfile and makes it executable



49
50
51
52
53
54
# File 'lib/packer/binary/executable.rb', line 49

def extract
  return if binary_exist?

  Compressor.extract("#{@download_path}/#{@download_filename}", @download_path)
  make_exe
end