linux_admin 0.11.0 → 0.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/linux_admin.rb +2 -0
- data/lib/linux_admin/hosts.rb +16 -0
- data/lib/linux_admin/ip_address.rb +27 -0
- data/lib/linux_admin/time_date.rb +31 -0
- data/lib/linux_admin/version.rb +1 -1
- data/spec/data/time_date/timedatectl_output +14 -0
- data/spec/hosts_spec.rb +42 -0
- data/spec/ip_address_spec.rb +52 -0
- data/spec/time_date_spec.rb +74 -0
- metadata +11 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 284247598cea1f7eabd5d8c8d4fb4710c0e040e0
|
4
|
+
data.tar.gz: a4943e48131e68c3bd2a8cc21b8dcd6c441fa5cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04bb042b12b6b6155582bc4091965bf2968d3d01dfdde64277100ba461a668ba37b156e83f01754f0e8b07b2408ab4a8a8018b12cc56473ecde59a25d42897e4
|
7
|
+
data.tar.gz: 64a75e5340636156aa82cd16f96aedf96ecefcd7976e6d63e8b4a35e19d67a8c43f3eafc4b68cab0dcf8862c7451943f2d85f984aac5586e197b9a0d92426c3d
|
data/lib/linux_admin.rb
CHANGED
data/lib/linux_admin/hosts.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module LinuxAdmin
|
2
2
|
class Hosts
|
3
|
+
include Common
|
4
|
+
|
3
5
|
attr_accessor :filename
|
4
6
|
attr_accessor :raw_lines
|
5
7
|
attr_accessor :parsed_file
|
@@ -36,6 +38,20 @@ module LinuxAdmin
|
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
41
|
+
def hostname=(name)
|
42
|
+
if cmd?("hostnamectl")
|
43
|
+
run!(cmd('hostnamectl'), :params => ['set-hostname', name])
|
44
|
+
else
|
45
|
+
File.write("/etc/hostname", name)
|
46
|
+
run!(cmd('hostname'), :params => {:file => "/etc/hostname"})
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def hostname
|
51
|
+
result = run(cmd("hostname"))
|
52
|
+
result.success? ? result.output : nil
|
53
|
+
end
|
54
|
+
|
39
55
|
private
|
40
56
|
def parse_file
|
41
57
|
@parsed_file = []
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module LinuxAdmin
|
2
|
+
class IpAddress
|
3
|
+
include Common
|
4
|
+
|
5
|
+
def address
|
6
|
+
address_list.detect { |ip| IPAddr.new(ip).ipv4? }
|
7
|
+
end
|
8
|
+
|
9
|
+
def address6
|
10
|
+
address_list.detect { |ip| IPAddr.new(ip).ipv6? }
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def address_list
|
16
|
+
result = nil
|
17
|
+
# Added retry to account for slow DHCP not assigning an IP quickly at boot; specifically:
|
18
|
+
# https://github.com/ManageIQ/manageiq-appliance/commit/160d8ccbfbfd617bdb5445e56cdab66b9323b15b
|
19
|
+
5.times do
|
20
|
+
result = run(cmd("hostname"), :params => ["-I"])
|
21
|
+
break if result.success?
|
22
|
+
end
|
23
|
+
|
24
|
+
result.success? ? result.output.split(' ') : []
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module LinuxAdmin
|
2
|
+
class TimeDate
|
3
|
+
extend Common
|
4
|
+
COMMAND = 'timedatectl'
|
5
|
+
|
6
|
+
TimeCommandError = Class.new(StandardError)
|
7
|
+
|
8
|
+
def self.system_timezone_detailed
|
9
|
+
result = run(cmd(COMMAND), :params => ["status"])
|
10
|
+
result.output.split("\n").each do |l|
|
11
|
+
return l.split(':')[1].strip if l =~ /Time.*zone/
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.system_timezone
|
16
|
+
system_timezone_detailed.split[0]
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.system_time=(time)
|
20
|
+
run!(cmd(COMMAND), :params => ["set-time", "#{time.strftime("%F %T")}", :adjust_system_clock])
|
21
|
+
rescue AwesomeSpawn::CommandResultError => e
|
22
|
+
raise TimeCommandError, e.message
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.system_timezone=(zone)
|
26
|
+
run!(cmd(COMMAND), :params => ["set-timezone", zone])
|
27
|
+
rescue AwesomeSpawn::CommandResultError => e
|
28
|
+
raise TimeCommandError, e.message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/linux_admin/version.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
Local time: Thu 2015-09-24 17:54:02 EDT
|
2
|
+
Universal time: Thu 2015-09-24 21:54:02 UTC
|
3
|
+
RTC time: Thu 2015-09-24 21:54:02
|
4
|
+
Time zone: America/New_York (EDT, -0400)
|
5
|
+
NTP enabled: yes
|
6
|
+
NTP synchronized: yes
|
7
|
+
RTC in local TZ: no
|
8
|
+
DST active: yes
|
9
|
+
Last DST change: DST began at
|
10
|
+
Sun 2015-03-08 01:59:59 EST
|
11
|
+
Sun 2015-03-08 03:00:00 EDT
|
12
|
+
Next DST change: DST ends (the clock jumps one hour backwards) at
|
13
|
+
Sun 2015-11-01 01:59:59 EDT
|
14
|
+
Sun 2015-11-01 01:00:00 EST
|
data/spec/hosts_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
describe LinuxAdmin::Hosts do
|
2
|
+
TEST_HOSTNAME = "test-hostname"
|
2
3
|
etc_hosts = "\n #Some Comment\n127.0.0.1\tlocalhost localhost.localdomain # with a comment\n127.0.1.1 my.domain.local"
|
3
4
|
before do
|
4
5
|
allow(File).to receive(:read).and_return(etc_hosts)
|
@@ -50,4 +51,45 @@ describe LinuxAdmin::Hosts do
|
|
50
51
|
expect(@instance.raw_lines).to eq(expected_array)
|
51
52
|
end
|
52
53
|
end
|
54
|
+
|
55
|
+
describe "#hostname=" do
|
56
|
+
it "sets the hostname using hostnamectl when the command exists" do
|
57
|
+
spawn_args = [
|
58
|
+
@instance.cmd('hostnamectl'),
|
59
|
+
:params => ['set-hostname', TEST_HOSTNAME]
|
60
|
+
]
|
61
|
+
expect(@instance).to receive(:cmd?).with("hostnamectl").and_return(true)
|
62
|
+
expect(AwesomeSpawn).to receive(:run!).with(*spawn_args)
|
63
|
+
@instance.hostname = TEST_HOSTNAME
|
64
|
+
end
|
65
|
+
|
66
|
+
it "sets the hostname with hostname when hostnamectl does not exist" do
|
67
|
+
spawn_args = [
|
68
|
+
@instance.cmd('hostname'),
|
69
|
+
:params => {:file => "/etc/hostname"}
|
70
|
+
]
|
71
|
+
expect(@instance).to receive(:cmd?).with("hostnamectl").and_return(false)
|
72
|
+
expect(File).to receive(:write).with("/etc/hostname", TEST_HOSTNAME)
|
73
|
+
expect(AwesomeSpawn).to receive(:run!).with(*spawn_args)
|
74
|
+
@instance.hostname = TEST_HOSTNAME
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#hostname" do
|
79
|
+
let(:spawn_args) do
|
80
|
+
[@instance.cmd('hostname'), {}]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "returns the hostname" do
|
84
|
+
result = AwesomeSpawn::CommandResult.new("", TEST_HOSTNAME, nil, 0)
|
85
|
+
expect(AwesomeSpawn).to receive(:run).with(*spawn_args).and_return(result)
|
86
|
+
expect(@instance.hostname).to eq(TEST_HOSTNAME)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns nil when the command fails" do
|
90
|
+
result = AwesomeSpawn::CommandResult.new("", "", "An error has happened", 1)
|
91
|
+
expect(AwesomeSpawn).to receive(:run).with(*spawn_args).and_return(result)
|
92
|
+
expect(@instance.hostname).to be_nil
|
93
|
+
end
|
94
|
+
end
|
53
95
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
describe LinuxAdmin::IpAddress do
|
2
|
+
let(:ip) { described_class.new }
|
3
|
+
|
4
|
+
SPAWN_ARGS = [
|
5
|
+
described_class.new.cmd("hostname"),
|
6
|
+
:params => ["-I"]
|
7
|
+
]
|
8
|
+
|
9
|
+
def result(output, exit_status)
|
10
|
+
AwesomeSpawn::CommandResult.new("", output, "", exit_status)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#address" do
|
14
|
+
it "returns an address" do
|
15
|
+
ip_addr = "192.168.1.2"
|
16
|
+
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
|
17
|
+
expect(ip.address).to eq(ip_addr)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns nil when no address is found" do
|
21
|
+
ip_addr = ""
|
22
|
+
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*SPAWN_ARGS).and_return(result(ip_addr, 1))
|
23
|
+
expect(ip.address).to be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns only IPv4 addresses" do
|
27
|
+
ip_addr = "fd12:3456:789a:1::1 192.168.1.2"
|
28
|
+
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
|
29
|
+
expect(ip.address).to eq("192.168.1.2")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#address6" do
|
34
|
+
it "returns an address" do
|
35
|
+
ip_addr = "fd12:3456:789a:1::1"
|
36
|
+
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
|
37
|
+
expect(ip.address6).to eq(ip_addr)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns nil when no address is found" do
|
41
|
+
ip_addr = ""
|
42
|
+
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*SPAWN_ARGS).and_return(result(ip_addr, 1))
|
43
|
+
expect(ip.address6).to be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "returns only IPv6 addresses" do
|
47
|
+
ip_addr = "192.168.1.2 fd12:3456:789a:1::1"
|
48
|
+
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
|
49
|
+
expect(ip.address6).to eq("fd12:3456:789a:1::1")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
describe LinuxAdmin::TimeDate do
|
2
|
+
RUN_COMMAND = described_class.cmd("timedatectl")
|
3
|
+
|
4
|
+
def timedatectl_result
|
5
|
+
output = File.read(Pathname.new(data_file_path("time_date/timedatectl_output")))
|
6
|
+
AwesomeSpawn::CommandResult.new("", output, "", 0)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ".system_timezone_detailed" do
|
10
|
+
it "returns the correct timezone" do
|
11
|
+
awesome_spawn_args = [
|
12
|
+
RUN_COMMAND,
|
13
|
+
:params => ["status"]
|
14
|
+
]
|
15
|
+
expect(AwesomeSpawn).to receive(:run).with(*awesome_spawn_args).and_return(timedatectl_result)
|
16
|
+
tz = described_class.system_timezone_detailed
|
17
|
+
expect(tz).to eq("America/New_York (EDT, -0400)")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".system_timezone" do
|
22
|
+
it "returns the correct timezone" do
|
23
|
+
awesome_spawn_args = [
|
24
|
+
RUN_COMMAND,
|
25
|
+
:params => ["status"]
|
26
|
+
]
|
27
|
+
expect(AwesomeSpawn).to receive(:run).with(*awesome_spawn_args).and_return(timedatectl_result)
|
28
|
+
tz = described_class.system_timezone
|
29
|
+
expect(tz).to eq("America/New_York")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".system_time=" do
|
34
|
+
it "sets the time" do
|
35
|
+
time = Time.new(2015, 1, 1, 1, 1, 1)
|
36
|
+
awesome_spawn_args = [
|
37
|
+
RUN_COMMAND,
|
38
|
+
:params => ["set-time", "2015-01-01 01:01:01", :adjust_system_clock]
|
39
|
+
]
|
40
|
+
expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args)
|
41
|
+
described_class.system_time = time
|
42
|
+
end
|
43
|
+
|
44
|
+
it "raises when the command fails" do
|
45
|
+
time = Time.new(2015, 1, 1, 1, 1, 1)
|
46
|
+
err = AwesomeSpawn::CommandResultError.new("message", nil)
|
47
|
+
allow(AwesomeSpawn).to receive(:run!).and_raise(err)
|
48
|
+
expect do
|
49
|
+
described_class.send(:system_time=, time)
|
50
|
+
end.to raise_error(described_class::TimeCommandError, "message")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe ".system_timezone=" do
|
55
|
+
it "sets the timezone" do
|
56
|
+
zone = "Location/City"
|
57
|
+
awesome_spawn_args = [
|
58
|
+
RUN_COMMAND,
|
59
|
+
:params => ["set-timezone", zone]
|
60
|
+
]
|
61
|
+
expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args)
|
62
|
+
described_class.system_timezone = zone
|
63
|
+
end
|
64
|
+
|
65
|
+
it "raises when the command fails" do
|
66
|
+
zone = "Location/City"
|
67
|
+
err = AwesomeSpawn::CommandResultError.new("message", nil)
|
68
|
+
allow(AwesomeSpawn).to receive(:run!).and_raise(err)
|
69
|
+
expect do
|
70
|
+
described_class.send(:system_timezone=, zone)
|
71
|
+
end.to raise_error(described_class::TimeCommandError, "message")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
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.11.
|
4
|
+
version: 0.11.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Dunne
|
@@ -14,7 +14,7 @@ authors:
|
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
|
-
date: 2015-
|
17
|
+
date: 2015-09-30 00:00:00.000000000 Z
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: bundler
|
@@ -184,6 +184,7 @@ files:
|
|
184
184
|
- lib/linux_admin/exceptions.rb
|
185
185
|
- lib/linux_admin/fstab.rb
|
186
186
|
- lib/linux_admin/hosts.rb
|
187
|
+
- lib/linux_admin/ip_address.rb
|
187
188
|
- lib/linux_admin/logging.rb
|
188
189
|
- lib/linux_admin/logical_volume.rb
|
189
190
|
- lib/linux_admin/mountable.rb
|
@@ -200,6 +201,7 @@ files:
|
|
200
201
|
- lib/linux_admin/service/sys_v_init_service.rb
|
201
202
|
- lib/linux_admin/service/systemd_service.rb
|
202
203
|
- lib/linux_admin/system.rb
|
204
|
+
- lib/linux_admin/time_date.rb
|
203
205
|
- lib/linux_admin/version.rb
|
204
206
|
- lib/linux_admin/volume.rb
|
205
207
|
- lib/linux_admin/volume_group.rb
|
@@ -219,6 +221,7 @@ files:
|
|
219
221
|
- spec/data/subscription_manager/output_list_installed_subscribed
|
220
222
|
- spec/data/subscription_manager/output_orgs
|
221
223
|
- spec/data/subscription_manager/output_repos
|
224
|
+
- spec/data/time_date/timedatectl_output
|
222
225
|
- spec/data/yum/first.repo
|
223
226
|
- spec/data/yum/output_repo_list
|
224
227
|
- spec/data/yum/output_repoquery_multiple
|
@@ -230,6 +233,7 @@ files:
|
|
230
233
|
- spec/etc_issue_spec.rb
|
231
234
|
- spec/fstab_spec.rb
|
232
235
|
- spec/hosts_spec.rb
|
236
|
+
- spec/ip_address_spec.rb
|
233
237
|
- spec/logical_volume_spec.rb
|
234
238
|
- spec/mountable_spec.rb
|
235
239
|
- spec/partition_spec.rb
|
@@ -244,6 +248,7 @@ files:
|
|
244
248
|
- spec/spec_helper.rb
|
245
249
|
- spec/subscription_manager_spec.rb
|
246
250
|
- spec/system_spec.rb
|
251
|
+
- spec/time_date_spec.rb
|
247
252
|
- spec/volume_group_spec.rb
|
248
253
|
- spec/yum_spec.rb
|
249
254
|
homepage: http://github.com/ManageIQ/linux_admin
|
@@ -266,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
266
271
|
version: '0'
|
267
272
|
requirements: []
|
268
273
|
rubyforge_project:
|
269
|
-
rubygems_version: 2.4.
|
274
|
+
rubygems_version: 2.4.5.1
|
270
275
|
signing_key:
|
271
276
|
specification_version: 4
|
272
277
|
summary: LinuxAdmin is a module to simplify management of linux systems.
|
@@ -285,6 +290,7 @@ test_files:
|
|
285
290
|
- spec/data/subscription_manager/output_list_installed_subscribed
|
286
291
|
- spec/data/subscription_manager/output_orgs
|
287
292
|
- spec/data/subscription_manager/output_repos
|
293
|
+
- spec/data/time_date/timedatectl_output
|
288
294
|
- spec/data/yum/first.repo
|
289
295
|
- spec/data/yum/output_repo_list
|
290
296
|
- spec/data/yum/output_repoquery_multiple
|
@@ -296,6 +302,7 @@ test_files:
|
|
296
302
|
- spec/etc_issue_spec.rb
|
297
303
|
- spec/fstab_spec.rb
|
298
304
|
- spec/hosts_spec.rb
|
305
|
+
- spec/ip_address_spec.rb
|
299
306
|
- spec/logical_volume_spec.rb
|
300
307
|
- spec/mountable_spec.rb
|
301
308
|
- spec/partition_spec.rb
|
@@ -310,5 +317,6 @@ test_files:
|
|
310
317
|
- spec/spec_helper.rb
|
311
318
|
- spec/subscription_manager_spec.rb
|
312
319
|
- spec/system_spec.rb
|
320
|
+
- spec/time_date_spec.rb
|
313
321
|
- spec/volume_group_spec.rb
|
314
322
|
- spec/yum_spec.rb
|