Module: VScripts::Util::LocalSystem
- Included in:
- Commands::Identify, Commands::Tags2facts
- Defined in:
- lib/vscripts/util/local_system.rb
Overview
Local system functions library
Instance Method Summary collapse
-
#checks ⇒ Object
Gets system checks from environment variables.
-
#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.
-
#process_checks ⇒ Object
Runs each command specified and returns the name and exit status.
-
#status_codes ⇒ Object
Extract the codes for each check.
-
#write_file(file, body) ⇒ Object
Writes to file.
Instance Method Details
#checks ⇒ Object
Gets system checks from environment variables
76 77 78 79 80 |
# File 'lib/vscripts/util/local_system.rb', line 76 def checks config['SystemChecks'] rescue {} end |
#ensure_file_content(file, body) ⇒ Object
Ensures the specified file has the specified content
67 68 69 70 71 72 73 |
# File 'lib/vscripts/util/local_system.rb', line 67 def ensure_file_content(file, body) write = write_file(file, body) read = IO.read(file) read == body || write rescue write end |
#ensure_file_dir(file) ⇒ Object
Creates the directory for the specified file
51 52 53 54 |
# File 'lib/vscripts/util/local_system.rb', line 51 def ensure_file_dir(file) path = File.dirname(file) `mkdir -p #{path}` end |
#external_dns ⇒ Object
Tries to get the reverse dns
42 43 44 45 46 47 48 |
# File 'lib/vscripts/util/local_system.rb', line 42 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
20 21 22 |
# File 'lib/vscripts/util/local_system.rb', line 20 def hostname_path '/etc/hostname' end |
#hosts_file ⇒ Object
@return The contents of the hosts file
15 16 17 |
# File 'lib/vscripts/util/local_system.rb', line 15 def hosts_file File.read(hosts_path) end |
#hosts_path ⇒ Object
Hosts file path
10 11 12 |
# File 'lib/vscripts/util/local_system.rb', line 10 def hosts_path '/etc/hosts' end |
#local_domain_name ⇒ Object
Returns the local domain name
35 36 37 38 39 |
# File 'lib/vscripts/util/local_system.rb', line 35 def local_domain_name `dnsdomainname`.strip rescue 'local' end |
#local_fqdn ⇒ Object
Returns the current fully qualified domain
25 26 27 |
# File 'lib/vscripts/util/local_system.rb', line 25 def local_fqdn `hostname -f`.strip end |
#local_host_name ⇒ Object
Returns the local host name
30 31 32 |
# File 'lib/vscripts/util/local_system.rb', line 30 def local_host_name `hostname`.strip end |
#process_checks ⇒ Object
Runs each command specified and returns the name and exit status
83 84 85 86 87 88 89 90 |
# File 'lib/vscripts/util/local_system.rb', line 83 def process_checks codes = {} checks.each do |name, command| system("#{command} > /dev/null 2>&1") codes[name] = $CHILD_STATUS.exitstatus end codes end |
#status_codes ⇒ Object
Extract the codes for each check
93 94 95 |
# File 'lib/vscripts/util/local_system.rb', line 93 def status_codes process_checks.values.compact end |
#write_file(file, body) ⇒ Object
Writes to 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 puts "FATAL: You need to be root in order to write to #{file}" end |