Foreman hooks problem with gems

I’m using a small framework for foreman hooks, written in ruby & requires additional gems.
If I run scripts manually - everything works fine.
When foreman runs scripts I see this raise:

Hook: 20_server_inventory.rb task failed with the following error: Hook failure running `/usr/share/foreman/config/hooks/host/managed/destroy/20_server_inventory.rb destroy sjc04t1itoap01.local`: /opt/rh/rh-ruby22/root/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- tiny_tds (LoadError) from /opt/rh/rh-ruby22/root/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require' from /usr/share/foreman/config/hooks/libs/inventory.rb:4:in `initialize' from /usr/share/foreman/config/hooks/host/managed/destroy/20_server_inventory.rb:7:in `new' from /usr/share/foreman/config/hooks/host/managed/destroy/20_server_inventory.rb:7:in `<main>'

If there a way to fix this?

I found gem & ruby under /opt/rh/rh-ruby22/root/bin - but I can’t install any gems

[root@foreman vagrant]# /opt/rh/rh-ruby22/root/usr/bin/gem install tiny_tds
/opt/rh/rh-ruby22/root/usr/bin/ruby: error while loading shared libraries: cannot open shared object file: No such file or directory


I think you need to install the gem from inside the rh-ruby22 scl - so something like this:

scl enable rh-ruby22 'gem install tiny_tds'
1 Like

Yep, this is exactly how you do it. If you want to use system ruby there is not an easy way of “exiting” SCL environment so it’s better to install the gems into the SCL.

Thank you for your help, guys.

One small question. I use Gemfile is there a way to make it work with bundle?
It installs by default to /tmp directory :confused: Thanks

[root@foreman destroy]# scl enable rh-ruby22 ‘bundler install --gemfile=/usr/share/foreman/config/hooks/Gemfile’
Don’t run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
Using bundler 1.16.2
Using net-ping 2.0.4
Using oauth 0.5.4
Using tiny_tds 2.1.2
Bundle complete! 3 Gemfile dependencies, 4 gems now installed.
Bundled gems are installed into /tmp
[root@foreman destroy]#

And actually it fails to install tiny_tds :confused:

[root@foreman destroy]# scl enable rh-ruby22 ‘gem install tiny_tds’
Fetching: tiny_tds-2.1.2.gem (100%)
Building native extensions. This could take a while…
ERROR: Error installing tiny_tds:
ERROR: Failed to build gem native extension.

/opt/rh/rh-ruby22/root/usr/bin/ruby -r ./siteconf20180704-13021-1go3hwd.rb extconf.rb

mkmf.rb can’t find header files for ruby at /opt/rh/rh-ruby22/root/usr/share/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /opt/rh/rh-ruby22/root/usr/local/share/gems/gems/tiny_tds-2.1.2 for inspection.
Results logged to /opt/rh/rh-ruby22/root/usr/local/lib64/gems/ruby/tiny_tds-2.1.2/gem_make.out

Okay, this fixed the problem yum install rh-ruby22-ruby-devel.x86_64

Okay, if anybody interested I made it work with OS ruby.

  1. Changed binary path to -> #!/usr/bin/ruby instead of #!/usr/bin/env ruby
  2. Bundler installation: /usr/local/bin/bundle --gemfile /usr/share/foreman/config/hooks/Gemfile --path /usr/share/foreman/.gem/

Works as a charm.

Thanks everyone

1 Like

This sounds like a potential blog post … if you’re looking for something to contribute :smiley: