Linux Distribution / Platform Discovery in Ruby

I can check the operating system of the platform my Ruby code is running on in several ways:

Is it possible to know what a Linux distribution is? For example, Debian based or Red Hat based.

+4


source to share


3 answers


As stated in the comments section above, it seems like there is no certainty that this "works on every distribution". What follows is what I used to determine which environment the script runs:

def linux_variant
  r = { :distro => nil, :family => nil }

  if File.exists?('/etc/lsb-release')
    File.open('/etc/lsb-release', 'r').read.each_line do |line|
      r = { :distro => $1 } if line =~ /^DISTRIB_ID=(.*)/
    end
  end

  if File.exists?('/etc/debian_version')
    r[:distro] = 'Debian' if r[:distro].nil?
    r[:family] = 'Debian' if r[:variant].nil?
  elsif File.exists?('/etc/redhat-release') or File.exists?('/etc/centos-release')
    r[:family] = 'RedHat' if r[:family].nil?
    r[:distro] = 'CentOS' if File.exists?('/etc/centos-release')
  elsif File.exists?('/etc/SuSE-release')
    r[:distro] = 'SLES' if r[:distro].nil?
  end

  return r
end

      



It is not a complete solution to handle every GNU / Linux distribution on earth. In fact, this is not the case. For example, it makes no distinction between OpenSUSE and SUSE Linux Enterprise Server, although they are two completely different beasts. Plus, it's pretty spaghetti even with multiple distributions. But that might be something to lean on.

You can find a more complete example of allocation detection from the Facter source code , which is used, among other things, to pass facts to the Puppet CMS .

+3


source


A Linux distribution is a collection of software, usually distinguished by a package manager, window system, window manager, and desktop environment. This is a lot of replacement parts. If the system retains the package manager but changes the window system and desktop environment, do we call it a new distribution? There is no definite answer, so different tools will give slightly different answers.

Train has a whole hierarchy of distributed families and may be the most complex of all. A quick comparison of Train and Ohai is here . It is designed to work over a network connection, but it also works fine locally, as shown here:

# gem install train
Train.create('local').connection.os[:name] #=> eg. "centos", "linuxmint"
Train.create('local').connection.os[:family] #=> eg. "redhat", "debian"

      

Faster osfamily returns fact , for example. "Debian" for Ubuntu. In Facter, the general form of finding facts is -Facter[factname]. value

Facter[factname]. value

# gem install facter
require 'facter'
puts Facter['osfamily'].value

      



Bringing back the fact about Ohai , like "Debian" for Ubuntu and "RHEL" for CentOS. In Ohai, the general form of finding facts is this.platform

node[factname]

# gem install ohai
node['platform'] #=> eg. "ubuntu" or "mint"
node['platform_family'] #=> eg. "debian" for Ubuntu and Mint

      

Ruby information libraries that are platform-agnostic

The platform extracts some basic data and can differentiate well between different Unix platforms. However, it does not work with various Linux distributions. Platform::IMPL

will return: freebsd ,: netbsd ,: hpux, etc., but all Linux distributions are just: linux. sys-uname and sysinfo are similar. utilinfo is even simpler and can run on any system other than Windows, Mac and Linux.

+1


source


require 'facter'

puts Facter['osfamily'].value

      

-1


source







All Articles