9 from types import StringTypes, ListType
11 from geni.util.geniserver import GeniServer
12 from geni.util.geniclient import GeniClient
13 from geni.util.storage import *
14 from geni.util.faults import *
16 # GeniLight client support is optional
18 from egeni.geniLight_client import *
20 GeniClientLight = None
23 class Aggregate(GeniServer):
26 # Create a new aggregate object.
28 # @param ip the ip address to listen on
29 # @param port the port to listen on
30 # @param key_file private key filename of registry
31 # @param cert_file certificate filename containing public key (could be a GID file)
33 def __init__(self, ip, port, key_file, cert_file):
34 GeniServer.__init__(self, ip, port, key_file, cert_file)
35 self.server.interface = 'aggregate'
38 # Aggregates is a dictionary of geniclient aggregate connections keyed on the aggregate hrn
40 class Aggregates(dict):
42 required_fields = ['hrn', 'addr', 'port']
44 def __init__(self, api, file = "/etc/sfa/aggregates.xml"):
45 dict.__init__(self, {})
48 # create default 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
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]
63 self.aggregate_info = XmlStorage(f, aggregates_dict)
67 # if file is missing, just recreate it in the right place
69 self.aggregate_info = XmlStorage(file, aggregates_dict)
70 self.aggregate_info.load()
71 self.connectAggregates()
74 def connectAggregates(self):
76 Get connection details for the trusted peer aggregates from file and
77 create an GeniClient connection to each.
79 aggregates = self.aggregate_info['aggregates']['aggregate']
80 if isinstance(aggregates, dict):
81 aggregates = [aggregates]
82 if isinstance(aggregates, list):
83 for aggregate in aggregates:
84 # make sure the required fields are present
85 if not set(self.required_fields).issubset(aggregate.keys()):
87 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
88 if not hrn or not address or not port:
90 # check which client we should use
91 # geniclient is default
92 client_type = 'geniclient'
93 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
94 client_type = 'geniclientlight'
97 url = 'http://%(address)s:%(port)s' % locals()
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)
104 self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
106 # set up a connection to the local registry
107 # connect to registry using GeniClient
108 address = self.api.config.GENI_AGGREGATE_HOST
109 port = self.api.config.GENI_AGGREGATE_PORT
110 url = 'http://%(address)s:%(port)s' % locals()
111 self[self.api.hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)