modified load() to load confg elements as dicts, not lists
[tests.git] / qaapi / qa / Config.py
1 import xmlrpclib
2 import os
3 import sys
4 import re
5 import socket
6 import utils
7 import copy
8 from PLCs import PLC, PLCs
9 from Sites import Site, Sites   
10 from Nodes import Node, Nodes
11 from Slices import Slice, Slices
12 from Persons import Person, Persons     
13
14 class Config:
15
16     path = os.path.dirname(os.path.abspath(__file__))
17     tests_path = path + os.sep + 'tests' + os.sep
18     node_tests_path = tests_path + os.sep + 'node' + os.sep
19     slice_tests_path = tests_path + os.sep + 'slice' + os.sep                           
20     vserver_scripts_path = path + os.sep + 'vserver' + os.sep
21     qemu_scripts_path = path + os.sep + 'qemu' + os.sep 
22
23     def update_api(self, plc = None):
24         # Set up API acccess
25         # If plc is specified, find its configuration
26         # and use its API
27         if plc is not None:
28             host, path, port  = plc['host'], plc['api_path'], plc['port']
29             api_server = "https://%(host)s:%(port)s/%(path)s" % locals()
30             self.api = xmlrpclib.Server(api_server, allow_none = 1)
31             self.api_type = 'xmlrpc'    
32         else:
33
34             # Try importing the API shell for direct api access.
35             # If that fails fall back to using xmlrpm
36             try:
37                 sys.path.append('/usr/share/plc_api')
38                 from PLC.Shell import Shell
39                 shell = Shell(globals = globals())
40             
41                 # test it
42                 shell.GetRoles()
43                 self.api = shell
44                 self.api_type = 'direct'
45             except:
46                 self.api = xmlrpclib.Server('https://%s/PLCAPI/' % self.PLC_API_HOST, allow_none = 1)
47                 self.api_type = 'xmlrpc'
48
49     def __init__(self, config_file = path+os.sep+'qa_config'):
50         # Load config file
51         try:
52             execfile(config_file, self.__dict__)
53         except:
54             raise "Could not find system config in %s" % config_file
55
56         self.auth = {}
57         self.auth['Username'] = self.PLC_ROOT_USER
58         self.auth['AuthString'] = self.PLC_ROOT_PASSWORD
59         self.auth['AuthMethod'] = 'password'
60         self.verbose = self.VERBOSE     
61         
62         # try setting hostname and ip
63         self.hostname = socket.gethostname()
64         try:
65             (stdout, stderr) = utils.popen("/sbin/ifconfig eth0 | grep 'inet addr'")
66             inet_addr = re.findall('inet addr:[0-9\.^\w]*', stdout[0])[0]
67             parts = inet_addr.split(":")
68             self.ip = parts[1]
69         except:
70             self.ip = '127.0.0.1'
71         
72         api_host = self.__dict__.get("PLC_API_HOST",self.ip)
73         self.PLC_API_HOST=api_host
74
75         self.update_api()
76
77         # Load list of node tests
78         valid_node_test_files = lambda name: not name.startswith('__init__') \
79                                              and not name.endswith('pyc')
80         node_test_files = os.listdir(self.node_tests_path)
81         self.node_test_files = filter(valid_node_test_files, node_test_files) 
82
83     def get_plc(self, plc_name):
84         plc = PLC(self)
85         if hasattr(self, 'plcs')  and plc_name in self.plcs.keys():
86             plc.update(self.plcs[plc_name])
87         return plc
88
89     def get_node(self, hostname):
90         node = Node(self)
91         if hasattr(self, 'nodes') and hostname in self.nodes.keys():
92             node.update(self.nodes[hostname])
93         return node                     
94
95     def load(self, conffile):
96         
97         confdata = {}
98         try: execfile(conffile, confdata)
99         except: raise 
100
101         from Nodes import Nodes
102         from PLCs import PLCs   
103         loadables = ['plcs', 'sites', 'nodes', 'slices', 'persons']
104         config = Config()
105         for loadable in loadables:
106             if loadable in confdata and loadable in ['plcs']:
107                 setattr(self, loadable, PLCs(config, confdata[loadable]).dict('name'))
108             elif loadable in confdata and loadable in ['nodes']:
109                 setattr(self, loadable, Nodes(config, confdata[loadable]).dict('hostname'))             
110             elif loadable in confdata and loadable in ['sites']:
111                 setattr(self, loadable, Sites(confdata[loadable]).dict('login_base'))
112             elif loadable in confdata and loadable in ['slices']:
113                 setattr(self, loadable, Slices(confdata[loadable]).dict('name'))
114             elif loadable in confdata and loadable in ['persons']:
115                 setattr(self, loadable, Persons(confdata[loadable]).dict('email')) 
116             
117