File: os_linux.rb

package info (click to toggle)
ruby-train 3.2.28-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,116 kB
  • sloc: ruby: 9,246; sh: 17; makefile: 8
file content (87 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# encoding: utf-8

module Train::Platforms::Detect::Helpers
  module Linux
    def redhatish_platform(conf)
      conf =~ /^red hat/i ? "redhat" : /(\w+)/i.match(conf)[1].downcase
    end

    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

    def redhatish(path)
      if (raw = unix_file_contents(path))
        @platform[:release] = redhatish_version(raw)
        true
      end
    end

    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

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

      raw.lines.each_with_object({}) do |line, memo|
        line.strip!
        next if line.nil? || line.empty?
        next if line.start_with?("#")

        key, value = line.split("=", 2)
        memo[key] = value.gsub(/\A"|"\Z/, "") unless value.nil? || value.empty?
      end
    end

    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

    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

    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
  end
end