Search code examples
chef-infrachef-recipechef-solo

Chef remote_file set maximum number of re-directions to zero


I have been 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.

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

The --max-redirect=0 flag in wget makes sure that there isn't any redirection.

The download url is sometimes redirected to ISP bill reminder page. And chef remote_file resource downloads this bill reminder html page as a zip file.

I can just add the command to a execute resource by wrapping it within. Or implement this using ruby-block with open-uri/net-http.

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

But is there any Chef like implementation to set the redirection as zero or false?

My chef recipe resource block is

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

Solution

  • 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