summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Dibowitz <phild@fb.com>2016-03-18 14:00:48 -0700
committerPhil Dibowitz <phil@ipom.com>2016-03-22 21:52:58 -0700
commitce63bd7c18786dfc180752236ac090a1a5febc83 (patch)
tree5e65892f458cd3c743fc165f7c69fa91ff4fb1cc
parentc67095f4da311feb3d63cfd35cb25ee07e49f4b2 (diff)
downloadchef-zero-ce63bd7c18786dfc180752236ac090a1a5febc83.tar.gz
Enable listening on more than one address
If you listen on :: you get all v6 addrs. If you listen on 0.0.0.0 you get all v4 addrs. There's no way to listen on both. Allow people to pass in multiple `--host`s
-rw-r--r--CHANGELOG.md2
-rwxr-xr-xbin/chef-zero3
-rw-r--r--lib/chef_zero/server.rb42
3 files changed, 26 insertions, 21 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cce1efb..98dda55 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -521,4 +521,4 @@
## [v0.9](https://github.com/chef/chef-zero/tree/v0.9) (2012-12-24)
-\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file
+\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
diff --git a/bin/chef-zero b/bin/chef-zero
index 231d71b..03b6d01 100755
--- a/bin/chef-zero
+++ b/bin/chef-zero
@@ -31,7 +31,8 @@ OptionParser.new do |opts|
opts.banner = "Usage: chef-zero [ARGS]"
opts.on("-H", "--host HOST", "Host to bind to (default: 127.0.0.1)") do |value|
- options[:host] = value
+ options[:host] ||= []
+ options[:host] << value
end
opts.on("-p", "--port PORT", "Port to listen on (e.g. 8889, or 8500-8600 or 8885,8888)") do |value|
diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb
index 9378afb..d550a4c 100644
--- a/lib/chef_zero/server.rb
+++ b/lib/chef_zero/server.rb
@@ -111,7 +111,7 @@ module ChefZero
class Server
DEFAULT_OPTIONS = {
- :host => '127.0.0.1',
+ :host => ['127.0.0.1'],
:port => 8889,
:log_level => :warn,
:generate_real_keys => true,
@@ -164,10 +164,10 @@ module ChefZero
#
def url
sch = @options[:ssl] ? 'https' : 'http'
- @url ||= if @options[:host].include?(':')
- URI("#{sch}://[#{@options[:host]}]:#{port}").to_s
+ @url ||= if @options[:host].first.include?(':')
+ URI("#{sch}://[#{@options[:host].first}]:#{port}").to_s
else
- URI("#{sch}://#{@options[:host]}:#{port}").to_s
+ URI("#{sch}://#{@options[:host].first}:#{port}").to_s
end
end
@@ -265,6 +265,17 @@ module ChefZero
# @return [Thread]
# the thread the background process is running in
#
+ def listen(hosts, port)
+ hosts.each do |host|
+ @server.listen(host, port)
+ end
+ true
+ rescue Errno::EADDRINUSE
+ ChefZero::Log.warn("Port #{port} not available")
+ @server.listeners.each { |l| l.close }
+ false
+ end
+
def start_background(wait = 5)
@server = WEBrick::HTTPServer.new(
:DoNotListen => true,
@@ -280,22 +291,15 @@ module ChefZero
@server.mount('/', Rack::Handler::WEBrick, app)
# Pick a port
- if options[:port].respond_to?(:each)
- options[:port].each do |port|
- begin
- @server.listen(options[:host], port)
- @port = port
- break
- rescue Errno::EADDRINUSE
- ChefZero::Log.info("Port #{port} in use: #{$!}")
- end
+ [options[:port]].flatten.each do |port|
+ if listen(options[:host], port)
+ @port = port
+ break
end
- if !@port
- raise Errno::EADDRINUSE, "No port in :port range #{options[:port]} is available"
- end
- else
- @server.listen(options[:host], options[:port])
- @port = options[:port]
+ end
+ if !@port
+ raise Errno::EADDRINUSE,
+ "No port in :port range #{options[:port]} is available"
end
# Start the server in the background