Skip to content

Commit 4652708

Browse files
nicklewisBolt Tests
authored and
Bolt Tests
committed
(GH-1350) Allow plugin_hooks to contain references
Previously, the value of `plugin_hooks` had to be static with all hooks and parameters being defined in the config file. We now allow these hooks and parameters to be looked up using plugins, which is helpful if an external source is responsible for controlling the Puppet version to use or if the Puppet install method requires the knowledge of some secret.
1 parent 356e3f9 commit 4652708

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

documentation/bolt_configuration_options.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,14 @@ plugin_hooks:
232232
master: 'puppet.example.com'
233233
cacert_content: <CERT>
234234
```
235+
236+
You can also configure `plugin_hooks` using `_plugin` references:
237+
238+
```yaml
239+
plugin_hooks:
240+
puppet_library:
241+
plugin: puppet_agent
242+
version:
243+
_plugin: prompt
244+
message: "Which version of Puppet do you want to install?"
245+
```

lib/bolt/inventory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def update_target(target)
234234
set_facts(target.name, data['facts']) unless @target_facts[target.name]
235235
data['features']&.each { |feature| set_feature(target, feature) } unless @target_features[target.name]
236236
unless @target_plugin_hooks[target.name]
237-
set_plugin_hooks(target.name, @config.plugin_hooks.merge(data['plugin_hooks'] || {}))
237+
set_plugin_hooks(target.name, (@plugins&.default_plugin_hooks || {}).merge(data['plugin_hooks'] || {}))
238238
end
239239

240240
# Use Config object to ensure config section is treated consistently with config file

lib/bolt/inventory/target.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def set_feature(feature, value = true)
8080
def plugin_hooks
8181
# Merge plugin_hooks from the config file with any defined by the group
8282
# or assigned dynamically to the target
83-
@inventory.config.plugin_hooks.merge(group_cache['plugin_hooks']).merge(@plugin_hooks)
83+
@inventory.plugins.default_plugin_hooks.merge(group_cache['plugin_hooks']).merge(@plugin_hooks)
8484
end
8585

8686
def set_config(key_or_key_path, value)

lib/bolt/plugin.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ def initialize(plugin_name, hook)
3939
end
4040

4141
class PluginContext
42-
def initialize(config, pal)
42+
def initialize(config, pal, plugins)
4343
@pal = pal
4444
@config = config
45+
@plugins = plugins
4546
end
4647

4748
def serial_executor
@@ -50,7 +51,7 @@ def serial_executor
5051
private :serial_executor
5152

5253
def empty_inventory
53-
@empty_inventory ||= Bolt::Inventory.new({}, @config)
54+
@empty_inventory ||= Bolt::Inventory::Inventory2.new({}, @config, plugins: @plugins)
5455
end
5556
private :empty_inventory
5657

@@ -131,25 +132,29 @@ def self.setup(config, pal, pdb_client, analytics)
131132
plugins.by_name(plugin)
132133
end
133134

135+
plugins.default_plugin_hooks = plugins.resolve_references(config.plugin_hooks)
136+
134137
plugins
135138
end
136139

137140
RUBY_PLUGINS = %w[install_agent task pkcs7 prompt].freeze
138141
BUILTIN_PLUGINS = %w[task terraform pkcs7 prompt vault aws_inventory puppetdb azure_inventory].freeze
139142

140143
attr_reader :pal, :plugin_context
144+
attr_accessor :default_plugin_hooks
141145

142146
private_class_method :new
143147

144148
def initialize(config, pal, analytics)
145149
@config = config
146150
@analytics = analytics
147-
@plugin_context = PluginContext.new(config, pal)
151+
@plugin_context = PluginContext.new(config, pal, self)
148152
@plugins = {}
149153
@pal = pal
150154
@unknown = Set.new
151155
@resolution_stack = []
152156
@unresolved_plugin_configs = config.plugins.dup
157+
@default_plugin_hooks = {}
153158
end
154159

155160
def modules

spec/bolt/inventory_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def common_data(transport)
759759
}] }
760760
}
761761

762-
let(:inventory) { Bolt::Inventory.new(data) }
762+
let(:inventory) { Bolt::Inventory.new(data, plugins: plugins) }
763763
let(:target) { get_target(inventory, 'foo') }
764764
let(:expected_data) {
765765
{ 'name' => 'foo',

spec/bolt/plugin_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,16 @@ def identity(value)
6161
expect { plugins }.to raise_error(/Configuration for plugin 'pkcs7' depends on the plugin itself/)
6262
end
6363
end
64+
65+
context 'loading plugin_hooks' do
66+
it 'evaluates plugin references in the plugin_hooks configuration' do
67+
config_data['plugin_hooks'] = {
68+
'puppet_library' => {
69+
'plugin' => 'my_hook',
70+
'param' => identity('foobar')
71+
}
72+
}
73+
expect(plugins.default_plugin_hooks['puppet_library']).to eq('plugin' => 'my_hook', 'param' => 'foobar')
74+
end
75+
end
6476
end

0 commit comments

Comments
 (0)