linux_admin 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,10 @@ class LinuxAdmin
9
9
  end
10
10
  end
11
11
 
12
+ def cmd(cmd)
13
+ Distro.local.class::COMMANDS[cmd]
14
+ end
15
+
12
16
  def run(cmd, options = {})
13
17
  params = options[:params] || options[:parameters]
14
18
 
@@ -33,14 +37,14 @@ class LinuxAdmin
33
37
  private
34
38
 
35
39
  def sanitize(params)
36
- return {} if params.blank?
37
- params.each_with_object({}) do |(k, v), h|
38
- h[k] =
39
- case v
40
- when Array; v.collect {|s| s.shellescape}
41
- when NilClass; v
42
- else v.shellescape
43
- end
40
+ return [] if params.blank?
41
+ params.collect do |k, v|
42
+ v = case v
43
+ when Array; v.collect(&:shellescape)
44
+ when NilClass; v
45
+ else v.to_s.shellescape
46
+ end
47
+ [k, v]
44
48
  end
45
49
  end
46
50
 
@@ -91,4 +95,4 @@ class LinuxAdmin
91
95
  Thread.current[THREAD_SYNC_KEY] = value
92
96
  end
93
97
  end
94
- end
98
+ end
@@ -0,0 +1,60 @@
1
+ # LinuxAdmin Disk Representation
2
+ #
3
+ # Copyright (C) 2013 Red Hat Inc.
4
+ # Licensed under the MIT License
5
+
6
+ require 'linux_admin/partition'
7
+
8
+ class LinuxAdmin
9
+ class Disk < LinuxAdmin
10
+ attr_accessor :path
11
+
12
+ def self.local
13
+ Dir.glob('/dev/[vhs]d[a-z]').collect do |d|
14
+ Disk.new :path => d
15
+ end
16
+ end
17
+
18
+ def initialize(args = {})
19
+ @path = args[:path]
20
+ end
21
+
22
+ def partitions
23
+ @partitions ||= begin
24
+ partitions = []
25
+
26
+ # requires sudo
27
+ out = run(cmd(:parted),
28
+ :return_output => true,
29
+ :params => { nil => [@path, 'print'] })
30
+
31
+ out.each_line do |l|
32
+ if l =~ /^ [0-9].*/
33
+ p = l.split
34
+ id,size,fs_type = p[0], p[3], p[5]
35
+ if size =~ /([0-9\.]*)([KMG])B/
36
+ size = case $2
37
+ when 'K' then
38
+ $1.to_f.kilobytes
39
+ when 'M' then
40
+ $1.to_f.megabytes
41
+ when 'G' then
42
+ $1.to_f.gigabytes
43
+ end
44
+ end
45
+ partitions << Partition.new(:disk => self,
46
+ :id => id.to_i,
47
+ :size => size,
48
+ :fs_type => fs_type)
49
+ end
50
+ end
51
+
52
+ partitions
53
+ end
54
+ end
55
+
56
+ def create_partition
57
+ # TODO
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,81 @@
1
+ # LinuxAdmin Distro Representation
2
+ #
3
+ # Copyright (C) 2013 Red Hat Inc.
4
+ # Licensed under the MIT License
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', '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
34
+
35
+ module Distros
36
+ def self.generic
37
+ @generic ||= Generic.new
38
+ end
39
+
40
+ def self.redhat
41
+ @redhat ||= RedHat.new
42
+ end
43
+
44
+ def self.ubuntu
45
+ @ubuntu ||= Ubuntu.new
46
+ end
47
+
48
+ def self.all
49
+ @distros ||= [generic, redhat, ubuntu]
50
+ end
51
+
52
+ class Generic < Distro
53
+ COMMANDS = {}
54
+
55
+ def initialize
56
+ @id = :generic
57
+ end
58
+ end
59
+
60
+ class RedHat < Distro
61
+ COMMANDS = {:service => '/usr/sbin/service',
62
+ :systemctl => '/usr/bin/systemctl',
63
+ :parted => '/usr/sbin/parted',
64
+ :mount => '/usr/bin/mount',
65
+ :umount => '/usr/bin/umount',
66
+ :shutdown => '/usr/sbin/shutdown'}
67
+
68
+ def initialize
69
+ @id = :redhat
70
+ end
71
+ end
72
+
73
+ class Ubuntu < Distro
74
+ COMMANDS = {}
75
+
76
+ def initialize
77
+ @id = :ubuntu
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ # LinuxAdmin Logical Volume Representation
2
+ #
3
+ # Copyright (C) 2013 Red Hat Inc.
4
+ # Licensed under the MIT License
5
+
6
+ class LinuxAdmin
7
+ class LogicalVolume < LinuxAdmin
8
+ def initialize
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ # LinuxAdmin Partition Representation
2
+ #
3
+ # Copyright (C) 2013 Red Hat Inc.
4
+ # Licensed under the MIT License
5
+
6
+ require 'fileutils'
7
+
8
+ class LinuxAdmin
9
+ class Partition < LinuxAdmin
10
+ attr_accessor :id
11
+ attr_accessor :fs_type
12
+ attr_accessor :size
13
+ attr_accessor :disk
14
+ attr_accessor :mount_point
15
+
16
+ def initialize(args={})
17
+ @id = args[:id]
18
+ @size = args[:size]
19
+ @disk = args[:disk]
20
+ @fs_type = args[:fs_type]
21
+ end
22
+
23
+ def path
24
+ "#{disk.path}#{id}"
25
+ end
26
+
27
+ def mount(mount_point=nil)
28
+ @mount_point = mount_point
29
+ @mount_point =
30
+ "/mnt/#{disk.path.split(File::SEPARATOR).last}#{id}" if mount_point.nil?
31
+ FileUtils.mkdir(@mount_point) unless File.directory?(@mount_point)
32
+
33
+ run(cmd(:mount),
34
+ :params => { nil => [self.path, @mount_point] })
35
+ end
36
+
37
+ def umount
38
+ run(cmd(:umount),
39
+ :params => { nil => [@mount_point] })
40
+ end
41
+
42
+ def format_to(fs_type)
43
+ #TODO
44
+ end
45
+ end
46
+ end
@@ -14,5 +14,40 @@ class LinuxAdmin
14
14
  end
