diff options
author | John Keiser <jkeiser@opscode.com> | 2013-10-10 16:33:32 -0700 |
---|---|---|
committer | John Keiser <jkeiser@opscode.com> | 2013-10-10 16:33:32 -0700 |
commit | 36113227dbb3b7f73a10d6fe3dea0e3b45a0eba8 (patch) | |
tree | 4624aa833c49e5cb576abef949f9c6fe1dfa74fc /lib/chef | |
parent | 6e7d667912f0456bbf90e01ac74338f9f18260b5 (diff) | |
parent | 71825e70f91ef9fc309849e3eae31d86065205db (diff) | |
download | chef-36113227dbb3b7f73a10d6fe3dea0e3b45a0eba8.tar.gz |
Add --local-mode / -z parameter to chef-client and knife
This mode makes it so that a normal user can run chef-client with no configuration, and it will pick up cookbooks and other data from the local directory.
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/application.rb | 12 | ||||
-rw-r--r-- | lib/chef/application/client.rb | 24 | ||||
-rw-r--r-- | lib/chef/application/knife.rb | 6 | ||||
-rw-r--r-- | lib/chef/config.rb | 29 | ||||
-rw-r--r-- | lib/chef/knife.rb | 18 |
5 files changed, 67 insertions, 22 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb index 167ea5dfec..ca9cfff291 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -66,9 +66,18 @@ class Chef::Application run_application end - # Parse the configuration file + # Parse configuration (options and config file) def configure_chef parse_options + load_config_file + end + + # Parse the config file + def load_config_file + if !config[:config_file] + Chef::Log.warn("No config file found or specified on command line, not loading.") + return + end begin case config[:config_file] @@ -90,7 +99,6 @@ class Chef::Application rescue Exception => error Chef::Application.fatal!("Unknown error processing config file #{config[:config_file]} with error #{error.message}", 2) end - end # Initialize and configure the logger. diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb index 2b34e87b80..ea8d599b4c 100644 --- a/lib/chef/application/client.rb +++ b/lib/chef/application/client.rb @@ -34,7 +34,6 @@ class Chef::Application::Client < Chef::Application option :config_file, :short => "-c CONFIG", :long => "--config CONFIG", - :default => Chef::Config.platform_specific_path("/etc/chef/client.rb"), :description => "The configuration file to use" option :formatter, @@ -197,10 +196,10 @@ class Chef::Application::Client < Chef::Application :description => "Enable reporting data collection for chef runs", :boolean => true - option :chef_zero_enabled, + option :local_mode, :short => "-z", - :long => "--zero", - :description => "Start a chef-zero instance pointed at local cookbooks", + :long => "--local-mode", + :description => "Point chef-client at local repository", :boolean => true option :chef_zero_port, @@ -229,7 +228,10 @@ class Chef::Application::Client < Chef::Application Chef::Config[:chef_server_url] = config[:chef_server_url] if config.has_key? :chef_server_url - Chef::Config.chef_zero.enabled = true if config[:chef_zero_enabled] + 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) + end Chef::Config.chef_zero.port = config[:chef_zero_port] if config[:chef_zero_port] if Chef::Config[:daemonize] @@ -269,6 +271,18 @@ class Chef::Application::Client < Chef::Application end end + def load_config_file + if !config.has_key?(:config_file) + if config[:local_mode] + require 'chef/knife' + config[:config_file] = Chef::Knife.locate_config_file + else + config[:config_file] = Chef::Config.platform_specific_path("/etc/chef/client.rb") + end + end + super + end + def configure_logging super Mixlib::Authentication::Log.use_log_devices( Chef::Log ) diff --git a/lib/chef/application/knife.rb b/lib/chef/application/knife.rb index a06451e82e..3a9cd4e870 100644 --- a/lib/chef/application/knife.rb +++ b/lib/chef/application/knife.rb @@ -106,10 +106,10 @@ class Chef::Application::Knife < Chef::Application :description => "Which format to use for output", :default => "summary" - option :chef_zero_enabled, + option :local_mode, :short => "-z", - :long => "--zero", - :description => "Start a chef-zero instance pointed at local cookbooks", + :long => "--local-mode", + :description => "Point knife commands at local repository instead of server", :boolean => true option :chef_zero_port, diff --git a/lib/chef/config.rb b/lib/chef/config.rb index dc8e7fae14..0158923da9 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -127,6 +127,22 @@ class Chef end end + def self.find_chef_repo_path(cwd) + # 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") + 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 + end + path = new_path + end + Chef::Log.info("Auto-discovered chef repository at #{path}") + path + end + 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}" @@ -199,14 +215,16 @@ class Chef # An array of paths to search for knife exec scripts if they aren't in the current directory default :script_path, [] + default(:cache_path) { local_mode ? "#{chef_repo_path}#{platform_path_separator}.cache" : platform_specific_path("/var/chef") } + # Where cookbook files are stored on the server (by content checksum) - default :checksum_path, '/var/chef/checksums' + default(:checksum_path) { "#{cache_path}#{platform_path_separator}checksums" } # Where chef's cache files should be stored - default(:file_cache_path) { platform_specific_path('/var/chef/cache') } + default(:file_cache_path) { "#{cache_path}#{platform_path_separator}cache" } # Where backups of chef-managed files should go - default(:file_backup_path) { platform_specific_path('/var/chef/backup') } + default(:file_backup_path) { "#{cache_path}#{platform_path_separator}backup" } # The chef-client (or solo) lockfile. # @@ -252,12 +270,13 @@ class Chef default :diff_disabled, false default :diff_filesize_threshold, 10000000 default :diff_output_threshold, 1000000 + default :local_mode, false default :pid_file, nil config_context :chef_zero do config_strict_mode true - default :enabled, false + default(:enabled) { Chef::Config.local_mode } default :port, 8889 end default :chef_server_url, "https://localhost:443" @@ -371,7 +390,7 @@ class Chef default(:syntax_check_cache_path) { cache_options[:path] } # Deprecated: - default(:cache_options) { { :path => platform_specific_path("/var/chef/cache/checksums") } } + default(:cache_options) { { :path => "#{file_cache_path}#{platform_path_separator}checksums" } } # Set to false to silence Chef 11 deprecation warnings: default :chef11_deprecation_warnings, true diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index 420e3557e8..5ff6b92643 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -314,7 +314,7 @@ class Chef config_file_settings end - def locate_config_file + def self.locate_config_file candidate_configs = [] # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine) @@ -326,8 +326,8 @@ class Chef candidate_configs << File.join(Dir.pwd, 'knife.rb') end # Look for $UPWARD/.chef/knife.rb - if self.class.chef_config_dir - candidate_configs << File.join(self.class.chef_config_dir, 'knife.rb') + if chef_config_dir + candidate_configs << File.join(chef_config_dir, 'knife.rb') end # Look for $HOME/.chef/knife.rb if ENV['HOME'] @@ -337,10 +337,10 @@ class Chef candidate_configs.each do | candidate_config | candidate_config = File.expand_path(candidate_config) if File.exist?(candidate_config) - config[:config_file] = candidate_config - break + return candidate_config end end + return nil end # Apply Config in this order: @@ -379,7 +379,10 @@ class Chef Chef::Config[:chef_server_url] = config[:chef_server_url] if config[:chef_server_url] Chef::Config[:environment] = config[:environment] if config[:environment] - Chef::Config.chef_zero.enabled = true if config[:chef_zero_enabled] + 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) + end Chef::Config.chef_zero.port = config[:chef_zero_port] if config[:chef_zero_port] # Expand a relative path from the config directory. Config from command @@ -401,7 +404,8 @@ class Chef def configure_chef unless config[:config_file] - locate_config_file + located_config_file = self.class.locate_config_file + config[:config_file] = located_config_file if located_config_file end # Don't try to load a knife.rb if it doesn't exist. |