Diff
checker
Texte
Texte
Images
Documents
Excel
Dossiers
Legal
Enterprise
Application de bureau
Prix
Se connecter
Télécharger Diffchecker Desktop
Comparer le texte
Trouver la différence entre deux fichiers texte
Outils
Historique
Affichage
Vue
Divisé
Unifié
Éditeur live
Masquer les espaces
Cacher identiques
Sans retour à la ligne
Niveau de précision
Intelligent
Syntaxe
Aucun
Modifier l’apparence
Traitement
Ignorer
Transformer le texte
Aller au premier écart
Modifier l'entrée
Diffchecker Desktop
La façon la plus sécurisée d'utiliser Diffchecker. Obtenez l'application Diffchecker Desktop : vos diffs ne quittent jamais votre ordinateur !
Obtenir Desktop
foremansalt
Créé
il y a 2 ans
Le diff n'expire jamais
Effacer
Exporter
Partager
Expliquer
47 suppressions
Lignes
Total
Supprimé
Caractères
Total
Supprimé
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
176 lignes
Copier tout
148 ajouts
Lignes
Total
Ajouté
Caractères
Total
Ajouté
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
262 lignes
Copier tout
module ForemanSalt
module ForemanSalt
class ReportImporter
class ReportImporter
delegate :logger, to: :Rails
delegate :logger, to: :Rails
attr_reader :report
attr_reader :report
Copier
Copié
Copier
Copié
# Define logger as a class method
def self.logger
Rails.logger
end
def self.import(raw, proxy_id = nil)
def self.import(raw, proxy_id = nil)
Copier
Copié
Copier
Copié
logger.info "Starting import with raw data: #{raw.inspect} and proxy_id: #{proxy_id}"
raise ::Foreman::Exception, _('Invalid report') unless raw.is_a?(Hash)
raise ::Foreman::Exception, _('Invalid report') unless raw.is_a?(Hash)
raw.map do |host, report|
raw.map do |host, report|
Copier
Copié
Copier
Copié
logger.debug "Processing report for host: #{host}"
importer = ForemanSalt::ReportImporter.new(host, report, proxy_id)
importer = ForemanSalt::ReportImporter.new(host, report, proxy_id)
importer.import
importer.import
report = importer.report
report = importer.report
report.origin = 'Salt'
report.origin = 'Salt'
report.save!
report.save!
Copier
Copié
Copier
Copié
logger.info "Report saved for host: #{host}"
report
report
end
end
Copier
Copié
Copier
Copié
rescue => e
logger.error "Import failed: #{e.message}"
raise e
end
end
def initialize(host, raw, proxy_id = nil)
def initialize(host, raw, proxy_id = nil)
Copier
Copié
Copier
Copié
logger.debug "Initializing ReportImporter with host: #{host}, proxy_id: #{proxy_id}"
@host = find_or_create_host(host)
@host = find_or_create_host(host)
@raw = raw
@raw = raw
@proxy_id = proxy_id
@proxy_id = proxy_id
end
end
def import
def import
Copier
Copié
Copier
Copié
logger.info "
p
rocessing report for #{@host}"
logger.info "
P
rocessing report for #{@host}"
logger.debug { "Report
: #{@raw.inspect}" }
logger.debug { "Report
raw data
: #{@raw.inspect}" }
if @host.new_record? && !Setting[:create_new_host_when_report_is_uploaded]
if @host.new_record? && !Setting[:create_new_host_when_report_is_uploaded]
Copier
Copié
Copier
Copié
logger.info("
s
kipping report for #{@host} as it
s an unknown host and create_new_host_when_report_is_uploaded setting is disabled")
logger.info("
S
kipping report for #{@host} as it
'
s an unknown host and create_new_host_when_report_is_uploaded setting is disabled")
return ConfigReport.new
return ConfigReport.new
end
end
@host.salt_proxy_id ||= @proxy_id
@host.salt_proxy_id ||= @proxy_id
@host.last_report = start_time
@host.last_report = start_time
Copier
Copié
Copier
Copié
if [Array, String].
member?
@raw.class
if [Array, String].
include?(
@raw.class
)
logger.debug "Detected failures in raw data; processing failures"
process_failures # If Salt sends us only an array (or string), it's a list of fatal failures
process_failures # If Salt sends us only an array (or string), it's a list of fatal failures
else
else
Copier
Copié
Copier
Copié
logger.debug "Processing normal report data"
process_normal
process_normal
end
end
Copier
Copié
Copier
Copié
@host.save(validate:
false)
if
@host.save(validate:
true)
logger.debug "Host #{@host.name} saved successfully"
else
logger.warn "Host #{@host.name} failed to save: #{@host.errors.full_messages.join(', ')}"
end
@host.reload
@host.reload
@host.refresh_statuses([HostStatus.find_status_by_humanized_name('configuration')])
@host.refresh_statuses([HostStatus.find_status_by_humanized_name('configuration')])
Copier
Copié
Copier
Copié
duration = (Time.zone.now - start_time).round(2)
logger.info("Imported report for #{@host} in #{
(Time.zone.now - start_time).round(2)
} seconds")
logger.info("Imported report for #{@host} in #{
duration
} seconds")
rescue => e
logger.error "Failed to import report for #{@host}: #{e.message}"
raise e
end
end
private
private
def find_or_create_host(host)
def find_or_create_host(host)
Copier
Copié
Copier
Copié
logger.debug "Finding or creating host: #{host}"
@host ||= Host::Managed.find_by(name: host)
@host ||= Host::Managed.find_by(name: host)
unless @host
unless @host
Copier
Copié
Copier
Copié
logger.info "Host not found; creating new host with name: #{host}"
new
= Host::Managed.new(name: host)
new
_host
= Host::Managed.new(name: host)
new.save(validate: false)
if new_host.save(validate: true)
@host = new
logger.debug "New host #{host} created successfully"
@host = new_host
else
logger.error "Failed to create host #{host}: #{new_host.errors.full_messages.join(', ')}"
raise ::Foreman::Exception, _('Failed to create host')
end
end
end
@host
@host
Copier
Copié
Copier
Copié
rescue => e
logger.error "Error in find_or_create_host: #{e.message}"
raise e
end
end
def import_log_messages
def import_log_messages
Copier
Copié
Copier
Copié
logger.debug "Importing log messages"
@raw.each do |resource, result|
@raw.each do |resource, result|
Copier
Copié
Copier
Copié
Text moved with changes to lines 229-233 (77.9% similarity)
level =
if result['changes'].blank? && result['result']
level =
determine_log_level(result)
Différences enregistrées
Texte d'origine
Ouvrir un fichier
module ForemanSalt class ReportImporter delegate :logger, to: :Rails attr_reader :report def self.import(raw, proxy_id = nil) raise ::Foreman::Exception, _('Invalid report') unless raw.is_a?(Hash) raw.map do |host, report| importer = ForemanSalt::ReportImporter.new(host, report, proxy_id) importer.import report = importer.report report.origin = 'Salt' report.save! report end end def initialize(host, raw, proxy_id = nil) @host = find_or_create_host(host) @raw = raw @proxy_id = proxy_id end def import logger.info "processing report for #{@host}" logger.debug { "Report: #{@raw.inspect}" } if @host.new_record? && !Setting[:create_new_host_when_report_is_uploaded] logger.info("skipping report for #{@host} as its an unknown host and create_new_host_when_report_is_uploaded setting is disabled") return ConfigReport.new end @host.salt_proxy_id ||= @proxy_id @host.last_report = start_time if [Array, String].member? @raw.class process_failures # If Salt sends us only an array (or string), it's a list of fatal failures else process_normal end @host.save(validate: false) @host.reload @host.refresh_statuses([HostStatus.find_status_by_humanized_name('configuration')]) logger.info("Imported report for #{@host} in #{(Time.zone.now - start_time).round(2)} seconds") end private def find_or_create_host(host) @host ||= Host::Managed.find_by(name: host) unless @host new = Host::Managed.new(name: host) new.save(validate: false) @host = new end @host end def import_log_messages @raw.each do |resource, result| level = if result['changes'].blank? && result['result'] :info elsif result['result'] == false :err else # nil mean "unchanged" when running highstate with test=True :notice end source = Source.find_or_create_by(value: resource) message = if result['changes']['diff'] result['changes']['diff'] elsif result['pchanges'].present? && result['pchanges'].include?('diff') result['pchanges']['diff'] elsif result['comment'].presence result['comment'] else 'No message available' end message = Message.find_or_create_by(value: message) Log.create(message_id: message.id, source_id: source.id, report: @report, level: level) end end def calculate_metrics success = 0 failed = 0 changed = 0 restarted = 0 restarted_failed = 0 pending = 0 time = {} @raw.each do |resource, result| next unless result.is_a? Hash if result['result'] success += 1 if resource.match(/^service_/) && result['comment'].include?('restarted') restarted += 1 elsif result['changes'].present? changed += 1 elsif result['pchanges'].present? pending += 1 end elsif result['result'].nil? pending += 1 elsif !result['result'] if resource.match(/^service_/) && result['comment'].include?('restarted') restarted_failed += 1 else failed += 1 end end duration = if result['duration'].is_a? String begin Float(result['duration'].delete(' ms')) rescue StandardError nil end else result['duration'] end # Convert duration from milliseconds to seconds duration /= 1000 if duration.is_a? Float time[resource] = duration || 0 end time[:total] = time.values.compact.sum || 0 events = { total: changed + failed + restarted + restarted_failed, success: success + restarted, failure: failed + restarted_failed } changes = { total: changed + restarted } resources = { 'total' => @raw.size, 'applied' => changed, 'restarted' => restarted, 'failed' => failed, 'failed_restarts' => restarted_failed, 'skipped' => 0, 'scheduled' => 0, 'pending' => pending } { events: events, resources: resources, changes: changes, time: time } end def process_normal metrics = calculate_metrics status = ConfigReportStatusCalculator.new(counters: metrics[:resources].slice(*::ConfigReport::METRIC)).calculate @report = ConfigReport.new(host: @host, reported_at: start_time, status: status, metrics: metrics) return @report unless @report.save import_log_messages end def process_failures @raw = [@raw] unless @raw.is_a? Array status = ConfigReportStatusCalculator.new(counters: { 'failed' => @raw.size }).calculate @report = ConfigReport.create(host: @host, reported_at: Time.zone.now, status: status, metrics: {}) source = Source.find_or_create_by(value: 'Salt') @raw.each do |failure| message = Message.find_or_create_by(value: failure) Log.create(message_id: message.id, source_id: source.id, report: @report, level: :err) end end def start_time @start_time ||= Time.zone.now end end end
Texte modifié
Ouvrir un fichier
module ForemanSalt class ReportImporter delegate :logger, to: :Rails attr_reader :report # Define logger as a class method def self.logger Rails.logger end def self.import(raw, proxy_id = nil) logger.info "Starting import with raw data: #{raw.inspect} and proxy_id: #{proxy_id}" raise ::Foreman::Exception, _('Invalid report') unless raw.is_a?(Hash) raw.map do |host, report| logger.debug "Processing report for host: #{host}" importer = ForemanSalt::ReportImporter.new(host, report, proxy_id) importer.import report = importer.report report.origin = 'Salt' report.save! logger.info "Report saved for host: #{host}" report end rescue => e logger.error "Import failed: #{e.message}" raise e end def initialize(host, raw, proxy_id = nil) logger.debug "Initializing ReportImporter with host: #{host}, proxy_id: #{proxy_id}" @host = find_or_create_host(host) @raw = raw @proxy_id = proxy_id end def import logger.info "Processing report for #{@host}" logger.debug { "Report raw data: #{@raw.inspect}" } if @host.new_record? && !Setting[:create_new_host_when_report_is_uploaded] logger.info("Skipping report for #{@host} as it's an unknown host and create_new_host_when_report_is_uploaded setting is disabled") return ConfigReport.new end @host.salt_proxy_id ||= @proxy_id @host.last_report = start_time if [Array, String].include?(@raw.class) logger.debug "Detected failures in raw data; processing failures" process_failures # If Salt sends us only an array (or string), it's a list of fatal failures else logger.debug "Processing normal report data" process_normal end if @host.save(validate: true) logger.debug "Host #{@host.name} saved successfully" else logger.warn "Host #{@host.name} failed to save: #{@host.errors.full_messages.join(', ')}" end @host.reload @host.refresh_statuses([HostStatus.find_status_by_humanized_name('configuration')]) duration = (Time.zone.now - start_time).round(2) logger.info("Imported report for #{@host} in #{duration} seconds") rescue => e logger.error "Failed to import report for #{@host}: #{e.message}" raise e end private def find_or_create_host(host) logger.debug "Finding or creating host: #{host}" @host ||= Host::Managed.find_by(name: host) unless @host logger.info "Host not found; creating new host with name: #{host}" new_host = Host::Managed.new(name: host) if new_host.save(validate: true) logger.debug "New host #{host} created successfully" @host = new_host else logger.error "Failed to create host #{host}: #{new_host.errors.full_messages.join(', ')}" raise ::Foreman::Exception, _('Failed to create host') end end @host rescue => e logger.error "Error in find_or_create_host: #{e.message}" raise e end def import_log_messages logger.debug "Importing log messages" @raw.each do |resource, result| level = determine_log_level(result) source_value = resource.to_s source = Source.find_or_create_by(value: source_value) logger.debug "Log source: #{source_value}" message_value = extract_message(result) message = Message.find_or_create_by(value: message_value) logger.debug "Log message: #{message_value}" Log.create(message_id: message.id, source_id: source.id, report: @report, level: level) logger.debug "Log entry created with level #{level} for resource #{resource}" end rescue => e logger.error "Error importing log messages: #{e.message}" raise e end def calculate_metrics logger.debug "Calculating metrics from raw data" success = 0 failed = 0 changed = 0 restarted = 0 restarted_failed = 0 pending = 0 time = {} @raw.each do |resource, result| next unless result.is_a?(Hash) logger.debug "Processing resource: #{resource}" if result['result'] == true success += 1 logger.debug "Resource #{resource} succeeded" if resource.match(/^service_/) && result['comment'].include?('restarted') restarted += 1 logger.debug "Service #{resource} was restarted" elsif result['changes'].present? changed += 1 logger.debug "Resource #{resource} changed" elsif result['pchanges'].present? pending += 1 logger.debug "Resource #{resource} has pending changes" end elsif result['result'].nil? pending += 1 logger.debug "Resource #{resource} is pending" elsif result['result'] == false if resource.match(/^service_/) && result['comment'].include?('restarted') restarted_failed += 1 logger.debug "Service #{resource} failed to restart" else failed += 1 logger.debug "Resource #{resource} failed" end end duration = parse_duration(result['duration']) time[resource] = duration || 0 logger.debug "Duration for resource #{resource}: #{time[resource]} seconds" end time[:total] = time.values.compact.sum || 0 logger.debug "Total execution time: #{time[:total]} seconds" events = { total: changed + failed + restarted + restarted_failed, success: success + restarted, failure: failed + restarted_failed } changes = { total: changed + restarted } resources = { 'total' => @raw.size, 'applied' => changed, 'restarted' => restarted, 'failed' => failed, 'failed_restarts' => restarted_failed, 'skipped' => 0, 'scheduled' => 0, 'pending' => pending } logger.debug "Metrics calculated: events=#{events}, changes=#{changes}, resources=#{resources}" { events: events, resources: resources, changes: changes, time: time } rescue => e logger.error "Error calculating metrics: #{e.message}" raise e end def process_normal logger.debug "Processing normal report" metrics = calculate_metrics status = ConfigReportStatusCalculator.new(counters: metrics[:resources].slice(*::ConfigReport::METRIC)).calculate logger.debug "Calculated status: #{status.inspect}" @report = ConfigReport.new(host: @host, reported_at: start_time, status: status, metrics: metrics) if @report.save logger.debug "Report saved successfully for host #{@host.name}" import_log_messages else logger.error "Failed to save report: #{@report.errors.full_messages.join(', ')}" end rescue => e logger.error "Error processing normal report: #{e.message}" raise e end def process_failures logger.debug "Processing failure report" @raw = [@raw] unless @raw.is_a?(Array) status = ConfigReportStatusCalculator.new(counters: { 'failed' => @raw.size }).calculate logger.debug "Calculated status for failures: #{status.inspect}" @report = ConfigReport.create(host: @host, reported_at: Time.zone.now, status: status, metrics: {}) source = Source.find_or_create_by(value: 'Salt') @raw.each do |failure| message = Message.find_or_create_by(value: failure) Log.create(message_id: message.id, source_id: source.id, report: @report, level: :err) logger.debug "Logged failure message: #{failure}" end rescue => e logger.error "Error processing failures: #{e.message}" raise e end def start_time @start_time ||= Time.zone.now end def determine_log_level(result) if result['changes'].blank? && result['result'] == true :info elsif result['result'] == false :err else :notice end end def extract_message(result) if result['changes'] && result['changes']['diff'] result['changes']['diff'] elsif result['pchanges'] && result['pchanges']['diff'] result['pchanges']['diff'] elsif result['comment'].present? result['comment'] else 'No message available' end end def parse_duration(duration) if duration.is_a?(String) duration_in_ms = duration.delete(' ms') Float(duration_in_ms) / 1000 else duration.to_f / 1000 end rescue logger.warn "Unable to parse duration: #{duration}" nil end end end
Trouver la différence