chef-zero 2.1.5 → 2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 322a85c869fafa6c722e5b54697fc5daa9aa669e
4
- data.tar.gz: 4b436b5ea2f219f7b17988d4832a2aa9f72bf64d
3
+ metadata.gz: 1e812fceafd51d3dbba1d43446f13c67c12dfbee
4
+ data.tar.gz: e6ec4a6756763c7fc69d10e4e76c309e91ad469e
5
5
  SHA512:
6
- metadata.gz: 89a0555f5a959b8593d27f07783ba18e297e6f7f76ae9915775abdcb82adc2e159a4bc10968797aebc25a1dc4ebadf9c3179248bf90cc732a701608c624bda83
7
- data.tar.gz: cd000b0a319f0ac6cab623f93fd306678ea1dbd7aa5f41e3b3f44f711a738cde5616c5d178afa00449cd13a0f6498c7374535b6e19453222dd59ff225334bcee
6
+ metadata.gz: bdb9632a016a53427b3d1a977d7fa8ddb6a50f1e02c73001bfbdc192ef0b368798dda5beca6cb18e81394e63fc8132c5db125e08f5b4bb45155fca5dbf839f10
7
+ data.tar.gz: 3b01505e14cefdee548abf5324d742742159a3d66ceaa604d2463095eed7b96c8f1eb5ae9c8ad93a90eea082e91b19d5008074b053926062a2ad42f48ad07874
data/Rakefile CHANGED
@@ -3,7 +3,13 @@ require 'bundler/gem_tasks'
3
3
 
4
4
  require 'chef_zero/version'
5
5
 
6
+ task :default => :pedant
7
+
6
8
  task :spec do
9
+ system('rspec spec/*_spec.rb')
10
+ end
11
+
12
+ task :pedant do
7
13
  require File.expand_path('spec/run')
8
14
  end
9
15
 
data/bin/chef-zero CHANGED
@@ -10,6 +10,19 @@ require 'chef_zero/version'
10
10
  require 'chef_zero/server'
11
11
  require 'optparse'
12
12
 
13
+ def parse_port(port)
14
+ array = []
15
+ port.split(',').each do |part|
16
+ a,b = part.split('-',2)
17
+ if b
18
+ array = array.concat(a.to_i.upto(b.to_i).to_a)
19
+ else
20
+ array = array.concat([a.to_i])
21
+ end
22
+ end
23
+ array
24
+ end
25
+
13
26
  options = {}
14
27
 
15
28
  OptionParser.new do |opts|
@@ -19,8 +32,9 @@ OptionParser.new do |opts|
19
32
  options[:host] = value
20
33
  end
21
34
 
22
- opts.on("-p", "--port PORT", Integer, "Port to listen on") do |value|
23
- options[:port] = value
35
+ opts.on("-p", "--port PORT", "Port to listen on (e.g. 8889, or 8500-8600 or 8885,8888)") do |value|
36
+ options[:port] ||= []
37
+ options[:port] += parse_port(value)
24
38
  end
25
39
 
26
40
  opts.on("--[no-]generate-keys", "Whether to generate actual keys or fake it (faster). Default: false.") do |value|
@@ -80,6 +80,17 @@ module ChefZero
80
80
  # @return [Hash]
81
81
  attr_reader :options
82
82
 
83
+ # @return [Integer]
84
+ def port
85
+ if @port
86
+ @port
87
+ elsif !options[:port].respond_to?(:each)
88
+ options[:port]
89
+ else
90
+ raise "port cannot be determined until server is started"
91
+ end
92
+ end
93
+
83
94
  # @return [WEBrick::HTTPServer]
84
95
  attr_reader :server
85
96
 
@@ -95,9 +106,9 @@ module ChefZero
95
106
  #
96
107
  def url
97
108
  @url ||= if @options[:host].include?(':')
98
- URI("http://[#{@options[:host]}]:#{@options[:port]}").to_s
109
+ URI("http://[#{@options[:host]}]:#{port}").to_s
99
110
  else
100
- URI("http://#{@options[:host]}:#{@options[:port]}").to_s
111
+ URI("http://#{@options[:host]}:#{port}").to_s
101
112
  end
102
113
  end
103
114
 
@@ -153,14 +164,20 @@ module ChefZero
153
164
  output = publish.respond_to?(:puts) ? publish : STDOUT
