diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-04-04 09:50:31 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-04-04 12:50:43 -0700 |
commit | 467499fbbbd3a29bdee2b171655f0d5f441e531e (patch) | |
tree | 8fda465d4bdf71fe26c781ba77a0f5fa0d06ca19 | |
parent | 6b8ca8c387b01799ed94fcd8555ba30618905a1d (diff) | |
download | chef-467499fbbbd3a29bdee2b171655f0d5f441e531e.tar.gz |
Chef-13: tweaks to rubygems source option for urls
By default now we don't pass any --source option or set the
`Chef::Config[:rubygems_url]` to anything. This is so that
if there is external config for rubygems sources we will simply
have rubygems pick it up and use it (the resource acts like
the command line).
There is also no more magic around `--clear-sources` and if you
set the property it sets the flag.
If the `Chef::Config[:rubygems_url]` setting is set then that
becomes the default `--source` flag sent to the command. This
also supports Arrays now.
If the `source` option is set to a URI (it can also be set to a
filesystem path for poorly-designed-API reasons) then we do not
use the default values at all. This may be an API break but this
much more clearly allows users to override individual resources
and is more declarative. You get what you type.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 3 | ||||
-rw-r--r-- | lib/chef/cookbook/gem_installer.rb | 6 | ||||
-rw-r--r-- | lib/chef/provider/package/rubygems.rb | 17 | ||||
-rw-r--r-- | lib/chef/resource/gem_package.rb | 13 | ||||
-rw-r--r-- | spec/unit/provider/package/rubygems_spec.rb | 2 |
5 files changed, 29 insertions, 12 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index 9bffb66eca..b4bb6b76ab 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -1050,7 +1050,8 @@ module ChefConfig # break Chef community cookbooks and is very highly discouraged. default :ruby_encoding, Encoding::UTF_8 - default :rubygems_url, "https://rubygems.org" + # can be set to a string or array of strings for URIs to set as rubygems sources + default :rubygems_url, nil # If installed via an omnibus installer, this gives the path to the # "embedded" directory which contains all of the software packaged with diff --git a/lib/chef/cookbook/gem_installer.rb b/lib/chef/cookbook/gem_installer.rb index 365b185426..5b1426e4e8 100644 --- a/lib/chef/cookbook/gem_installer.rb +++ b/lib/chef/cookbook/gem_installer.rb @@ -1,5 +1,5 @@ #-- -# Copyright:: Copyright (c) 2010-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2010-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,7 +50,9 @@ class Chef begin Dir.mktmpdir("chef-gem-bundle") do |dir| File.open("#{dir}/Gemfile", "w+") do |tf| - tf.puts "source '#{Chef::Config[:rubygems_url]}'" + Array(Chef::Config[:rubygems_url] || "https://www.rubygems.org").each do |s| + tf.puts "source '#{s}'" + end cookbook_gems.each do |gem_name, args| tf.puts "gem(*#{([gem_name] + args).inspect})" end diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb index 9bf8fb5fbc..1269b24c93 100644 --- a/lib/chef/provider/package/rubygems.rb +++ b/lib/chef/provider/package/rubygems.rb @@ -533,18 +533,21 @@ class Chef end def install_via_gem_command(name, version) - if new_resource.source =~ /\.gem$/i + src = [] + if new_resource.source.is_a?(String) && new_resource.source =~ /\.gem$/i name = new_resource.source - elsif new_resource.clear_sources - src = " --clear-sources" - src << (new_resource.source && " --source=#{new_resource.source}" || "") else - src = new_resource.source && " --source=#{new_resource.source} --source=#{Chef::Config[:rubygems_url]}" + src << "--clear-sources" if new_resource.clear_sources + srcarry = [ new_resource.source || Chef::Config[:rubygems_url] ].flatten.compact + srcarry.each do |s| + src << "--source=#{s}" + end end + src_str = src.empty? ? "" : " #{src.join(" ")}" if !version.nil? && !version.empty? - shell_out_with_timeout!("#{gem_binary_path} install #{name} -q --no-rdoc --no-ri -v \"#{version}\"#{src}#{opts}", env: nil) + shell_out_with_timeout!("#{gem_binary_path} install #{name} -q --no-rdoc --no-ri -v \"#{version}\"#{src_str}#{opts}", env: nil) else - shell_out_with_timeout!("#{gem_binary_path} install \"#{name}\" -q --no-rdoc --no-ri #{src}#{opts}", env: nil) + shell_out_with_timeout!("#{gem_binary_path} install \"#{name}\" -q --no-rdoc --no-ri #{src_str}#{opts}", env: nil) end end diff --git a/lib/chef/resource/gem_package.rb b/lib/chef/resource/gem_package.rb index 5511d3c580..bcbe6d37b3 100644 --- a/lib/chef/resource/gem_package.rb +++ b/lib/chef/resource/gem_package.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2008-2016, Chef Software Inc. +# Copyright:: Copyright 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,6 +23,17 @@ class Chef class GemPackage < Chef::Resource::Package resource_name :gem_package + # the source can either be a path to a package source like: + # source /var/tmp/mygem-1.2.3.4.gem + # or it can be a url rubygems source like: + # https://www.rubygems.org + # the default has to be nil in order for the magical wiring up of the name property to + # the source pathname to work correctly. + # + # we don't do coercions here because its all a bit too complicated + # + # FIXME? the array form of installing paths most likely does not work? + # property :source, [ String, Array ] property :clear_sources, [ true, false ], default: false, desired_state: false # Sets a custom gem_binary to run for gem commands. diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb index 53c82f2f70..626a5585a6 100644 --- a/spec/unit/provider/package/rubygems_spec.rb +++ b/spec/unit/provider/package/rubygems_spec.rb @@ -638,7 +638,7 @@ describe Chef::Provider::Package::Rubygems do let(:gem_binary) { "/foo/bar" } it "installs the gem with rubygems.org as an added source" do - expected = "#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=#{source} --source=https://rubygems.org" + expected = "#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=#{source}" expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action |