【发布时间】:2016-11-04 21:53:52
【问题描述】:
从文档看来,使用 Serverspec 来验证是否安装了软件包应该非常简单,但我在使用 vim 和 ag (the_silver_searcher) 时遇到了一些有趣的问题。
我正在使用带有kitchen-vagrant 插件的Test Kitchen,并且有两个平台:ubuntu-1404 和centos-72。我的所有规范都通过了 Ubuntu,其中两个不通过 Centos:vim 和 ag。
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
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一样的问题。
【问题讨论】:
-
在您的 Centos 服务器上,如果您登录运行
rpm -q vim(is_installed匹配器尝试的命令),它会输出什么?命令的退出代码是什么?至于 ag 示例,这将失败,因为您是从源代码构建而不是使用package资源,所以正如您所发现的,您将不得不使用备用匹配器进行测试。 -
感谢您的评论。我对
ag的猜测也差不多,这很遗憾,但我想这是意料之中的,所以我真的不明白vim的事情。对于您的问题:在 Centos 服务器上手动运行该命令会产生与规范运行程序相同的消息:package vim is not installed。退出状态为1。知道为什么会这样吗? -
这一行
Package 2:vim-enhanced-7.4.160-1.el7.x86_64 already installed and latest version显示包名实际上是vim-enhanced。自从我不得不处理 yum/CentOS/RHEL 以来已经有好几年了,但看起来实际上并没有vim软件包。您的系统已经通过其他方式安装了vim-enhanced,或者正在发生某种别名。无论哪种方式,尝试将软件包安装为vim-enhanced并在 CentOS 的 serverspec 中进行测试。 -
@KarenB 谢谢,那条评论很有启发性!看起来我需要了解每个系统的特定包名称。这解决了
vim问题。至于ag,通过在我打开的 GitHub 问题中的讨论,我找到了一种通过包管理器进行安装的方法,现在可以使用 Serverspec 包资源进行测试。如果您想将您的最后一条评论重新表述为答案(请注意,如果未使用包管理器安装某些内容,则必须解决包资源问题)我会将其标记为正确! -
我只想指出,
yum允许您以vim获取包,即使它在系统上安装为vim-enhanced,这让我在困惑中走到了这一步/问题。
标签: ruby centos vagrant chef-infra serverspec