-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmerge_ng_config_data.rb
61 lines (54 loc) · 2.26 KB
/
merge_ng_config_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
# @summary
# This function takes the name of a Node Group and a config data hash,
# returning the merge of the node group's current config data and the new
# information specified. It is intended to be used in conjunction with
# Deferred().
#
# @return
# Hash
#
# @example
# $data = Deferred('peadm::merge_ng_config_data', ['PE Master', $new_config_data])
#
Puppet::Functions.create_function(:'peadm::merge_ng_config_data') do
dispatch :merge_ng_config_data do
param 'String', :group_name
param 'Hash', :new_config_data
end
def merge_ng_config_data(group_name, new_config_data)
require_libs
ensure_config
ng = Puppet::Util::Nc_https.new
group = ng.get_groups.select { |g| g['name'] == group_name }.first
group['config_data'].deep_merge(new_config_data)
rescue StandardError => e
Puppet.warn "Error attempting to read and merge node_group config data for #{group_name}: #{e.message}"
new_config_data
end
def require_libs
require 'deep_merge'
# We are using utilities from the node_manager module. Load 'em up, trying
# hard to get at them even if simple requires don't seem to be working.
begin
require 'puppet/util/nc_https'
require 'puppet_x/node_manager/common'
rescue LoadError
mod = Puppet::Module.find('node_manager', Puppet[:environment].to_s)
require File.join mod.path, 'lib/puppet/util/nc_https'
require File.join mod.path, 'lib/puppet_x/node_manager/common'
end
end
def ensure_config
# Because of failings in the node_manager module, we have to do some jerry
# rigging to ensure this will work when running over `bolt apply`.
return if File.exist?("#{Puppet.settings['confdir']}/node_manager.yaml") ||
!File.exist?('/etc/puppetlabs/puppet/classifier.yaml')
config = YAML.load_file('/etc/puppetlabs/puppet/classifier.yaml').first
config['port'] = 4433
config['hostcert'] = "/etc/puppetlabs/puppet/ssl/certs/#{config['server']}.pem"
config['hostprivkey'] = "/etc/puppetlabs/puppet/ssl/private_keys/#{config['server']}.pem"
config['localcacert'] = '/etc/puppetlabs/puppet/ssl/certs/ca.pem'
File.open("#{Puppet.settings['confdir']}/node_manager.yaml", 'w') { |f| f.write(config.to_yaml) }
end
end