2016-04-06 29 views

回答

0

這不是廚師直接支持的東西。您可以使用多個remote_file資源和ruby_blockexecutecat來實現concat。

0

remote_file不支持級聯,所以你不能就能夠這樣直接使用該資源來實現,但是你可以使用file資源拼湊期望的結果和Net::HTTP像這樣:

file_path = '/path/to/your_whole_file' 

unless File.exist?(file_path) && 
     Digest::SHA256.hexdigest(File.read(file_path)) == 'your_file_checksum' 
    file file_path do 
    content(
     Net::HTTP.get(URI('http://source.url/content1')) + 
     Net::HTTP.get(URI('http://source.url/content2')) 
    ) 
    owner 'root' 
    group 'root' 
    mode '0755' 
    end 
end 

原因Digest::SHA256開頭的呼叫是爲了防止Chef在每次Chef運行期間嘗試下載這兩個文件。請注意,您可能必須要求配方頂部的net/httpdigest寶石才能運行。

此外,由於違反了將Ruby代碼直接放入食譜的最佳做法,您可能希望將上述代碼包裝在簡單的custom resource中。

+0

幾乎總是錯誤的想法廚師代碼使用'網:: HTTP'。你應該使用'Chef :: HTTP'來代替。 – coderanger

+0

是啊這個答案相當複雜。最好使用兩個remote_file資源下載到Chef :: Config [:file_cache_path]並依靠廚師的冪等性在每次運行時不重新下載。然後構建一個文件資源,其中內容將IO.read連接到remote_fille資源使用的路徑名。 – lamont

+0

關於它被捲入錯誤(雖然我認爲請求是一個相當具體的請求),但爲什麼調用'IO.read'比調用'Net :: HTTP'(或者'Chef :: HTTP'> )? –

0

像這樣的東西可能是開始,但是隨後調整你喜歡的好去處:

cache1 = "#{Chef::Config[:file_cache_path]}/content1" 
cache2 = "#{Chef::Config[:file_cache_path]}/content2" 

# this will not redownload if cache1 exists and has not been updated 
remote_file cache1 do 
    source "http://source.url/content1" 
end 

# this will not redownload if cache1 exists and has not been updated 
remote_file cache2 do 
    source "http://source.url/content2" 
end 

# this will not update the file if the contents has not changed 
file "/my/combined/file" do 
    content lazy { IO.read(cache1) + IO.read(cache2) } 
end 
相關問題