1

I am trying to implement the below wget download command using chef remote_file resource. But I couldn't find a way to avoid the re-directions. The URL that I am trying to access may sometimes be redirected to my ISP bill reminder page. And chef remote_file resource downloads this bill reminder HTML page as a zip file.

wget -N --max-redirect=0 http://www.someurl.com/file.zip

The chef recipe resource is

remote_file "#{node['download-zip-path']}/#{zip}" do
    source "http://www.someurl.com/#{zip}"
    action :create
    notifies :run, 'execute[unzip_file]', :delayed
end

The --max-redirect=0 flag in wget makes sure that there isn't any redirection to ISP page. I can achieve the same using ruby's open-uri or execute command resource but is there any native chef way to achieve this no/zero re-direction?

4
  • No there's not, failing to download would mean failure of run. The proper course of action is to host the target file on an internal server, don't trust remotes to be available
    – Tensibai
    Commented Jan 13, 2019 at 15:55
  • Thanks for the reply. The chef-client machines are deployed at customer premises and are connected to the internet through the client's ISPs. The file that is downloaded is vpn config and hence hosing on a internal server is not possible. So I will go ahead with a ruby module and add it as a library for my recipes.
    – Yuge
    Commented Jan 14, 2019 at 14:39
  • Yes, you have to bake that yourself
    – Tensibai
    Commented Jan 14, 2019 at 15:02
  • @Tensibai I have make use of the Down gem in ruby and write a ruby_block resource.
    – Yuge
    Commented Jan 18, 2019 at 17:58

1 Answer 1

1

Found out that the remote_file resource couldn't handle redirects. Hence I had to write a ruby_block resource that makes use of 'Down' gem.

ruby_block 'download_openvpn_zip' do
    block do 
        attempt = 2
        begin
            retries ||= 0
            tempfile = Down::NetHttp.download("http://www.someurl.com/#{zip},max_redirects: 0)
            FileUtils.mv tempfile.path, "#{node['openvpn-conf-path']}/#{tempfile.original_filename}"
        rescue Down::TooManyRedirects => e
            puts "\n \t ERROR: #{e.message}"
            retry if (retries += 1) < 1
        end 
    end
    action :run
    notifies :run, 'execute[unzip_file]', :delayed
end
3
  • Maybe you could have achieved the same thing using the http class of chef, but I'm not really sure. Thanks for sharing your solution anyway :)
    – Tensibai
    Commented Jan 18, 2019 at 21:52
  • 1
    You way have a better experience using Chef::Log.warn instead of puts also.
    – Tensibai
    Commented Jan 18, 2019 at 21:53
  • Thanks for the tip, will be using the Chef::Log.warn instead of puts.
    – Yuge
    Commented Jan 18, 2019 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.