summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-07-03 20:20:03 -0700
committerJohn Keiser <jkeiser@opscode.com>2014-07-03 20:20:03 -0700
commit9ec6b92b9eefc07b35e6d36bad3568cfd7bf82d4 (patch)
treeba7e99fbafa1bc45b5908df75453c0c4dcb61494
parent8def9a480e8bbb9a4182fa1cacc1f248403052d2 (diff)
downloadchef-jk/lotsa_ports.tar.gz
Refactor caller decorations into with_server_connectivityjk/lotsa_ports
-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