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

Instance Method Details

#ensure_file_content(file, body) ⇒ Object

Ensures the specified file has the specified content

Parameters:

  • file (String)

    the path of the file

  • body (String)

    the body of the file



64
65
66
67
68
# File 'lib/vscripts/util/local_system.rb', line 64

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

Parameters:

  • file (String)

    the path of the file



44
45
46
47
# File 'lib/vscripts/util/local_system.rb', line 44

def ensure_file_dir(file)
  path = File.dirname(file)
  `mkdir -p #{path}`
end

#external_dnsString

Tries to get the reverse dns

Returns:

  • (String)

    the reverse dns



34
35
36
37
38
39
40
# File 'lib/vscripts/util/local_system.rb', line 34

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_pathString

Returns the hostname file path.

Returns:

  • (String)

    the hostname file path



11
12
13
# File 'lib/vscripts/util/local_system.rb', line 11

def hostname_path
  '/etc/hostname'
end

#hosts_pathString

Returns the hosts file path.

Returns:

  • (String)

    the hosts file path



6
7
8
# File 'lib/vscripts/util/local_system.rb', line 6

def hosts_path
  '/etc/hosts'
end

#local_domain_nameString

Returns the local domain name.

Returns:

  • (String)

    the local domain name



26
27
28
29
30
# File 'lib/vscripts/util/local_system.rb', line 26

def local_domain_name
  `dnsdomainname`.strip
rescue
  'local'
end

#local_fqdnString

Returns the local fully qualified domain.

Returns:

  • (String)

    the local fully qualified domain



16
17
18
# File 'lib/vscripts/util/local_system.rb', line 16

def local_fqdn
  `hostname -f`.strip
end

#local_host_nameString

Returns the local host name.

Returns:

  • (String)

    the local host name



21
22
23
# File 'lib/vscripts/util/local_system.rb', line 21

def local_host_name
  `hostname`.strip
end

#write_file(file, body) ⇒ Object

Writes to a file

Parameters:

  • file (String)

    the path of the file

  • body (String)

    the body of the file



52
53
54
55
56
57
58
59
# File 'lib/vscripts/util/local_system.rb', line 52

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