Class: Bocuse::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/bocuse/project.rb

Overview

A bocuse project comes in two flavours: simple and co-located with chef. The simple project is a directory with the subdirectories

nodes/
templates/
lib/

The nodes/ subdirectory contains all node descriptions, templates/ contains templates and lib/ is added to the load path and can contain code to help with construction of the configuration.

When you co-host bocuse with chef, normally you would store all these directories one level deeper. Here’s what your hierarchy should look like in this case:

config/
  nodes/
  templates/
  lib/

This class does most of the base path handling and of the path manipulation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_directory = nil) ⇒ Project

Initialize a bocuse project by specifying its base directory.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bocuse/project.rb', line 33

def initialize(base_directory=nil)
  base_directory = base_directory || Dir.pwd
    
  if chef_cohosted?(base_directory)
    base_directory = ::File.join(base_directory, 'config')
  end
  
  @base_path = Pathname.new(base_directory)
  @templates = Hash.new
  @nodes     = Hash.new
  
  # Make sure that project libraries can be loaded: 
  lib_dir = base_path.join('lib')
  $:.unshift lib_dir if ::File.directory?(lib_dir)
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



29
30
31
# File 'lib/bocuse/project.rb', line 29

def base_path
  @base_path
end

Class Method Details

.bocuse_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
# File 'lib/bocuse/project.rb', line 146

def bocuse_path?(path)
  %w(nodes).all? { |el| 
    ::File.directory?(::File.join(path, el)) }
end

Instance Method Details

#evaluate(path) ⇒ void

This method returns an undefined value.

Evaluates a file given by path.

Parameters:

  • path (String)

    file path, either absolute or relative to project base directory



68
69
70
# File 'lib/bocuse/project.rb', line 68

def evaluate path
  file(path).evaluate
end

#evaluate_allvoid

This method returns an undefined value.

Evaluates all files in the project.

Retrieve the found configurations via

Nodes.find pattern_or_name

Parameters:

  • dir (String)

    directory to use as project base directory



137
138
139
140
141
142
143
# File 'lib/bocuse/project.rb', line 137

def evaluate_all
  nodes_glob = base_path.join('nodes', '**', '*.rb')

  Dir[nodes_glob].each do |filename|
    file(filename).evaluate
  end
end

#file(path) ⇒ File

Returns one of the projects files as a File.

Parameters:

  • path (String)

    a relative or absolute file path

Returns:

  • (File)

    file at path below the project directory



54
55
56
57
58
59
60
# File 'lib/bocuse/project.rb', line 54

def file path
  path = Pathname.new(path)
  path = base_path.join(path) unless path.absolute?
  
  ctx = ProjectContext.new
  Bocuse::File.new(path, self, ctx)
end

#lookup_template(name) ⇒ Object

Returns a template by name. Official template names can be either absolute paths or relative paths to template files, but do NOT end in .rb.

foo               -> templates/foo.rb
/templates/bar    -> /usr/templates/bar.rb


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bocuse/project.rb', line 98

def lookup_template name
  unless @templates.has_key?(name.to_sym)
    file_name = name.to_s
    file_name += '.rb' unless file_name.end_with?('.rb')
    
    # Try to find and load this template: 
    template_path = base_path.join('templates', file_name)
    file(template_path).evaluate
    
    # A successful load will register the template.
  end
  
  @templates.fetch(name.to_sym)
end

#nodesHash<name,Configuration>

Returns all nodes in this project as a hash.

Returns:

  • (Hash<name,Configuration>)

    A hash mapping node names to their configuration objects.



124
125
126
127
# File 'lib/bocuse/project.rb', line 124

def nodes
  evaluate_all unless @nodes.size > 0
  @nodes
end

#register_node(name, node) ⇒ Object

Registers a machine node in the project.



115
116
117
# File 'lib/bocuse/project.rb', line 115

def register_node name, node
  @nodes.store name, node
end

#register_template(path, template) ⇒ Object

Registers a template for project usage.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bocuse/project.rb', line 74

def register_template path, template
  path = path.to_s
  template_base = base_path.join('templates/').to_s
  
  # Is this template path below base_path?
  if path.start_with?(template_base)
    path = path[template_base.size..-1]
  end
  
  # Does the path end in .rb? (most will)
  if path.end_with?('.rb')
    path = path.slice 0..-4
  end

  @templates.store path.to_sym, template
end