8970a82a984a3a4a0a9a8debfad087a938c26413
[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 from geni.util.debug import log
20 import traceback
21
22 geni =  os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))+ os.sep + "geni"
23
24 class Config:
25     """
26     Parse the bash/Python/PHP version of the configuration file. Very
27     fast but no type conversions.
28     """
29
30     def __init__(self, file = "/etc/geni/geni_config"):
31         # Load plc_config
32
33         loaded = False
34         path = os.path.dirname(os.path.abspath(__file__))
35         self.path = path
36         filename = file.split(os.sep)[-1]
37         alt_file = path + os.sep + 'util' + os.sep + filename
38         geni_file = geni + os.sep + 'util' + os.sep + filename
39         files = [file, alt_file, geni_file]
40
41         for file in files:
42             try:
43                 execfile(file, self.__dict__)
44                 loaded = True
45                 self.config_file = file
46                 break
47             except:
48                 pass
49
50         if not loaded:
51             raise Exception, "Could not find config in " + ", ".join(files)
52
53         # set up some useful variables
54
55     def load(self, file):
56         try:
57             execfile(file, self.__dict__)
58         except:
59             raise Exception, "Could not find config in " + file
60
61 plcConfig = Config("/etc/planetlab/plc_config")
62
63 def get_default_dbinfo():
64     dbinfo={ 'dbname' : plcConfig.PLC_DB_NAME,
65     'address' : plcConfig.PLC_DB_HOST,
66     'port' : plcConfig.PLC_DB_PORT,
67     'user' : plcConfig.PLC_DB_USER,
68     'password' : plcConfig.PLC_DB_PASSWORD
69         }
70
71     return dbinfo
72
73 ##
74 # Geniwrapper uses a PLCAPI connection to perform operations on the registry,
75 # such as creating and deleting slices. This connection requires an account
76 # on the PLC server with full administrator access.
77 #
78 # The Url parameter controls whether the connection uses PLCAPI directly (i.e.
79 # Geniwrapper is located on the same machine as PLC), or uses a XMLRPC connection
80 # to the PLC machine. If you wish to use the API directly, then remove the Url
81 # field from the dictionary. 
82
83 def get_pl_auth():
84     pl_auth = {'Username': plcConfig.PLC_API_MAINTENANCE_USER,
85     'AuthMethod': 'capability',
86     'AuthString':  plcConfig.PLC_API_MAINTENANCE_PASSWORD,
87     "Url": 'https://%s:%s%s' %(plcConfig.PLC_API_HOST, plcConfig.PLC_API_PORT, plcConfig.PLC_API_PATH)
88     }
89
90     return pl_auth