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



74
75
76
# File 'lib/vagrant/action/box/download.rb', line 74

def box_temp_path
  @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



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

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

#download_to(f) ⇒ Object



78
79
80
# File 'lib/vagrant/action/box/download.rb', line 78

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

#instantiate_downloaderObject



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

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["box_url"])
      @env[:ui].info I18n.t("vagrant.actions.box.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["box_url"])
  true
end

#recover(env) ⇒ Object



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

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



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

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