summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2015-04-01 08:56:15 -0700
committerdanielsdeleo <dan@getchef.com>2015-04-01 08:56:15 -0700
commitad6523da1ab7c5b41642e4bf4d0eeeed7d2ce1d3 (patch)
treecab7ae8afe964d4335f32b0a34a6b8b1ecb1ac49
parentd9fe481bf29f7325a0128fa53777df57f1951359 (diff)
parentd0e7b61728d5bf8e08fc1e40e7f2adf25fae216d (diff)
downloadchef-zero-ad6523da1ab7c5b41642e4bf4d0eeeed7d2ce1d3.tar.gz
Merge branch 'socketless'
-rw-r--r--lib/chef_zero/rest_base.rb3
-rw-r--r--lib/chef_zero/server.rb25
-rw-r--r--lib/chef_zero/socketless_server_map.rb84
-rw-r--r--spec/socketless_server_map_spec.rb71
4 files changed, 180 insertions, 3 deletions
diff --git a/lib/chef_zero/rest_base.rb b/lib/chef_zero/rest_base.rb
index 8156e7c..48d423a 100644
--- a/lib/chef_zero/rest_base.rb
+++ b/lib/chef_zero/rest_base.rb
@@ -195,8 +195,9 @@ module ChefZero
# Strip off /organizations/chef if we are in single org mode
if rest_path[0..1] != [ 'organizations', server.options[:single_org] ]
raise "Unexpected URL #{rest_path[0..1]} passed to build_uri in single org mode"
+ else
+ "#{base_uri}/#{rest_path[2..-1].join('/')}"
end
- "#{base_uri}/#{rest_path[2..-1].join('/')}"
else
"#{base_uri}/#{rest_path.join('/')}"
end
diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb
index d78daf5..9cf7b39 100644
--- a/lib/chef_zero/server.rb
+++ b/lib/chef_zero/server.rb
@@ -27,6 +27,7 @@ require 'webrick'
require 'webrick/https'
require 'chef_zero'
+require 'chef_zero/socketless_server_map'
require 'chef_zero/chef_data/cookbook_data'
require 'chef_zero/chef_data/acl_path'
require 'chef_zero/rest_router'
@@ -87,6 +88,7 @@ require 'chef_zero/endpoints/version_endpoint'
module ChefZero
class Server
+
DEFAULT_OPTIONS = {
:host => '127.0.0.1',
:port => 8889,
@@ -108,6 +110,7 @@ module ChefZero
end
@options.freeze
ChefZero::Log.level = @options[:log_level].to_sym
+ @app = nil
end
# @return [Hash]
@@ -146,6 +149,12 @@ module ChefZero
end
end
+ def local_mode_url
+ raise "Port not yet set, cannot generate URL" unless port.kind_of?(Integer)
+ "chefzero://localhost:#{port}"
+ end
+
+
#
# The data store for this server (default is in-memory).
#
@@ -224,7 +233,6 @@ module ChefZero
thread.join
end
-
#
# Start a Chef Zero server in a forked process. This method returns the PID
# to the forked process.
@@ -284,9 +292,19 @@ module ChefZero
sleep(0.01)
end
+ SocketlessServerMap.instance.register_port(@port, self)
+
@thread
end
+ def start_socketless
+ @port = SocketlessServerMap.instance.register_no_listen_server(self)
+ end
+
+ def handle_socketless_request(request_env)
+ app.call(request_env)
+ 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
@@ -315,6 +333,7 @@ module ChefZero
if @thread
ChefZero::Log.error("Chef Zero did not stop within #{wait} seconds! Killing...")
@thread.kill
+ SocketlessServerMap.deregister(port)
end
ensure
@server = nil
@@ -545,6 +564,7 @@ module ChefZero
end
def app
+ return @app if @app
router = RestRouter.new(open_source_endpoints)
router.not_found = NotFoundEndpoint.new
@@ -553,7 +573,7 @@ module ChefZero
else
rest_base_prefix = []
end
- return proc do |env|
+ @app = proc do |env|
begin
prefix = global_endpoint?(env['PATH_INFO']) ? [] : rest_base_prefix
@@ -591,6 +611,7 @@ module ChefZero
end
end
end
+ @app
end
def dejsonize_children(hash)
diff --git a/lib/chef_zero/socketless_server_map.rb b/lib/chef_zero/socketless_server_map.rb
new file mode 100644
index 0000000..531dbbb
--- /dev/null
+++ b/lib/chef_zero/socketless_server_map.rb
@@ -0,0 +1,84 @@
+#
+# Author:: Daniel DeLeo (<dan@chef.io>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'thread'
+require 'singleton'
+
+module ChefZero
+
+ class ServerNotFound < StandardError
+ end
+
+ class NoSocketlessPortAvailable < StandardError
+ end
+
+ class SocketlessServerMap
+
+ def self.request(port, request_env)
+ instance.request(port, request_env)
+ end
+
+ MUTEX = Mutex.new
+
+ include Singleton
+
+ def initialize()
+ reset!
+ end
+
+ def reset!
+ @servers_by_port = {}
+ end
+
+ def register_port(port, server)
+ MUTEX.synchronize do
+ @servers_by_port[port] = server
+ end
+ end
+
+ def register_no_listen_server(server)
+ MUTEX.synchronize do
+ 1.upto(1000) do |port|
+ unless @servers_by_port.key?(port)
+ @servers_by_port[port] = server
+ return port
+ end
+ end
+ raise NoSocketlessPortAvailable, "No socketless ports left to register"
+ end
+ end
+
+ def has_server_on_port?(port)
+ @servers_by_port.key?(port)
+ end
+
+ def deregister(port)
+ MUTEX.synchronize do
+ @servers_by_port.delete(port)
+ end
+ end
+
+ def request(port, request_env)
+ server = @servers_by_port[port]
+ raise ServerNotFound, "No socketless chef-zero server on given port #{port.inspect}" unless server
+ server.handle_socketless_request(request_env)
+ end
+
+ end
+end
+
diff --git a/spec/socketless_server_map_spec.rb b/spec/socketless_server_map_spec.rb
new file mode 100644
index 0000000..8ef2ac7
--- /dev/null
+++ b/spec/socketless_server_map_spec.rb
@@ -0,0 +1,71 @@
+require 'chef_zero/socketless_server_map'
+
+
+describe "Socketless Mode" do
+
+ let(:server_map) { ChefZero::SocketlessServerMap.instance.tap { |i| i.reset! } }
+
+ let(:server) { instance_double("ChefZero::Server") }
+
+ let(:second_server) { instance_double("ChefZero::Server") }
+
+ it "registers a socketful server" do
+ server_map.register_port(8889, server)
+ expect(server_map).to have_server_on_port(8889)
+ end
+
+ context "when a no-listen server is registered" do
+
+ let!(:port) { server_map.register_no_listen_server(server) }
+
+ it "assigns the server a low port number" do
+ expect(port).to eq(1)
+ end
+
+ context "and another server is registered" do
+
+ let!(:next_port) { server_map.register_no_listen_server(second_server) }
+
+ it "assigns another port when another server is registered" do
+ expect(next_port).to eq(2)
+ end
+
+ it "raises NoSocketlessPortAvailable when too many servers are registered" do
+ expect { 1000.times { server_map.register_no_listen_server(server) } }.to raise_error(ChefZero::NoSocketlessPortAvailable)
+ end
+
+ it "deregisters a server" do
+ expect(server_map).to have_server_on_port(1)
+ server_map.deregister(1)
+ expect(server_map).to_not have_server_on_port(1)
+ end
+
+ describe "routing requests to a server" do
+
+ let(:rack_req) do
+ r = {}
+ r["REQUEST_METHOD"] = "GET"
+ r["SCRIPT_NAME"] = ""
+ r["PATH_INFO"] = "/clients"
+ r["QUERY_STRING"] = ""
+ r["rack.input"] = StringIO.new("")
+ r
+ end
+
+ let(:rack_response) { [200, {}, ["this is the response body"] ] }
+
+ it "routes a request to the registered port" do
+ expect(server).to receive(:handle_socketless_request).with(rack_req).and_return(rack_response)
+ response = server_map.request(1, rack_req)
+ expect(response).to eq(rack_response)
+ end
+
+ it "raises ServerNotFound when a request is sent to an unregistered port" do
+ expect { server_map.request(99, rack_req) }.to raise_error(ChefZero::ServerNotFound)
+ end
+ end
+ end
+ end
+
+
+end