【问题标题】:Chef remote_file set maximum number of re-directions to zeroChef remote_file 将最大重定向数设置为零
【发布时间】:2019-01-19 02:04:24
【问题描述】:

我一直在尝试使用 chef remote_file 资源实现以下 wget 下载命令。但我找不到避免重定向的方法。

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

wget 中的 --max-redirect=0 标志确保没有任何重定向。

下载网址有时会重定向到 ISP 账单提醒页面。 Chef remote_file 资源将此账单提醒 html 页面下载为 zip 文件。

我可以通过包装将命令添加到执行资源中。或者使用带有 open-uri/net-http 的 ruby​​-block 来实现。

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

但是有没有类似 Chef 的实现将重定向设置为零或假?

我的厨师食谱资源块是

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

【问题讨论】:

    标签: chef-infra chef-recipe chef-solo


    【解决方案1】:

    发现 remote_file 资源无法处理重定向。因此,我不得不编写一个使用 'Down' gem 的 ruby​​_block 资源。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 2016-01-18
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多