Art*_*ero 5 ruby linux platform operating-system
我可以通过几种方式检查正在运行我的Ruby代码的平台的操作系统:
RUBY_PLATFORM:https : //stackoverflow.com/a/171011/462015RbConfig::CONFIG['host_os']:https : //stackoverflow.com/a/13586108/462015是否可以知道正在运行什么Linux发行版?例如,基于Debian或基于Red Hat的发行版。
小智 3
正如上面评论部分所指出的,似乎没有确定的“在每个发行版中都有效”的方法来做到这一点。以下是我用来检测脚本正在运行的环境类型的方法:
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
Run Code Online (Sandbox Code Playgroud)
这并不是处理地球上每个 GNU/Linux 发行版的完整解决方案。事实上,远非如此。例如,它对 OpenSUSE 和 SUSE Linux Enterprise Server 没有区别,尽管它们是两种完全不同的野兽。此外,即使只有几个发行版,它也是意大利面条。但这可能是人们可以在此基础上继续发展的东西。
您可以从Facter的源代码中找到更完整的分布检测示例,该示例用于将事实提供给配置管理系统Puppet等。
| 归档时间: |
|
| 查看次数: |
1255 次 |
| 最近记录: |