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