Module: Train::Platforms::Detect::Helpers::Linux

Included in:
OSCommon
Defined in:
lib/train/platforms/detect/helpers/os_linux.rb

Instance Method Summary collapse

Instance Method Details

#linux_os_releaseObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 22

def linux_os_release
  data = unix_file_contents('/etc/os-release')
  return if data.nil?

  os_info = parse_os_release_info(data)
  cisco_info_file = os_info['CISCO_RELEASE_INFO']
  if cisco_info_file
    os_info.merge!(parse_os_release_info(unix_file_contents(cisco_info_file)))
  end

  os_info
end

#lsb_config(content) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 46

def lsb_config(content)
  id = /^DISTRIB_ID=["']?(.+?)["']?$/.match(content)
  release = /^DISTRIB_RELEASE=["']?(.+?)["']?$/.match(content)
  codename = /^DISTRIB_CODENAME=["']?(.+?)["']?$/.match(content)
  {
    id:       id.nil? ? nil : id[1],
    release:  release.nil? ? nil : release[1],
    codename: codename.nil? ? nil : codename[1],
  }
end

#lsb_release(content) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 57

def lsb_release(content)
  id = /^Distributor ID:\s+(.+)$/.match(content)
  release = /^Release:\s+(.+)$/.match(content)
  codename = /^Codename:\s+(.+)$/.match(content)
  {
    id:       id.nil? ? nil : id[1],
    release:  release.nil? ? nil : release[1],
    codename: codename.nil? ? nil : codename[1],
  }
end

#parse_os_release_info(raw) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 35

def parse_os_release_info(raw)
  return {} if raw.nil?

  raw.lines.each_with_object({}) do |line, memo|
    line.strip!
    next if line.empty?
    key, value = line.split('=', 2)
    memo[key] = value.gsub(/\A"|"\Z/, '') unless value.empty?
  end
end

#read_linux_lsbObject



68
69
70
71
72
73
74
75
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 68

def read_linux_lsb
  return @lsb unless @lsb.empty?
  if !(raw = unix_file_contents('/etc/lsb-release')).nil?
    @lsb = lsb_config(raw)
  elsif !(raw = unix_file_contents('/usr/bin/lsb-release')).nil?
    @lsb = lsb_release(raw)
  end
end

#redhatish_platform(conf) ⇒ Object



5
6
7
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 5

def redhatish_platform(conf)
  conf =~ /^red hat/i ? 'redhat' : /(\w+)/i.match(conf)[1].downcase
end

#redhatish_version(conf) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/train/platforms/detect/helpers/os_linux.rb', line 9

def redhatish_version(conf)
  case conf
  when /rawhide/i
    /((\d+) \(Rawhide\))/i.match(conf)[1].downcase
  when /Amazon Linux/i
    /([\d\.]+)/.match(conf)[1]
  when /derived from .*linux|amazon/i
    /Linux ((\d+|\.)+)/i.match(conf)[1]
  else
    /release ([\d\.]+)/.match(conf)[1]
  end
end