diff options
author | danielsdeleo <dan@opscode.com> | 2013-10-16 17:52:06 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2013-10-17 14:03:53 -0700 |
commit | f732bbb1e264b6ccc4fc2c1e9b99a8b9838d2199 (patch) | |
tree | 488f4e589997e34ce73c33bf25de52d309b1ff24 | |
parent | c1bbc0df02f618f907833f7efe686453e732c61e (diff) | |
download | chef-f732bbb1e264b6ccc4fc2c1e9b99a8b9838d2199.tar.gz |
Add a root config_dir config option
Also, refactor path joins in Config to a method that will support
components containing a trailing separator or not.
-rw-r--r-- | lib/chef/config.rb | 48 | ||||
-rw-r--r-- | spec/unit/config_spec.rb | 64 |
2 files changed, 100 insertions, 12 deletions
diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 0442dcd13e..87a747f3cc 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -57,16 +57,32 @@ class Chef configuration.inspect end + def self.on_windows? + RUBY_PLATFORM =~ /mswin|mingw|windows/ + end + + BACKSLASH = '\\'.freeze + def self.platform_path_separator - if RUBY_PLATFORM =~ /mswin|mingw|windows/ - File::ALT_SEPARATOR || '\\' + if on_windows? + File::ALT_SEPARATOR || BACKSLASH else File::SEPARATOR end end + def self.path_join(*args) + args = args.flatten + args.inject do |joined_path, component| + unless joined_path[-1] == platform_path_separator + joined_path += platform_path_separator + end + joined_path += component + end + end + def self.platform_specific_path(path) - if RUBY_PLATFORM =~ /mswin|mingw|windows/ + if on_windows? # turns /etc/chef/client.rb into C:/chef/client.rb system_drive = ENV['SYSTEMDRIVE'] ? ENV['SYSTEMDRIVE'] : "" path = File.join(system_drive, path.split('/')[2..-1]) @@ -83,6 +99,14 @@ class Chef # Config file to load (client.rb, knife.rb, etc. defaults set differently in knife, chef-client, etc.) configurable(:config_file) + default(:config_dir) do + if local_mode + path_join(user_home, ".chef#{platform_path_separator}") + else + ::File.dirname(config_file) + end + end + # No config file (client.rb / knife.rb / etc.) will be loaded outside this path. # Major use case is tests, where we don't want to load the user's config files. configurable(:config_file_jail) @@ -145,7 +169,7 @@ class Chef # 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?("#{path}#{platform_path_separator}cookbooks") + until File.directory?(path_join(path, "cookbooks")) 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}.") @@ -159,9 +183,9 @@ class Chef def self.derive_path_from_chef_repo_path(child_path) if chef_repo_path.kind_of?(String) - "#{chef_repo_path}#{platform_path_separator}#{child_path}" + path_join(chef_repo_path, child_path) else - chef_repo_path.map { |path| "#{path}#{platform_path_separator}#{child_path}"} + chef_repo_path.map { |path| path_join(path, child_path)} end end @@ -233,27 +257,27 @@ class Chef # this is under the user's home directory. default(:cache_path) do if local_mode - "#{user_home}#{platform_path_separator}.chef#{platform_path_separator}local-mode-cache" + "#{config_dir}local-mode-cache" else platform_specific_path("/var/chef") end end # Where cookbook files are stored on the server (by content checksum) - default(:checksum_path) { "#{cache_path}#{platform_path_separator}checksums" } + default(:checksum_path) { path_join(cache_path, "checksums") } # Where chef's cache files should be stored - default(:file_cache_path) { "#{cache_path}#{platform_path_separator}cache" } + default(:file_cache_path) { path_join(cache_path, "cache") } # Where backups of chef-managed files should go - default(:file_backup_path) { "#{cache_path}#{platform_path_separator}backup" } + default(:file_backup_path) { path_join(cache_path, "backup") } # The chef-client (or solo) lockfile. # # If your `file_cache_path` resides on a NFS (or non-flock()-supporting # fs), it's recommended to set this to something like # '/tmp/chef-client-running.pid' - default(:lockfile) { "#{file_cache_path}#{platform_path_separator}chef-client-running.pid" } + default(:lockfile) { path_join(file_cache_path, "chef-client-running.pid") } ## Daemonization Settings ## # What user should Chef run as? @@ -424,7 +448,7 @@ class Chef default(:syntax_check_cache_path) { cache_options[:path] } # Deprecated: - default(:cache_options) { { :path => "#{file_cache_path}#{platform_path_separator}checksums" } } + default(:cache_options) { { :path => path_join(file_cache_path, "checksums") } } # Set to false to silence Chef 11 deprecation warnings: default :chef11_deprecation_warnings, true diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 1b4241b7c8..cb48ab5bcc 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -202,6 +202,70 @@ describe Chef::Config do Chef::Config[:environment_path].should == environment_path end + + describe "joining platform specific paths" do + + context "on UNIX" do + before do + Chef::Config.stub(:on_windows?).and_return(false) + end + + it "joins components when some end with separators" do + Chef::Config.path_join("/foo/", "bar", "baz").should == "/foo/bar/baz" + end + + it "joins components that don't end in separators" do + Chef::Config.path_join("/foo", "bar", "baz").should == "/foo/bar/baz" + end + + end + + context "on Windows" do + before do + Chef::Config.stub(:on_windows?).and_return(true) + end + + it "joins components with the windows separator" do + Chef::Config.path_join('c:\\foo\\', 'bar', "baz").should == 'c:\\foo\\bar\\baz' + end + end + end + + describe "setting the config dir" do + + before do + Chef::Config.stub(:on_windows?).and_return(false) + Chef::Config.config_file = "/etc/chef/client.rb" + end + + context "by default" do + it "is the parent dir of the config file" do + Chef::Config.config_dir.should == "/etc/chef" + end + end + + context "when chef is running in local mode" do + before do + Chef::Config.local_mode = true + Chef::Config.user_home = "/home/charlie" + end + + it "is in the user's home dir" do + Chef::Config.config_dir.should == "/home/charlie/.chef/" + end + end + + context "when explicitly set" do + before do + Chef::Config.config_dir = "/other/config/dir/" + end + + it "uses the explicit value" do + Chef::Config.config_dir.should == "/other/config/dir/" + end + end + + end end describe "Chef::Config[:user_home]" do |