Class: Bocuse::Project
- Inherits:
-
Object
- Object
- Bocuse::Project
- 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
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
Class Method Summary collapse
Instance Method Summary collapse
-
#evaluate(path) ⇒ void
Evaluates a file given by path.
-
#evaluate_all ⇒ void
Evaluates all files in the project.
-
#file(path) ⇒ File
Returns one of the projects files as a File.
-
#initialize(base_directory = nil) ⇒ Project
constructor
Initialize a bocuse project by specifying its base directory.
-
#lookup_template(name) ⇒ Object
Returns a template by name.
-
#nodes ⇒ Hash<name,Configuration>
Returns all nodes in this project as a hash.
-
#register_node(name, node) ⇒ Object
Registers a machine node in the project.
-
#register_template(path, template) ⇒ Object
Registers a template for project usage.
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_path ⇒ Object (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
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.
68 69 70 |
# File 'lib/bocuse/project.rb', line 68 def evaluate path file(path).evaluate end |
#evaluate_all ⇒ void
This method returns an undefined value.
Evaluates all files in the project.
Retrieve the found configurations via
Nodes.find pattern_or_name
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.
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 |
#nodes ⇒ Hash<name,Configuration>
Returns all nodes in this project as a hash.
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 |