Class: OctofactsUpdater::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/octofacts_updater/plugin.rb

Class Method Summary collapse

Class Method Details

.clear!(plugin_name) ⇒ Object

Clear out a plugin definition. (Useful for testing.)

plugin_name - The name of the plugin to clear.



44
45
46
47
# File 'lib/octofacts_updater/plugin.rb', line 44

def self.clear!(plugin_name)
  @plugins ||= {}
  @plugins.delete(plugin_name.to_sym)
end

.execute(plugin_name, fact, args = {}, all_facts = {}) ⇒ Object

Execute a plugin

plugin_name - A Symbol which is the name of the plugin. fact - An OctofactsUpdater::Fact object args - An optional Hash of additional configuration arguments all_facts - A Hash of all of the facts

Returns nothing, but may adjust the “fact”



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/octofacts_updater/plugin.rb', line 28

def self.execute(plugin_name, fact, args = {}, all_facts = {})
  unless @plugins.key?(plugin_name.to_sym)
    raise NoMethodError, "A plugin named #{plugin_name} could not be found."
  end

  begin
    @plugins[plugin_name.to_sym].call(fact, args, all_facts)
  rescue => e
    warn "#{e.class} occurred executing #{plugin_name} on #{fact.name} with value #{fact.value.inspect}"
    raise e
  end
end

.pluginsObject

Get the plugins hash.



50
51
52
# File 'lib/octofacts_updater/plugin.rb', line 50

def self.plugins
  @plugins
end

.randomize_long_string(string_in) ⇒ Object

Randomize a long string. This method accepts a string (consisting of, for example, a SSH key) and returns a string of the same length, but with randomized characters.

string_in - A String with the original fact value.

Returns a String with the same length as string_in.



64
65
66
67
68
69
70
# File 'lib/octofacts_updater/plugin.rb', line 64

def self.randomize_long_string(string_in)
  seed = Digest::SHA512.hexdigest(string_in).to_i(36)

  prng = Random.new(seed)
  chars = [("a".."z"), ("A".."Z"), ("0".."9")].flat_map(&:to_a)
  (1..(string_in.length)).map { chars[prng.rand(chars.length)] }.join
end

.register(plugin_name, &block) ⇒ Object

Register a plugin.

plugin_name - A Symbol which is the name of the plugin. block - A block of code that constitutes the plugin. See sample plugins for expected format.



12
13
14
15
16
17
18
# File 'lib/octofacts_updater/plugin.rb', line 12

def self.register(plugin_name, &block)
  @plugins ||= {}
  if @plugins.key?(plugin_name.to_sym)
    raise ArgumentError, "A plugin named #{plugin_name} is already registered."
  end
  @plugins[plugin_name.to_sym] = block
end