ext_ip should be address
[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.geniserver import GeniServer
12 from sfa.util.geniclient import GeniClient
13 from sfa.util.storage import *
14 from sfa.util.faults import *
15
16 # GeniLight client support is optional
17 try:
18     from egeni.geniLight_client import *
19 except ImportError:
20     GeniClientLight = None
21
22
23 class Aggregate(GeniServer):
24
25     ##
26     # Create a new aggregate object.
27     #
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)     
32     def __init__(self, ip, port, key_file, cert_file):
33         GeniServer.__init__(self, ip, port, key_file, cert_file)
34         self.server.interface = 'aggregate'
35
36 ##
37 # Aggregates is a dictionary of geniclient aggregate connections keyed on the aggregate hrn
38
39 class Aggregates(dict):
40
41     required_fields = ['hrn', 'addr', 'port']
42      
43     def __init__(self, api, file = "/etc/sfa/aggregates.xml"):
44         dict.__init__(self, {})
45         self.api = api
46         self.interfaces = []
47         # create default connection dict
48         connection_dict = {}
49         for field in self.required_fields:
50             connection_dict[field] = ''
51         aggregates_dict = {'aggregates': {'aggregate': [connection_dict]}}
52         # get possible config file locations
53         loaded = False
54         path = os.path.dirname(os.path.abspath(__file__))
55         filename = file.split(os.sep)[-1]
56         alt_file = path + os.sep + filename
57         files = [file, alt_file]
58         
59         for f in files:
60             try:
61                 if os.path.isfile(f):
62                     self.aggregate_info = XmlStorage(f, aggregates_dict)
63                     loaded = True
64             except: pass
65
66         # if file is missing, just recreate it in the right place
67         if not loaded:
68             self.aggregate_info = XmlStorage(file, aggregates_dict)
69         self.aggregate_info.load()
70         self.connectAggregates()
71
72     def connectAggregates(self):
73         """
74         Get connection details for the trusted peer aggregates from file and 
75         create an GeniClient connection to each. 
76         """
77         aggregates = self.aggregate_info['aggregates']['aggregate']
78         if isinstance(aggregates, dict):
79             aggregates = [aggregates]
80         if isinstance(aggregates, list):
81             for aggregate in aggregates:
82                 # make sure the required fields are present
83                 if not set(self.required_fields).issubset(aggregate.keys()):
84                     continue
85                 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
86                 if not hrn or not address or not port:
87                     continue
88                 self.interfaces.append(aggregate)
89                 # check which client we should use
90                 # geniclient is default
91                 client_type = 'geniclient'
92                 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
93                     client_type = 'geniclientlight'
94                 
95                 # create url
96                 url = 'http://%(address)s:%(port)s' % locals()
97
98                 # create the client connection
99                 # make sure module exists before trying to instantiate it
100                 if client_type in ['geniclientlight'] and GeniClientLight:
101                     self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
102                 else:
103                     self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
104
105         # set up a connection to the local registry
106         # connect to registry using GeniClient
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] = GeniClient(url, self.api.key_file, self.api.cert_file)
113
114