linux_admin 0.12.1 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4cff55729b03594f6057598b4c52e8b46f9d150f
4
- data.tar.gz: c4396ebccac857e757be07437c734b307682c359
3
+ metadata.gz: 0c5b96fbeb251ef6a0e5c6a2215b828d78ccaa94
4
+ data.tar.gz: 32276203e615b1a9a22e0d0899fa3eb1cc7f7551
5
5
  SHA512:
6
- metadata.gz: 304dae896d5b1d43efe3b92206c029ea305933eb38c5a37f0e77b7393b729542e71c4e18ebf01e0b1eed558ecd6512890609efde18a343f353f8a6b12935e290
7
- data.tar.gz: 90aea3e3a2f7f043e68c506fa342201c8ca3a7f8a5078e7a5abe500fc3cd187b7691b4393416af16793e955a76d4c9bfdb36a635fd906e9badc0267b4e6790f8
6
+ metadata.gz: a02c17575fc1db9259f7192c9ad138a69d2aa0bf5abf87059f5a91566b3ba0f0fea862279f24a69271eb4ec7d873ce3451b928b6e7ee4256b274dea67df92022
7
+ data.tar.gz: b7b51b7c0bab05f59e4cce1105abf548ca925be5ee2b2213d8ecebb2f055e41e508adb5eb72862426a15e36023f7d32b7ac5989afb5c78e1d412221f0982b366
@@ -0,0 +1,21 @@
1
+ module LinuxAdmin
2
+ class Chrony
3
+ def initialize(conf = "/etc/chrony.conf")
4
+ raise MissingConfigurationFileError, "#{conf} does not exist" unless File.exist?(conf)
5
+ @conf = conf
6
+ end
7
+
8
+ def clear_servers
9
+ data = File.read(@conf)
10
+ data.gsub!(/^server\s+.+\n/, "")
11
+ File.write(@conf, data)
12
+ end
13
+
14
+ def add_servers(*servers)
15
+ data = File.read(@conf)
16
+ data << "\n" unless data.end_with?("\n")
17
+ servers.each { |s| data << "server #{s}\n" }
18
+ File.write(@conf, data)
19
+ end
20
+ end
21
+ end
@@ -6,4 +6,6 @@ module LinuxAdmin
6
6
  end
7
7
 
8
8
  class NetworkInterfaceError < AwesomeSpawn::CommandResultError; end
9
+
10
+ class MissingConfigurationFileError < StandardError; end
9
11
  end
@@ -19,23 +19,17 @@ module LinuxAdmin
19
19
  def save
20
20
  cleanup_empty
21
21
  @raw_lines = assemble_lines
22
- File.write(@filename, @raw_lines.join("\n"))
22
+ File.write(@filename, @raw_lines.join("\n") + "\n")
23
23
  end
24
24
 
25
- def update_entry(address, hostname, comment = nil)
26
- # Delete entries for this hostname first
27
- @parsed_file.each {|i| i[:hosts].to_a.delete(hostname)}
25
+ def add_alias(address, hostname, comment = nil)
26
+ add_name(address, hostname, false, comment)
27
+ end
28
28
 
29
- # Add entry
30
- line_number = @parsed_file.find_path(address).first
29
+ alias_method :update_entry, :add_alias
31
30
 
32
- if line_number.blank?
33
- @parsed_file.push({:address => address, :hosts => [hostname], :comment => comment})
34
- else
35
- new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname)
36
- @parsed_file.store_path(line_number, :hosts, new_hosts)
37
- @parsed_file.store_path(line_number, :comment, comment) if comment
38
- end
31
+ def set_canonical_hostname(address, hostname, comment = nil)
32
+ add_name(address, hostname, true, comment)
39
33
  end
40
34
 
41
35
  def hostname=(name)
@@ -52,7 +46,28 @@ module LinuxAdmin
52
46
  result.success? ? result.output.strip : nil
53
47
  end
54
48
 
55
- private
49
+ private
50
+
51
+ def add_name(address, hostname, fqdn, comment)
52
+ # Delete entries for this hostname first
53
+ @parsed_file.each { |i| i[:hosts].to_a.delete(hostname) }
54
+
55
+ # Add entry
56
+ line_number = @parsed_file.find_path(address).first
57
+
58
+ if line_number.blank?
59
+ @parsed_file.push(:address => address, :hosts => [hostname], :comment => comment)
60
+ else
61
+ if fqdn
62
+ new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.unshift(hostname)
63
+ else
64
+ new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname)
65
+ end
66
+ @parsed_file.store_path(line_number, :hosts, new_hosts)
67
+ @parsed_file.store_path(line_number, :comment, comment) if comment
68
+ end
69
+ end
70
+
56
71
  def parse_file
