Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load needed (dynamic) dependencies for provisioners at creation time. #358

Merged
merged 3 commits into from Feb 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 30 additions & 8 deletions lib/kitchen/command/diagnose.rb
Expand Up @@ -31,20 +31,42 @@ module Command
class Diagnose < Kitchen::Command::Base

def call
loader = if options[:all] || options[:loader]
@loader
else
nil
end
instances = record_failure { load_instances }

loader = record_failure { load_loader }

puts Kitchen::Diagnostic.new(
:loader => loader, :instances => instances).read.to_yaml
end

private

instances = if options[:all] || options[:instances]
def load_instances
if options[:all] || options[:instances]
parse_subcommand(args.first)
else
[]
end
end

puts Kitchen::Diagnostic.new(
:loader => loader, :instances => instances).read.to_yaml
def load_loader
if options[:all] || options[:loader]
@loader
else
nil
end
end

def record_failure
yield
rescue => e
{
:error => {
:exception => e.inspect,
:message => e.message,
:backtrace => e.backtrace
}
}
end
end
end
Expand Down
16 changes: 14 additions & 2 deletions lib/kitchen/diagnostic.rb
Expand Up @@ -50,12 +50,24 @@ def prepare_common
end

def prepare_loader
result[:loader] = loader.diagnose if loader
if error_hash?(loader)
result[:loader] = loader
else
result[:loader] = loader.diagnose if loader
end
end

def prepare_instances
result[:instances] = Hash.new
Array(instances).each { |i| result[:instances][i.name] = i.diagnose }
if error_hash?(instances)
result[:instances][:error] = instances[:error]
else
Array(instances).each { |i| result[:instances][i.name] = i.diagnose }
end
end

def error_hash?(obj)
obj.is_a?(Hash) && obj.has_key?(:error)
end
end
end
3 changes: 2 additions & 1 deletion lib/kitchen/errors.rb
Expand Up @@ -122,7 +122,8 @@ def self.handle_instance_failure(e)

def self.handle_error(e)
stderr_log(Error.formatted_exception(e))
stderr_log("Please see .kitchen/logs/kitchen.log for more details\n")
stderr_log("Please see .kitchen/logs/kitchen.log for more details")
stderr_log("Also try running `kitchen diagnose --all` for configuration\n")
file_log(:error, Error.formatted_trace(e))
end
end
3 changes: 3 additions & 0 deletions lib/kitchen/provisioner/base.rb
Expand Up @@ -41,6 +41,7 @@ def initialize(config = {})
def instance=(instance)
@instance = instance
expand_paths!
load_needed_dependencies!
end

# Returns the name of this driver, suitable for display in a CLI.
Expand Down Expand Up @@ -128,6 +129,8 @@ def expand_paths!
end
end

def load_needed_dependencies! ; end

def logger
instance ? instance.logger : Kitchen.logger
end
Expand Down
22 changes: 16 additions & 6 deletions lib/kitchen/provisioner/chef/berkshelf.rb
Expand Up @@ -39,12 +39,15 @@ def initialize(berksfile, path, logger = Kitchen.logger)
@logger = logger
end

def self.load!(logger = Kitchen.logger)
load_berkshelf!(logger)
end

def resolve
info("Resolving cookbook dependencies with Berkshelf...")
version = ::Berkshelf::VERSION
info("Resolving cookbook dependencies with Berkshelf #{version}...")
debug("Using Berksfile from #{berksfile}")

load_berkshelf!

::Berkshelf.ui.mute do
if ::Berkshelf::Berksfile.method_defined?(:vendor)
# Berkshelf 3.0 requires the directory to not exist
Expand All @@ -60,10 +63,17 @@ def resolve

attr_reader :berksfile, :path, :logger

def load_berkshelf!
require 'berkshelf'
def self.load_berkshelf!(logger)
first_load = require 'berkshelf'

version = ::Berkshelf::VERSION
if first_load
logger.debug("Berkshelf #{version} library loaded")
else
logger.debug("Berkshelf #{version} previously loaded")
end
rescue LoadError => e
fatal("The `berkshelf' gem is missing and must be installed" +
logger.fatal("The `berkshelf' gem is missing and must be installed" +
" or cannot be properly activated. Run" +
" `gem install berkshelf` or add the following to your" +
" Gemfile if you are using Bundler: `gem 'berkshelf'`.")
Expand Down
23 changes: 17 additions & 6 deletions lib/kitchen/provisioner/chef/librarian.rb
Expand Up @@ -33,18 +33,22 @@ class Librarian

include Logging


def initialize(cheffile, path, logger = Kitchen.logger)
@cheffile = cheffile
@path = path
@logger = logger
end

def self.load!(logger = Kitchen.logger)
load_librarian!(logger)
end

def resolve
info("Resolving cookbook dependencies with Librarian-Chef")
version = ::Librarian::Chef::VERSION
info("Resolving cookbook dependencies with Librarian-Chef #{version}...")
debug("Using Cheffile from #{cheffile}")

load_librarian!

env = ::Librarian::Chef::Environment.new(
:project_path => File.dirname(cheffile))
env.config_db.local["path"] = path
Expand All @@ -54,12 +58,19 @@ def resolve

attr_reader :cheffile, :path, :logger

def load_librarian!
require 'librarian/chef/environment'
def self.load_librarian!(logger)
first_load = require 'librarian/chef/environment'
require 'librarian/action/resolve'
require 'librarian/action/install'

version = ::Librarian::Chef::VERSION
if first_load
logger.debug("Librarian-Chef #{version} library loaded")
else
logger.debug("Librarian-Chef #{version} previously loaded")
end
rescue LoadError => e
fatal("The `librarian-chef' gem is missing and must be installed" +
logger.fatal("The `librarian-chef' gem is missing and must be installed" +
" or cannot be properly activated. Run" +
" `gem install librarian-chef` or add the following to your" +
" Gemfile if you are using Bundler: `gem 'librarian-chef'`.")
Expand Down
10 changes: 10 additions & 0 deletions lib/kitchen/provisioner/chef_base.rb
Expand Up @@ -129,6 +129,16 @@ def create_sandbox

protected

def load_needed_dependencies!
if File.exists?(berksfile)
debug("Berksfile found at #{berksfile}, loading Berkshelf")
Chef::Berkshelf.load!(logger)
elsif File.exists?(cheffile)
debug("Cheffile found at #{cheffile}, loading Librarian-Chef")
Chef::Librarian.load!(logger)
end
end

def format_config_file(data)
data.each.map { |attr, value|
[attr, (value.is_a?(Array) ? value.to_s : %{"#{value}"})].join(" ")
Expand Down