15
15
  id.length > 0
16
16
  end
17
+
18
+ def self.register(options)
19
+ cmd = "rhnreg_ks"
20
+ params = {}
21
+
22
+ if options[:activationkey]
23
+ params["--activationkey="] = options[:activationkey]
24
+ elsif options[:username] && options[:password]
25
+ params["--username="] = options[:username]
26
+ params["--password="] = options[:password]
27
+ else
28
+ raise ArgumentError, "activation key or username and password are required"
29
+ end
30
+
31
+ params["--proxy="] = options[:proxy_address] if options[:proxy_address]
32
+ params["--proxyUser="] = options[:proxy_username] if options[:proxy_username]
33
+ params["--proxyPassword="] = options[:proxy_password] if options[:proxy_password]
34
+ params["--serverUrl="] = options[:server_url] if options[:server_url]
35
+
36
+ run(cmd, :params => params)
37
+ end
38
+
39
+ def self.subscribe(options)
40
+ raise ArgumentError, "pools, username and password are required" if options[:pools].blank? || options[:username].blank? || options[:password].blank?
41
+ cmd = "rhn-channel -a"
42
+
43
+ pools = options[:pools].collect {|pool| ["--channel=", pool]}
44
+
45
+ params = {}
46
+ params["--user="] = options[:username]
47
+ params["--password="] = options[:password]
48
+ params = params.to_a + pools
49
+
50
+ run(cmd, :params => params)
51
+ end
17
52
  end
18
53
  end
@@ -0,0 +1,40 @@
1
+ # LinuxAdmin Service Representation
2
+ #
3
+ # Copyright (C) 2013 Red Hat Inc.
4
+ # Licensed under the MIT License
5
+
6
+ class LinuxAdmin
7
+ class Service < LinuxAdmin
8
+ attr_accessor :id
9
+
10
+ def initialize(id)
11
+ @id = id
12
+ end
13
+
14
+ def running?
15
+ run(cmd(:service),
16
+ :params => { nil => [id, "status"] },
17
+ :return_exitstatus => true) == 0
18
+ end
19
+
20
+ def enable
21
+ run(cmd(:systemctl),
22
+ :params => { nil => ["enable", "#{id}.service"] })
23
+ end
24
+
25
+ def disable
26
+ run(cmd(:systemctl),
27
+ :params => { nil => ["disable", "#{id}.service"] })
28
+ end
29
+
30
+ def start
31
+ run(cmd(:service),
32
+ :params => { nil => [id, "start"] })
33
+ end
34
+
35
+ def stop
36
+ run(cmd(:service),
37
+ :params => { nil => [id, "stop"] })
38
+ end
39
+ end
40
+ end
@@ -10,28 +10,24 @@ class LinuxAdmin
10
10
  run("subscription-manager refresh")
