linux_admin 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/linux_admin.rb CHANGED
@@ -16,6 +16,7 @@ require 'linux_admin/mountable'
16
16
  require 'linux_admin/disk'
17
17
  require 'linux_admin/hosts'
18
18
  require 'linux_admin/partition'
19
+ require 'linux_admin/etc_issue'
19
20
  require 'linux_admin/distro'
20
21
  require 'linux_admin/system'
21
22
  require 'linux_admin/fstab'
@@ -3,7 +3,7 @@ require 'awesome_spawn'
3
3
  class LinuxAdmin
4
4
  module Common
5
5
  def cmd(cmd)
6
- Distro.local.class::COMMANDS[cmd]
6
+ Distros::Distro.local.class::COMMANDS[cmd]
7
7
  end
8
8
 
9
9
  def run(cmd, options = {})
@@ -3,35 +3,9 @@
3
3
  # Copyright (C) 2013 Red Hat Inc.
4
4
  # Licensed under the MIT License
5
5
 
6
- class LinuxAdmin
7
- class Distro < LinuxAdmin
8
- attr_accessor :id
9
-
10
- def initialize(id)
11
- @id = id
12
- end
13
-
14
- def self.local
15
- @local ||= begin
16
- if File.exists?('/etc/issue')
17
- issue = File.read('/etc/issue')
18
- if issue.include?('ubuntu')
19
- return Distros.ubuntu
20
- elsif ['Fedora', 'red hat', 'Red Hat', 'centos'].any? { |d| issue.include?(d) }
21
- return Distros.redhat
22
- end
23
-
24
- elsif File.exists?('/etc/redhat-release') ||
25
- File.exists?('/etc/fedora-release')
26
- return Distros.redhat
27
- end
28
-
29
- Distros.generic
30
- end
31
- end
32
-
33
- end
6
+ require 'linux_admin/etc_issue'
34
7
 
8
+ class LinuxAdmin
35
9
  module Distros
36
10
  def self.generic
37
11
  @generic ||= Generic.new
@@ -41,6 +15,14 @@ class LinuxAdmin
41
15
  @redhat ||= RedHat.new
42
16
  end
43
17
 
18
+ def self.rhel
19
+ @rhel ||= RHEL.new
20
+ end
21
+
22
+ def self.fedora
23
+ @fedora ||= Fedora.new
24
+ end
25
+
44
26
  def self.ubuntu
45
27
  @ubuntu ||= Ubuntu.new
46
28
  end
@@ -49,6 +31,49 @@ class LinuxAdmin
49
31
  @distros ||= [generic, redhat, ubuntu]
50
32
  end
51
33
 
34
+ def self.local
35
+ Distro.local
36
+ end
37
+
38
+ class Distro
39
+ RELEASE_FILE = ''
40
+ ETC_ISSUE_KEYWORDS = []
41
+
42
+ def self.etc_issue_keywords
43
+ self::ETC_ISSUE_KEYWORDS
44
+ end
45
+
46
+ def self.release_file
47
+ self::RELEASE_FILE
48
+ end
49
+
50
+ def self.local
51
+ # this can be cleaned up..
52
+ @local ||= begin
53
+ result = nil
54
+ Distros.constants.each do |cdistro|
55
+ distro_method = cdistro.to_s.downcase.to_sym
56
+ distro = Distros.const_get(cdistro)
57
+ next unless distro < Distro
58
+ result = Distros.send(distro_method) if distro.detected?
59
+ end
60
+ result || Distros.generic
61
+ end
62
+ end
63
+
64
+ def self.detected?
65
+ detected_by_etc_issue? || detected_by_etc_release?
66
+ end
67
+
68
+ def self.detected_by_etc_issue?
69
+ etc_issue_keywords.any? { |k| EtcIssue.instance.to_s.include?(k) }
70
+ end
71
+
72
+ def self.detected_by_etc_release?
73
+ File.exists?(release_file)
74
+ end
75
+ end
76
+
52
77
  class Generic < Distro
53
78
  COMMANDS = {}
54
79
 
@@ -81,7 +106,33 @@ class LinuxAdmin
81
106
  end
82
107
  end
83
108
 
