theforeman / foreman_hooks

Run custom hook scripts on Foreman events
http://m0dlx.com/blog/Extending_Foreman_quickly_with_hook_scripts.html
GNU General Public License v3.0
56 stars 50 forks source link

Hook data json structure has changed in latest release #46

Open alexjfisher opened 7 years ago

alexjfisher commented 7 years ago

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,
alexjfisher commented 7 years ago

Also see https://groups.google.com/forum/#!topic/foreman-users/5mDxvp6V_yw

alexjfisher commented 7 years ago

@domcleal Any thoughts? I'm guess it's something to do with https://github.com/theforeman/foreman_hooks/commit/ed2c50ce920def5ed9c8c5f528c68d931d736137

dLobatog commented 6 years ago

Seems OK now, apparently the API response here was being rendered with 'host' : { host contents } instead of just { host contents }

2018-02-03T10:50:23 12c0d92b [app] [D] Observed after_create hook on joel-salem.lobatolan.home
2018-02-03T10:50:23 12c0d92b [app] [D] Running 1 hooks for Host::Managed#after_create
2018-02-03T10:50:23 12c0d92b [app] [D] Running hook: /home/vagrant/foreman/config/hooks/host/managed/after_create/99_print_host.sh after_create joel-salem.lobatolan.home
2018-02-03T10:50:23 b92fc47f [app] [I] Started GET "/tasks/2215d33b-85af-45f4-8a57-2fe7286465a7" for ::1 at 2018-02-03 10:50:23 +0000
2018-02-03T10:50:23 b92fc47f [app] [I] Processing by TasksController#show as */*
2018-02-03T10:50:23 b92fc47f [app] [I]   Parameters: {"id"=>"2215d33b-85af-45f4-8a57-2fe7286465a7"}
2018-02-03T10:50:23 b92fc47f [app] [I] Current user: dlobatog (administrator)
2018-02-03T10:50:23 b92fc47f [app] [D] Setting current user thread-local variable to dlobatog
2018-02-03T10:50:23 b92fc47f [app] [I]   Rendered tasks/_list.html.erb (0.8ms)
2018-02-03T10:50:23 b92fc47f [app] [I] Completed 200 OK in 24ms (Views: 3.4ms | ActiveRecord: 5.6ms)
 |
 |