57
72
  @parsed_file = []
58
73
  @raw_lines.each { |line| @parsed_file.push(parse_line(line.strip)) }
@@ -10,8 +10,9 @@ module LinuxAdmin
10
10
 
11
11
  # @param interface [String] Name of the network interface to manage
12
12
  def initialize(interface)
13
+ @interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{interface}")
14
+ raise MissingConfigurationFileError unless File.exist?(@interface_file)
13
15
  super
14
- @interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{@interface}")
15
16
  parse_conf
16
17
  end
17
18
 
@@ -25,6 +25,8 @@ module LinuxAdmin
25
25
  # Creates an instance of the correct NetworkInterface subclass for the local distro
26
26
  def self.new(*args)
27
27
  self == LinuxAdmin::NetworkInterface ? dist_class.new(*args) : super
28
+ rescue MissingConfigurationFileError
29
+ NetworkInterfaceGeneric.new(*args)
28
30
  end
29
31
 
30
32
  # @return [String] the interface for networking operations
@@ -156,7 +158,7 @@ module LinuxAdmin
156
158
  #
157
159
  # @param ip_output [String] The command output
158
160
  def parse_ip4(ip_output)
159
- cidr_ip = parse_ip_output(ip_output, /inet/, 1)
161
+ cidr_ip = parse_ip_output(ip_output, /inet /, 1)
160
162
  return unless cidr_ip
161
163
 
162
164
  @network_conf[:address] = cidr_ip.split('/')[0]
@@ -1,3 +1,3 @@
1
1
  module LinuxAdmin
2
- VERSION = "0.12.1"
2
+ VERSION = "0.13.0"
3
3
  end
data/lib/linux_admin.rb CHANGED
@@ -32,6 +32,7 @@ require 'linux_admin/time_date'
32
32
  require 'linux_admin/ip_address'
33
33
  require 'linux_admin/dns'
34
34
  require 'linux_admin/network_interface'
35
+ require 'linux_admin/chrony'
35
36
 
36
37
  module LinuxAdmin
37
38
  extend Common
@@ -0,0 +1,41 @@
1
+ describe LinuxAdmin::Chrony do
2
+ CHRONY_CONF = <<-EOF
3
+ # commented server baz.example.net
4
+ server foo.example.net
5
+ server bar.example.net iburst
6
+ driftfile /var/lib/chrony/drift
7
+ makestep 10 3
8
+ rtcsync
9
+ EOF
10
+
11
+ subject do
12
+ allow(File).to receive(:exist?).and_return(true)
13
+ described_class.new
14
+ end
15
+
16
+ describe ".new" do
17
+ it "raises when the given config file doesn't exist" do
18
+ expect { described_class.new("nonsense/file") }.to raise_error(LinuxAdmin::MissingConfigurationFileError)
19
+ end
20
+ end
21
+
22
+ describe "#clear_servers" do
23
+ it "removes all the server lines from the conf file" do
24
+ allow(File).to receive(:read).and_return(CHRONY_CONF.dup)
25
+ expect(File).to receive(:write) do |_file, contents|
26
+ expect(contents).to eq "# commented server baz.example.net\ndriftfile /var/lib/chrony/drift\nmakestep 10 3\nrtcsync\n"
27
+ end
28
+ subject.clear_servers
29
+ end
30
+ end
31
+
32
+ describe "#add_servers" do
33
+ it "adds server lines to the conf file" do
34
+ allow(File).to receive(:read).and_return(CHRONY_CONF.dup)
35
+ expect(File).to receive(:write) do |_file, contents|
36
+ expect(contents).to eq(CHRONY_CONF + "server baz.example.net\nserver foo.bar.example.com\n")
37
+ end
38
+ subject.add_servers("baz.example.net", "foo.bar.example.com")
39
+ end
40
+ end
41
+ end
data/spec/hosts_spec.rb CHANGED
@@ -32,12 +32,30 @@ describe LinuxAdmin::Hosts do
32
32
  end
33
33
  end
34
34
 