11
11
  end
12
12
 
13
- def self.register(options = {})
13
+ def self.register(options)
14
14
  raise ArgumentError, "username and password are required" unless options[:username] && options[:password]
15
15
  cmd = "subscription-manager register"
16
16
 
17
- params = {}
18
- if options[:username] && options[:password]
19
- params["--username="] = options[:username]
20
- params["--password="] = options[:password]
21
- end
22
- params["--org="] = options[:org] if options[:org] && options[:server_url]
23
- params["--proxy="] = options[:proxy_address] if options[:proxy_address]
24
- params["--proxyuser="] = options[:proxy_username] if options[:proxy_username]
25
- params["--proxypassword="] = options[:proxy_password] if options[:proxy_password]
26
- params["--serverurl="] = options[:server_url] if options[:server_url]
17
+ params = {"--username=" => options[:username], "--password=" => options[:password]}
18
+ params.merge!(proxy_params(options))
19
+ params["--org="] = options[:org] if options[:server_url] && options[:org]
20
+ params["--serverurl="] = options[:server_url] if options[:server_url]
27
21
 
28
22
  run(cmd, :params => params)
29
23
  end
30
24
 
31
- def self.subscribe(pool_id)
32
- params = {"--pool" => pool_id}
25
+ def self.subscribe(options)
26
+ cmd = "subscription-manager attach"
27
+ pools = options[:pools].collect {|pool| ["--pool", pool]}
28
+ params = proxy_params(options).to_a + pools
33
29
 
34
- run("subscription-manager attach", :params => params)
30
+ run(cmd, :params => params)
35
31
  end
36
32
 
37
33
  def self.available_subscriptions
@@ -51,5 +47,15 @@ class LinuxAdmin
51
47
  subscriptions_hash[hash[:pool_id]] = hash
52
48
  end
53
49
  end
50
+
51
+ private
52
+
53
+ def self.proxy_params(options)
54
+ config = {}
55
+ config["--proxy="] = options[:proxy_address] if options[:proxy_address]
56
+ config["--proxyuser="] = options[:proxy_username] if options[:proxy_username]
57
+ config["--proxypassword="] = options[:proxy_password] if options[:proxy_password]
58
+ config
59
+ end
54
60
  end
55
61
  end
@@ -0,0 +1,18 @@
1
+ # LinuxAdmin System Representation
2
+ #
3
+ # Copyright (C) 2013 Red Hat Inc.
4
+ # Licensed under the MIT License
5
+
6
+ class LinuxAdmin
7
+ class System < LinuxAdmin
8
+ def self.reboot!
9
+ run(cmd(:shutdown),
10
+ :params => { "-r" => "now" })
11
+ end
12
+
13
+ def self.shutdown!
14
+ run(cmd(:shutdown),
15
+ :params => { "-h" => "0" })
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  class LinuxAdmin
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/linux_admin.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'more_core_extensions/all'
2
- require 'active_support/core_ext/string'
2
+ require 'active_support/core_ext'
3
3
 
4
4
  require 'linux_admin/common'
5
5
  require 'linux_admin/rhn'
@@ -8,8 +8,15 @@ require 'linux_admin/subscription_manager'
8
8
  require 'linux_admin/version'
9
9
  require 'linux_admin/yum'
10
10
 
11
+ require 'linux_admin/service'
12
+ require 'linux_admin/disk'
13
+ require 'linux_admin/partition'
14
+ require 'linux_admin/distro'
15
+ require 'linux_admin/system'
16
+
11
17
  class LinuxAdmin
12
18
  extend Common
19
+ include Common
13
20
 
14
21
  def self.registered?
15
22
  !!self.registration_type
data/linux_admin.gemspec CHANGED
@@ -27,7 +27,7 @@ registration, updates, etc.
27
27
  spec.add_development_dependency "rspec", "~> 2.13"
28
28
  spec.add_development_dependency "coveralls"
