“one-off” use of http_proxy in a Chef remote_file resource

chef

I have a use case where most of my remote_file resources and yum resources download files directly from an internal server. However, there is a need to download one or two files with remote_file that is outside our firewall and which must go through a HTTP proxy. If I set the http_proxy setting in /etc/chef/client.rb, it adversely affects the recipe's ability to download yum and other files from internal resources. Is there a way to have a remote_file resource download a remote URL through a proxy without setting the http_proxy value in /etc/chef/client.rb?

In my sample code, below, I'm downloading a redmine bundle from rubyforge.org, which requires my servers to go through a corporate proxy. I came up with a ruby_block before and after the remote_file resource that sets the http_proxy and "unsets" it. I'm looking for a cleaner way to do this.

ruby_block "setenv-http_proxy" do
    block do
        Chef::Config.http_proxy = node['redmine']['http_proxy']
        ENV['http_proxy'] = node['redmine']['http_proxy']
        ENV['HTTP_PROXY'] = node['redmine']['http_proxy']
    end
    action node['redmine']['rubyforge_use_proxy'] ? :create : :nothing
    notifies :create_if_missing, "remote_file[redmine-bundle.zip]", :immediately
end

remote_file "redmine-bundle.zip" do
    path "#{Dir.tmpdir}/redmine-#{attrs['version']}-bundle.zip"
    source attrs['download_url']
    mode "0644"
    action :create_if_missing
    notifies :decompress, "zipp[redmine-bundle.zip]", :immediately
    notifies :create, "ruby_block[unsetenv-http_proxy]", :immediately
end

ruby_block "unsetenv-http_proxy" do
    block do
        Chef::Config.http_proxy = nil
        ENV['http_proxy'] = nil
        ENV['HTTP_PROXY'] = nil
    end
    action node['redmine']['rubyforge_use_proxy'] ? :create : :nothing
end

Best Answer

You could try setting :http_proxy in the recipe immediately before the remote_file (and unset it afterward), like this:

save_http_proxy = Chef::Config[:http_proxy]
Chef::Config[:http_proxy] = "http://someproxyserver.com:8080"

remote_file "redmine-bundle.zip" do
    path "#{Dir.tmpdir}/redmine-#{attrs['version']}-bundle.zip"
    source attrs['download_url']
    mode "0644"
    action :create_if_missing
    notifies :decompress, "zipp[redmine-bundle.zip]", :immediately
    notifies :create, "ruby_block[unsetenv-http_proxy]", :immediately
end

Chef::Config[:http_proxy] = save_http_proxy