class NodeSpec::Provisioning::Chef

Constants

ATTRIBUTES_FILENAME
CLIENT_CONFIG_FILENAME
NODES_DIRNAME

Public Class Methods

new(node) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 11
def initialize(node)
  @node = node
  @custom_attributes = {}
  @configuration_entries = []
end

Public Instance Methods

chef_apply_execute(snippet, options = []) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 17
def chef_apply_execute(snippet, options = [])
  @node.execute("chef-apply #{options.join(' ')} -e #{snippet.shellescape}")
end
chef_apply_recipe(recipe_file, options = []) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 21
def chef_apply_recipe(recipe_file, options = [])
  @node.execute("chef-apply #{recipe_file.shellescape} #{options.join(' ')}")
end
chef_client_config(text) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 36
def chef_client_config(text)
  @configuration_entries << text
end
chef_client_runlist(*args) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 40
def chef_client_runlist(*args)
  run_list_items, options = [], []
  run_list_items << args.take_while {|arg| arg.is_a? String}
  options += args.last if args.last.is_a? Array
  options << configuration_option
  options << attributes_option
  @node.execute("chef-client -z #{options.compact.join(' ')} -o #{run_list_items.join(',').shellescape}")
end
set_attributes(attributes) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 32
def set_attributes(attributes)
  @custom_attributes = attributes
end
set_cookbook_paths(*paths) click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 25
def set_cookbook_paths(*paths)
  unless paths.empty?
    paths_in_quotes = paths.map {|p| "'#{p}'"}
    @configuration_entries << %Q(cookbook_path [#{paths_in_quotes.join(",")}])
  end
end

Private Instance Methods

attributes_option() click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 60
def attributes_option
  unless @custom_attributes.empty?
    attr_file = @node.create_file(ATTRIBUTES_FILENAME, JSON.generate(@custom_attributes))
    "-j #{attr_file}"
  end
end
configuration_option() click to toggle source
# File lib/nodespec/provisioning/chef.rb, line 51
def configuration_option
  unless @configuration_entries.any? {|c| c =~ /^node_path .+$/}
    nodes_directory = @node.create_temp_directory(NODES_DIRNAME)
    @configuration_entries.unshift("node_path '#{nodes_directory}'")
  end
  config_file = @node.create_file(CLIENT_CONFIG_FILENAME, @configuration_entries.join("\n"))
  "-c #{config_file}"
end