Skip to content

Instantly share code, notes, and snippets.

@primalmotion
Created September 1, 2011 08:55
Show Gist options
  • Select an option

  • Save primalmotion/1185744 to your computer and use it in GitHub Desktop.

Select an option

Save primalmotion/1185744 to your computer and use it in GitHub Desktop.
Sample script to communicate with hypervisor and ask for its VMs
# -*- coding: utf-8 -*-
#
# archipel-samplecommunication.py
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# This file is part of ArchipelProject
# http://archipelproject.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import xmpp
def xmpp_connect(jid, password, auth=True):
"""
create a new XMPP connection
"""
xmppclient = xmpp.Client(jid.getDomain(), debug=[])
if not xmppclient.connect():
print_error("Cannot connect to the XMPP server")
return
if auth:
if xmppclient.auth(jid.getNode(), password, "configurator") == None:
print_error("Bad authentication")
return
return xmppclient
if __name__ == "__main__":
ADMINJID = xmpp.JID("admin@ramucho")
ADMINPASS = "password"
HYPERVISOR_JID = xmpp.JID("hypervisor@ramucho/ramucho")
# create a connection and authetify
xmppclient = xmpp_connect(ADMINJID, ADMINPASS)
if not xmppclient:
raise Exception("Unable to connect")
# prepare the archipel command
archipelCommand = xmpp.Node("archipel", attrs={"action": "rostervm"})
# prepare the IQ. beware of the queryNS
iq = xmpp.Iq(typ='get', queryNS='archipel:hypervisor:control', to=HYPERVISOR_JID)
# put the archipel command in the IQ
iq.setQueryPayload([archipelCommand])
# send and wait for a reponse
resp = xmppclient.SendAndWaitForResponse(iq)
# print raw answer
print str(resp)
# parse answer if you want
print "\n\n#########################"
print "Result of the command : %s" % resp.getType()
print "list of vms :"
for item in resp.getTag("query").getTags("item"):
print " - %s" % item.getData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment