Sha256: ac5d66f8b97c5c9472143043c6c5ebbfb08fe5c31ef9ff8f52d8b099ed1bc052

Contents?: true

Size: 1.75 KB

Versions: 35

Compression:

Stored size: 1.75 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]
          @downloader = nil
        end

        def call(env)
          @env = env

          download if instantiate_downloader
          @app.call(@env)

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

        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

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

        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

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

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

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

Version data entries

35 entries across 35 versions & 5 rubygems

Version Path
vagrantup-0.8.7 lib/vagrant/action/box/download.rb
vagrantup-0.8.6 lib/vagrant/action/box/download.rb
vagrantup-0.8.5 lib/vagrant/action/box/download.rb
vagrantup-0.8.4 lib/vagrant/action/box/download.rb
vagrantup-0.8.3 lib/vagrant/action/box/download.rb
vagrantup-0.8.2 lib/vagrant/action/box/download.rb
vagrantup-0.8.1 lib/vagrant/action/box/download.rb
vagrantup-0.8.0 lib/vagrant/action/box/download.rb
vagrantup-0.7.8 lib/vagrant/action/box/download.rb
vagrantup-0.7.7 lib/vagrant/action/box/download.rb
vagrantup-0.7.6 lib/vagrant/action/box/download.rb
vagrantup-0.7.5 lib/vagrant/action/box/download.rb
vagrantup-0.7.4 lib/vagrant/action/box/download.rb
vagrantup-0.7.3 lib/vagrant/action/box/download.rb
vagrantup-0.7.2 lib/vagrant/action/box/download.rb
vagrantup-0.7.1 lib/vagrant/action/box/download.rb
vagrantup-0.7.0 lib/vagrant/action/box/download.rb
vagrant-0.8.7 lib/vagrant/action/box/download.rb
rvagrant-0.8.7.dev lib/vagrant/action/box/download.rb
vagrant-0.8.6 lib/vagrant/action/box/download.rb