Get Virtualizor
Hooks

Overview

Hooks enable the admin to modify certain aspects of the task being done. There are some Hooks in Virtualizor that you can take advantage of :

Hooks directory is located at  /usr/local/virtualizor/hooks/

after_createvps

  • This hook will be executed after the VPS has been created but not started.
  • To execute this hook you have to place hook file on server where the VPS is located.
  • Create after_createvps.php in hooks folder and write your code in the __after_createvps() function
<?php
function __after_createvps($vps){    
 //Write your code here. e.g. You can add an additional NIC to the VPS configuration file if you wanted.
 }
?>

after_config_write

  • This hook will be executed after the VPS Configuration is created or edited.
  • To execute this hook you have to place hook file on server where the VPS is located.
  • Create after_config_write.php in hooks folder and write your code in the __after_config_write() function.
<?php
function __after_config_write($vps){

	// Following is the example of how to add another interface element
	$path = '/etc/libvirt/qemu/'.$vps['vps_name'].'.xml';
	
	$doc = new DOMDocument;
	$doc->load($path);
	
	$root = $doc->documentElement;
	$devices = $root->getElementsByTagName('devices')->item(0);
	
	// For adding any element
	$interface = $doc->createElement('interface', '');
	$interface_attr = $doc->createAttribute('type');
	$interface_attr->value = 'bridge';
	$interface->appendChild($interface_attr);
	
	$source = $doc->createElement('source', '');
	$source_attr = $doc->createAttribute('bridge');
	$source_attr->value = 'YOUR_BRIDGE';
	$source->appendChild($source_attr);	
	$interface->appendChild($source);
	
	$target = $doc->createElement('target', '');
	$target_attr = $doc->createAttribute('dev');
	$target_attr->value = 'YOUR_INTERFACE';
	$target->appendChild($target_attr);	
	$interface->appendChild($target);
	
	$mac = $doc->createElement('mac', '');
	$mac_attr = $doc->createAttribute('address');
	$mac_attr->value = 'MAC_ADDRESS';
	$mac->appendChild($mac_attr);	
	$interface->appendChild($mac);
	
	$devices->appendChild($interface);

	// For removing any element
	/*$sound = $root->getElementsByTagName('sound');
	
	foreach($sound as $sounds){
		$sounds->parentNode->removeChild($sounds);
	}*/
	
	$doc->save($path);


}
?>

before_deletevps (Since 2.9.9+)

  • This hook will be executed before the VPS deletion.
  • To execute this hook you have to place hook file on server where the VPS is located.
  • Create before_deletevps.php in hooks folder and write your code in the __before_deletevps() function.
<?php
function __before_deletevps($vps){

    // Write your code here. e.g. You can add additional parameters in the config file of the the VPS.

}
?>

after_deletevps

  • This hook will be executed after the VPS is deleted.
  • To execute this hook you have to place hook file on server where the VPS is located.
  • Create after_deletevps.php in hooks folder and write your code in the __after_deletevps() function.
<?php
function __after_deletevps($vps){

    // Write your code here. e.g. You can add additional parameters in the config file of the the VPS.

}
?>

before_startvps (Since 2.9.9+)

  • This hook will be executed before vps is started.
  • To execute this hook you have to place hook file on server where the VPS is located.
  • Create before_startvps.php in hooks folder and write your code in the __before_startvps() function.
<?php
function __before_startvps($vps){

    // Write your code here. e.g. You can add additional parameters in the config file of the the VPS.

}
?>

after_startvps (Since 2.9.9+)

  • This hook will be executed after the vps is started .
  • To execute this hook you have to place hook file on server where the VPS is located.
  • Create after_startvps.php in hooks folder and write your code in the __after_startvps() function.
<?php
function __after_startvps($vps){

    // Write your code here. e.g. You can add additional parameters in the config file of the the VPS.

}
?>

addvs (Since 2.9.9+)

  • This hook will be executed on MASTER only not on Slave server after the VPS is created successfully.
  • Create addvs.php in hooks folder and write your code in the __addvs() function.
<?php
function __addvs($vps){

    // Write your code here. e.g. You can add additional parameters in the config file of the the VPS.

}
?>

editvs (Since 2.9.9+)

  • This hook will be executed on MASTER only not on Slave server after the VPS is edited successfully.
  • Create editvs.php in hooks folder and write your code in the __editvs() function.
  • $old_vps will have the VPS config before editing and $new_vps will conatin the new setting after editing the VPS.
<?php
function __editvs($old_vps, $new_vps){

    // Write your code here. 

}
?>

deletevs (Since 2.9.9+)

  • This hook will be executed on MASTER only not on Slave server after the VPS is deleted successfully.
  • Create deletevs.php in hooks folder and write your code in the __deletevs() function.
<?php
function __deletevs($vps){

    // Write your code here. 

}
?>

pre_process_payment (Since 3.0.2+)

  • This hook will be executed when the response from payment gateway will be redirected to Virtualizor.
  • Create pre_process_payment.php in hooks folder and write your code in the __pre_process_payment() function
<?php
function __pre_process_payment(){
	$ccavenue_resp = optREQ('encResp');
	if(!empty($ccavenue_resp)){
		$_REQUEST['process_payment'] = 'ccavenue';
		$_REQUEST['id'] = optREQ('orderNo');
	}
	
}
?>

Get IP details of the VM

  • If you want to get the IP details of the VM inside any hook where $vps is passed, you can get the IP details using following code.
  • You will need to write the following code inside the hook function and you will get the IPs of your VM in $ips variable.


<?php


// List the IPs
$res = makequery("SELECT i.*, ip.* FROM ips i
                  LEFT JOIN ippool ip ON (ip.ippid = i.ippid) 
                  WHERE i.vpsid = :vid
                  ORDER BY `primary` DESC", array(':vid' => $vps['vpsid']));   

for($i=0; $i < vsql_num_rows($res); $i++){
    $ips[$i] = vsql_fetch_assoc($res);
}
?>
    Was this page helpful?
    Newsletter Subscription
    Subscribing you to the mailing list