Internal/ruby event that could be used for hook on subnet creation?

Hello all!

Is there a way for me to associate some internal ruby event to a hook to run something for me on subnet creation?

I’m looking for something similar to host/discovered/[after_commit|after_create|after_destroy] so I can automate subnet creation/deletion of said subnet in DHCP config and restart ISC dhcpd after that.

Thanks!

Possibly just answered my own question:

# foreman-rake hooks:events[subnet]
after_audit
after_commit
after_create
after_destroy
after_find
after_initialize
after_rollback
after_save
after_touch
after_update
after_validate
after_validation
before_audit
before_create
before_destroy
before_save
before_update
before_validate
before_validation

1 Like

Hmm, even though Foreman does recognize a simple script I put under ~foreman/config/hooks/subnet/{after_commit,after_create,after_destroy}/log.sh, I don’t see that script executed:

2018-09-06 14:44:06 [app] [D] Found hook to Subnet#after_destroy, filename 00_log.sh
2018-09-06 14:44:06 [app] [D] Found hook to Subnet#after_create, filename 00_log.sh
2018-09-06 14:44:06 [app] [D] Found hook to Subnet#after_commit, filename 00_log.sh
2018-09-06 14:44:06 [app] [I] Finished discovering 1 hooks for Subnet#after_destroy
2018-09-06 14:44:06 [app] [I] Finished discovering 1 hooks for Subnet#after_create
2018-09-06 14:44:06 [app] [I] Finished discovering 1 hooks for Subnet#after_commit
2018-09-06 14:44:09 [app] [D] Extending Subnet with foreman_hooks Rails hooking support
2018-09-06 14:44:09 [app] [D] Created hook method after_destroy on Subnet
2018-09-06 14:44:09 [app] [D] Created hook method after_create on Subnet
2018-09-06 14:44:09 [app] [D] Created hook method after_commit on Subnet

On a subnet deletion/update/create, I only see this in a log:

2018-09-06 14:47:10 6e900e71 [app] [D] Observed after_commit hook on

Instead of expected output like this (other hooks on the same Foreman instance):

2018-09-06 14:52:47 1069c3e6 [sql] [D] Observed update hook on
2018-09-06 14:52:47 1069c3e6 [sql] [D] Queuing 3 hooks for Host::Managed#update
2018-09-06 14:52:47 1069c3e6 [sql] [D] Queuing hook 00_log.sh for Host::Managed#update at priority 00
2018-09-06 14:52:47 1069c3e6 [sql] [D] Running hook: /usr/share/foreman/config/hooks/host/managed/update/00_log.sh update
2018-09-06 14:52:48 1069c3e6 [sql] [D] Task Hook: 00_log.sh completed

Am I out of luck here and can’t actually use Ruby events for subnets?
Thanks!

This is 1.15. Forgot to mention.

Shooting in the dark: Is the file executable? Permissions and ownership?

This should work.

Yes, everything with the script is perfectly fine. In fact, that’s just a symlink to the same script that host/managed hooks use and the latter have no issues with it as you can see in my example above.

I agree, this seems like something that should just work, but it does not and I’m puzzled here…

Can you drop the symlink and do a copy? SELinux might be preventing that. Absolute symlinks are also problem sometimes (not in this context but in chroots).

I did try that, but there’s no difference in behavior - I still see that hook is observed, but not executed :frowning:

Anything else I could try?

Can you put bunch of puts into the code and identify where it stops?

Sure, I can try that. I’m just wondering if anybody else have tried and got that working? Should be easy to setup and try it out.