linux_admin 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/linux_admin/common.rb +3 -1
- data/lib/linux_admin/fstab.rb +49 -0
- data/lib/linux_admin/service.rb +19 -0
- data/lib/linux_admin/version.rb +1 -1
- data/lib/linux_admin.rb +1 -0
- data/spec/fstab_spec.rb +27 -0
- data/spec/service_spec.rb +45 -0
- metadata +5 -8
data/lib/linux_admin/common.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'shellwords'
|
2
2
|
|
3
3
|
class LinuxAdmin
|
4
|
+
class CommandError < RuntimeError; end
|
5
|
+
|
4
6
|
module Common
|
5
7
|
def write(file, content)
|
6
8
|
raise ArgumentError, "file and content can not be empty" if file.blank? || content.blank?
|
@@ -24,7 +26,7 @@ class LinuxAdmin
|
|
24
26
|
elsif options[:return_exitstatus] || exitstatus == 0
|
25
27
|
exitstatus
|
26
28
|
else
|
27
|
-
raise "
|
29
|
+
raise CommandError, "#{build_cmd(cmd, params)}: exit code: #{exitstatus}"
|
28
30
|
end
|
29
31
|
rescue
|
30
32
|
return nil if options[:return_exitstatus]
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# LinuxAdmin fstab Representation
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Red Hat Inc.
|
4
|
+
# Licensed under the MIT License
|
5
|
+
|
6
|
+
require 'singleton'
|
7
|
+
|
8
|
+
class LinuxAdmin
|
9
|
+
class FSTabEntry < LinuxAdmin
|
10
|
+
attr_accessor :device
|
11
|
+
attr_accessor :mount_point
|
12
|
+
attr_accessor :fs_type
|
13
|
+
attr_accessor :mount_options
|
14
|
+
attr_accessor :dumpable
|
15
|
+
attr_accessor :fsck_order
|
16
|
+
end
|
17
|
+
|
18
|
+
class FSTab < LinuxAdmin
|
19
|
+
include Singleton
|
20
|
+
|
21
|
+
attr_accessor :entries
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
refresh
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def refresh
|
30
|
+
@entries = []
|
31
|
+
f = File.read('/etc/fstab')
|
32
|
+
f.each_line { |line|
|
33
|
+
first_char = line.strip[0]
|
34
|
+
if first_char != '#' && first_char !~ /\s/
|
35
|
+
columns = line.split
|
36
|
+
entry = FSTabEntry.new
|
37
|
+
entry.device = columns[0]
|
38
|
+
entry.mount_point = columns[1]
|
39
|
+
entry.fs_type = columns[2]
|
40
|
+
entry.mount_options = columns[3]
|
41
|
+
entry.dumpable = columns[4].to_i
|
42
|
+
entry.fsck_order = columns[5].to_i
|
43
|
+
@entries << entry
|
44
|
+
end
|
45
|
+
}
|
46
|
+
self
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/linux_admin/service.rb
CHANGED
@@ -20,21 +20,40 @@ class LinuxAdmin
|
|
20
20
|
def enable
|
21
21
|
run(cmd(:systemctl),
|
22
22
|
:params => { nil => ["enable", "#{id}.service"] })
|
23
|
+
self
|
23
24
|
end
|
24
25
|
|
25
26
|
def disable
|
26
27
|
run(cmd(:systemctl),
|
27
28
|
:params => { nil => ["disable", "#{id}.service"] })
|
29
|
+
self
|
28
30
|
end
|
29
31
|
|
30
32
|
def start
|
31
33
|
run(cmd(:service),
|
32
34
|
:params => { nil => [id, "start"] })
|
35
|
+
self
|
33
36
|
end
|
34
37
|
|
35
38
|
def stop
|
36
39
|
run(cmd(:service),
|
37
40
|
:params => { nil => [id, "stop"] })
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def restart
|
45
|
+
status =
|
46
|
+
run(cmd(:service),
|
47
|
+
:params => { nil => [id, "restart"] },
|
48
|
+
:return_exitstatus => true)
|
49
|
+
|
50
|
+
# attempt to manually stop/start if restart fails
|
51
|
+
if status != 0
|
52
|
+
self.stop
|
53
|
+
self.start
|
54
|
+
end
|
55
|
+
|
56
|
+
self
|
38
57
|
end
|
39
58
|
end
|
40
59
|
end
|
data/lib/linux_admin/version.rb
CHANGED
data/lib/linux_admin.rb
CHANGED
data/spec/fstab_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LinuxAdmin::FSTab do
|
4
|
+
it "creates FSTabEntry for each line in fstab" do
|
5
|
+
fstab = <<eos
|
6
|
+
/dev/sda1 / ext4 defaults 1 1
|
7
|
+
/dev/sda2 swap swap defaults 0 0
|
8
|
+
eos
|
9
|
+
File.should_receive(:read).with('/etc/fstab').and_return(fstab)
|
10
|
+
entries = LinuxAdmin::FSTab.instance.entries
|
11
|
+
entries.size.should == 2
|
12
|
+
|
13
|
+
entries[0].device.should == '/dev/sda1'
|
14
|
+
entries[0].mount_point.should == '/'
|
15
|
+
entries[0].fs_type.should == 'ext4'
|
16
|
+
entries[0].mount_options.should == 'defaults'
|
17
|
+
entries[0].dumpable.should == 1
|
18
|
+
entries[0].fsck_order.should == 1
|
19
|
+
|
20
|
+
entries[1].device.should == '/dev/sda2'
|
21
|
+
entries[1].mount_point.should == 'swap'
|
22
|
+
entries[1].fs_type.should == 'swap'
|
23
|
+
entries[1].mount_options.should == 'defaults'
|
24
|
+
entries[1].dumpable.should == 0
|
25
|
+
entries[1].fsck_order.should == 0
|
26
|
+
end
|
27
|
+
end
|
data/spec/service_spec.rb
CHANGED
@@ -42,6 +42,11 @@ describe LinuxAdmin::Service do
|
|
42
42
|
:params => { nil => [ 'enable', 'foo.service']})
|
43
43
|
@service.enable
|
44
44
|
end
|
45
|
+
|
46
|
+
it "returns self" do
|
47
|
+
@service.should_receive(:run) # stub out cmd invocation
|
48
|
+
@service.enable.should == @service
|
49
|
+
end
|
45
50
|
end
|
46
51
|
|
47
52
|
describe "#disable" do
|
@@ -51,6 +56,11 @@ describe LinuxAdmin::Service do
|
|
51
56
|
:params => { nil => [ 'disable', 'foo.service']})
|
52
57
|
@service.disable
|
53
58
|
end
|
59
|
+
|
60
|
+
it "returns self" do
|
61
|
+
@service.should_receive(:run)
|
62
|
+
@service.disable.should == @service
|
63
|
+
end
|
54
64
|
end
|
55
65
|
|
56
66
|
describe "#start" do
|
@@ -60,6 +70,11 @@ describe LinuxAdmin::Service do
|
|
60
70
|
:params => { nil => [ 'foo', 'start']})
|
61
71
|
@service.start
|
62
72
|
end
|
73
|
+
|
74
|
+
it "returns self" do
|
75
|
+
@service.should_receive(:run)
|
76
|
+
@service.start.should == @service
|
77
|
+
end
|
63
78
|
end
|
64
79
|
|
65
80
|
describe "#stop" do
|
@@ -69,5 +84,35 @@ describe LinuxAdmin::Service do
|
|
69
84
|
:params => { nil => [ 'foo', 'stop']})
|
70
85
|
@service.stop
|
71
86
|
end
|
87
|
+
|
88
|
+
it "returns self" do
|
89
|
+
@service.should_receive(:run)
|
90
|
+
@service.stop.should == @service
|
91
|
+
end
|
72
92
|
end
|
93
|
+
|
94
|
+
describe "#restart" do
|
95
|
+
it "stops service" do
|
96
|
+
@service.should_receive(:run).
|
97
|
+
with(@service.cmd(:service),
|
98
|
+
:params => { nil => [ 'foo', 'restart']},
|
99
|
+
:return_exitstatus => true).and_return(0)
|
100
|
+
@service.restart
|
101
|
+
end
|
102
|
+
|
103
|
+
context "service restart fails" do
|
104
|
+
it "manually stops/starts service" do
|
105
|
+
@service.should_receive(:run).and_return(1)
|
106
|
+
@service.should_receive(:stop)
|
107
|
+
@service.should_receive(:start)
|
108
|
+
@service.restart
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
it "returns self" do
|
113
|
+
@service.should_receive(:run).and_return(0)
|
114
|
+
@service.restart.should == @service
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
73
118
|
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.
|
4
|
+
version: 0.1.2
|
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-07-
|
12
|
+
date: 2013-07-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- lib/linux_admin/common.rb
|
166
166
|
- lib/linux_admin/disk.rb
|
167
167
|
- lib/linux_admin/distro.rb
|
168
|
+
- lib/linux_admin/fstab.rb
|
168
169
|
- lib/linux_admin/logical_volume.rb
|
169
170
|
- lib/linux_admin/partition.rb
|
170
171
|
- lib/linux_admin/rhn.rb
|
@@ -185,6 +186,7 @@ files:
|
|
185
186
|
- spec/data/yum/second.repo
|
186
187
|
- spec/disk_spec.rb
|
187
188
|
- spec/distro_spec.rb
|
189
|
+
- spec/fstab_spec.rb
|
188
190
|
- spec/linux_admin_spec.rb
|
189
191
|
- spec/partition_spec.rb
|
190
192
|
- spec/rhn_spec.rb
|
@@ -207,18 +209,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
207
209
|
- - ! '>='
|
208
210
|
- !ruby/object:Gem::Version
|
209
211
|
version: '0'
|
210
|
-
segments:
|
211
|
-
- 0
|
212
|
-
hash: -2093528175376818552
|
213
212
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
213
|
none: false
|
215
214
|
requirements:
|
216
215
|
- - ! '>='
|
217
216
|
- !ruby/object:Gem::Version
|
218
217
|
version: '0'
|
219
|
-
segments:
|
220
|
-
- 0
|
221
|
-
hash: -2093528175376818552
|
222
218
|
requirements: []
|
223
219
|
rubyforge_project:
|
224
220
|
rubygems_version: 1.8.25
|
@@ -236,6 +232,7 @@ test_files:
|
|
236
232
|
- spec/data/yum/second.repo
|
237
233
|
- spec/disk_spec.rb
|
238
234
|
- spec/distro_spec.rb
|
235
|
+
- spec/fstab_spec.rb
|
239
236
|
- spec/linux_admin_spec.rb
|
240
237
|
- spec/partition_spec.rb
|
241
238
|
- spec/rhn_spec.rb
|