Class: MotherBrain::Plugin

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, cookbook_versions = {}, &block) ⇒ Plugin

Returns a new instance of Plugin.

Parameters:

  • metadata (MB::CookbookMetadata)
  • cookbook_versions (Hash<String, String>) (defaults to: {})

    The cookbook dependencies which this plugin depends on. Key is the cookbook name and the value is the version of the cookbook.



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

#commandsSet<MB::Command> (readonly)

Returns:



67
68
69
# File 'lib/mb/plugin.rb', line 67

def commands
  @commands
end

#componentsSet<MB::Component> (readonly)

Returns:



65
66
67
# File 'lib/mb/plugin.rb', line 65

def components
  @components
end

#cookbook_versionsHash<String, String> (readonly)

Returns:

  • (Hash<String, String>)


69
70
71
# File 'lib/mb/plugin.rb', line 69

def cookbook_versions
  @cookbook_versions
end

#metadataMB::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.

Parameters:

  • path (#to_s)

    a path to a directory containing a motherbrain plugin file and cookbook metadata file

Returns:

Raises:



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).message
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

Parameters:

  • metadata (MB::CookbookMetadata)
  • cookbook_versions (Hash<String, String>) (defaults to: {})

    The cookbook dependencies which this plugin depends on. Key is the cookbook name and the value is the version of the cookbook.

Yield Returns:

Raises:



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

Parameters:



208
209
210
# File 'lib/mb/plugin.rb', line 208

def add_command(command)
  self.commands.add(command)
end

#add_component(component) ⇒ Object

Parameters:



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.

Parameters:

  • name (#to_s)

    name of the command to find and return

Returns:



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.

Parameters:

  • name (#to_s)

    name of the command to find and return

Returns:

Raises:

  • (CommandNotFound)

    if a command matching the given name is not found on this plugin



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?

Parameters:

Returns:



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

Parameters:

Returns:

Raises:

  • (ComponentNotFound)

    if a component of the given name is not a part of this plugin



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

Returns:

  • (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

Parameters:

Returns:

  • (Boolean)


124
125
126
# File 'lib/mb/plugin.rb', line 124

def has_component?(name)
  component(name).present?
end

#idSymbol

Returns:

  • (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.

Parameters:

  • errors (Hash)

Returns:

  • (String)


235
236
237
238
239
240
241
242
243
# File 'lib/mb/plugin.rb', line 235

def messages_from_errors(errors)
  buffer = []

  errors.each do |attribute, messages|
    buffer |= messages
  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.

Examples:


{
  "activemq" => {
    database_masters" => [
      {
        "name" => "db-master1",
        ...
      }
    ],
    "database_slaves" => [
      {
        "name" => "db-slave1",
        ...
      },
      {
        "name" => "db-slave2",
        ...
      }
    ]
  }
}

Parameters:

  • environment (#to_s)

Returns:

  • (Hash)

Raises:



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_hashObject



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

Parameters:

  • options (Hash) (defaults to: {})

    a set of options to pass to MultiJson.encode

Returns:

  • (String)


280
281
282
# File 'lib/mb/plugin.rb', line 280

def to_json(options = {})
  MultiJson.encode(self.to_hash, options)
end

#to_sObject



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

Returns:

  • (self)


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: messages_from_errors(errors)
  end

  self
end