summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2013-06-13 00:07:16 -0700
committerJohn Keiser <jkeiser@opscode.com>2013-09-12 23:04:21 -0700
commit434b6aeb068db9eddf7605b442a17e0a2fb27bb1 (patch)
tree3bf7e40e7c8dce92dd96e994cf0283a75ad97761 /lib
parent4282b7452a03e147ed8b8dd1b52e7f21245c28c5 (diff)
downloadchef-434b6aeb068db9eddf7605b442a17e0a2fb27bb1.tar.gz
Support chef_server_url 'local' and create knife.rb if none exists
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/chef_fs/knife.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/chef/chef_fs/knife.rb b/lib/chef/chef_fs/knife.rb
index 68bfb70e34..673e2c9ead 100644
--- a/lib/chef/chef_fs/knife.rb
+++ b/lib/chef/chef_fs/knife.rb
@@ -68,6 +68,13 @@ class Chef
@chef_fs_config = Chef::ChefFS::Config.new(Chef::Config)
Chef::ChefFS::Parallelizer.threads = (Chef::Config[:concurrency] || 10) - 1
+
+ if Chef::Config[:chef_server_url].to_sym == :local
+ local_url = start_local_server
+ Chef::Config[:chef_server_url] = local_url
+ Chef::Config[:client_key] = nil
+ Chef::Config[:validation_key] = nil
+ end
end
def chef_fs
@@ -110,6 +117,56 @@ class Chef
def parallelize(inputs, options = {}, &block)
Chef::ChefFS::Parallelizer.parallelize(inputs, options, &block)
end
+
+ def locate_config_file
+ super
+ if !config[:config_file]
+ # If the config file doesn't already exist, find out where it should be,
+ # and create it.
+ repo_dir = discover_repo_dir(Dir.pwd)
+ if repo_dir
+ dot_chef = File.join(repo_dir, ".chef")
+ if !File.directory?(dot_chef)
+ Dir.mkdir(dot_chef)
+ end
+ knife_rb = File.join(dot_chef, "knife.rb")
+ if !File.exist?(knife_rb)
+ ui.warn("No configuration found. Creating .chef/knife.rb in #{repo_dir} ...")
+ File.open(knife_rb, "w") do |file|
+ file.write <<EOM
+ chef_server_url 'local'
+ chef_repo_path File.dirname(File.dirname(__FILE__))
+ cookbook_path File.join(chef_repo_path, "cookbooks")
+EOM
+ end
+ end
+ config[:config_file] = knife_rb
+ end
+ end
+ end
+
+ def discover_repo_dir(dir)
+ %w(.chef cookbooks data_bags environments roles).each do |subdir|
+ return dir if File.directory?(File.join(dir, subdir))
+ end
+ # If this isn't it, check the parent
+ parent = File.dirname(dir)
+ if parent && parent != dir
+ discover_repo_dir(parent)
+ else
+ nil
+ end
+ end
+
+ def start_local_server
+ server_options = {}
+ server_options[:data_store] = ChefFSChefFSDataStore.new(local_fs)
+ server_options[:log_level] = Chef::Log.level
+ server_options[:port] = 8889
+ server = ChefZero::Server.new(server_options)
+ server.start_background
+ server.url
+ end
end
end
end