changed GENI_CONFIG_HOSTNAME to GENI_CONFIG_HOST
[sfa.git] / geni / aggregate.py
1 import os
2 import sys
3 import datetime
4 import time
5 import xmlrpclib
6
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
11 try:
12     from egeni.geniLight_client import *
13 except ImportError:
14     GeniClientLight = None
15 from geni.util.storage import *
16 from geni.util.excep import *
17
18 class Aggregate(GeniServer):
19
20     ##
21     # Create a new aggregate object.
22     #
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)     
27
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'
31
32 ##
33 # Aggregates is a dictionary of geniclient aggregate connections keyed on the aggregate hrn
34
35 class Aggregates(dict):
36
37     required_fields = ['hrn', 'addr', 'port']
38      
39     def __init__(self, api, file = "/etc/geni/aggregates.xml"):
40         dict.__init__(self, {})
41         self.api = api
42
43         # create default connection dict
44         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
49         loaded = False
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]
54         
55         for f in files:
56             try:
57                 if os.path.isfile(f):
58                     self.aggregate_info = XmlStorage(f, aggregates_dict)
59                     loaded = True
60             except: pass
61
62         # if file is missing, just recreate it in the right place
63         if not loaded:
64             self.aggregate_info = XmlStorage(file, aggregates_dict)
65         self.aggregate_info.load()
66         self.connectAggregates()
67
68
69     def connectAggregates(self):
70         """
71         Get connection details for the trusted peer aggregates from file and 
72         create an GeniClient connection to each. 
73         """
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()):
81                     continue
82                 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
83                 if not hrn or not address or not port:
84                     continue
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'
90                 
91                 # create url
92                 url = 'http://%(address)s:%(port)s' % locals()
93
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)
98                 else:
99                     self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
100
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)
107