plugins/inspect/services_inspector.rb in machinery-tool-1.5.0 vs plugins/inspect/services_inspector.rb in machinery-tool-1.6.0

- old
+ new

@@ -14,31 +14,39 @@ # # To contact SUSE about this file by physical or electronic mail, # you may find current contact information at www.suse.com class ServicesInspector < Inspector - def inspect(system, description, _filter, _options = {}) - if system.has_command?("systemctl") + def initialize(system, description) + @system = system + @description = description + end + + def inspect(_filter, _options = {}) + if @system.has_command?("systemctl") result = ServicesScope.new( init_system: "systemd", - services: inspect_systemd_services(system) + services: inspect_systemd_services ) else result = ServicesScope.new( init_system: "sysvinit", - services: inspect_sysvinit_services(system) + services: inspect_sysvinit_services ) end - description.services = result - @summary + @description.services = result end + def summary + "Found #{@description.services.services.length} services." + end + private - def inspect_systemd_services(system) - output = system.run_command( + def inspect_systemd_services + output = @system.run_command( "systemctl", "list-unit-files", "--type=service,socket", :stdout => :capture ) @@ -51,34 +59,32 @@ name, state = line.split(/\s+/) Service.new(name: name, state: state) end - @summary = "Found #{services.size} services." ServiceList.new(services.sort_by(&:name)) end - def inspect_sysvinit_services(system) + def inspect_sysvinit_services # Red Hat's chkconfig behaves differently than SUSE's: It takes different # command line arguments and has a different output format. We determine # if it's Red Hat by calling 'chkconfig --version'. On SUSE it exits with # an error, on Red Hat it doesn't. # begin - system.run_command("chkconfig", "--version") - services = parse_redhat_chkconfig(system) + @system.run_command("chkconfig", "--version") + services = parse_redhat_chkconfig rescue - services = parse_suse_chkconfig(system) + services = parse_suse_chkconfig end - @summary = "Found #{services.size} services." ServiceList.new(services.sort_by(&:name)) end - def parse_suse_chkconfig(system) - system.check_requirement("chkconfig", "--help") - output = system.run_command( + def parse_suse_chkconfig + @system.check_requirement("chkconfig", "--help") + output = @system.run_command( "chkconfig", "--allservices", :stdout => :capture ) @@ -86,18 +92,18 @@ name, state = line.split(/\s+/) Service.new(name: name, state: state) end end - def parse_redhat_chkconfig(system) - system.check_requirement("chkconfig", "--version") - system.check_requirement("runlevel") - _, runlevel = system.run_command( + def parse_redhat_chkconfig + @system.check_requirement("chkconfig", "--version") + @system.check_requirement("runlevel") + _, runlevel = @system.run_command( "runlevel", stdout: :capture ).split(" ") - output = system.run_command( + output = @system.run_command( "chkconfig", "--list", stdout: :capture ) services = output.lines.map do |line|