Foreman hooks not working

Problem:
Foreman_hooks actually doesn’t work for my physical machine.
I’'ve tried to follow steps from: RHEL guide - Foreman hooks and the Github page.

I’ve tried to create /usr/share/foreman/config/hooks/host/managed/create with the two script:
10_logger.sh:

#!/bin/bash
source hook_functions.sh
logger $1  $2

(examples/hook_functions.sh)

I also receive the following error message, which I am unsure is from my hooks:
Not queueing Host::Managed: [“Ip is too long (maximum is 15 characters)”, “Ip is invalid”]

I can ensure you all that Foreman is able to find the hooks, but not able to execute it seems:

  • [I|app|] Finished discovering 2 hooks for Host::Managed#create*

Foreman and Proxy versions:
2.3.1

Other relevant data:

Is your hook executable? Permissions are correct? SELinux context?

If you disable all your hooks, do you still see the error?

If so, you need to figure out why you see this in the first place.

As I mentioned I followed the guide provided by Red Hat, so I can confirm:
hooks are executable, permissions are correct and SELinux context is OK.

Sorry the OP or your second one don’t have necessary information. What exactly is not working?

The error you see says what it says, the IP address was entered invalid. This has nothing to do with hooks.

Hi, sorry for my absence.

It seems the follow errors:

  • Not queueing Nic::Managed: ["IP address is too long (maximum is 15 characters)", "IP address is invalid"]

  • Not queueing Host::Managed: ["Ip is too long (maximum is 15 characters)", "Ip is invalid"]

  • Not queueing Discovery reboot: Ip is too long (maximum is 15 characters) and Ip is invalid

Are a result of calling POST to the "/unattended/built?token=XXXX".

When it comes to the foreman hooks, I can confirm that the hooks are discovered, but these are not executed:

[I|app|] Finished discovering 2 hooks for Host::Managed#create
[W|app|] Creating scope :completer_scope. Overwriting existing method Location.completer_scope.
[W|app|] Creating scope :completer_scope. Overwriting existing method Organization.completer_scope.

[I|app|a60f5815] Processed 4 tasks from queue 'Host::Managed Main', completed 4/4
[I|aud|a60f5815] Host::Base (4) update event on build true, false

Path to my hooks: /usr/share/foreman/config/hooks/host/managed/create/
The scripts:

/usr/share/foreman/config/hooks/host/managed/create/logger.sh

#!/bin/bash
. hook_functions.sh

logger $1 $2

/usr/share/foreman/config/hooks/host/managed/create/hook_functions.sh

# Utilities for hook scripts, used with foreman_hooks
#
# Source this at the start of any bash hook script:
#
#   . hook_functions.sh
#
# It reads in a JSON representation of the object and then provides a
# hook_data wrapper around jgrep to read fields from it, e.g.
#
#   hostname=$(hook_data host.name)
#   comment=$(hook_data host.comment)

# update, create, before_destroy etc.
HOOK_EVENT=$1
# to_s representation of the object, e.g. host's fqdn
HOOK_OBJECT=$2

HOOK_OBJECT_FILE=$(mktemp -t foreman_hooks.XXXXXXXXXX)
trap "rm -f $HOOK_OBJECT_FILE" EXIT
cat > $HOOK_OBJECT_FILE

hook_data() {
  if [ $# -eq 1 ]; then
    jgrep -s "$1" < $HOOK_OBJECT_FILE
  else
    jgrep "$*" < $HOOK_OBJECT_FILE
  fi
}

Selinux: system_u:object_r:bin_t:s0

This usually is misunderstanding of when certain actions trigger.

If you see those errors an object was not saved, validation failed thus create hooks will not be called.

I really can’t tell from what I see. First get your host created and properly saved, then investigate hooks. Turn on debug mode to see more.

Ok, that make sense. Right now i’m having hooks for creating host, but what i’m actually doing is re-building an existing host… Making me unsure of what “event” I should actually use. It should be something with after_build/provisioned.

I’ll turn on DEBUG mode.

Yeah, right now I feel sorry for wasting your time.
I believe it is working. The logger.sh worked.

Even though i didn’t find the events (GitHub - theforeman/foreman_hooks: Run custom hook scripts on Foreman events) intuitive, but i’m sure that’s just me.

Thanks for your time.

1 Like