Skip to content

Commit af35b3a

Browse files
committed
(GH-1350) Allow entire plugin_hooks to be set with a reference
Previously, setting `plugin_hooks` to a `_plugin` reference would cause a failure because we would merge the default `plugin_hooks` with the reference before evaluating it. That caused the reference to receive arguments it didn't expect. We now evaluate the configured `plugin_hooks` key and then merge it over top of the defaults afterward.
1 parent 8b3d2bc commit af35b3a

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

lib/bolt/config.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def initialize(boltdir, config_data, overrides = {})
7171
@save_rerun = true
7272
@puppetfile_config = {}
7373
@plugins = {}
74-
@plugin_hooks = { 'puppet_library' => { 'plugin' => 'puppet_agent', 'stop_service' => true } }
74+
@plugin_hooks = {}
7575

7676
# add an entry for the default console logger
7777
@log = { 'console' => {} }
@@ -167,7 +167,7 @@ def update_from_file(data)
167167
@save_rerun = data['save-rerun'] if data.key?('save-rerun')
168168

169169
@plugins = data['plugins'] if data.key?('plugins')
170-
@plugin_hooks.merge!(data['plugin_hooks']) if data.key?('plugin_hooks')
170+
@plugin_hooks = data['plugin_hooks'] if data.key?('plugin_hooks')
171171

172172
%w[concurrency format puppetdb color].each do |key|
173173
send("#{key}=", data[key]) if data.key?(key)

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, (@plugins&.default_plugin_hooks || {}).merge(data['plugin_hooks'] || {}))
237+
set_plugin_hooks(target.name, (@plugins&.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
@@ -78,7 +78,7 @@ def set_feature(feature, value = true)
7878
def plugin_hooks
7979
# Merge plugin_hooks from the config file with any defined by the group
8080
# or assigned dynamically to the target
81-
@inventory.plugins.default_plugin_hooks.merge(group_cache['plugin_hooks']).merge(@plugin_hooks)
81+
@inventory.plugins.plugin_hooks.merge(group_cache['plugin_hooks']).merge(@plugin_hooks)
8282
end
8383

8484
def set_config(key_or_key_path, value)

lib/bolt/plugin.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,17 @@ def self.setup(config, pal, pdb_client, analytics)
132132
plugins.by_name(plugin)
133133
end
134134

135-
plugins.default_plugin_hooks = plugins.resolve_references(config.plugin_hooks)
135+
plugins.plugin_hooks.merge!(plugins.resolve_references(config.plugin_hooks))
136136

137137
plugins
138138
end
139139

140140
RUBY_PLUGINS = %w[install_agent task pkcs7 prompt].freeze
141141
BUILTIN_PLUGINS = %w[task terraform pkcs7 prompt vault aws_inventory puppetdb azure_inventory].freeze
142+
DEFAULT_PLUGIN_HOOKS = { 'puppet_library' => { 'plugin' => 'puppet_agent', 'stop_service' => true } }.freeze
142143

143144
attr_reader :pal, :plugin_context
144-
attr_accessor :default_plugin_hooks
145+
attr_accessor :plugin_hooks
145146

146147
private_class_method :new
147148

@@ -154,7 +155,7 @@ def initialize(config, pal, analytics)
154155
@unknown = Set.new
155156
@resolution_stack = []
156157
@unresolved_plugin_configs = config.plugins.dup
157-
@default_plugin_hooks = {}
158+
@plugin_hooks = DEFAULT_PLUGIN_HOOKS.dup
158159
end
159160

160161
def modules

spec/bolt/plugin_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,29 @@ def identity(value)
7070
'param' => identity('foobar')
7171
}
7272
}
73-
expect(plugins.default_plugin_hooks['puppet_library']).to eq('plugin' => 'my_hook', 'param' => 'foobar')
73+
expect(plugins.plugin_hooks['puppet_library']).to eq('plugin' => 'my_hook', 'param' => 'foobar')
74+
end
75+
76+
it 'allows the whole plugin_hooks value to be set with a reference' do
77+
hooks = {
78+
'another_hook' => {
79+
'plugin' => 'my_hook',
80+
'param' => identity('foobar')
81+
}
82+
}
83+
84+
config_data['plugin_hooks'] = identity(hooks)
85+
86+
expect(plugins.plugin_hooks).to eq(
87+
'another_hook' => {
88+
'plugin' => 'my_hook',
89+
'param' => 'foobar'
90+
},
91+
'puppet_library' => {
92+
'plugin' => 'puppet_agent',
93+
'stop_service' => true
94+
}
95+
)
7496
end
7597
end
7698
end

0 commit comments

Comments
 (0)