2

从文档看来,使用 Serverspec 来验证是否安装了软件包应该非常简单,但是我遇到了一些有趣的问题vimag( the_silver_searcher)。

我正在使用带有kitchen-vagrant插件的 Test Kitchen,并且有两个平台:ubuntu-1404centos-72. 我的所有规格都通过了 Ubuntu,其中两个不适合 Centos:vimag.

vim

处理此安装的 Chef 代码非常简单:

package "vim"

这是规格:

describe "Vim" do
  describe package("vim") do
    it { should be_installed }
  end
end

再次,非常直截了当。但是,它在我的 Centos 构建中失败并出现以下错误:

 2) Vim Package "vim" should be installed
    Failure/Error: it { should be_installed }
      expected Package "vim" to be installed
      /bin/sh -c rpm\ -q\ vim
      package vim is not installed

然而,如果我登录到服务器,它肯定是安装的:

▶ kitchen login all-centos-72
Last login: Sat Jul  2 17:53:30 2016 from 10.0.2.2
[vagrant@all-centos-72 ~]$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 10 2014 06:55:55)
[vagrant@all-centos-72 ~]$ which vim
/usr/bin/vim
[vagrant@all-centos-72 ~]$ sudo yum install -y vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: distro.ibiblio.org
 * extras: mirror.us.leaseweb.net
 * updates: mirror.eboundhost.com
Package 2:vim-enhanced-7.4.160-1.el7.x86_64 already installed and latest version
Nothing to do

ag更复杂的是安装需要在 Centos 上从源代码构建,而在 Ubuntu 上它可以使用apt-get. 这是配方的相关部分:

  bash "install Development Tools" do
    code "yum -y groupinstall \"Development Tools\""
  end

  package %w(pcre-devel xz-devel)

  target_dir = File.join("/", "usr", "local", "the_silver_searcher")

  git "clone the ag repo" do
    repo "https://github.com/ggreer/the_silver_searcher/"
    revision "master"
    destination target_dir
  end

  bash "install ag" do
    not_if system("hash ag")

    cwd target_dir
    code <<-EOF
      ./build.sh
      make install
    EOF
  end

这是规格:

describe "The Silver Searcher" do    
  if host_inventory["platform"] == "ubuntu"
    describe package("silversearcher-ag") do
      it { should be_installed }
    end
  else
    describe package("the_silver_searcher") do
      it { should be_installed }
    end
  end
end

Centos 失败:

 1) The Silver Searcher Package "the_silver_searcher" should be installed
    Failure/Error: it { should be_installed }
      expected Package "the_silver_searcher" to be installed
      /bin/sh -c rpm\ -q\ the_silver_searcher
      package the_silver_searcher is not installed

同样,如果我登录到 Centos VM,我可以使用ag

[vagrant@all-centos-72 ~]$ ag --version
ag version 0.32.0
[vagrant@all-centos-72 ~]$ which ag
/usr/local/bin/ag

如果我切换到root用户,这些命令也可以工作。

我试图通过以不同方式为 Centos 平台编写规范来欺骗系统:

  describe command("ag") do
    its(:stderr) { should match /Usage: ag/ }
  end

即使ag在登录时输入(退出状态1)确实会产生该使用内容,上述内容也不起作用。我的最后一次尝试是:

describe file("/usr/local/bin/ag") do
  it { should exist }
end

这行得通,但感觉超级hacky,好像没有必要。

有人在这里推荐吗?这些包有什么我遗漏/做错的吗?我最初认为ag问题只是因为它是从源安装而不是包管理器,但是使用包管理vim 安装并且仍然存在与ag.

4

2 回答 2

2

这个问题的答案有两个部分,一个涉及 Serverspec 的工作方式,另一个涉及各种 Linux 发行版如何处理包。

1) Serverspec 用户不应假设package资源名称未字面暗示的任何行为。通过系统包管理器以外的任何方式安装的应用程序将不会被拾取,并且它们的成功安装应该通过其他方式进行测试。如问题所示,这可能包括测试二进制文件是否存在。

2) 当开发者安装了一个带有包管理器的应用程序时,他/她必须知道不同 Linux 发行版上的包管理器有时(经常?)对同一个包有不同的名称。一个典型的例子是apache2Debian 系统与httpdRedHat 系统。在问题中提到的特定情况下,vim在 CentOS 上被识别为vim-enhanced即使在安装时yum接受vim作为名称(感谢 @matt-schuchard 指出它们是链接的)。

还想感谢@Karen B 在对问题的评论中帮助我得出这些结论。

于 2016-07-08T11:45:01.190 回答
0

您没有通过软件包安装 ag,因此它不是“黑客”,因为它不起作用。

于 2016-07-05T22:36:14.877 回答