2014-09-22 32 views

回答

2

正如上面在評論部分所指出的,似乎沒有確定的「在每個分配中都有效」的方式。以下是我所使用的檢測正在運行什麼樣的環境的腳本:

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,雖然它們是兩個截然不同的野獸。此外,即使只有幾個發行版,它也是一個意大利麪條。但它可能是一個可能能夠建立的東西。

你可以找到分佈檢測的從Facter這是用來除其他事物的 source code一個更完整的例子,以事實饋送到配置管理系統Puppet

+0

我很感謝您的回答,但這看起來很可怕,並且不適用於所有環境,如亞馬遜實例你需要在'/ etc/system-release'中看到。無論如何,謝謝。 – 2014-10-14 09:47:31