7 from types import StringTypes, ListType
8 from geni.util.geniserver import GeniServer
9 from geni.util.geniclient import GeniClient
10 # GeniLight client support is optional
12 from egeni.geniLight_client import *
14 GeniClientLight = None
15 from geni.util.storage import *
16 from geni.util.excep import *
18 class Aggregate(GeniServer):
21 # Create a new aggregate object.
23 # @param ip the ip address to listen on
24 # @param port the port to listen on
25 # @param key_file private key filename of registry
26 # @param cert_file certificate filename containing public key (could be a GID file)
28 def __init__(self, ip, port, key_file, cert_file):
29 GeniServer.__init__(self, ip, port, key_file, cert_file)
30 self.server.interface = 'aggregate'
33 # Aggregates is a dictionary of geniclient aggregate connections keyed on the aggregate hrn
35 class Aggregates(dict):
37 required_fields = ['hrn', 'addr', 'port']
39 def __init__(self, api, file = "/etc/geni/aggregates.xml"):
40 dict.__init__(self, {})
43 # create default connection dict
45 for field in self.required_fields:
46 connection_dict[field] = ''
47 aggregates_dict = {'aggregates': {'aggregate': [connection_dict]}}
48 # get possible config file locations
50 path = os.path.dirname(os.path.abspath(__file__))
51 filename = file.split(os.sep)[-1]
52 alt_file = path + os.sep + filename
53 files = [file, alt_file]
58 self.aggregate_info = XmlStorage(f, aggregates_dict)
62 # if file is missing, just recreate it in the right place
64 self.aggregate_info = XmlStorage(file, aggregates_dict)
65 self.aggregate_info.load()
66 self.connectAggregates()
69 def connectAggregates(self):
71 Get connection details for the trusted peer aggregates from file and
72 create an GeniClient connection to each.
74 aggregates = self.aggregate_info['aggregates']['aggregate']
75 if isinstance(aggregates, dict):
76 aggregates = [aggregates]
77 if isinstance(aggregates, list):
78 for aggregate in aggregates:
79 # make sure the required fields are present
80 if not set(self.required_fields).issubset(aggregate.keys()):
82 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
83 if not hrn or not address or not port:
85 # check which client we should use
86 # geniclient is default
87 client_type = 'geniclient'
88 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
89 client_type = 'geniclientlight'
92 url = 'http://%(address)s:%(port)s' % locals()
94 # create the client connection
95 # make sure module exists before trying to instantiate it
96 if client_type in ['geniclientlight'] and GeniClientLight:
97 self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
99 self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
101 # set up a connection to the local registry
102 # connect to registry using GeniClient
103 address = self.api.config.GENI_AGGREGATE_HOST
104 port = self.api.config.GENI_AGGREGATE_PORT
105 url = 'http://%(address)s:%(port)s' % locals()
106 self[self.api.hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)