Module: VScripts::Util::LocalSystem
- Included in:
- VScripts::Util
- Defined in:
- lib/vscripts/util/local_system.rb
Overview
Local system functions library
Instance Method Summary collapse
-
#ensure_file_content(file, body) ⇒ Object
Ensures the specified file has the specified content.
-
#ensure_file_dir(file) ⇒ Object
Creates the directory for the specified file.
-
#external_dns ⇒ Object
Tries to get the reverse dns.
-
#hostname_path ⇒ Object
Hostname file path.
-
#hosts_file ⇒ Object
@return The contents of the hosts file.
-
#hosts_path ⇒ Object
Hosts file path.
-
#local_domain_name ⇒ Object
Returns the local domain name.
-
#local_fqdn ⇒ Object
Returns the current fully qualified domain.
-
#local_host_name ⇒ Object
Returns the local host name.
-
#write_file(file, body) ⇒ Object
Writes to file.
Instance Method Details
#ensure_file_content(file, body) ⇒ Object
Ensures the specified file has the specified content
63 64 65 66 67 |
# File 'lib/vscripts/util/local_system.rb', line 63 def ensure_file_content(file, body) write = write_file(file, body) read = IO.read(file) read == body || write end |
#ensure_file_dir(file) ⇒ Object
Creates the directory for the specified file
47 48 49 50 |
# File 'lib/vscripts/util/local_system.rb', line 47 def ensure_file_dir(file) path = File.dirname(file) `mkdir -p #{path}` end |
#external_dns ⇒ Object
Tries to get the reverse dns
38 39 40 41 42 43 44 |
# File 'lib/vscripts/util/local_system.rb', line 38 def external_dns ext_ip = `wget -q -O - checkip.dyndns.org \ | sed -e 's/[^[:digit:]|.]//g'` `dig +short -x #{ext_ip}`.strip rescue false end |
#hostname_path ⇒ Object
Hostname file path
16 17 18 |
# File 'lib/vscripts/util/local_system.rb', line 16 def hostname_path '/etc/hostname' end |
#hosts_file ⇒ Object
@return The contents of the hosts file
11 12 13 |
# File 'lib/vscripts/util/local_system.rb', line 11 def hosts_file File.read(hosts_path) end |
#hosts_path ⇒ Object
Hosts file path
6 7 8 |
# File 'lib/vscripts/util/local_system.rb', line 6 def hosts_path '/etc/hosts' end |
#local_domain_name ⇒ Object
Returns the local domain name
31 32 33 34 35 |
# File 'lib/vscripts/util/local_system.rb', line 31 def local_domain_name `dnsdomainname`.strip rescue 'local' end |
#local_fqdn ⇒ Object
Returns the current fully qualified domain
21 22 23 |
# File 'lib/vscripts/util/local_system.rb', line 21 def local_fqdn `hostname -f`.strip end |
#local_host_name ⇒ Object
Returns the local host name
26 27 28 |
# File 'lib/vscripts/util/local_system.rb', line 26 def local_host_name `hostname`.strip end |
#write_file(file, body) ⇒ Object
Writes to file
53 54 55 56 57 58 59 60 |
# File 'lib/vscripts/util/local_system.rb', line 53 def write_file(file, body) ensure_file_dir(file) File.open(file, 'w') do |newfile| newfile.write(body) end rescue Errno::EACCES abort "FATAL: You need to be root in order to write to #{file}" end |