lib/server.rb in machinery-tool-1.14.0 vs lib/server.rb in machinery-tool-1.14.1

- old
+ new

@@ -15,15 +15,92 @@ # To contact SUSE about this file by physical or electronic mail, # you may find current contact information at www.suse.com class Server < Sinatra::Base module Helpers + def render_partial(partial, locals = {}) + source = File.read(File.join(Machinery::ROOT, "html/partials/#{partial}.html.haml")) + haml source, locals: locals + end + + def render_scope(scope) + render_partial scope, scope => @description[scope] + end + + def scope_meta_info(scope) + return "" if !@description[scope] + + " (" \ + "inspected host: '#{@description[scope].meta.hostname}', " \ + "at: #{DateTime.parse(@description[scope].meta.modified).strftime("%F %T")})" + end + def scope_help(scope) text = File.read(File.join(Machinery::ROOT, "plugins", "#{scope}/#{scope}.md")) Kramdown::Document.new(text).to_html end + def safe_length(object, attribute) + if object && collection = object.send(attribute) + collection.length + else + 0 + end + end + + def only_in_a + "<h3>Only in '#{@description_a.name}':</h3>" + end + + def only_in_b + "<h3>Only in '#{@description_b.name}':</h3>" + end + + def in_both + "<h3>In both descriptions:</h3>" + end + + def changed + "<h3>In both with different attributes:</h3>" + end + + def changed_packages + changed = [] + @diff["packages"].changed.each do |change| + changes = [] + relevant_attributes = ["version", "vendor", "arch"] + + if change[0].version == change[1].version + relevant_attributes.push("release") + if change[0].release == change[1].release + relevant_attributes.push("checksum") + end + end + + relevant_attributes.each do |attribute| + if change[0][attribute] != change[1][attribute] + changes.push(attribute + ": " + change[0][attribute] + " ↔ " + change[1][attribute]) + end + end + + changed.push(change[0].name + " (" + changes.join(", ") + ")") + end + changed + end + + def diffable_unmanaged_files + return @diffable_unmanaged_files if @diffable_unmanaged_files + + return [] if !@diff["unmanaged_files"] || !@diff["unmanaged_files"].only_in1 || + !@diff["unmanaged_files"].only_in2 + + files_in_1 = @diff["unmanaged_files"].only_in1.files.select(&:file?).map(&:name) + files_in_2 = @diff["unmanaged_files"].only_in2.files.select(&:file?).map(&:name) + + @diffable_unmanaged_files = files_in_1 & files_in_2 + end + def diff_to_object(diff) diff = Machinery.scrub(diff) lines = diff.lines[2..-1] diff_object = { file: diff[/--- a(.*)/, 1], @@ -77,33 +154,10 @@ end end helpers Helpers - get "/descriptions/:id.js" do - description = SystemDescription.load(params[:id], settings.system_description_store) - diffs_dir = description.scope_file_store("analyze/config_file_diffs").path - if description.config_files && diffs_dir - # Enrich description with the config file diffs - description.config_files.files.each do |file| - path = File.join(diffs_dir, file.name + ".diff") - file.diff = diff_to_object(File.read(path)) if File.exists?(path) - end - end - - # Enrich file information with downloadable flag - ["config_files", "changed_managed_files", "unmanaged_files"].each do |scope| - next if !description[scope] - - description[scope].files.each do |file| - file.downloadable = file.on_disk? - end - end - - description.to_hash.to_json - end - get "/descriptions/:id/files/:scope/*" do description = SystemDescription.load(params[:id], settings.system_description_store) filename = File.join("/", params["splat"].first) file = description[params[:scope]].files.find { |f| f.name == filename } @@ -120,47 +174,37 @@ attachment File.basename(filename) content end - get "/compare/:a/:b.json" do - description_a = SystemDescription.load(params[:a], settings.system_description_store) - description_b = SystemDescription.load(params[:b], settings.system_description_store) + get "/compare/:a/:b" do + @description_a = SystemDescription.load(params[:a], settings.system_description_store) + @description_b = SystemDescription.load(params[:b], settings.system_description_store) - diff = { - meta: { - description_a: description_a.name, - description_b: description_b.name, - } - } + @meta = {} + @diff = {} Inspector.all_scopes.each do |scope| - if description_a[scope] && description_b[scope] - comparison = Comparison.compare_scope(description_a, description_b, scope) - diff[scope] = comparison.as_json + if @description_a[scope] && @description_b[scope] + @diff[scope] = Comparison.compare_scope(@description_a, @description_b, scope) else - diff[:meta][:uninspected] ||= Hash.new + @meta[:uninspected] ||= Hash.new - if !description_a[scope] && description_b[scope] - diff[:meta][:uninspected][description_a.name] ||= Array.new - diff[:meta][:uninspected][description_a.name] << scope + if !@description_a[scope] && @description_b[scope] + @meta[:uninspected][@description_a.name] ||= Array.new + @meta[:uninspected][@description_a.name] << scope end - if !description_b[scope] && description_a[scope] - diff[:meta][:uninspected][description_b.name] ||= Array.new - diff[:meta][:uninspected][description_b.name] << scope + if !@description_b[scope] && @description_a[scope] + @meta[:uninspected][@description_b.name] ||= Array.new + @meta[:uninspected][@description_b.name] << scope end end end - diff.to_json + haml File.read(File.join(Machinery::ROOT, "html/comparison.html.haml")) end - get "/compare/:a/:b" do - haml File.read(File.join(Machinery::ROOT, "html/comparison.html.haml")), - locals: { description_a: params[:a], description_b: params[:b] } - end - get "/compare/:a/:b/files/:scope/*" do description1 = SystemDescription.load(params[:a], settings.system_description_store) description2 = SystemDescription.load(params[:b], settings.system_description_store) filename = File.join("/", params["splat"].first) @@ -173,9 +217,19 @@ diff.to_s(:html) end get "/:id" do - haml File.read(File.join(Machinery::ROOT, "html/index.html.haml")), - locals: { description_name: params[:id] } + @description = SystemDescription.load(params[:id], settings.system_description_store) + + diffs_dir = @description.scope_file_store("analyze/config_file_diffs").path + if @description.config_files && diffs_dir + # Enrich description with the config file diffs + @description.config_files.files.each do |file| + path = File.join(diffs_dir, file.name + ".diff") + file.diff = diff_to_object(File.read(path)) if File.exists?(path) + end + end + + haml File.read(File.join(Machinery::ROOT, "html/index.html.haml")) end end