chef-zero 1.7.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/chef-zero +8 -9
- data/lib/chef_zero/cookbook_data.rb +2 -2
- data/lib/chef_zero/server.rb +130 -106
- data/lib/chef_zero/version.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2731ca89a7a17e4eddfe989bd8dc57c361105ed
|
4
|
+
data.tar.gz: f81dabcb437d9e327d547b6723273a7bb1c760c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c87db48733d2add9a6de265fddfc99f028051feaf9c2aaef7b849bdde3f02c9d17966ca88fc3df6fc8d44572262a33392aeccc321ce12fff2da25a6831dbacf0
|
7
|
+
data.tar.gz: bbb68d646e14d774987d46d13e3541b20d1f93813d8a0112525569ef22f1e8b00940e057242ce39f7f773e2ae363751da10dd8152f85ce8814b0d18e8139671e
|
data/bin/chef-zero
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
# Trap interrupts to quit cleanly.
|
4
|
+
Signal.trap('INT') { exit 1 }
|
5
|
+
|
3
6
|
require 'rubygems'
|
4
7
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
|
5
8
|
|
@@ -20,10 +23,6 @@ OptionParser.new do |opts|
|
|
20
23
|
options[:port] = value
|
21
24
|
end
|
22
25
|
|
23
|
-
opts.on("--socket PATH", String, "Unix socket path to listen on") do |value|
|
24
|
-
options[:socket] = value
|
25
|
-
end
|
26
|
-
|
27
26
|
opts.on("--[no-]generate-keys", "Whether to generate actual keys or fake it (faster). Default: false.") do |value|
|
28
27
|
options[:generate_real_keys] = value
|
29
28
|
end
|
@@ -50,12 +49,12 @@ end.parse!
|
|
50
49
|
server = ChefZero::Server.new(options)
|
51
50
|
|
52
51
|
if options[:daemon]
|
53
|
-
|
54
|
-
abort 'Process.deamon requires Ruby >= 1.9'
|
55
|
-
else
|
52
|
+
if Process.respond_to?(:daemon)
|
56
53
|
Process.daemon(true)
|
57
|
-
server.start(
|
54
|
+
server.start(true)
|
55
|
+
else
|
56
|
+
abort 'Process.daemon requires Ruby >= 1.9'
|
58
57
|
end
|
59
58
|
else
|
60
|
-
server.start(
|
59
|
+
server.start(true)
|
61
60
|
end
|
data/lib/chef_zero/server.rb
CHANGED
@@ -17,10 +17,13 @@
|
|
17
17
|
#
|
18
18
|
|
19
19
|
require 'openssl'
|
20
|
+
require 'open-uri'
|
20
21
|
require 'rubygems'
|
21
22
|
require 'timeout'
|
22
23
|
require 'stringio'
|
23
|
-
|
24
|
+
|
25
|
+
require 'rack'
|
26
|
+
require 'webrick'
|
24
27
|
|
25
28
|
require 'chef_zero'
|
26
29
|
require 'chef_zero/cookbook_data'
|
@@ -61,138 +64,155 @@ module ChefZero
|
|
61
64
|
DEFAULT_OPTIONS = {
|
62
65
|
:host => '127.0.0.1',
|
63
66
|
:port => 8889,
|
64
|
-
:socket => nil,
|
65
67
|
:log_level => :info,
|
66
68
|
:generate_real_keys => true
|
67
69
|
}.freeze
|
68
70
|
|
69
71
|
def initialize(options = {})
|
70
|
-
options = DEFAULT_OPTIONS.merge(options)
|
71
|
-
@options = options
|
72
|
-
|
73
|
-
@url = "http://#{uri_safe_host}:#{options[:port]}"
|
74
|
-
@generate_real_keys = options[:generate_real_keys]
|
75
|
-
|
76
|
-
ChefZero::Log.level = options[:log_level].to_sym
|
77
|
-
|
78
|
-
begin
|
79
|
-
require 'puma'
|
80
|
-
@server = Puma::Server.new(make_app, Puma::Events.new(STDERR, STDOUT))
|
81
|
-
if options[:socket]
|
82
|
-
@server.add_unix_listener(options[:socket])
|
83
|
-
else
|
84
|
-
@server.add_tcp_listener(options[:host], options[:port])
|
85
|
-
end
|
86
|
-
@server_type = :puma
|
87
|
-
rescue LoadError
|
88
|
-
require 'rack'
|
89
|
-
@server_type = :webrick
|
90
|
-
end
|
72
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
73
|
+
@options[:host] = "[#{@options[:host]}]" if @options[:host].include?(':')
|
74
|
+
@options.freeze
|
91
75
|
|
92
|
-
|
76
|
+
ChefZero::Log.level = @options[:log_level].to_sym
|
93
77
|
end
|
94
78
|
|
79
|
+
# @return [Hash]
|
95
80
|
attr_reader :options
|
81
|
+
|
82
|
+
# @return [WEBrick::HTTPServer]
|
96
83
|
attr_reader :server
|
97
|
-
attr_reader :data_store
|
98
|
-
attr_reader :url
|
99
84
|
|
100
85
|
include ChefZero::Endpoints
|
101
86
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
87
|
+
#
|
88
|
+
# The URL for this Chef Zero server.
|
89
|
+
#
|
90
|
+
# @return [String]
|
91
|
+
#
|
92
|
+
def url
|
93
|
+
"http://#{@options[:host]}:#{@options[:port]}"
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# The data store for this server (default is in-memory).
|
98
|
+
#
|
99
|
+
# @return [~ChefZero::DataStore]
|
100
|
+
#
|
101
|
+
def data_store
|
102
|
+
@data_store ||= @options[:data_store] || DataStore::MemoryStore.new
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# Boolean method to determine if real Public/Private keys should be
|
107
|
+
# generated.
|
108
|
+
#
|
109
|
+
# @return [Boolean]
|
110
|
+
# true if real keys should be created, false otherwise
|
111
|
+
#
|
112
|
+
def generate_real_keys?
|
113
|
+
!!@options[:generate_real_keys]
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
# Start a Chef Zero server in the current thread. You can stop this server
|
118
|
+
# by canceling the current thread.
|
119
|
+
#
|
120
|
+
# @param [Boolean] publish
|
121
|
+
# publish the server information to STDOUT
|
122
|
+
#
|
123
|
+
# @return [nil]
|
124
|
+
# this method will block the main thread until interrupted
|
125
|
+
#
|
126
|
+
def start(publish = true)
|
127
|
+
publish = publish[:publish] if publish.is_a?(Hash) # Legacy API
|
128
|
+
|
129
|
+
if publish
|
130
|
+
puts <<-EOH.gsub(/^ {10}/, '')
|
131
|
+
>> Starting Chef Zero (v#{ChefZero::VERSION})...
|
132
|
+
>> WEBrick (v#{WEBrick::VERSION}) on Rack (v#{Rack.release}) is listening at #{url}
|
133
|
+
>> Press CTRL+C to stop
|
134
|
+
|
135
|
+
EOH
|
112
136
|
end
|
113
137
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
make_app,
|
121
|
-
:BindAddress => @options[:host],
|
122
|
-
:Port => @options[:port],
|
123
|
-
:AccessLog => [],
|
124
|
-
:Logger => WEBrick::Log::new(StringIO.new, 7)
|
125
|
-
) do |server|
|
126
|
-
@server = server
|
127
|
-
end
|
128
|
-
end
|
129
|
-
rescue Object, Interrupt
|
130
|
-
if running?
|
131
|
-
puts "\n>> Stopping Chef Zero ..."
|
132
|
-
case @server_type
|
133
|
-
when :puma
|
134
|
-
server.stop(true)
|
135
|
-
when :webrick
|
136
|
-
server.shutdown
|
137
|
-
end
|
138
|
-
end
|
139
|
-
ensure
|
140
|
-
case @server_type
|
141
|
-
when :webrick
|
142
|
-
@server = nil
|
143
|
-
else
|
138
|
+
thread = start_background
|
139
|
+
|
140
|
+
%w[INT TERM].each do |signal|
|
141
|
+
Signal.trap(signal) do
|
142
|
+
puts "\n>> Stopping Chef Zero..."
|
143
|
+
@server.shutdown
|
144
144
|
end
|
145
145
|
end
|
146
|
-
end
|
147
146
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
start
|
152
|
-
rescue
|
153
|
-
@server_error = $!
|
154
|
-
ChefZero::Log.error("#{$!.message}\n#{$!.backtrace.join("\n")}")
|
155
|
-
end
|
156
|
-
}
|
147
|
+
# Move the background process to the main thread
|
148
|
+
thread.join
|
149
|
+
end
|
157
150
|
|
158
|
-
# Wait x seconds to make sure the server actually started
|
159
|
-
Timeout::timeout(wait) {
|
160
|
-
sleep(0.01) until running? || @server_error
|
161
|
-
raise @server_error if @server_error
|
162
|
-
}
|
163
151
|
|
164
|
-
|
152
|
+
#
|
153
|
+
# Start a Chef Zero server in a forked process. This method returns the PID
|
154
|
+
# to the forked process.
|
155
|
+
#
|
156
|
+
# @param [Fixnum] wait
|
157
|
+
# the number of seconds to wait for the server to start
|
158
|
+
#
|
159
|
+
# @return [Thread]
|
160
|
+
# the thread the background process is running in
|
161
|
+
#
|
162
|
+
def start_background(wait = 5)
|
163
|
+
@server = WEBrick::HTTPServer.new(
|
164
|
+
:BindAddress => @options[:host],
|
165
|
+
:Port => @options[:port],
|
166
|
+
:AccessLog => [],
|
167
|
+
:Logger => WEBrick::Log.new(StringIO.new, 7)
|
168
|
+
)
|
169
|
+
@server.mount('/', Rack::Handler::WEBrick, app)
|
170
|
+
|
171
|
+
@thread = Thread.new { @server.start }
|
172
|
+
@thread.abort_on_exception = true
|
165
173
|
@thread
|
166
174
|
end
|
167
175
|
|
176
|
+
#
|
177
|
+
# Boolean method to determine if the server is currently ready to accept
|
178
|
+
# requests. This method will attempt to make an HTTP request against the
|
179
|
+
# server. If this method returns true, you are safe to make a request.
|
180
|
+
#
|
181
|
+
# @return [Boolean]
|
182
|
+
# true if the server is accepting requests, false otherwise
|
183
|
+
#
|
168
184
|
def running?
|
169
|
-
|
170
|
-
|
171
|
-
!!server.running
|
172
|
-
when :webrick
|
173
|
-
!!(server && server.status == :Running)
|
185
|
+
if @server.nil? || @server.status != :Running
|
186
|
+
return false
|
174
187
|
end
|
188
|
+
|
189
|
+
uri = URI.join(url, 'cookbooks')
|
190
|
+
headers = { 'Accept' => 'application/json' }
|
191
|
+
|
192
|
+
Timeout.timeout(0.1) { !open(uri, headers).nil? }
|
193
|
+
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error
|
194
|
+
false
|
175
195
|
end
|
176
196
|
|
197
|
+
#
|
198
|
+
# Gracefully stop the Chef Zero server.
|
199
|
+
#
|
200
|
+
# @param [Fixnum] wait
|
201
|
+
# the number of seconds to wait before raising force-terminating the
|
202
|
+
# server
|
203
|
+
#
|
177
204
|
def stop(wait = 5)
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
when :webrick
|
182
|
-
server.shutdown
|
183
|
-
@server
|
205
|
+
Timeout.timeout(wait) do
|
206
|
+
@server.shutdown
|
207
|
+
@thread.join(wait) if @thread
|
184
208
|
end
|
209
|
+
rescue Timeout::Error
|
185
210
|
if @thread
|
186
|
-
|
187
|
-
|
188
|
-
rescue
|
189
|
-
if @thread
|
190
|
-
ChefZero::Log.error "Server did not stop within #{wait} seconds. Killing..."
|
191
|
-
@thread.kill
|
192
|
-
end
|
193
|
-
end
|
211
|
+
ChefZero::Log.error("Chef Zero did not stop within #{wait} seconds! Killing...")
|
212
|
+
@thread.kill
|
194
213
|
end
|
195
214
|
ensure
|
215
|
+
@server = nil
|
196
216
|
@thread = nil
|
197
217
|
end
|
198
218
|
|
@@ -283,9 +303,17 @@ module ChefZero
|
|
283
303
|
@request_handler = block
|
284
304
|
end
|
285
305
|
|
306
|
+
def to_s
|
307
|
+
"#<#{self.class} #{url}>"
|
308
|
+
end
|
309
|
+
|
310
|
+
def inspect
|
311
|
+
"#<#{self.class} @url=#{url.inspect}>"
|
312
|
+
end
|
313
|
+
|
286
314
|
private
|
287
315
|
|
288
|
-
def
|
316
|
+
def app
|
289
317
|
router = RestRouter.new([
|
290
318
|
[ '/authenticate_user', AuthenticateUserEndpoint.new(self) ],
|
291
319
|
[ '/clients', ActorsEndpoint.new(self) ],
|
@@ -365,9 +393,5 @@ module ChefZero
|
|
365
393
|
end
|
366
394
|
value
|
367
395
|
end
|
368
|
-
|
369
|
-
def generate_real_keys?
|
370
|
-
!!@generate_real_keys
|
371
|
-
end
|
372
396
|
end
|
373
397
|
end
|
data/lib/chef_zero/version.rb
CHANGED
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:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Keiser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2013-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mixlib-log
|
@@ -119,8 +119,6 @@ files:
|
|
119
119
|
- LICENSE
|
120
120
|
- README.md
|
121
121
|
- Rakefile
|
122
|
-
- bin/chef-zero
|
123
|
-
- lib/chef_zero.rb
|
124
122
|
- lib/chef_zero/cookbook_data.rb
|
125
123
|
- lib/chef_zero/data_normalizer.rb
|
126
124
|
- lib/chef_zero/data_store/data_already_exists_error.rb
|
@@ -173,10 +171,12 @@ files:
|
|
173
171
|
- lib/chef_zero/solr/solr_doc.rb
|
174
172
|
- lib/chef_zero/solr/solr_parser.rb
|
175
173
|
- lib/chef_zero/version.rb
|
174
|
+
- lib/chef_zero.rb
|
176
175
|
- spec/run.rb
|
177
176
|
- spec/search_spec.rb
|
178
177
|
- spec/support/pedant.rb
|
179
178
|
- spec/support/stickywicket.pem
|
179
|
+
- bin/chef-zero
|
180
180
|
homepage: http://www.opscode.com
|
181
181
|
licenses:
|
182
182
|
- Apache 2.0
|
@@ -197,10 +197,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
197
197
|
version: '0'
|
198
198
|
requirements: []
|
199
199
|
rubyforge_project:
|
200
|
-
rubygems_version: 2.
|
200
|
+
rubygems_version: 2.1.11
|
201
201
|
signing_key:
|
202
202
|
specification_version: 4
|
203
203
|
summary: Self-contained, easy-setup, fast-start in-memory Chef server for testing
|
204
204
|
and solo setup purposes
|
205
205
|
test_files: []
|
206
|
-
has_rdoc:
|