Showing with 35 additions and 23 deletions.
  1. +8 −0 CHANGELOG.md
  2. +26 −22 lib/facter/pe_status_check_role.rb
  3. +1 −1 metadata.json
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).

## [v2.0.1](https://github.com/puppetlabs/puppetlabs-pe_status_check/tree/v2.0.1) (2022-06-14)

[Full Changelog](https://github.com/puppetlabs/puppetlabs-pe_status_check/compare/v2.0.0...v2.0.1)

### Fixed

- \(SUP-3381\) Add error handling to role fact [\#132](https://github.com/puppetlabs/puppetlabs-pe_status_check/pull/132) ([m0dular](https://github.com/m0dular))

## [v2.0.0](https://github.com/puppetlabs/puppetlabs-pe_status_check/tree/v2.0.0) (2022-06-13)

[Full Changelog](https://github.com/puppetlabs/puppetlabs-pe_status_check/compare/v1.5.0...v2.0.0)
Expand Down
48 changes: 26 additions & 22 deletions lib/facter/pe_status_check_role.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
Facter.add(:pe_status_check_role) do
confine { Facter.value(:pe_build) }
require 'puppet'
require_relative '../shared/pe_status_check'
setcode do
require 'puppet'
require_relative '../shared/pe_status_check'
classfile = Puppet.settings[:classfile]
return nil unless File.file?(classfile)

node_profiles = []
# Turn the list of infra roles defined in the module constant into a regex we can use with grep()
profile_regex = %r{^puppet_enterprise::profile::(#{PEStatusCheck.infra_profiles.join('|')})$}

File.open(classfile).grep(profile_regex) do |profile|
# Strip the leading 'puppet_enterprise::profile::' from each match
short_profile = profile.split(':')[-1].chomp
# Add unique entries to the node_profiles array by doing a bitwise or between it and the current match
# e.g. `['foo', 'bar'] | ['bar', 'baz']` produces `['foo', 'bar', 'baz']`
node_profiles |= [short_profile]
end
begin
File.open(classfile).grep(profile_regex) do |profile|
# Strip the leading 'puppet_enterprise::profile::' from each match
short_profile = profile.split(':')[-1].chomp
# Add unique entries to the node_profiles array by doing a bitwise or between it and the current match
# e.g. `['foo', 'bar'] | ['bar', 'baz']` produces `['foo', 'bar', 'baz']`
node_profiles |= [short_profile]
end

if node_profiles.include?('certificate_authority')
then 'primary'
elsif node_profiles.include?('primary_master_replica')
then 'replica'
# Use array subtraction to determine if more than one role is present
elsif (['master', 'puppetdb'] - node_profiles).empty?
then 'pe_compiler'
elsif node_profiles.include?('master')
then 'legacy_compiler'
elsif node_profiles.include?('database')
then 'postgres'
else
if node_profiles.include?('certificate_authority')
then 'primary'
elsif node_profiles.include?('primary_master_replica')
then 'replica'
# Use array subtraction to determine if more than one role is present
elsif (['master', 'puppetdb'] - node_profiles).empty?
then 'pe_compiler'
elsif node_profiles.include?('master')
then 'legacy_compiler'
elsif node_profiles.include?('database')
then 'postgres'
else
'unknown'
end
rescue StandardError => e
Facter.debug("Could not resolve 'pe_status_check_role' fact: #{e.message}")
'unknown'
end
end
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppetlabs-pe_status_check",
"version": "2.0.0",
"version": "2.0.1",
"author": "Marty Ewings",
"summary": "A Puppet Enterprise Module to Promote Preventative Maintenance and Self Service",
"license": "Apache-2.0",
Expand Down