class Ruby::Terraform::Executable

Public Class Methods

new(config = Ruby::Terraform.config) click to toggle source
# File lib/ruby/terraform/executable.rb, line 17
def initialize(config = Ruby::Terraform.config)
  @terraform_version = config.terraform_version
  @download_path = config.download_path
  @binary = config.binary
  @download_filename = "#{@terraform_version}-terraform.zip"
  FileUtils.mkdir_p(@download_path)

  raise PlatformUnsupported unless supported?
end

Public Instance Methods

binary() click to toggle source
# File lib/ruby/terraform/executable.rb, line 27
def binary
  @binary
end
download(opts = {}) click to toggle source
# File lib/ruby/terraform/executable.rb, line 31
def download(opts = {})
  return if binary_exist?
  opts[:uri] ||= ENV['TERRAFORM_DOWNLOAD_URL'] || "https://releases.hashicorp.com/#{download_uri}"

  $stderr.puts("Downloading terraform binary from #{opts[:uri]}") if opts[:verbose]

  open("#{@download_path}/#{@download_filename}", 'wb') do |saved_file|
    open(opts[:uri], 'rb') do |read_file|
      saved_file.write(read_file.read)
    end
  end
end
extract(opts={}) click to toggle source
# File lib/ruby/terraform/executable.rb, line 44
def extract(opts={})
  return if binary_exist?

  Decompressor.extract("#{@download_path}/#{@download_filename}", @download_path, opts[:verbose] || false)
  make_exe
end

Private Instance Methods

binary_exist?() click to toggle source
# File lib/ruby/terraform/executable.rb, line 53
def binary_exist?
  File.exist?(binary)
end
download_uri() click to toggle source
# File lib/ruby/terraform/executable.rb, line 65
def download_uri
  return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_freebsd_amd64.zip" if OS.freebsd?
  return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_darwin_amd64.zip" if OS.mac?
  return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_windows_amd64.zip" if OS.windows?
  return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_linux_amd64.zip" if OS.linux?
end
make_exe() click to toggle source
# File lib/ruby/terraform/executable.rb, line 61
def make_exe
  FileUtils.chmod('a+x', binary) if binary_exist? && !OS.windows?
end
supported?() click to toggle source
# File lib/ruby/terraform/executable.rb, line 57
def supported?
  OS.freebsd? || OS.mac? || OS.windows? || OS.linux?
end