如何找出节点上安装的软件包的版本? [英] How to find out version of software package installed on the node?

查看:62
本文介绍了如何找出节点上安装的软件包的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改Apache食谱以与2.4 Apache一起使用。 Opscode Cookbook当前失败,因为它正在生成带有LockFile关键字的conf文件,该文件被排除在Apache 2.4关键字列表之外。

I'm adapting Apache cookbook to work with 2.4 Apache. Opscode cookbook is currently failing because it's generating conf file with LockFile keyword that is excluded from the list of the Apache 2.4 keywords.

我想提出一个通用解决方案,并填充我的conf文件取决于软件版本。为此,我必须找出已经安装的软件的版本。这个问题已经困扰我一段时间了,但是我一直在努力避免它。由于我要一遍又一遍地讨论它,所以我想问:

I want to make a general solution, and populate my conf file depending on the version of software. To do so, I have to find out what's the version of already installed software. This same question has been bothering me for about a time now, but I've been managing to avoid it. Since I'm getting to it over and over again, I'd like to ask:

如何查找已安装的软件包/软件的版本(如果有的话)没有指定它,即安装最新的可用版本?

注意:请不要告诉我将命令传递给底层shell。我想知道是否还有更好,更...厨师的方式?也许与Ohai有关?

Note: Please don't tell me to pass the command to the underlying shell. I'd like to know if there is a better, more... Chefy way? Maybe something related to Ohai?

编辑:似乎我被误解了。我想知道是否有一种解决方案可以抽象化平台层(就像Ohai一样),在这里我要说类似 gimme_version('apache')并得到安装apache版本。我想避免在以下情况下编写 case节点['platform_family']:……何时……何时里面包含平台特定的代码。

Seems I'm misunderstood. I wanted to know if there's a solution that abstracts the platform layer (just like Ohai does), where I'd say something like gimme_version('apache') and get the installed apache version. I want to avoid writing case node['platform_family'] when... when... when with platform specific code inside.

推荐答案

我创建了Ohai插件,可以为我解决此问题。将以下代码放入.rb文件,然后将该文件放入 ohai / plugin / linux 文件夹。 (由于平台和安装类型的不同,无法提供绝对路径)

I created Ohai plugin that solves the issue for me. Place the following code into a .rb file, and put the file into ohai/plugin/linux folder. (Cannot provide absolute path since it varies depending on the platform and installation type)

Ohai.plugin(:PackageVersion) do
  provides "package_version"

  depends "platform_family"

  collect_data do
    pckg_list = Hash.new
    case platform_family
      when 'debian'
        pckg_list = eval '{'+`dpkg-query -W -f='"${Package}"=> "${Version}", '`+'}'
      when 'rhel' || 'fedora'
        pckg_list = eval '{'+`rpm -qa --queryformat '"%{NAME}"=> "%{VERSION}", '`+'}'
      when 'arch'
        pckg_list = eval '{'+`package-query -Q -f '"%n"=> "%v", '`+'}'
      when 'gentoo'
        pckg_list = eval '{'+`equery list --format='"$name" => "$version", ' '*'`+'}'
      end                                                                                                    
    package_version Mash.new pckg_list
  end                                                                                                                   
end    

下次运行Chef时,软件包版本信息将位于 node [ package_version] [< package_name>] 例如 node [ package_version] [ glibc] 。我让它适用于5个平台系列,但对于Arch,您必须确保已安装 package-query

Next time you run Chef, package version information will be in node["package_version"]["<package_name>"] e.g. node["package_version"]["glibc"]. I made it work for 5 platform families, but for Arch you'll have to make sure you have package-query installed.

重要提示:在执行厨师配方之前已加载/填充了Ohai。此解决方案将不会自动更新Ohai,因此,如果您要访问新安装/升级的软件包的数据,则在当前Chef运行期间,您必须手动重新加载Ohai。为此,请将以下代码块放在您的食谱的软件包安装/升级块下,如下所示:

Important note: Ohai is loaded/populated before Chef recipe execution. This solution will not automatically update Ohai, so if you want to access data of the newly installed/upgraded packages, during current Chef run, you'll have to manually reload Ohai. To do it, place following block to your recipe under the package installation/upgrade block, just like shown below:

#example package instalation
package "whatever"

ohai "reload_ohai" do
  action :reload
end

感谢@markoconnor。他指出,该解决方案基于社区插件。这些不再在线。我调整了它们以与Chef 11.0+一起使用,就在这里。

Thanks to @markoconnor. This solution is based on community plugins he pointed out. Those are no longer online. I adjusted them to work with Chef 11.0+ and here they are.

这篇关于如何找出节点上安装的软件包的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