dont make Config inherit dict clas
[sfa.git] / geni / util / config.py
1 ##
2 # Geniwrapper Configuration Info
3 #
4 # This module holds configuration parameters for geniwrapper. There are two
5 # main pieces of information that are used: the database connection and
6 # the PLCAPI connection
7 ##
8
9 ##
10 # Geniwrapper uses a MYSQL database to store records. This database may be
11 # co-located with the PLC database, or it may be a separate database. The
12 # following parameters define the connection to the database.
13 #
14 # Note that Geniwrapper does not access any of the PLC databases directly via
15 # a mysql connection; All PLC databases are accessed via PLCAPI.
16
17 import os
18 import sys
19
20 geni =  os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))+ os.sep + "geni"
21
22 class Config:
23     """
24     Parse the bash/Python/PHP version of the configuration file. Very
25     fast but no type conversions.
26     """
27
28     def __init__(self, file = "/etc/geni/geni_config"):
29         # Load plc_config
30
31         loaded = False
32         path = os.path.dirname(os.path.abspath(__file__))
33         filename = file.split(os.sep)[-1]
34         alt_file = path + os.sep + 'util' + os.sep + filename
35         geni_file = geni + os.sep + 'util' + os.sep + filename
36         files = [file, alt_file, geni_file]
37
38         for file in files:
39             try: 
40                 execfile(file, self.__dict__)
41                 loaded = True
42             except:
43                 pass
44
45         if not loaded:
46             raise Exception, "Could not find config in " + ", ".join(files)
47
48         # set up some useful variables
49
50     def load(self, file):
51         try:
52             execfile(file, self.__dict__)
53         except:
54             raise Exception, "Could not find config in " + file
55
56 plcConfig = Config("/etc/planetlab/plc_config")
57
58 def get_default_dbinfo():
59     dbinfo={ 'dbname' : plcConfig.PLC_DB_NAME,
60     'address' : plcConfig.PLC_DB_HOST,
61     'port' : plcConfig.PLC_DB_PORT,
62     'user' : plcConfig.PLC_DB_USER,
63     'password' : plcConfig.PLC_DB_PASSWORD
64         }
65
66     return dbinfo
67
68 ##
69 # Geniwrapper uses a PLCAPI connection to perform operations on the registry,
70 # such as creating and deleting slices. This connection requires an account
71 # on the PLC server with full administrator access.
72 #
73 # The Url parameter controls whether the connection uses PLCAPI directly (i.e.
74 # Geniwrapper is located on the same machine as PLC), or uses a XMLRPC connection
75 # to the PLC machine. If you wish to use the API directly, then remove the Url
76 # field from the dictionary. 
77
78 def get_pl_auth():
79     pl_auth = {'Username': plcConfig.PLC_API_MAINTENANCE_USER,
80     'AuthMethod': 'capability',
81     'AuthString':  plcConfig.PLC_API_MAINTENANCE_PASSWORD,
82     "Url": 'https://%s:%s%s' %(plcConfig.PLC_API_HOST, plcConfig.PLC_API_PORT, plcConfig.PLC_API_PATH)
83     }
84
85     return pl_auth