XenAPI in Python with examples
XenAPI in Python with examples
Xensource has released (some time ago actually) the new XenAPI, which offers a standardized method of working with Xen VMs. Sadly, the documentation for the python bindings is a little spotty, so I thought I would cover some of the more obscure items here.
First of all, the initialization against the unix socket (the default config) isn't clearly documented. To start a new XenAPI session which can connect to the socket and do, well, anything, try the following code snippet:
from xen.xm.XenAPI import Session
session=Session('httpu:///var/run/xend/xen-api.sock') #This is the default location
session.login_with_password('', '') #No auth required for the socket!
xenapi=session.xenapi #To make it the same namespace as the docs.
#Let's retrieve some random stats.
print xenapi.VM.get_all_records(),"\n\n\n",\
xenapi.VM_metrics.get_all_records(),"\n\n\n",\
xenapi.VM.get_all(),"\n\n\n"
The Session initialization code is the important part, as I had to trawl the xen source code to find this socket and syntax. Lame. The good news is that the API is super easy to use once you get this part working.


