Object
# File lib/chef_zero/server.rb, line 304 def clear_data data_store.clear end
The data store for this server (default is in-memory).
@return [~ChefZero::DataStore]
# File lib/chef_zero/server.rb, line 107 def data_store @data_store ||= @options[:data_store] || DataStore::MemoryStore.new end
# File lib/chef_zero/server.rb, line 225 def gen_key_pair if generate_real_keys? private_key = OpenSSL::PKey::RSA.new(2048) public_key = private_key.public_key.to_s public_key.sub!(/^-----BEGIN RSA PUBLIC KEY-----/, '-----BEGIN PUBLIC KEY-----') public_key.sub!(/-----END RSA PUBLIC KEY-----(\s+)$/, '-----END PUBLIC KEY-----\1') [private_key.to_s, public_key] else [PRIVATE_KEY, PUBLIC_KEY] end end
Boolean method to determine if real Public/Private keys should be generated.
@return [Boolean]
true if real keys should be created, false otherwise
# File lib/chef_zero/server.rb, line 118 def generate_real_keys? !!@options[:generate_real_keys] end
# File lib/chef_zero/server.rb, line 316 def inspect "#<#{self.class} @url=#{url.inspect}>" end
Load data in a nice, friendly form: {
'roles' => { 'desert' => '{ "description": "Hot and dry"' }, 'rainforest' => { "description" => 'Wet and humid' } }, 'cookbooks' => { 'apache2-1.0.1' => { 'templates' => { 'default' => { 'blah.txt' => 'hi' }} 'recipes' => { 'default.rb' => 'template "blah.txt"' } 'metadata.rb' => 'depends "mysql"' }, 'apache2-1.2.0' => { 'templates' => { 'default' => { 'blah.txt' => 'lo' }} 'recipes' => { 'default.rb' => 'template "blah.txt"' } 'metadata.rb' => 'depends "mysql"' }, 'mysql' => { 'recipes' => { 'default.rb' => 'file { contents "hi" }' }, 'metadata.rb' => 'version "1.0.0"' } }
}
# File lib/chef_zero/server.rb, line 268 def load_data(contents) %(clients environments nodes roles users).each do |data_type| if contents[data_type] dejsonize_children(contents[data_type]).each_pair do |name, data| data_store.set([data_type, name], data, :create) end end end if contents['data'] contents['data'].each_pair do |key, data_bag| data_store.create_dir(['data'], key, :recursive) dejsonize_children(data_bag).each do |item_name, item| data_store.set(['data', key, item_name], item, :create) end end end if contents['cookbooks'] contents['cookbooks'].each_pair do |name_version, cookbook| if name_version =~ /(.+)-(\d+\.\d+\.\d+)$/ cookbook_data = CookbookData.to_hash(cookbook, $1, $2) else cookbook_data = CookbookData.to_hash(cookbook, name_version) end raise "No version specified" if !cookbook_data[:version] data_store.create_dir(['cookbooks'], cookbook_data[:cookbook_name], :recursive) data_store.set(['cookbooks', cookbook_data[:cookbook_name], cookbook_data[:version]], JSON.pretty_generate(cookbook_data), :create) cookbook_data.values.each do |files| next unless files.is_a? Array files.each do |file| data_store.set(['file_store', 'checksums', file[:checksum]], get_file(cookbook, file[:path]), :create) end end end end end
# File lib/chef_zero/server.rb, line 237 def on_request(&block) @on_request_proc = block end
# File lib/chef_zero/server.rb, line 241 def on_response(&block) @on_response_proc = block end
# File lib/chef_zero/server.rb, line 308 def request_handler(&block) @request_handler = block end
Boolean method to determine if the server is currently ready to accept requests. This method will attempt to make an HTTP request against the server. If this method returns true, you are safe to make a request.
@return [Boolean]
true if the server is accepting requests, false otherwise
# File lib/chef_zero/server.rb, line 190 def running? if @server.nil? || @server.status != :Running return false end uri = URI.join(url, 'cookbooks') headers = { 'Accept' => 'application/json' } Timeout.timeout(0.1) { !open(uri, headers).nil? } rescue SocketError, Errno::ECONNREFUSED, Timeout::Error false end
Start a Chef Zero server in the current thread. You can stop this server by canceling the current thread.
@param [Boolean] publish
publish the server information to STDOUT
@return [nil]
this method will block the main thread until interrupted
# File lib/chef_zero/server.rb, line 132 def start(publish = true) publish = publish[:publish] if publish.is_a?(Hash) # Legacy API if publish puts >> Starting Chef Zero (v#{ChefZero::VERSION})... >> WEBrick (v#{WEBrick::VERSION}) on Rack (v#{Rack.release}) is listening at #{url} >> Press CTRL+C to stop.gsub(/^ {10}/, '') end thread = start_background ]INT TERM].each do |signal| Signal.trap(signal) do puts "\n>> Stopping Chef Zero..." @server.shutdown end end # Move the background process to the main thread thread.join end
Start a Chef Zero server in a forked process. This method returns the PID to the forked process.
@param [Fixnum] wait
the number of seconds to wait for the server to start
@return [Thread]
the thread the background process is running in
# File lib/chef_zero/server.rb, line 168 def start_background(wait = 5) @server = WEBrick::HTTPServer.new( :BindAddress => @options[:host], :Port => @options[:port], :AccessLog => [], :Logger => WEBrick::Log.new(StringIO.new, 7) ) @server.mount('/', Rack::Handler::WEBrick, app) @thread = Thread.new { @server.start } @thread.abort_on_exception = true @thread end
Gracefully stop the Chef Zero server.
@param [Fixnum] wait
the number of seconds to wait before raising force-terminating the server
# File lib/chef_zero/server.rb, line 210 def stop(wait = 5) Timeout.timeout(wait) do @server.shutdown @thread.join(wait) if @thread end rescue Timeout::Error if @thread ChefZero::Log.error("Chef Zero did not stop within #{wait} seconds! Killing...") @thread.kill end ensure @server = nil @thread = nil end
# File lib/chef_zero/server.rb, line 312 def to_s "#<#{self.class} #{url}>" end
The URL for this Chef Zero server. If the given host is an IPV6 address, it is escaped in brackets according to RFC-2732.
@see www.ietf.org/rfc/rfc2732.txt RFC-2732
@return [String]
# File lib/chef_zero/server.rb, line 94 def url @url ||= if @options[:host].include?(':') "http://[#{@options[:host]}]:#{@options[:port]}" else "http://#{@options[:host]}:#{@options[:port]}" end end
Generated with the Darkfish Rdoc Generator 2.