moving modpython stuff here
[sfa.git] / sfa / server / aggregate.py
1 ### $Id$
2 ### $URL$
3
4 import os
5 import sys
6 import datetime
7 import time
8
9 from sfa.util.server import SfaServer
10 from sfa.util.storage import *
11 from sfa.util.faults import *
12 from sfa.trust.gid import GID
13 from sfa.util.table import SfaTable
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     default_fields = {
43         'hrn': '',
44         'addr': '',
45         'port': '',
46     }    
47  
48     def __init__(self, api, file = "/etc/sfa/aggregates.xml"):
49         dict.__init__(self, {})
50         self.api = api
51         
52         # create default connection dict
53         aggregates_dict = {'aggregates': {'aggregate': [default_fields]}}
54         
55         # load config file
56         self.aggregate_info = XmlStorage(file, aggregates_dict)
57         self.aggregate_info.load()
58         self.interfaces = self.registry_info['aggregates']['aggregate']
59         if not isinstance(self.interfaces, list):
60             self.interfaces = [self.interfaces]
61
62         # Attempt to get any missing peer gids
63         # There should be a gid file in /etc/sfa/trusted_roots for every
64         # peer registry found in in the aggregates.xml config file. If there
65         # are any missing gids, request a new one from the peer registry.
66         gids_current = self.api.auth.trusted_cert_list.get_list()
67         hrns_current = [gid.get_hrn() for gid in gids_found]
68         hrns_expected = [interface['hrn'] for interfaces in self.interfaces]
69         new_hrns = set(hrns_current).difference(hrns_expected)
70
71         self.get_peer_gids(new_hrns)
72         self.connectAggregates()
73
74     def connectAggregates(self):
75         """
76         Get connection details for the trusted peer aggregates from file and 
77         create an connection to each. 
78         """
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()):
86                     continue
87                 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
88                 if not hrn or not address or not port:
89                     continue
90                 self.interfaces.append(aggregate)
91                 # check which client we should use
92                 # sfa.util.xmlrpcprotocol is default
93                 client_type = 'xmlrpcprotocol'
94                 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
95                     client_type = 'geniclientlight'
96                 
97                 # create url
98                 url = 'http://%(address)s:%(port)s' % locals()
99
100                 # create the client connection
101                 # make sure module exists before trying to instantiate it
102                 if client_type in ['geniclientlight'] and GeniClientLight:
103                     self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
104                 else:
105                     self[hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
106
107         # set up a connection to the local registry
108         address = self.api.config.SFA_AGGREGATE_HOST
109         port = self.api.config.SFA_AGGREGATE_PORT
110         url = 'http://%(address)s:%(port)s' % locals()
111         local_aggregate = {'hrn': self.api.hrn, 'addr': address, 'port': port}
112         self.interfaces.append(local_aggregate) 
113         self[self.api.hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
114
115