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