运行 作为 Test Kitchen 中特定用户的命令

Running a command as a specific user in Test Kitchen

我知道您可以 运行 Chef 食谱中的 'execute' 模块,它包含一个 cwd 和用户参数,允许您以该用户身份 运行 命令当前工作目录。这是我在我的代码中所做的:

execute 'rpmdev-setuptree' do
   user 'rpmbuild'
   cwd '/home/rpmbuild'
   live_stream true
   action :run
end

我一直在做不同的变体,以 root 身份执行此命令或显式更改目录(即执行 'cd /home/rpmbuild && rpmdev-setuptree),但这不起作用。无论我做什么,Chef 似乎都下定决心要在 /root 目录中 运行 这个命令。为什么会这样?请注意,我一直在做:

execute 'cd /home/rpmbuild  && rpmdev-setuptree' do
   user 'rpmbuild'
   cwd '/home/rpmbuild'
   live_stream true
   action :run
end

execute 'sudo su - rpmbuild  && rpmdev-setuptree' do
   user 'root'
   cwd '/home/rpmbuild'
   live_stream true
   action :run
end 

谁能告诉我为什么会这样?我正在尝试 运行 /home/rpmbuild 目录中的 rpmdev-setuptree 以便它构建路径,但它总是 运行 在 /root 目录中执行此命令。

输出在我 运行 的哪个片段之间有所不同,有些片段已完成但没有输出。但是我在尝试单独使用 rpmdev-setuptree 时没有注意到,它会:

   [execute] touch: cannot touch ‘/root/.rpmmacros’: Permission denied
      grep: /root/.rpmmacros: No such file or directory
      /bin/rpmdev-setuptree: line 43: [: -lt: unary operator expected
      grep: /root/.rpmmacros: No such file or directory
      /bin/rpmdev-setuptree: line 57: [: -lt: unary operator expected
      grep: /root/.rpmmacros: No such file or directory
      /bin/rpmdev-setuptree: line 75: [: -lt: unary operator expected
      mkdir: cannot create directory ‘/root/rpmbuild’: Permission denied
      mkdir: cannot create directory ‘/root/rpmbuild’: Permission denied
      mkdir: cannot create directory ‘/root/rpmbuild’: Permission denied
      mkdir: cannot create directory ‘/root/rpmbuild’: Permission denied
      mkdir: cannot create directory ‘/root/rpmbuild’: Permission denied

您确定问题出在工作目录上,而不是 rpmdev-setuptree 试图使用 $HOME 做某事吗?由于各种不太充分的原因,当 运行 为不同的用户时,我们不会自动设置 $HOME,因此您可能还必须添加 environment 'HOME' => '/home/rpmbuild'