Overview
Filters enable the admin to modify/hook into certain aspects of the task being done. There are several Filters in Virtualizor that you can take advantage of.
Note : Filters have been added since Virtualizor version 3.0.4
The following guide will show you how the Server Admin can configure Filter Functions to customize the create vps, manage vps, start vps and several other events.
1. SSH to your server and go to the following path:
/usr/local/virtualizor/hooks
2. You can see the file filter.txt
3. Rename the file to make it a PHP file as filter.php
4. Uncomment the function that matches your requirement, add the code inside the function as per your requirements and save the file.
5. That’s it you have configured your filter
insert_filter
We make use of insert_filter function to trigger a specific filter.
insert_filter(string $tag, string $function_to_add, int $priority, int $accepted_args);
Parameters
Param | Type | Description |
---|---|---|
$tag | string/Required | Action name that triggers the specific function. |
$function_to_add | string/Required | Name of the function to be triggered. |
$priority | int/optional | Priority in which you want the filter to be triggered. (Default is 10) |
$accepted_args | int/optional | Number of arguments accepted by the filter function. (Default is 1) |
Error Handle
Following is the example describing how a filter is triggered by an action
If you would like to make some changes whenever an error is triggered in Virtualizor, you will have to rename the file filter.txt to filter.php and uncomment the following part and add the code in my_error_handle() function.
insert_filter('error_handle', 'my_error_handle', 1, 1);
function my_error_handle($error){
// Add your custom code here
}
insert_filter adds the data added by you to Virtualizor and when the appropriate event is triggered your respective function is called. For eg., in the above example, whenever the event error_handle occurs, function my_error_handle is called and the code inside the same is executed.
addvs
The addvs filter will trigger your function VM creation is done.
NOTE: This function will be called ONLY on Master
Parameters
Key | Value | Description |
---|---|---|
$newvs | array (Required) | Contains details of the VM which is being created |
Uncomment the following part inside filter.php and add your code inside the function my_addvs
insert_filter('addvs', 'my_addvs', 1, 1);
function my_addvs($newvs){
// Add your custom code here
}
before_addvs
The before_addvs filter will trigger before making entry of the VM inside the DB.
NOTE: This function will be called ONLY on Master
Parameters
Key | Value | Description |
---|---|---|
$newvs | array (Required) | Contains details of the VM which is being created |
Uncomment the following part inside filter.php and add your code inside the function my_before_addvs
insert_filter('before_addvs', 'my_before_addvs', 1, 1);
function my_before_addvs ($newvs){
// Modify the $newvs as per your requirement
return $newvs;
}
editvs
The editvs filter will trigger your function VM creation is done.
NOTE: This function will be called ONLY on Master
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM before edit operation |
$editvs | array (Required) | Contains New details of the VM after edit operation |
Uncomment the following part inside filter.php and add your code inside the function my_editvs
insert_filter('editvs', 'my_editvs', 1, 2);
function my_editvs($vps, $editvs){
// Add your custom code here
}
after_config_write
The after_config_write filter will trigger your function when Virtualizor has written the config file for the VM.
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_after_config_write
insert_filter('after_config_write', 'my_after_config_write', 1, 1);
function my_after_config_write($vps){
// Add your custom code here
}
after_createvps
The after_createvps filter will trigger your function after Virtualizor has created the VM.
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_after_createvps
insert_filter('after_createvps', 'my_after_createvps', 1, 1);
function my_after_createvps($vps){
// Add your custom code here
}
before_deletevps
The before_deletevps filter will trigger your function before virtualizor deletes the VM. (Since 3.0.7+)
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_before_deletevps
insert_filter('before_deletevps', 'my_before_deletevps', 1, 1);
function my_before_deletevps($vps){
// Add your custom code here
}
deletevs
The before_deletevps filter will trigger your function when Virtualizor deletes the VM.
NOTE: This filter will be called on Master server ONLY
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_deletevs
insert_filter('deletevs', 'my_deletevs', 1, 1);
function my_deletevs($vps){
// Add your custom code here
}
after_deletevps
The after_deletevps filter will trigger your function after Virtualizor has deleted the VM.
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_after_deletevps
insert_filter('after_deletevps', 'my_after_deletevps', 1, 1);
function my_after_deletevps($vps){
// Add your custom code here
}
before_startvps
The before_startvps filter will trigger your function before Virtualizor starts the VM.
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_after_deletevps
insert_filter('before_startvps', 'my_before_startvps', 1, 1);
function my_before_startvps($vps){
// Add your custom code here
}
after_startvps
The after_startvps filter will trigger your function after Virtualizor has started the VM.
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_after_startvps
insert_filter('after_startvps', 'my_after_startvps', 1, 1);
function my_after_startvps($vps){
// Add your custom code here
}
create_vm_pre_addvs
The create_vm_pre_addvs filter will trigger your function before creating a VM from Cloud user account
Parameters
Key | Value | Description |
---|---|---|
$vps | array (Required) | Contains details of the VM |
Uncomment the following part inside filter.php and add your code inside the function my_create_vm_pre_addvs
insert_filter('create_vm_pre_addvs', 'my_create_vm_pre_addvs', 1, 1);
function my_create_vm_pre_addvs($vps){
// Add your custom code here
}
server_selection
The server_selection filter will trigger your function after Virtualizor gets the list of eligible server for VM creation. So that you can apply your selection algorithm to this eligible list and return it to virtualizor.
Parameters
Key | Value | Description |
---|---|---|
$tmp_servers | array (Required) | Contains details of the eligible server for VM creation |
Uncomment the following part inside filter.php and add your code inside the function my_server_selection
insert_filter('server_selection', 'my_server_selection', 1, 1);
function my_server_selection($tmp_servers){
// Add your custom code here
return $tmp_servers;
}
randomize_ips
The randomize_ips filter will trigger your function after Virtualizor gets the list of ips for VM creation. Using this filter you can arrange the list of ips(IPv4, IPv6, Internal IPs, IPv6 subnets) whichever way you want.
Parameters
Key | Value | Description |
---|---|---|
$ips, $ips_int, $ips6, $ips6_subnet | array (Required) | Each array contains list of available ips for VM creation. |
Uncomment the following part inside filter.php and add your code inside the function my_randomize_ips
insert_filter('randomize_ips', 'my_randomize_ips', 1, 4);
function my_randomize_ips($ips, $ips_int, $ips6, $ips6_subnet){
// @param array $ips Array containting available ipv4 addresses
// @param array $ips_int Array containting available internal ipv4 addresses
// @param array $ips6 Array containting available ipv6 addresses
// @param array $ips6_subnet Array containting available ipv6 subnets
if(!empty($ips)){
shuffle($ips);
}
if(!empty($ips_int)){
shuffle($ips_int);
}
if(!empty($ips6)){
shuffle($ips6);
}
if(!empty($ips6_subnet)){
shuffle($ips6_subnet);
}
return array('ips' => $ips, 'ips_int' => $ips_int, 'ips6' => $ips6, 'ips6_subnet' => $ips6_subnet);
}
before_adduser
The before_adduser filter will trigger your function before adding new user to DB.
Parameters
Key | Value | Description |
---|---|---|
$user_posted_data | array (Required) | Contains details of the User which will be added |
Uncomment the following part inside filter.php and add your code inside the function my_before_adduser
insert_filter('before_adduser', 'my_before_adduser', 1, 1);
// @param array $user_posted_data Array containing all the posted data
function my_before_adduser($user_posted_data){
// Add your custom code here
return $user_posted_data;
}
after_adduser
The after_adduser filter will trigger your function after a new user has been added to DB.
Parameters
Key | Value | Description |
---|---|---|
$uid | int | Contains user id of the user. It will not return anything to virtualizor. |
Uncomment the following part inside filter.php and add your code inside the function my_after_adduser
insert_filter('after_adduser', 'my_after_adduser', 1, 1);
// @param int $uid Integer containing user id of the user that is added
function my_after_adduser($uid){
// Add your custom code here
}