Class: Vagrant::Util::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/uploader.rb

Overview

This class uploads files using various protocols by subprocessing to cURL. cURL is a much more capable and complete download tool than a hand-rolled Ruby library, so we defer to its expertise.

Instance Method Summary collapse

Constructor Details

#initialize(destination, file, options = nil) ⇒ Uploader

Returns a new instance of Uploader.

Parameters:

  • destination (String)

    Valid URL to upload file to

  • file (String)

    Location of file to upload on disk

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :ui (Vagrant::UI)

    UI interface for output

  • :method (String, Symbol)

    Request method for upload



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant/util/uploader.rb', line 24

def initialize(destination, file, options=nil)
  options ||= {}
  @logger         = Log4r::Logger.new("vagrant::util::uploader")
  @destination    = destination.to_s
  @file           = file.to_s
  @ui             = options[:ui]
  @request_method = options[:method]

  if !@request_method
    @request_method = "PUT"
  end
  @request_method = @request_method.to_s.upcase
end

Instance Method Details

#upload!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant/util/uploader.rb', line 38

def upload!
  data_proc = Vagrant::Util::CurlHelper.capture_output_proc(@logger, @ui)

  @logger.info("Uploader starting upload: ")
  @logger.info("  -- Source: #{@file}")
  @logger.info("  -- Destination: #{@destination}")

  options = build_options
  subprocess_options = {notify: :stderr}

  begin
    execute_curl(options, subprocess_options, &data_proc)
  rescue Errors::UploaderError => e
    raise
  ensure
    @ui.clear_line if @ui
  end
end