29
29
 
30
- spec.add_dependency "activesupport"
30
+ spec.add_dependency "activesupport", "< 4.0"
31
31
  spec.add_dependency "inifile", "~> 2.0.2"
32
32
  spec.add_dependency "more_core_extensions"
33
33
  spec.add_dependency "nokogiri"
data/spec/common_spec.rb CHANGED
@@ -21,6 +21,10 @@ describe LinuxAdmin::Common do
21
21
  }
22
22
  end
23
23
 
24
+ let (:modified_params) do
25
+ params.to_a + [123, 456].collect {|pool| ["--pool", pool]}
26
+ end
27
+
24
28
  subject { TestClass }
25
29
 
26
30
  context ".write" do
@@ -29,11 +33,20 @@ describe LinuxAdmin::Common do
29
33
  end
30
34
  end
31
35
 
36
+ context ".cmd" do
37
+ it "looks up local command from id" do
38
+ d = stub(LinuxAdmin::Distro)
39
+ d.class::COMMANDS = { :sh => '/bin/sh' }
40
+ LinuxAdmin::Distro.should_receive(:local).and_return(d)
41
+ subject.cmd(:sh).should == '/bin/sh'
42
+ end
43
+ end
44
+
32
45
  context ".run" do
33
46
  context "with params" do
34
47
  it "sanitizes crazy params" do
35
- subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg")
36
- subject.run("true", :params => params, :return_exitstatus => true)
48
+ subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg --pool 123 --pool 456")
49
+ subject.run("true", :params => modified_params, :return_exitstatus => true)
37
50
  end
38
51
 
39
52
  it "as empty hash" do
@@ -93,4 +106,4 @@ describe LinuxAdmin::Common do
93
106
  end
94
107
  end
95
108
  end
