Zenith
Zenith

Reputation: 21

chef-run vs chef under systemd - different gem repos

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.

Upvotes: 2

Views: 208

Answers (1)

coderanger
coderanger

Reputation: 54181

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.

Upvotes: 1

Related Questions