removed another bunch of references to geni
[sfa.git] / sfa / server / aggregate.py
1 ### $Id$
2 ### $URL$
3
4 import os
5 import sys
6 import datetime
7 import time
8 import xmlrpclib
9 from types import StringTypes, ListType
10
11 from sfa.util.server import SfaServer
12 from sfa.util.storage import *
13 from sfa.util.faults import *
14 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
15 import sfa.util.soapprotocol as soapprotocol
16
17 # GeniLight client support is optional
18 try:
19     from egeni.geniLight_client import *
20 except ImportError:
21     GeniClientLight = None
22
23
24 class Aggregate(SfaServer):
25
26     ##
27     # Create a new aggregate object.
28     #
29     # @param ip the ip address to listen on
30     # @param port the port to listen on
31     # @param key_file private key filename of registry
32     # @param cert_file certificate filename containing public key (could be a GID file)     
33     def __init__(self, ip, port, key_file, cert_file):
34         SfaServer.__init__(self, ip, port, key_file, cert_file)
35         self.server.interface = 'aggregate'
36
37 ##
38 # Aggregates is a dictionary of aggregate connections keyed on the aggregate hrn
39
40 class Aggregates(dict):
41
42     required_fields = ['hrn', 'addr', 'port']
43      
44     def __init__(self, api, file = "/etc/sfa/aggregates.xml"):
45         dict.__init__(self, {})
46         self.api = api
47         self.interfaces = []
48         # create default connection dict
49         connection_dict = {}
50         for field in self.required_fields:
51             connection_dict[field] = ''
52         aggregates_dict = {'aggregates': {'aggregate': [connection_dict]}}
53         # get possible config file locations
54         loaded = False
55         path = os.path.dirname(os.path.abspath(__file__))
56         filename = file.split(os.sep)[-1]
57         alt_file = path + os.sep + filename
58         files = [file, alt_file]
59         
60         for f in files:
61             try:
62                 if os.path.isfile(f):
63                     self.aggregate_info = XmlStorage(f, aggregates_dict)
64                     loaded = True
65             except: pass
66
67         # if file is missing, just recreate it in the right place
68         if not loaded:
69             self.aggregate_info = XmlStorage(file, aggregates_dict)
70         self.aggregate_info.load()
71         self.connectAggregates()
72
73     def connectAggregates(self):
74         """
75         Get connection details for the trusted peer aggregates from file and 
76         create an connection to each. 
77         """
78         aggregates = self.aggregate_info['aggregates']['aggregate']
79         if isinstance(aggregates, dict):
80             aggregates = [aggregates]
81         if isinstance(aggregates, list):
82             for aggregate in aggregates:
83                 # make sure the required fields are present
84                 if not set(self.required_fields).issubset(aggregate.keys()):
85                     continue
86                 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
87                 if not hrn or not address or not port:
88                     continue
89                 self.interfaces.append(aggregate)
90                 # check which client we should use
91                 # sfa.util.xmlrpcprotocol is default
92                 client_type = 'xmlrpcprotocol'
93                 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
94                     client_type = 'geniclientlight'
95                 
96                 # create url
97                 url = 'http://%(address)s:%(port)s' % locals()
98
99                 # create the client connection
100                 # make sure module exists before trying to instantiate it
101                 if client_type in ['geniclientlight'] and GeniClientLight:
102                     self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
103                 else:
104                     self[hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
105
106         # set up a connection to the local registry
107         address = self.api.config.SFA_AGGREGATE_HOST
108         port = self.api.config.SFA_AGGREGATE_PORT
109         url = 'http://%(address)s:%(port)s' % locals()
110         local_aggregate = {'hrn': self.api.hrn, 'addr': address, 'port': port}
111         self.interfaces.append(local_aggregate) 
112         self[self.api.hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
113
114