summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom May <thom@chef.io>2017-12-15 15:52:15 +0000
committerThom May <thom@chef.io>2017-12-18 11:26:34 +0000
commit57732ab92cc315c6161fbdef4ee17ceaebffc656 (patch)
tree1b426b0cd5916098da2452b3706a11525bf46922
parentd390c679654a92511f67e0a6fdebd211d5c84426 (diff)
downloadchef-57732ab92cc315c6161fbdef4ee17ceaebffc656.tar.gz
Specify a profile on the command line
Signed-off-by: Thom May <thom@chef.io>
-rw-r--r--chef-config/lib/chef-config/config.rb3
-rw-r--r--chef-config/lib/chef-config/workstation_config_loader.rb8
-rw-r--r--lib/chef/application/knife.rb4
-rw-r--r--lib/chef/knife.rb5
-rw-r--r--spec/unit/knife_spec.rb2
5 files changed, 18 insertions, 4 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb
index 63dd4ecda2..4855533266 100644
--- a/chef-config/lib/chef-config/config.rb
+++ b/chef-config/lib/chef-config/config.rb
@@ -698,6 +698,9 @@ module ChefConfig
# on running chef-client
default :count_log_resource_updates, true
+ # The selected profile when using credentials.
+ default :profile, nil
+
# knife configuration data
config_context :knife do
# XXX: none of these default values are applied to knife (and would create a backcompat
diff --git a/chef-config/lib/chef-config/workstation_config_loader.rb b/chef-config/lib/chef-config/workstation_config_loader.rb
index 4c07cac702..fdde1e0255 100644
--- a/chef-config/lib/chef-config/workstation_config_loader.rb
+++ b/chef-config/lib/chef-config/workstation_config_loader.rb
@@ -31,7 +31,9 @@ module ChefConfig
# Path to a config file requested by user, (e.g., via command line option). Can be nil
attr_accessor :explicit_config_file
- attr_reader :profile
+ # The name of a credentials profile. Can be nil
+ attr_accessor :profile
+ attr_reader :credentials_found
# TODO: initialize this with a logger for Chef and Knife
def initialize(explicit_config_file, logger = nil, profile: nil)
@@ -40,10 +42,11 @@ module ChefConfig
@config_location = nil
@profile = profile
@logger = logger || NullLogger.new
+ @credentials_found = false
end
def no_config_found?
- config_location.nil?
+ config_location.nil? && !credentials_found
end
def config_location
@@ -152,6 +155,7 @@ module ChefConfig
extract_key(creds, "validation_key", :validation_key, :validation_key_contents)
extract_key(creds, "validator_key", :validation_key, :validation_key_contents)
extract_key(creds, "client_key", :client_key, :client_key_contents)
+ @credentials_found = true
end
def extract_key(creds, name, config_path, config_contents)
diff --git a/lib/chef/application/knife.rb b/lib/chef/application/knife.rb
index 6a09427ccd..c972e9313e 100644
--- a/lib/chef/application/knife.rb
+++ b/lib/chef/application/knife.rb
@@ -148,6 +148,10 @@ class Chef::Application::Knife < Chef::Application
:boolean => true,
:default => nil
+ option :profile,
+ :long => "--profile PROFILE",
+ :description => "The credentials profile to select"
+
# Run knife
def run
Mixlib::Log::Formatter.show_time = false
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index aa30f2e2ed..1b9f165e21 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -178,8 +178,9 @@ class Chef
@config_loader ||= WorkstationConfigLoader.new(nil, Chef::Log)
end
- def self.load_config(explicit_config_file)
+ def self.load_config(explicit_config_file, profile)
config_loader.explicit_config_file = explicit_config_file
+ config_loader.profile = profile
config_loader.load
ui.warn("No knife configuration file found") if config_loader.no_config_found?
@@ -404,7 +405,7 @@ class Chef
def configure_chef
# knife needs to send logger output to STDERR by default
Chef::Config[:log_location] = STDERR
- config_loader = self.class.load_config(config[:config_file])
+ config_loader = self.class.load_config(config[:config_file], config[:profile])
config[:config_file] = config_loader.config_location
# For CLI options like `--config-option key=value`. These have to get
diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb
index 2b22dbc4f7..adaab11d55 100644
--- a/spec/unit/knife_spec.rb
+++ b/spec/unit/knife_spec.rb
@@ -47,6 +47,7 @@ describe Chef::Knife do
allow(Chef::WorkstationConfigLoader).to receive(:new).and_return(config_loader)
allow(config_loader).to receive(:explicit_config_file=)
+ allow(config_loader).to receive(:profile=)
# Prevent gratuitous code reloading:
allow(Chef::Knife).to receive(:load_commands)
@@ -331,6 +332,7 @@ describe Chef::Knife do
knife.config[:config_file] = fake_config
config_loader = double("Chef::WorkstationConfigLoader", :load => true, :no_config_found? => false, :chef_config_dir => "/etc/chef", :config_location => fake_config)
allow(config_loader).to receive(:explicit_config_file=).with(fake_config).and_return(fake_config)
+ allow(config_loader).to receive(:profile=)
allow(Chef::WorkstationConfigLoader).to receive(:new).and_return(config_loader)
end