summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-06-30 10:33:48 -0700
committerJohn Keiser <jkeiser@opscode.com>2014-07-07 10:04:59 -0700
commitc5956378e9035af7f05e1be1c7741b50de7eb84c (patch)
tree33feaf826e9dd3126b7515d5b4a7b9cd339dc1c5
parentb536215ece2183584188c3a304c2322de2f2a4ff (diff)
downloadchef-c5956378e9035af7f05e1be1c7741b50de7eb84c.tar.gz
Support --chef-zero-port=A-B,C,D-E syntax for finding ports
-rw-r--r--chef.gemspec2
-rw-r--r--lib/chef/application.rb25
-rw-r--r--lib/chef/application/client.rb2
-rw-r--r--lib/chef/application/knife.rb2
-rw-r--r--lib/chef/config.rb2
-rw-r--r--spec/integration/knife/common_options_spec.rb51
-rw-r--r--spec/integration/knife/serve_spec.rb2
7 files changed, 78 insertions, 8 deletions
diff --git a/chef.gemspec b/chef.gemspec
index d3e497264b..15d51ec4ac 100644
--- a/chef.gemspec
+++ b/chef.gemspec
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
s.add_dependency "erubis", "~> 2.7"
s.add_dependency "diff-lcs", "~> 1.2", ">= 1.2.4"
- s.add_dependency "chef-zero", "~> 2.1", ">= 2.1.4"
+ s.add_dependency "chef-zero", "~> 2.2"
s.add_dependency "pry", "~> 0.9"
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index ca610480d0..c77fcd5ee7 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -199,10 +199,11 @@ class Chef::Application
server_options[:data_store] = data_store
server_options[:log_level] = Chef::Log.level
server_options[:host] = Chef::Config.chef_zero.host
- server_options[:port] = Chef::Config.chef_zero.port
+ server_options[:port] = Chef::Application.parse_port(Chef::Config.chef_zero.port)
Chef::Log.info("Starting chef-zero on host #{Chef::Config.chef_zero.host}, port #{Chef::Config.chef_zero.port} with repository at #{chef_fs.fs_description}")
@chef_zero_server = ChefZero::Server.new(server_options)
@chef_zero_server.start_background
+ Chef::Log.info("chef-zero started at #{@chef_zero_server.url}")
Chef::Config.chef_server_url = @chef_zero_server.url
end
end
@@ -240,6 +241,28 @@ class Chef::Application
Chef::Application.destroy_server_connectivity
end
+ def self.parse_port(port)
+ if port.is_a?(String)
+ parts = port.split(',')
+ if parts.size == 1
+ a,b = parts[0].split('-',2)
+ if b
+ a.to_i.upto(b.to_i)
+ else
+ [ a.to_i ]
+ end
+ else
+ array = []
+ parts.each do |part|
+ array += parse_port(part).to_a
+ end
+ array
+ end
+ else
+ port
+ end
+ end
+
private
def apply_config(config_content, config_file_path)
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index e9973a1131..c581bb0da0 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -217,7 +217,7 @@ class Chef::Application::Client < Chef::Application
option :chef_zero_port,
:long => "--chef-zero-port PORT",
- :description => "Port to start chef-zero on"
+ :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 :config_file_jail,
:long => "--config-file-jail PATH",
diff --git a/lib/chef/application/knife.rb b/lib/chef/application/knife.rb
index e838e44632..d3e2f55757 100644
--- a/lib/chef/application/knife.rb
+++ b/lib/chef/application/knife.rb
@@ -120,7 +120,7 @@ class Chef::Application::Knife < Chef::Application
option :chef_zero_port,
:long => "--chef-zero-port PORT",
- :description => "Port to start chef-zero on"
+ :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 :version,
:short => "-v",
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 0ac82cc5ac..65952b8cf7 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -325,7 +325,7 @@ class Chef
config_strict_mode true
default(:enabled) { Chef::Config.local_mode }
default :host, 'localhost'
- default :port, 8889
+ default :port, 8889.upto(9999) # Will try ports from 8889-9999 until one works
end
default :chef_server_url, "https://localhost:443"
diff --git a/spec/integration/knife/common_options_spec.rb b/spec/integration/knife/common_options_spec.rb
index c9b16d50c0..02a7932074 100644
--- a/spec/integration/knife/common_options_spec.rb
+++ b/spec/integration/knife/common_options_spec.rb
@@ -52,10 +52,9 @@ describe 'knife common options' do
context 'And chef_zero.host is 0.0.0.0' do
before(:each) { Chef::Config.chef_zero.host = '0.0.0.0' }
-
+
it 'knife raw /nodes/x should retrieve the role' do
knife('raw /nodes/x').should_succeed /"name": "x"/
- Chef::Config.chef_server_url.should == 'http://0.0.0.0:8889'
end
end
@@ -108,5 +107,53 @@ EOM
knife('raw -z --chef-zero-port=9999 /nodes/x').should_succeed /"name": "x"/
Chef::Config.chef_server_url.should == 'http://localhost:9999'
end
+
+ context 'when the default port (8889) is already bound' do
+ before :each do
+ begin
+ @server = ChefZero::Server.new(:host => 'localhost', :port => 8889)
+ @server.start_background
+ rescue Errno::EADDRINUSE
+ # OK. Don't care who has it in use, as long as *someone* does.
+ end
+ end
+ after :each do
+ @server.stop if @server
+ end
+
+ it 'knife raw -z /nodes/x retrieves the node' do
+ knife('raw -z /nodes/x').should_succeed /"name": "x"/
+ expect(URI(Chef::Config.chef_server_url).port).to be > 8889
+ end
+ end
+
+ context 'when port 9999 is already bound' do
+ before :each do
+ begin
+ @server = ChefZero::Server.new(:host => 'localhost', :port => 9999)
+ @server.start_background
+ rescue Errno::EADDRINUSE
+ # OK. Don't care who has it in use, as long as *someone* does.
+ end
+ end
+ after :each do
+ @server.stop if @server
+ end
+
+ it 'knife raw -z --chef-zero-port=9999-20000 /nodes/x' do
+ knife('raw -z --chef-zero-port=9999-20000 /nodes/x').should_succeed /"name": "x"/
+ expect(URI(Chef::Config.chef_server_url).port).to be > 9999
+ end
+
+ it 'knife raw -z --chef-zero-port=9999-9999,19423' do
+ knife('raw -z --chef-zero-port=9999-9999,19423 /nodes/x').should_succeed /"name": "x"/
+ expect(URI(Chef::Config.chef_server_url).port).to be == 19423
+ end
+ end
+
+ it 'knife raw -z --chef-zero-port=9999 /nodes/x retrieves the node' do
+ knife('raw -z --chef-zero-port=9999 /nodes/x').should_succeed /"name": "x"/
+ Chef::Config.chef_server_url.should == 'http://localhost:9999'
+ end
end
end
diff --git a/spec/integration/knife/serve_spec.rb b/spec/integration/knife/serve_spec.rb
index 6f8d046518..20e4e43586 100644
--- a/spec/integration/knife/serve_spec.rb
+++ b/spec/integration/knife/serve_spec.rb
@@ -31,7 +31,7 @@ describe 'knife serve' do
exception = nil
t = Thread.new do
begin
- knife('serve')
+ knife('serve --chef-zero-port=8889')
rescue
exception = $!
end