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
    )
  • See the remote_directory resource, documentation : 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
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
  • 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
  • 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
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.