summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/application.rb6
-rw-r--r--lib/chef/knife.rb5
-rw-r--r--lib/chef/local_mode.rb28
3 files changed, 30 insertions, 9 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index b1d28f618c..5b404a3a50 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -187,9 +187,7 @@ class Chef::Application
# Initializes Chef::Client instance and runs it
def run_chef_client(specific_recipes = [])
- Chef::LocalMode.setup_server_connectivity
-
- begin
+ Chef::LocalMode.with_server_connectivity do
override_runlist = config[:override_runlist]
if specific_recipes.size > 0
override_runlist ||= []
@@ -204,8 +202,6 @@ class Chef::Application
@chef_client.run
@chef_client = nil
- ensure
- Chef::LocalMode.destroy_server_connectivity
end
end
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index 4874f28cd6..038ab61715 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -489,11 +489,8 @@ class Chef
ui.error "You need to add a #run method to your knife command before you can use it"
end
enforce_path_sanity
- Chef::LocalMode.setup_server_connectivity
- begin
+ Chef::LocalMode.with_server_connectivity do
run
- ensure
- Chef::LocalMode.destroy_server_connectivity
end
rescue Exception => e
raise if raise_exception || Chef::Config[:verbosity] == 2
diff --git a/lib/chef/local_mode.rb b/lib/chef/local_mode.rb
index eac2174c0b..0b8b66cae2 100644
--- a/lib/chef/local_mode.rb
+++ b/lib/chef/local_mode.rb
@@ -18,6 +18,32 @@ require 'chef/config'
class Chef
module LocalMode
+ # Create a chef local server (if the configuration requires one) for the
+ # duration of the given block.
+ #
+ # # This ...
+ # with_server_connectivity { stuff }
+ #
+ # # Is exactly equivalent to this ...
+ # Chef::LocalMode.setup_server_connectivity
+ # begin
+ # stuff
+ # ensure
+ # Chef::LocalMode.destroy_server_connectivity
+ # end
+ #
+ def self.with_server_connectivity
+ setup_server_connectivity
+ begin
+ yield
+ ensure
+ destroy_server_connectivity
+ end
+ end
+
+ # If Chef::Config.chef_zero.enabled is true, sets up a chef-zero server
+ # according to the Chef::Config.chef_zero and path options, and sets
+ # chef_server_url to point at it.
def self.setup_server_connectivity
if Chef::Config.chef_zero.enabled
destroy_server_connectivity
@@ -42,10 +68,12 @@ class Chef
end
end
+ # Return the current chef-zero server set up by setup_server_connectivity.
def self.chef_zero_server
@chef_zero_server
end
+ # If chef_zero_server is non-nil, stop it and remove references to it.
def self.destroy_server_connectivity
if @chef_zero_server
@chef_zero_server.stop