Fetch agent ENC facts from encapi #528
@@ -6,8 +6,8 @@ require 'net/http'
|
||||
require 'uri'
|
||||
require 'fileutils'
|
||||
|
||||
# CobblerENC module: Fetches ENC data from Cobbler, caches it, and provides structured facts.
|
||||
module CobblerENC
|
||||
# EncapiENC module: Fetches ENC data from encapi, caches it, and provides structured facts.
|
||||
module EncapiENC
|
||||
CACHE_FILE = '/var/cache/puppet_enc.yaml'
|
||||
CACHE_TTL = 7 * 24 * 60 * 60 # 7 days in seconds
|
||||
@enc_data = nil # In-memory cache for the ENC response
|
||||
@@ -29,8 +29,8 @@ module CobblerENC
|
||||
File.write(CACHE_FILE, cache_data.to_yaml)
|
||||
end
|
||||
|
||||
def self.fetch_from_cobbler
|
||||
uri = URI("http://cobbler.main.unkin.net/cblr/svc/op/puppet/hostname/#{Facter.value(:fqdn) || Facter.value(:hostname)}")
|
||||
def self.fetch_from_encapi
|
||||
uri = URI("https://encapi.k8s.syd1.au.unkin.net/cblr/svc/op/puppet/hostname/#{Facter.value(:fqdn) || Facter.value(:hostname)}")
|
||||
response = Net::HTTP.get_response(uri)
|
||||
|
||||
raise "Failed to fetch ENC data. HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
||||
@@ -41,7 +41,7 @@ module CobblerENC
|
||||
def self.retrieve_enc_data
|
||||
return @enc_data if @enc_data
|
||||
|
||||
@enc_data = fetch_from_cobbler
|
||||
@enc_data = fetch_from_encapi
|
||||
write_cache(@enc_data)
|
||||
@enc_data
|
||||
end
|
||||
@@ -49,26 +49,26 @@ module CobblerENC
|
||||
def self.fetch_enc_data
|
||||
retrieve_enc_data
|
||||
rescue StandardError => e
|
||||
Facter.warn("Error retrieving Cobbler ENC data: #{e.message}")
|
||||
Facter.warn("Error retrieving encapi ENC data: #{e.message}")
|
||||
@enc_data = read_cache
|
||||
return @enc_data unless @enc_data.empty?
|
||||
|
||||
raise 'No cached ENC data available and Cobbler is down.'
|
||||
raise 'No cached ENC data available and encapi is unreachable.'
|
||||
end
|
||||
|
||||
def self.enc_role
|
||||
fetch_enc_data.fetch('classes', {}).keys.first || raise('ENC Role not found in Cobbler ENC response')
|
||||
fetch_enc_data.fetch('classes', {}).keys.first || raise('ENC Role not found in encapi ENC response')
|
||||
end
|
||||
|
||||
def self.enc_env
|
||||
fetch_enc_data.fetch('environment', nil) || raise('ENC Environment not found in Cobbler ENC response')
|
||||
fetch_enc_data.fetch('environment', nil) || raise('ENC Environment not found in encapi ENC response')
|
||||
end
|
||||
end
|
||||
|
||||
Facter.add('enc_role') do
|
||||
setcode { CobblerENC.enc_role }
|
||||
setcode { EncapiENC.enc_role }
|
||||
end
|
||||
|
||||
Facter.add('enc_env') do
|
||||
setcode { CobblerENC.enc_env }
|
||||
setcode { EncapiENC.enc_env }
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user