chef-run vs chef under systemd - different gem repos

前提是你 提交于 2019-12-10 23:53:34

问题


I'm trying to make chef swap the official rubygems repo with my own local one. It kind of does work but let's say 'not always'. More on that below.

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

https://rubygems.org/

Chef is running under control of systemd. When I check in journal what chef has done so far I get

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)

The code of my recipe mycookbook::gem is as follows

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)

If I check the list of gem sources again I'll get

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

https://rubygems.org/

Unfortunately nothing has changed so far. Now, if I run chef-client directly from the console I finally see chef doing what I wanted to be done.

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/

When I turned on debug mode I noticed chef claiming the condition was not met

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

Wth? I did some further investigation and in desperation added to the recipe

  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)

Now when I checked the content of /tmp/chef_gem_sources I was utterly bewildered

cat chef_gem_sources 
*** CURRENT SOURCES ***

http://myrepo

Finally, in /tmp/chef_env I found HOME=/. It's obviously HOME=/root when I launch chef-client myself. It makes a huge difference as .gemrc has two locations and may have different values in :sources section.


回答1:


Two issues. First you want to put action :nothing on your two execute resources so they don't happen at both compile and converge time. Second, the output differs because Chef checks if stdout is a TTY when determining the output style. If it is a TTY, you get the new formatter output, otherwise you get logger output.



来源:https://stackoverflow.com/questions/34784151/chef-run-vs-chef-under-systemd-different-gem-repos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!