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