use self.get_auth_info
[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 ### $Id$
18 ### $URL$
19
20 import os.path
21 import traceback
22
23 from geni.util.debug import log
24
25 # xxx the path-search part could use a cleanup; 
26 # why would anyone want to store the config in /usr/share/geniwrapper at all ?
27 # also, if users want to use this, it might help to store stuff in ~/.sfirc or something
28
29 # this would denote "/usr/share/geniwrapper/geni"
30 # geni =  join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "geni")
31
32 class Config:
33     """
34     Parse the bash/Python/PHP version of the configuration file. Very
35     fast but no type conversions.
36     """
37
38     def __init__(self, filepath = "/etc/sfa/sfa_config"):
39         # Load plc_config
40
41         loaded = False
42         # path to config.py source - this would be '/usr/share/geniwrapper/geni/util'
43         path = os.path.dirname(os.path.abspath(__file__))
44         # parent directory of config.py source
45         self.basepath = os.path.dirname(path)
46         # path to actual config file
47         filename = os.path.basename(filepath)
48         alt_file = os.path.join(path, 'util', filename)
49         files = [filepath, alt_file]
50
51         for config_file in files:
52             try:
53                 execfile(config_file, self.__dict__)
54                 loaded = True
55                 self.config_file = config_file
56                 self.config_path = os.path.dirname(config_file)
57                 break
58             except:
59                 pass
60
61         if not loaded:
62             raise Exception, "Could not find config in " + ", ".join(files)
63
64         # set up some useful variables
65
66     def load(self, filepath):
67         try:
68             execfile(filepath, self.__dict__)
69         except:
70             raise Exception, "Could not find config in " + filepath
71
72 plcConfig = Config("/etc/planetlab/plc_config")
73
74 def get_default_dbinfo():
75     dbinfo={ 'dbname' : plcConfig.PLC_DB_NAME,
76     'address' : plcConfig.PLC_DB_HOST,
77     'port' : plcConfig.PLC_DB_PORT,
78     'user' : plcConfig.PLC_DB_USER,
79     'password' : plcConfig.PLC_DB_PASSWORD
80         }
81
82     return dbinfo
83
84 ##
85 # Geniwrapper uses a PLCAPI connection to perform operations on the registry,
86 # such as creating and deleting slices. This connection requires an account
87 # on the PLC server with full administrator access.
88 #
89 # The Url parameter controls whether the connection uses PLCAPI directly (i.e.
90 # Geniwrapper is located on the same machine as PLC), or uses a XMLRPC connection
91 # to the PLC machine. If you wish to use the API directly, then remove the Url
92 # field from the dictionary. 
93
94 def get_pl_auth():
95     pl_auth = {'Username': plcConfig.PLC_API_MAINTENANCE_USER,
96                'AuthMethod': 'capability',
97                'AuthString':  plcConfig.PLC_API_MAINTENANCE_PASSWORD,
98                "Url": 'https://%s:%s%s' %(plcConfig.PLC_API_HOST, plcConfig.PLC_API_PORT, plcConfig.PLC_API_PATH)
99                }
100
101     return pl_auth