diff --git a/lib/omnibus/fetchers/net_fetcher.rb b/lib/omnibus/fetchers/net_fetcher.rb index eec94dcd3..a892bb30b 100644 --- a/lib/omnibus/fetchers/net_fetcher.rb +++ b/lib/omnibus/fetchers/net_fetcher.rb @@ -148,7 +148,8 @@ def download_url if Config.use_s3_caching S3Cache.url_for(self) elsif Config.use_internal_sources && !source[:internal] - raise InternalSourceMissing.new(self) + log.warn(log_key) { "Internal source missing for #{name}; falling back to external source URL." } + source[:url] else source[:url] end @@ -161,6 +162,22 @@ def download_url # # @return [void] # + # def download + # log.warn(log_key) { source[:warning] } if source.key?(:warning) + + # options = {} + + # if source[:unsafe] + # log.warn(log_key) { "Permitting unsafe redirects!" } + # options[:allow_unsafe_redirects] = true + # end + + # # Set the cookie if one was given + # options["Cookie"] = source[:cookie] if source[:cookie] + # options["Authorization"] = source[:authorization] if source[:authorization] + + # download_file!(download_url, downloaded_file, options) + # end def download log.warn(log_key) { source[:warning] } if source.key?(:warning) @@ -175,7 +192,19 @@ def download options["Cookie"] = source[:cookie] if source[:cookie] options["Authorization"] = source[:authorization] if source[:authorization] - download_file!(download_url, downloaded_file, options) + begin + # Attempt download from internal or S3 source URL + download_file!(download_url, downloaded_file, options) + rescue OpenURI::HTTPError => e + # If 404 comes from internal source, fallback to external URL + if e.io.status[0] == "404" && Config.use_internal_sources && source[:internal] + log.warn(log_key) { "Internal source returned 404 Not Found for #{name}, falling back to external source URL." } + download_file!(source[:url], downloaded_file, options) + else + # Re-raise other errors + raise + end + end end # diff --git a/spec/functional/fetchers/net_fetcher_spec.rb b/spec/functional/fetchers/net_fetcher_spec.rb index 73c278275..9215e879c 100644 --- a/spec/functional/fetchers/net_fetcher_spec.rb +++ b/spec/functional/fetchers/net_fetcher_spec.rb @@ -173,11 +173,15 @@ def stub_ohai(options = {}, &block); end end end - context "when use_internal_sources is true and no internal source url" do - before { Omnibus::Config.use_internal_sources(true) } + context "when use_internal_sources is true but internal source is missing" do + before do + Config.use_internal_sources(true) + end - it "raises an exception" do - expect { fetch! }.to raise_error(InternalSourceMissing) + it "logs a warning and falls back to the external url" do + expect(subject).to receive(:log).and_call_original + url = subject.send(:download_url) + expect(url).to eq(source[:url]) end end