How to create a FOG Event?
-
I am trying to make an image complete event hook that is executed on the FOG server side. I copied the Slack ImageComplete event to /var/www/fog/lib/events/, and modified it to do what I want.
Unfortunately, it doesnt appear to be executing. Class is below.
What am I missing or doing wrong? Currently I’m just trying to log the data to a file to prove that I can make this work. I also added HOST_LIST_EVENT to try to get it to trigger elsewhere so I could see if it would work there, but nothing.
class ImageComplete_DS_FW extends Event { /** * The name of this event * * @var string */ public $name = 'ImageComplete_DS_FWEvent'; /** * The description of this event * * @var string */ public $description = 'Triggers when a host finishes imaging'; /** * The event is active * * @var bool */ public $active = true; /** * Initialize object. * * @return void */ public function __construct() { parent::__construct(); self::$EventManager->register( 'HOST_LIST_EVENT', $this )->register( 'HOST_IMAGE_COMPLETE', $this )->register( 'HOST_IMAGEUP_COMPLETE', $this ); } /** * Perform action * * @param string $event the event to enact * @param mixed $data the data * * @return void */ public function onEvent($event, $data) { $d = var_export($data); file_put_contents(self::$logpath . "ds_fw.log",$d); } }
-
@CWDS What’s your file named? AFAIK filename and class name must match (wouldn’t use
_
)… -
@Sebastian-Roth The file+path is ‘/var/www/html/fog/lib/events/imagecomplete_ds_fw.event.php’
-
@CWDS Usually when I don’t get the expected output I start messing around with the code just to make sure it even runs at all. So you might want to change
file_put_contents
tofile_put_contents_does_not_exist
and see if you get the expected errors in apache/php-fpm logs (see my signature).Beside that I often see that the log file needs to exist for this to work as apache can log to a file but might not have rights to create that file:
touch ....../ds_fw.log chown www-data:www-data ......./ds_fw.log
The user and group name depends on the Linux OS you have. This is specific for Debian.
-
@Sebastian-Roth The extra _ did appear to be preventing it from loading, however, the event does not appear to be firing? I changed the file_put_contents to something that I knew would fail; and it threw an error in /var/log/httpd/ssl_error_log. However, when I fixed it back to file_put_contents (even tried other functions too, echo, print, var_dump), the file loads, but doesnt seem to execute the onEvent call. I even set php error_reporting to E_ALL, and all I get is an unrelated notice. I even changed from file_put_contents to fopen/fwrite/fclose; and attached ‘or die(“Some message”)’ to them, no errors, no messages, no death, no writing to the file either.
-
@CWDS Where/when do you expect the event to trigger? I just tested your code and it wrote to my log file just fine right at the end of an upload task. But be aware the events
HOST_LIST_EVENT
andHOST_IMAGEUP_COMPLETE
don’t exist (anymore). So the only one you can see triggered using your code is theHOST_IMAGE_COMPLETE
event fired when a task is finished (deploy, capture, …).If you want a full list of events defined in your FOG version use the commands I posted in another thread in our forums.
-
@Sebastian-Roth Well that would certainly explain why I’m not seeing it then! I was testing using Hosts -> List. I added HOST_LIST_EVENT as a test, so that I wasnt having to do imaging all the time to test it.
Can you/the rest of the mods/team please update the wiki?! Forum posts are great, but having to search and hope the information is correct kind of sucks. The wiki should be the central knowledge repo, not the forum. I mean, at one point while working with the API, I had to dive into the code in order to find the requirements each endpoint needed. Using an API shouldnt require code diving!
I did find this list: https://forums.fogproject.org/assets/uploads/files/1545241373509-hooks_and_tie-ins.txt but it references 1.6, not 1.5.5
-
@Sebastian-Roth Actually, the link I mentioned, doesnt have HOST_IMAGE_COMPLETE listed there. am I looking at the wrong type of things or is this event name changing soon?
-
@CWDS said in How to create a FOG Event?:
Can you/the rest of the mods/team please update the wiki?! Forum posts are great, but having to search and hope the information is correct kind of sucks. The wiki should be the central knowledge repo, not the forum.
I can understand your frustration but there is just not enough people helping us to improve FOG (including the documentation)! Please get involved and make things better than they are right now.
-
@CWDS Hooks and tie in’s are only the “hooks”. It does not go into Events.
There’s so little number of events currently (even in 1.6). Compared to hooks, events are simply triggers.
-
@Tom-Elliott Thanks, understood.
I was able to get my log file test writing out the $data info. I think I’m doing something else wrong now, trying to get the host object based on that data. Ideas?
public function onEvent($event, $data) { $hostData["name"] = $data['HostName']; $iData = var_export($hostData,true); $hostObj = new Host($hostData); $macList = $hostObj->getMyMacs(); $oData = var_export($macList,true); file_put_contents("/var/www/fog/ds_fw.log",$iData . "\n" . $oData); } }
Based on my reading of the code; I can call ‘new Host()’ with an array that gets passed for searching for a host, and in return, I should get a Host object, for that host. I’ve tried every variation in the $hostData[] entry; ‘Name’, ‘name’, ‘hostName’, ‘HostName’.
Is there somewhere I can read about how these classes are supposed to act together, or how I’m supposed to actually initiate/call them?
(Edit:) - $macList returns an empty array. which leads me to believe I’m calling/loading Host() wrong (thus, my questions)
-
Update
Still unable to get the MACs from Host(), but I think I’m getting closer. I just dont know what I’m supposed to do with the output of a ->get() call.
public function onEvent($event, $data) { $hostData["name"] = $data['HostName']; $iData = var_export($hostData,true); $hostObj = new Host($hostData); $hData = var_export($hostObj,true); $primaryMac = $hostObj->get('primac')->get('mac'); $macList = array(); foreach ((array)$hostObj->get('additionalMACs') as $ind => &$MAC) { $macList[] = $MAC; } $oData1 = var_export($primaryMac,true); $oData2 = var_export($macList,true); file_put_contents("/var/www/fog/ds_fw.log",$iData . "\n" . $hData . "\n" . $oData1 . "\n" . $oData2); } }
$primaryMac results in NULL, and $macList results in an empty array. I picked/adjusted the $macList loop from one of the pages code, so that ‘should’ theoretically work, as long as the get()'s actually return data.