summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-01-25 13:36:00 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2015-01-25 13:36:00 -0800
commitbf033a6e6df652dc5bc9a21a2f102c15395b3d23 (patch)
tree77fd45d8a458304005ef3c1b63a9b30122d87497
parentf923755a505c31745f6df14e201149128ba4ebec (diff)
parent7042f3aab1b9deda1f9ec7e94294f848a31632b3 (diff)
downloadchef-bf033a6e6df652dc5bc9a21a2f102c15395b3d23.tar.gz
Merge pull request #2807 from chef/lcg/2431
Lcg/2431
-rw-r--r--CHANGELOG.md2
-rw-r--r--lib/chef/application/client.rb30
-rw-r--r--spec/data/recipes.tgzbin0 -> 4120 bytes
-rw-r--r--spec/integration/client/client_spec.rb50
4 files changed, 81 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e2eae350b..ab6adf8e36 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,8 @@
Add --lockfile opt for chef-client and chef-solo
* [**Josh Murphy**](https://github.com/jdmurphy)
Check cookbooks exist in path(s) before attempting to upload them with --all
+* [**Vasiliy Tolstov**](https://github.com/vtolstov)
+ add ability to fetch recipes like in chef-solo when using local-mode
### Chef Contributions
* ruby 1.9.3 support is dropped
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index 403b5c0593..753a9c2804 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -27,6 +27,7 @@ require 'chef/handler/error_report'
require 'chef/workstation_config_loader'
class Chef::Application::Client < Chef::Application
+ include Chef::Mixin::ShellOut
# Mimic self_pipe sleep from Unicorn to capture signals safely
SELF_PIPE = []
@@ -205,6 +206,10 @@ class Chef::Application::Client < Chef::Application
:description => "Fork client",
:boolean => true
+ option :recipe_url,
+ :long => "--recipe-url",
+ :description => "Pull down a remote archive of recipes and unpack it to the cookbook cache. Only used in local mode."
+
option :enable_reporting,
:short => "-R",
:long => "--enable-reporting",
@@ -258,7 +263,7 @@ class Chef::Application::Client < Chef::Application
super
raise Chef::Exceptions::PIDFileLockfileMatch if Chef::Util::PathHelper.paths_eql? (Chef::Config[:pid_file] || '' ), (Chef::Config[:lockfile] || '')
-
+
Chef::Config[:specific_recipes] = cli_arguments.map { |file| File.expand_path(file) }
Chef::Config[:chef_server_url] = config[:chef_server_url] if config.has_key? :chef_server_url
@@ -267,6 +272,20 @@ class Chef::Application::Client < Chef::Application
if Chef::Config.local_mode && !Chef::Config.has_key?(:cookbook_path) && !Chef::Config.has_key?(:chef_repo_path)
Chef::Config.chef_repo_path = Chef::Config.find_chef_repo_path(Dir.pwd)
end
+
+ if !Chef::Config.local_mode && Chef::Config.has_key?(:recipe_url)
+ Chef::Application.fatal!("chef-client recipe-url can be used only in local-mode", 1)
+ elsif Chef::Config.local_mode && Chef::Config.has_key?(:recipe_url)
+ Chef::Log.debug "Cleanup path #{Chef::Config.chef_repo_path} before extract recipes into it"
+ FileUtils.rm_rf(Chef::Config.chef_repo_path, :secure => true)
+ Chef::Log.debug "Creating path #{Chef::Config.chef_repo_path} to extract recipes into"
+ FileUtils.mkdir_p(Chef::Config.chef_repo_path)
+ tarball_path = File.join(Chef::Config.chef_repo_path, 'recipes.tgz')
+ fetch_recipe_tarball(Chef::Config[:recipe_url], tarball_path)
+ result = shell_out!("tar zxvf #{tarball_path} -C #{Chef::Config.chef_repo_path}")
+ Chef::Log.debug "#{result.stdout}"
+ end
+
Chef::Config.chef_zero.host = config[:chef_zero_host] if config[:chef_zero_host]
Chef::Config.chef_zero.port = config[:chef_zero_port] if config[:chef_zero_port]
@@ -442,4 +461,13 @@ class Chef::Application::Client < Chef::Application
msg += audit_mode_settings_explaination
return msg
end
+
+ def fetch_recipe_tarball(url, path)
+ Chef::Log.debug("Download recipes tarball from #{url} to #{path}")
+ File.open(path, 'wb') do |f|
+ open(url) do |r|
+ f.write(r.read)
+ end
+ end
+ end
end
diff --git a/spec/data/recipes.tgz b/spec/data/recipes.tgz
new file mode 100644
index 0000000000..e5d3e1669a
--- /dev/null
+++ b/spec/data/recipes.tgz
Binary files differ
diff --git a/spec/integration/client/client_spec.rb b/spec/integration/client/client_spec.rb
index 62660bb852..880e9c55d8 100644
--- a/spec/integration/client/client_spec.rb
+++ b/spec/integration/client/client_spec.rb
@@ -1,6 +1,33 @@
require 'support/shared/integration/integration_helper'
require 'chef/mixin/shell_out'
+def recipes_filename
+ File.join(CHEF_SPEC_DATA, 'recipes.tgz')
+end
+
+def start_tiny_server(server_opts={})
+ recipes_size = File::Stat.new(recipes_filename).size
+ @server = TinyServer::Manager.new(server_opts)
+ @server.start
+ @api = TinyServer::API.instance
+ @api.clear
+ #
+ # trivial endpoints
+ #
+ # just a normal file
+ # (expected_content should be uncompressed)
+ @api.get("/recipes.tgz", 200) {
+ File.open(recipes_filename, "rb") do |f|
+ f.read
+ end
+ }
+end
+
+def stop_tiny_server
+ @server.stop
+ @server = @api = nil
+end
+
describe "chef-client" do
include IntegrationSupport
include Chef::Mixin::ShellOut
@@ -279,4 +306,27 @@ end
end
end
+ context "when using recipe-url" do
+ before(:all) do
+ start_tiny_server
+ end
+
+ after(:all) do
+ stop_tiny_server
+ end
+
+ it "should complete with success when passed -z and --recipe-url" do
+ file 'config/client.rb', <<EOM
+cookbook_path "#{path_to('cookbooks')}"
+EOM
+
+ result = shell_out("#{chef_client} -c \"#{path_to('config/client.rb')}\" --recipe-url=http://localhost:9000/recipes.tgz -o 'x::default' -z", :cwd => chef_dir)
+ result.error!
+ end
+
+ it 'should fail when passed --recipe-url and not passed -z' do
+ result = shell_out("#{chef_client} --recipe-url=http://localhost:9000/recipes.tgz", :cwd => chef_dir)
+ expect(result.exitstatus).to eq(1)
+ end
+ end
end