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/vault.rb,
lib/bolt/plugin/module.rb,
lib/bolt/plugin/prompt.rb,
lib/bolt/plugin/puppetdb.rb,
lib/bolt/plugin/terraform.rb,
lib/bolt/plugin/aws_inventory.rb,
lib/bolt/plugin/install_agent.rb

Defined Under Namespace

Classes: AwsInventory, InstallAgent, Module, Pkcs7, PluginContext, PluginError, Prompt, Puppetdb, Task, Terraform, Vault

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].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.



141
142
143
144
145
146
147
# File 'lib/bolt/plugin.rb', line 141

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

Instance Attribute Details

#palObject (readonly)

Returns the value of attribute pal.



139
140
141
# File 'lib/bolt/plugin.rb', line 139

def pal
  @pal
end

#plugin_contextObject (readonly)

Returns the value of attribute plugin_context.



139
140
141
# File 'lib/bolt/plugin.rb', line 139

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
133
134
135
# 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::AwsInventory')
  plugins.add_ruby_plugin('Bolt::Plugin::InstallAgent')
  plugins.add_ruby_plugin('Bolt::Plugin::Task')
  plugins.add_ruby_plugin('Bolt::Plugin::Terraform')
  plugins.add_ruby_plugin('Bolt::Plugin::Pkcs7')
  plugins.add_ruby_plugin('Bolt::Plugin::Prompt')
  plugins.add_ruby_plugin('Bolt::Plugin::Vault')

  plugins
end

Instance Method Details

#add_from_configObject



182
183
184
185
186
# File 'lib/bolt/plugin.rb', line 182

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

#add_module_plugin(plugin_name) ⇒ Object



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

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



154
155
156
# File 'lib/bolt/plugin.rb', line 154

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

#add_ruby_plugin(cls_name) ⇒ Object



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

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



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

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



188
189
190
# File 'lib/bolt/plugin.rb', line 188

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

#get_hook(plugin_name, hook) ⇒ Object



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

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) if BUILTIN_PLUGINS.include?(plugin_name)

  plugin.method(hook)
end

#modulesObject



149
150
151
# File 'lib/bolt/plugin.rb', line 149

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