summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2015-03-31 14:33:19 -0700
committerdanielsdeleo <dan@getchef.com>2015-04-01 13:35:01 -0700
commit8157fdea21c913dfb99ee4f8c285d2d64557d4bc (patch)
tree3d4d3998276f92411cc61cac8135befd3de714e2
parentf066fef19be99ba1b857ecaaabc4c70c9201f1d4 (diff)
downloadchef-8157fdea21c913dfb99ee4f8c285d2d64557d4bc.tar.gz
Add --no-listen option to disable zero binding to port
-rw-r--r--lib/chef/application/client.rb5
-rw-r--r--lib/chef/application/knife.rb6
-rw-r--r--lib/chef/config.rb8
-rw-r--r--lib/chef/http.rb1
-rw-r--r--lib/chef/knife.rb3
-rw-r--r--lib/chef/local_mode.rb6
-rw-r--r--spec/integration/client/client_spec.rb11
-rw-r--r--spec/unit/application/client_spec.rb10
-rw-r--r--spec/unit/knife_spec.rb5
9 files changed, 53 insertions, 2 deletions
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index 9984ad5b9d..a5faee9d35 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -258,6 +258,11 @@ class Chef::Application::Client < Chef::Application
:description => "Only run the bare minimum ohai plugins chef needs to function",
:boolean => true
+ option :listen,
+ :long => "--[no-]listen",
+ :description => "Whether a local mode (-z) server binds to a port",
+ :boolean => true
+
IMMEDIATE_RUN_SIGNAL = "1".freeze
attr_reader :chef_client_json
diff --git a/lib/chef/application/knife.rb b/lib/chef/application/knife.rb
index 1a19a45598..af5216ae00 100644
--- a/lib/chef/application/knife.rb
+++ b/lib/chef/application/knife.rb
@@ -121,6 +121,11 @@ class Chef::Application::Knife < Chef::Application
:long => "--chef-zero-port PORT",
:description => "Port (or port range) to start chef-zero on. Port ranges like 1000,1010 or 8889-9999 will try all given ports until one works."
+ option :listen,
+ :long => "--[no-]listen",
+ :description => "Whether a local mode (-z) server binds to a port",
+ :boolean => true
+
option :version,
:short => "-v",
:long => "--version",
@@ -129,7 +134,6 @@ class Chef::Application::Knife < Chef::Application
:proc => lambda {|v| puts "Chef: #{::Chef::VERSION}"},
:exit => 0
-
# Run knife
def run
Mixlib::Log::Formatter.show_time = false
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index b897f9fdbd..25557b077f 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -305,6 +305,14 @@ class Chef
default :pid_file, nil
+ # Whether Chef Zero local mode should bind to a port. All internal requests
+ # will go through the socketless code path regardless, so the socket is
+ # only needed if other processes will connect to the local mode server.
+ #
+ # For compatibility this is set to true but it will be changed to false in
+ # the future.
+ default :listen, true
+
config_context :chef_zero do
config_strict_mode true
default(:enabled) { Chef::Config.local_mode }
diff --git a/lib/chef/http.rb b/lib/chef/http.rb
index bfdc2b5e42..16a826a3db 100644
--- a/lib/chef/http.rb
+++ b/lib/chef/http.rb
@@ -25,6 +25,7 @@ require 'tempfile'
require 'net/https'
require 'uri'
require 'chef/http/basic_client'
+require 'chef/http/socketless_chef_zero_client'
require 'chef/monkey_patches/net_http'
require 'chef/config'
require 'chef/platform/query_helpers'
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index e13f80b5a8..2e0694aebc 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -373,6 +373,9 @@ class Chef
Chef::Config[:environment] = config[:environment] if config[:environment]
Chef::Config.local_mode = config[:local_mode] if config.has_key?(:local_mode)
+
+ Chef::Config.listen = config[:listen] if config.has_key?(:listen)
+
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
diff --git a/lib/chef/local_mode.rb b/lib/chef/local_mode.rb
index c6c5685e3f..79fb750dd8 100644
--- a/lib/chef/local_mode.rb
+++ b/lib/chef/local_mode.rb
@@ -65,7 +65,11 @@ class Chef
server_options[:port] = parse_port(Chef::Config.chef_zero.port)
@chef_zero_server = ChefZero::Server.new(server_options)
- @chef_zero_server.start_background
+ if Chef::Config[:listen]
+ @chef_zero_server.start_background
+ else
+ @chef_zero_server.start_socketless
+ end
local_mode_url = @chef_zero_server.local_mode_url
diff --git a/spec/integration/client/client_spec.rb b/spec/integration/client/client_spec.rb
index aa9aefe1a7..b5c5e12781 100644
--- a/spec/integration/client/client_spec.rb
+++ b/spec/integration/client/client_spec.rb
@@ -60,6 +60,17 @@ EOM
result.error!
end
+ it "should complete successfully with --no-listen" do
+ file 'config/client.rb', <<EOM
+local_mode true
+cookbook_path "#{path_to('cookbooks')}"
+EOM
+
+ result = shell_out("#{chef_client} --no-listen -c \"#{path_to('config/client.rb')}\" -o 'x::default'", :cwd => chef_dir)
+ result.error!
+ end
+
+
context 'and no config file' do
it 'should complete with success when cwd is just above cookbooks and paths are not specified' do
result = shell_out("#{chef_client} -z -o 'x::default' --disable-config", :cwd => path_to(''))
diff --git a/spec/unit/application/client_spec.rb b/spec/unit/application/client_spec.rb
index 501251b7fe..c753ca0ab8 100644
--- a/spec/unit/application/client_spec.rb
+++ b/spec/unit/application/client_spec.rb
@@ -131,6 +131,16 @@ Enable chef-client interval runs by setting `:client_fork = true` in your config
end
+ describe "when --no-listen is set" do
+
+ it "configures listen = false" do
+ app.config[:listen] = false
+ app.reconfigure
+ expect(Chef::Config[:listen]).to eq(false)
+ end
+
+ end
+
describe "when the json_attribs configuration option is specified" do
let(:json_attribs) { {"a" => "b"} }
diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb
index 2ccf8493ad..b748232081 100644
--- a/spec/unit/knife_spec.rb
+++ b/spec/unit/knife_spec.rb
@@ -271,6 +271,11 @@ describe Chef::Knife do
expect(knife_command.config[:opt_with_default]).to eq("from-cli")
end
+ it "merges `listen` config to Chef::Config" do
+ Chef::Knife.run(%w[test yourself --no-listen], Chef::Application::Knife.options)
+ expect(Chef::Config[:listen]).to be(false)
+ end
+
context "verbosity is greater than zero" do
let(:fake_config) { "/does/not/exist/knife.rb" }