From ae44751aeddb951e9f50bd77bf55ead422f6c415 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Sat, 29 Mar 2014 21:41:31 -0400 Subject: [PATCH 1/5] better stubbing of distro - only 1 'local' method Distros.local - don't 'exist' on a release_file that doesn't exist - consistent stubbing of distro. --- lib/linux_admin/common.rb | 2 +- lib/linux_admin/distro.rb | 29 ++++++++++++----------------- lib/linux_admin/rpm.rb | 2 +- spec/common_spec.rb | 5 +---- spec/distro_spec.rb | 33 ++++++++++++++++++++------------- spec/logical_volume_spec.rb | 2 -- spec/physical_volume_spec.rb | 2 -- spec/service_spec.rb | 4 ---- spec/spec_helper.rb | 15 +++++++-------- spec/volume_group_spec.rb | 2 -- 10 files changed, 42 insertions(+), 54 deletions(-) diff --git a/lib/linux_admin/common.rb b/lib/linux_admin/common.rb index a194b42..3fa90ea 100644 --- a/lib/linux_admin/common.rb +++ b/lib/linux_admin/common.rb @@ -3,7 +3,7 @@ class LinuxAdmin module Common def cmd(cmd) - Distros::Distro.local.class::COMMANDS[cmd] + Distros.local.class::COMMANDS[cmd] end def run(cmd, options = {}) diff --git a/lib/linux_admin/distro.rb b/lib/linux_admin/distro.rb index 39fef36..77fa29a 100644 --- a/lib/linux_admin/distro.rb +++ b/lib/linux_admin/distro.rb @@ -32,11 +32,20 @@ def self.all end def self.local - Distro.local + @local ||= begin + result = nil + Distros.constants.each do |cdistro| + distro_method = cdistro.to_s.downcase.to_sym + distro = Distros.const_get(cdistro) + next unless distro < Distro + result = Distros.send(distro_method) if distro.detected? + end + result || Distros.generic + end end class Distro - RELEASE_FILE = '' + RELEASE_FILE = nil ETC_ISSUE_KEYWORDS = [] def self.etc_issue_keywords @@ -47,20 +56,6 @@ def self.release_file self::RELEASE_FILE end - def self.local - # this can be cleaned up.. - @local ||= begin - result = nil - Distros.constants.each do |cdistro| - distro_method = cdistro.to_s.downcase.to_sym - distro = Distros.const_get(cdistro) - next unless distro < Distro - result = Distros.send(distro_method) if distro.detected? - end - result || Distros.generic - end - end - def self.detected? detected_by_etc_issue? || detected_by_etc_release? end @@ -70,7 +65,7 @@ def self.detected_by_etc_issue? end def self.detected_by_etc_release? - File.exists?(release_file) + release_file && File.exists?(release_file) end end diff --git a/lib/linux_admin/rpm.rb b/lib/linux_admin/rpm.rb index db5506e..d396e95 100644 --- a/lib/linux_admin/rpm.rb +++ b/lib/linux_admin/rpm.rb @@ -1,7 +1,7 @@ class LinuxAdmin class Rpm < Package def self.rpm_cmd - Distros::Distro.local.class::COMMANDS[:rpm] + Distros.local.class::COMMANDS[:rpm] end def self.list_installed diff --git a/spec/common_spec.rb b/spec/common_spec.rb index 941940c..bce924a 100644 --- a/spec/common_spec.rb +++ b/spec/common_spec.rb @@ -15,10 +15,7 @@ class TestClass context "#cmd" do it "looks up local command from id" do - d = double(LinuxAdmin::Distros::Distro) - d.class::COMMANDS = {:sh => '/bin/sh'} - LinuxAdmin::Distros::Distro.should_receive(:local).and_return(d) - subject.cmd(:sh).should == '/bin/sh' + expect(subject.cmd(:dd)).to match(/bin\/dd/) end end diff --git a/spec/distro_spec.rb b/spec/distro_spec.rb index 77b0e96..6a64dae 100644 --- a/spec/distro_spec.rb +++ b/spec/distro_spec.rb @@ -1,7 +1,12 @@ require 'spec_helper' describe LinuxAdmin::Distros::Distro do + let(:subject) { LinuxAdmin::Distros.local } describe "#local" do + before do + LinuxAdmin::Distros.unstub(:local) + end + [['ubuntu', :ubuntu], ['Fedora', :fedora], ['red hat', :rhel], @@ -9,13 +14,13 @@ ['centos', :rhel]].each do |i, d| context "/etc/issue contains '#{i}'" do before(:each) do - LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return(i) - File.should_receive(:exists?).at_least(:once).and_return(false) + expect(LinuxAdmin::EtcIssue.instance).to receive(:to_s).at_least(:once).and_return(i) + exists("/etc/fedora-release" => false, "/etc/redhat-release" => false) end it "returns Distros.#{d}" do distro = LinuxAdmin::Distros.send(d) - described_class.local.should == distro + expect(subject).to eq(distro) end end end @@ -27,27 +32,29 @@ context "/etc/redhat-release exists" do it "returns Distros.rhel" do - File.should_receive(:exists?).with('/etc/redhat-release').and_return(true) - LinuxAdmin::Distros::Fedora.should_receive(:detected?).and_return(false) - File.should_receive(:exists?).at_least(:once).and_call_original - described_class.local.should == LinuxAdmin::Distros.rhel + exists("/etc/fedora-release" => false, "/etc/redhat-release" => true) + expect(subject).to eq(LinuxAdmin::Distros.rhel) end end context "/etc/fedora-release exists" do it "returns Distros.fedora" do - File.should_receive(:exists?).with('/etc/redhat-release').and_return(false) - File.should_receive(:exists?).with('/etc/fedora-release').and_return(true) - File.should_receive(:exists?).at_least(:once).and_call_original - described_class.local.should == LinuxAdmin::Distros.fedora + exists("/etc/fedora-release" => true, "/etc/redhat-release" => false) + expect(subject).to eq(LinuxAdmin::Distros.fedora) end end end it "returns Distros.generic" do LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return('') - File.should_receive(:exists?).at_least(:once).and_return(false) - described_class.local.should == LinuxAdmin::Distros.generic + exists("/etc/fedora-release" => false, "/etc/redhat-release" => false) + expect(subject).to eq(LinuxAdmin::Distros.generic) end end + + private + + def exists(files) + files.each_pair { |file, value| allow(File).to receive(:exists?).with(file).and_return(value) } + end end diff --git a/spec/logical_volume_spec.rb b/spec/logical_volume_spec.rb index 4f07360..a039e56 100644 --- a/spec/logical_volume_spec.rb +++ b/spec/logical_volume_spec.rb @@ -2,8 +2,6 @@ describe LinuxAdmin::LogicalVolume do before(:each) do - LinuxAdmin::Distros::Distro.stub(:local => LinuxAdmin::Distros::Test.new) - @logical_volumes = < LinuxAdmin::Distros::Test.new) - @physical_volumes = < distro) +end def data_file_path(to) File.expand_path(to, File.join(File.dirname(__FILE__), "data")) @@ -47,10 +53,3 @@ def sample_output(to) def clear_caches LinuxAdmin::RegistrationSystem.instance_variable_set(:@registration_type, nil) end - -class LinuxAdmin - module Distros - # simply alias test distro to redhat distro for time being - Distros::Test = Distros::RedHat - end -end diff --git a/spec/volume_group_spec.rb b/spec/volume_group_spec.rb index c68d425..c25ecbf 100644 --- a/spec/volume_group_spec.rb +++ b/spec/volume_group_spec.rb @@ -2,8 +2,6 @@ describe LinuxAdmin::VolumeGroup do before(:each) do - LinuxAdmin::Distros::Distro.stub(:local => LinuxAdmin::Distros::Test.new) - @groups = < Date: Sun, 30 Mar 2014 00:03:28 -0400 Subject: [PATCH 2/5] move distro constants into instance variables - move package distro case statement into distro itself - remove redhat distro constant (it is rhel or fedora) - leverage Distros.all instead of meta class fu --- lib/linux_admin.rb | 1 - lib/linux_admin/common.rb | 2 +- lib/linux_admin/deb.rb | 3 +- lib/linux_admin/distro.rb | 115 ++++++++++++++++--------------------- lib/linux_admin/package.rb | 18 ------ lib/linux_admin/rpm.rb | 4 +- spec/common_spec.rb | 12 +--- spec/distro_spec.rb | 19 ++++++ spec/package_spec.rb | 17 ------ 9 files changed, 74 insertions(+), 117 deletions(-) delete mode 100644 lib/linux_admin/package.rb delete mode 100644 spec/package_spec.rb diff --git a/lib/linux_admin.rb b/lib/linux_admin.rb index 99e8607..de3c9e0 100644 --- a/lib/linux_admin.rb +++ b/lib/linux_admin.rb @@ -5,7 +5,6 @@ require 'linux_admin/common' require 'linux_admin/exceptions' -require 'linux_admin/package' require 'linux_admin/rpm' require 'linux_admin/deb' require 'linux_admin/version' diff --git a/lib/linux_admin/common.rb b/lib/linux_admin/common.rb index 3fa90ea..fcbeb03 100644 --- a/lib/linux_admin/common.rb +++ b/lib/linux_admin/common.rb @@ -3,7 +3,7 @@ class LinuxAdmin module Common def cmd(cmd) - Distros.local.class::COMMANDS[cmd] + Distros.local.command(cmd) end def run(cmd, options = {}) diff --git a/lib/linux_admin/deb.rb b/lib/linux_admin/deb.rb index 8bf7639..1e50f15 100644 --- a/lib/linux_admin/deb.rb +++ b/lib/linux_admin/deb.rb @@ -4,7 +4,7 @@ # Licensed under the MIT License class LinuxAdmin - class Deb < Package + class Deb APT_CACHE_CMD = '/usr/bin/apt-cache' def self.from_line(apt_cache_line, in_description=false) @@ -34,6 +34,5 @@ def self.from_string(apt_cache_string) def self.info(pkg) self.from_string(run!(APT_CACHE_CMD, :params => ["show", pkg]).output) end - end end diff --git a/lib/linux_admin/distro.rb b/lib/linux_admin/distro.rb index 77fa29a..e77eb5c 100644 --- a/lib/linux_admin/distro.rb +++ b/lib/linux_admin/distro.rb @@ -11,10 +11,6 @@ def self.generic @generic ||= Generic.new end - def self.redhat - @redhat ||= RedHat.new - end - def self.rhel @rhel ||= RHEL.new end @@ -28,110 +24,99 @@ def self.ubuntu end def self.all - @distros ||= [generic, redhat, ubuntu] + @distros ||= [rhel, fedora, ubuntu, generic] end def self.local @local ||= begin - result = nil - Distros.constants.each do |cdistro| - distro_method = cdistro.to_s.downcase.to_sym - distro = Distros.const_get(cdistro) - next unless distro < Distro - result = Distros.send(distro_method) if distro.detected? - end - result || Distros.generic + Distros.all.detect(&:detected?) || Distros.generic end end class Distro - RELEASE_FILE = nil - ETC_ISSUE_KEYWORDS = [] + attr_accessor :commands, :release_file, :etc_issue_keywords, :info_class - def self.etc_issue_keywords - self::ETC_ISSUE_KEYWORDS + def initialize(commands, release_file = nil, etc_issue_keywords = [], info_class = nil) + @commands = commands + @release_file = release_file + @etc_issue_keywords = etc_issue_keywords + @info_class = info_class end - def self.release_file - self::RELEASE_FILE + def id + @id ||= self.class.name.downcase.to_sym end - def self.detected? + def detected? detected_by_etc_issue? || detected_by_etc_release? end - def self.detected_by_etc_issue? - etc_issue_keywords.any? { |k| EtcIssue.instance.to_s.include?(k) } + def detected_by_etc_issue? + etc_issue_keywords && etc_issue_keywords.any? { |k| EtcIssue.instance.to_s.include?(k) } end - def self.detected_by_etc_release? + def detected_by_etc_release? release_file && File.exists?(release_file) end + + def command(name) + commands[name] + end + + def info(pkg) + info_class ? info_class.info(pkg) : nil + end end class Generic < Distro - COMMANDS = {} - def initialize - @id = :generic + super({}) end end class RedHat < Distro - COMMANDS = {:service => '/sbin/service', - :chkconfig => '/sbin/chkconfig', - :parted => '/sbin/parted', - :mount => '/bin/mount', - :umount => '/bin/umount', - :shutdown => '/sbin/shutdown', - :mke2fs => '/sbin/mke2fs', - :fdisk => '/sbin/fdisk', - :dd => '/bin/dd', - :vgdisplay => '/sbin/vgdisplay', - :pvdisplay => '/sbin/pvdisplay', - :lvdisplay => '/sbin/lvdisplay', - :lvextend => '/sbin/lvextend', - :vgextend => '/sbin/vgextend', - :lvcreate => '/sbin/lvcreate', - :pvcreate => '/sbin/pvcreate', - :vgcreate => '/sbin/vgcreate'} - - def initialize - @id = :redhat + def initialize(extra_commands, release_file, etc_issue_keywords) + super({ + :service => '/sbin/service', + :chkconfig => '/sbin/chkconfig', + :parted => '/sbin/parted', + :mount => '/bin/mount', + :umount => '/bin/umount', + :shutdown => '/sbin/shutdown', + :mke2fs => '/sbin/mke2fs', + :fdisk => '/sbin/fdisk', + :dd => '/bin/dd', + :vgdisplay => '/sbin/vgdisplay', + :pvdisplay => '/sbin/pvdisplay', + :lvdisplay => '/sbin/lvdisplay', + :lvextend => '/sbin/lvextend', + :vgextend => '/sbin/vgextend', + :lvcreate => '/sbin/lvcreate', + :pvcreate => '/sbin/pvcreate', + :vgcreate => '/sbin/vgcreate' + }.merge(extra_commands), release_file, etc_issue_keywords, LinuxAdmin::Rpm) end end class RHEL < RedHat - RELEASE_FILE = "/etc/redhat-release" - ETC_ISSUE_KEYWORDS = ['red hat', 'Red Hat', 'centos', 'CentOS'] - - COMMANDS = COMMANDS.merge( - :rpm => '/bin/rpm' - ) def initialize - @id = :rhel + super({:rpm => '/bin/rpm'}, '/etc/redhat-release', ['red hat', 'Red Hat', 'centos', 'CentOS']) end + + # def detected? + # super || File.exists?("/etc/redhat-release") + # end end class Fedora < RedHat - RELEASE_FILE = "/etc/fedora-release" - ETC_ISSUE_KEYWORDS = ['Fedora'] - - COMMANDS = COMMANDS.merge( - :rpm => '/usr/bin/rpm' - ) def initialize - @id = :fedora + super({:rpm => '/usr/bin/rpm'}, "/etc/fedora-release", ['Fedora']) end end class Ubuntu < Distro - ETC_ISSUE_KEYWORDS = ['ubuntu'] - - COMMANDS = {} - def initialize - @id = :ubuntu + super({}, nil, ['ubuntu'], LinuxAdmin::Deb) end end end diff --git a/lib/linux_admin/package.rb b/lib/linux_admin/package.rb deleted file mode 100644 index 364591c..0000000 --- a/lib/linux_admin/package.rb +++ /dev/null @@ -1,18 +0,0 @@ -# LinuxAdmin Abstract Package Representation -# -# Copyright (C) 2013 Red Hat Inc. -# Licensed under the MIT License - -class LinuxAdmin - class Package < LinuxAdmin - def self.info(pkg) - if Distros::Distro.local == Distros.redhat - return Rpm.info(pkg) - elsif Distros::Distro.local == Distros.ubuntu - return Deb.info(pkg) - end - - nil - end - end -end diff --git a/lib/linux_admin/rpm.rb b/lib/linux_admin/rpm.rb index d396e95..6d10301 100644 --- a/lib/linux_admin/rpm.rb +++ b/lib/linux_admin/rpm.rb @@ -1,7 +1,7 @@ class LinuxAdmin - class Rpm < Package + class Rpm def self.rpm_cmd - Distros.local.class::COMMANDS[:rpm] + Distros.local.command(:rpm) end def self.list_installed diff --git a/spec/common_spec.rb b/spec/common_spec.rb index bce924a..5e9e4fc 100644 --- a/spec/common_spec.rb +++ b/spec/common_spec.rb @@ -1,17 +1,7 @@ require 'spec_helper' describe LinuxAdmin::Common do - before do - class TestClass - extend LinuxAdmin::Common - end - end - - after do - Object.send(:remove_const, :TestClass) - end - - subject { TestClass } + subject { Class.new { include LinuxAdmin::Common }.new } context "#cmd" do it "looks up local command from id" do diff --git a/spec/distro_spec.rb b/spec/distro_spec.rb index 6a64dae..c609ec8 100644 --- a/spec/distro_spec.rb +++ b/spec/distro_spec.rb @@ -52,6 +52,25 @@ end end + describe "#info" do + it "dispatches to redhat lookup mechanism" do + stub_distro(LinuxAdmin::Distros.rhel) + expect(LinuxAdmin::Rpm).to receive(:info).with('ruby') + LinuxAdmin::Distros.local.info 'ruby' + end + + it "dispatches to ubuntu lookup mechanism" do + stub_distro(LinuxAdmin::Distros.ubuntu) + expect(LinuxAdmin::Deb).to receive(:info).with('ruby') + LinuxAdmin::Distros.local.info 'ruby' + end + + it "dispatches to ubuntu lookup mechanism" do + stub_distro(LinuxAdmin::Distros.generic) + expect { LinuxAdmin::Distros.local.info 'ruby' }.not_to raise_error + end + end + private def exists(files) diff --git a/spec/package_spec.rb b/spec/package_spec.rb deleted file mode 100644 index 98eb940..0000000 --- a/spec/package_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe LinuxAdmin::Package do - describe "#info" do - it "dispatches to redhat lookup mechanism" do - LinuxAdmin::Distros::Distro.should_receive(:local).and_return(LinuxAdmin::Distros.redhat) - LinuxAdmin::Rpm.should_receive(:info).with('ruby') - described_class.info 'ruby' - end - - it "dispatches to ubuntu lookup mechanism" do - LinuxAdmin::Distros::Distro.should_receive(:local).twice.and_return(LinuxAdmin::Distros.ubuntu) - LinuxAdmin::Deb.should_receive(:info).with('ruby') - described_class.info 'ruby' - end - end -end From 91dd147e6f8d07f6db86b01f7adcf4953a14df12 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Sun, 30 Mar 2014 00:20:59 -0400 Subject: [PATCH 3/5] Clean up etc_issue - remove knowledge of internals from specs - lazy load file - add include? method --- lib/linux_admin/distro.rb | 4 ++-- lib/linux_admin/etc_issue.rb | 12 +++++------ spec/distro_spec.rb | 6 +++--- spec/etc_issue_spec.rb | 40 ++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 6 ++++++ 5 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 spec/etc_issue_spec.rb diff --git a/lib/linux_admin/distro.rb b/lib/linux_admin/distro.rb index e77eb5c..4529ae6 100644 --- a/lib/linux_admin/distro.rb +++ b/lib/linux_admin/distro.rb @@ -52,7 +52,7 @@ def detected? end def detected_by_etc_issue? - etc_issue_keywords && etc_issue_keywords.any? { |k| EtcIssue.instance.to_s.include?(k) } + etc_issue_keywords && etc_issue_keywords.any? { |k| EtcIssue.instance.include?(k) } end def detected_by_etc_release? @@ -100,7 +100,7 @@ def initialize(extra_commands, release_file, etc_issue_keywords) class RHEL < RedHat def initialize - super({:rpm => '/bin/rpm'}, '/etc/redhat-release', ['red hat', 'Red Hat', 'centos', 'CentOS']) + super({:rpm => '/bin/rpm'}, '/etc/redhat-release', ['red hat','centos']) end # def detected? diff --git a/lib/linux_admin/etc_issue.rb b/lib/linux_admin/etc_issue.rb index 33c184d..2de69ea 100644 --- a/lib/linux_admin/etc_issue.rb +++ b/lib/linux_admin/etc_issue.rb @@ -11,18 +11,16 @@ class EtcIssue PATH = '/etc/issue' - def initialize - refresh + def include?(osname) + data.downcase.include?(osname.to_s.downcase) end - def to_s - @data.to_s + def data + @data ||= File.exists?(PATH) ? File.read(PATH) : "" end - private - def refresh - @data = File.exists?(PATH) ? File.read(PATH) : "" + @data = nil end end end diff --git a/spec/distro_spec.rb b/spec/distro_spec.rb index c609ec8..2364fa4 100644 --- a/spec/distro_spec.rb +++ b/spec/distro_spec.rb @@ -14,7 +14,7 @@ ['centos', :rhel]].each do |i, d| context "/etc/issue contains '#{i}'" do before(:each) do - expect(LinuxAdmin::EtcIssue.instance).to receive(:to_s).at_least(:once).and_return(i) + etc_issue_contains(i) exists("/etc/fedora-release" => false, "/etc/redhat-release" => false) end @@ -27,7 +27,7 @@ context "/etc/issue did not match" do before(:each) do - LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return('') + etc_issue_contains('') end context "/etc/redhat-release exists" do @@ -46,7 +46,7 @@ end it "returns Distros.generic" do - LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return('') + etc_issue_contains('') exists("/etc/fedora-release" => false, "/etc/redhat-release" => false) expect(subject).to eq(LinuxAdmin::Distros.generic) end diff --git a/spec/etc_issue_spec.rb b/spec/etc_issue_spec.rb new file mode 100644 index 0000000..bc911d4 --- /dev/null +++ b/spec/etc_issue_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' +require 'stringio' + +describe LinuxAdmin::EtcIssue do + subject { described_class.instance } + before do + # Reset the singleton so subsequent tests get a new instance + subject.refresh + end + + it "should not find the phrase when the file is missing" do + expect(File).to receive(:exists?).with('/etc/issue').at_least(:once).and_return(false) + expect(subject).not_to include("phrase") + end + + it "should not find phrase when the file is empty" do + etc_issue_contains("") + expect(subject).not_to include("phrase") + end + + it "should not find phrase when the file has a different phrase" do + etc_issue_contains("something\nelse") + expect(subject).not_to include("phrase") + end + + it "should find phrase in same case" do + etc_issue_contains("phrase") + expect(subject).to include("phrase") + end + + it "should find upper phrase in file" do + etc_issue_contains("PHRASE\nother") + expect(subject).to include("phrase") + end + + it "should find phrase when searching with upper" do + etc_issue_contains("other\nphrase") + expect(subject).to include("PHRASE") + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 691a87c..9b5fe51 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -37,6 +37,12 @@ require 'linux_admin' +def etc_issue_contains(contents) + LinuxAdmin::EtcIssue.instance.send(:refresh) + allow(File).to receive(:exists?).with('/etc/issue').at_least(:once).and_return(true) + allow(File).to receive(:read).with('/etc/issue').at_least(:once).and_return(contents) +end + def stub_distro(distro = LinuxAdmin::Distros.rhel) # simply alias test distro to redhat distro for time being LinuxAdmin::Distros.stub(:local => distro) From cc1152a0165aa74d62c4f40152775eef018ce9f7 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Sun, 30 Mar 2014 00:45:56 -0400 Subject: [PATCH 4/5] use a PATH instead of hardcoding commands --- lib/linux_admin/distro.rb | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/lib/linux_admin/distro.rb b/lib/linux_admin/distro.rb index 4529ae6..02e6179 100644 --- a/lib/linux_admin/distro.rb +++ b/lib/linux_admin/distro.rb @@ -34,10 +34,10 @@ def self.local end class Distro - attr_accessor :commands, :release_file, :etc_issue_keywords, :info_class + attr_accessor :release_file, :etc_issue_keywords, :info_class - def initialize(commands, release_file = nil, etc_issue_keywords = [], info_class = nil) - @commands = commands + def initialize(release_file = nil, etc_issue_keywords = [], info_class = nil) + @path = %w(/sbin /bin /usr/bin /usr/sbin) @release_file = release_file @etc_issue_keywords = etc_issue_keywords @info_class = info_class @@ -60,7 +60,7 @@ def detected_by_etc_release? end def command(name) - commands[name] + @path.collect { |dir| "#{dir}/#{name}" }.detect { |cmd| File.exists?(cmd) } end def info(pkg) @@ -70,37 +70,19 @@ def info(pkg) class Generic < Distro def initialize - super({}) + super() end end class RedHat < Distro - def initialize(extra_commands, release_file, etc_issue_keywords) - super({ - :service => '/sbin/service', - :chkconfig => '/sbin/chkconfig', - :parted => '/sbin/parted', - :mount => '/bin/mount', - :umount => '/bin/umount', - :shutdown => '/sbin/shutdown', - :mke2fs => '/sbin/mke2fs', - :fdisk => '/sbin/fdisk', - :dd => '/bin/dd', - :vgdisplay => '/sbin/vgdisplay', - :pvdisplay => '/sbin/pvdisplay', - :lvdisplay => '/sbin/lvdisplay', - :lvextend => '/sbin/lvextend', - :vgextend => '/sbin/vgextend', - :lvcreate => '/sbin/lvcreate', - :pvcreate => '/sbin/pvcreate', - :vgcreate => '/sbin/vgcreate' - }.merge(extra_commands), release_file, etc_issue_keywords, LinuxAdmin::Rpm) + def initialize(release_file, etc_issue_keywords) + super(release_file, etc_issue_keywords, LinuxAdmin::Rpm) end end class RHEL < RedHat def initialize - super({:rpm => '/bin/rpm'}, '/etc/redhat-release', ['red hat','centos']) + super('/etc/redhat-release', ['red hat','centos']) end # def detected? @@ -110,13 +92,13 @@ def initialize class Fedora < RedHat def initialize - super({:rpm => '/usr/bin/rpm'}, "/etc/fedora-release", ['Fedora']) + super("/etc/fedora-release", ['Fedora']) end end class Ubuntu < Distro def initialize - super({}, nil, ['ubuntu'], LinuxAdmin::Deb) + super(nil, ['ubuntu'], LinuxAdmin::Deb) end end end From 3b0f239ac8390f8cce5c4d05d18929041bac92c1 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Sun, 30 Mar 2014 01:08:12 -0400 Subject: [PATCH 5/5] remove extra distro classes --- lib/linux_admin/distro.rb | 49 +++++---------------------------------- 1 file changed, 6 insertions(+), 43 deletions(-) diff --git a/lib/linux_admin/distro.rb b/lib/linux_admin/distro.rb index 02e6179..31fba7b 100644 --- a/lib/linux_admin/distro.rb +++ b/lib/linux_admin/distro.rb @@ -8,19 +8,19 @@ class LinuxAdmin module Distros def self.generic - @generic ||= Generic.new + @generic ||= Distro.new(:generic) end def self.rhel - @rhel ||= RHEL.new + @rhel ||= Distro.new(:rhel, '/etc/redhat-release', ['red hat', 'centos'], LinuxAdmin::Rpm) end def self.fedora - @fedora ||= Fedora.new + @fedora ||= Distro.new(:fedora, "/etc/fedora-release", ['Fedora'], LinuxAdmin::Rpm) end def self.ubuntu - @ubuntu ||= Ubuntu.new + @ubuntu ||= Distro.new(:ubuntu, nil, ['ubuntu'], LinuxAdmin::Deb) end def self.all @@ -36,17 +36,14 @@ def self.local class Distro attr_accessor :release_file, :etc_issue_keywords, :info_class - def initialize(release_file = nil, etc_issue_keywords = [], info_class = nil) + def initialize(id, release_file = nil, etc_issue_keywords = [], info_class = nil) + @id = id @path = %w(/sbin /bin /usr/bin /usr/sbin) @release_file = release_file @etc_issue_keywords = etc_issue_keywords @info_class = info_class end - def id - @id ||= self.class.name.downcase.to_sym - end - def detected? detected_by_etc_issue? || detected_by_etc_release? end @@ -67,39 +64,5 @@ def info(pkg) info_class ? info_class.info(pkg) : nil end end - - class Generic < Distro - def initialize - super() - end - end - - class RedHat < Distro - def initialize(release_file, etc_issue_keywords) - super(release_file, etc_issue_keywords, LinuxAdmin::Rpm) - end - end - - class RHEL < RedHat - def initialize - super('/etc/redhat-release', ['red hat','centos']) - end - - # def detected? - # super || File.exists?("/etc/redhat-release") - # end - end - - class Fedora < RedHat - def initialize - super("/etc/fedora-release", ['Fedora']) - end - end - - class Ubuntu < Distro - def initialize - super(nil, ['ubuntu'], LinuxAdmin::Deb) - end - end end end