linux_admin 0.3.0 → 0.4.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.
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in linux_admin.gemspec
4
4
  gemspec
5
+
6
+ gem "more_core_extensions", "=1.1.0", :git => "git://github.com/ManageIQ/more_core_extensions.git", :tag => "v1.1.0"
data/lib/linux_admin.rb CHANGED
@@ -12,6 +12,7 @@ require 'linux_admin/yum'
12
12
 
13
13
  require 'linux_admin/service'
14
14
  require 'linux_admin/disk'
15
+ require 'linux_admin/hosts'
15
16
  require 'linux_admin/partition'
16
17
  require 'linux_admin/distro'
17
18
  require 'linux_admin/system'
@@ -0,0 +1,73 @@
1
+ class LinuxAdmin
2
+ class Hosts < LinuxAdmin
3
+ attr_accessor :filename
4
+ attr_accessor :raw_lines
5
+ attr_accessor :parsed_file
6
+
7
+ def initialize(filename = "/etc/hosts")
8
+ @filename = filename
9
+ self.reload
10
+ end
11
+
12
+ def reload
13
+ @raw_lines = File.read(@filename).split("\n")
14
+ parse_file
15
+ end
16
+
17
+ def save
18
+ cleanup_empty
19
+ @raw_lines = assemble_lines
20
+ File.write(@filename, @raw_lines.join("\n"))
21
+ end
22
+
23
+ def update_entry(address, hostname, comment = nil)
24
+ # Delete entries for this hostname first
25
+ @parsed_file.each {|i| i[:hosts].to_a.delete(hostname)}
26
+
27
+ # Add entry
28
+ line_number = @parsed_file.find_path(address).first
29
+
30
+ if line_number.blank?
31
+ @parsed_file.push({:address => address, :hosts => [hostname], :comment => comment})
32
+ else
33
+ new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname)
34
+ @parsed_file.store_path(line_number, :hosts, new_hosts)
35
+ @parsed_file.store_path(line_number, :comment, comment) if comment
36
+ end
37
+ end
38
+
39
+ private
40
+ def parse_file
41
+ @parsed_file = []
42
+ @raw_lines.each { |line| @parsed_file.push(parse_line(line.strip)) }
43
+ @parsed_file.delete_blank_paths
44
+ end
45
+
46
+ def parse_line(line)
47
+ data, comment = line.split("#", 2)
48
+ address, hosts = data.to_s.split(" ", 2)
49
+ hostnames = hosts.to_s.split(" ")
50
+
51
+ { :address => address.to_s, :hosts => hostnames, :comment => comment.to_s.strip, :blank => line.blank?}
52
+ end
53
+
54
+ def cleanup_empty
55
+ @parsed_file.each do |h|
56
+ h.delete(:hosts) if h[:address].blank?
57
+ h.delete(:address) if h[:hosts].blank?
58
+ end
59
+
60
+ @parsed_file.delete_blank_paths
61
+ end
62
+
63
+ def assemble_lines
64
+ @parsed_file.each_with_object([]) { |l, a| a.push(l[:blank] ? "" : build_line(l[:address], l[:hosts], l[:comment])) }
65
+ end
66
+
67
+ def build_line(address, hosts, comment)
68
+ line = [address.to_s.ljust(16), hosts.to_a.uniq]
69
+ line.push("##{comment}") if comment
70
+ line.flatten.join(" ").strip
71
+ end
72
+ end
73
+ end
@@ -86,7 +86,7 @@ class LinuxAdmin
86
86
  group = group.split("\n").each_with_object({}) do |line, hash|
87
87
  next if line.blank?
88
88
  key, value = line.split(":", 2)
89
- hash[key.strip.downcase.tr(" -", "_").to_sym] = value.strip
89
+ hash[key.strip.downcase.tr(" -", "_").to_sym] = value.strip unless value.blank?
90
90
  end
91
91
  group_array.push(format_values(group))
92
92
  end
@@ -1,3 +1,3 @@
1
1
  class LinuxAdmin
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -89,4 +89,6 @@ class LinuxAdmin
89
89
  end
90
90
  end
91
91
  end
92
- end
92
+ end
93
+
94
+ Dir.glob(File.join(File.dirname(__FILE__), "yum", "*.rb")).each { |f| require f }
@@ -0,0 +1,12 @@
1
+ require 'inifile'
2
+
3
+ class LinuxAdmin
4
+ class Yum
5
+ class RepoFile < IniFile
6
+ def self.create(filename)
7
+ File.write(filename, "")
8
+ self.new(:filename => filename)
9
+ end
10
+ end
11
+ end
12
+ end
data/linux_admin.gemspec CHANGED
@@ -33,6 +33,6 @@ registration, updates, etc.
33
33
 
34
34
  spec.add_dependency "activesupport", "< 4.0"
35
35
  spec.add_dependency "inifile", "~> 2.0.2"
36
- spec.add_dependency "more_core_extensions"
36
+ spec.add_dependency "more_core_extensions", "~> 1.1.0"
37
37
  spec.add_dependency "nokogiri"
38
38
  end
@@ -15,5 +15,5 @@ Version: 2.1
15
15
  Arch: x86_64
16
16
  Status: Subscribed
17
17
  Starts: 01/03/2013
