Coder Social home page Coder Social logo

theforeman / foreman_hooks Goto Github PK

View Code? Open in Web Editor NEW
54.0 12.0 50.0 111 KB

Run custom hook scripts on Foreman events

Home Page: http://m0dlx.com/blog/Extending_Foreman_quickly_with_hook_scripts.html

License: GNU General Public License v3.0

Ruby 98.47% Shell 1.53%
hacktoberfest

foreman_hooks's Introduction

foreman_hooks

Allows you to trigger scripts and commands on the Foreman server at any point in an object's lifecycle in Foreman. This lets you run scripts when a host is created, or finishes provisioning etc.

It enables extension of Foreman's host orchestration so additional tasks can be executed, and can register hooks into standard Rails callbacks for any Foreman object, all with shell scripts.

Installation:

Please see the Foreman wiki for appropriate instructions:

The gem name is "foreman_hooks".

RPM users can install the "tfm-rubygem-foreman_hooks" or "rubygem-foreman_hooks" packages. Debian/Ubuntu users can install the "ruby-foreman-hooks" package.

Usage

Hooks are stored in /usr/share/foreman/config/hooks (~foreman/config/hooks) with a subdirectory for the object, then a subdirectory for the event name.

~foreman/config/hooks/[OBJECT]/[EVENT]/[HOOK_SCRIPT]

Examples:

~foreman/config/hooks/host/managed/create/50_register_system.sh
~foreman/config/hooks/host/managed/destroy/15_cleanup_database.sh
~foreman/config/hooks/smart_proxy/after_create/01_email_operations.sh
~foreman/config/hooks/audited/audit/after_create/01_syslog.sh

After adding or removing hooks, restart the Foreman server to update the list of known hooks (usually apache2 or httpd when using Passenger, or touch ~foreman/tmp/restart.txt).

Objects / Models

Every object (or model in Rails terms) in Foreman can have hooks. Check ~foreman/app/models for the full list, but these are the interesting ones:

  • host/managed
  • config_report (or report in Foreman 1.10 or older)
  • nic/managed
  • hostgroup
  • user

To generate a list of all possible models, issue the following command:

# foreman-rake hooks:objects

and to get events for a listed object (e.g. host/managed):

# foreman-rake hooks:events[host/managed]

Orchestration events