96
- end
109
+ end
data/spec/disk_spec.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxAdmin::Disk do
4
+ describe "#local" do
5
+ it "returns local disks" do
6
+ Dir.should_receive(:glob).with('/dev/[vhs]d[a-z]').
7
+ and_return(['/dev/hda', '/dev/sda'])
8
+ disks = LinuxAdmin::Disk.local
9
+ paths = disks.collect { |disk| disk.path }
10
+ paths.should include('/dev/hda')
11
+ paths.should include('/dev/sda')
12
+ end
13
+ end
14
+
15
+ describe "#partitions" do
16
+ it "uses parted" do
17
+ disk = LinuxAdmin::Disk.new :path => '/dev/hda'
18
+ disk.should_receive(:run).
19
+ with(disk.cmd(:parted),
20
+ :return_output => true,
21
+ :params => { nil => ['/dev/hda', 'print'] }).and_return ""
22
+ disk.partitions
23
+ end
24
+
25
+ it "sets partitons" do
26
+ partitions = <<eos
27
+ Model: ATA TOSHIBA MK5061GS (scsi)
28
+ Disk /dev/sda: 500GB
29
+ Sector size (logical/physical): 512B/512B
30
+ Partition Table: msdos
31
+ Disk Flags:
32
+
33
+ Number Start End Size Type File system Flags
34
+ 1 1259MB 81.8GB 80.5GB primary ntfs
35
+ 2 81.8GB 162GB 80.5GB primary ext4
36
+ 3 162GB 163GB 1074MB logical linux-swap(v1)
37
+ eos
38
+ disk = LinuxAdmin::Disk.new
39
+ disk.should_receive(:run).and_return(partitions)
40
+
41
+ disk.partitions[0].id.should == 1
42
+ disk.partitions[0].disk.should == disk
43
+ disk.partitions[0].size.should == 80.5.gigabytes
44
+ disk.partitions[0].fs_type.should == 'ntfs'
45
+ disk.partitions[1].id.should == 2
46
+ disk.partitions[1].disk.should == disk
47
+ disk.partitions[1].size.should == 80.5.gigabytes
48
+ disk.partitions[1].fs_type.should == 'ext4'
49
+ disk.partitions[2].id.should == 3
50
+ disk.partitions[2].disk.should == disk
51
+ disk.partitions[2].size.should == 1074.megabytes
52
+ disk.partitions[2].fs_type.should == 'linux-swap(v1)'
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxAdmin::Distro do
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
+ [['ubuntu', :ubuntu],
11
+ ['Fedora', :redhat],
12
+ ['red hat', :redhat],
13
+ ['centos', :redhat]].each do |i,d|
14
+ context "/etc/issue contains '#{i}'" do
15
+ 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)
18
+ end
19
+
20
+ it "returns Distros.#{d}" do
21
+ LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.send(d)
22
+ end
23
+ end
24
+ end
25
+
26
+ context "/etc/issue did not match" do
27
+ before(:each) do
28
+ File.should_receive(:exists?).with('/etc/issue').and_return(false)
29
+ end
30
+
31
+ context "/etc/redhat-release exists" do
32
+ it "returns Distros.redhat" do
33
+ File.should_receive(:exists?).with('/etc/redhat-release').and_return(true)
34
+ LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.redhat
35
+ end
36
+ end
37
+
38
+ context "/etc/fedora-release exists" do
39
+ it "returns Distros.redhat" do
40
+ File.should_receive(:exists?).with('/etc/redhat-release').and_return(false)
41
+ File.should_receive(:exists?).with('/etc/fedora-release').and_return(true)
42
+ LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.redhat
43
+ end
44
+ end
45
+ end
46
+
47
+ it "returns Distros.generic" do
48
+ File.stub!(:exists?).and_return(false)
49
+ LinuxAdmin::Distro.local.should == LinuxAdmin::Distros.generic
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxAdmin::Partition do
4
+ before(:each) do
5
+ @disk = LinuxAdmin::Disk.new :path => '/dev/sda'
6
+ @partition = LinuxAdmin::Partition.new :disk => @disk, :id => 2
7
+
8
+ # stub out calls that modify system
9
+ FileUtils.stub(:mkdir)
10
+ @partition.stub(:run)
11
+ end
12
+
13
+ describe "#path" do
14
+ it "returns partition path" do
15
+ @partition.path.should == '/dev/sda2'
16
+ end
17
+ end
18
+
19
+ describe "#mount" do
20
+ it "sets mount point" do
21
+ @partition.should_receive(:run) # ignore actual mount cmd
22
+ @partition.mount
23
+ @partition.mount_point.should == '/mnt/sda2'
24
+ end
25
+
26
+ context "mountpoint does not exist" do
27
+ it "creates mountpoint" do
28
+ File.should_receive(:directory?).with('/mnt/sda2').and_return(false)
29
+ FileUtils.should_receive(:mkdir).with('/mnt/sda2')
30
+ @partition.should_receive(:run) # ignore actual mount cmd
31
+ @partition.mount
32
+ end
33
+ end
34
+
35
+ it "mounts partition" do
36
+ @partition.should_receive(:run).
37
+ with(@partition.cmd(:mount),
38
+ :params => { nil => ['/dev/sda2', '/mnt/sda2']})
39
+ @partition.mount
40
+ end
41
+ end
42
+
43
+ describe "#umount" do
44
+ it "unmounts partition" do
45
+ @partition.mount_point = '/mnt/sda2'
46
+ @partition.should_receive(:run).
47
+ with(@partition.cmd(:umount),
48
+ :params => { nil => ['/mnt/sda2']})
49
+ @partition.umount
50
+ end
51
+ end
52
+ end
data/spec/rhn_spec.rb CHANGED
@@ -21,4 +21,48 @@ describe LinuxAdmin::Rhn do
21
21
  expect(described_class).to_not be_registered
22
22
  end
23
23
  end
