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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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 .
  • 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. 

}
?>

Was this helpful to you?