summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-09-02 22:18:29 -0700
committerJohn Keiser <john@johnkeiser.com>2015-01-08 21:05:55 -0800
commit2a71bd0c71d8ccbe28369f28e3d6c5ced78dbe4b (patch)
tree18438952a39fe3d3cdceab5967decb992de7fffd
parenta6ed1e9733ac524990b22121bee6957f93c4984e (diff)
downloadchef-2a71bd0c71d8ccbe28369f28e3d6c5ced78dbe4b.tar.gz
Infer chef_repo_path from clients/ as well as cookbooks/
-rw-r--r--lib/chef/application/client.rb5
-rw-r--r--lib/chef/config.rb24
-rw-r--r--lib/chef/knife.rb2
-rw-r--r--spec/integration/client/client_spec.rb2
-rw-r--r--spec/integration/knife/chef_repo_path_spec.rb12
-rw-r--r--spec/support/shared/integration/knife_support.rb8
6 files changed, 38 insertions, 15 deletions
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index 17b5cea91b..237c1b1cab 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -258,7 +258,10 @@ class Chef::Application::Client < Chef::Application
Chef::Config.local_mode = config[:local_mode] if config.has_key?(:local_mode)
if Chef::Config.local_mode && !Chef::Config.has_key?(:cookbook_path) && !Chef::Config.has_key?(:chef_repo_path)
- Chef::Config.chef_repo_path = Chef::Config.find_chef_repo_path(Dir.pwd)
+ assumed_cwd = Chef::Config.find_chef_repo_path(Dir.pwd)
+ if assumed_cwd
+ Chef::Log.warn("chef-client will create a #{Chef::Config.client_path} and #{Chef::Config.node_path} directory, and you will not see this warning again.")
+ end
end
Chef::Config.chef_zero.host = config[:chef_zero_host] if config[:chef_zero_host]
Chef::Config.chef_zero.port = config[:chef_zero_port] if config[:chef_zero_port]
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 32c9a15eb5..a9d5e0d76c 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -128,19 +128,35 @@ class Chef
end
def self.find_chef_repo_path(cwd)
+ # If there is a config_file_jail, don't auto-set chef_repo_path outside of it
+ if Chef::Config.config_file_jail
+ begin
+ real_config_file_jail = Pathname.new(Chef::Config.config_file_jail).realpath.to_s
+ rescue Errno::ENOENT
+ return false
+ end
+
+ real_cwd = Pathname.new(cwd).realpath.to_s
+ if !Chef::ChefFS::PathUtils.descendant_of?(real_cwd, real_config_file_jail)
+ return false
+ end
+ end
+
# In local mode, we auto-discover the repo root by looking for a path with "cookbooks" under it.
# This allows us to run config-free.
path = cwd
- until File.directory?(PathHelper.join(path, "cookbooks"))
+ until File.directory?(PathHelper.join(path, "cookbooks")) || File.directory?(PathHelper.join(path, "clients"))
new_path = File.expand_path('..', path)
if new_path == path
- Chef::Log.warn("No cookbooks directory found at or above current directory. Assuming #{Dir.pwd}.")
- return Dir.pwd
+ Chef::Log.warn("No cookbooks or clients directory found at or above current directory. Assuming #{Dir.pwd}.")
+ Chef::Config.chef_repo_path = Dir.pwd
+ return true
end
path = new_path
end
Chef::Log.info("Auto-discovered chef repository at #{path}")
- path
+ Chef::Config.chef_repo_path = path
+ return false
end
def self.derive_path_from_chef_repo_path(child_path)
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index fd34165ef2..6fd52b912d 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -372,7 +372,7 @@ class Chef
Chef::Config.local_mode = config[:local_mode] if config.has_key?(:local_mode)
if Chef::Config.local_mode && !Chef::Config.has_key?(:cookbook_path) && !Chef::Config.has_key?(:chef_repo_path)
- Chef::Config.chef_repo_path = Chef::Config.find_chef_repo_path(Dir.pwd)
+ assumed_cwd = Chef::Config.find_chef_repo_path(Dir.pwd)
end
Chef::Config.chef_zero.host = config[:chef_zero_host] if config[:chef_zero_host]
Chef::Config.chef_zero.port = config[:chef_zero_port] if config[:chef_zero_port]
diff --git a/spec/integration/client/client_spec.rb b/spec/integration/client/client_spec.rb
index 7d7c79fd73..5707fdd4f8 100644
--- a/spec/integration/client/client_spec.rb
+++ b/spec/integration/client/client_spec.rb
@@ -62,7 +62,7 @@ EOM
end
it 'fails to load .chef/knife.rb when --config-file-jail does not include the .chef/knife.rb' do
- result = shell_out("#{chef_client} -o 'x::default' --config-file-jail \"#{path_to('roles')}\"", :cwd => path_to(''))
+ result = shell_out("#{chef_client} --config-file-jail \"#{path_to('roles')}\"", :cwd => path_to(''))
result.error!
end
end
diff --git a/spec/integration/knife/chef_repo_path_spec.rb b/spec/integration/knife/chef_repo_path_spec.rb
index 15aace4612..ed52b03db2 100644
--- a/spec/integration/knife/chef_repo_path_spec.rb
+++ b/spec/integration/knife/chef_repo_path_spec.rb
@@ -27,6 +27,8 @@ describe 'chef_repo_path tests', :workstation do
# TODO alternate repo_path / *_path
context 'alternate *_path' do
when_the_repository 'has clients and clients2, cookbooks and cookbooks2, etc.' do
+ before { Chef::Config.config_file_jail = path_to('') }
+
before do
file 'clients/client1.json', {}
file 'cookbooks/cookbook1/metadata.rb', ''
@@ -841,16 +843,16 @@ EOM
it 'knife list --local -Rfp / lists data bags' do
knife('list --local -Rfp /').should_succeed <<EOM
-data_bags/
-data_bags/bag/
-data_bags/bag/item.json
+/data_bags/
+/data_bags/bag/
+/data_bags/bag/item.json
EOM
end
it 'knife list --local -Rfp /data_bags lists data bags' do
knife('list --local -Rfp /data_bags').should_succeed <<EOM
-data_bags/bag/
-data_bags/bag/item.json
+/data_bags/bag/
+/data_bags/bag/item.json
EOM
end
diff --git a/spec/support/shared/integration/knife_support.rb b/spec/support/shared/integration/knife_support.rb
index 30293be6cf..02627fe9db 100644
--- a/spec/support/shared/integration/knife_support.rb
+++ b/spec/support/shared/integration/knife_support.rb
@@ -61,18 +61,20 @@ module KnifeSupport
# Don't print stuff
Chef::Config[:verbosity] = ( DEBUG ? 2 : 0 )
- instance.config[:config_file] = File.join(CHEF_SPEC_DATA, "null_config.rb")
# Configure chef with a (mostly) blank knife.rb
# We set a global and then mutate it in our stub knife.rb so we can be
# extra sure that we're not loading someone's real knife.rb and then
# running test scenarios against a real chef server. If things don't
# smell right, abort.
+ if !Chef::Config.config_file_jail
+ instance.config[:config_file] = File.join(CHEF_SPEC_DATA, "null_config.rb")
+ $__KNIFE_INTEGRATION_FAILSAFE_CHECK = "ole"
+ end
- $__KNIFE_INTEGRATION_FAILSAFE_CHECK = "ole"
instance.configure_chef
- unless $__KNIFE_INTEGRATION_FAILSAFE_CHECK == "ole ole"
+ unless Chef::Config.config_file_jail || $__KNIFE_INTEGRATION_FAILSAFE_CHECK == "ole ole"
raise Exception, "Potential misconfiguration of integration tests detected. Aborting test."
end