Hooks

From Virtualizor Wiki
Jump to: navigation, search

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


}

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.
function __after_deletevps($vps){

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

}