Problem with Hooks events


I am trying to use Foreman_hooks to provision our IPAM (Netbox) during Host creation.

I managed to do it during the create event, but the data sent to my script by Foreman is very poor : there is not enough information for our IPAM (as vcpus, memory, disk, netmask, cluster name, etc…).

So, I modified my script so that it uses Foreman API (/api/hosts/:id/vm_compute_attributes) to gather those information.

Problem is that, during create event, the Host is not yet created and the API call returns “host does not exists”.

So I tried with after_create event, same problem.
I also tried the postcreate event (as documented), but this event is never triggered (and it does not even appear in the foreman-rake hooks:events[host/managed] list…)

Then, I tried with the after_commit event, and then it worked. But the problem is that this event seems also to be triggered for a lot of other Hosts already that were created before, I don’t understand why…

Can someone explain to me when exactly are the different event triggered ?

What is the right way to achieve what I’m trying to do ?



Major problem with hooks is that this is tightly coupled with Rails ActiveRecord stack, particularly it’s callback system which also change over time. Some related objects are not available until after_commit. Also it renders JSON via our HAML/API templates, so it can only provide what’s available there.

All I can recommend for now is to forget about the incoming JSON and make your API request to get the data. The problem is that you should not do the request the moment you get called because the data was not yet commited to database so the other request would not see them. So terrible hack and idea but save the event to some queue, wait a bit and then pull the required data via API and do your actions.

This is actually what I would like to propose - to replace foreman_hooks with a simple callbacks hooked on controller level and published via Redis Queues, which we currently consider adding into the project.


Thank you very much for this very detailed answer !
I will try to do what you suggest.

Thanks again for the quick answer.


Thomas Castelle

I just created RFC on new callback mechanism. You are not the only one struggling with hooks.

1 Like

I could not get the postcreate event to work as well. I looked at the code for a bit and everything looked correct.

I ended up doing the work-around mentioned here:!topic/foreman-users/X3excyjwzM4

I have a simple create hook that touches a file (named after the host) and my after_commit hook only executes if the file exists. It deletes the file before doing any work, thereby eliminating the possibility of the hook executing twice if 2 after_commit events fired in rapid succession.

I determined why postcreate isn’t working for me. I am using ruby-foreman-hooks 0.3.14-1 and postcreate was added after this release:

Author: Lukas Zapletal
Date: Tue Aug 14 13:14:48 2018 +0200

Post queue orchestration mechanism

commit b8f2ab5f0cdda76a060e003080f58a8184caed64
Author: Lukas Zapletal
Date: Tue Aug 14 12:54:04 2018 +0200

Task name is now unique

commit 4fc1977e446bae65a42219c7dc8ff1ad10315313 (tag: v0.3.15)
Author: Sean O’Keeffe
Date: Mon Jun 18 10:39:53 2018 +0100

Release v0.3.15

Version v0.3.16 hit nightly last week which includes the patch.