18
- Ends: 01/03/2014
18
+ Ends:
19
19
 
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxAdmin::Hosts do
4
+ etc_hosts = "\n #Some Comment\n127.0.0.1\tlocalhost localhost.localdomain # with a comment\n127.0.1.1 my.domain.local"
5
+ before do
6
+ File.stub(:read).and_return(etc_hosts)
7
+ @instance = LinuxAdmin::Hosts.new
8
+ end
9
+
10
+ describe "#reload" do
11
+ it "sets raw_lines" do
12
+ expected_array = ["", " #Some Comment", "127.0.0.1\tlocalhost localhost.localdomain # with a comment", "127.0.1.1 my.domain.local"]
13
+ expect(@instance.raw_lines).to eq(expected_array)
14
+ end
15
+
16
+ it "sets parsed_file" do
17
+ expected_hash = [{:blank=>true}, {:comment=>"Some Comment"}, {:address=>"127.0.0.1", :hosts=>["localhost", "localhost.localdomain"], :comment=>"with a comment"}, {:address=>"127.0.1.1", :hosts=>["my.domain.local"]}]
18
+ expect(@instance.parsed_file).to eq(expected_hash)
19
+ end
20
+ end
21
+
22
+ describe "#update_entry" do
23
+ it "removes an existing entry and creates a new one" do
24
+ expected_hash = [{:blank=>true}, {:comment=>"Some Comment"}, {:address=>"127.0.0.1", :hosts=>["localhost", "localhost.localdomain"], :comment=>"with a comment"}, {:address=>"127.0.1.1", :hosts=>[]}, {:address=>"1.2.3.4", :hosts=>["my.domain.local"], :comment=>nil}]
25
+ @instance.update_entry("1.2.3.4", "my.domain.local")
26
+ expect(@instance.parsed_file).to eq(expected_hash)
27
+ end
28
+
29
+ it "updates an existing entry" do
30
+ expected_hash = [{:blank=>true}, {:comment=>"Some Comment"}, {:address=>"127.0.0.1", :hosts=>["localhost", "localhost.localdomain", "new.domain.local"], :comment=>"with a comment"}, {:address=>"127.0.1.1", :hosts=>["my.domain.local"]}]
31
+ @instance.update_entry("127.0.0.1", "new.domain.local")
32
+ expect(@instance.parsed_file).to eq(expected_hash)
33
+ end
34
+ end
35
+
36
+ describe "#save" do
37
+ before do
38
+ File.stub(:write)
39
+ end
40
+
41
+ it "properly generates file with new content" do
42
+ 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"]
43
+ @instance.update_entry("1.2.3.4", "test")
44
+ @instance.save
45
+ expect(@instance.raw_lines).to eq(expected_array)
46
+ end
47
+
48
+ it "properly generates file with removed content" do
49
+ expected_array = ["", "#Some Comment", "127.0.0.1 localhost localhost.localdomain my.domain.local #with a comment"]
50
+ @instance.update_entry("127.0.0.1", "my.domain.local")
51
+ @instance.save
52
+ expect(@instance.raw_lines).to eq(expected_array)
53
+ end
54
+ end
55
+ 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.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-09-20 00:00:00.000000000 Z
15
+ date: 2013-10-02 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: bundler
@@ -115,17 +115,17 @@ dependencies:
115
115
  requirement: !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
- - - ! '>='
118
+ - - ~>
119
119
  - !ruby/object:Gem::Version
120
- version: '0'
120
+ version: 1.1.0
121
121
  type: :runtime
122
122
  prerelease: false
123
123
  version_requirements: !ruby/object:Gem::Requirement
124
124
  none: false
125
125
  requirements:
126
- - - ! '>='
126
+ - - ~>
127
127
  - !ruby/object:Gem::Version
128
- version: '0'
128
+ version: 1.1.0
129
129
  - !ruby/object:Gem::Dependency
130
130
  name: nokogiri
131
131
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +174,7 @@ files:
174
174
  - lib/linux_admin/distro.rb
175
175
  - lib/linux_admin/exceptions.rb
176
176
  - lib/linux_admin/fstab.rb
177
+ - lib/linux_admin/hosts.rb
177
178
  - lib/linux_admin/logical_volume.rb
178
179
  - lib/linux_admin/partition.rb
179
180
  - lib/linux_admin/physical_volume.rb
@@ -187,6 +188,7 @@ files:
187
188
  - lib/linux_admin/volume.rb
188
189
  - lib/linux_admin/volume_group.rb
189
190
  - lib/linux_admin/yum.rb
191
+ - lib/linux_admin/yum/repo_file.rb
190
192
  - linux_admin.gemspec
191
193
  - spec/common_spec.rb
192
194
  - spec/data/rhn/output_rhn-channel_list
@@ -204,6 +206,7 @@ files:
204
206
  - spec/disk_spec.rb
205
207
  - spec/distro_spec.rb
206
208
  - spec/fstab_spec.rb
209
+ - spec/hosts_spec.rb
207
210
  - spec/linux_admin_spec.rb
208
211
  - spec/logical_volume_spec.rb
209
212
  - spec/partition_spec.rb
@@ -259,6 +262,7 @@ test_files:
259
262
  - spec/disk_spec.rb
260
263
  - spec/distro_spec.rb
261
264
  - spec/fstab_spec.rb
265
+ - spec/hosts_spec.rb
262
266
  - spec/linux_admin_spec.rb
263
267
  - spec/logical_volume_spec.rb
264
268
  - spec/partition_spec.rb