Class: VagrantPlugins::S3MultiDownloader::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-s3-multidownloader/downloader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, ui, options = {}) ⇒ Downloader

Returns a new instance of Downloader.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-s3-multidownloader/downloader.rb', line 38

def initialize(source, destination, ui, options = {})
  @source = source
  @destination = destination
  @ui = ui
  @options = options
  @env = options[:env]
  @logger = Log4r::Logger.new("vagrant::s3-multidownloader")
  @metadata_handler = MetadataHandler.new(ui)

  @logger.info("S3MultiDownloader initialized")
  @logger.info("Source: #{@source}")
  @logger.info("Destination: #{@destination}")
end

Class Method Details

.patch_vagrant_downloaderObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-s3-multidownloader/downloader.rb', line 15

def self.patch_vagrant_downloader
  ::Vagrant::Util::Downloader.class_eval do
    alias_method :original_download!, :download! unless method_defined?(:original_download!)

    def download!
      if @source.to_s.start_with?("s3://")
        logger = Log4r::Logger.new("vagrant::downloaders::s3")
        logger.info("Detected S3 URL: #{@source}")

        # Use our custom S3 downloader
        downloader = VagrantPlugins::S3MultiD ownloader::Downloader.new(
          @source, @destination, @ui, continue: @continue, headers: @headers
        )
        return downloader.download!
      end
      original_download!
    end
  end
end

Instance Method Details

#download!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-s3-multidownloader/downloader.rb', line 52

def download!
  # Parse the S3 URL
  uri, bucket, key = parse_s3_url(@source)

  # Log download information
  log_download_info(bucket, key)

  # Create S3 client
  client = create_s3_client

  begin
    # Check for requested version from Vagrantfile
    requested_version = @metadata_handler.get_requested_version(@env)
    log_message(:info, "Box version requested: #{requested_version || 'latest'}") if requested_version

    # Check if this is a metadata.json file
     = key.end_with?("metadata.json")
    log_message(:detail, "File type: #{ ? 'metadata.json' : 'box file'}")

    if 
      # Handle metadata.json specially
      (client, bucket, key, requested_version)
    else
      # Direct box download
      download_s3_object(client, bucket, key)
      log_message(:success, "Successfully downloaded #{@source} to #{@destination}")
    end
  rescue Aws::S3::Errors::Forbidden => e
    handle_forbidden_error(e)
  rescue Aws::S3::Errors::NotFound => e
    handle_not_found_error(e, bucket, key)
  rescue Aws::S3::Errors::ServiceError => e
    handle_service_error(e)
  rescue StandardError => e
    handle_standard_error(e)
  end
end