summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-05-26 12:14:55 -0700
committerJohn Keiser <jkeiser@opscode.com>2014-05-26 12:14:55 -0700
commit70a5a2b23317887fbdd1e3e6ac580f440a666cac (patch)
tree0e5efeb8911f2677426c10fc1491a01562eaffdc
parent83b2686a59e637f7ac9888b7e370a4b5a7fc59e8 (diff)
downloadchef-70a5a2b23317887fbdd1e3e6ac580f440a666cac.tar.gz
Add "knife serve" to serve up chef repo in chef-zero
-rw-r--r--lib/chef/application.rb5
-rw-r--r--lib/chef/config.rb1
-rw-r--r--lib/chef/knife/serve.rb41
-rw-r--r--spec/integration/knife/serve_spec.rb57
4 files changed, 104 insertions, 0 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index 1683ed8861..cc80cef980 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -189,6 +189,7 @@ class Chef::Application
server_options[:data_store] = Chef::ChefFS::ChefFSDataStore.new(chef_fs)
server_options[:log_level] = Chef::Log.level
server_options[:port] = Chef::Config.chef_zero.port
+ server_options[:host] = Chef::Config.chef_zero.host
Chef::Log.info("Starting chef-zero on port #{Chef::Config.chef_zero.port} with repository at #{server_options[:data_store].chef_fs.fs_description}")
@chef_zero_server = ChefZero::Server.new(server_options)
@chef_zero_server.start_background
@@ -196,6 +197,10 @@ class Chef::Application
end
end
+ def self.chef_zero_server
+ @chef_zero_server
+ end
+
def self.destroy_server_connectivity
if @chef_zero_server
@chef_zero_server.stop
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 66dd4cc77e..d884aabc3d 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -307,6 +307,7 @@ class Chef
config_strict_mode true
default(:enabled) { Chef::Config.local_mode }
default :port, 8889
+ default :host, 'localhost'
end
default :chef_server_url, "https://localhost:443"
diff --git a/lib/chef/knife/serve.rb b/lib/chef/knife/serve.rb
new file mode 100644
index 0000000000..936e34fc70
--- /dev/null
+++ b/lib/chef/knife/serve.rb
@@ -0,0 +1,41 @@
+require 'chef/knife'
+
+class Chef
+ class Knife
+ class Serve < Knife
+ option :repo_mode,
+ :long => '--repo-mode MODE',
+ :description => "Specifies the local repository layout. Values: static, everything, hosted_everything. Default: everything/hosted_everything"
+
+ option :chef_repo_path,
+ :long => '--chef-repo-path PATH',
+ :description => 'Overrides the location of chef repo. Default is specified by chef_repo_path in the config'
+
+ option :chef_zero_host,
+ :long => '--chef-zero-host IP',
+ :description => 'Overrides the host upon which chef-zero listens. Default is 127.0.0.1.'
+
+
+ def configure_chef
+ super
+ Chef::Config.local_mode = true
+ Chef::Config[:repo_mode] = config[:repo_mode] if config[:repo_mode]
+
+ # --chef-repo-path forcibly overrides all other paths
+ if config[:chef_repo_path]
+ Chef::Config.chef_repo_path = config[:chef_repo_path]
+ %w(acl client cookbook container data_bag environment group node role user).each do |variable_name|
+ Chef::Config.delete("#{variable_name}_path".to_sym)
+ end
+ end
+ end
+
+ def run
+ server = Chef::Application.chef_zero_server
+ puts "Serving files from:\n#{server.options[:data_store].chef_fs.fs_description}"
+ server.stop
+ server.start(true) # to print header
+ end
+ end
+ end
+end
diff --git a/spec/integration/knife/serve_spec.rb b/spec/integration/knife/serve_spec.rb
new file mode 100644
index 0000000000..ef6990e434
--- /dev/null
+++ b/spec/integration/knife/serve_spec.rb
@@ -0,0 +1,57 @@
+#
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2013 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 'support/shared/integration/integration_helper'
+require 'chef/knife/serve'
+require 'chef/server_api'
+
+describe 'knife serve' do
+ extend IntegrationSupport
+ include KnifeSupport
+ include AppServerSupport
+
+ when_the_repository 'also has one of each thing' do
+ file 'nodes/x.json', { 'foo' => 'bar' }
+
+ it 'knife serve serves up /nodes/x' do
+ exception = nil
+ t = Thread.new do
+ begin
+ knife('serve')
+ rescue
+ exception = $!
+ end
+ end
+ begin
+ Chef::Config.log_level = :debug
+ Chef::Config.chef_server_url = 'http://127.0.0.1:8889'
+ Chef::Config.node_name = nil
+ Chef::Config.client_key = nil
+ api = Chef::ServerAPI.new
+ api.get('nodes/x')['name'].should == 'x'
+ rescue
+ if exception
+ raise exception
+ else
+ raise
+ end
+ ensure
+ t.kill
+ end
+ end
+ end
+end