diff --git a/README.md b/README.md index f042b21..4ab5f34 100644 --- a/README.md +++ b/README.md @@ -1,50 +1,79 @@ # Extlib module for Puppet [![Build Status](https://travis-ci.org/voxpupuli/puppet-extlib.png?branch=master)](https://travis-ci.org/voxpupuli/puppet-extlib) [![Puppet Forge](https://img.shields.io/puppetforge/v/puppet/extlib.svg)](https://forge.puppetlabs.com/puppet/extlib) [![Puppet Forge - downloads](https://img.shields.io/puppetforge/dt/puppet/extlib.svg)](https://forge.puppetlabs.com/puppet/extlib) [![Puppet Forge - endorsement](https://img.shields.io/puppetforge/e/puppet/extlib.svg)](https://forge.puppetlabs.com/puppet/extlib) [![Puppet Forge - scores](https://img.shields.io/puppetforge/f/puppet/extlib.svg)](https://forge.puppetlabs.com/puppet/extlib) #### Table of Contents 1. [Overview](#overview) 2. [Setup - The basics of getting started with extlib](#setup) 3. [Usage - Configuration options and additional functionality](#usage) 4. [Limitations - OS compatibility, etc.](#limitations) 5. [Development - Guide for contributing to the module](#development) ## Overview This module provides functions and facts that are out of scope for [stdlib](https://github.com/puppetlabs/puppetlabs-stdlib). Some of them are even intrinsically tied to stdlib. ## Setup ```console % puppet module install puppet-extlib ``` ## Usage Since `puppet/extlib` version 3, all functions are provided under the `extlib` namespace. Non namespaced versions were removed in version 5. All functions are documented in [REFERENCE.md](REFERENCE.md) +### Facts + +All facts in this module are namespaced and begin `extlib__`. +Facter 4 users can also find all facts under a single `extlib` toplevel structured fact. + +#### `extlib__puppet_config` (or `extlib['puppet_config']` when using facter 4). + +A fact to expose puppet.conf settings. These are resolved on the agent, (unlike `$settings::` which is resolved on the puppet master). + +The following sections/settings are included. + +``` +{ + main => { + hostpubkey, + hostprivkey, + hostcert, + localcacert, + ssldir, + vardir, + server, + }, + master => { + localcacert, + ssldir, + } +} +``` + ## Limitations Some functions require puppetlabs-stdlib (>= 4.6.0) and all functions are only compatible with Puppet 4.7 and later. ## Development We highly welcome new contributions to this module, especially those that include documentation, and rspec tests ;) but will happily guide you through the process, so, yes, please submit that pull request! Reference documentation is generated using puppet-strings. To regenerate it, please run the rake task as follows. ```console bundle exec rake reference ``` diff --git a/lib/facter/extlib__puppet_config.rb b/lib/facter/extlib__puppet_config.rb new file mode 100644 index 0000000..74254b5 --- /dev/null +++ b/lib/facter/extlib__puppet_config.rb @@ -0,0 +1,39 @@ +Facter.add(:extlib__puppet_config) do + setcode do + puppet_config = {} + + desired_settings = { + master: [ + :localcacert, + :ssldir + ], + main: [ + :hostpubkey, + :hostprivkey, + :hostcert, + :localcacert, + :ssldir, + :vardir, + :server + ] + } + + desired_settings.each_pair do |section, settings| + settings.each do |setting| + puppet_config[section.to_s] = {} unless puppet_config.key?(section.to_s) + puppet_config[section.to_s][setting.to_s] = Puppet.settings.values( + Puppet[:environment].to_sym, section + ).interpolate(setting) + end + end + + puppet_config + end +end + +# Facter 4 namespaced version +Facter.add(:'extlib.puppet_config') do + setcode do + Facter.value(:extlib__puppet_config) + end +end diff --git a/lib/facter/puppet_config.rb b/lib/facter/puppet_config.rb deleted file mode 100644 index f608e56..0000000 --- a/lib/facter/puppet_config.rb +++ /dev/null @@ -1,29 +0,0 @@ -desired_settings = { - master: [ - :localcacert, - :ssldir - ], - main: [ - :hostpubkey, - :hostprivkey, - :hostcert, - :localcacert, - :ssldir, - :vardir, - :server - ] -} -Facter.add(:puppet_config) do - puppet_config = {} - setcode do - desired_settings.each_pair do |section, settings| - settings.each do |setting| - puppet_config[section.to_s] = {} unless puppet_config.key?(section.to_s) - puppet_config[section.to_s][setting.to_s] = Puppet.settings.values( - Puppet[:environment].to_sym, section - ).interpolate(setting) - end - end - puppet_config - end -end diff --git a/spec/unit/facter/extlib__puppet_config_spec.rb b/spec/unit/facter/extlib__puppet_config_spec.rb new file mode 100644 index 0000000..868e9f4 --- /dev/null +++ b/spec/unit/facter/extlib__puppet_config_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' +require 'puppet' + +describe 'extlib__puppet_config fact' do + let(:hostname) { Puppet[:certname] } + let(:settings) do + { + 'main' => { + 'hostcert' => "/dev/null/ssl/certs/#{hostname}.pem", + 'hostprivkey' => "/dev/null/ssl/private_keys/#{hostname}.pem", + 'hostpubkey' => "/dev/null/ssl/public_keys/#{hostname}.pem", + 'localcacert' => '/dev/null/ssl/certs/ca.pem', + 'server' => 'puppet', + 'ssldir' => '/dev/null/ssl', + 'vardir' => '/dev/null' + }, + 'master' => { + 'localcacert' => '/dev/null/ssl/certs/ca.pem', + 'ssldir' => '/dev/null/ssl' + } + } + end + + it { expect(Facter.fact(:extlib__puppet_config).value).to eq(settings) } + + if Gem::Version.new(Gem.loaded_specs['facter'].version) >= Gem::Version.new('4') + it 'is also available under the toplevel `extlib` structured fact' do + expect(Facter.fact(:extlib).value).to include('puppet_config' => settings) + end + end +end diff --git a/spec/unit/facter/puppet_settings.rb b/spec/unit/facter/puppet_settings.rb deleted file mode 100644 index 4ec5be9..0000000 --- a/spec/unit/facter/puppet_settings.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'spec_helper' -require 'puppet' - -hostname = Puppet[:certname] -settings = { - 'main' => { - 'hostcert' => "/dev/null/ssl/certs/#{hostname}.pem", - 'hostprivkey' => "/dev/null/ssl/private_keys/#{hostname}.pem", - 'hostpubkey' => "/dev/null/ssl/public_keys/#{hostname}.pem", - 'localcacert' => '/dev/null/ssl/certs/ca.pem', - 'server' => 'puppet', - 'ssldir' => '/dev/null/ssl', - 'vardir' => '/dev/null' - }, - 'master' => { - 'localcacert' => '/dev/null/ssl/certs/ca.pem', - 'ssldir' => '/dev/null/ssl' - } -} -describe 'puppet_config' do - context 'when puppet' do - it 'puppet_config' do - expect(Facter.fact(:puppet_config).value).to eq(settings) - end - end -end