Xenapi
A Xen PHP API for managment of Hypervisor and Citrix Server and their Virtual Machines for Laravel
Install / Use
/learn @sirCamp/XenapiREADME
Xenapi for PHP
A Xen PHP API for managment of Hypervisor and Citrix Server and their Virtual Machines for PHP, it works on Laravel 4, Laravel 5, Codeigniter and other PHP framework. Before install this library make sure to have installed xmlrpc php module
API in PHP to communicate with Xen Server . This packages is available on Composer's repositories . The package Allows to completely Manage an Hypervisvor Citrix . With this , you can clone , start , stop and reboot any VPS of your Hypervisor Also, this APi allow you toi have a realtime graph of your Hypervisor's VPS! I've create the Method that obtain XML realtime stats of Machine and convert it to RRD file, needed for the FlotJS library to draw the graph. This API is available for all the major PHP Frameworks, such as Laravel, Symfony or Codeigniter.
Table Of Contents
Installation :
Package is available on Packagist, you can install it using Composer.
In the require key of composer.json file add the following:
"sircamp/xenapi": "2.1"
Or type this command from your project folder
composer require sircamp/xenapi
Documentation:
Namespace Import
Sircamp\Xenapi is namespaced, but you can make your life easier by importing a single class into your context:
use Sircamp\Xenapi\Xen as Xen;
Connect With an Hypervisor
Make sure that you have IP, User and Password of your hypervisor:
$ip = "123.123.123.123";
$user = "username";
$password = "password";
$xen = new Xen($ip,$user,$password); //OK now you have an open connection to Hypervisor
Get Virtual Machine
This serves to have a virtual machine (by the hostname) that is on selected hypervisor :
$vm = $xen->getVMByNameLabel("virtual.machine.hostaname");
Virtual Machine Management
Now you have the an XenVirtualMachine object that map your virtual machine, so we are ready to manage the VM
Start VM
This method starts a stopped VM
$vm->start();
You can, also, pass two parameters at this method:
$pause = true; // start VM in pause status , dafault is false
$force = true; // force the boot of VM, default is true
$vm->start($pause, $force);
StartOn VM
This method starts the specified VM on a particular host. This function can only be called with the VM is in the Halted State. This method needs an XenHost object as parameter, but if you want you can pass a UUID host string
$host // this is a XenHost object istance or a string istance
$hostRef = $host;
$pause = true; // start VM in pause status , dafault is false
$force = true; // force the boot of VM, default is true
$vm->startOn($hostRef,$pause, $force);
Shutdown VM
This method sends a shutdown command to the Virtual Machine
$vm->cleanShutdown();
Hard Shutdown VM
This method immediatly a shutdown the Virtual Machine
$vm->hardShutdown();
Reboot VM
This method sends a reboot command to the Virtual Machine
$vm->cleanShutdown();
Hard Reboot VM
This method immediatly restarts the Virtual Machine
$vm->hardShutdown();
Suspend VM
This method puts Virtual Machine in a suspend mode (read the Citrix manual)
$vm->suspend();
Resume VM
This method resumes a Virtual Machine that is in a suspend mode (read the Citrix manual)
$vm->resume();
ResumeOn VM
This method awaken the specified VM and resume it on a particular Host. This can only be called when the specified VM is in the Suspended state. This method needs an XenHost object as parameter, but if you want you can pass a UUID host string
$host // this is a XenHost object istance or a string istance
$hostRef = $host;
$vm->resumeOn($hostRef);
Pause VM
This method puts a Virtual Machine in pause
$vm->pause();
Unpause VM
This method restores a Virtual Machine that is in pause
$vm->unpause();
Clone VM
This method clones the selected Virtual Machine into another ( please check if your hypervisor supports another machine).
before this, you must stop the virtual machine that you want clone
$name = "new_cloned_vm"; // name of cloned vm
$vm->cleanShudown(); // stop vm
$vm->clonevm($name);
Power Status of VM
This method gets the power status of the selected Virtual Machine
$vm->getPowerState();
Power State Reset of VM
Reset the power-state of the VM to halted in the database only. (Used to recover from slave failures in pooling scenarios by resetting the power-states of VMs running on dead slaves to halted.) This is a potentially dangerous operation; use with care.
$vm->powerStateReset();
UUID of VM
This method obtains the UUID of the selected Virtual Machine.
$vm->getUUID();
Consoles of VM
This method returns the all console instances of selected Virtual Machine. The console istance allows you to have and manage a SSH or RDP session of Virtual Machine
$vm->getConsoles()
UUID of VM Console
This method returns the UUID of selected Virtual Machine's console. The UUID is very usefull for console istance mangement.
$vm->getConsoleUUID($console)
Guest Metrics of VM
This method returns the guest metrics of selected Virtual Machine. This metrics contains:
- uuid
- os_version (name, uname, distro, relase version)
- memory
- disks
- networks
- other
in the future, i will write an example
$vm->getGuestMetrics()
Metrics of VM
This method returns the metrics of selected Virtual Machine. This metrics contains:
- uuid
- memory_actual
- VCPUs_number
- VCPUs_utilisation
as for guest metrics, in the future, i will write an example
$vm->getMetrics()
Statistics of VM
This method returns the current stats of the running selected Virtual Machine. With this method, you can obtain the stats of CPUs, RAM and DISK I/O in realtime!
However, this method return an response object that contains a XML string in the value attribute. Inside this XML string you find the required statistics.
as for last two methods, in the future, i will write an example. Also, i would to show you how to obtain a realtime stats graph, stay tuned ;)
$vm->getStats()
Disks Total Space of VM
This method returns the total amount of Virtual Machine's Disks space. Actually this method return the total in bytes.
$vm->getDiskSpace()
Also, you can pass an argument:
$format = "GB";
$vm->getDiskSpace($format);
This allow you to have the disk space in the format as you want. NB: this feature is not yet implemented
Name of VM
This method returns the name of VM
$vm->getName()
GetAll VMs
This method returns a list of all the VMs known to the system.
$vm->getAll()
PoolMigrate VM
Migrate a VM to another Host. This can only be called when the specified VM is in the Running state. This method needs an XenHost object as parameter, but if you want you can pass a UUID host string The optionsMap parameter is a map that contains the options for pool migrating.
$optionsMap = array(
'option1' => "option first",
'option2' => "option second"
)
$host // this is a XenHost object istance or a string istance
$hostRef = $host;
$vm->poolMigrate($refHost, $optionsMap);
MigrateSend VM
Assert whether a VM can be migrated to the specified destination. In this method the first parameter, dest, is the result of a Host.migrate receive call. The vdiMap parameter is the map of source VDI to destination SR. The vifMap parameter is the map of VIF to destination network. The optionsMap Extra configuration operations. The live parameter allows to migrate the VM in live mode or nothing, on default this paramter is set to false.
$dest // Host.migrate call result
$vdiMap //map of source VDI
$vifMap //map of source VFI
$options = array(
'option1' => "option first",
'option2' => "option second"
);
$live = true;
$vm->migrateSend($dest,$vdiMap,$vifMap,$options,$live)
NB: this method is still in testing mode
AssertCanMigrate VM
Assert whether a VM can be migrated to the specified destination. In this method the first parameter, dest, is the result of a Host.migrate receive call. The vdiMap parameter is the map of source VDI to destination SR. The vifMap parameter is the map of VIF to destination network. The optionsMap Extra configuration operations. The live parameter allows to migrate the VM in live mode or nothing, on default this paramter is set to false.
$dest // Host.migrate call result
$vdiMap //map of source VDI
$vifMap //map of source VFI
$options = array(
'option1' => "option first",
'option2' => "option second"
);
$live = true;
$vm->assertCanMigrate($dest,$vdiMap,$vifMap,$options,$live)
NB: this method is still in testing mode
Snapshot of VM
Snapshots the specified VM, making a new VM. Snapshot automatically exploits the capabilities of the underlying storage repository in which the VM’s disk images are stored
$name = "name of snapshot";
$vm->snapshot($name)
Snapshot of VM
Snapshots the specified VM with quiesce, making a new VM. Snapshot automatically exploits the capabilities of the underlying storage repository in which the VM’s disk images are stored
$name = "name of s
