Skip to content

Instantly share code, notes, and snippets.

@kallies
Created June 4, 2021 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kallies/1fdc0d400a978eb32774a7d7f39a1426 to your computer and use it in GitHub Desktop.
Save kallies/1fdc0d400a978eb32774a7d7f39a1426 to your computer and use it in GitHub Desktop.
rhsm_lce fact for subscription_manager
#!/usr/bin/ruby
# frozen_string_literal: true
#
# Report the lifecycle environment name of the client.
#
# based on rhsm_identity.rb
#
require 'English'
begin
require 'facter/util/facter_cacheable'
rescue LoadError => e
Facter.debug("#{e.backtrace[0]}: #{$ERROR_INFO}.")
end
# lce of this client
module Facter::Util::RhsmLCE
@doc = <<EOF
LCE for this client.
EOF
CACHE_TTL = 86_400 unless defined? CACHE_TTL # 24 * 60 * 60 seconds
CACHE_FILE = '/var/cache/rhsm/lce.yaml' unless defined? CACHE_FILE
module_function
def rhsm_lce
value = nil
begin
output = Facter::Core::Execution.execute(
'/usr/sbin/subscription-manager identity',
on_fail: Facter::Core::Execution::ExecutionFailure,
)
unless output.nil?
output.split("\n").each do |line|
if line =~ %r{^environment .*(?: is)?: ([a-zA-Z0-9_-]*)\/.*}
value = Regexp.last_match(1)
end
end
end
rescue UncaughtThrowError, Facter::Core::Execution::ExecutionFailure => e
if $ERROR_INFO !~ %r{This system is not yet registered}
Facter.debug("#{e.backtrace[0]}: #{$ERROR_INFO}.")
end
end
value
end
end
if File.exist? '/usr/sbin/subscription-manager'
lces = Facter::Util::RhsmLCE
if Puppet.features.facter_cacheable?
Facter.add(:rhsm_lce) do
setcode do
# TODO: use another fact to set the TTL in userspace
# right now this can be done by removing the cache files
cache = Facter::Util::FacterCacheable.cached?(
:rhsm_lce, lces::CACHE_TTL, lces::CACHE_FILE
)
if !cache
lce = lces.rhsm_lce
Facter::Util::FacterCacheable.cache(
:rhsm_lce, lce, lces::CACHE_FILE
)
lce
elsif cache.is_a? Array
cache
else
cache['rhsm_lce']
end
end
end
else
Facter.add(:rhsm_lce) do
setcode { lces.rhsm_lce }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment