Host Foreman Hooks

I am trying to extend the host provisioning (namely the fact that a
location doesn't have a default puppet and puppet CA proxy) through the
hook functionality.

My script is fairly simple, upon saving a host (I tried create,
after_create, after_save) I query the location, find the default proxies
and then update the host through the API.

The first issue I ran into is that the data sent to create doesn't include
the new host ID, it is null. I tried query the hosts on the hostname, but
it doesn't return a result. Then I tried after_create, this time the new ID
is present, however a call to api/hosts/HOST-ID returns 404. I moved onto
after_save but same thing, 404 when trying to update the host.

I was able to get it to work using after_commit. It appears that in the
other cases the host isn't fully saved such that it isn't available via the
API. The problem with after_commit is that it is called whenever the host
is saved. That includes when editing and when things such as facts and
reports are uploaded. Meaning it runs all the time.

> I am trying to extend the host provisioning (namely the fact that a
> location doesn't have a default puppet and puppet CA proxy) through the
> hook functionality.
>
> My script is fairly simple, upon saving a host (I tried create,
> after_create, after_save) I query the location, find the default proxies
> and then update the host through the API.
>
> The first issue I ran into is that the data sent to create doesn't
> include the new host ID, it is null. I tried query the hosts on the
> hostname, but it doesn't return a result. Then I tried after_create,
> this time the new ID is present, however a call to api/hosts/HOST-ID
> returns 404. I moved onto after_save but same thing, 404 when trying to
> update the host.
>
> I was able to get it to work using after_commit. It appears that in the
> other cases the host isn't fully saved such that it isn't available via
> the API.

Yes, commit is the database transaction commit, so that happens, the
transaction will still be in progress and you can't see or update the
host. Perhaps a future enhancement for foreman_hooks will be some
two-way functionality so it can handle updating of the object to avoid
needing to use the API.

> The problem with after_commit is that it is called whenever the
> host is saved. That includes when editing and when things such as facts
> and reports are uploaded. Meaning it runs all the time.

This can sometimes be solved by using another hook on a create action to
touch a file, then only perform an action in the second hook if the file
exists - then delete it.

··· On 10/03/16 04:30, Matthew Ceroni wrote:


Dominic Cleal
dominic@cleal.org

>
> > I am trying to extend the host provisioning (namely the fact that a
> > location doesn't have a default puppet and puppet CA proxy) through the
> > hook functionality.
> >
> > My script is fairly simple, upon saving a host (I tried create,
> > after_create, after_save) I query the location, find the default proxies
> > and then update the host through the API.
> >
> > The first issue I ran into is that the data sent to create doesn't
> > include the new host ID, it is null. I tried query the hosts on the
> > hostname, but it doesn't return a result. Then I tried after_create,
> > this time the new ID is present, however a call to api/hosts/HOST-ID
> > returns 404. I moved onto after_save but same thing, 404 when trying to
> > update the host.
> >
> > I was able to get it to work using after_commit. It appears that in the
> > other cases the host isn't fully saved such that it isn't available via
> > the API.
>
> Yes, commit is the database transaction commit, so that happens, the
> transaction will still be in progress and you can't see or update the
> host. Perhaps a future enhancement for foreman_hooks will be some
> two-way functionality so it can handle updating of the object to avoid
> needing to use the API.
>
> > The problem with after_commit is that it is called whenever the
> > host is saved. That includes when editing and when things such as facts
> > and reports are uploaded. Meaning it runs all the time.
>
> This can sometimes be solved by using another hook on a create action to
> touch a file, then only perform an action in the second hook if the file
> exists - then delete it.
>

I like this solution. Thanks.

··· On Thursday, March 10, 2016 at 1:22:46 AM UTC-8, Dominic Cleal wrote: > On 10/03/16 04:30, Matthew Ceroni wrote:


Dominic Cleal
dom...@cleal.org <javascript:>