109
+ class RHEL < RedHat
110
+ RELEASE_FILE = "/etc/redhat-release"
111
+ ETC_ISSUE_KEYWORDS = ['red hat', 'Red Hat', 'centos', 'CentOS']
112
+
113
+ COMMANDS = COMMANDS.merge(
114
+ :rpm => '/bin/rpm'
115
+ )
116
+ def initialize
117
+ @id = :rhel
118
+ end
119
+ end
120
+
121
+ class Fedora < RedHat
122
+ RELEASE_FILE = "/etc/fedora-release"
123
+ ETC_ISSUE_KEYWORDS = ['Fedora']
124
+
125
+ COMMANDS = COMMANDS.merge(
126
+ :rpm => '/usr/bin/rpm'
127
+ )
128
+ def initialize
129
+ @id = :fedora
130
+ end
131
+ end
132
+
84
133
  class Ubuntu < Distro
134
+ ETC_ISSUE_KEYWORDS = ['ubuntu']
135
+
85
136
  COMMANDS = {}
86
137
 
87
138
  def initialize
@@ -0,0 +1,28 @@
1
+ # LinuxAdmin /etc/issue Representation
2
+ #
3
+ # Copyright (C) 2014 Red Hat Inc.
4
+ # Licensed under the MIT License
5
+
6
+ require 'singleton'
7
+
8
+ class LinuxAdmin
9
+ class EtcIssue
10
+ include Singleton
11
+
12
+ PATH = '/etc/issue'
13
+
14
+ def initialize
15
+ refresh
16
+ end
17
+
18
+ def to_s
19
+ @data.to_s
20
+ end
21
+
22
+ private
23
+
24
+ def refresh
25
+ @data = File.exists?(PATH) ? File.read(PATH) : ""
26
+ end
27
+ end
28
+ end
@@ -6,9 +6,9 @@
6
6
  class LinuxAdmin
7
7
  class Package < LinuxAdmin
8
8
  def self.info(pkg)
9
- if Distro.local == Distros.redhat
9
+ if Distros::Distro.local == Distros.redhat
10
10
  return Rpm.info(pkg)
11
- elsif Distro.local == Distros.ubuntu
11
+ elsif Distros::Distro.local == Distros.ubuntu
12
12
  return Deb.info(pkg)
13
13
  end
14
14
 
@@ -1,9 +1,11 @@
1
1
  class LinuxAdmin
2
2
  class Rpm < Package
3
- RPM_CMD = '/usr/bin/rpm'
3
+ def self.rpm_cmd
4
+ Distros::Distro.local.class::COMMANDS[:rpm]
5
+ end
4
6
 
5
7
  def self.list_installed
6
- out = run!("rpm -qa --qf \"%{NAME} %{VERSION}-%{RELEASE}\n\"").output
8
+ out = run!("#{rpm_cmd} -qa --qf \"%{NAME} %{VERSION}-%{RELEASE}\n\"").output
7
9
  out.split("\n").each_with_object({}) do |line, pkg_hash|
8
10
  name, ver = line.split(" ")
9
11
  pkg_hash[name] = ver
@@ -21,8 +23,12 @@ class LinuxAdmin
21
23
  def self.info(pkg)
22
24
  params = { "-qi" => pkg}
23
25
  in_description = false
24
- out = run!(RPM_CMD, :params => params).output
26
+ out = run!(rpm_cmd, :params => params).output
27
+ # older versions of rpm may have multiple fields per line,
28
+ # split up lines with multiple tags/values:
29
+ out.gsub!(/(^.*:.*)\s\s+(.*:.*)$/, "\\1\n\\2")
25
30
  out.split("\n").each.with_object({}) do |line, rpm|
31
+ next if !line || line.empty?
26
32
  tag,value = line.split(':')
27
33
  tag = tag.strip
28
34
  if tag == 'Description'
@@ -1,3 +1,3 @@
1
1
  class LinuxAdmin
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
data/spec/common_spec.rb CHANGED
@@ -15,9 +15,9 @@ describe LinuxAdmin::Common do
15
15
 
16
16
  context "#cmd" do
17
17
  it "looks up local command from id" do
18
- d = double(LinuxAdmin::Distro)
18
+ d = double(LinuxAdmin::Distros::Distro)
19
19
  d.class::COMMANDS = {:sh => '/bin/sh'}
20
- LinuxAdmin::Distro.should_receive(:local).and_return(d)
20
+ LinuxAdmin::Distros::Distro.should_receive(:local).and_return(d)
21
21
  subject.cmd(:sh).should == '/bin/sh'
22
22
  end
23
23
  end
data/spec/distro_spec.rb CHANGED
@@ -1,52 +1,53 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe LinuxAdmin::Distro do
3
+ describe LinuxAdmin::Distros::Distro do
4
4
  describe "#local" do
