source: trunk/packages/xen-3.1/xen-3.1/tools/xm-test/lib/XmTestLib/xapi.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

  • Property svn:mime-type set to text/script
File size: 2.3 KB
Line 
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
20import atexit
21import os
22import sys
23from XmTestLib import *
24from xen.xm import main as xmmain
25from xen.xm import XenAPI
26from xen.xm.opts import OptionError
27from types import DictType
28import xml.dom.minidom
29
30def 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
41sessions=[]
42
43def 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
Note: See TracBrowser for help on using the repository browser.