我可以通过以下几种方式检查运行我的Ruby代码的平台的操作系统:
RUBY_PLATFORM
:https://stackoverflow.com/a/171011/462015RbConfig::CONFIG['host_os']
:https://stackoverflow.com/a/13586108/462015是否有可能知道Linux发行版正在运行?例如基于Debian或基于Red Hat的发行版。
正如评论部分中所指出的那样,似乎没有确定“以每种分配方式工作”的方式来做到这一点。接下来是我用来检测脚本运行的环境类型:
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
这不是处理地球上每个GNU / Linux发行版的完整解决方案。实际上远非如此。例如,它不区分OpenSUSE和SUSE Linux Enterprise Server,尽管它们是两个完全不同的野兽。此外,即使只有一些发行版,它也是一个意大利面条。但它可能是一个人可能能够建立的东西。
您可以从 source code的Facter找到更完整的分布检测示例,其中包括将事实提供给配置管理系统Puppet。
Linux发行版是一组软件,通常可以通过包管理器,窗口系统,窗口管理器和桌面环境进行区分。这是很多可互换的部分。如果系统保留了包管理器,但更改了窗口系统和桌面环境,我们称之为新的发行版吗?没有明确的答案,因此各种工具会给出稍微不同的答案。
Train有一个whole hierarchy的分布系列,可能是最复杂的一群。火车和Ohai is here的快速比较。它被设计为通过网络连接运行,但在本地也可以正常工作,如下所示:
# gem install train
Train.create('local').connection.os[:name] #=> eg. "centos", "linuxmint"
Train.create('local').connection.os[:family] #=> eg. "redhat", "debian"
Facter的osfamily fact回归,例如。 Ubuntu的“Debian”。使用Facter,检索事实的一般形式是Facter[factname].value
。
# gem install facter
require 'facter'
puts Facter['osfamily'].value
Ohai的platform
事实回归,例如。 "debian" for Ubuntu和CentOS的“rhel”。与Ohai一起,检索事实的一般形式是node[factname]
。
# gem install ohai
node['platform'] #=> eg. "ubuntu" or "mint"
node['platform_family'] #=> eg. "debian" for Ubuntu and Mint
Platform检索一些基本数据,可以很好地区分各种Unix平台。但是,它根本不处理Linux的不同发行版。 Platform::IMPL
将返回:freebsd,:netbsd,:hpux等,但所有Linux发行版都只是:linux。 sys-uname和sysinfo是相似的。 utilinfo甚至更基本,并且在Windows,Mac和Linux以外的任何系统上都会失败。
require 'facter'
puts Facter['osfamily'].value