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