【问题标题】:chef-run vs chef under systemd - different gem repossystemd 下的 chef-run vs chef - 不同的 gem repos
【发布时间】:2016-01-16 19:15:01
【问题描述】:

我正在尝试让厨师将官方 ruby​​gems 存储库与我自己的本地存储库交换。它确实有效,但让我们说“并不总是”。更多内容如下。

bash-4.2$ /opt/chef/embedded/bin/gem sources
*** CURRENT SOURCES ***

https://rubygems.org/

Chef 在 systemd 的控制下运行。当我在日记中查看厨师到目前为止所做的事情时,我得到了

Jan 14 07:39:24 myserver.srv chef-client[32274]: [2016-01-14T07:39:24+01:00] INFO: Processing execute[add_my_own_repo] action run (mycookbook::gem line 5)
Jan 14 07:39:24 myserver.srv chef-client[32274]: [2016-01-14T07:39:24+01:00] INFO: Processing execute[Guard resource] action run (dynamically defined)
Jan 14 07:39:24 myserver.srv chef-client[32274]: [2016-01-14T07:39:24+01:00] INFO: execute[Guard resource] ran successfully
Jan 14 07:39:24 myserver.srv chef-client[32274]: [2016-01-14T07:39:24+01:00] INFO: Processing execute[del_official_https_rubygems_repo] action run (mycookbook::gem line 10)
Jan 14 07:39:24 myserver.srv chef-client[32274]: [2016-01-14T07:39:24+01:00] INFO: Processing execute[Guard resource] action run (dynamically defined)

我的食谱 mycookbook::gem 的代码如下

execute 'add_my_own_repo' do
  command '/opt/chef/embedded/bin/gem sources --add http://myrepo'
  not_if '/opt/chef/embedded/bin/gem sources --list | grep myrepo'
end.run_action(:run)

execute 'del_official_https_rubygems_repo' do
  command '/opt/chef/embedded/bin/gem sources --remove  https://rubygems.org/'
  only_if '/opt/chef/embedded/bin/gem sources --list | grep https://rubygems.org'
end.run_action(:run)

如果我再次检查 gem 来源列表,我会得到

bash-4.2$ /opt/chef/embedded/bin/gem sources
*** CURRENT SOURCES ***

https://rubygems.org/

不幸的是,到目前为止一切都没有改变。现在,如果我直接从控制台运行 chef-client,我终于看到 chef 正在做我想做的事情。

Recipe: mycookbook::gem
  * execute[add_my_own_repo] action run
   - execute /opt/chef/embedded/bin/gem sources --add http://myrepo
  * execute[del_official_https_rubygems_repo] action run
    - execute /opt/chef/embedded/bin/gem sources --remove  https://rubygems.org/

当我打开调试模式时,我注意到厨师声称条件不满足

DEBUG: Skipping execute[del_official_https_rubygems_repo] due to only_if command `gem sources --list | /usr/bin/grep https://rubygems.org`

什么?我做了一些进一步的调查,绝望地添加到食谱中

  execute 'CHEF_ENV' do
    command 'env >> /tmp/chef_env'
  end.run_action(:run)

  execute 'GEM_SOURCES' do
   command 'gem sources --list >> /tmp/chef_gem_sources'
  end.run_action(:run)

现在当我检查 /tmp/chef_gem_sources 的内容时,我完全不知所措

cat chef_gem_sources 
*** CURRENT SOURCES ***

http://myrepo

最后,在 /tmp/chef_env 中我找到了 HOME=/。当我自己启动 chef-client 时,显然是 HOME=/root 。它有很大的不同,因为 .gemrc 有两个位置,并且在 :sources 部分中可能有不同的值。

【问题讨论】:

  • 使用 grep 的完整路径,因此您确定这不是父环境 PATH 的 PATH 问题。我很确定env -i /opt/chef/bin/chef-client 也会失败。
  • 我将 chef-client 拥有的 $PATH 变量的值记录到一个文件中。一切安好。不需要 grep 的完整路径。
  • 在您的 systemd 启动中添加 -l debug 以便您在日志中获得更多信息。如果 grep 行为正常,我认为没有任何理由。
  • 在日记中chef-client[14910]: [2016-01-15T18:04:30+01:00] DEBUG: Skipping execute[del_official_https_rubygems_repo] due to only_if command /opt/chef/embedded/bin/gem sources --list | /usr/bin/grep https://rubygems.org 然后当我自己运行它时,条件就满足了......
  • edit你的帖子,并确保之前的状态......

标签: chef-infra


【解决方案1】:

两个问题。首先,您要将action :nothing 放在您的两个执行资源上,这样它们就不会在编译和收敛时发生。其次,输出不同,因为 Chef 在确定输出样式时会检查 stdout 是否为 TTY。如果是 TTY,则获得新的格式化程序输出,否则获得记录器输出。

【讨论】:

  • 感谢您的回复。仍然没有帮助让它按应有的方式工作。
猜你喜欢
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 2023-04-08
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
相关资源
最近更新 更多