Skip to content

System Events

Tenantos provides public events that allow developers to execute their own code in response to specific events.

The events are not encrypted, and the data passed to the listeners can be viewed in the directory /var/www/html/app/Events/publicEvents

To listen to an event, an event listener must be created.

Application Events

Application Events are events that are triggered by Tenantos on specific actions. Currently, the following events are available:

Event Description
beforeServerIpAssignments Triggered before the assignment of server IPs starts.
afterEachServerIpAssignment Triggered after each individual server IP assignment.
afterServerIpAssignments Triggered once all server IP assignments are complete.
beforeServerIpRemovals Triggered before the removal of server IPs starts.
afterEachServerIpRemoval Triggered after each individual server IP removal.
afterServerIpRemovals Triggered once all server IP removals have been completed.

The before events are only triggered if the validation succeeds.

How to create a new event listener

Important Note

  • After creating or deleting an event listener, it is necessary to execute the command app event:cache via SSH.
  • When updating Tenantos, custom events are kept. It is not required to re-add the files manually.

To create a new event listener, you must create a new file in the directory /var/www/html/app/Custom/EventListeners ending with .php. As example: /var/www/html/app/Custom/EventListeners/afterIpAssignmentListener.php

The class name must be the same as the file name. If the file is named "afterIpAssignmentListener.php", the class must be named "afterIpAssignmentListener".

Other information:

  • The namespace must be App\Custom\EventListeners.
  • A sample listener using the afterServerIpAssignments event is provided in the file /var/www/html/app/Custom/EventListeners/afterIpAssignmentListener.php.example demonstrating how to execute commands on the assigned switch ports.
  • Tenantos is build on Laravel. All standard Laravel functions, helpers and methods are available and the Tenantos log function and helper functions for interaction with switches are shown in the example file.
  • For database queries, either use the models in the directory /var/www/html/app/Models, or use the Eloquent query builder directly. To see the database schema, connect to the database server. The login details can be found in the file /var/www/html/.env
  • Remember to execute app event:cache after creating or deleting an event. It is not necessary to re-cache the events when modifying the code.

Tips & Tricks

Get all IPs of an server

To retrieve all IP assignments for a specific server, use the servers model as follows:

use App\Models\servers;

...

$ipAssignments = servers::find($event->serverId)->ipassignments;

$ipAssignments is an array of objects, each representing an IP assignment. This array includes detailed information about each assigned IP, as well as data about the associated subnet. Use logActivity(json_encode($ipAssignments)) to see the exact content.