Class: LinuxAdmin::Hosts
- Inherits:
-
Object
- Object
- LinuxAdmin::Hosts
- Includes:
- Common
- Defined in:
- lib/linux_admin/hosts.rb
Constant Summary
Constants included from Common
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#parsed_file ⇒ Object
Returns the value of attribute parsed_file.
-
#raw_lines ⇒ Object
Returns the value of attribute raw_lines.
Instance Method Summary collapse
- #add_alias(address, hostname, comment = nil) ⇒ Object (also: #update_entry)
- #hostname ⇒ Object
- #hostname=(name) ⇒ Object
-
#initialize(filename = "/etc/hosts") ⇒ Hosts
constructor
A new instance of Hosts.
- #reload ⇒ Object
- #save ⇒ Object
- #set_canonical_hostname(address, hostname, comment = nil) ⇒ Object
Methods included from Common
Constructor Details
#initialize(filename = "/etc/hosts") ⇒ Hosts
Returns a new instance of Hosts.
9 10 11 12 |
# File 'lib/linux_admin/hosts.rb', line 9 def initialize(filename = "/etc/hosts") @filename = filename self.reload end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
5 6 7 |
# File 'lib/linux_admin/hosts.rb', line 5 def filename @filename end |
#parsed_file ⇒ Object
Returns the value of attribute parsed_file.
7 8 9 |
# File 'lib/linux_admin/hosts.rb', line 7 def parsed_file @parsed_file end |
#raw_lines ⇒ Object
Returns the value of attribute raw_lines.
6 7 8 |
# File 'lib/linux_admin/hosts.rb', line 6 def raw_lines @raw_lines end |
Instance Method Details
#add_alias(address, hostname, comment = nil) ⇒ Object Also known as: update_entry
25 26 27 |
# File 'lib/linux_admin/hosts.rb', line 25 def add_alias(address, hostname, comment = nil) add_name(address, hostname, false, comment) end |
#hostname ⇒ Object
44 45 46 47 |
# File 'lib/linux_admin/hosts.rb', line 44 def hostname result = run(cmd("hostname")) result.success? ? result.output.strip : nil end |
#hostname=(name) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/linux_admin/hosts.rb', line 35 def hostname=(name) if cmd?("hostnamectl") run!(cmd('hostnamectl'), :params => ['set-hostname', name]) else File.write("/etc/hostname", name) run!(cmd('hostname'), :params => {:file => "/etc/hostname"}) end end |
#reload ⇒ Object
14 15 16 17 |
# File 'lib/linux_admin/hosts.rb', line 14 def reload @raw_lines = File.read(@filename).split("\n") parse_file end |
#save ⇒ Object
19 20 21 22 23 |
# File 'lib/linux_admin/hosts.rb', line 19 def save cleanup_empty @raw_lines = assemble_lines File.write(@filename, @raw_lines.join("\n") + "\n") end |
#set_canonical_hostname(address, hostname, comment = nil) ⇒ Object
31 32 33 |
# File 'lib/linux_admin/hosts.rb', line 31 def set_canonical_hostname(address, hostname, comment = nil) add_name(address, hostname, true, comment) end |