::1 - - [03/Feb/2018:10:50:23 UTC] "GET /tasks/2215d33b-85af-45f4-8a57-2fe7286465a7 HTTP/1.1" 200 1173
https://centos7-devel.lobatolan.home/hosts/new -> /tasks/2215d33b-85af-45f4-8a57-2fe7286465a7
2018-02-03T10:50:23 12c0d92b [app] [D] Hook output: /home/vagrant/foreman/config/hooks/host/managed/after_create/99_print_host.sh: 行 3: /home/vagrant/foreman/config/hooks/host/managed/after_create/hook_functions.sh: そのようなファイル やディレクトリはありません
 | {"host":{"ip":"192.168.121.78","ip6":"","environment_id":null,"environment_name":null,"last_report":null,"mac":"52:54:00:5d:a0:20","realm_id":null,"realm_name":null,"sp_mac":null,"sp_ip":null,"sp_name":null,"domain_id":2,"domain_name":"lobatolan.home","architecture_id":1,"architecture_name":"x86_64","operatingsystem_id":10,"operatingsystem_name":"Container Linux 1632.2.1","subnet_id":1,"subnet_name":"vagrant-libvirt","subnet6_id":null,"subnet6_name":null,"sp_subnet_id":null,"ptable_id":85,"ptable_name":"CoreOS default fake","medium_id":8,"medium_name":"CoreOS mirror","pxe_loader":"PXELinux BIOS","build":true,"comment":"","disk":"","installed_at":null,"model_id":null,"hostgroup_id":2,"owner_id":5,"owner_name":"dlobatog","owner_type":"User","enabled":true,"managed":true,"use_image":null,"image_file":"","uuid":"3145aae6-fa3f-486e-97fc-7938ce9fd48e","compute_resource_id":3,"compute_resource_name":"Lenovo Libvirt","compute_profile_id":4,"compute_profile_name":"CoreOS cluster","capabilities":["build","image","new_volume"],"provision_method":"build","certname":"joel-salem.lobatolan.home","image_id":null,"image_name":null,"created_at":"2018-02-03 11:50:23 +0100","updated_at":"2018-02-03 11:50:23 +0100","last_compile":null,"global_status":0,"global_status_label":"OK","organization_id":14,"organization_name":"candlepin-ok","puppet_status":0,"model_name":null,"build_status":1,"build_status_label":"Pending installation","name":"joel-salem.lobatolan.home","id":88,"puppet_proxy_id":null,"puppet_proxy_name":null,"puppet_ca_proxy_id":null,"puppet_ca_proxy_name":null,"puppet_proxy":null,"puppet_ca_proxy":null,"token":"0e6d15cb-5671-4469-9b83-32d8725dc4ad","hostgroup_name":"demo_cluster","hostgroup_title":"demo_cluster","parameters":[],"all_parameters":[{"priority":60,"created_at":"2018-02-03 01:38:38 +0100","updated_at":"2018-02-03 01:38:38 +0100","id":6,"name":"ssh_authorized_keys","value":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD0wcv4sXAcBcseIP44R1+0LyQyeI2luPs50MU2MbOvXOCWw4pT/Ridv0FkRg9cGGnX4RcP9NK7LoojjsgjG0WhRUPZQRfmoQpRYnQHLmyYhpCB/DS3fT943n/rNWG4OcSt50ozfzNPr5eWV5j8GFj5JqjRUQOn6+RkQzxk3RYUE+PfQSP1muxhXkOX5U512LI+Ney/o/Y6g37IRlSm/tTeuD8o3qKcKwVlNcTVvm5FLTzz2dkiHR2CGNbEKbz8kJxA8TBWBhKtEOViY9/JQKrfNbDNSWid+IPqMSabx16l9W78hbP7ZNyofLN3LWwImC5S+XoqjOs2TNpc0mifydtB elobatocs@gmail.com"},{"priority":60,"created_at":"2018-02-03 01:38:38 +0100","updated_at":"2018-02-03 01:38:38 +0100","id":5,"name":"install-disk","value":"/dev/vda"},{"priority":60,"created_at":"2018-02-03 01:38:38 +0100","updated_at":"2018-02-03 01:38:38 +0100","id":4,"name":"etcd_discovery_url","value":"https://discovery.etcd.io/454432db616639bdf60ed458975650f5"},{"priority":0,"created_at":"2018-01-10 03:03:37 +0100","updated_at":"2018-01-17 11:15:47 +0100","id":2,"name":"remote_execution_ssh_keys","value":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD0wcv4sXAcBcseIP44R1+0LyQyeI2luPs50MU2MbOvXOCWw4pT/Ridv0FkRg9cGGnX4RcP9NK7LoojjsgjG0WhRUPZQRfmoQpRYnQHLmyYhpCB/DS3fT943n/rNWG4OcSt50ozfzNPr5eWV5j8GFj5JqjRUQOn6+RkQzxk3RYUE+PfQSP1muxhXkOX5U512LI+Ney/o/Y6g37IRlSm/tTeuD8o3qKcKwVlNcTVvm5FLTzz2dkiHR2CGNbEKbz8kJxA8TBWBhKtEOViY9/JQKrfNbDNSWid+IPqMSabx16l9W78hbP7ZNyofLN3LWwImC5S+XoqjOs2TNpc0mifydtB elobatocs@gmail.com"},{"priority":0,"created_at":"2018-01-10 03:06:16 +0100","updated_at":"2018-01-10 03:06:16 +0100","id":3,"name":"kt_activation_keys","value":"forklift"}],"interfaces":[{"id":null,"name":"joel-salem.lobatolan.home","ip":"192.168.121.78","ip6":"","mac":"52:54:00:5d:a0:20","fqdn":"joel-salem.lobatolan.home","identifier":"","primary":true,"provision":true,"type":"interface"}],"puppetclasses":[],"config_groups":[],"all_puppetclasses":[],"permissions":{"view_hosts":true,"create_hosts":true,"edit_hosts":true,"destroy_hosts":true,"build_hosts":true,"power_hosts":true,"console_hosts":true,"ipmi_boot_hosts":true,"puppetrun_hosts":true,"view_discovered_hosts":true,"submit_discovered_hosts":true,"auto_provision_discovered_hosts":true,"provision_discovered_hosts":true,"edit_discovered_hosts":true,"destroy_discovered_hosts":true,"play_roles_on_host":true}}}
dLobatog commented 6 years ago

BTW the hook I had was config/hooks/host/managed/after_create/99_print_host.sh

#!/bin/bash

. $(dirname $0)/hook_functions.sh

cat $HOOK_OBJECT_FILE
alexjfisher commented 6 years ago

@dLobatog Thanks for spending the time looking into this. I've just upgraded to 1.16.0 and this is still broken for me.

cat /usr/share/foreman/config/hooks/host/managed/before_provision/00_unregister_host_from_pulp.sh
#!/bin/bash

. /usr/share/foreman/config/hooks/lib/hook_functions.sh

consumer_id=$(hook_data host.name)

url="https://pulp.example.com/pulp/api/v2/consumers/${consumer_id}/"
/bin/curl -u SVC_PULP:REDACTED -X "DELETE" $url

/bin/cp $HOOK_OBJECT_FILE /usr/share/foreman/log/hook-debug-`date +%s`

rebuild my host...

cat /var/log/foreman/hook-debug-1519291712  | jq
{
  "host": {
    "host": {
      "ip": "192.168...

IIRC, you were using a development version of foreman, so hopefully I'll find this is fixed when 1.17 comes out.

alexjfisher commented 6 years ago

FWIW, I also tried with an after_create hook.

amard33p commented 6 years ago

Can confirm there is still an extra host:

{"host": {"host": {"all_parameters": [],
                   "all_puppetclasses": [],
                   "architecture_id": 1,
                   "architecture_name": "x86_64",
                   "build": "True",
                   ...

Which is why the foreman-hook python sample script is not working: /usr/share/foreman/vendor/ruby/2.3.0/gems/foreman_hooks-0.3.14/examples/python/host/managed/create/10-logger.py Instead of mac_address = HOOK_JSON.get('host').get('mac', None) now it should be mac_address = HOOK_JSON.get('host').get('host').get('mac', None)

Foreman version = 1.15.6 (stable) Foreman-hooks version = 0.3.14

aaroncaskeydemaret commented 6 years ago

We finally upgraded to 1.17 from 1.13 and ran into this issue. We can fix our hooks, but is it likely that things will revert later?