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 ⇒ String
Tries to get the reverse dns.
-
#hostname_path ⇒ String
The hostname file path.
-
#hosts_file ⇒ String
The contents of the hosts file.
-
#hosts_path ⇒ String
The hosts file path.
-
#local_domain_name ⇒ String
The local domain name.
-
#local_fqdn ⇒ String
The local fully qualified domain.
-
#local_host_name ⇒ String
The local host name.
-
#write_file(file, body) ⇒ Object
Writes to a file.
Instance Method Details
#ensure_file_content(file, body) ⇒ Object
Ensures the specified file has the specified content
69 70 71 72 73 |
# File 'lib/vscripts/util/local_system.rb', line 69 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
49 50 51 52 |
# File 'lib/vscripts/util/local_system.rb', line 49 def ensure_file_dir(file) path = File.dirname(file) `mkdir -p #{path}` end |
#external_dns ⇒ String
Tries to get the reverse dns
39 40 41 42 43 44 45 |
# File 'lib/vscripts/util/local_system.rb', line 39 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 ⇒ String
Returns the hostname file path.
16 17 18 |
# File 'lib/vscripts/util/local_system.rb', line 16 def hostname_path '/etc/hostname' end |
#hosts_file ⇒ String
Returns 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 ⇒ String
Returns the hosts file path.
6 7 8 |
# File 'lib/vscripts/util/local_system.rb', line 6 def hosts_path '/etc/hosts' end |
#local_domain_name ⇒ String
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 ⇒ String
Returns the local 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 ⇒ String
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 a file
57 58 59 60 61 62 63 64 |
# File 'lib/vscripts/util/local_system.rb', line 57 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 |