class Train::Extras::OSCommon

Constants

OS

Public Class Methods

new(backend, platform = nil) click to toggle source
# File lib/train/extras/os_common.rb, line 23
def initialize(backend, platform = nil)
  @backend = backend
  @platform = platform || {}
  detect_family
end

Public Instance Methods

[](key) click to toggle source
# File lib/train/extras/os_common.rb, line 29
def [](key)
  @platform[key]
end
to_hash() click to toggle source
# File lib/train/extras/os_common.rb, line 33
def to_hash
  @platform
end

Private Instance Methods

detect_family() click to toggle source
# File lib/train/extras/os_common.rb, line 78
def detect_family
  # if some information is already defined, try to verify it
  # with the remaining detection
  unless @platform[:family].nil?
    # return ok if the preconfigured family yielded a good result
    return true if detect_family_type
    # if not, reset the platform to presets and run the full detection
    # TODO: print an error message in this case, as the instantiating
    # backend is doing something wrong
    @platform = {}
  end

  # TODO: extend base implementation for detecting the family type
  # to Windows and others
  case uname_s
  when /linux/i
    @platform[:family] = 'linux'
  when /./
    @platform[:family] = 'unix'
  else
    # Don't know what this is
    @platform[:family] = nil
  end

  # try to detect the platform
  return nil unless @platform[:family].nil?
  detect_family_type
end
detect_family_type() click to toggle source
# File lib/train/extras/os_common.rb, line 107
def detect_family_type
  pf = @platform[:family]

  return detect_windows if pf == 'windows'
  return detect_darwin if pf == 'darwin'

  if %w{freebsd netbsd openbsd aix solaris2 hpux}.include?(pf)
    return detect_via_uname
  end

  # unix based systems combine the above
  return true if pf == 'unix' and detect_darwin
  return true if pf == 'unix' and detect_via_uname

  # if we arrive here, we most likey have a regular linux
  detect_linux
end
get_config(path) click to toggle source
# File lib/train/extras/os_common.rb, line 125
def get_config(path)
  res = @backend.run_command("test -f #{path} && cat #{path}")
  # ignore files that can't be read
  return nil if res.exit_status != 0
  res.stdout
end
unix_file?(path) click to toggle source
# File lib/train/extras/os_common.rb, line 132
def unix_file?(path)
  @backend.run_command("test -f #{path}").exit_status == 0
end