5

I am wondering if there is a resource to download multiple files from remote using chef resource. I want to use:

remote_file 'Download remote file' do
  path /opt/
  source http:///xxx
  mode '0644'
  action :create
end

...for one file. What if I want to download multiple files in a same place with different urls? Or how can I modify this resource to make multiple downloads which have different URLs?

This is how I added attributes in recipe:

node['file']['name'].each do |pkg|
    remote_file "path/plugins/#{pkg}" do
        source "node['file']['url']/#{pkg}"
        action :create
    end
end

in attributes

default['file']['name'] = %w(
    xx-2.0.jar
    xx-2.jar
    xx.jar
    )
pandey
  • 949
  • 2
  • 9
  • 18
  • See the remote_directory resource, documentation : https://docs.chef.io/resource_remote_directory.html – Tensibai Apr 27 '17 at 19:13
  • @Tensibai I couldn't see there anything with multiple URL's – pandey Apr 27 '17 at 19:38
  • Aww, sorry, I did miss this point, a loop sounds the way to go, you'll have to specify early h file BTW. I'll try an answer, but there can be typo – Tensibai Apr 27 '17 at 19:52

2 Answers2

5

what about something like that:

%w{
        mysql-community-common-5.7.16-1.el7.x86_64.rpm
        mysql-community-libs-5.7.16-1.el7.x86_64.rpm
        mysql-community-client-5.7.16-1.el7.x86_64.rpm
        mysql-community-server-5.7.16-1.el7.x86_64.rpm
}.each do |pkg|
        remote_file "/tmp/#{pkg}" do
          source "https://s3.amazonaws.com/tmp/mysql/#{pkg}"
        end

       rpm_package pkg do
        source "/tmp/#{pkg}"
        action :install
      end
end

another way:

urllist = {
  { 'url': 'http://some.url1/', 'path': '/some/path1/', 'filename': 'some.file' },
  { 'url': 'https://some.url2/', 'path': '/some/path2/', 'filename': 'another.file'}
}

urllist.each do |urlinfo|
  remote_file "#{urlinfo['path']}/#{urlinfo['filename']}" do
    source "#{urlinfo['url']}/#{urlinfo['filename']}"
    owner 'someowner'
    group 'somegroup'
    mode 0755
  end
end
Berlin
  • 780
  • 6
  • 15
  • looks great @Berlin. is there ant way I can setup path to install them. I would like to install some plugins for Jenkins in a specified path like eg: '/opt/XXX/plugins/'. I want to download those plugins into plugins folder but some times it says plugins exists but its a folder – pandey Apr 27 '17 at 20:03
  • @Berlin it worked for me. Is there any way that I can write attribute for those rpm that you mentioned – pandey Apr 27 '17 at 20:12
  • mysql-community-common-5.7.16-1.el7.x86_64.rpm mysql-community-libs-5.7.16-1.el7.x86_64.rpm mysql-community-client-5.7.16-1.el7.x86_64.rpm mysql-community-server-5.7.16-1.el7.x86_64.rpm How can I write this thing as attribute – pandey Apr 27 '17 at 20:26
  • 1
    see below @Tensibai answer attributes/default.rb, is it what you looking for ? – Berlin Apr 27 '17 at 20:29
  • @berlin sorry for the edit, seems my phone disagree with me on spaces and it is not better :/ I let you fix it, that's a valid options for multiples files from same source – Tensibai Apr 27 '17 at 20:33
  • @Tensibai no worries, it is fine. – Berlin Apr 27 '17 at 20:39
  • 1
    @berlin for information you can paste indented code, select it an press the code button (the {} ) or ctrl+k to indent a block with 4 spaces and keep original format – Tensibai Apr 27 '17 at 20:43
  • when I do as @Tensibai way for attributes I am getting error as undefined method `each' for – pandey Apr 27 '17 at 20:53
  • @pandey edit your question to show what you did, guessing won't help to help you) – Tensibai Apr 27 '17 at 20:58
  • @pandey or maybe better, ask a new one as a follow up from this one with your actual problem to not invalidate actual answers – Tensibai Apr 27 '17 at 21:13
  • I solved it @Tensibai its a spacing problem. its my bad....thanks for your help. I am thankful to both of you and Berlin – pandey Apr 27 '17 at 21:25
  • @pandey you're welcome :) I hope I won't sound selfish but think about upvoting answers you find useful and as you already did accept the one you prefer – Tensibai Apr 27 '17 at 21:30
  • because he did not ask from the beginning on attributes :) – Berlin Apr 27 '17 at 21:38
2

What I do for this case is using node attributes hash for a key value mapping as follow :

In attributes/default.rb

default['namespace']['files']['file1']='http://server1/path/source_fileX'
default['namespace']['files']['file2']='http://server2/path/source_fileY'

Or with hash notation :

default['namespace']['files']={'file1' => 'http://server1/path/source_fileX',
'file2'=>'http://server2/path/source_fileY'}

Then in recipe:

node['namespace']['files'].each do |filename,src|
  remote_file "/path/destinaton/#{filename}" do
    source src
    mode '0644'
    action :create
  end
end

This way you can map a file with its source taking advantage that node attributes are a hash structure.

If course you can set the full path instead of just the file name and avoid the string interpolation in the recipe remote_file.

(Typed on phone, please forgive typos)

Tensibai
  • 11,366
  • 2
  • 35
  • 62