Class: VagrantVbguest::Download

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util
Defined in:
lib/vagrant-vbguest/download.rb

Overview

This implementation is based on Action::Box::Download by vagrant

This adoption does not run as a action/middleware, but is called manually

MIT License - Mitchell Hashimoto and John Bender - github.com/mitchellh/vagrant

Constant Summary collapse

BASENAME =
"vbguest"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Download

Returns a new instance of Download.



19
20
21
22
23
24
# File 'lib/vagrant-vbguest/download.rb', line 19

def initialize(env)
  @env = env
  @env["download.classes"] ||= []
  @env["download.classes"] += [Vagrant::Downloaders::HTTP, Vagrant::Downloaders::File]
  @downloader = nil
end

Instance Attribute Details

#temp_pathObject (readonly)

Returns the value of attribute temp_path.



17
18
19
# File 'lib/vagrant-vbguest/download.rb', line 17

def temp_path
  @temp_path
end

Instance Method Details

#cleanupObject



61
62
63
64
65
66
# File 'lib/vagrant-vbguest/download.rb', line 61

def cleanup
  if temp_path && File.exist?(temp_path)
    @env[:ui].info I18n.t("vagrant.plugins.vbguest.download.cleaning")
    File.unlink(temp_path)
  end
end

#downloadObject



52
53
54
55
56
57
58
59
# File 'lib/vagrant-vbguest/download.rb', line 52

def download
  if instantiate_downloader
    with_tempfile do |tempfile|
      download_to(tempfile)
      @temp_path = @env["download.temp_path"] = tempfile.path
    end
  end
end

#download_to(f) ⇒ Object



78
79
80
# File 'lib/vagrant-vbguest/download.rb', line 78

def download_to(f)
  @downloader.download!(@env[:url], f)
end

#instantiate_downloaderObject

Raises:

  • (Errors::BoxDownloadUnknownType)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-vbguest/download.rb', line 26

def instantiate_downloader
  # Assign to a temporary variable since this is easier to type out,
  # since it is used so many times.
  classes = @env["download.classes"]

  # Find the class to use.
  classes.each_index do |i|
    klass = classes[i]

    # Use the class if it matches the given URI or if this
    # is the last class...
    if classes.length == (i + 1) || klass.match?(@env[:url])
      @env[:ui].info I18n.t("vagrant.plugins.vbguest.download.with", :class => klass.to_s)
      @downloader = klass.new(@env[:ui])
      break
    end
  end

  # This line should never be reached, but we'll keep this here
  # just in case for now.
  raise Errors::BoxDownloadUnknownType if !@downloader

  @downloader.prepare(@env[:url]) if @downloader.respond_to?(:prepare)
  true
end

#temp_filenameObject



74
75
76
# File 'lib/vagrant-vbguest/download.rb', line 74

def temp_filename
  @env[:tmp_path].join(BASENAME + Time.now.to_i.to_s)
end

#with_tempfileObject



68
69
70
71
72
# File 'lib/vagrant-vbguest/download.rb', line 68

def with_tempfile
  File.open(temp_filename, Platform.tar_file_options) do |tempfile|
    yield tempfile
  end
end