Class: ChefWorkflow::KnifeSupport

Inherits:
Object
  • Object
show all
Extended by:
AttrSupport
Includes:
DebugSupport, GenericSupport
Defined in:
lib/chef-workflow/support/knife.rb

Overview

Configuration class for chef tooling and SSH interaction. Uses ‘GenericSupport`.

Constant Summary collapse

DEFAULTS =

defaults, yo

{
  :search_index_wait      => 60,
  :cookbooks_path         => File.join(Dir.pwd, 'cookbooks'),
  :chef_config_path       => File.join(ChefWorkflow::GeneralSupport.workflow_dir, 'chef'),
  :knife_config_path      => File.join(ChefWorkflow::GeneralSupport.workflow_dir, 'chef', 'knife.rb'),
  :roles_path             => File.join(Dir.pwd, 'roles'),
  :environments_path      => File.join(Dir.pwd, 'environments'),
  :data_bags_path         => File.join(Dir.pwd, 'data_bags'),
  :ssh_user               => "vagrant",
  :ssh_password           => "vagrant",
  :ssh_identity_file      => nil,
  :use_sudo               => true,
  :test_environment       => "vagrant",
  :test_recipes           => []
}

Constants included from DebugSupport

DebugSupport::CHEF_WORKFLOW_DEBUG_DEFAULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttrSupport

fancy_attr

Methods included from GenericSupport

included

Methods included from DebugSupport

#if_debug

Constructor Details

#initializeKnifeSupport

Returns a new instance of KnifeSupport.



50
51
52
53
54
55
56
# File 'lib/chef-workflow/support/knife.rb', line 50

def initialize
  @attributes = { }

  DEFAULTS.each do |key, value|
    add_attribute(key, value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef-workflow/support/knife.rb', line 70

def method_missing(sym, *args)
  attr_name = sym.to_s.sub(/=$/, '').to_sym
  assignment = sym.to_s.end_with?("=")

  if @attributes.has_key?(attr_name)
    if assignment or args.count > 0
      @attributes[attr_name] = args[0]
    else
      @attributes[attr_name]
    end
  else
    raise "Attribute #{attr_name} does not exist"
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



48
49
50
# File 'lib/chef-workflow/support/knife.rb', line 48

def attributes
  @attributes
end

Instance Method Details

#add_attribute(attr_name, default) ⇒ Object

Helper method to allow extensions to add attributes to this class. Takes an attribute name and a default which will be set initially, intended to be overridden by the user if necessary.

– FIXME Move all the user-configurable stuff to its own support system ++



66
67
68
# File 'lib/chef-workflow/support/knife.rb', line 66

def add_attribute(attr_name, default)
  @attributes[attr_name] = default
end

#build_knife_configObject

Writes out a knife.rb based on the settings in this configuration. Uses the ‘knife_config_path` and `chef_config_path` to determine where to write it.



89
90
91
92
# File 'lib/chef-workflow/support/knife.rb', line 89

def build_knife_config
  FileUtils.mkdir_p(chef_config_path)
  File.binwrite(knife_config_path, ERB.new(knife_config_template).result(binding))
end