Class: MotherBrain::Plugin
- Inherits:
-
Object
- Object
- MotherBrain::Plugin
- Extended by:
- Forwardable
- Includes:
- Comparable, VariaModel
- Defined in:
- lib/mb/plugin.rb
Defined Under Namespace
Classes: CleanRoom
Constant Summary collapse
- NODE_GROUP_ID_REGX =
/^(.+)::(.+)$/.freeze
- PLUGIN_FILENAME =
'motherbrain.rb'.freeze
Instance Attribute Summary collapse
- #commands ⇒ Set<MB::Command> readonly
- #components ⇒ Set<MB::Component> readonly
- #cookbook_versions ⇒ Hash<String, String> readonly
- #metadata ⇒ MB::CookbookMetadata readonly
Class Method Summary collapse
-
.from_path(path) ⇒ MotherBrain::Plugin
Load the contents of a directory into an instance of MB::Plugin.
- .key_for(name, version) ⇒ Object
-
.load(metadata, cookbook_versions = {}, &block) ⇒ Object
Create a new plugin instance from the given content.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add_command(command) ⇒ Object
- #add_component(component) ⇒ Object
-
#command(name) ⇒ MB::Command?
Return a command from the plugins list of commands.
-
#command!(name) ⇒ MB::Command
Return a command from the plugin’s list of commands.
- #component(name) ⇒ MB::Component?
- #component!(name) ⇒ MB::Component
- #eql?(other) ⇒ Boolean
- #has_component?(name) ⇒ Boolean
- #id ⇒ Symbol
-
#initialize(metadata, cookbook_versions = {}, &block) ⇒ Plugin
constructor
A new instance of Plugin.
-
#messages_from_errors(errors) ⇒ String
Creates an error message from an error hash, where the keys are attributes and the values are an array of error messages.
-
#nodes(environment) ⇒ Hash
Finds the nodes for the given environment for each Component of the plugin groups them by Component#name and Group#name into a Hash where the keys are Component#name and values are a hash where the keys are Group#name and the values are a Hash representing a node from Chef.
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ String (also: #as_json)
- #to_s ⇒ Object
-
#validate! ⇒ self
Completely validate a loaded plugin and raise an exception of errors.
Constructor Details
#initialize(metadata, cookbook_versions = {}, &block) ⇒ Plugin
Returns a new instance of Plugin.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mb/plugin.rb', line 83 def initialize(, cookbook_versions = {}, &block) @metadata = @cookbook_versions = cookbook_versions @components = Set.new @commands = Set.new if block_given? dsl_eval(&block) end end |
Instance Attribute Details
#commands ⇒ Set<MB::Command> (readonly)
67 68 69 |
# File 'lib/mb/plugin.rb', line 67 def commands @commands end |
#components ⇒ Set<MB::Component> (readonly)
65 66 67 |
# File 'lib/mb/plugin.rb', line 65 def components @components end |
#cookbook_versions ⇒ Hash<String, String> (readonly)
69 70 71 |
# File 'lib/mb/plugin.rb', line 69 def cookbook_versions @cookbook_versions end |
#metadata ⇒ MB::CookbookMetadata (readonly)
63 64 65 |
# File 'lib/mb/plugin.rb', line 63 def @metadata end |
Class Method Details
.from_path(path) ⇒ MotherBrain::Plugin
Load the contents of a directory into an instance of MB::Plugin
The cookbook dependencies of this plugin will be loaded from the Berksfile.lock if it is found.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mb/plugin.rb', line 32 def from_path(path) unless Dir.has_mb_plugin?(path) raise PluginLoadError, "Expected a motherbrain and metadata file at: #{path}" end plugin_filename = File.join(path, PLUGIN_FILENAME) plugin_contents = File.read(plugin_filename) = CookbookMetadata.from_path(path) berksfile_lock = Berkshelf::Lockfile.from_path(path) load(, berksfile_lock.locked_versions) { eval(plugin_contents, binding, plugin_filename, 1) } rescue PluginSyntaxError => ex raise PluginSyntaxError, ErrorHandler.new(ex, file_path: plugin_filename). end |
.key_for(name, version) ⇒ Object
47 48 49 |
# File 'lib/mb/plugin.rb', line 47 def key_for(name, version) "#{name}-#{version}".to_sym end |
.load(metadata, cookbook_versions = {}, &block) ⇒ Object
Create a new plugin instance from the given content
14 15 16 17 18 |
# File 'lib/mb/plugin.rb', line 14 def load(, cookbook_versions = {}, &block) new(, cookbook_versions, &block).validate! rescue PluginSyntaxError => ex ErrorHandler.wrap(ex) end |
Instance Method Details
#<=>(other) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/mb/plugin.rb', line 245 def <=>(other) unless other.is_a?(self.class) return 0 end if self.name == other.name self.version <=> other.version else self.name <=> other.name end end |
#add_command(command) ⇒ Object
208 209 210 |
# File 'lib/mb/plugin.rb', line 208 def add_command(command) self.commands.add(command) end |
#add_component(component) ⇒ Object
203 204 205 |
# File 'lib/mb/plugin.rb', line 203 def add_component(component) self.components.add(component) end |
#command(name) ⇒ MB::Command?
Return a command from the plugins list of commands.
134 135 136 |
# File 'lib/mb/plugin.rb', line 134 def command(name) commands.find { |command| command.name == name.to_s } end |
#command!(name) ⇒ MB::Command
Return a command from the plugin’s list of commands. If a command is not found an exception will be rasied.
146 147 148 149 150 151 152 153 154 |
# File 'lib/mb/plugin.rb', line 146 def command!(name) found = command(name) if found.nil? raise CommandNotFound.new(name, self) end found end |
#component(name) ⇒ MB::Component?
102 103 104 |
# File 'lib/mb/plugin.rb', line 102 def component(name) components.find { |component| component.name == name.to_s } end |
#component!(name) ⇒ MB::Component
111 112 113 114 115 116 117 118 119 |
# File 'lib/mb/plugin.rb', line 111 def component!(name) component = component(name) if component.nil? raise ComponentNotFound.new(name, self) end component end |
#eql?(other) ⇒ Boolean
257 258 259 |
# File 'lib/mb/plugin.rb', line 257 def eql?(other) other.is_a?(self.class) && self == other end |
#has_component?(name) ⇒ Boolean
124 125 126 |
# File 'lib/mb/plugin.rb', line 124 def has_component?(name) component(name).present? end |
#id ⇒ Symbol
95 96 97 |
# File 'lib/mb/plugin.rb', line 95 def id self.class.key_for(self.name, self.version) end |
#messages_from_errors(errors) ⇒ String
Creates an error message from an error hash, where the keys are attributes and the values are an array of error messages.
235 236 237 238 239 240 241 242 243 |
# File 'lib/mb/plugin.rb', line 235 def (errors) buffer = [] errors.each do |attribute, | buffer |= end buffer.join "\n" end |
#nodes(environment) ⇒ Hash
Finds the nodes for the given environment for each Component of the plugin groups them by Component#name and Group#name into a Hash where the keys are Component#name and values are a hash where the keys are Group#name and the values are a Hash representing a node from Chef.
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mb/plugin.rb', line 190 def nodes(environment) unless Application.ridley.environment.find(environment) raise EnvironmentNotFound.new(environment) end {}.tap do |nodes| self.components.each do |component| nodes[component.name] = component.nodes(environment) end end end |
#to_hash ⇒ Object
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/mb/plugin.rb', line 265 def to_hash { name: name, version: version, maintainer: maintainer, maintainer_email: maintainer_email, description: description, long_description: long_description } end |
#to_json(options = {}) ⇒ String Also known as: as_json
280 281 282 |
# File 'lib/mb/plugin.rb', line 280 def to_json( = {}) MultiJson.encode(self.to_hash, ) end |
#to_s ⇒ Object
261 262 263 |
# File 'lib/mb/plugin.rb', line 261 def to_s "#{self.name} (#{self.version})" end |
#validate! ⇒ self
Completely validate a loaded plugin and raise an exception of errors
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/mb/plugin.rb', line 215 def validate! errors = validate if errors.any? ErrorHandler.wrap PluginSyntaxError, backtrace: [], plugin_name: try(:name), plugin_version: try(:version), text: (errors) end self end |