Sha256: 4a5b1eef93e350d5070fba050b3c03b68804ab61256cd2bb576bf583e2b2afa9

Contents?: true

Size: 1.85 KB

Versions: 10

Compression:

Stored size: 1.85 KB

Contents

module Vagrant
  class Action
    module Box
      class Download
        BASENAME = "box"

        include Util

        attr_reader :temp_path

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

        def call(env)
          @env = env

          download if instantiate_downloader
          return if env.error?

          @app.call(@env)

          cleanup
        end

        def instantiate_downloader
          @env["download.classes"].each do |klass|
            if klass.match?(@env["box"].uri)
              @env.logger.info "Downloading with #{klass}..."
              @downloader = klass.new(@env)
            end
          end

          if !@downloader
            @env.error!(:box_download_unknown_type)
            return false
          end

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

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

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

        def with_tempfile
          @env.logger.info "Creating tempfile for storing box file..."
          File.open(box_temp_path, Platform.tar_file_options) do |tempfile|
            yield tempfile
          end
        end

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

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

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
vagrantup-0.5.4 lib/vagrant/action/box/download.rb
vagrantup-0.5.3 lib/vagrant/action/box/download.rb
vagrantup-0.5.2 lib/vagrant/action/box/download.rb
vagrantup-0.5.1 lib/vagrant/action/box/download.rb
vagrantup-0.5.0 lib/vagrant/action/box/download.rb
vagrant-0.5.4 lib/vagrant/action/box/download.rb
vagrant-0.5.3 lib/vagrant/action/box/download.rb
vagrant-0.5.2 lib/vagrant/action/box/download.rb
vagrant-0.5.1 lib/vagrant/action/box/download.rb
vagrant-0.5.0 lib/vagrant/action/box/download.rb