35
- describe "#save" do
36
- before do
37
- allow(File).to receive(:write)
35
+ describe "#set_canonical_hostname" do
36
+ it "removes an existing entry and creates a new one" do
37
+ expected_hash = [{:blank => true},
38
+ {:comment => "Some Comment"},
39
+ {:address => "127.0.0.1", :hosts => ["localhost", "localhost.localdomain"], :comment => "with a comment"},
40
+ {:address => "127.0.1.1", :hosts => []},
41
+ {:address => "1.2.3.4", :hosts => ["my.domain.local"], :comment => nil}]
42
+ @instance.set_canonical_hostname("1.2.3.4", "my.domain.local")
43
+ expect(@instance.parsed_file).to eq(expected_hash)
44
+ end
45
+
46
+ it "adds the hostname to the start of the hosts list" do
47
+ expected_hash = [{:blank => true},
48
+ {:comment => "Some Comment"},
49
+ {:address => "127.0.0.1", :hosts => ["examplehost.example.com", "localhost", "localhost.localdomain"], :comment => "with a comment"},
50
+ {:address => "127.0.1.1", :hosts => ["my.domain.local"]}]
51
+ @instance.set_canonical_hostname("127.0.0.1", "examplehost.example.com")
52
+ expect(@instance.parsed_file).to eq(expected_hash)
38
53
  end
54
+ end
39
55
 
56
+ describe "#save" do
40
57
  it "properly generates file with new content" do
58
+ allow(File).to receive(:write)
41
59
  expected_array = ["", "#Some Comment", "127.0.0.1 localhost localhost.localdomain #with a comment", "127.0.1.1 my.domain.local", "1.2.3.4 test"]
42
60
  @instance.update_entry("1.2.3.4", "test")
43
61
  @instance.save
@@ -45,11 +63,19 @@ describe LinuxAdmin::Hosts do
45
63
  end
46
64
 
47
65
  it "properly generates file with removed content" do
66
+ allow(File).to receive(:write)
48
67
  expected_array = ["", "#Some Comment", "127.0.0.1 localhost localhost.localdomain my.domain.local #with a comment"]
49
68
  @instance.update_entry("127.0.0.1", "my.domain.local")
50
69
  @instance.save
51
70
  expect(@instance.raw_lines).to eq(expected_array)
52
71
  end
72
+
73
+ it "ends the file with a new line" do
74
+ expect(File).to receive(:write) do |_file, contents|
75
+ expect(contents).to end_with("\n")
76
+ end
77
+ @instance.save
78
+ end
53
79
  end
54
80
 
55
81
  describe "#hostname=" do
@@ -34,12 +34,14 @@ EOF
34
34
  end
35
35
 
36
36
  subject(:dhcp_interface) do
37
+ allow(File).to receive(:exist?).and_return(true)
37
38
  stub_foreach_to_string(IFCFG_FILE_DHCP)
38
39
  allow(AwesomeSpawn).to receive(:run!).twice.and_return(result("", 0))
39
40
  described_class.new(DEVICE_NAME)
40
41
  end
41
42
 
42
43
  subject(:static_interface) do
44
+ allow(File).to receive(:exist?).and_return(true)
43
45
  stub_foreach_to_string(IFCFG_FILE_STATIC)
44
46
  allow(AwesomeSpawn).to receive(:run!).twice.and_return(result("", 0))
45
47
  described_class.new(DEVICE_NAME)
@@ -1,13 +1,22 @@
1
1
  describe LinuxAdmin::NetworkInterface do
2
2
  context "on redhat systems" do
3
- subject do
3
+ subject(:subj_success) do
4
4
  allow_any_instance_of(described_class).to receive(:ip_show).and_return(nil)
5
5
  allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.rhel)
6
6
  described_class.dist_class(true)
7
+ allow(File).to receive(:exist?).and_return(true)
7
8
  allow(File).to receive(:foreach).and_return("")
8
9
  described_class.new("eth0")
9
10
  end
10
11
 
12
+ subject(:subj_failure) do
13
+ allow_any_instance_of(described_class).to receive(:ip_show).and_return(nil)
14
+ allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.rhel)
15
+ described_class.dist_class(true)
16
+ allow(File).to receive(:exist?).and_return(false)
17
+ described_class.new("eth0")
18
+ end
19
+
11
20
  describe ".dist_class" do
12
21
  it "returns NetworkInterfaceRH" do
13
22
  allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.rhel)
