summaryrefslogtreecommitdiff
path: root/lib/chef/knife/core
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/knife/core')
-rw-r--r--lib/chef/knife/core/bootstrap_context.rb17
-rw-r--r--lib/chef/knife/core/generic_presenter.rb7
-rw-r--r--lib/chef/knife/core/object_loader.rb2
-rw-r--r--lib/chef/knife/core/subcommand_loader.rb24
-rw-r--r--lib/chef/knife/core/ui.rb10
5 files changed, 53 insertions, 7 deletions
diff --git a/lib/chef/knife/core/bootstrap_context.rb b/lib/chef/knife/core/bootstrap_context.rb
index 3833182c70..7197653489 100644
--- a/lib/chef/knife/core/bootstrap_context.rb
+++ b/lib/chef/knife/core/bootstrap_context.rb
@@ -23,13 +23,15 @@ class Chef
class Knife
module Core
# Instances of BootstrapContext are the context objects (i.e., +self+) for
- # bootstrap templates. For backwards compatability, they +must+ set the
+ # bootstrap templates. For backwards compatibility, they +must+ set the
# following instance variables:
# * @config - a hash of knife's config values
# * @run_list - the run list for the node to boostrap
#
class BootstrapContext
+ attr_accessor :client_pem
+
def initialize(config, run_list, chef_config, secret = nil)
@config = config
@run_list = run_list
@@ -42,13 +44,19 @@ class Chef
end
def validation_key
- IO.read(File.expand_path(@chef_config[:validation_key]))
+ if File.exist?(File.expand_path(@chef_config[:validation_key]))
+ IO.read(File.expand_path(@chef_config[:validation_key]))
+ else
+ false
+ end
end
def encrypted_data_bag_secret
@secret
end
+ # Contains commands and content, see trusted_certs_content
+ # TODO: Rename to trusted_certs_script
def trusted_certs
@trusted_certs ||= trusted_certs_content
end
@@ -135,7 +143,7 @@ CONFIG
def latest_current_chef_version_string
installer_version_string = nil
if @config[:prerelease]
- installer_version_string = "-p"
+ installer_version_string = ["-p"]
else
chef_version_string = if knife_config[:bootstrap_version]
knife_config[:bootstrap_version]
@@ -159,6 +167,9 @@ CONFIG
end
private
+
+ # Returns a string for copying the trusted certificates on the workstation to the system being bootstrapped
+ # This string should contain both the commands necessary to both create the files, as well as their content
def trusted_certs_content
content = ""
if @chef_config[:trusted_certs_dir]
diff --git a/lib/chef/knife/core/generic_presenter.rb b/lib/chef/knife/core/generic_presenter.rb
index a1aeadb0f3..f3ea0f0d6c 100644
--- a/lib/chef/knife/core/generic_presenter.rb
+++ b/lib/chef/knife/core/generic_presenter.rb
@@ -178,10 +178,13 @@ class Chef
nested_value_spec.split(".").each do |attr|
if data.nil?
nil # don't get no method error on nil
- elsif data.respond_to?(attr.to_sym)
- data = data.send(attr.to_sym)
+ # Must check :[] before attr because spec can include
+ # `keys` - want the key named `keys`, not a list of
+ # available keys.
elsif data.respond_to?(:[])
data = data[attr]
+ elsif data.respond_to?(attr.to_sym)
+ data = data.send(attr.to_sym)
else
data = begin
data.send(attr.to_sym)
diff --git a/lib/chef/knife/core/object_loader.rb b/lib/chef/knife/core/object_loader.rb
index 209f6987d4..698b09ac84 100644
--- a/lib/chef/knife/core/object_loader.rb
+++ b/lib/chef/knife/core/object_loader.rb
@@ -105,7 +105,7 @@ class Chef
end
def file_exists_and_is_readable?(file)
- File.exists?(file) && File.readable?(file)
+ File.exist?(file) && File.readable?(file)
end
end
diff --git a/lib/chef/knife/core/subcommand_loader.rb b/lib/chef/knife/core/subcommand_loader.rb
index d2be1be2d3..f9b8f5008e 100644
--- a/lib/chef/knife/core/subcommand_loader.rb
+++ b/lib/chef/knife/core/subcommand_loader.rb
@@ -22,6 +22,9 @@ class Chef
class Knife
class SubcommandLoader
+ MATCHES_CHEF_GEM = %r{/chef-[\d]+\.[\d]+\.[\d]+}.freeze
+ MATCHES_THIS_CHEF_GEM = %r{/chef-#{Chef::VERSION}/}.freeze
+
attr_reader :chef_config_dir
attr_reader :env
@@ -122,6 +125,14 @@ class Chef
subcommand_files = {}
files.each do |file|
rel_path = file[/(#{Regexp.escape File.join('chef', 'knife', '')}.*)\.rb/, 1]
+
+ # When not installed as a gem (ChefDK/appbundler in particular), AND
+ # a different version of Chef is installed via gems, `files` will
+ # include some files from the 'other' Chef install. If this contains
+ # a knife command that doesn't exist in this version of Chef, we will
+ # get a LoadError later when we try to require it.
+ next if from_different_chef_version?(file)
+
subcommand_files[rel_path] = file
end
@@ -185,6 +196,19 @@ class Chef
Dir[glob].map { |f| f.untaint }
end
+
+ def from_different_chef_version?(path)
+ matches_any_chef_gem?(path) && !matches_this_chef_gem?(path)
+ end
+
+ def matches_any_chef_gem?(path)
+ path =~ MATCHES_CHEF_GEM
+ end
+
+ def matches_this_chef_gem?(path)
+ path =~ MATCHES_THIS_CHEF_GEM
+ end
+
end
end
end
diff --git a/lib/chef/knife/core/ui.rb b/lib/chef/knife/core/ui.rb
index 00a4834638..a54f14afc1 100644
--- a/lib/chef/knife/core/ui.rb
+++ b/lib/chef/knife/core/ui.rb
@@ -163,9 +163,17 @@ class Chef
end
end
+
+ # Hash -> Hash
+ # Works the same as edit_data but
+ # returns a hash rather than a JSON string/Fully infated object
+ def edit_hash(hash)
+ raw = edit_data(hash, false)
+ Chef::JSONCompat.parse(raw)
+ end
+
def edit_data(data, parse_output=true)
output = Chef::JSONCompat.to_json_pretty(data)
-
if (!config[:disable_editing])
Tempfile.open([ 'knife-edit-', '.json' ]) do |tf|
tf.sync = true