Class: Vagrant::Actions::Box::Download

Inherits:
Vagrant::Actions::Base show all
Defined in:
lib/vagrant/actions/box/download.rb

Overview

An action which acts on a box by downloading the box file from the given URI into a temporary location. This action parses a given URI and handles downloading it via one of the many Vagrant downloads (such as Downloaders::File).

This action cleans itself up by removing the downloaded box file.

Constant Summary collapse

BASENAME =
"box"
BUFFERSIZE =

1 MB

1048576

Instance Attribute Summary collapse

Attributes inherited from Vagrant::Actions::Base

#runner

Instance Method Summary collapse

Methods inherited from Vagrant::Actions::Base

#follows, #initialize, #precedes

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

This class inherits a constructor from Vagrant::Actions::Base

Instance Attribute Details

#downloaderObject (readonly)

Returns the value of attribute downloader.



14
15
16
# File 'lib/vagrant/actions/box/download.rb', line 14

def downloader
  @downloader
end

Instance Method Details

#cleanupObject



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

def cleanup
  if @runner.temp_path && File.exist?(@runner.temp_path)
    logger.info "Cleaning up downloaded box..."
    File.unlink(@runner.temp_path)
  end
end

#download_to(f) ⇒ Object



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

def download_to(f)
  logger.info "Copying box to temporary location..."
  downloader.download!(@runner.uri, f)
end

#execute!Object



34
35
36
37
38
39
# File 'lib/vagrant/actions/box/download.rb', line 34

def execute!
  with_tempfile do |tempfile|
    download_to(tempfile)
    @runner.temp_path = tempfile.path
  end
end

#file_temp_pathObject



61
62
63
# File 'lib/vagrant/actions/box/download.rb', line 61

def file_temp_path
  File.join(@runner.env.tmp_path, BASENAME + Time.now.to_i.to_s)
end

#prepareObject

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant/actions/box/download.rb', line 16

def prepare
  # Parse the URI given and prepare a downloader
  uri = URI.parse(@runner.uri)
  uri_map = [[URI::HTTP, Downloaders::HTTP], [URI::Generic, Downloaders::File]]

  uri_map.find do |uri_type, downloader_klass|
    if uri.is_a?(uri_type)
      logger.info "#{uri_type} for URI, downloading via #{downloader_klass}..."
      @downloader = downloader_klass.new
    end
  end

  raise ActionException.new(:box_download_unknown_type) unless @downloader

  # Prepare the downloader
  @downloader.prepare(@runner.uri)
end

#rescue(exception) ⇒ Object



48
49
50
# File 'lib/vagrant/actions/box/download.rb', line 48

def rescue(exception)
  cleanup
end

#with_tempfileObject



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

def with_tempfile
  logger.info "Creating tempfile for storing box file..."

  # create, write only, fail if the file exists
  File.open(file_temp_path, File::WRONLY | File::EXCL | File::CREAT) do |tempfile|
    yield tempfile
  end
end