I am about to call an OpenStack xen-api host plugin to ask my host to upload images to OpenStack glance. As I was exploring how to do that, I thought it would be a good time to write a Hello XenAPI host plugin example. You can read more about the XenAPI here:
Step 1: Create the host plugin
The host plugin should be an executable file on the host. In my case, it will be a simple python script. I choose the name “hi” for my plugin, so the plugin location is:
- On XCP or XenServer:
/etc/xapi.d/plugins/hi
- On Debian/Ubuntu with xcp-xapi:
/usr/lib/xcp/plugins/hi
The “hi” plugin looks like this:
#!/usr/bin/python
import XenAPIPlugin
def hello(session, arg_dict):
name = arg_dict['name']
return "Hello " + name + "!"
if __name__ == "__main__":
XenAPIPlugin.dispatch(
{"hello": hello}
)
Don’t forget to make it executable:
chmod +x /usr/lib/xcp/plugins/hi
Step 2: call the host plugin
I will use the XenAPI python plugin for doing this. Start an interactive python session, and type in these things:
import XenAPI
session = XenAPI.Session('https://kronosmachine.somedomain')
session.login_with_password('root', 'megasecret')
host, = session.xenapi.host.get_all()
print session.xenapi.host.call_plugin(host, 'hi', 'hello', {'name' : 'OpenStack'})
You should see something like:
Hello OpenStack!
The session
The session parameter passed to your dispatched function is a xenapi session. So if you want to list the hosts, you could do it this way:
...
def hello(session, arg_dict):
return repr(session.xenapi.host.get_all())
...
Again, for more information: