[34] | 1 | #!/usr/bin/python |
---|
| 2 | #============================================================================ |
---|
| 3 | # This library is free software; you can redistribute it and/or |
---|
| 4 | # modify it under the terms of version 2.1 of the GNU Lesser General Public |
---|
| 5 | # License as published by the Free Software Foundation. |
---|
| 6 | # |
---|
| 7 | # This library is distributed in the hope that it will be useful, |
---|
| 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 10 | # Lesser General Public License for more details. |
---|
| 11 | # |
---|
| 12 | # You should have received a copy of the GNU Lesser General Public |
---|
| 13 | # License along with this library; if not, write to the Free Software |
---|
| 14 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 15 | #============================================================================ |
---|
| 16 | # Copyright (C) 2006 XenSource Ltd. |
---|
| 17 | # Copyright (C) 2006 IBM Corporation |
---|
| 18 | #============================================================================ |
---|
| 19 | |
---|
| 20 | import atexit |
---|
| 21 | import os |
---|
| 22 | import sys |
---|
| 23 | from XmTestLib import * |
---|
| 24 | from xen.xm import main as xmmain |
---|
| 25 | from xen.xm import XenAPI |
---|
| 26 | from xen.xm.opts import OptionError |
---|
| 27 | from types import DictType |
---|
| 28 | import xml.dom.minidom |
---|
| 29 | |
---|
| 30 | def get_login_pwd(): |
---|
| 31 | if xmmain.serverType == xmmain.SERVER_XEN_API: |
---|
| 32 | try: |
---|
| 33 | login, password = xmmain.parseAuthentication() |
---|
| 34 | return (login, password) |
---|
| 35 | except: |
---|
| 36 | raise OptionError("Configuration for login/pwd not found. " |
---|
| 37 | "Need to run xapi-setup.py?") |
---|
| 38 | raise OptionError("Xm configuration file not using Xen-API for " |
---|
| 39 | "communication with xend.") |
---|
| 40 | |
---|
| 41 | sessions=[] |
---|
| 42 | |
---|
| 43 | def connect(*args): |
---|
| 44 | try: |
---|
| 45 | creds = get_login_pwd() |
---|
| 46 | except Exception, e: |
---|
| 47 | FAIL("%s" % str(e)) |
---|
| 48 | try: |
---|
| 49 | session = XenAPI.Session(xmmain.serverURI) |
---|
| 50 | except: |
---|
| 51 | raise OptionError("Could not create XenAPI session with Xend." \ |
---|
| 52 | "URI=%s" % xmmain.serverURI) |
---|
| 53 | try: |
---|
| 54 | session.login_with_password(*creds) |
---|
| 55 | except: |
---|
| 56 | raise OptionError("Could not login to Xend. URI=%s" % xmmain.serverURI) |
---|
| 57 | def logout(): |
---|
| 58 | try: |
---|
| 59 | for s in sessions: |
---|
| 60 | s.xenapi.session.logout() |
---|
| 61 | except: |
---|
| 62 | pass |
---|
| 63 | sessions.append(session) |
---|
| 64 | atexit.register(logout) |
---|
| 65 | return session |
---|