"one-off" use of http_proxy in a Chef remote_file resource

Posted by user169200 on Server Fault See other posts from Server Fault or by user169200
Published on 2013-04-12T07:38:32Z Indexed on 2014/08/20 22:22 UTC
Read the original article Hit count: 266

Filed under:

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

© Server Fault or respective owner

Related posts about chef