5
- after(:each) do
6
- # distro generates a local copy, reset after each run
7
- LinuxAdmin::Distro.instance_variable_set(:@local, nil)
8
- end
9
-
10
5
  [['ubuntu', :ubuntu],
11
- ['Fedora', :redhat],
12
- ['red hat', :redhat],
13
- ['centos', :redhat]].each do |i,d|
6
+ ['Fedora', :fedora],
7
+ ['red hat', :rhel],
8
+ ['CentOS', :rhel],
9
+ ['centos', :rhel]].each do |i, d|
14
10
  context "/etc/issue contains '#{i}'" do
15
11
  before(:each) do
16
- File.should_receive(:exists?).with('/etc/issue').and_return(true)
17
- File.should_receive(:read).with('/etc/issue').and_return(i)
12
+ LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return(i)
13
+ File.should_receive(:exists?).at_least(:once).and_return(false)
18
14
  end
19
15
 
20
16
  it "returns Distros.#{d}" do
21
- LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.send(d)
17
+ distro = LinuxAdmin::Distros.send(d)
18
+ described_class.local.should == distro
22
19
  end
23
20
  end
24
21
  end
25
22
 
26
23
  context "/etc/issue did not match" do
27
24
  before(:each) do
28
- File.should_receive(:exists?).with('/etc/issue').and_return(false)
25
+ LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return('')
29
26
  end
30
27
 
31
28
  context "/etc/redhat-release exists" do
32
- it "returns Distros.redhat" do
29
+ it "returns Distros.rhel" do
33
30
  File.should_receive(:exists?).with('/etc/redhat-release').and_return(true)
34
- LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.redhat
31
+ LinuxAdmin::Distros::Fedora.should_receive(:detected?).and_return(false)
32
+ File.should_receive(:exists?).at_least(:once).and_call_original
33
+ described_class.local.should == LinuxAdmin::Distros.rhel
35
34
  end
36
35
  end
37
36
 
38
37
  context "/etc/fedora-release exists" do
39
- it "returns Distros.redhat" do
38
+ it "returns Distros.fedora" do
40
39
  File.should_receive(:exists?).with('/etc/redhat-release').and_return(false)
41
40
  File.should_receive(:exists?).with('/etc/fedora-release').and_return(true)
42
- LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.redhat
41
+ File.should_receive(:exists?).at_least(:once).and_call_original
42
+ described_class.local.should == LinuxAdmin::Distros.fedora
43
43
  end
44
44
  end
45
45
  end
46
46
 
47
47
  it "returns Distros.generic" do
48
- File.stub(:exists?).and_return(false)
49
- LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.generic
48
+ LinuxAdmin::EtcIssue.instance.should_receive(:to_s).at_least(:once).and_return('')
49
+ File.should_receive(:exists?).at_least(:once).and_return(false)
50
+ described_class.local.should == LinuxAdmin::Distros.generic
50
51
  end
51
52
  end
52
53
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe LinuxAdmin::LogicalVolume do
4
4
  before(:each) do
5
- LinuxAdmin::Distro.stub(:local => LinuxAdmin::Distros::Test.new)
5
+ LinuxAdmin::Distros::Distro.stub(:local => LinuxAdmin::Distros::Test.new)
6
6
 
7
7
  @logical_volumes = <<eos
8
8
  /dev/vg_foobar/lv_swap:vg_foobar:3:1:-1:2:4128768:63:-1:0:-1:253:0
data/spec/package_spec.rb CHANGED
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  describe LinuxAdmin::Package do
4
4
  describe "#info" do
5
5
  it "dispatches to redhat lookup mechanism" do
6
- LinuxAdmin::Distro.should_receive(:local).and_return(LinuxAdmin::Distros.redhat)
6
+ LinuxAdmin::Distros::Distro.should_receive(:local).and_return(LinuxAdmin::Distros.redhat)
7
7
  LinuxAdmin::Rpm.should_receive(:info).with('ruby')
8
8
  described_class.info 'ruby'
9
9
  end
10
10
 
11
11
  it "dispatches to ubuntu lookup mechanism" do
12
- LinuxAdmin::Distro.should_receive(:local).twice.and_return(LinuxAdmin::Distros.ubuntu)
12
+ LinuxAdmin::Distros::Distro.should_receive(:local).twice.and_return(LinuxAdmin::Distros.ubuntu)
13
13
  LinuxAdmin::Deb.should_receive(:info).with('ruby')
14
14
  described_class.info 'ruby'
15
15
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe LinuxAdmin::PhysicalVolume do
4
4
  before(:each) do
5
- LinuxAdmin::Distro.stub(:local => LinuxAdmin::Distros::Test.new)
5
+ LinuxAdmin::Distros::Distro.stub(:local => LinuxAdmin::Distros::Test.new)
6
6
 