24
+
25
+ context ".register" do
26
+ it "no username or activation key" do
27
+ expect { described_class.register({}) }.to raise_error(ArgumentError)
28
+ end
29
+
30
+ it "with username and password" do
31
+ described_class.should_receive(:run).once.with("rhnreg_ks", {:params=>{"--username="=>"SomeUser", "--password="=>"SomePass", "--proxy="=>"1.2.3.4", "--proxyUser="=>"ProxyUser", "--proxyPassword="=>"ProxyPass", "--serverUrl="=>"192.168.1.1"}}).and_return(0)
32
+ described_class.register(
33
+ :username => "SomeUser",
34
+ :password => "SomePass",
35
+ :proxy_address => "1.2.3.4",
36
+ :proxy_username => "ProxyUser",
37
+ :proxy_password => "ProxyPass",
38
+ :server_url => "192.168.1.1",
39
+ )
40
+ end
41
+
42
+ it "with activation key" do
43
+ described_class.should_receive(:run).once.with("rhnreg_ks", {:params=>{"--activationkey="=>"123abc", "--proxy="=>"1.2.3.4", "--proxyUser="=>"ProxyUser", "--proxyPassword="=>"ProxyPass", "--serverUrl="=>"192.168.1.1"}}).and_return(0)
44
+ described_class.register(
45
+ :activationkey => "123abc",
46
+ :proxy_address => "1.2.3.4",
47
+ :proxy_username => "ProxyUser",
48
+ :proxy_password => "ProxyPass",
49
+ :server_url => "192.168.1.1",
50
+ )
51
+ end
52
+ end
53
+
54
+ context ".subscribe" do
55
+ it "without arguments" do
56
+ expect { described_class.subscribe({}) }.to raise_error(ArgumentError)
57
+ end
58
+
59
+ it "with pools" do
60
+ described_class.should_receive(:run).once.with("rhn-channel -a", {:params=>[["--user=", "SomeUser"], ["--password=", "SomePass"], ["--channel=", 123], ["--channel=", 456]]})
61
+ described_class.subscribe({
62
+ :username => "SomeUser",
63
+ :password => "SomePass",
64
+ :pools => [123, 456]
65
+ })
66
+ end
67
+ end
24
68
  end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxAdmin::Service do
4
+ before(:each) do
5
+ # stub distro.local to return test distro for command lookup
6
+ LinuxAdmin::Distro.stub(:local).
7
+ and_return(LinuxAdmin::Distros::Test.new)
8
+
9
+ @service = LinuxAdmin::Service.new 'foo'
10
+ end
11
+
12
+ describe "#running?" do
13
+ it "checks service" do
14
+ @service.should_receive(:run).
15
+ with(@service.cmd(:service),
16
+ :params => { nil => ['foo', 'status']},
17
+ :return_exitstatus => true)
18
+ @service.running?
19
+ end
20
+
21
+ context "service is running" do
22
+ it "returns true" do
23
+ @service = LinuxAdmin::Service.new :id => :foo
24
+ @service.should_receive(:run).and_return(0)
25
+ @service.should be_running
26
+ end
27
+ end
28
+
29
+ context "service is not running" do
30
+ it "returns false" do
31
+ @service = LinuxAdmin::Service.new :id => :foo
32
+ @service.should_receive(:run).and_return(1)
33
+ @service.should_not be_running
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#enable" do
39
+ it "enables service" do
40
+ @service.should_receive(:run).
41
+ with(@service.cmd(:systemctl),
42
+ :params => { nil => [ 'enable', 'foo.service']})
43
+ @service.enable
44
+ end
45
+ end
46
+
47
+ describe "#disable" do
48
+ it "disable service" do
49
+ @service.should_receive(:run).
50
+ with(@service.cmd(:systemctl),
51
+ :params => { nil => [ 'disable', 'foo.service']})
52
+ @service.disable
53
+ end
54
+ end
55
+
56
+ describe "#start" do
57
+ it "starts service" do
58
+ @service.should_receive(:run).
59
+ with(@service.cmd(:service),
60
+ :params => { nil => [ 'foo', 'start']})
61
+ @service.start
62
+ end
63
+ end
64
+
65
+ describe "#stop" do
66
+ it "stops service" do
67
+ @service.should_receive(:run).
68
+ with(@service.cmd(:service),
69
+ :params => { nil => [ 'foo', 'stop']})
70
+ @service.stop
71
+ end
72
+ end
73
+ end
data/spec/spec_helper.rb CHANGED
@@ -26,4 +26,11 @@ end
26
26
 
27
27
  def sample_output(to)
28
28
  File.read(data_file_path(to))
29
- end
29
+ end
30
+
31
+ class LinuxAdmin
32
+ module Distros
33
+ # simply alias test distro to redhat distro for time being
34
+ Distros::Test = Distros::RedHat
35
+ end
36
+ end
@@ -38,8 +38,8 @@ describe LinuxAdmin::SubscriptionManager do
38
38
  end
39
39
 
40
40
  it ".subscribe" do
41
- described_class.should_receive(:run).once
42
- described_class.subscribe(nil)
41
+ described_class.should_receive(:run).once.with("subscription-manager attach", {:params=>[["--pool", 123], ["--pool", 456]]})
42
+ described_class.subscribe({:pools => [123, 456]})
43
43
  end
