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