154
165
  output.puts <<-EOH.gsub(/^ {10}/, '')
155
166
  >> Starting Chef Zero (v#{ChefZero::VERSION})...
167
+ EOH
168
+ end
169
+
170
+ thread = start_background
171
+
172
+ if publish
173
+ output = publish.respond_to?(:puts) ? publish : STDOUT
174
+ output.puts <<-EOH.gsub(/^ {10}/, '')
156
175
  >> WEBrick (v#{WEBrick::VERSION}) on Rack (v#{Rack.release}) is listening at #{url}
157
176
  >> Press CTRL+C to stop
158
177
 
159
178
  EOH
160
179
  end
161
180
 
162
- thread = start_background
163
-
164
181
  %w[INT TERM].each do |signal|
165
182
  Signal.trap(signal) do
166
183
  puts "\n>> Stopping Chef Zero..."
@@ -185,8 +202,7 @@ module ChefZero
185
202
  #
186
203
  def start_background(wait = 5)
187
204
  @server = WEBrick::HTTPServer.new(
188
- :BindAddress => @options[:host],
189
- :Port => @options[:port],
205
+ :DoNotListen => true,
190
206
  :AccessLog => [],
191
207
  :Logger => WEBrick::Log.new(StringIO.new, 7),
192
208
  :StartCallback => proc {
@@ -195,18 +211,41 @@ module ChefZero
195
211
  )
196
212
  @server.mount('/', Rack::Handler::WEBrick, app)
197
213
 
214
+ # Pick a port
215
+ if options[:port].respond_to?(:each)
216
+ options[:port].each do |port|
217
+ begin
218
+ @server.listen(options[:host], port)
219
+ @port = port
220
+ break
221
+ rescue Errno::EADDRINUSE
222
+ ChefZero::Log.info("Port #{port} in use: #{$!}")
223
+ end
224
+ end
225
+ if !@port
226
+ raise Errno::EADDRINUSE, "No port in :port range #{options[:port]} is available"
227
+ end
228
+ else
229
+ @server.listen(options[:host], options[:port])
230
+ @port = options[:port]
231
+ end
232
+
233
+ # Start the server in the background
198
234
  @thread = Thread.new do
199
235
  begin
200
236
  Thread.current.abort_on_exception = true
201
237
  @server.start
202
238
  ensure
239
+ @port = nil
203
240
  @running = false
204
241
  end
205
242
  end
243
+
206
244
  # Do not return until the web server is genuinely started.
207
245
  while !@running && @thread.alive?
208
246
  sleep(0.01)
209
247
  end
248
+
210
249
  @thread
211
250
  end
212
251
 
@@ -1,3 +1,3 @@
1
1
  module ChefZero
2
- VERSION = '2.1.5'
2
+ VERSION = '2.2'
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'chef_zero/server'
2
+
3
+ describe ChefZero::Server do
4
+ context 'with a server bound to port 8889' do
5
+ before :each do
6
+ @server = ChefZero::Server.new(:port => 8889)
7
+ @server.start_background
8
+ end
9
+ after :each do
10
+ @server.stop
11
+ end
12
+
13
+ it 'a second server bound to port 8889 throws EADDRINUSE' do
14
+ expect { ChefZero::Server.new(:port => 8889).start }.to raise_error Errno::EADDRINUSE
15
+ end
16
+
17
+ it 'a server bound to range 8889-9999 binds to a port > 8889' do
18
+ server = ChefZero::Server.new(:port => 8889.upto(9999))
19
+ server.start_background
20
+ expect(server.port).to be > 8889
21
+ expect(URI(server.url).port).to be > 8889
22
+ end
23
+
24
+ it 'a server bound to range 8889-8889 throws an exception' do
25
+ expect { ChefZero::Server.new(:port => 8889.upto(8889)).start_background }.to raise_error Errno::EADDRINUSE
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.5
4
+ version: '2.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Keiser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-03 00:00:00.000000000 Z
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mixlib-log
@@ -166,6 +166,7 @@ files:
166
166
  - lib/chef_zero/version.rb
167
167
  - spec/run.rb
168
168
  - spec/search_spec.rb
169
+ - spec/server_spec.rb
169
170
  - spec/support/pedant.rb
170
171
  - spec/support/stickywicket.pem
171
172
  homepage: http://www.opscode.com