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, config = "/usr/share/geniwrapper/geni/util/geni_config"):
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):
40 dict.__init__(self, {})
42 aggregates_file = self.api.server_basedir + os.sep + 'aggregates.xml'
44 for field in self.required_fields:
45 connection_dict[field] = ''
46 self.aggregate_info = XmlStorage(aggregates_file, {'aggregates': {'aggregate': [connection_dict]}})
47 self.aggregate_info.load()
48 self.connectAggregates()
51 def connectAggregates(self):
53 Get connection details for the trusted peer aggregates from file and
54 create an GeniClient connection to each.
56 aggregates = self.aggregate_info['aggregates']['aggregate']
57 if isinstance(aggregates, dict):
58 aggregates = [aggregates]
59 if isinstance(aggregates, list):
60 for aggregate in aggregates:
61 # make sure the required fields are present
62 if not set(self.required_fields).issubset(aggregate.keys()):
64 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
65 if not hrn or not address or not port:
67 # check which client we should use
68 # geniclient is default
69 client_type = 'geniclient'
70 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
71 client_type = 'geniclientlight'
74 url = 'http://%(address)s:%(port)s' % locals()
76 # create the client connection
77 # make sure module exists before trying to instantiate it
78 if client_type in ['geniclientlight'] and GeniClientLight:
79 self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
81 self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
83 # set up a connection to the local registry
84 # connect to registry using GeniClient
85 address = self.api.config.GENI_AGGREGATE_HOSTNAME
86 port = self.api.config.GENI_AGGREGATE_PORT
87 url = 'http://%(address)s:%(port)s' % locals()
88 self[self.api.hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)