Class: VagrantPlugins::S3MultiDownloader::HandleS3Urls

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ HandleS3Urls

Returns a new instance of HandleS3Urls.



7
8
9
10
11
# File 'lib/vagrant-s3-multidownloader/middleware/handle_s3_urls.rb', line 7

def initialize(app, env)
  @app = app
  @env = env
  @logger = Log4r::Logger.new("vagrant::s3-multidownloader")
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant-s3-multidownloader/middleware/handle_s3_urls.rb', line 13

def call(env)
  @env = env

  # Save references to the original Downloader methods
  unless defined? @@original_curl_execute
    @@original_curl_execute = Vagrant::Util::Downloader.instance_method(:execute_curl)
  end

  unless defined? @@original_download
    @@original_download = Vagrant::Util::Downloader.instance_method(:download!)
  end

  # Override both download! and execute_curl to handle s3:// URLs
  # We need to intercept at both levels to ensure we catch all S3 URL usage
  unless Vagrant::Util::Downloader.instance_methods.include?(:original_download)
    Vagrant::Util::Downloader.class_eval do
      alias_method :original_download, :download!

      def download!
        if @source.to_s.start_with?("s3://")
          ui = @ui || @logger
          ui.info("Intercepted S3 download at download! level: #{@source}")
          downloader = VagrantPlugins::S3MultiDownloader::Downloader.new(
            @source, @destination, ui,
            continue: @continue,
            headers: @headers,
            env: @env
          )
          return downloader.download!
        end
        original_download
      end
    end
  end

  # Also override execute_curl to catch any direct curl executions with S3 URLs
  unless Vagrant::Util::Downloader.instance_methods.include?(:original_execute_curl)
    Vagrant::Util::Downloader.class_eval do
      alias_method :original_execute_curl, :execute_curl

      def execute_curl(options, subprocess_options, &data_proc)
        # More aggressively check for S3 URLs in the options array
        # This needs to catch any occurrence of s3:// in the curl command
        s3_url = nil
        options.each do |opt|
          if opt.to_s =~ /^s3:\/\//
            s3_url = opt.to_s
            break
          end
        end

        if s3_url
          ui = @ui || @logger
          ui.info("Intercepted S3 URL in curl command: #{s3_url}")

          # Find the output path from the curl options
          output_index = options.find_index("--output")
          output_path = output_index ? options[output_index + 1] : nil

          if output_path
            ui.detail("Output path: #{output_path}")
            # Use our S3 downloader instead
            downloader = VagrantPlugins::S3MultiDownloader::Downloader.new(
              s3_url, output_path, ui, env: @env
            )

            begin
              ui.detail("Downloading from S3 via custom downloader: #{s3_url}")
              downloader.download!
              ui.detail("S3 download completed successfully")
              return 0 # Return success status code
            rescue => e
              ui.error("S3 download error: #{e.message}")
              ui.error(e.backtrace.join("\n"))
              return 1 # Return error status code
            end
          else
            ui.error("Could not determine output path for S3 download")
            return 1
          end
        end

        # For non-S3 URLs, use the original method
        original_execute_curl(options, subprocess_options, &data_proc)
      end
    end
  end

  # Continue middleware chain
  @app.call(env)
end