7
7
  @physical_volumes = <<eos
8
8
  /dev/vda2:vg_foobar:24139776:-1:8:8:-1:32768:368:0:368:pxR32D-YkC2-PfHe-zOwb-eaGD-9Ar0-mAOl9u
data/spec/rpm_spec.rb CHANGED
@@ -59,9 +59,9 @@ object-oriented programming. It has many features to process text
59
59
  files and to do system management tasks (as in Perl). It is simple,
60
60
  straight-forward, and extensible.
61
61
  EOS
62
- described_class.should_receive(:run!).
63
- with(described_class::RPM_CMD, :params => {"-qi" => "ruby"}).
64
- and_return(double(:output => data))
62
+ arguments = [described_class.rpm_cmd, :params => {"-qi" => "ruby"}]
63
+ result = AwesomeSpawn::CommandResult.new("", data, "", 0)
64
+ described_class.should_receive(:run!).with(*arguments).and_return(result)
65
65
  metadata = described_class.info("ruby")
66
66
  metadata['name'].should == 'ruby'
67
67
  metadata['version'].should == '2.0.0.247'
data/spec/service_spec.rb CHANGED
@@ -3,8 +3,8 @@ require 'spec_helper'
3
3
  describe LinuxAdmin::Service do
4
4
  before(:each) do
5
5
  # stub distro.local to return test distro for command lookup
6
- LinuxAdmin::Distro.stub(:local).
7
- and_return(LinuxAdmin::Distros::Test.new)
6
+ LinuxAdmin::Distros::Distro.stub(:local)
7
+ .and_return(LinuxAdmin::Distros::Test.new)
8
8
 
9
9
  @service = LinuxAdmin::Service.new 'foo'
10
10
  end
data/spec/spec_helper.rb CHANGED
@@ -21,6 +21,9 @@ RSpec.configure do |config|
21
21
 
22
22
  config.after do
23
23
  clear_caches
24
+
25
+ # reset the distro, tested in various placed & used extensively
26
+ LinuxAdmin::Distros::Distro.instance_variable_set(:@local, nil)
24
27
  end
25
28
  end
26
29
 
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe LinuxAdmin::VolumeGroup do
4
4
  before(:each) do
5
- LinuxAdmin::Distro.stub(:local => LinuxAdmin::Distros::Test.new)
5
+ LinuxAdmin::Distros::Distro.stub(:local => LinuxAdmin::Distros::Test.new)
6
6
 
7
7
  @groups = <<eos
8
8
  vg_foobar:r/w:772:-1:0:2:2:-1:0:1:1:12058624:32768:368:368:0:tILZUF-IspH-H90I-pT5j-vVFl-b76L-zWx3CW
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,11 @@ authors:
9
9
  - Jason Frey
10
10
  - Mo Morsi
11
11
  - Joe Rafaniello
12
+ - Keenan Brock
12
13
  autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
- date: 2014-02-12 00:00:00.000000000 Z
16
+ date: 2014-03-21 00:00:00.000000000 Z
16
17
  dependencies:
17
18
  - !ruby/object:Gem::Dependency
18
19
  name: bundler
@@ -172,6 +173,7 @@ email:
172
173
  - fryguy9@gmail.com
173
174
  - mmorsi@redhat.com
174
175
  - jrafanie@redhat.com
176
+ - kbrock@redhat.com
175
177
  executables: []
176
178
  extensions: []
177
179
  extra_rdoc_files: []
@@ -181,6 +183,7 @@ files:
181
183
  - lib/linux_admin/deb.rb
182
184
  - lib/linux_admin/disk.rb
183
185
  - lib/linux_admin/distro.rb
186
+ - lib/linux_admin/etc_issue.rb
184
187
  - lib/linux_admin/exceptions.rb
185
188
  - lib/linux_admin/fstab.rb
186
189
  - lib/linux_admin/hosts.rb
@@ -251,18 +254,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
251
254
  - - ! '>='
252
255
  - !ruby/object:Gem::Version
253
256
  version: '0'
254
- segments:
255
- - 0
256
- hash: -1812183474622246574
257
257
  required_rubygems_version: !ruby/object:Gem::Requirement
258
258
  none: false
259
259
  requirements:
260
260
  - - ! '>='
261
261
  - !ruby/object:Gem::Version
262
262
  version: '0'
263
- segments:
264
- - 0
265
- hash: -1812183474622246574
266
263
  requirements: []
267
264
  rubyforge_project:
268
265
  rubygems_version: 1.8.23