Lokeshwari V
Lokeshwari V

Reputation: 29

Chef - Download Artifact from Nexus using remote_file resource

I'm using Chef to download a war file into a folder from nexus.The Recipe is as below

  remote_file '/home/Test/AAA.war' do    
  source 'https://IP.com:8082/ URL Of the Repo/AAA.war'    
  owner 'root'    
  group 'root'    
  mode '0755'   
  action :create  
  end

However If I run this recipe ,I am getting Unauthorised access error.

Is it necessary to give username and password for nexus login?

should i write a batch script to download from nexus instead of recipe?

Upvotes: 1

Views: 4187

Answers (3)

Bhaskar Jagadeesha
Bhaskar Jagadeesha

Reputation: 209

To download Nexus artifact on Windows Machine.. You can use the remote_file resource like this:

   remote_file  'D:/local/Chef/file.zip' do    
     source 'https://Nexus_URL_Address/file.zip'
     username = 'xxxx'
     password = 'xxxxxxx'
     headers( "Authorization"=>"Basic #{ Base64.encode64("#{username}:#{password}").gsub("\n", "") }" )
   end

Upvotes: 0

Tom Lime
Tom Lime

Reputation: 1204

This is how I did it (based on coderanger answer).

nodejs_url = node['ci_cd']['ubu_nodejs_url']
...

remote_file nodejs_path do                                                                                          
  source   nodejs_url.gsub(/(https?:\/{2})/, '%s%s:%s@' % ['\1', artifactory_user, artifactory_pass])        
  checksum nodejs_url_checksum                                                                               
  owner    'root'                                                                                            
  group    'root'                                                                                            
  mode     '0644'                                                                                            
  notifies :install, 'dpkg_package[nodejs]', :immediately                                                    
  not_if   { ::File.exists?(nodejs_path) }                                                                            
end

dpkg_package 'nodejs' do                                                                                            
  source nodejs_path                                                                                               
  action :nothing                                                                                                  
end

In this example I created dependency between remote_file and dpkg_package just to manage guard (not_if) in one place.

Upvotes: 0

tfitch
tfitch

Reputation: 31

First way I would verify if your Nexus server requires authentication to download any files. I'd do this by opening your browser in Incognito Mode and go the URL of the source property of your remote_file resource. I'm expecting it to give you a login page with a "Unauthorised access error". This will be confirmation of your question that it is necessary to give a username and password for a Nexus login.

To do this, you can pass the necessary credentials to the Nexus server by using the header property of the remote_file resource.

Full details about this are available here. https://docs.chef.io/resource_remote_file.html#properties

I don't know Nexus's login method specifically, but it could look something like this example from the docs page. headers( "Authorization"=>"Basic #{ Base64.encode64("#{username}:#{password}").gsub("\n", "") }" )

Upvotes: 3

Related Questions