Class: Bolt::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/plugin.rb,
lib/bolt/plugin/task.rb,
lib/bolt/plugin/pkcs7.rb,
lib/bolt/plugin/module.rb,
lib/bolt/plugin/prompt.rb,
lib/bolt/plugin/puppetdb.rb,
lib/bolt/plugin/install_agent.rb

Defined Under Namespace

Classes: InstallAgent, Module, Pkcs7, PluginContext, PluginError, Prompt, Puppetdb, Task

Constant Summary collapse

KNOWN_HOOKS =
%i[
  puppet_library
  resolve_reference
  secret_encrypt
  secret_decrypt
  secret_createkeys
  validate_resolve_reference
].freeze
BUILTIN_PLUGINS =
%w[task terraform pkcs7 prompt vault aws_inventory puppetdb azure_inventory].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, pal, analytics) ⇒ Plugin

Returns a new instance of Plugin.



138
139
140
141
142
143
144
145
# File 'lib/bolt/plugin.rb', line 138

def initialize(config, pal, analytics)
  @config = config
  @analytics = analytics
  @plugin_context = PluginContext.new(config, pal)
  @plugins = {}
  @pal = pal
  @unknown = Set.new
end

Instance Attribute Details

#palObject (readonly)

Returns the value of attribute pal.



136
137
138
# File 'lib/bolt/plugin.rb', line 136

def pal
  @pal
end

#plugin_contextObject (readonly)

Returns the value of attribute plugin_context.



136
137
138
# File 'lib/bolt/plugin.rb', line 136

def plugin_context
  @plugin_context
end

Class Method Details

.setup(config, pal, pdb_client, analytics) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bolt/plugin.rb', line 121

def self.setup(config, pal, pdb_client, analytics)
  plugins = new(config, pal, analytics)
  # PDB is special do we want to expose the default client to the context?
  plugins.add_plugin(Bolt::Plugin::Puppetdb.new(pdb_client))

  plugins.add_ruby_plugin('Bolt::Plugin::InstallAgent')
  plugins.add_ruby_plugin('Bolt::Plugin::Task')
  plugins.add_ruby_plugin('Bolt::Plugin::Pkcs7')
  plugins.add_ruby_plugin('Bolt::Plugin::Prompt')

  plugins
end

Instance Method Details

#add_from_configObject



180
181
182
183
184
# File 'lib/bolt/plugin.rb', line 180

def add_from_config
  @config.plugins.keys.each do |plugin_name|
    by_name(plugin_name)
  end
end

#add_module_plugin(plugin_name) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/bolt/plugin.rb', line 170

def add_module_plugin(plugin_name)
  opts = {
    context: @plugin_context,
    config: config_for_plugin(plugin_name)
  }

  plugin = Bolt::Plugin::Module.load(plugin_name, modules, opts)
  add_plugin(plugin)
end

#add_plugin(plugin) ⇒ Object

Generally this is private. Puppetdb is special though



152
153
154
# File 'lib/bolt/plugin.rb', line 152

def add_plugin(plugin)
  @plugins[plugin.name] = plugin
end

#add_ruby_plugin(cls_name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bolt/plugin.rb', line 156

def add_ruby_plugin(cls_name)
  snake_name = Bolt::Util.class_name_to_file_name(cls_name)
  require snake_name
  cls = Kernel.const_get(cls_name)
  plugin_name = snake_name.split('/').last
  opts = {
    context: @plugin_context,
    config: config_for_plugin(plugin_name)
  }

  plugin = cls.new(**opts)
  add_plugin(plugin)
end

#by_name(plugin_name) ⇒ Object

Calling by_name or get_hook will load any module based plugin automatically



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/bolt/plugin.rb', line 200

def by_name(plugin_name)
  return @plugins[plugin_name] if @plugins.include?(plugin_name)
  begin
    unless @unknown.include?(plugin_name)
      add_module_plugin(plugin_name)
    end
  rescue PluginError::Unknown
    @unknown << plugin_name
    nil
  end
end

#config_for_plugin(plugin_name) ⇒ Object



186
187
188
# File 'lib/bolt/plugin.rb', line 186

def config_for_plugin(plugin_name)
  @config.plugins[plugin_name] || {}
end

#get_hook(plugin_name, hook) ⇒ Object



190
191
192
193
194
195
196
197
# File 'lib/bolt/plugin.rb', line 190

def get_hook(plugin_name, hook)
  plugin = by_name(plugin_name)
  raise PluginError::Unknown, plugin_name unless plugin
  raise PluginError::UnsupportedHook.new(plugin_name, hook) unless plugin.hooks.include?(hook)
  @analytics.report_bundled_content("Plugin #{hook}", plugin_name)

  plugin.method(hook)
end

#modulesObject



147
148
149
# File 'lib/bolt/plugin.rb', line 147

def modules
  @modules ||= Bolt::Module.discover(@pal.modulepath)
end