Class: Vagrant::Action::Box::Download

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/vagrant/action/box/download.rb

Constant Summary collapse

BASENAME =
"box"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Download

Returns a new instance of Download.



11
12
13
14
15
16
17
# File 'lib/vagrant/action/box/download.rb', line 11

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

Instance Attribute Details

#temp_pathObject (readonly)

Returns the value of attribute temp_path.



9
10
11
# File 'lib/vagrant/action/box/download.rb', line 9

def temp_path
  @temp_path
end

Instance Method Details

#box_temp_pathObject



62
63
64
# File 'lib/vagrant/action/box/download.rb', line 62

def box_temp_path
  @env.env.tmp_path.join(BASENAME + Time.now.to_i.to_s)
end

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/vagrant/action/box/download.rb', line 19

def call(env)
  @env = env

  download if instantiate_downloader
  @app.call(@env)

  recover(env) # called in both cases to cleanup workspace
end

#downloadObject



42
43
44
45
46
47
# File 'lib/vagrant/action/box/download.rb', line 42

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

#download_to(f) ⇒ Object



66
67
68
# File 'lib/vagrant/action/box/download.rb', line 66

def download_to(f)
  @downloader.download!(@env["box"].uri, f)
end

#instantiate_downloaderObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vagrant/action/box/download.rb', line 28

def instantiate_downloader
  @env["download.classes"].each do |klass|
    if klass.match?(@env["box"].uri)
      @env.ui.info I18n.t("vagrant.actions.box.download.with", :class => klass.to_s)
      @downloader = klass.new(@env)
    end
  end

  raise Errors::BoxDownloadUnknownType if !@downloader

  @downloader.prepare(@env["box"].uri)
  true
end

#recover(env) ⇒ Object



49
50
51
52
53
54
# File 'lib/vagrant/action/box/download.rb', line 49

def recover(env)
  if temp_path && File.exist?(temp_path)
    env.ui.info I18n.t("vagrant.actions.box.download.cleaning")
    File.unlink(temp_path)
  end
end

#with_tempfileObject



56
57
58
59
60
# File 'lib/vagrant/action/box/download.rb', line 56

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