summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-03-25 11:03:39 -0700
committerGitHub <noreply@github.com>2021-03-25 11:03:39 -0700
commitf0ea1cd32cc0960d389a1e79c4700e577d0828f1 (patch)
treeeac465af559224829c08ac0bb3440b0f3b03db13
parent996bae1260d730415424eb13d74ee67f1e0b89ef (diff)
parent9fec14a0a4a947509f38103be15f95c371b53f63 (diff)
downloadchef-f0ea1cd32cc0960d389a1e79c4700e577d0828f1.tar.gz
Merge pull request #11245 from chef/15_backport
Backport several fixes and improvements for Chef 16
-rw-r--r--lib/chef/provider/package/freebsd/pkgng.rb4
-rw-r--r--lib/chef/shell.rb33
-rw-r--r--lib/chef/util/dsc/configuration_generator.rb2
-rw-r--r--spec/unit/provider/package/freebsd/pkgng_spec.rb2
4 files changed, 37 insertions, 4 deletions
diff --git a/lib/chef/provider/package/freebsd/pkgng.rb b/lib/chef/provider/package/freebsd/pkgng.rb
index 48fc7a0dd5..f7f1c39c15 100644
--- a/lib/chef/provider/package/freebsd/pkgng.rb
+++ b/lib/chef/provider/package/freebsd/pkgng.rb
@@ -42,7 +42,9 @@ class Chef
end
def current_installed_version
- pkg_info = shell_out!("pkg", "info", new_resource.package_name, env: nil, returns: [0, 70])
+ # pkgng up to version 1.15.99.7 returns 70 for pkg not found,
+ # later versions return 1
+ pkg_info = shell_out!("pkg", "info", new_resource.package_name, env: nil, returns: [0, 1, 70])
pkg_info.stdout[/^Version +: (.+)$/, 1]
end
diff --git a/lib/chef/shell.rb b/lib/chef/shell.rb
index 20b6cf5508..7cb34aa5b9 100644
--- a/lib/chef/shell.rb
+++ b/lib/chef/shell.rb
@@ -19,6 +19,7 @@ require "singleton" unless defined?(Singleton)
require "pp" unless defined?(PP)
require "etc" unless defined?(Etc)
require "mixlib/cli" unless defined?(Mixlib::CLI)
+require "chef-config/mixin/dot_d"
require_relative "../chef"
require_relative "version"
@@ -199,6 +200,7 @@ module Shell
class Options
include Mixlib::CLI
+ include ChefConfig::Mixin::DotD
def self.footer(text = nil)
@footer = text if text
@@ -314,15 +316,44 @@ module Shell
# We have to nuke ARGV to make sure irb's option parser never sees it.
# otherwise, IRB complains about command line switches it doesn't recognize.
ARGV.clear
+
+ # This code should not exist.
+ # We should be using Application::Client and then calling load_config_file
+ # which does all this properly. However this will do for now.
config[:config_file] = config_file_for_shell_mode(environment)
config_msg = config[:config_file] || "none (standalone session)"
puts "loading configuration: #{config_msg}"
- Chef::Config.from_file(config[:config_file]) if !config[:config_file].nil? && File.exist?(config[:config_file]) && File.readable?(config[:config_file])
+
+ # load the config (if we have one)
+ if !config[:config_file].nil?
+ if File.exist?(config[:config_file]) && File.readable?(config[:config_file])
+ Chef::Config.from_file(config[:config_file])
+ end
+
+ # even if we couldn't load that, we need to tell Chef::Config what
+ # the file was so it sets confdir and d_dir and such properly
+ Chef::Config[:config_file] = config[:config_file]
+
+ # now attempt to load any relevant dot-dirs
+ load_dot_d(Chef::Config[:client_d_dir]) if Chef::Config[:client_d_dir]
+ end
+
+ # finally merge command-line options in
Chef::Config.merge!(config)
end
private
+ # shamelessly lifted from application.rb
+ def apply_config(config_content, config_file_path)
+ Chef::Config.from_string(config_content, config_file_path)
+ rescue Exception => error
+ logger.fatal("Configuration error #{error.class}: #{error.message}")
+ filtered_trace = error.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
+ filtered_trace.each { |line| logger.fatal(" " + line ) }
+ raise Chef::Exceptions::ConfigurationError.new("Aborting due to error in '#{config_file_path}': #{error}")
+ end
+
def config_file_for_shell_mode(environment)
dot_chef_dir = Chef::Util::PathHelper.home(".chef")
if config[:config_file]
diff --git a/lib/chef/util/dsc/configuration_generator.rb b/lib/chef/util/dsc/configuration_generator.rb
index 479e96dfa2..dfb7cab3a4 100644
--- a/lib/chef/util/dsc/configuration_generator.rb
+++ b/lib/chef/util/dsc/configuration_generator.rb
@@ -64,7 +64,7 @@ class Chef::Util::DSC
# The name may not be null or empty, and should start with a letter.
def validate_configuration_name!(configuration_name)
if !!(configuration_name =~ /\A[A-Za-z]+[_a-zA-Z0-9]*\Z/) == false
- raise ArgumentError, 'Configuration `#{configuration_name}` is not a valid PowerShell cmdlet name'
+ raise ArgumentError, "Configuration `#{configuration_name}` is not a valid PowerShell cmdlet name"
end
end
diff --git a/spec/unit/provider/package/freebsd/pkgng_spec.rb b/spec/unit/provider/package/freebsd/pkgng_spec.rb
index bc78a178eb..ce5d58b7dc 100644
--- a/spec/unit/provider/package/freebsd/pkgng_spec.rb
+++ b/spec/unit/provider/package/freebsd/pkgng_spec.rb
@@ -66,7 +66,7 @@ describe Chef::Provider::Package::Freebsd::Port do
end
it "should query pkg database" do
- expect(@provider).to receive(:shell_out_compacted!).with("pkg", "info", "zsh", env: nil, returns: [0, 70], timeout: 900).and_return(@pkg_info)
+ expect(@provider).to receive(:shell_out_compacted!).with("pkg", "info", "zsh", env: nil, returns: [0, 1, 70], timeout: 900).and_return(@pkg_info)
expect(@provider.current_installed_version).to eq("3.1.7")
end
end