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');
	}
	
}
?>

error_handle

This hook will let you handle/define the error handling your way.

server_selection

This hook will help you in the server selection algorithm. This hook will give you the list of server which Virtualizor has filtered as per the resources.

function my_server_selection($tmp_servers){
	// Add your custom code here
	return $tmp_servers;
}

randomize_ips

This hook will randomize the IPs which are being selected as available IPs while creating Virtual machines.

function my_randomize_ips($ips, $ips_int, $ips6, $ips6_subnet){

	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);
}

check_insert_vps_bw

This hook will help you to check / modify the Bandwidth data before inserting to the virtualizor Database.

function my_check_insert_vps_bw($default, $vps, $value){
	
	// Add your custom code here
	$changed = 0;
	// $value['in'] and $value['out'] is the value which will be coming from CT/VM for bandwidth (Total download till NOW)
	// $vps['last_in'] and $vps['last_out'] are the values which are calculated for the CT/VM till last cron
	
	// Following is just an example to handle the value you can write your own logic to modify $in and $out
 	if($vps['last_in'] > $value['in'] && (($vps['last_in'] - $value['in']) > SOME_VALUE)){
		$in = $vps['last_in'];
		$value['in'] = 0;
		$changed = 1;
	}

	if($vps['last_out'] > $value['out'] && (($vps['last_out'] - $value['out']) > SOME_VALUE)){
		$out = $vps['last_out'];
		$value['out'] = 0;
		$changed = 1;
	}

	if(!empty($changed)){
		return array(
				'in' => $in, 'out' => $out,
				'c_in' => $value['in'], 'c_out' => $value['out']
				);
	}

	return $default;
}

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 the 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