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