Only supported on these objects:

  • host/managed
  • nic/*

Foreman supports orchestration tasks for hosts and NICs (each network interface) which happen when the object is created, updated and destroyed. These tasks are shown to the user in the UI and if they fail, will automatically trigger a rollback of the action. A rollback is performed as an opposite action (e.g. for DHCP record creation a rollback action is destroy).

The following hooks are executed during around_save Rails callback:

  • create
  • update

The following hooks are executed during on_destroy Rails callback:

  • destroy

The following hooks are executed during after_commit Rails callback:

  • postcreate
  • postupdate

The major difference between create and postcreate (or update respectively) is how late the hook is called during save operation. In the former case when a hook fails it starts rollback and operation can be still cancelled. In the latter case object was already saved and there is no way of cancelling the operation, but all referenced data should be properly loaded. The advice is to use the latter hooks as they will likely contain all the required data (e.g. nested parameters).

Orchestration hooks can be given a priority by prefixing the filename with the priority number, therefore it is possible to order them before or after built-in orchestration steps (before DNS records are created for example). Existing common priority levels are:

  • 2: Set up compute instance (create VM)
  • 10: Create DNS record
  • 10: Create DHCP reservation
  • 20: Deploy TFTP configs
  • 50: Create realm entry
  • 1000: Power up compute instance

Rails events

Supported on all object types.

For hooks on anything apart from hosts or NICs (which support orchestration, as above) then the standard Rails events will be needed. These are the most interesting events provided:

  • after_create, before_create
  • after_destroy, before_destroy

Every event has a "before" and "after" hook. For the full list, see the Constants section at the bottom of the ActiveRecord::Callbacks documentation.

The host object has two additional callbacks that you can use:

  • host/managed/after_build triggers when a host is put into build mode (does not trigger upon new host creation, even when build flag is set)
  • host/managed/before_provision triggers when a host completes the OS install

Execution of hooks

Hooks are executed in the context of the Foreman server, so usually under the foreman user.

The first argument is always the event name, enabling scripts to be symlinked into multiple event directories. The second argument is the string representation of the object that was hooked, e.g. the hostname for a host.

~foreman/config/hooks/host/managed/create/50_register_system.sh create foo.example.com

A JSON representation of the hook object will be passed in on stdin. A utility to read this with jgrep is provided in examples/hook_functions.sh and sourcing this utility script will be enough for most users. Otherwise, you may want to ensure stdin is closed to prevent pipe buffer from filling.

echo '{"host":{"name":"foo.example.com"}}' \
  | ~foreman/config/hooks/host/managed/create/50_register_system.sh \
       create foo.example.com

Some arguments are available as environment variables:

Variable Description
FOREMAN_HOOKS_USER Username of Foreman user

Every hook within the event directory is executed in alphabetical order. For orchestration hooks, an integer prefix in the hook filename will be used as the priority value, so influences where it's done in relation to DNS, DHCP, VM creation and other tasks.

When testing hooks, don't rely on writing logs to /tmp or /var/tmp as you may not be able to see the contents. On modern systemd-based OSes, Apache (and Foreman) is run with a private temp directory to improve security - consider using ~foreman/tmp/ instead, or read /tmp/systemd-private-* as root.

Hook failures and rollback

If a hook fails (non-zero return code), the event is logged. For Rails events, execution of other hooks will continue.

For orchestration events, a failure will halt the action and rollback will occur. If another orchestration action fails, the hook might be called again to rollback its action - in this case the first argument will change as appropriate, so must be obeyed by the script (e.g. a "create" hook will be called with "destroy" if it has to be rolled back later).

Logging

Entries are logged at application startup and during execution of hooks, but most will be at 'debug' level and may use the 'sql' logger. Enable this in Foreman's /etc/foreman/settings.yaml:

:logging:
  :level: debug
:loggers:
  :sql:
    :enabled: true

See Foreman manual: Debugging for full details.

Enabling debugging and searching the Foreman log file (/var/log/foreman/production.log) for the word "hook" will find all relevant log entries.

Hook discovery and setup

Expect to see these entries when the server starts:

  • Found hook to Host::Managed#create, filename 01_example - for each executable hook script in the correct location
  • Finished discovering 3 hooks for Host::Managed#create - for each unique event with hook scripts
  • Extending Host::Managed with foreman_hooks orchestration hooking support - if any orchestration (create/update/destroy) hooks exist for that object
  • Extending Host::Managed with foreman_hooks Rails hooking support - if any Rails events hooks exist for that object
  • Created hook method after_create on Host::Managed - for each type of Rails event that has hooks

Running hooks

Expect to see these entries logged when a hooked action occurs:

  • Observed after_create hook on test.example.com when a registered Rails event occurs, hook will then execute immediately
  • Queuing 3 hooks for Host::Managed#create when an orchestration action is being set up (hook will be executed later during orchestration)
  • Queuing hook 01_example for Host::Managed#create at priority 01 for each hook registered when setting up an orchestration action
  • Running hook: /example/config/hooks/host/managed/create/01_example create test.example.com as the hook (orchestration or Rails event) is executed

Transactions

Most hooks are triggered during database transaction. This can cause conflicting updates when hook scripts emits database updates via Foreman CLI or API. It is recommended to avoid this behavior and write a Foreman plugin instead.

SELinux notes

When using official installation on Red Hat and Fedora system, note that SELinux is turned on by default and Foreman is running in confined mode. Make sure that hook scripts has the correct context (foreman_hook_t on RHEL7+/Fedora 19+ or bin_t on RHEL6):

restorecon -RvF /usr/share/foreman/config/hooks

Also keep in mind that the script is running confined, therefore some actions might be denied by SELinux. Check audit.log and use audit2allow and other tools when writing scripts.

More resources

Copyright

Copyright (c) 2012-2017 Dominic Cleal

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

foreman_hooks's People

Contributors

abenari avatar bkearney avatar doddo avatar domcleal avatar ekohl avatar jameslikeslinux avatar jlsherrill avatar lzap avatar marcgrimme avatar phirince avatar pronix avatar sean797 avatar shimshtein avatar stbenjam avatar tchellomello avatar wiad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

foreman_hooks's Issues

FOREMAN_HOOKS_USER not working with audits and user logins

The following line:
ENV['FOREMAN_HOOKS_USER'] = User.current.login

in lib/foreman_hooks/util.rb sets current user as an environment variable. This does not work when a new external user logs in and Foreman tries to update user attributes (firstname, lastname, email). The changes are executed and audited which will trigger any hooks you may have in audited/audit/, which will fail with backtrace:

2022-05-12T08:59:08 [D|app|d0bc1f0d] Running 6 hooks for Audited::Audit#after_create
2022-05-12T08:59:08 [D|sql|d0bc1f0d] Running hook: /usr/share/foreman/config/hooks/audited/audit/after_create/05-test.sh after_create 166891
2022-05-12T08:59:08 [D|sql|d0bc1f0d]    (0.5ms)  ROLLBACK
2022-05-12T08:59:08 [W|app|d0bc1f0d] undefined method `login' for nil:NilClass
2022-05-12T08:59:08 [I|app|d0bc1f0d] Backtrace for 'undefined method `login' for nil:NilClass' error (NoMethodError): undefined method `login' for nil:NilClass
 d0bc1f0d | /usr/share/gems/gems/foreman_hooks-0.3.17/lib/foreman_hooks/util.rb:56:in `exec_hook_int'

Probably because the User.current.login is not available at this stage. So we would need some validation and fallback setting here.

Request additional JSON data be passed to hooks

(From @grimme-atix-de)

Currently the JSON representation of the object receiving the event is passed into the hook, but it would be good to pass in additional related data such as host parameters.

Not able to render json because the rabl file is not found fore host/discovereds

I'm trying to run a hook fore after_save event of Host::Discoverd object.

I'm getting the following error:
2015-08-07 07:58:28 [W] Unable to render mac002128a5131e (Host::Discovered) using RABL: Cannot find rabl template 'api/v2/host/discovereds/show' within registered (["app/views", "/usr/share/foreman/app/views"]) view paths!

this line of code assumes that there is a rabl file for the host object. Only that in this case, the object is not of Host::Managed class. Since foreman_discovery is a plugin, it doesn't make sense to ship the rabl file with the foreman code.
https://github.com/theforeman/foreman_hooks/blob/master/lib/foreman_hooks/util.rb#L18https://github.com/theforeman/foreman_hooks/blob/master/lib/foreman_hooks/util.rb#L18

What could be the best solution in this case?

-PP

Empty values/No facts as json input for after_create event after discovery

Using
tfm-rubygem-foreman_hooks-0.3.14-1.fm1_15.el7.noarch
foreman-proxy-1.15.3-1.el7.noarch
foreman-1.15.3-1.el7.noarch
tfm-rubygem-foreman_discovery-9.1.1-1.fm1_15.el7.noarch

I dont see facts in input json after foreman discovers a VM/baremetal.

This is the workflow that i am trying with hooks and foreman discovery

VM/baremetal gets discovered -> JSON input to hooks -> Hooks use the facts from JSON input to add more facts from CMDB -> Facts get uploaded to foreman.

I have created the following hooks:

/usr/share/foreman/config/hooks/host/discovered/after_create/10-logger.py

ls /usr/share/foreman-community/hooks/
functions.py functions.pyc init.py

The scripts just get the input json and output the json to temp directory, to help me understand the structure.
However - I dont see facts or any other useful information during input json.

{
"id": 22,
"name": "mac00163e5426c9",
"last_compile": null,
"last_report": null,
"updated_at": "2017-08-18T20:13:44.058Z",
"created_at": "2017-08-18T20:13:44.058Z",
"root_pass": null,
"architecture_id": null,
"operatingsystem_id": null,
"environment_id": null,
"ptable_id": null,
"medium_id": null,
"build": false,
"comment": null,
"disk": null,
"installed_at": null,
"model_id": null,
"hostgroup_id": null,
"owner_id": null,
"owner_type": null,
"enabled": true,
"puppet_ca_proxy_id": null,
"managed": false,
"use_image": null,
"image_file": null,
"uuid": null,
"compute_resource_id": null,
"puppet_proxy_id": null,
"certname": null,
"image_id": null,
"organization_id": null,
"location_id": null,
"otp": null,
"realm_id": null,
"compute_profile_id": null,
"provision_method": null,
"grub_pass": "",
"global_status": 0,
"lookup_value_matcher": null,
"pxe_loader": null,
"discovery_rule_id": null
}

Foreman 1.8.2 crashes after installing foreman_hooks

I am using Ubuntu 14.04 and the Foreman 1.8 deb repo. I have a Foreman 1.8.2 instance that I upgraded from 1.7.2. After I install foreman_hooks, either with the deb of 0.3.7 or the latest gem of 0.3.8, Foreman crashes on any web page with the following message:

Web application could not be started

undefined local variable or method `belongs_to_host' for #<Class:0x0000000432a9e8> (NameError)
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activerecord-3.2.21/lib/active_record/dynamic_matchers.rb:55:in `method_missing'
  /usr/share/foreman/app/models/report.rb:6:in `<class:Report>'
  /usr/share/foreman/app/models/report.rb:1:in `<top (required)>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:359:in `require_or_load'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:502:in `load_missing_constant'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:514:in `load_missing_constant'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:514:in `load_missing_constant'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/app/models/concerns/hostext/search.rb:18:in `block in <module:Search>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/concern.rb:121:in `class_eval'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/concern.rb:121:in `append_features'
  /usr/share/foreman/app/models/host/managed.rb:3:in `include'
  /usr/share/foreman/app/models/host/managed.rb:3:in `<class:Managed>'
  /usr/share/foreman/app/models/host/managed.rb:1:in `<top (required)>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:359:in `require_or_load'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:502:in `load_missing_constant'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:230:in `block in constantize'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:229:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:229:in `constantize'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
  /usr/share/foreman/app/models/host.rb:15:in `method_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks/orchestration_hook.rb:8:in `block in <module:OrchestrationHook>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/concern.rb:121:in `class_eval'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/concern.rb:121:in `append_features'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:71:in `include'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:71:in `attach_hook'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:85:in `block in load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:84:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:84:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:514:in `load_missing_constant'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:514:in `load_missing_constant'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:192:in `block in const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:190:in `const_missing'
  /usr/share/foreman/config/initializers/active_record_extensions.rb:2:in `<class:Base>'
  /usr/share/foreman/config/initializers/active_record_extensions.rb:1:in `<top (required)>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:245:in `load'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:245:in `block in load'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:245:in `load'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/engine.rb:593:in `block (2 levels) in <class:Engine>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/engine.rb:592:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/engine.rb:592:in `block in <class:Engine>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:30:in `instance_exec'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:30:in `run'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:55:in `block in run_initializers'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:54:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:54:in `run_initializers'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/application.rb:136:in `initialize!'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/railtie/configurable.rb:30:in `method_missing'
  /usr/share/foreman/config/environment.rb:12:in `<top (required)>'
  config.ru:3:in `require'
  config.ru:3:in `block in <main>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/rack-1.4.7/lib/rack/builder.rb:51:in `instance_eval'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/rack-1.4.7/lib/rack/builder.rb:51:in `initialize'
  config.ru:1:in `new'
  config.ru:1:in `<main>'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'

error message from foreman_hook to be printed in the foreman UI

Failing hooks sometimes are hard for the user to understand, and if they were printed in the appearing error message and not just only the logs, that would help a lot.

So if hooks could print STDERR when failing it would make the reason for failure easier to understand

Audit hook never called

I am attempting to setup the examples/ scripts to verify that audit events are logged, but I can't get any audit events to write to /tmp/hook.log. I copied the same scripts to config/hooks/host/managed/after_update/ and changing host group was logged. I've tried config/hooks/audited/adapters/active_record/audit/after_create/log.sh per the documentation but nothing is logged. When I start rails via rails s -d in development, this plugin prints the hooks are registered. This is using foreman_hooks 0.3.9 and current develop branch of Foreman.

long running process

What happens when a create hook script takes 10 minutes to complete? Does the hook get backgrounded? Would the UI block on the script? Aside from backgrounding the system command used inside the script is there a better way to wait for tasks without hanging the UI?

question: does foreman hooks work on Katello Content hosts too?

Hi! Im new to Foreman and Katello and I was wondering if I can make foreman hooks work on content hosts too, or if is there a way to add an existing host to Foreman. Ive made some research and I found a script called foreman_client but that did not work for me.

Hosts: Base versus Manged

When setting up a hook, I want this one to apply to all non discovered hosts regardless of if they are managed or not, I was trying to use host/base which host/managed inherits. In this scenario, it appears that host/base is not called. Specifically, I'm using after_destroy. Is this intentional? If so is there a way to setup a hook for all hosts or is it suggested to symlink to multiple paths host/base and host/managed?

Hook data json structure has changed in latest release

In 0.3.14 the hook data is now nested too deep (extra host hash)

[root@foreman-web01c tmp]# cat hook.object | jq
{
  "host": {
    "host": {
      "ip": "192.168.1.125",
      "ip6": "fe80::250:56ff:fea0:3078",
      "environment_id": 46,
 ...

compared to 0.3.13

[root@foreman-web01c tmp]# cat hook.object  | jq
{
  "host": {
    "ip": "192.168.1.125",
    "ip6": "fe80::250:56ff:fea0:3078",
    "environment_id": 46,

foreman-rake hooks:events[host/managed] doesnt list all events

The below output doesn't include after_build & before_provision. I think it should?

[root@foreman ~]# foreman-rake hooks:events[host/managed]
after_commit
after_create
after_destroy
after_find
after_initialize
after_rollback
after_save
after_touch
after_update
after_validation
before_create
before_destroy
before_save
before_update
before_validation
create
destroy
update

Hooks fail to render JSON when running under foreman-tasks

In Katello, the job of destroying hosts is farmed off to foreman-tasks so that associated Katello entities like content_hosts can be dealt with at the same time. foreman-tasks runs outside of the main Foreman process, but takes care to load Foreman's application.rb and all of Foreman's plugins. The result is that hooks are run in foreman-tasks jobs, but they fail to find Foreman's views path:

2015-12-03 00:28:20 [D] Running hook: /usr/share/foreman/config/hooks/host/managed/destroy/50_remove_dhcp_conf.sh destroy ci-build-1235.umd.edu
2015-12-03 00:28:20 [W] Unable to render ci-build-1235.umd.edu (Host::Managed) using RABL: Cannot find rabl template 'api/v2/hosts/show' within registered (["app/views"]) view paths!
2015-12-03 00:28:20 [D] /opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl/partials.rb:47:in `block in fetch_source'
/opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl.rb:58:in `source_cache'
/opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl/partials.rb:34:in `fetch_source'
/opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl/renderer.rb:62:in `process_source'
/opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl/renderer.rb:36:in `initialize'
/opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl.rb:77:in `new'
/opt/rh/ruby193/root/usr/share/gems/gems/rabl-0.9.0/lib/rabl.rb:77:in `render'
/opt/rh/ruby193/root/usr/share/gems/gems/foreman_hooks-0.3.7/lib/foreman_hooks/util.rb:18:in `render_hook_json'
/opt/rh/ruby193/root/usr/share/gems/gems/foreman_hooks-0.3.7/lib/foreman_hooks/util.rb:29:in `block in exec_hook'
...

Not able to run hooks for "host/discovered" events from forman_discovery plugin

As per this link, this is supported:

https://www.omniref.com/ruby/gems/foreman_hooks/0.1.0

But I tried putting a script in the directory ~foreman/config/hooks/host/discovered/{create, destroy}.

But I get this error in the logs:
2015-07-16 09:39:04 [W] Host::Discovered doesn't support orchestration, can't run orchestration hooks: use Rails events instead

I checked the Host::Discovered class and it doesn't include Orchestration module. Is this a limitation of the class? If so, the above link should be corrected.
Is there any way to use the callback hooks or any other mechanism to achieve the same?

Thanks,
-PP

hooks not removed if removed from directory

If I remove a hook from the hooks directory and restore foreman, it is not found, which is great. However foreman keeps complaining in the UI that the file can not be found and fails the build process.

How do I get rid of a deprecated hook?

foreman-rake hooks:events[nic/bmc] ends with error bug

Hello,

The foreman-rake hooks:objects gives nic/bmc object.

However listing of foreman-rake hooks:events[nic/bmc]

fails with error:

** Invoke hooks:events (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute hooks:events
rake aborted!
LoadError: Unable to autoload constant Nic::Bmc, expected /usr/share/foreman/app/models/nic/bmc.rb to define it
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:495:in `load_missing_constant'
/usr/share/foreman/vendor/ruby/2.1.0/gems/foreman_hooks-0.3.14/lib/foreman_hooks/as_dependencies_hook.rb:4:in `load_missing_constant'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:184:in `const_missing'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/inflector/methods.rb:263:in `const_get'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/inflector/methods.rb:263:in `block in constantize'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/inflector/methods.rb:259:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/inflector/methods.rb:259:in `inject'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/inflector/methods.rb:259:in `constantize'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activesupport-4.2.9/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/usr/share/foreman/vendor/ruby/2.1.0/gems/foreman_hooks-0.3.14/lib/tasks/hooks.rake:11:in `block (2 levels) in <top (required)>'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `call'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `block in execute'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `execute'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:195:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:188:in `invoke_with_call_chain'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/task.rb:181:in `invoke'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:160:in `invoke_task'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:116:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:116:in `block in top_level'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:125:in `run_with_threads'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:110:in `top_level'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:83:in `block in run'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:186:in `standard_exception_handling'
/usr/share/foreman/vendor/ruby/2.1.0/gems/rake-12.3.0/lib/rake/application.rb:80:in `run'
/usr/bin/rake:27:in `<main>'
Tasks: TOP => hooks:events

As domcleal says:
Arguably this is a bug in Foreman, it should define mixed-case Ec2 rather than all-caps EC2 to match the Rails autoloader expectations, but foreman_hooks could work around it.

Anybody knows if a workaround is possible or in progress ?

I was looking on foreman issue without finding is this problem was open. Is this normal?

Foreman reports are broken since i've installed foreman_hooks

Hi,

Nightly cron doesn't send reports anymore since I've installed foreman_hooks.
Here is the output with --trace :

$ export RAILS_ENV=production; export FOREMAN_HOME=/usr/share/foreman; cd ${FOREMAN_HOME} && bundle exec /usr/bin/rake --trace reports:summarize
(in /usr/share/foreman)
rake aborted!
uninitialized constant Rake::DSL
/usr/share/foreman/vendor/ruby/1.8/gems/foreman_hooks-0.3.2/lib/foreman_hooks.rb:75:in load_missing_constant' /usr/share/foreman/Rakefile:3 /usr/lib/ruby/1.8/rake.rb:2383:inload'
/usr/lib/ruby/1.8/rake.rb:2383:in raw_load_rakefile' /usr/lib/ruby/1.8/rake.rb:2017:inload_rakefile'
/usr/lib/ruby/1.8/rake.rb:2068:in standard_exception_handling' /usr/lib/ruby/1.8/rake.rb:2016:inload_rakefile'
/usr/lib/ruby/1.8/rake.rb:2000:in run' /usr/lib/ruby/1.8/rake.rb:2068:instandard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:1998:in `run'
/usr/bin/rake:28

Thanks for your help.

Remove of hook result in 500 Error

I created an testscript as /usr/share/foreman/config/hooks/host/managed/after_initialize/50_testing.sh

After removing this script i was unable to view my hosts anymore. The productionlog shows:

2016-09-09 14:10:14 [app] [W] Action failed
 | Errno::ENOENT: No such file or directory - /usr/share/foreman/config/hooks/host/managed/after_initialize/50_testing.sh
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:193:in `spawn'
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:193:in `popen_run'
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:188:in `popen2e'
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:335:in `capture2e'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.11/lib/foreman_hooks/util.rb:46:in `exec_hook_int'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.11/lib/foreman_hooks/util.rb:34:in `block in exec_hook'
 | /opt/rh/rh-ruby22/root/usr/share/gems/gems/bundler-1.7.8/lib/bundler.rb:236:in `block in with_clean_env'
 | /opt/rh/rh-ruby22/root/usr/share/gems/gems/bundler-1.7.8/lib/bundler.rb:223:in `with_original_env'
 | /opt/rh/rh-ruby22/root/usr/share/gems/gems/bundler-1.7.8/lib/bundler.rb:229:in `with_clean_env'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.11/lib/foreman_hooks/util.rb:34:in `exec_hook'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.11/lib/foreman_hooks/callback_hooks.rb:19:in `block (4 levels) in <module:CallbackHooks>'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.11/lib/foreman_hooks/callback_hooks.rb:19:in `each'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.11/lib/foreman_hooks/callback_hooks.rb:19:in `block (3 levels) in <module:CallbackHooks>'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:432:in `block in make_lambda'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:239:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:239:in `block in halting'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:506:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:506:in `block in call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:506:in `each'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:506:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:778:in `_run_initialize_callbacks'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/core.rb:312:in `init_with'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/persistence.rb:69:in `instantiate'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:50:in `block (2 levels) in find_by_sql'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/result.rb:51:in `block in each'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/result.rb:51:in `each'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/result.rb:51:in `each'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:50:in `map'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:50:in `block in find_by_sql'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:49:in `find_by_sql'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:639:in `exec_queries'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:515:in `load'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:243:in `to_a'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/will_paginate-3.1.0/lib/will_paginate/active_record.rb:126:in `block in to_a'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/will_paginate-3.1.0/lib/will_paginate/collection.rb:96:in `create'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/will_paginate-3.1.0/lib/will_paginate/active_record.rb:125:in `to_a'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/relation/delegation.rb:46:in `map'
 | /usr/share/foreman/app/controllers/hosts_controller.rb:49:in `block (2 levels) in index'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/mime_responds.rb:217:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/mime_responds.rb:217:in `respond_to'
 | /usr/share/foreman/app/controllers/hosts_controller.rb:45:in `index'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/abstract_controller/base.rb:198:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:117:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:117:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `block (2 levels) in halting'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/rails-observers-0.1.2/lib/rails/observers/action_controller/caching/sweeping.rb:73:in `around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:455:in `public_send'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:455:in `block in make_lambda'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `block in halting'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `block in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `block (2 levels) in halting'
 | /usr/share/foreman/app/controllers/concerns/application_shared.rb:13:in `set_timezone'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:432:in `block in make_lambda'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `block in halting'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `block in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `block (2 levels) in halting'
 | /usr/share/foreman/app/models/concerns/foreman/thread_session.rb:32:in `clear_thread'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:432:in `block in make_lambda'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `block in halting'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `block in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:313:in `block (2 levels) in halting'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/rails-observers-0.1.2/lib/rails/observers/action_controller/caching/sweeping.rb:73:in `around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:455:in `public_send'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:455:in `block in make_lambda'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:312:in `block in halting'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:497:in `block in around'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/notifications.rb:164:in `block in instrument'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/notifications.rb:164:in `instrument'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/abstract_controller/base.rb:137:in `process'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionview-4.2.5.1/lib/action_view/rendering.rb:30:in `process'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal.rb:196:in `dispatch'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_controller/metal.rb:237:in `block in action'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/route_set.rb:74:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/route_set.rb:43:in `serve'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/journey/router.rb:43:in `block in serve'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/journey/router.rb:30:in `each'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/journey/router.rb:30:in `serve'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/route_set.rb:815:in `call'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/apipie-rails-0.3.6/lib/apipie/static_dispatcher.rb:65:in `call'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/apipie-rails-0.3.6/lib/apipie/extractor/recorder.rb:132:in `call'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/apipie-rails-0.3.6/lib/apipie/middleware/checksum_in_headers.rb:27:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/etag.rb:24:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/conditionalget.rb:25:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/head.rb:13:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 | /usr/share/foreman/lib/middleware/catch_json_parse_errors.rb:9:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/flash.rb:260:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/session/abstract/id.rb:225:in `context'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/session/abstract/id.rb:220:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/cookies.rb:560:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/query_cache.rb:36:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/railties-4.2.5.1/lib/rails/rack/logger.rb:38:in `call_app'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/railties-4.2.5.1/lib/rails/rack/logger.rb:22:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/methodoverride.rb:22:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/runtime.rb:18:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/activesupport-4.2.5.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/static.rb:116:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/sendfile.rb:113:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/railties-4.2.5.1/lib/rails/engine.rb:518:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/railties-4.2.5.1/lib/rails/application.rb:165:in `call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/railties-4.2.5.1/lib/rails/railtie.rb:194:in `public_send'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/railties-4.2.5.1/lib/rails/railtie.rb:194:in `method_missing'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/urlmap.rb:66:in `block in call'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/urlmap.rb:50:in `each'
 | /opt/rh/sclo-ror42/root/usr/share/gems/gems/rack-1.6.2/lib/rack/urlmap.rb:50:in `call'
 | /usr/share/passenger/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
 | /usr/share/passenger/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
 | /usr/share/passenger/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
 | /usr/share/passenger/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/logging-1.8.2/lib/logging/diagnostic_context.rb:323:in `call'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/logging-1.8.2/lib/logging/diagnostic_context.rb:323:in `block in create_with_logging_context'
2016-09-09 14:10:14 [app] [I]   Rendered common/500.html.erb within layouts/application (4.7ms)
2016-09-09 14:10:14 [app] [I]   Rendered layouts/_application_content.html.erb (0.3ms)
2016-09-09 14:10:14 [app] [I]   Rendered layouts/base.html.erb (1.1ms)
2016-09-09 14:10:14 [app] [I] Completed 500 Internal Server Error in 396ms (Views: 7.3ms | ActiveRecord: 167.0ms)

Cannot use after_build hook

This is what happens when I restart foreman after I added my script to /usr/share/foreman/config/hooks/managed/after_build:

uninitialized constant Managed (NameError)
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks.rb:83:in `load_missing_constant_with_hooks'
  /usr/share/foreman/config/initializers/deprecations.rb:3:in `const_missing'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:230:in `block in constantize'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:229:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:229:in `constantize'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks/engine.rb:6:in `block (2 levels) in <class:Engine>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks/engine.rb:6:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/foreman_hooks-0.3.8/lib/foreman_hooks/engine.rb:6:in `block in <class:Engine>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/callbacks.rb:462:in `_run__2952295411430003890__prepare__2925492264482690565__callbacks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/callbacks.rb:405:in `__run_callback'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/activesupport-3.2.21/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/actionpack-3.2.21/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/actionpack-3.2.21/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:30:in `instance_exec'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:30:in `run'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:55:in `block in run_initializers'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:54:in `each'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/initializable.rb:54:in `run_initializers'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/application.rb:136:in `initialize!'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/railties-3.2.21/lib/rails/railtie/configurable.rb:30:in `method_missing'
  /usr/share/foreman/config/environment.rb:5:in `<top (required)>'
  config.ru:3:in `require'
  config.ru:3:in `block in <main>'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/rack-1.4.7/lib/rack/builder.rb:51:in `instance_eval'
  /usr/share/foreman/vendor/ruby/1.9.1/gems/rack-1.4.7/lib/rack/builder.rb:51:in `initialize'
  config.ru:1:in `new'
  config.ru:1:in `<main>'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'

Empty 'parameters' array in json output

Related to #43, which partly solved the problem but is now closed.

The json output passed to my hook scripts are missing data - the 'parameters' array is empty. If I apply the workaround mentioned in the issue #43 the parameters array is populated as expected though.

Hook scripts found and registered but never called

Hi,

I'm currently using the development branch of Foreman.
I installed the foreman_hooks plugin, and it seems to be loaded fine. I had errors at first because I defined my hook scripts in config/hooks/host instead of config/hooks/host/managed but now I can see in the log that the plugin finds my scripts (and tells they have been registered).

I then provisioned several Rackspace VMs, everything went fine but none of my hook script were called. I carefully checked the scripts permissions.
For now I have 3 scripts, inside hosts/managed/create/, host/managed/destroy/ and host/managed/after_build/.

Why are these script not getting called? There is nothing in the log. Could it be because I'm using a development version of Foreman (2013/07/14)? Or because I'm provisioning Rackspace/cloud VMs?

Thanks for your help.

Return hook output on error

This is probably an enhancement.
If a hook fails, it would be helpful to return the error output of the hook to the user.
This is mostly useful when users enter an incorrect value, which gets validated by the hook.

With #11 in place, on failure only the path is visible in the bubble.
As far as I see, the output is already captured, but it is not returned to the exception.

Regards,
Stefan

Documentation error

The frontpage says "cd ~foreman && sudo -u foreman bundle install" but your blog says "bundle update" - I suspect the former is causing people on foreman1.1 (rpm-style) to accidentally upgrade rails to 3.2 (not actually tested that though).

JSON passed to hooks doesn't mention multiple NICs

I've noticed that the JSON data passed into the script doesn't mention more than one NIC.

It would be useful for our server build/deploy process if I could grab extra MAC addresses on host creation (we'd use that for things like DNS entries for the extra NICs, linking different servers together in our applications, etc etc)

Alternatively, should the "interfaces" section be populated? It's currently empty in the JSON I'm getting (from 1.7.3)

Missing hooks are failures

If a hook is deleted, tasks will fail. Additionally, it is unclear when/where hooks get installed or uninstalled.

Hook: 50-ad_realm task failed with the following error: No such file or directory - /usr/share/foreman/config/hooks/host/managed/create/50-ad_realm
 | Errno::ENOENT: No such file or directory - /usr/share/foreman/config/hooks/host/managed/create/50-ad_realm
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:193:in `spawn'
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:193:in `popen_run'
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:188:in `popen2e'
 | /opt/rh/rh-ruby22/root/usr/share/ruby/open3.rb:335:in `capture2e'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.13/lib/foreman_hooks/util.rb:46:in `exec_hook_int'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.13/lib/foreman_hooks/util.rb:34:in `block in exec_hook'
 | /opt/rh/rh-ruby22/root/usr/share/gems/gems/bundler-1.7.8/lib/bundler.rb:236:in `block in with_clean_env'
 | /opt/rh/rh-ruby22/root/usr/share/gems/gems/bundler-1.7.8/lib/bundler.rb:223:in `with_original_env'
 | /opt/rh/rh-ruby22/root/usr/share/gems/gems/bundler-1.7.8/lib/bundler.rb:229:in `with_clean_env'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.13/lib/foreman_hooks/util.rb:34:in `exec_hook'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/foreman_hooks-0.3.13/lib/foreman_hooks/orchestration_hook.rb:57:in `hook_execute_set'
 | /usr/share/foreman/app/models/concerns/orchestration.rb:195:in `execute'
 | /usr/share/foreman/app/models/concerns/orchestration.rb:135:in `block in process'
 | /usr/share/foreman/app/models/concerns/orchestration.rb:127:in `each'
 | /usr/share/foreman/app/models/concerns/orchestration.rb:127:in `process'
 | /usr/share/foreman/app/models/concerns/orchestration.rb:35:in `around_save_orchestration'

question about events and hooks

First of all this plugin rocks!

I created a hook to kick off a puppet run via shelling out to mcollective whenever an update occurs. But now I want filter out some of the updates so that mco is not called every single time. For example:.

run mco command when a host group is updated.
do not run mco command when other details are updated.

Since I am not able to compare any state information with the data coming in is there a way that I can see which call was made to trigger the hook? Specifically the url in the PUT statement

For example If my hook knew that update was called via /api/hosts/72/parameters/9 than I could exclude it from running the mco command.

Any way to pass the url as a new argument?
PUT "/api/hosts/72/parameters/9"

Can not get json data.

my foreman version = 1.4.2

and i already tested foreman_hooks tag = 0.3.4 & 0.3.7

and also reference hook_functions.sh & jgrep ruby gem already installed.

but I can not get json data, here is my hook script.

#!/bin/bash

HOOK_EVENT=$1
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
}

echo "current path = $(dirname $0)" >> /tmp/hook.log

# event name (create, before_destroy etc.)
# orchestration hooks must obey this to support rollbacks (create/update/destroy)
event=${HOOK_EVENT}

# to_s representation of the object, e.g. host's fqdn
object=${HOOK_OBJECT}

# Example of using hook_data to query the JSON representation of the object
# passed by foreman_hooks.  `cat $HOOK_OBJECT_FILE` to see the contents.
hostname=$(hook_data host.name)

echo "$(date): received ${event} on ${object}" >> /tmp/hook.log
echo "hostname = ${hostname}" >> /tmp/hook.log

# exit code is important on orchestration tasks
exit 0

I can correct get $event & $object variable in output log.
but my $hostname is always empty.

is it a bug? or just my mismatch configuration?

hooks on bulk actions

Hello,
I didn't find any mention on this, so I am not sure if this is a bug or a feature request, but hooks are not run on bulk actions.
A way to reproduce this is by changing the environment on a host:

  • navigate to the view Hosts -> All hosts
  • select a host
  • Select Action -> Change Environment
  • select another environment, then click 'Submit'

foreman version: 1.15.6
foreman-hooks: 0.3.14

config_report hook generates log warning

I created a hook script for the 'config_report' object:
config/hooks/config_report/after_create/10-test.sh

The hook script works (check report status and sends update to our monitoring system via REST api), but in the foreman log (production.log) I get a warning for every host:

2018-06-20 07:58:49 a742dbf8 [app] [W] Unable to render lxserv1330.example.com / 2018-06-20 07:58:21 UTC (ConfigReport) using RABL: Cannot find rabl template 'api/v2/configreports/show' within registered (["/usr/share/foreman/app/views", "/opt/theforeman/tfm/root/usr/share/gems/gems/foreman_snapshot_management-1.1.0/app/views", "/opt/theforeman/tfm/root/usr/share/gems/gems/apipie-rails-0.5.4/app/views", "/usr/share/foreman/app/views"]) view paths!

I also noticed that the second argument to the hook script ($2), which should be hostname, in fact is:
lxserv1330.example.com / 2018-06-20 07:58:21 UTC
(for the host in the log output above)

which seems wrong to me. As I said, the script works, I get the config report json data correctly, but the warning messages in the log is worrying.

Unable to run hook on bare metal build or simple rebuild

I am using Foreman 1.14.3, foreman_hooks plugin 0.3.14. I have a generic script equivalent to the provided example logs.sh. When placed under host/managed/after_create/, every time a KVM (Libvirt) guest is created, it gets executed. But I cannot get it to execute in the following two scenarios:

  • Bare metal build
  • KVM guest rebuild

I have tried host/managed/after_build/, host/base/after_create/, etc.

Hook for instances power-on/power-off

It seems that i can't find any hook that allows me to run something after a machine (in my case either GCE/EC2 instance) power state changes from ON to OFF and vise versa

Plus it seems that i can reach the hooks under foreman/model/ec2

foreman-rake hooks:events[foreman/model/ec2] ends with error

The foreman-rake hooks:objects gives foreman/model/ec2 object.

However listing of
foreman-rake hooks:events[foreman/model/ec2]
fails with error:

rake aborted!
LoadError: Unable to autoload constant Foreman::Model::Ec2, expected /usr/share/foreman/app/models/compute_resources/foreman/model/ec2.rb to define it
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:495:in `load_missing_constant'
/usr/share/foreman/vendor/ruby/2.3.0/gems/foreman_hooks-0.3.14/lib/foreman_hooks/as_dependencies_hook.rb:4:in `load_missing_constant'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:184:in `const_missing'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/inflector/methods.rb:263:in `const_get'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/inflector/methods.rb:263:in `block in constantize'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/inflector/methods.rb:259:in `each'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/inflector/methods.rb:259:in `inject'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/inflector/methods.rb:259:in `constantize'
/usr/share/foreman/vendor/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/usr/share/foreman/vendor/ruby/2.3.0/gems/foreman_hooks-0.3.14/lib/tasks/hooks.rake:11:in `block (2 levels) in <top (required)>'

Missing host parameters in foreman_hooks json

Copied from http://projects.theforeman.org/issues/19565

Started playing with foreman_hooks and noticed that the json data passed to my hook script is missing host parameters when I create a new host. I started digging through commits and found that if i revert this commit:

theforeman/foreman@94265cf#diff-5ef0189b17a78606adef8f22e909697e

in the app/views/api/v2/hosts/show.json.rabl file it works again.

So, this code (which nowadays is moved to main.json.rabl):

node do |host|
  { :parameters => partial("api/v2/parameters/base", :object => host.host_parameters.authorized) }
end

does not work for me but this do:

child :host_parameters => :parameters do
  extends "api/v2/parameters/base" 
end

I'm running this as administrator in my Foreman installation, am I missing something or is this a bug?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.