44
44
 
45
45
  it ".available_subscriptions" do
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxAdmin::System do
4
+ describe "#reboot!" do
5
+ it "reboots the system" do
6
+ LinuxAdmin::System.should_receive(:run).
7
+ with(LinuxAdmin::System.cmd(:shutdown),
8
+ :params => { '-r' => 'now'})
9
+ LinuxAdmin::System.reboot!
10
+ end
11
+ end
12
+
13
+ describe "#shutdown!" do
14
+ it "shuts down the system" do
15
+ LinuxAdmin::System.should_receive(:run).
16
+ with(LinuxAdmin::System.cmd(:shutdown),
17
+ :params => { '-h' => '0'})
18
+ LinuxAdmin::System.shutdown!
19
+ end
20
+ end
21
+ end
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.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-16 00:00:00.000000000 Z
12
+ date: 2013-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -80,17 +80,17 @@ dependencies:
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - <
84
84
  - !ruby/object:Gem::Version
85
- version: '0'
85
+ version: '4.0'
86
86
  type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - <
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: '4.0'
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: inifile
96
96
  requirement: !ruby/object:Gem::Requirement
@@ -163,9 +163,15 @@ files:
163
163
  - examples/subscription_manager_hosted.rb
164
164
  - lib/linux_admin.rb
165
165
  - lib/linux_admin/common.rb
166
+ - lib/linux_admin/disk.rb
167
+ - lib/linux_admin/distro.rb
168
+ - lib/linux_admin/logical_volume.rb
169
+ - lib/linux_admin/partition.rb
166
170
  - lib/linux_admin/rhn.rb
167
171
  - lib/linux_admin/rpm.rb
172
+ - lib/linux_admin/service.rb
168
173
  - lib/linux_admin/subscription_manager.rb
174
+ - lib/linux_admin/system.rb
169
175
  - lib/linux_admin/version.rb
170
176
  - lib/linux_admin/yum.rb
171
177
  - linux_admin.gemspec
@@ -177,11 +183,16 @@ files:
177
183
  - spec/data/yum/first.repo
178
184
  - spec/data/yum/output_repoquery
179
185
  - spec/data/yum/second.repo
186
+ - spec/disk_spec.rb
187
+ - spec/distro_spec.rb
180
188
  - spec/linux_admin_spec.rb
189
+ - spec/partition_spec.rb
181
190
  - spec/rhn_spec.rb
182
191
  - spec/rpm_spec.rb
192
+ - spec/service_spec.rb
183
193
  - spec/spec_helper.rb
184
194
  - spec/subscription_manager_spec.rb
195
+ - spec/system_spec.rb
185
196
  - spec/yum_spec.rb
186
197
  homepage: http://github.com/ManageIQ/linux_admin
187
198
  licenses:
@@ -198,7 +209,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
198
209
  version: '0'
199
210
  segments:
200
211
  - 0
201
- hash: -3348050275566390969
212
+ hash: -2093528175376818552
202
213
  required_rubygems_version: !ruby/object:Gem::Requirement
203
214
  none: false
204
215
  requirements:
@@ -207,10 +218,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
218
  version: '0'
208
219
  segments:
209
220
  - 0
210
- hash: -3348050275566390969
221
+ hash: -2093528175376818552
211
222
  requirements: []
212
223
  rubyforge_project:
213
- rubygems_version: 1.8.24
224
+ rubygems_version: 1.8.25
214
225
  signing_key:
215
226
  specification_version: 3
216
227
  summary: LinuxAdmin is a module to simplify management of linux systems.
@@ -223,9 +234,14 @@ test_files:
223
234
  - spec/data/yum/first.repo
224
235
  - spec/data/yum/output_repoquery
225
236
  - spec/data/yum/second.repo
237
+ - spec/disk_spec.rb
238
+ - spec/distro_spec.rb
226
239
  - spec/linux_admin_spec.rb
240
+ - spec/partition_spec.rb
227
241
  - spec/rhn_spec.rb
228
242
  - spec/rpm_spec.rb
243
+ - spec/service_spec.rb
229
244
  - spec/spec_helper.rb
230
245
  - spec/subscription_manager_spec.rb
246
+ - spec/system_spec.rb
231
247
  - spec/yum_spec.rb