summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2016-02-01 15:47:30 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2016-02-09 09:16:06 -0800
commitaa74c0ac58393e9186c2ebecbc3144c2c8d2ee85 (patch)
tree13a59b98b8c643e9bfe3b0a0958ddc396d609f46
parent99ea57ca5bdd46902855a7cd80b86d24d3371486 (diff)
downloadchef-aa74c0ac58393e9186c2ebecbc3144c2c8d2ee85.tar.gz
get the formatting for cookbook gems correct
this leverages the PR: https://github.com/bundler/bundler/pull/4245 to redirect output from bundler into chef's logger/formatter to get the UI right. if you don't have that in the external bundler you'll just get output on STDOUT. fix is to upgrade bundler.
-rw-r--r--lib/chef/cookbook/cookbook_collection.rb6
-rw-r--r--lib/chef/cookbook/gem_installer.rb72
-rw-r--r--lib/chef/event_dispatch/base.rb24
-rw-r--r--lib/chef/formatters/doc.rb25
-rw-r--r--lib/chef/policy_builder/expand_node_object.rb6
-rw-r--r--lib/chef/policy_builder/policyfile.rb4
6 files changed, 119 insertions, 18 deletions
diff --git a/lib/chef/cookbook/cookbook_collection.rb b/lib/chef/cookbook/cookbook_collection.rb
index 7e633da03d..d06b8fd042 100644
--- a/lib/chef/cookbook/cookbook_collection.rb
+++ b/lib/chef/cookbook/cookbook_collection.rb
@@ -1,7 +1,7 @@
#--
# Author:: Tim Hinderliter (<tim@chef.io>)
# Author:: Christopher Walters (<cw@chef.io>)
-# Copyright:: Copyright 2010-2016, Chef Software, Inc.
+# Copyright:: Copyright 2010-2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -56,8 +56,8 @@ class Chef
end
end
- def install_gems
- Cookbook::GemInstaller.new(self).install
+ def install_gems(events)
+ Cookbook::GemInstaller.new(self, events).install
end
end
end
diff --git a/lib/chef/cookbook/gem_installer.rb b/lib/chef/cookbook/gem_installer.rb
index 38e23d4807..fa6dccfa4f 100644
--- a/lib/chef/cookbook/gem_installer.rb
+++ b/lib/chef/cookbook/gem_installer.rb
@@ -17,16 +17,22 @@
# limitations under the License.
#
-require 'tmpdir'
-require 'bundler/inline'
+require "tmpdir"
+begin
+ require "bundler/inline"
+rescue LoadError
+ raise RuntimeError, "The RFC060 metadata gem feature requires bundler 1.10.0 or greater."
+end
class Chef
class Cookbook
class GemInstaller
+ attr_accessor :events
attr_accessor :cookbook_collection
- def initialize(cookbook_collection)
+ def initialize(cookbook_collection, events)
@cookbook_collection = cookbook_collection
+ @events = events
end
def install
@@ -36,24 +42,70 @@ class Chef
cookbook_gems += cookbook_version.metadata.gems
end
- return if cookbook_gems.empty?
+ events.cookbook_gem_start(cookbook_gems)
- gemfile(true) do
- source 'https://rubygems.org'
- cookbook_gems.each do |args|
- gem *args
+ unless cookbook_gems.empty?
+ begin
+ inline_gemfile do
+ source "https://rubygems.org"
+ cookbook_gems.each do |args|
+ gem *args
+ end
+ end
+ rescue Exception => e
+ events.cookbook_gem_failed(e)
+ raise
end
end
+
+ events.cookbook_gem_finished
end
class ChefBundlerUI < Bundler::UI::Silent
+ attr_accessor :events
+
+ def initialize(events)
+ @events = events
+ super()
+ end
+
def confirm(msg, newline = nil)
- Chef::Log.warn("CONFIRM: #{msg}")
+ # looks like "Installing time_ago_in_words 0.1.1" when installing
+ if msg =~ /Installing\s+(\S+)\s+(\S+)/
+ events.cookbook_gem_installing($1, $2)
+ end
+ Chef::Log.info(msg)
end
def error(msg, newline = nil)
- Chef::Log.warn("ERROR: #{msg}")
+ Chef::Log.error(msg)
+ end
+
+ def debug(msg, newline = nil)
+ Chef::Log.debug(msg)
end
+
+ def info(msg, newline = nil)
+ # looks like "Using time_ago_in_words 0.1.1" when using, plus other misc output
+ if msg =~ /Using\s+(\S+)\s+(\S+)/
+ events.cookbook_gem_using($1, $2)
+ end
+ Chef::Log.info(msg)
+ end
+
+ def warn(msg, newline = nil)
+ Chef::Log.warn(msg)
+ end
+ end
+
+ private
+
+ def inline_gemfile(&block)
+ # requires https://github.com/bundler/bundler/pull/4245
+ gemfile(true, ui: ChefBundlerUI.new(events), &block)
+ rescue ArgumentError # Method#arity doesn't inspect optional arguments, so we rescue
+ # requires bundler 1.10.0
+ gemfile(true, &block)
end
end
end
diff --git a/lib/chef/event_dispatch/base.rb b/lib/chef/event_dispatch/base.rb
index a6a18718c2..14444dd4eb 100644
--- a/lib/chef/event_dispatch/base.rb
+++ b/lib/chef/event_dispatch/base.rb
@@ -139,6 +139,30 @@ class Chef
def cookbook_sync_complete
end
+ # Called when starting to collect gems from the cookbooks
+ def cookbook_gem_start
+ end
+
+ # Called with the full list of gems to resolve
+ def cookbook_gem_set(gems)
+ end
+
+ # Called when the result of installing the bundle is to install the gem
+ def cookbook_gem_installing(gem, version)
+ end
+
+ # Called when the result of installing the bundle is to use the gem
+ def cookbook_gem_using(gem, version)
+ end
+
+ # Called when finished installing cookbook gems
+ def cookbook_gem_finished
+ end
+
+ # Called when cookbook gem installation fails
+ def cookbook_gem_failed(exception)
+ end
+
## TODO: add cookbook name to the API for file load callbacks
## TODO: add callbacks for overall cookbook eval start and complete.
diff --git a/lib/chef/formatters/doc.rb b/lib/chef/formatters/doc.rb
index 5462241049..b7c65b9739 100644
--- a/lib/chef/formatters/doc.rb
+++ b/lib/chef/formatters/doc.rb
@@ -180,6 +180,31 @@ class Chef
unindent
end
+ # Called when starting to collect gems from the cookbooks
+ def cookbook_gem_start(gems)
+ puts_line "Installing Cookbook Gems:"
+ indent
+ end
+
+ # Called when the result of installing the bundle is to install the gem
+ def cookbook_gem_installing(gem, version)
+ puts_line "- Installing #{gem} #{version}", :green
+ end
+
+ # Called when the result of installing the bundle is to use the gem
+ def cookbook_gem_using(gem, version)
+ puts_line "- Using #{gem} #{version}"
+ end
+
+ # Called when finished installing cookbook gems
+ def cookbook_gem_finished
+ unindent
+ end
+
+ def cookbook_gem_failed(exception)
+ unindent
+ end
+
# Called when cookbook loading starts.
def library_load_start(file_count)
puts_line "Compiling Cookbooks..."
diff --git a/lib/chef/policy_builder/expand_node_object.rb b/lib/chef/policy_builder/expand_node_object.rb
index e3d136684b..de6d7ccee3 100644
--- a/lib/chef/policy_builder/expand_node_object.rb
+++ b/lib/chef/policy_builder/expand_node_object.rb
@@ -3,7 +3,7 @@
# Author:: Tim Hinderliter (<tim@chef.io>)
# Author:: Christopher Walters (<cw@chef.io>)
# Author:: Daniel DeLeo (<dan@chef.io>)
-# Copyright:: Copyright 2008-2016, Chef Software, Inc.
+# Copyright:: Copyright 2008-2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -75,7 +75,7 @@ class Chef
cl.load_cookbooks
cookbook_collection = Chef::CookbookCollection.new(cl)
cookbook_collection.validate!
- cookbook_collection.install_gems
+ cookbook_collection.install_gems(events)
run_context = Chef::RunContext.new(node, cookbook_collection, @events)
else
@@ -83,7 +83,7 @@ class Chef
cookbook_hash = sync_cookbooks
cookbook_collection = Chef::CookbookCollection.new(cookbook_hash)
cookbook_collection.validate!
- cookbook_collection.install_gems
+ cookbook_collection.install_gems(events)
run_context = Chef::RunContext.new(node, cookbook_collection, @events)
end
diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb
index 12268d426a..af6c0da511 100644
--- a/lib/chef/policy_builder/policyfile.rb
+++ b/lib/chef/policy_builder/policyfile.rb
@@ -3,7 +3,7 @@
# Author:: Tim Hinderliter (<tim@chef.io>)
# Author:: Christopher Walters (<cw@chef.io>)
# Author:: Daniel DeLeo (<dan@chef.io>)
-# Copyright:: Copyright 2008-2016, Chef Software, Inc.
+# Copyright:: Copyright 2008-2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -153,7 +153,7 @@ class Chef
sync_cookbooks
cookbook_collection = Chef::CookbookCollection.new(cookbooks_to_sync)
cookbook_collection.validate!
- cookbook_collection.install_gems
+ cookbook_collection.install_gems(events)
run_context = Chef::RunContext.new(node, cookbook_collection, events)