Class: Berkshelf::Vagrant::ChefConfig

Inherits:
Vagrant::Util::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/berkshelf/vagrant/chef_config.rb

Defined Under Namespace

Classes: Proxy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents = "", path = "") ⇒ ChefConfig

Returns a new instance of ChefConfig.



52
53
54
# File 'lib/berkshelf/vagrant/chef_config.rb', line 52

def initialize(contents = "", path = "")
  instance_eval(contents, path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



68
69
70
# File 'lib/berkshelf/vagrant/chef_config.rb', line 68

def method_missing(m, *args, &block)
  Proxy.new
end

Class Method Details

.from_file(file = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/berkshelf/vagrant/chef_config.rb', line 37

def from_file(file = nil)
  file = file.nil? ? location : file
  if !file.nil? && File.exist?(file) && File.readable?(file)
    contents = File.read(file)
    return parse(contents, file)
  end

  new
end

.instanceObject



33
34
35
# File 'lib/berkshelf/vagrant/chef_config.rb', line 33

def instance
  @instance ||= from_file(location)
end

.locationString?

Return the most sensible path to the Chef configuration file. This can be configured by setting a value for the ‘CHEF_CONFIG’ environment variable.

Returns:

  • (String, nil)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/berkshelf/vagrant/chef_config.rb', line 12

def location
  possibles = []

  possibles << ENV['CHEF_CONFIG'] if ENV['CHEF_CONFIG']
  possibles << File.join(ENV['KNIFE_HOME'], 'knife.rb') if ENV['KNIFE_HOME']
  possibles << File.join(Dir.pwd, 'knife.rb')

  # Ascending search for .chef directory siblings
  Pathname.new(Dir.pwd).ascend do |file|
    sibling_chef = File.join(file, '.chef')
    possibles << File.join(sibling_chef, 'knife.rb')
  end

  possibles << File.join(ENV['HOME'], '.chef', 'knife.rb') if ENV['HOME']
  possibles.compact!

  location = possibles.find { |loc| File.exists?(File.expand_path(loc)) }

  File.expand_path(location) unless location.nil?
end

.parse(contents, path = nil) ⇒ Object



47
48
49
# File 'lib/berkshelf/vagrant/chef_config.rb', line 47

def parse(contents, path = nil)
  new(contents, path)
end

Instance Method Details

#chef_server_url(value = nil) ⇒ Object



56
57
58
# File 'lib/berkshelf/vagrant/chef_config.rb', line 56

def chef_server_url(value = nil)
  set_or_return(:chef_server_url, value)
end

#client_key(value = nil) ⇒ Object



60
61
62
# File 'lib/berkshelf/vagrant/chef_config.rb', line 60

def client_key(value = nil)
  set_or_return(:client_key, value)
end

#node_name(value = nil) ⇒ Object



64
65
66
# File 'lib/berkshelf/vagrant/chef_config.rb', line 64

def node_name(value = nil)
  set_or_return(:node_name, value)
end