@@ -17,7 +26,11 @@ describe LinuxAdmin::NetworkInterface do
17
26
 
18
27
  describe ".new" do
19
28
  it "creates a NetworkInterfaceRH instance" do
20
- expect(subject).to be_an_instance_of(LinuxAdmin::NetworkInterfaceRH)
29
+ expect(subj_success).to be_an_instance_of(LinuxAdmin::NetworkInterfaceRH)
30
+ end
31
+
32
+ it "creates a NetworkInterfaceGeneric instance if the config file does not exist" do
33
+ expect(subj_failure).to be_an_instance_of(LinuxAdmin::NetworkInterfaceGeneric)
21
34
  end
22
35
  end
23
36
  end
@@ -78,6 +91,15 @@ describe LinuxAdmin::NetworkInterface do
78
91
  valid_lft forever preferred_lft forever
79
92
  IP_OUT
80
93
 
94
+ IP6_ADDR_OUT = <<-IP_OUT
95
+ 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
96
+ link/ether 00:0c:29:ed:0e:8b brd ff:ff:ff:ff:ff:ff
97
+ inet6 fe80::20c:29ff:feed:e8b/64 scope link
98
+ valid_lft forever preferred_lft forever
99
+ inet6 fd12:3456:789a:1::1/96 scope global
100
+ valid_lft forever preferred_lft forever
101
+ IP_OUT
102
+
81
103
  IP_ROUTE_OUT = <<-IP_OUT
82
104
  default via 192.168.1.1 dev eth0 proto static metric 100
83
105
  192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.9 metric 100
@@ -92,6 +114,15 @@ IP_OUT
92
114
  described_class.new("eth0")
93
115
  end
94
116
 
117
+ subject(:subj6) do
118
+ allow(LinuxAdmin::Distros).to receive(:local).and_return(LinuxAdmin::Distros.generic)
119
+ described_class.dist_class(true)
120
+
121
+ allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP6_ADDR_OUT, 0))
122
+ allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_return(result(IP_ROUTE_OUT, 0))
123
+ described_class.new("eth0")
124
+ end
125
+
95
126
  def result(output, exit_status)
96
127
  AwesomeSpawn::CommandResult.new("", output, "", exit_status)
97
128
  end
@@ -111,6 +142,13 @@ IP_OUT
111
142
  allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_raise(awesome_error)
112
143
  expect { subj.reload }.to raise_error(LinuxAdmin::NetworkInterfaceError)
113
144
  end
145
+
146
+ it "doesn't blow up when given only ipv6 addresses" do
147
+ subj6
148
+ allow(AwesomeSpawn).to receive(:run!).with(*IP_SHOW_ARGS).and_return(result(IP6_ADDR_OUT, 0))
149
+ allow(AwesomeSpawn).to receive(:run!).with(*IP_ROUTE_ARGS).and_return(result(IP_ROUTE_OUT, 0))
150
+ expect { subj.reload }.to_not raise_error
151
+ end
114
152
  end
115
153
 
116
154
  describe "#address" do
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.12.1
4
+ version: 0.13.0
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-10-22 00:00:00.000000000 Z
17
+ date: 2015-12-04 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: bundler
@@ -176,6 +176,7 @@ files:
176
176
  - LICENSE.txt
177
177
  - README.md
178
178
  - lib/linux_admin.rb
179
+ - lib/linux_admin/chrony.rb
179
180
  - lib/linux_admin/common.rb
180
181
  - lib/linux_admin/deb.rb
181
182
  - lib/linux_admin/disk.rb
@@ -211,6 +212,7 @@ files:
211
212
  - lib/linux_admin/volume_group.rb
212
213
  - lib/linux_admin/yum.rb
213
214
  - lib/linux_admin/yum/repo_file.rb
215
+ - spec/chrony_spec.rb
214
216
  - spec/common_spec.rb
215
217
  - spec/data/rhn/output_rhn-channel_list
216
218
  - spec/data/rhn/output_rhn-channel_list_available
@@ -283,6 +285,7 @@ signing_key:
283
285
  specification_version: 4
284
286
  summary: LinuxAdmin is a module to simplify management of linux systems.
285
287
  test_files:
288
+ - spec/chrony_spec.rb
286
289
  - spec/common_spec.rb
287
290
  - spec/data/rhn/output_rhn-channel_list
288
291
  - spec/data/rhn/output_rhn-channel_list_available